xref: /linux/net/core/netpoll.c (revision cea465a96a294e7bc2537f27a737cfa7c6234b3d)
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 <net/tcp.h>
33 #include <net/udp.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>
40 
41 /*
42  * We maintain a small pool of fully-sized skbs, to make sure the
43  * message gets out even in extreme OOM situations.
44  */
45 
46 #define MAX_UDP_CHUNK 1460
47 #define MAX_SKBS 32
48 #define USEC_PER_POLL	50
49 
50 #define MAX_SKB_SIZE							\
51 	(sizeof(struct ethhdr) +					\
52 	 sizeof(struct iphdr) +						\
53 	 sizeof(struct udphdr) +					\
54 	 MAX_UDP_CHUNK)
55 
56 static void zap_completion_queue(void);
57 
58 static unsigned int carrier_timeout = 4;
59 module_param(carrier_timeout, uint, 0644);
60 
61 static netdev_tx_t netpoll_start_xmit(struct sk_buff *skb,
62 				      struct net_device *dev,
63 				      struct netdev_queue *txq)
64 {
65 	netdev_tx_t status = NETDEV_TX_OK;
66 	netdev_features_t features;
67 
68 	features = netif_skb_features(skb);
69 
70 	if (skb_vlan_tag_present(skb) &&
71 	    !vlan_hw_offload_capable(features, skb->vlan_proto)) {
72 		skb = __vlan_hwaccel_push_inside(skb);
73 		if (unlikely(!skb)) {
74 			/* This is actually a packet drop, but we
75 			 * don't want the code that calls this
76 			 * function to try and operate on a NULL skb.
77 			 */
78 			goto out;
79 		}
80 	}
81 
82 	status = netdev_start_xmit(skb, dev, txq, false);
83 
84 out:
85 	return status;
86 }
87 
88 static void queue_process(struct work_struct *work)
89 {
90 	struct netpoll_info *npinfo =
91 		container_of(work, struct netpoll_info, tx_work.work);
92 	struct sk_buff *skb;
93 	unsigned long flags;
94 
95 	while ((skb = skb_dequeue(&npinfo->txq))) {
96 		struct net_device *dev = skb->dev;
97 		struct netdev_queue *txq;
98 		unsigned int q_index;
99 
100 		if (!netif_device_present(dev) || !netif_running(dev)) {
101 			kfree_skb(skb);
102 			continue;
103 		}
104 
105 		local_irq_save(flags);
106 		/* check if skb->queue_mapping is still valid */
107 		q_index = skb_get_queue_mapping(skb);
108 		if (unlikely(q_index >= dev->real_num_tx_queues)) {
109 			q_index = q_index % dev->real_num_tx_queues;
110 			skb_set_queue_mapping(skb, q_index);
111 		}
112 		txq = netdev_get_tx_queue(dev, q_index);
113 		HARD_TX_LOCK(dev, txq, smp_processor_id());
114 		if (netif_xmit_frozen_or_stopped(txq) ||
115 		    !dev_xmit_complete(netpoll_start_xmit(skb, dev, txq))) {
116 			skb_queue_head(&npinfo->txq, skb);
117 			HARD_TX_UNLOCK(dev, txq);
118 			local_irq_restore(flags);
119 
120 			schedule_delayed_work(&npinfo->tx_work, HZ/10);
121 			return;
122 		}
123 		HARD_TX_UNLOCK(dev, txq);
124 		local_irq_restore(flags);
125 	}
126 }
127 
128 static int netif_local_xmit_active(struct net_device *dev)
129 {
130 	int i;
131 
132 	for (i = 0; i < dev->num_tx_queues; i++) {
133 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
134 
135 		if (READ_ONCE(txq->xmit_lock_owner) == smp_processor_id())
136 			return 1;
137 	}
138 
139 	return 0;
140 }
141 
142 static void poll_one_napi(struct napi_struct *napi)
143 {
144 	int work;
145 
146 	/* If we set this bit but see that it has already been set,
147 	 * that indicates that napi has been disabled and we need
148 	 * to abort this operation
149 	 */
150 	if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
151 		return;
152 
153 	/* We explicitly pass the polling call a budget of 0 to
154 	 * indicate that we are clearing the Tx path only.
155 	 */
156 	work = napi->poll(napi, 0);
157 	WARN_ONCE(work, "%pS exceeded budget in poll\n", napi->poll);
158 	trace_napi_poll(napi, work, 0);
159 
160 	clear_bit(NAPI_STATE_NPSVC, &napi->state);
161 }
162 
163 static void poll_napi(struct net_device *dev)
164 {
165 	struct napi_struct *napi;
166 	int cpu = smp_processor_id();
167 
168 	list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
169 		if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) {
170 			poll_one_napi(napi);
171 			smp_store_release(&napi->poll_owner, -1);
172 		}
173 	}
174 }
175 
176 void netpoll_poll_dev(struct net_device *dev)
177 {
178 	struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
179 	const struct net_device_ops *ops;
180 
181 	/* Don't do any rx activity if the dev_lock mutex is held
182 	 * the dev_open/close paths use this to block netpoll activity
183 	 * while changing device state
184 	 */
185 	if (!ni || down_trylock(&ni->dev_lock))
186 		return;
187 
188 	/* Some drivers will take the same locks in poll and xmit,
189 	 * we can't poll if local CPU is already in xmit.
190 	 */
191 	if (!netif_running(dev) || netif_local_xmit_active(dev)) {
192 		up(&ni->dev_lock);
193 		return;
194 	}
195 
196 	ops = dev->netdev_ops;
197 	if (ops->ndo_poll_controller)
198 		ops->ndo_poll_controller(dev);
199 
200 	poll_napi(dev);
201 
202 	up(&ni->dev_lock);
203 
204 	zap_completion_queue();
205 }
206 EXPORT_SYMBOL(netpoll_poll_dev);
207 
208 void netpoll_poll_disable(struct net_device *dev)
209 {
210 	struct netpoll_info *ni;
211 
212 	might_sleep();
213 	ni = rtnl_dereference(dev->npinfo);
214 	if (ni)
215 		down(&ni->dev_lock);
216 }
217 
218 void netpoll_poll_enable(struct net_device *dev)
219 {
220 	struct netpoll_info *ni;
221 
222 	ni = rtnl_dereference(dev->npinfo);
223 	if (ni)
224 		up(&ni->dev_lock);
225 }
226 
227 static void refill_skbs(struct netpoll *np)
228 {
229 	struct sk_buff_head *skb_pool;
230 	struct sk_buff *skb;
231 	unsigned long flags;
232 
233 	skb_pool = &np->skb_pool;
234 
235 	spin_lock_irqsave(&skb_pool->lock, flags);
236 	while (skb_pool->qlen < MAX_SKBS) {
237 		skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
238 		if (!skb)
239 			break;
240 
241 		__skb_queue_tail(skb_pool, skb);
242 	}
243 	spin_unlock_irqrestore(&skb_pool->lock, flags);
244 }
245 
246 static void zap_completion_queue(void)
247 {
248 	unsigned long flags;
249 	struct softnet_data *sd = &get_cpu_var(softnet_data);
250 
251 	if (sd->completion_queue) {
252 		struct sk_buff *clist;
253 
254 		local_irq_save(flags);
255 		clist = sd->completion_queue;
256 		sd->completion_queue = NULL;
257 		local_irq_restore(flags);
258 
259 		while (clist != NULL) {
260 			struct sk_buff *skb = clist;
261 			clist = clist->next;
262 			if (!skb_irq_freeable(skb)) {
263 				refcount_set(&skb->users, 1);
264 				dev_kfree_skb_any(skb); /* put this one back */
265 			} else {
266 				__kfree_skb(skb);
267 			}
268 		}
269 	}
270 
271 	put_cpu_var(softnet_data);
272 }
273 
274 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
275 {
276 	int count = 0;
277 	struct sk_buff *skb;
278 
279 	zap_completion_queue();
280 repeat:
281 
282 	skb = alloc_skb(len, GFP_ATOMIC);
283 	if (!skb) {
284 		skb = skb_dequeue(&np->skb_pool);
285 		schedule_work(&np->refill_wq);
286 	}
287 
288 	if (!skb) {
289 		if (++count < 10) {
290 			netpoll_poll_dev(np->dev);
291 			goto repeat;
292 		}
293 		return NULL;
294 	}
295 
296 	refcount_set(&skb->users, 1);
297 	skb_reserve(skb, reserve);
298 	return skb;
299 }
300 
301 static int netpoll_owner_active(struct net_device *dev)
302 {
303 	struct napi_struct *napi;
304 
305 	list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) {
306 		if (READ_ONCE(napi->poll_owner) == smp_processor_id())
307 			return 1;
308 	}
309 	return 0;
310 }
311 
312 /* call with IRQ disabled */
313 static netdev_tx_t __netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
314 {
315 	netdev_tx_t status = NETDEV_TX_BUSY;
316 	netdev_tx_t ret = NET_XMIT_DROP;
317 	struct net_device *dev;
318 	unsigned long tries;
319 	/* It is up to the caller to keep npinfo alive. */
320 	struct netpoll_info *npinfo;
321 
322 	lockdep_assert_irqs_disabled();
323 
324 	dev = np->dev;
325 	rcu_read_lock();
326 	npinfo = rcu_dereference_bh(dev->npinfo);
327 
328 	if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
329 		dev_kfree_skb_irq(skb);
330 		goto out;
331 	}
332 
333 	/* don't get messages out of order, and no recursion */
334 	if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
335 		struct netdev_queue *txq;
336 
337 		txq = netdev_core_pick_tx(dev, skb, NULL);
338 
339 		/* try until next clock tick */
340 		for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
341 		     tries > 0; --tries) {
342 			if (HARD_TX_TRYLOCK(dev, txq)) {
343 				if (!netif_xmit_stopped(txq))
344 					status = netpoll_start_xmit(skb, dev, txq);
345 
346 				HARD_TX_UNLOCK(dev, txq);
347 
348 				if (dev_xmit_complete(status))
349 					break;
350 
351 			}
352 
353 			/* tickle device maybe there is some cleanup */
354 			netpoll_poll_dev(np->dev);
355 
356 			udelay(USEC_PER_POLL);
357 		}
358 
359 		WARN_ONCE(!irqs_disabled(),
360 			"netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n",
361 			dev->name, dev->netdev_ops->ndo_start_xmit);
362 
363 	}
364 
365 	if (!dev_xmit_complete(status)) {
366 		skb_queue_tail(&npinfo->txq, skb);
367 		schedule_delayed_work(&npinfo->tx_work,0);
368 	}
369 	ret = NETDEV_TX_OK;
370 out:
371 	rcu_read_unlock();
372 	return ret;
373 }
374 
375 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
376 {
377 	unsigned long flags;
378 	netdev_tx_t ret;
379 
380 	if (unlikely(!np)) {
381 		dev_kfree_skb_irq(skb);
382 		ret = NET_XMIT_DROP;
383 	} else {
384 		local_irq_save(flags);
385 		ret = __netpoll_send_skb(np, skb);
386 		local_irq_restore(flags);
387 	}
388 	return ret;
389 }
390 EXPORT_SYMBOL(netpoll_send_skb);
391 
392 int netpoll_send_udp(struct netpoll *np, const char *msg, int len)
393 {
394 	int total_len, ip_len, udp_len;
395 	struct sk_buff *skb;
396 	struct udphdr *udph;
397 	struct iphdr *iph;
398 	struct ethhdr *eth;
399 	static atomic_t ip_ident;
400 	struct ipv6hdr *ip6h;
401 
402 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
403 		WARN_ON_ONCE(!irqs_disabled());
404 
405 	udp_len = len + sizeof(*udph);
406 	if (np->ipv6)
407 		ip_len = udp_len + sizeof(*ip6h);
408 	else
409 		ip_len = udp_len + sizeof(*iph);
410 
411 	total_len = ip_len + LL_RESERVED_SPACE(np->dev);
412 
413 	skb = find_skb(np, total_len + np->dev->needed_tailroom,
414 		       total_len - len);
415 	if (!skb)
416 		return -ENOMEM;
417 
418 	skb_copy_to_linear_data(skb, msg, len);
419 	skb_put(skb, len);
420 
421 	skb_push(skb, sizeof(*udph));
422 	skb_reset_transport_header(skb);
423 	udph = udp_hdr(skb);
424 	udph->source = htons(np->local_port);
425 	udph->dest = htons(np->remote_port);
426 	udph->len = htons(udp_len);
427 
428 	if (np->ipv6) {
429 		udph->check = csum_ipv6_magic(&np->local_ip.in6,
430 					      &np->remote_ip.in6,
431 					      udp_len, IPPROTO_UDP,
432 					      csum_partial(udph, udp_len, 0));
433 		if (udph->check == 0)
434 			udph->check = CSUM_MANGLED_0;
435 
436 		skb_push(skb, sizeof(*ip6h));
437 		skb_reset_network_header(skb);
438 		ip6h = ipv6_hdr(skb);
439 
440 		/* ip6h->version = 6; ip6h->priority = 0; */
441 		*(unsigned char *)ip6h = 0x60;
442 		ip6h->flow_lbl[0] = 0;
443 		ip6h->flow_lbl[1] = 0;
444 		ip6h->flow_lbl[2] = 0;
445 
446 		ip6h->payload_len = htons(sizeof(struct udphdr) + len);
447 		ip6h->nexthdr = IPPROTO_UDP;
448 		ip6h->hop_limit = 32;
449 		ip6h->saddr = np->local_ip.in6;
450 		ip6h->daddr = np->remote_ip.in6;
451 
452 		eth = skb_push(skb, ETH_HLEN);
453 		skb_reset_mac_header(skb);
454 		skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
455 	} else {
456 		udph->check = 0;
457 		udph->check = csum_tcpudp_magic(np->local_ip.ip,
458 						np->remote_ip.ip,
459 						udp_len, IPPROTO_UDP,
460 						csum_partial(udph, udp_len, 0));
461 		if (udph->check == 0)
462 			udph->check = CSUM_MANGLED_0;
463 
464 		skb_push(skb, sizeof(*iph));
465 		skb_reset_network_header(skb);
466 		iph = ip_hdr(skb);
467 
468 		/* iph->version = 4; iph->ihl = 5; */
469 		*(unsigned char *)iph = 0x45;
470 		iph->tos      = 0;
471 		put_unaligned(htons(ip_len), &(iph->tot_len));
472 		iph->id       = htons(atomic_inc_return(&ip_ident));
473 		iph->frag_off = 0;
474 		iph->ttl      = 64;
475 		iph->protocol = IPPROTO_UDP;
476 		iph->check    = 0;
477 		put_unaligned(np->local_ip.ip, &(iph->saddr));
478 		put_unaligned(np->remote_ip.ip, &(iph->daddr));
479 		iph->check    = ip_fast_csum((unsigned char *)iph, iph->ihl);
480 
481 		eth = skb_push(skb, ETH_HLEN);
482 		skb_reset_mac_header(skb);
483 		skb->protocol = eth->h_proto = htons(ETH_P_IP);
484 	}
485 
486 	ether_addr_copy(eth->h_source, np->dev->dev_addr);
487 	ether_addr_copy(eth->h_dest, np->remote_mac);
488 
489 	skb->dev = np->dev;
490 
491 	return (int)netpoll_send_skb(np, skb);
492 }
493 EXPORT_SYMBOL(netpoll_send_udp);
494 
495 
496 static void skb_pool_flush(struct netpoll *np)
497 {
498 	struct sk_buff_head *skb_pool;
499 
500 	cancel_work_sync(&np->refill_wq);
501 	skb_pool = &np->skb_pool;
502 	skb_queue_purge_reason(skb_pool, SKB_CONSUMED);
503 }
504 
505 static void refill_skbs_work_handler(struct work_struct *work)
506 {
507 	struct netpoll *np =
508 		container_of(work, struct netpoll, refill_wq);
509 
510 	refill_skbs(np);
511 }
512 
513 int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
514 {
515 	struct netpoll_info *npinfo;
516 	const struct net_device_ops *ops;
517 	int err;
518 
519 	skb_queue_head_init(&np->skb_pool);
520 
521 	if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
522 		np_err(np, "%s doesn't support polling, aborting\n",
523 		       ndev->name);
524 		err = -ENOTSUPP;
525 		goto out;
526 	}
527 
528 	npinfo = rtnl_dereference(ndev->npinfo);
529 	if (!npinfo) {
530 		npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
531 		if (!npinfo) {
532 			err = -ENOMEM;
533 			goto out;
534 		}
535 
536 		sema_init(&npinfo->dev_lock, 1);
537 		skb_queue_head_init(&npinfo->txq);
538 		INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
539 
540 		refcount_set(&npinfo->refcnt, 1);
541 
542 		ops = ndev->netdev_ops;
543 		if (ops->ndo_netpoll_setup) {
544 			err = ops->ndo_netpoll_setup(ndev);
545 			if (err)
546 				goto free_npinfo;
547 		}
548 	} else {
549 		refcount_inc(&npinfo->refcnt);
550 	}
551 
552 	np->dev = ndev;
553 	strscpy(np->dev_name, ndev->name, IFNAMSIZ);
554 	npinfo->netpoll = np;
555 
556 	/* fill up the skb queue */
557 	refill_skbs(np);
558 	INIT_WORK(&np->refill_wq, refill_skbs_work_handler);
559 
560 	/* last thing to do is link it to the net device structure */
561 	rcu_assign_pointer(ndev->npinfo, npinfo);
562 
563 	return 0;
564 
565 free_npinfo:
566 	kfree(npinfo);
567 out:
568 	return err;
569 }
570 EXPORT_SYMBOL_GPL(__netpoll_setup);
571 
572 /*
573  * Returns a pointer to a string representation of the identifier used
574  * to select the egress interface for the given netpoll instance. buf
575  * must be a buffer of length at least MAC_ADDR_STR_LEN + 1.
576  */
577 static char *egress_dev(struct netpoll *np, char *buf)
578 {
579 	if (np->dev_name[0])
580 		return np->dev_name;
581 
582 	snprintf(buf, MAC_ADDR_STR_LEN, "%pM", np->dev_mac);
583 	return buf;
584 }
585 
586 int netpoll_setup(struct netpoll *np)
587 {
588 	struct net *net = current->nsproxy->net_ns;
589 	char buf[MAC_ADDR_STR_LEN + 1];
590 	struct net_device *ndev = NULL;
591 	bool ip_overwritten = false;
592 	struct in_device *in_dev;
593 	int err;
594 
595 	rtnl_lock();
596 	if (np->dev_name[0])
597 		ndev = __dev_get_by_name(net, np->dev_name);
598 	else if (is_valid_ether_addr(np->dev_mac))
599 		ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac);
600 
601 	if (!ndev) {
602 		np_err(np, "%s doesn't exist, aborting\n", egress_dev(np, buf));
603 		err = -ENODEV;
604 		goto unlock;
605 	}
606 	netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
607 
608 	if (netdev_master_upper_dev_get(ndev)) {
609 		np_err(np, "%s is a slave device, aborting\n",
610 		       egress_dev(np, buf));
611 		err = -EBUSY;
612 		goto put;
613 	}
614 
615 	if (!netif_running(ndev)) {
616 		unsigned long atmost;
617 
618 		np_info(np, "device %s not up yet, forcing it\n",
619 			egress_dev(np, buf));
620 
621 		err = dev_open(ndev, NULL);
622 
623 		if (err) {
624 			np_err(np, "failed to open %s\n", ndev->name);
625 			goto put;
626 		}
627 
628 		rtnl_unlock();
629 		atmost = jiffies + carrier_timeout * HZ;
630 		while (!netif_carrier_ok(ndev)) {
631 			if (time_after(jiffies, atmost)) {
632 				np_notice(np, "timeout waiting for carrier\n");
633 				break;
634 			}
635 			msleep(1);
636 		}
637 
638 		rtnl_lock();
639 	}
640 
641 	if (!np->local_ip.ip) {
642 		if (!np->ipv6) {
643 			const struct in_ifaddr *ifa;
644 
645 			in_dev = __in_dev_get_rtnl(ndev);
646 			if (!in_dev)
647 				goto put_noaddr;
648 
649 			ifa = rtnl_dereference(in_dev->ifa_list);
650 			if (!ifa) {
651 put_noaddr:
652 				np_err(np, "no IP address for %s, aborting\n",
653 				       egress_dev(np, buf));
654 				err = -EDESTADDRREQ;
655 				goto put;
656 			}
657 
658 			np->local_ip.ip = ifa->ifa_local;
659 			ip_overwritten = true;
660 			np_info(np, "local IP %pI4\n", &np->local_ip.ip);
661 		} else {
662 #if IS_ENABLED(CONFIG_IPV6)
663 			struct inet6_dev *idev;
664 
665 			err = -EDESTADDRREQ;
666 			idev = __in6_dev_get(ndev);
667 			if (idev) {
668 				struct inet6_ifaddr *ifp;
669 
670 				read_lock_bh(&idev->lock);
671 				list_for_each_entry(ifp, &idev->addr_list, if_list) {
672 					if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
673 					    !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
674 						continue;
675 					np->local_ip.in6 = ifp->addr;
676 					ip_overwritten = true;
677 					err = 0;
678 					break;
679 				}
680 				read_unlock_bh(&idev->lock);
681 			}
682 			if (err) {
683 				np_err(np, "no IPv6 address for %s, aborting\n",
684 				       egress_dev(np, buf));
685 				goto put;
686 			} else
687 				np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
688 #else
689 			np_err(np, "IPv6 is not supported %s, aborting\n",
690 			       egress_dev(np, buf));
691 			err = -EINVAL;
692 			goto put;
693 #endif
694 		}
695 	}
696 
697 	err = __netpoll_setup(np, ndev);
698 	if (err)
699 		goto flush;
700 	rtnl_unlock();
701 	return 0;
702 
703 flush:
704 	skb_pool_flush(np);
705 put:
706 	DEBUG_NET_WARN_ON_ONCE(np->dev);
707 	if (ip_overwritten)
708 		memset(&np->local_ip, 0, sizeof(np->local_ip));
709 	netdev_put(ndev, &np->dev_tracker);
710 unlock:
711 	rtnl_unlock();
712 	return err;
713 }
714 EXPORT_SYMBOL(netpoll_setup);
715 
716 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
717 {
718 	struct netpoll_info *npinfo =
719 			container_of(rcu_head, struct netpoll_info, rcu);
720 
721 	skb_queue_purge(&npinfo->txq);
722 
723 	/* we can't call cancel_delayed_work_sync here, as we are in softirq */
724 	cancel_delayed_work(&npinfo->tx_work);
725 
726 	/* clean after last, unfinished work */
727 	__skb_queue_purge(&npinfo->txq);
728 	/* now cancel it again */
729 	cancel_delayed_work(&npinfo->tx_work);
730 	kfree(npinfo);
731 }
732 
733 static void __netpoll_cleanup(struct netpoll *np)
734 {
735 	struct netpoll_info *npinfo;
736 
737 	npinfo = rtnl_dereference(np->dev->npinfo);
738 	if (!npinfo)
739 		return;
740 
741 	if (refcount_dec_and_test(&npinfo->refcnt)) {
742 		const struct net_device_ops *ops;
743 
744 		ops = np->dev->netdev_ops;
745 		if (ops->ndo_netpoll_cleanup)
746 			ops->ndo_netpoll_cleanup(np->dev);
747 
748 		RCU_INIT_POINTER(np->dev->npinfo, NULL);
749 		call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
750 	} else
751 		RCU_INIT_POINTER(np->dev->npinfo, NULL);
752 
753 	skb_pool_flush(np);
754 }
755 
756 void __netpoll_free(struct netpoll *np)
757 {
758 	ASSERT_RTNL();
759 
760 	/* Wait for transmitting packets to finish before freeing. */
761 	synchronize_rcu();
762 	__netpoll_cleanup(np);
763 	kfree(np);
764 }
765 EXPORT_SYMBOL_GPL(__netpoll_free);
766 
767 void do_netpoll_cleanup(struct netpoll *np)
768 {
769 	__netpoll_cleanup(np);
770 	netdev_put(np->dev, &np->dev_tracker);
771 	np->dev = NULL;
772 }
773 EXPORT_SYMBOL(do_netpoll_cleanup);
774 
775 void netpoll_cleanup(struct netpoll *np)
776 {
777 	rtnl_lock();
778 	if (!np->dev)
779 		goto out;
780 	do_netpoll_cleanup(np);
781 out:
782 	rtnl_unlock();
783 }
784 EXPORT_SYMBOL(netpoll_cleanup);
785