2 * Common framework for low-level network console, dump, and debugger code
4 * Sep 8 2003 Matt Mackall <mpm@selenic.com>
6 * based on the netconsole code from:
8 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2002 Red Hat, Inc.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/moduleparam.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/string.h>
19 #include <linux/if_arp.h>
20 #include <linux/inetdevice.h>
21 #include <linux/inet.h>
22 #include <linux/interrupt.h>
23 #include <linux/netpoll.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/rcupdate.h>
27 #include <linux/workqueue.h>
28 #include <linux/slab.h>
29 #include <linux/export.h>
30 #include <linux/if_vlan.h>
34 #include <net/addrconf.h>
35 #include <net/ndisc.h>
36 #include <net/ip6_checksum.h>
37 #include <asm/unaligned.h>
38 #include <trace/events/napi.h>
41 * We maintain a small pool of fully-sized skbs, to make sure the
42 * message gets out even in extreme OOM situations.
45 #define MAX_UDP_CHUNK 1460
48 static struct sk_buff_head skb_pool;
50 DEFINE_STATIC_SRCU(netpoll_srcu);
52 #define USEC_PER_POLL 50
54 #define MAX_SKB_SIZE \
55 (sizeof(struct ethhdr) + \
56 sizeof(struct iphdr) + \
57 sizeof(struct udphdr) + \
60 static void zap_completion_queue(void);
61 static void netpoll_async_cleanup(struct work_struct *work);
63 static unsigned int carrier_timeout = 4;
64 module_param(carrier_timeout, uint, 0644);
66 #define np_info(np, fmt, ...) \
67 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
68 #define np_err(np, fmt, ...) \
69 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
70 #define np_notice(np, fmt, ...) \
71 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
73 static int netpoll_start_xmit(struct sk_buff *skb, struct net_device *dev,
74 struct netdev_queue *txq)
76 int status = NETDEV_TX_OK;
77 netdev_features_t features;
79 features = netif_skb_features(skb);
81 if (skb_vlan_tag_present(skb) &&
82 !vlan_hw_offload_capable(features, skb->vlan_proto)) {
83 skb = __vlan_hwaccel_push_inside(skb);
85 /* This is actually a packet drop, but we
86 * don't want the code that calls this
87 * function to try and operate on a NULL skb.
93 status = netdev_start_xmit(skb, dev, txq, false);
99 static void queue_process(struct work_struct *work)
101 struct netpoll_info *npinfo =
102 container_of(work, struct netpoll_info, tx_work.work);
106 while ((skb = skb_dequeue(&npinfo->txq))) {
107 struct net_device *dev = skb->dev;
108 struct netdev_queue *txq;
109 unsigned int q_index;
111 if (!netif_device_present(dev) || !netif_running(dev)) {
116 local_irq_save(flags);
117 /* check if skb->queue_mapping is still valid */
118 q_index = skb_get_queue_mapping(skb);
119 if (unlikely(q_index >= dev->real_num_tx_queues)) {
120 q_index = q_index % dev->real_num_tx_queues;
121 skb_set_queue_mapping(skb, q_index);
123 txq = netdev_get_tx_queue(dev, q_index);
124 HARD_TX_LOCK(dev, txq, smp_processor_id());
125 if (netif_xmit_frozen_or_stopped(txq) ||
126 !dev_xmit_complete(netpoll_start_xmit(skb, dev, txq))) {
127 skb_queue_head(&npinfo->txq, skb);
128 HARD_TX_UNLOCK(dev, txq);
129 local_irq_restore(flags);
131 schedule_delayed_work(&npinfo->tx_work, HZ/10);
134 HARD_TX_UNLOCK(dev, txq);
135 local_irq_restore(flags);
139 static void poll_one_napi(struct napi_struct *napi)
143 /* If we set this bit but see that it has already been set,
144 * that indicates that napi has been disabled and we need
145 * to abort this operation
147 if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
150 /* We explicilty pass the polling call a budget of 0 to
151 * indicate that we are clearing the Tx path only.
153 work = napi->poll(napi, 0);
154 WARN_ONCE(work, "%pF exceeded budget in poll\n", napi->poll);
155 trace_napi_poll(napi, work, 0);
157 clear_bit(NAPI_STATE_NPSVC, &napi->state);
160 static void poll_napi(struct net_device *dev)
162 struct napi_struct *napi;
163 int cpu = smp_processor_id();
165 list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
166 if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) {
168 smp_store_release(&napi->poll_owner, -1);
173 void netpoll_poll_dev(struct net_device *dev)
175 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
176 const struct net_device_ops *ops;
178 /* Don't do any rx activity if the dev_lock mutex is held
179 * the dev_open/close paths use this to block netpoll activity
180 * while changing device state
182 if (!ni || down_trylock(&ni->dev_lock))
185 if (!netif_running(dev)) {
190 ops = dev->netdev_ops;
191 if (ops->ndo_poll_controller)
192 ops->ndo_poll_controller(dev);
198 zap_completion_queue();
200 EXPORT_SYMBOL(netpoll_poll_dev);
202 void netpoll_poll_disable(struct net_device *dev)
204 struct netpoll_info *ni;
207 idx = srcu_read_lock(&netpoll_srcu);
208 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
211 srcu_read_unlock(&netpoll_srcu, idx);
213 EXPORT_SYMBOL(netpoll_poll_disable);
215 void netpoll_poll_enable(struct net_device *dev)
217 struct netpoll_info *ni;
219 ni = rcu_dereference(dev->npinfo);
224 EXPORT_SYMBOL(netpoll_poll_enable);
226 static void refill_skbs(void)
231 spin_lock_irqsave(&skb_pool.lock, flags);
232 while (skb_pool.qlen < MAX_SKBS) {
233 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
237 __skb_queue_tail(&skb_pool, skb);
239 spin_unlock_irqrestore(&skb_pool.lock, flags);
242 static void zap_completion_queue(void)
245 struct softnet_data *sd = &get_cpu_var(softnet_data);
247 if (sd->completion_queue) {
248 struct sk_buff *clist;
250 local_irq_save(flags);
251 clist = sd->completion_queue;
252 sd->completion_queue = NULL;
253 local_irq_restore(flags);
255 while (clist != NULL) {
256 struct sk_buff *skb = clist;
258 if (!skb_irq_freeable(skb)) {
259 refcount_set(&skb->users, 1);
260 dev_kfree_skb_any(skb); /* put this one back */
267 put_cpu_var(softnet_data);
270 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
275 zap_completion_queue();
279 skb = alloc_skb(len, GFP_ATOMIC);
281 skb = skb_dequeue(&skb_pool);
285 netpoll_poll_dev(np->dev);
291 refcount_set(&skb->users, 1);
292 skb_reserve(skb, reserve);
296 static int netpoll_owner_active(struct net_device *dev)
298 struct napi_struct *napi;
300 list_for_each_entry(napi, &dev->napi_list, dev_list) {
301 if (napi->poll_owner == smp_processor_id())
307 /* call with IRQ disabled */
308 void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
309 struct net_device *dev)
311 int status = NETDEV_TX_BUSY;
313 /* It is up to the caller to keep npinfo alive. */
314 struct netpoll_info *npinfo;
316 lockdep_assert_irqs_disabled();
318 npinfo = rcu_dereference_bh(np->dev->npinfo);
319 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
320 dev_kfree_skb_irq(skb);
324 /* don't get messages out of order, and no recursion */
325 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
326 struct netdev_queue *txq;
328 txq = netdev_pick_tx(dev, skb, NULL);
330 /* try until next clock tick */
331 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
332 tries > 0; --tries) {
333 if (HARD_TX_TRYLOCK(dev, txq)) {
334 if (!netif_xmit_stopped(txq))
335 status = netpoll_start_xmit(skb, dev, txq);
337 HARD_TX_UNLOCK(dev, txq);
339 if (dev_xmit_complete(status))
344 /* tickle device maybe there is some cleanup */
345 netpoll_poll_dev(np->dev);
347 udelay(USEC_PER_POLL);
350 WARN_ONCE(!irqs_disabled(),
351 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
352 dev->name, dev->netdev_ops->ndo_start_xmit);
356 if (!dev_xmit_complete(status)) {
357 skb_queue_tail(&npinfo->txq, skb);
358 schedule_delayed_work(&npinfo->tx_work,0);
361 EXPORT_SYMBOL(netpoll_send_skb_on_dev);
363 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
365 int total_len, ip_len, udp_len;
370 static atomic_t ip_ident;
371 struct ipv6hdr *ip6h;
373 WARN_ON_ONCE(!irqs_disabled());
375 udp_len = len + sizeof(*udph);
377 ip_len = udp_len + sizeof(*ip6h);
379 ip_len = udp_len + sizeof(*iph);
381 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
383 skb = find_skb(np, total_len + np->dev->needed_tailroom,
388 skb_copy_to_linear_data(skb, msg, len);
391 skb_push(skb, sizeof(*udph));
392 skb_reset_transport_header(skb);
394 udph->source = htons(np->local_port);
395 udph->dest = htons(np->remote_port);
396 udph->len = htons(udp_len);
400 udph->check = csum_ipv6_magic(&np->local_ip.in6,
402 udp_len, IPPROTO_UDP,
403 csum_partial(udph, udp_len, 0));
404 if (udph->check == 0)
405 udph->check = CSUM_MANGLED_0;
407 skb_push(skb, sizeof(*ip6h));
408 skb_reset_network_header(skb);
409 ip6h = ipv6_hdr(skb);
411 /* ip6h->version = 6; ip6h->priority = 0; */
412 put_unaligned(0x60, (unsigned char *)ip6h);
413 ip6h->flow_lbl[0] = 0;
414 ip6h->flow_lbl[1] = 0;
415 ip6h->flow_lbl[2] = 0;
417 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
418 ip6h->nexthdr = IPPROTO_UDP;
419 ip6h->hop_limit = 32;
420 ip6h->saddr = np->local_ip.in6;
421 ip6h->daddr = np->remote_ip.in6;
423 eth = skb_push(skb, ETH_HLEN);
424 skb_reset_mac_header(skb);
425 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
428 udph->check = csum_tcpudp_magic(np->local_ip.ip,
430 udp_len, IPPROTO_UDP,
431 csum_partial(udph, udp_len, 0));
432 if (udph->check == 0)
433 udph->check = CSUM_MANGLED_0;
435 skb_push(skb, sizeof(*iph));
436 skb_reset_network_header(skb);
439 /* iph->version = 4; iph->ihl = 5; */
440 put_unaligned(0x45, (unsigned char *)iph);
442 put_unaligned(htons(ip_len), &(iph->tot_len));
443 iph->id = htons(atomic_inc_return(&ip_ident));
446 iph->protocol = IPPROTO_UDP;
448 put_unaligned(np->local_ip.ip, &(iph->saddr));
449 put_unaligned(np->remote_ip.ip, &(iph->daddr));
450 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
452 eth = skb_push(skb, ETH_HLEN);
453 skb_reset_mac_header(skb);
454 skb->protocol = eth->h_proto = htons(ETH_P_IP);
457 ether_addr_copy(eth->h_source, np->dev->dev_addr);
458 ether_addr_copy(eth->h_dest, np->remote_mac);
462 netpoll_send_skb(np, skb);
464 EXPORT_SYMBOL(netpoll_send_udp);
466 void netpoll_print_options(struct netpoll *np)
468 np_info(np, "local port %d\n", np->local_port);
470 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
472 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
473 np_info(np, "interface '%s'\n", np->dev_name);
474 np_info(np, "remote port %d\n", np->remote_port);
476 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
478 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
479 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
481 EXPORT_SYMBOL(netpoll_print_options);
483 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
487 if (!strchr(str, ':') &&
488 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
492 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
493 #if IS_ENABLED(CONFIG_IPV6)
503 int netpoll_parse_options(struct netpoll *np, char *opt)
505 char *cur=opt, *delim;
507 bool ipversion_set = false;
510 if ((delim = strchr(cur, '@')) == NULL)
513 if (kstrtou16(cur, 10, &np->local_port))
520 ipversion_set = true;
521 if ((delim = strchr(cur, '/')) == NULL)
524 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
528 np->ipv6 = (bool)ipv6;
534 /* parse out dev name */
535 if ((delim = strchr(cur, ',')) == NULL)
538 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
545 if ((delim = strchr(cur, '@')) == NULL)
548 if (*cur == ' ' || *cur == '\t')
549 np_info(np, "warning: whitespace is not allowed\n");
550 if (kstrtou16(cur, 10, &np->remote_port))
557 if ((delim = strchr(cur, '/')) == NULL)
560 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
563 else if (ipversion_set && np->ipv6 != (bool)ipv6)
566 np->ipv6 = (bool)ipv6;
571 if (!mac_pton(cur, np->remote_mac))
575 netpoll_print_options(np);
580 np_info(np, "couldn't parse config at '%s'!\n", cur);
583 EXPORT_SYMBOL(netpoll_parse_options);
585 int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
587 struct netpoll_info *npinfo;
588 const struct net_device_ops *ops;
592 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
593 INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
595 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
596 np_err(np, "%s doesn't support polling, aborting\n",
603 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
609 sema_init(&npinfo->dev_lock, 1);
610 skb_queue_head_init(&npinfo->txq);
611 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
613 refcount_set(&npinfo->refcnt, 1);
615 ops = np->dev->netdev_ops;
616 if (ops->ndo_netpoll_setup) {
617 err = ops->ndo_netpoll_setup(ndev, npinfo);
622 npinfo = rtnl_dereference(ndev->npinfo);
623 refcount_inc(&npinfo->refcnt);
626 npinfo->netpoll = np;
628 /* last thing to do is link it to the net device structure */
629 rcu_assign_pointer(ndev->npinfo, npinfo);
638 EXPORT_SYMBOL_GPL(__netpoll_setup);
640 int netpoll_setup(struct netpoll *np)
642 struct net_device *ndev = NULL, *dev = NULL;
643 struct net *net = current->nsproxy->net_ns;
644 struct in_device *in_dev;
649 ndev = __dev_get_by_name(net, np->dev_name);
652 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
658 /* bring up DSA management network devices up first */
659 for_each_netdev(net, dev) {
660 if (!netdev_uses_dsa(dev))
663 err = dev_change_flags(dev, dev->flags | IFF_UP);
665 np_err(np, "%s failed to open %s\n",
666 np->dev_name, dev->name);
671 if (netdev_master_upper_dev_get(ndev)) {
672 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
677 if (!netif_running(ndev)) {
678 unsigned long atmost, atleast;
680 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
682 err = dev_open(ndev);
685 np_err(np, "failed to open %s\n", ndev->name);
690 atleast = jiffies + HZ/10;
691 atmost = jiffies + carrier_timeout * HZ;
692 while (!netif_carrier_ok(ndev)) {
693 if (time_after(jiffies, atmost)) {
694 np_notice(np, "timeout waiting for carrier\n");
700 /* If carrier appears to come up instantly, we don't
701 * trust it and pause so that we don't pump all our
702 * queued console messages into the bitbucket.
705 if (time_before(jiffies, atleast)) {
706 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
712 if (!np->local_ip.ip) {
714 in_dev = __in_dev_get_rtnl(ndev);
716 if (!in_dev || !in_dev->ifa_list) {
717 np_err(np, "no IP address for %s, aborting\n",
723 np->local_ip.ip = in_dev->ifa_list->ifa_local;
724 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
726 #if IS_ENABLED(CONFIG_IPV6)
727 struct inet6_dev *idev;
730 idev = __in6_dev_get(ndev);
732 struct inet6_ifaddr *ifp;
734 read_lock_bh(&idev->lock);
735 list_for_each_entry(ifp, &idev->addr_list, if_list) {
736 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
738 np->local_ip.in6 = ifp->addr;
742 read_unlock_bh(&idev->lock);
745 np_err(np, "no IPv6 address for %s, aborting\n",
749 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
751 np_err(np, "IPv6 is not supported %s, aborting\n",
759 /* fill up the skb queue */
762 err = __netpoll_setup(np, ndev);
775 EXPORT_SYMBOL(netpoll_setup);
777 static int __init netpoll_init(void)
779 skb_queue_head_init(&skb_pool);
782 core_initcall(netpoll_init);
784 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
786 struct netpoll_info *npinfo =
787 container_of(rcu_head, struct netpoll_info, rcu);
789 skb_queue_purge(&npinfo->txq);
791 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
792 cancel_delayed_work(&npinfo->tx_work);
794 /* clean after last, unfinished work */
795 __skb_queue_purge(&npinfo->txq);
796 /* now cancel it again */
797 cancel_delayed_work(&npinfo->tx_work);
801 void __netpoll_cleanup(struct netpoll *np)
803 struct netpoll_info *npinfo;
805 /* rtnl_dereference would be preferable here but
806 * rcu_cleanup_netpoll path can put us in here safely without
807 * holding the rtnl, so plain rcu_dereference it is
809 npinfo = rtnl_dereference(np->dev->npinfo);
813 synchronize_srcu(&netpoll_srcu);
815 if (refcount_dec_and_test(&npinfo->refcnt)) {
816 const struct net_device_ops *ops;
818 ops = np->dev->netdev_ops;
819 if (ops->ndo_netpoll_cleanup)
820 ops->ndo_netpoll_cleanup(np->dev);
822 RCU_INIT_POINTER(np->dev->npinfo, NULL);
823 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
825 RCU_INIT_POINTER(np->dev->npinfo, NULL);
827 EXPORT_SYMBOL_GPL(__netpoll_cleanup);
829 static void netpoll_async_cleanup(struct work_struct *work)
831 struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
834 __netpoll_cleanup(np);
839 void __netpoll_free_async(struct netpoll *np)
841 schedule_work(&np->cleanup_work);
843 EXPORT_SYMBOL_GPL(__netpoll_free_async);
845 void netpoll_cleanup(struct netpoll *np)
850 __netpoll_cleanup(np);
856 EXPORT_SYMBOL(netpoll_cleanup);