xref: /linux/net/core/netpoll.c (revision 333f7de560e1196034b67db16916b10a0c529e1d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Common framework for low-level network console, dump, and debugger code
4  *
5  * Sep 8 2003  Matt Mackall <mpm@selenic.com>
6  *
7  * based on the netconsole code from:
8  *
9  * Copyright (C) 2001  Ingo Molnar <mingo@redhat.com>
10  * Copyright (C) 2002  Red Hat, Inc.
11  */
12 
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 
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>
32 #include <linux/udp.h>
33 #include <net/tcp.h>
34 #include <net/addrconf.h>
35 #include <net/ndisc.h>
36 #include <trace/events/napi.h>
37 #include <linux/kconfig.h>
38 
39 #define USEC_PER_POLL	50
40 
41 static unsigned int carrier_timeout = 4;
42 module_param(carrier_timeout, uint, 0644);
43 
44 static netdev_tx_t netpoll_start_xmit(struct sk_buff *skb,
45 				      struct net_device *dev,
46 				      struct netdev_queue *txq)
47 {
48 	netdev_tx_t status = NETDEV_TX_OK;
49 	netdev_features_t features;
50 
51 	features = netif_skb_features(skb);
52 
53 	if (skb_vlan_tag_present(skb) &&
54 	    !vlan_hw_offload_capable(features, skb->vlan_proto)) {
55 		skb = __vlan_hwaccel_push_inside(skb);
56 		if (unlikely(!skb)) {
57 			/* This is actually a packet drop, but we
58 			 * don't want the code that calls this
59 			 * function to try and operate on a NULL skb.
60 			 */
61 			goto out;
62 		}
63 	}
64 
65 	status = netdev_start_xmit(skb, dev, txq, false);
66 
67 out:
68 	return status;
69 }
70 
71 static void queue_process(struct work_struct *work)
72 {
73 	struct netpoll_info *npinfo =
74 		container_of(work, struct netpoll_info, tx_work.work);
75 	struct sk_buff *skb;
76 	unsigned long flags;
77 
78 	while ((skb = skb_dequeue(&npinfo->txq))) {
79 		struct net_device *dev = skb->dev;
80 		struct netdev_queue *txq;
81 		unsigned int q_index;
82 
83 		if (!netif_device_present(dev) || !netif_running(dev)) {
84 			kfree_skb(skb);
85 			continue;
86 		}
87 
88 		local_irq_save(flags);
89 		/* check if skb->queue_mapping is still valid */
90 		q_index = skb_get_queue_mapping(skb);
91 		if (unlikely(q_index >= dev->real_num_tx_queues)) {
92 			q_index = q_index % dev->real_num_tx_queues;
93 			skb_set_queue_mapping(skb, q_index);
94 		}
95 		txq = netdev_get_tx_queue(dev, q_index);
96 		HARD_TX_LOCK(dev, txq, smp_processor_id());
97 		if (netif_xmit_frozen_or_stopped(txq) ||
98 		    !dev_xmit_complete(netpoll_start_xmit(skb, dev, txq))) {
99 			skb_queue_head(&npinfo->txq, skb);
100 			HARD_TX_UNLOCK(dev, txq);
101 			local_irq_restore(flags);
102 
103 			schedule_delayed_work(&npinfo->tx_work, HZ/10);
104 			return;
105 		}
106 		HARD_TX_UNLOCK(dev, txq);
107 		local_irq_restore(flags);
108 	}
109 }
110 
111 static int netif_local_xmit_active(struct net_device *dev)
112 {
113 	int i;
114 
115 	for (i = 0; i < dev->num_tx_queues; i++) {
116 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
117 
118 		if (netif_tx_owned(txq, smp_processor_id()))
119 			return 1;
120 	}
121 
122 	return 0;
123 }
124 
125 static void poll_one_napi(struct napi_struct *napi)
126 {
127 	int work;
128 
129 	/* If we set this bit but see that it has already been set,
130 	 * that indicates that napi has been disabled and we need
131 	 * to abort this operation
132 	 */
133 	if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
134 		return;
135 
136 	/* We explicitly pass the polling call a budget of 0 to
137 	 * indicate that we are clearing the Tx path only.
138 	 */
139 	work = napi->poll(napi, 0);
140 	WARN_ONCE(work, "%pS exceeded budget in poll\n", napi->poll);
141 	trace_napi_poll(napi, work, 0);
142 
143 	clear_bit(NAPI_STATE_NPSVC, &napi->state);
144 }
145 
146 static void poll_napi(struct net_device *dev)
147 {
148 	struct napi_struct *napi;
149 	int cpu = smp_processor_id();
150 
151 	list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
152 		if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) {
153 			poll_one_napi(napi);
154 			smp_store_release(&napi->poll_owner, -1);
155 		}
156 	}
157 }
158 
159 void netpoll_poll_dev(struct net_device *dev)
160 {
161 	struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
162 	const struct net_device_ops *ops;
163 
164 	/* Don't do any rx activity if the dev_lock mutex is held
165 	 * the dev_open/close paths use this to block netpoll activity
166 	 * while changing device state
167 	 */
168 	if (!ni || down_trylock(&ni->dev_lock))
169 		return;
170 
171 	/* Some drivers will take the same locks in poll and xmit,
172 	 * we can't poll if local CPU is already in xmit.
173 	 */
174 	if (!netif_running(dev) || netif_local_xmit_active(dev)) {
175 		up(&ni->dev_lock);
176 		return;
177 	}
178 
179 	ops = dev->netdev_ops;
180 	if (ops->ndo_poll_controller)
181 		ops->ndo_poll_controller(dev);
182 
183 	poll_napi(dev);
184 
185 	up(&ni->dev_lock);
186 
187 	netpoll_zap_completion_queue();
188 }
189 EXPORT_SYMBOL(netpoll_poll_dev);
190 
191 void netpoll_poll_disable(struct net_device *dev)
192 {
193 	struct netpoll_info *ni;
194 
195 	might_sleep();
196 	ni = rtnl_dereference(dev->npinfo);
197 	if (ni)
198 		down(&ni->dev_lock);
199 }
200 
201 void netpoll_poll_enable(struct net_device *dev)
202 {
203 	struct netpoll_info *ni;
204 
205 	ni = rtnl_dereference(dev->npinfo);
206 	if (ni)
207 		up(&ni->dev_lock);
208 }
209 
210 void netpoll_zap_completion_queue(void)
211 {
212 	unsigned long flags;
213 	struct softnet_data *sd = &get_cpu_var(softnet_data);
214 
215 	if (sd->completion_queue) {
216 		struct sk_buff *clist;
217 
218 		local_irq_save(flags);
219 		clist = sd->completion_queue;
220 		sd->completion_queue = NULL;
221 		local_irq_restore(flags);
222 
223 		while (clist != NULL) {
224 			struct sk_buff *skb = clist;
225 			clist = clist->next;
226 			if (!skb_irq_freeable(skb)) {
227 				refcount_set(&skb->users, 1);
228 				dev_kfree_skb_any(skb); /* put this one back */
229 			} else {
230 				__kfree_skb(skb);
231 			}
232 		}
233 	}
234 
235 	put_cpu_var(softnet_data);
236 }
237 EXPORT_SYMBOL_NS_GPL(netpoll_zap_completion_queue, "NETDEV_INTERNAL");
238 
239 static int netpoll_owner_active(struct net_device *dev)
240 {
241 	struct napi_struct *napi;
242 
243 	list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
244 		if (READ_ONCE(napi->poll_owner) == smp_processor_id())
245 			return 1;
246 	}
247 	return 0;
248 }
249 
250 /* call with IRQ disabled */
251 static netdev_tx_t __netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
252 {
253 	netdev_tx_t status = NETDEV_TX_BUSY;
254 	netdev_tx_t ret = NET_XMIT_DROP;
255 	struct net_device *dev;
256 	unsigned long tries;
257 	/* It is up to the caller to keep npinfo alive. */
258 	struct netpoll_info *npinfo;
259 
260 	lockdep_assert_irqs_disabled();
261 
262 	dev = np->dev;
263 	/* npinfo->txq belongs to np->dev, so retries must stay bound to it. */
264 	skb->dev = dev;
265 	rcu_read_lock();
266 	npinfo = rcu_dereference_bh(dev->npinfo);
267 
268 	if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
269 		dev_kfree_skb_irq(skb);
270 		goto out;
271 	}
272 
273 	/* don't get messages out of order, and no recursion */
274 	if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
275 		struct netdev_queue *txq;
276 
277 		txq = netdev_core_pick_tx(dev, skb, NULL);
278 
279 		/* try until next clock tick */
280 		for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
281 		     tries > 0; --tries) {
282 			if (HARD_TX_TRYLOCK(dev, txq)) {
283 				if (!netif_xmit_stopped(txq))
284 					status = netpoll_start_xmit(skb, dev, txq);
285 
286 				HARD_TX_UNLOCK(dev, txq);
287 
288 				if (dev_xmit_complete(status))
289 					break;
290 
291 			}
292 
293 			/* tickle device maybe there is some cleanup */
294 			netpoll_poll_dev(np->dev);
295 
296 			udelay(USEC_PER_POLL);
297 		}
298 
299 		WARN_ONCE(!irqs_disabled(),
300 			"netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n",
301 			dev->name, dev->netdev_ops->ndo_start_xmit);
302 
303 	}
304 
305 	if (!dev_xmit_complete(status)) {
306 		skb_queue_tail(&npinfo->txq, skb);
307 		schedule_delayed_work(&npinfo->tx_work,0);
308 	}
309 	ret = NETDEV_TX_OK;
310 out:
311 	rcu_read_unlock();
312 	return ret;
313 }
314 
315 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
316 {
317 	unsigned long flags;
318 	netdev_tx_t ret;
319 
320 	if (unlikely(!np)) {
321 		dev_kfree_skb_irq(skb);
322 		ret = NET_XMIT_DROP;
323 	} else {
324 		local_irq_save(flags);
325 		ret = __netpoll_send_skb(np, skb);
326 		local_irq_restore(flags);
327 	}
328 	return ret;
329 }
330 EXPORT_SYMBOL(netpoll_send_skb);
331 
332 int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
333 {
334 	struct netpoll_info *npinfo;
335 	const struct net_device_ops *ops;
336 	int err;
337 
338 	if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
339 		np_err(np, "%s doesn't support polling, aborting\n",
340 		       ndev->name);
341 		err = -ENOTSUPP;
342 		goto out;
343 	}
344 
345 	npinfo = rtnl_dereference(ndev->npinfo);
346 	if (!npinfo) {
347 		npinfo = kmalloc_obj(*npinfo);
348 		if (!npinfo) {
349 			err = -ENOMEM;
350 			goto out;
351 		}
352 
353 		sema_init(&npinfo->dev_lock, 1);
354 		skb_queue_head_init(&npinfo->txq);
355 		INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
356 
357 		refcount_set(&npinfo->refcnt, 1);
358 
359 		ops = ndev->netdev_ops;
360 		if (ops->ndo_netpoll_setup) {
361 			err = ops->ndo_netpoll_setup(ndev);
362 			if (err)
363 				goto free_npinfo;
364 		}
365 	} else {
366 		refcount_inc(&npinfo->refcnt);
367 	}
368 
369 	np->dev = ndev;
370 	strscpy(np->dev_name, ndev->name, IFNAMSIZ);
371 
372 	/* last thing to do is link it to the net device structure */
373 	rcu_assign_pointer(ndev->npinfo, npinfo);
374 
375 	return 0;
376 
377 free_npinfo:
378 	kfree(npinfo);
379 out:
380 	return err;
381 }
382 EXPORT_SYMBOL_GPL(__netpoll_setup);
383 
384 /*
385  * Returns a pointer to a string representation of the identifier used
386  * to select the egress interface for the given netpoll instance. buf
387  * is used to format np->dev_mac when np->dev_name is empty; bufsz must
388  * be at least MAC_ADDR_STR_LEN + 1 to fit the formatted MAC address
389  * and its NUL terminator.
390  */
391 static char *egress_dev(struct netpoll *np, char *buf, size_t bufsz)
392 {
393 	if (np->dev_name[0])
394 		return np->dev_name;
395 
396 	snprintf(buf, bufsz, "%pM", np->dev_mac);
397 	return buf;
398 }
399 
400 static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev,
401 				 unsigned int timeout)
402 {
403 	unsigned long atmost;
404 
405 	atmost = jiffies + timeout * HZ;
406 	while (!netif_carrier_ok(ndev)) {
407 		if (time_after(jiffies, atmost)) {
408 			np_notice(np, "timeout waiting for carrier\n");
409 			break;
410 		}
411 		msleep(1);
412 	}
413 }
414 
415 /*
416  * Take the IPv6 from ndev and populate local_ip structure in netpoll
417  */
418 static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
419 {
420 	char buf[MAC_ADDR_STR_LEN + 1];
421 	int err = -EDESTADDRREQ;
422 	struct inet6_dev *idev;
423 
424 	if (!IS_ENABLED(CONFIG_IPV6)) {
425 		np_err(np, "IPv6 is not supported %s, aborting\n",
426 		       egress_dev(np, buf, sizeof(buf)));
427 		return -EINVAL;
428 	}
429 
430 	idev = __in6_dev_get(ndev);
431 	if (idev) {
432 		struct inet6_ifaddr *ifp;
433 
434 		read_lock_bh(&idev->lock);
435 		list_for_each_entry(ifp, &idev->addr_list, if_list) {
436 			if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
437 				!!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
438 				continue;
439 			/* Got the IP, let's return */
440 			np->local_ip.in6 = ifp->addr;
441 			err = 0;
442 			break;
443 		}
444 		read_unlock_bh(&idev->lock);
445 	}
446 	if (err) {
447 		np_err(np, "no IPv6 address for %s, aborting\n",
448 		       egress_dev(np, buf, sizeof(buf)));
449 		return err;
450 	}
451 
452 	np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
453 	return 0;
454 }
455 
456 /*
457  * Take the IPv4 from ndev and populate local_ip structure in netpoll
458  */
459 static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev)
460 {
461 	char buf[MAC_ADDR_STR_LEN + 1];
462 	const struct in_ifaddr *ifa;
463 	struct in_device *in_dev;
464 
465 	in_dev = __in_dev_get_rtnl(ndev);
466 	if (!in_dev) {
467 		np_err(np, "no IP address for %s, aborting\n",
468 		       egress_dev(np, buf, sizeof(buf)));
469 		return -EDESTADDRREQ;
470 	}
471 
472 	ifa = rtnl_dereference(in_dev->ifa_list);
473 	if (!ifa) {
474 		np_err(np, "no IP address for %s, aborting\n",
475 		       egress_dev(np, buf, sizeof(buf)));
476 		return -EDESTADDRREQ;
477 	}
478 
479 	np->local_ip.ip = ifa->ifa_local;
480 	np_info(np, "local IP %pI4\n", &np->local_ip.ip);
481 
482 	return 0;
483 }
484 
485 /*
486  * Test whether the caller left np->local_ip unset, so that
487  * netpoll_setup() should auto-populate it from the egress device.
488  *
489  * np->local_ip is a union of __be32 (IPv4) and struct in6_addr (IPv6),
490  * so an IPv6 address whose first 4 bytes are zero (e.g. ::1, ::2,
491  * IPv4-mapped ::ffff:a.b.c.d) must not be tested via the IPv4 arm —
492  * doing so would misclassify a caller-supplied address as unset and
493  * silently overwrite it with whatever address the device exposes.
494  */
495 static bool netpoll_local_ip_unset(const struct netpoll *np)
496 {
497 	if (np->ipv6)
498 		return ipv6_addr_any(&np->local_ip.in6);
499 	return !np->local_ip.ip;
500 }
501 
502 int netpoll_setup(struct netpoll *np)
503 {
504 	struct net *net = current->nsproxy->net_ns;
505 	char buf[MAC_ADDR_STR_LEN + 1];
506 	struct net_device *ndev = NULL;
507 	bool ip_overwritten = false;
508 	int err;
509 
510 	rtnl_lock();
511 	if (np->dev_name[0])
512 		ndev = __dev_get_by_name(net, np->dev_name);
513 	else if (is_valid_ether_addr(np->dev_mac))
514 		ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac);
515 
516 	if (!ndev) {
517 		np_err(np, "%s doesn't exist, aborting\n",
518 		       egress_dev(np, buf, sizeof(buf)));
519 		err = -ENODEV;
520 		goto unlock;
521 	}
522 	netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
523 
524 	if (netdev_master_upper_dev_get(ndev)) {
525 		np_err(np, "%s is a slave device, aborting\n",
526 		       egress_dev(np, buf, sizeof(buf)));
527 		err = -EBUSY;
528 		goto put;
529 	}
530 
531 	if (!netif_running(ndev)) {
532 		np_info(np, "device %s not up yet, forcing it\n",
533 			egress_dev(np, buf, sizeof(buf)));
534 
535 		err = dev_open(ndev, NULL);
536 		if (err) {
537 			np_err(np, "failed to open %s\n", ndev->name);
538 			goto put;
539 		}
540 
541 		rtnl_unlock();
542 		netpoll_wait_carrier(np, ndev, carrier_timeout);
543 		rtnl_lock();
544 	}
545 
546 	if (netpoll_local_ip_unset(np)) {
547 		if (!np->ipv6) {
548 			err = netpoll_take_ipv4(np, ndev);
549 			if (err)
550 				goto put;
551 		} else {
552 			err = netpoll_take_ipv6(np, ndev);
553 			if (err)
554 				goto put;
555 		}
556 		ip_overwritten = true;
557 	}
558 
559 	err = __netpoll_setup(np, ndev);
560 	if (err)
561 		goto put;
562 	rtnl_unlock();
563 
564 	/* Make sure all NAPI polls which started before dev->npinfo
565 	 * was visible have exited before we start calling NAPI poll.
566 	 * NAPI skips locking if dev->npinfo is NULL.
567 	 */
568 	synchronize_rcu();
569 
570 	return 0;
571 
572 put:
573 	DEBUG_NET_WARN_ON_ONCE(np->dev);
574 	if (ip_overwritten)
575 		memset(&np->local_ip, 0, sizeof(np->local_ip));
576 	netdev_put(ndev, &np->dev_tracker);
577 unlock:
578 	rtnl_unlock();
579 	return err;
580 }
581 EXPORT_SYMBOL(netpoll_setup);
582 
583 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
584 {
585 	struct netpoll_info *npinfo =
586 			container_of(rcu_head, struct netpoll_info, rcu);
587 
588 	skb_queue_purge(&npinfo->txq);
589 	kfree(npinfo);
590 }
591 
592 static void __netpoll_cleanup(struct netpoll *np)
593 {
594 	struct netpoll_info *npinfo;
595 
596 	npinfo = rtnl_dereference(np->dev->npinfo);
597 	if (!npinfo)
598 		return;
599 
600 	/* At this point, there is a single npinfo instance per netdevice, and
601 	 * its refcnt tracks how many netpoll structures are linked to it. We
602 	 * only perform npinfo cleanup when the refcnt decrements to zero.
603 	 */
604 	if (refcount_dec_and_test(&npinfo->refcnt)) {
605 		const struct net_device_ops *ops;
606 
607 		ops = np->dev->netdev_ops;
608 		if (ops->ndo_netpoll_cleanup)
609 			ops->ndo_netpoll_cleanup(np->dev);
610 
611 		RCU_INIT_POINTER(np->dev->npinfo, NULL);
612 		disable_delayed_work_sync(&npinfo->tx_work);
613 		call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
614 	}
615 }
616 
617 void __netpoll_free(struct netpoll *np)
618 {
619 	ASSERT_RTNL();
620 
621 	/* Wait for transmitting packets to finish before freeing. */
622 	synchronize_net();
623 	__netpoll_cleanup(np);
624 	kfree(np);
625 }
626 EXPORT_SYMBOL_GPL(__netpoll_free);
627 
628 void do_netpoll_cleanup(struct netpoll *np)
629 {
630 	__netpoll_cleanup(np);
631 	netdev_put(np->dev, &np->dev_tracker);
632 	np->dev = NULL;
633 }
634 EXPORT_SYMBOL(do_netpoll_cleanup);
635 
636 void netpoll_cleanup(struct netpoll *np)
637 {
638 	rtnl_lock();
639 	if (!np->dev)
640 		goto out;
641 	do_netpoll_cleanup(np);
642 out:
643 	rtnl_unlock();
644 }
645 EXPORT_SYMBOL(netpoll_cleanup);
646