xref: /linux/net/core/netpoll.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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 static void netpoll_udp_checksum(struct netpoll *np, struct sk_buff *skb,
376 				 int len)
377 {
378 	struct udphdr *udph;
379 	int udp_len;
380 
381 	udp_len = len + sizeof(struct udphdr);
382 	udph = udp_hdr(skb);
383 
384 	/* check needs to be set, since it will be consumed in csum_partial */
385 	udph->check = 0;
386 	if (np->ipv6)
387 		udph->check = csum_ipv6_magic(&np->local_ip.in6,
388 					      &np->remote_ip.in6,
389 					      udp_len, IPPROTO_UDP,
390 					      csum_partial(udph, udp_len, 0));
391 	else
392 		udph->check = csum_tcpudp_magic(np->local_ip.ip,
393 						np->remote_ip.ip,
394 						udp_len, IPPROTO_UDP,
395 						csum_partial(udph, udp_len, 0));
396 	if (udph->check == 0)
397 		udph->check = CSUM_MANGLED_0;
398 }
399 
400 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
401 {
402 	unsigned long flags;
403 	netdev_tx_t ret;
404 
405 	if (unlikely(!np)) {
406 		dev_kfree_skb_irq(skb);
407 		ret = NET_XMIT_DROP;
408 	} else {
409 		local_irq_save(flags);
410 		ret = __netpoll_send_skb(np, skb);
411 		local_irq_restore(flags);
412 	}
413 	return ret;
414 }
415 EXPORT_SYMBOL(netpoll_send_skb);
416 
417 static void push_ipv6(struct netpoll *np, struct sk_buff *skb, int len)
418 {
419 	struct ipv6hdr *ip6h;
420 
421 	skb_push(skb, sizeof(struct ipv6hdr));
422 	skb_reset_network_header(skb);
423 	ip6h = ipv6_hdr(skb);
424 
425 	/* ip6h->version = 6; ip6h->priority = 0; */
426 	*(unsigned char *)ip6h = 0x60;
427 	ip6h->flow_lbl[0] = 0;
428 	ip6h->flow_lbl[1] = 0;
429 	ip6h->flow_lbl[2] = 0;
430 
431 	ip6h->payload_len = htons(sizeof(struct udphdr) + len);
432 	ip6h->nexthdr = IPPROTO_UDP;
433 	ip6h->hop_limit = 32;
434 	ip6h->saddr = np->local_ip.in6;
435 	ip6h->daddr = np->remote_ip.in6;
436 
437 	skb->protocol = htons(ETH_P_IPV6);
438 }
439 
440 static void push_ipv4(struct netpoll *np, struct sk_buff *skb, int len)
441 {
442 	static atomic_t ip_ident;
443 	struct iphdr *iph;
444 	int ip_len;
445 
446 	ip_len = len + sizeof(struct udphdr) + sizeof(struct iphdr);
447 
448 	skb_push(skb, sizeof(struct iphdr));
449 	skb_reset_network_header(skb);
450 	iph = ip_hdr(skb);
451 
452 	/* iph->version = 4; iph->ihl = 5; */
453 	*(unsigned char *)iph = 0x45;
454 	iph->tos = 0;
455 	put_unaligned(htons(ip_len), &iph->tot_len);
456 	iph->id = htons(atomic_inc_return(&ip_ident));
457 	iph->frag_off = 0;
458 	iph->ttl = 64;
459 	iph->protocol = IPPROTO_UDP;
460 	iph->check = 0;
461 	put_unaligned(np->local_ip.ip, &iph->saddr);
462 	put_unaligned(np->remote_ip.ip, &iph->daddr);
463 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
464 	skb->protocol = htons(ETH_P_IP);
465 }
466 
467 static void push_udp(struct netpoll *np, struct sk_buff *skb, int len)
468 {
469 	struct udphdr *udph;
470 	int udp_len;
471 
472 	udp_len = len + sizeof(struct udphdr);
473 
474 	skb_push(skb, sizeof(struct udphdr));
475 	skb_reset_transport_header(skb);
476 
477 	udph = udp_hdr(skb);
478 	udph->source = htons(np->local_port);
479 	udph->dest = htons(np->remote_port);
480 	udph->len = htons(udp_len);
481 
482 	netpoll_udp_checksum(np, skb, len);
483 }
484 
485 static void push_eth(struct netpoll *np, struct sk_buff *skb)
486 {
487 	struct ethhdr *eth;
488 
489 	eth = skb_push(skb, ETH_HLEN);
490 	skb_reset_mac_header(skb);
491 	ether_addr_copy(eth->h_source, np->dev->dev_addr);
492 	ether_addr_copy(eth->h_dest, np->remote_mac);
493 	if (np->ipv6)
494 		eth->h_proto = htons(ETH_P_IPV6);
495 	else
496 		eth->h_proto = htons(ETH_P_IP);
497 }
498 
499 int netpoll_send_udp(struct netpoll *np, const char *msg, int len)
500 {
501 	int total_len, ip_len, udp_len;
502 	struct sk_buff *skb;
503 
504 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
505 		WARN_ON_ONCE(!irqs_disabled());
506 
507 	udp_len = len + sizeof(struct udphdr);
508 	if (np->ipv6)
509 		ip_len = udp_len + sizeof(struct ipv6hdr);
510 	else
511 		ip_len = udp_len + sizeof(struct iphdr);
512 
513 	total_len = ip_len + LL_RESERVED_SPACE(np->dev);
514 
515 	skb = find_skb(np, total_len + np->dev->needed_tailroom,
516 		       total_len - len);
517 	if (!skb)
518 		return -ENOMEM;
519 
520 	skb_copy_to_linear_data(skb, msg, len);
521 	skb_put(skb, len);
522 
523 	push_udp(np, skb, len);
524 	if (np->ipv6)
525 		push_ipv6(np, skb, len);
526 	else
527 		push_ipv4(np, skb, len);
528 	push_eth(np, skb);
529 	skb->dev = np->dev;
530 
531 	return (int)netpoll_send_skb(np, skb);
532 }
533 EXPORT_SYMBOL(netpoll_send_udp);
534 
535 
536 static void skb_pool_flush(struct netpoll *np)
537 {
538 	struct sk_buff_head *skb_pool;
539 
540 	cancel_work_sync(&np->refill_wq);
541 	skb_pool = &np->skb_pool;
542 	skb_queue_purge_reason(skb_pool, SKB_CONSUMED);
543 }
544 
545 static void refill_skbs_work_handler(struct work_struct *work)
546 {
547 	struct netpoll *np =
548 		container_of(work, struct netpoll, refill_wq);
549 
550 	refill_skbs(np);
551 }
552 
553 int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
554 {
555 	struct netpoll_info *npinfo;
556 	const struct net_device_ops *ops;
557 	int err;
558 
559 	skb_queue_head_init(&np->skb_pool);
560 
561 	if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
562 		np_err(np, "%s doesn't support polling, aborting\n",
563 		       ndev->name);
564 		err = -ENOTSUPP;
565 		goto out;
566 	}
567 
568 	npinfo = rtnl_dereference(ndev->npinfo);
569 	if (!npinfo) {
570 		npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
571 		if (!npinfo) {
572 			err = -ENOMEM;
573 			goto out;
574 		}
575 
576 		sema_init(&npinfo->dev_lock, 1);
577 		skb_queue_head_init(&npinfo->txq);
578 		INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
579 
580 		refcount_set(&npinfo->refcnt, 1);
581 
582 		ops = ndev->netdev_ops;
583 		if (ops->ndo_netpoll_setup) {
584 			err = ops->ndo_netpoll_setup(ndev);
585 			if (err)
586 				goto free_npinfo;
587 		}
588 	} else {
589 		refcount_inc(&npinfo->refcnt);
590 	}
591 
592 	np->dev = ndev;
593 	strscpy(np->dev_name, ndev->name, IFNAMSIZ);
594 
595 	/* fill up the skb queue */
596 	refill_skbs(np);
597 	INIT_WORK(&np->refill_wq, refill_skbs_work_handler);
598 
599 	/* last thing to do is link it to the net device structure */
600 	rcu_assign_pointer(ndev->npinfo, npinfo);
601 
602 	return 0;
603 
604 free_npinfo:
605 	kfree(npinfo);
606 out:
607 	return err;
608 }
609 EXPORT_SYMBOL_GPL(__netpoll_setup);
610 
611 /*
612  * Returns a pointer to a string representation of the identifier used
613  * to select the egress interface for the given netpoll instance. buf
614  * must be a buffer of length at least MAC_ADDR_STR_LEN + 1.
615  */
616 static char *egress_dev(struct netpoll *np, char *buf)
617 {
618 	if (np->dev_name[0])
619 		return np->dev_name;
620 
621 	snprintf(buf, MAC_ADDR_STR_LEN, "%pM", np->dev_mac);
622 	return buf;
623 }
624 
625 static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev,
626 				 unsigned int timeout)
627 {
628 	unsigned long atmost;
629 
630 	atmost = jiffies + timeout * HZ;
631 	while (!netif_carrier_ok(ndev)) {
632 		if (time_after(jiffies, atmost)) {
633 			np_notice(np, "timeout waiting for carrier\n");
634 			break;
635 		}
636 		msleep(1);
637 	}
638 }
639 
640 /*
641  * Take the IPv6 from ndev and populate local_ip structure in netpoll
642  */
643 static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
644 {
645 	char buf[MAC_ADDR_STR_LEN + 1];
646 	int err = -EDESTADDRREQ;
647 	struct inet6_dev *idev;
648 
649 	if (!IS_ENABLED(CONFIG_IPV6)) {
650 		np_err(np, "IPv6 is not supported %s, aborting\n",
651 		       egress_dev(np, buf));
652 		return -EINVAL;
653 	}
654 
655 	idev = __in6_dev_get(ndev);
656 	if (idev) {
657 		struct inet6_ifaddr *ifp;
658 
659 		read_lock_bh(&idev->lock);
660 		list_for_each_entry(ifp, &idev->addr_list, if_list) {
661 			if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
662 				!!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
663 				continue;
664 			/* Got the IP, let's return */
665 			np->local_ip.in6 = ifp->addr;
666 			err = 0;
667 			break;
668 		}
669 		read_unlock_bh(&idev->lock);
670 	}
671 	if (err) {
672 		np_err(np, "no IPv6 address for %s, aborting\n",
673 		       egress_dev(np, buf));
674 		return err;
675 	}
676 
677 	np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
678 	return 0;
679 }
680 
681 /*
682  * Take the IPv4 from ndev and populate local_ip structure in netpoll
683  */
684 static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev)
685 {
686 	char buf[MAC_ADDR_STR_LEN + 1];
687 	const struct in_ifaddr *ifa;
688 	struct in_device *in_dev;
689 
690 	in_dev = __in_dev_get_rtnl(ndev);
691 	if (!in_dev) {
692 		np_err(np, "no IP address for %s, aborting\n",
693 		       egress_dev(np, buf));
694 		return -EDESTADDRREQ;
695 	}
696 
697 	ifa = rtnl_dereference(in_dev->ifa_list);
698 	if (!ifa) {
699 		np_err(np, "no IP address for %s, aborting\n",
700 		       egress_dev(np, buf));
701 		return -EDESTADDRREQ;
702 	}
703 
704 	np->local_ip.ip = ifa->ifa_local;
705 	np_info(np, "local IP %pI4\n", &np->local_ip.ip);
706 
707 	return 0;
708 }
709 
710 int netpoll_setup(struct netpoll *np)
711 {
712 	struct net *net = current->nsproxy->net_ns;
713 	char buf[MAC_ADDR_STR_LEN + 1];
714 	struct net_device *ndev = NULL;
715 	bool ip_overwritten = false;
716 	int err;
717 
718 	rtnl_lock();
719 	if (np->dev_name[0])
720 		ndev = __dev_get_by_name(net, np->dev_name);
721 	else if (is_valid_ether_addr(np->dev_mac))
722 		ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac);
723 
724 	if (!ndev) {
725 		np_err(np, "%s doesn't exist, aborting\n", egress_dev(np, buf));
726 		err = -ENODEV;
727 		goto unlock;
728 	}
729 	netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
730 
731 	if (netdev_master_upper_dev_get(ndev)) {
732 		np_err(np, "%s is a slave device, aborting\n",
733 		       egress_dev(np, buf));
734 		err = -EBUSY;
735 		goto put;
736 	}
737 
738 	if (!netif_running(ndev)) {
739 		np_info(np, "device %s not up yet, forcing it\n",
740 			egress_dev(np, buf));
741 
742 		err = dev_open(ndev, NULL);
743 		if (err) {
744 			np_err(np, "failed to open %s\n", ndev->name);
745 			goto put;
746 		}
747 
748 		rtnl_unlock();
749 		netpoll_wait_carrier(np, ndev, carrier_timeout);
750 		rtnl_lock();
751 	}
752 
753 	if (!np->local_ip.ip) {
754 		if (!np->ipv6) {
755 			err = netpoll_take_ipv4(np, ndev);
756 			if (err)
757 				goto put;
758 		} else {
759 			err = netpoll_take_ipv6(np, ndev);
760 			if (err)
761 				goto put;
762 		}
763 		ip_overwritten = true;
764 	}
765 
766 	err = __netpoll_setup(np, ndev);
767 	if (err)
768 		goto flush;
769 	rtnl_unlock();
770 
771 	/* Make sure all NAPI polls which started before dev->npinfo
772 	 * was visible have exited before we start calling NAPI poll.
773 	 * NAPI skips locking if dev->npinfo is NULL.
774 	 */
775 	synchronize_rcu();
776 
777 	return 0;
778 
779 flush:
780 	skb_pool_flush(np);
781 put:
782 	DEBUG_NET_WARN_ON_ONCE(np->dev);
783 	if (ip_overwritten)
784 		memset(&np->local_ip, 0, sizeof(np->local_ip));
785 	netdev_put(ndev, &np->dev_tracker);
786 unlock:
787 	rtnl_unlock();
788 	return err;
789 }
790 EXPORT_SYMBOL(netpoll_setup);
791 
792 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
793 {
794 	struct netpoll_info *npinfo =
795 			container_of(rcu_head, struct netpoll_info, rcu);
796 
797 	skb_queue_purge(&npinfo->txq);
798 
799 	/* we can't call cancel_delayed_work_sync here, as we are in softirq */
800 	cancel_delayed_work(&npinfo->tx_work);
801 
802 	/* clean after last, unfinished work */
803 	__skb_queue_purge(&npinfo->txq);
804 	/* now cancel it again */
805 	cancel_delayed_work(&npinfo->tx_work);
806 	kfree(npinfo);
807 }
808 
809 static void __netpoll_cleanup(struct netpoll *np)
810 {
811 	struct netpoll_info *npinfo;
812 
813 	npinfo = rtnl_dereference(np->dev->npinfo);
814 	if (!npinfo)
815 		return;
816 
817 	if (refcount_dec_and_test(&npinfo->refcnt)) {
818 		const struct net_device_ops *ops;
819 
820 		ops = np->dev->netdev_ops;
821 		if (ops->ndo_netpoll_cleanup)
822 			ops->ndo_netpoll_cleanup(np->dev);
823 
824 		RCU_INIT_POINTER(np->dev->npinfo, NULL);
825 		call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
826 	} else
827 		RCU_INIT_POINTER(np->dev->npinfo, NULL);
828 
829 	skb_pool_flush(np);
830 }
831 
832 void __netpoll_free(struct netpoll *np)
833 {
834 	ASSERT_RTNL();
835 
836 	/* Wait for transmitting packets to finish before freeing. */
837 	synchronize_net();
838 	__netpoll_cleanup(np);
839 	kfree(np);
840 }
841 EXPORT_SYMBOL_GPL(__netpoll_free);
842 
843 void do_netpoll_cleanup(struct netpoll *np)
844 {
845 	__netpoll_cleanup(np);
846 	netdev_put(np->dev, &np->dev_tracker);
847 	np->dev = NULL;
848 }
849 EXPORT_SYMBOL(do_netpoll_cleanup);
850 
851 void netpoll_cleanup(struct netpoll *np)
852 {
853 	rtnl_lock();
854 	if (!np->dev)
855 		goto out;
856 	do_netpoll_cleanup(np);
857 out:
858 	rtnl_unlock();
859 }
860 EXPORT_SYMBOL(netpoll_cleanup);
861