1 // SPDX-License-Identifier: GPL-2.0-only
3 * Common framework for low-level network console, dump, and debugger code
7 * based on the netconsole code from:
10 * Copyright (C) 2002 Red Hat, Inc.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/moduleparam.h>
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/string.h>
20 #include <linux/if_arp.h>
21 #include <linux/inetdevice.h>
22 #include <linux/inet.h>
23 #include <linux/interrupt.h>
24 #include <linux/netpoll.h>
25 #include <linux/sched.h>
26 #include <linux/delay.h>
27 #include <linux/rcupdate.h>
28 #include <linux/workqueue.h>
29 #include <linux/slab.h>
30 #include <linux/export.h>
31 #include <linux/if_vlan.h>
34 #include <net/addrconf.h>
35 #include <net/ndisc.h>
36 #include <net/ip6_checksum.h>
37 #include <linux/unaligned.h>
38 #include <trace/events/napi.h>
39 #include <linux/kconfig.h>
42 * We maintain a small pool of fully-sized skbs, to make sure the
43 * message gets out even in extreme OOM situations.
46 #define MAX_UDP_CHUNK 1460
49 static struct sk_buff_head skb_pool;
51 #define USEC_PER_POLL 50
53 #define MAX_SKB_SIZE \
54 (sizeof(struct ethhdr) + \
55 sizeof(struct iphdr) + \
56 sizeof(struct udphdr) + \
59 static void zap_completion_queue(void);
61 static unsigned int carrier_timeout = 4;
62 module_param(carrier_timeout, uint, 0644);
64 #define np_info(np, fmt, ...) \
65 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
66 #define np_err(np, fmt, ...) \
67 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
68 #define np_notice(np, fmt, ...) \
69 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
71 static netdev_tx_t netpoll_start_xmit(struct sk_buff *skb,
72 struct net_device *dev,
73 struct netdev_queue *txq)
75 netdev_tx_t status = NETDEV_TX_OK;
76 netdev_features_t features;
78 features = netif_skb_features(skb);
80 if (skb_vlan_tag_present(skb) &&
81 !vlan_hw_offload_capable(features, skb->vlan_proto)) {
82 skb = __vlan_hwaccel_push_inside(skb);
84 /* This is actually a packet drop, but we
85 * don't want the code that calls this
86 * function to try and operate on a NULL skb.
92 status = netdev_start_xmit(skb, dev, txq, false);
98 static void queue_process(struct work_struct *work)
100 struct netpoll_info *npinfo =
101 container_of(work, struct netpoll_info, tx_work.work);
105 while ((skb = skb_dequeue(&npinfo->txq))) {
106 struct net_device *dev = skb->dev;
107 struct netdev_queue *txq;
108 unsigned int q_index;
110 if (!netif_device_present(dev) || !netif_running(dev)) {
115 local_irq_save(flags);
116 /* check if skb->queue_mapping is still valid */
117 q_index = skb_get_queue_mapping(skb);
118 if (unlikely(q_index >= dev->real_num_tx_queues)) {
119 q_index = q_index % dev->real_num_tx_queues;
120 skb_set_queue_mapping(skb, q_index);
122 txq = netdev_get_tx_queue(dev, q_index);
123 HARD_TX_LOCK(dev, txq, smp_processor_id());
124 if (netif_xmit_frozen_or_stopped(txq) ||
125 !dev_xmit_complete(netpoll_start_xmit(skb, dev, txq))) {
126 skb_queue_head(&npinfo->txq, skb);
127 HARD_TX_UNLOCK(dev, txq);
128 local_irq_restore(flags);
130 schedule_delayed_work(&npinfo->tx_work, HZ/10);
133 HARD_TX_UNLOCK(dev, txq);
134 local_irq_restore(flags);
138 static int netif_local_xmit_active(struct net_device *dev)
142 for (i = 0; i < dev->num_tx_queues; i++) {
143 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
145 if (READ_ONCE(txq->xmit_lock_owner) == smp_processor_id())
152 static void poll_one_napi(struct napi_struct *napi)
156 /* If we set this bit but see that it has already been set,
157 * that indicates that napi has been disabled and we need
158 * to abort this operation
160 if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
163 /* We explicitly pass the polling call a budget of 0 to
164 * indicate that we are clearing the Tx path only.
166 work = napi->poll(napi, 0);
167 WARN_ONCE(work, "%pS exceeded budget in poll\n", napi->poll);
168 trace_napi_poll(napi, work, 0);
170 clear_bit(NAPI_STATE_NPSVC, &napi->state);
173 static void poll_napi(struct net_device *dev)
175 struct napi_struct *napi;
176 int cpu = smp_processor_id();
178 list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
179 if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) {
181 smp_store_release(&napi->poll_owner, -1);
186 void netpoll_poll_dev(struct net_device *dev)
188 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
189 const struct net_device_ops *ops;
191 /* Don't do any rx activity if the dev_lock mutex is held
192 * the dev_open/close paths use this to block netpoll activity
193 * while changing device state
195 if (!ni || down_trylock(&ni->dev_lock))
198 /* Some drivers will take the same locks in poll and xmit,
199 * we can't poll if local CPU is already in xmit.
201 if (!netif_running(dev) || netif_local_xmit_active(dev)) {
206 ops = dev->netdev_ops;
207 if (ops->ndo_poll_controller)
208 ops->ndo_poll_controller(dev);
214 zap_completion_queue();
216 EXPORT_SYMBOL(netpoll_poll_dev);
218 void netpoll_poll_disable(struct net_device *dev)
220 struct netpoll_info *ni;
223 ni = rtnl_dereference(dev->npinfo);
228 void netpoll_poll_enable(struct net_device *dev)
230 struct netpoll_info *ni;
232 ni = rtnl_dereference(dev->npinfo);
237 static void refill_skbs(void)
242 spin_lock_irqsave(&skb_pool.lock, flags);
243 while (skb_pool.qlen < MAX_SKBS) {
244 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
248 __skb_queue_tail(&skb_pool, skb);
250 spin_unlock_irqrestore(&skb_pool.lock, flags);
253 static void zap_completion_queue(void)
256 struct softnet_data *sd = &get_cpu_var(softnet_data);
258 if (sd->completion_queue) {
259 struct sk_buff *clist;
261 local_irq_save(flags);
262 clist = sd->completion_queue;
263 sd->completion_queue = NULL;
264 local_irq_restore(flags);
266 while (clist != NULL) {
267 struct sk_buff *skb = clist;
269 if (!skb_irq_freeable(skb)) {
270 refcount_set(&skb->users, 1);
271 dev_kfree_skb_any(skb); /* put this one back */
278 put_cpu_var(softnet_data);
281 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
286 zap_completion_queue();
290 skb = alloc_skb(len, GFP_ATOMIC);
292 skb = skb_dequeue(&skb_pool);
296 netpoll_poll_dev(np->dev);
302 refcount_set(&skb->users, 1);
303 skb_reserve(skb, reserve);
307 static int netpoll_owner_active(struct net_device *dev)
309 struct napi_struct *napi;
311 list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
312 if (READ_ONCE(napi->poll_owner) == smp_processor_id())
318 /* call with IRQ disabled */
319 static netdev_tx_t __netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
321 netdev_tx_t status = NETDEV_TX_BUSY;
322 struct net_device *dev;
324 /* It is up to the caller to keep npinfo alive. */
325 struct netpoll_info *npinfo;
327 lockdep_assert_irqs_disabled();
330 npinfo = rcu_dereference_bh(dev->npinfo);
332 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
333 dev_kfree_skb_irq(skb);
334 return NET_XMIT_DROP;
337 /* don't get messages out of order, and no recursion */
338 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
339 struct netdev_queue *txq;
341 txq = netdev_core_pick_tx(dev, skb, NULL);
343 /* try until next clock tick */
344 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
345 tries > 0; --tries) {
346 if (HARD_TX_TRYLOCK(dev, txq)) {
347 if (!netif_xmit_stopped(txq))
348 status = netpoll_start_xmit(skb, dev, txq);
350 HARD_TX_UNLOCK(dev, txq);
352 if (dev_xmit_complete(status))
357 /* tickle device maybe there is some cleanup */
358 netpoll_poll_dev(np->dev);
360 udelay(USEC_PER_POLL);
363 WARN_ONCE(!irqs_disabled(),
364 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n",
365 dev->name, dev->netdev_ops->ndo_start_xmit);
369 if (!dev_xmit_complete(status)) {
370 skb_queue_tail(&npinfo->txq, skb);
371 schedule_delayed_work(&npinfo->tx_work,0);
376 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
382 dev_kfree_skb_irq(skb);
385 local_irq_save(flags);
386 ret = __netpoll_send_skb(np, skb);
387 local_irq_restore(flags);
391 EXPORT_SYMBOL(netpoll_send_skb);
393 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
395 int total_len, ip_len, udp_len;
400 static atomic_t ip_ident;
401 struct ipv6hdr *ip6h;
403 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
404 WARN_ON_ONCE(!irqs_disabled());
406 udp_len = len + sizeof(*udph);
408 ip_len = udp_len + sizeof(*ip6h);
410 ip_len = udp_len + sizeof(*iph);
412 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
414 skb = find_skb(np, total_len + np->dev->needed_tailroom,
419 skb_copy_to_linear_data(skb, msg, len);
422 skb_push(skb, sizeof(*udph));
423 skb_reset_transport_header(skb);
425 udph->source = htons(np->local_port);
426 udph->dest = htons(np->remote_port);
427 udph->len = htons(udp_len);
431 udph->check = csum_ipv6_magic(&np->local_ip.in6,
433 udp_len, IPPROTO_UDP,
434 csum_partial(udph, udp_len, 0));
435 if (udph->check == 0)
436 udph->check = CSUM_MANGLED_0;
438 skb_push(skb, sizeof(*ip6h));
439 skb_reset_network_header(skb);
440 ip6h = ipv6_hdr(skb);
442 /* ip6h->version = 6; ip6h->priority = 0; */
443 *(unsigned char *)ip6h = 0x60;
444 ip6h->flow_lbl[0] = 0;
445 ip6h->flow_lbl[1] = 0;
446 ip6h->flow_lbl[2] = 0;
448 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
449 ip6h->nexthdr = IPPROTO_UDP;
450 ip6h->hop_limit = 32;
451 ip6h->saddr = np->local_ip.in6;
452 ip6h->daddr = np->remote_ip.in6;
454 eth = skb_push(skb, ETH_HLEN);
455 skb_reset_mac_header(skb);
456 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
459 udph->check = csum_tcpudp_magic(np->local_ip.ip,
461 udp_len, IPPROTO_UDP,
462 csum_partial(udph, udp_len, 0));
463 if (udph->check == 0)
464 udph->check = CSUM_MANGLED_0;
466 skb_push(skb, sizeof(*iph));
467 skb_reset_network_header(skb);
470 /* iph->version = 4; iph->ihl = 5; */
471 *(unsigned char *)iph = 0x45;
473 put_unaligned(htons(ip_len), &(iph->tot_len));
474 iph->id = htons(atomic_inc_return(&ip_ident));
477 iph->protocol = IPPROTO_UDP;
479 put_unaligned(np->local_ip.ip, &(iph->saddr));
480 put_unaligned(np->remote_ip.ip, &(iph->daddr));
481 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
483 eth = skb_push(skb, ETH_HLEN);
484 skb_reset_mac_header(skb);
485 skb->protocol = eth->h_proto = htons(ETH_P_IP);
488 ether_addr_copy(eth->h_source, np->dev->dev_addr);
489 ether_addr_copy(eth->h_dest, np->remote_mac);
493 netpoll_send_skb(np, skb);
495 EXPORT_SYMBOL(netpoll_send_udp);
497 void netpoll_print_options(struct netpoll *np)
499 np_info(np, "local port %d\n", np->local_port);
501 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
503 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
504 np_info(np, "interface '%s'\n", np->dev_name);
505 np_info(np, "remote port %d\n", np->remote_port);
507 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
509 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
510 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
512 EXPORT_SYMBOL(netpoll_print_options);
514 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
518 if (!strchr(str, ':') &&
519 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
523 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
524 #if IS_ENABLED(CONFIG_IPV6)
534 int netpoll_parse_options(struct netpoll *np, char *opt)
536 char *cur=opt, *delim;
538 bool ipversion_set = false;
541 if ((delim = strchr(cur, '@')) == NULL)
544 if (kstrtou16(cur, 10, &np->local_port))
551 ipversion_set = true;
552 if ((delim = strchr(cur, '/')) == NULL)
555 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
559 np->ipv6 = (bool)ipv6;
565 /* parse out dev name */
566 if ((delim = strchr(cur, ',')) == NULL)
569 strscpy(np->dev_name, cur, sizeof(np->dev_name));
576 if ((delim = strchr(cur, '@')) == NULL)
579 if (*cur == ' ' || *cur == '\t')
580 np_info(np, "warning: whitespace is not allowed\n");
581 if (kstrtou16(cur, 10, &np->remote_port))
588 if ((delim = strchr(cur, '/')) == NULL)
591 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
594 else if (ipversion_set && np->ipv6 != (bool)ipv6)
597 np->ipv6 = (bool)ipv6;
602 if (!mac_pton(cur, np->remote_mac))
606 netpoll_print_options(np);
611 np_info(np, "couldn't parse config at '%s'!\n", cur);
614 EXPORT_SYMBOL(netpoll_parse_options);
616 int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
618 struct netpoll_info *npinfo;
619 const struct net_device_ops *ops;
622 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
623 np_err(np, "%s doesn't support polling, aborting\n",
630 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
636 sema_init(&npinfo->dev_lock, 1);
637 skb_queue_head_init(&npinfo->txq);
638 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
640 refcount_set(&npinfo->refcnt, 1);
642 ops = ndev->netdev_ops;
643 if (ops->ndo_netpoll_setup) {
644 err = ops->ndo_netpoll_setup(ndev, npinfo);
649 npinfo = rtnl_dereference(ndev->npinfo);
650 refcount_inc(&npinfo->refcnt);
654 strscpy(np->dev_name, ndev->name, IFNAMSIZ);
655 npinfo->netpoll = np;
657 /* last thing to do is link it to the net device structure */
658 rcu_assign_pointer(ndev->npinfo, npinfo);
667 EXPORT_SYMBOL_GPL(__netpoll_setup);
669 int netpoll_setup(struct netpoll *np)
671 struct net_device *ndev = NULL;
672 bool ip_overwritten = false;
673 struct in_device *in_dev;
677 if (np->dev_name[0]) {
678 struct net *net = current->nsproxy->net_ns;
679 ndev = __dev_get_by_name(net, np->dev_name);
682 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
686 netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
688 if (netdev_master_upper_dev_get(ndev)) {
689 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
694 if (!netif_running(ndev)) {
695 unsigned long atmost;
697 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
699 err = dev_open(ndev, NULL);
702 np_err(np, "failed to open %s\n", ndev->name);
707 atmost = jiffies + carrier_timeout * HZ;
708 while (!netif_carrier_ok(ndev)) {
709 if (time_after(jiffies, atmost)) {
710 np_notice(np, "timeout waiting for carrier\n");
719 if (!np->local_ip.ip) {
721 const struct in_ifaddr *ifa;
723 in_dev = __in_dev_get_rtnl(ndev);
727 ifa = rtnl_dereference(in_dev->ifa_list);
730 np_err(np, "no IP address for %s, aborting\n",
736 np->local_ip.ip = ifa->ifa_local;
737 ip_overwritten = true;
738 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
740 #if IS_ENABLED(CONFIG_IPV6)
741 struct inet6_dev *idev;
744 idev = __in6_dev_get(ndev);
746 struct inet6_ifaddr *ifp;
748 read_lock_bh(&idev->lock);
749 list_for_each_entry(ifp, &idev->addr_list, if_list) {
750 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
751 !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
753 np->local_ip.in6 = ifp->addr;
754 ip_overwritten = true;
758 read_unlock_bh(&idev->lock);
761 np_err(np, "no IPv6 address for %s, aborting\n",
765 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
767 np_err(np, "IPv6 is not supported %s, aborting\n",
775 /* fill up the skb queue */
778 err = __netpoll_setup(np, ndev);
785 DEBUG_NET_WARN_ON_ONCE(np->dev);
787 memset(&np->local_ip, 0, sizeof(np->local_ip));
788 netdev_put(ndev, &np->dev_tracker);
793 EXPORT_SYMBOL(netpoll_setup);
795 static int __init netpoll_init(void)
797 skb_queue_head_init(&skb_pool);
800 core_initcall(netpoll_init);
802 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
804 struct netpoll_info *npinfo =
805 container_of(rcu_head, struct netpoll_info, rcu);
807 skb_queue_purge(&npinfo->txq);
809 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
810 cancel_delayed_work(&npinfo->tx_work);
812 /* clean after last, unfinished work */
813 __skb_queue_purge(&npinfo->txq);
814 /* now cancel it again */
815 cancel_delayed_work(&npinfo->tx_work);
819 void __netpoll_cleanup(struct netpoll *np)
821 struct netpoll_info *npinfo;
823 npinfo = rtnl_dereference(np->dev->npinfo);
827 if (refcount_dec_and_test(&npinfo->refcnt)) {
828 const struct net_device_ops *ops;
830 ops = np->dev->netdev_ops;
831 if (ops->ndo_netpoll_cleanup)
832 ops->ndo_netpoll_cleanup(np->dev);
834 RCU_INIT_POINTER(np->dev->npinfo, NULL);
835 call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
837 RCU_INIT_POINTER(np->dev->npinfo, NULL);
839 EXPORT_SYMBOL_GPL(__netpoll_cleanup);
841 void __netpoll_free(struct netpoll *np)
845 /* Wait for transmitting packets to finish before freeing. */
847 __netpoll_cleanup(np);
850 EXPORT_SYMBOL_GPL(__netpoll_free);
852 void do_netpoll_cleanup(struct netpoll *np)
854 __netpoll_cleanup(np);
855 netdev_put(np->dev, &np->dev_tracker);
858 EXPORT_SYMBOL(do_netpoll_cleanup);
860 void netpoll_cleanup(struct netpoll *np)
865 do_netpoll_cleanup(np);
869 EXPORT_SYMBOL(netpoll_cleanup);