xref: /openbmc/linux/net/core/netpoll.c (revision 4e0c1159d83a658d1ffba5bc3442f4ec4cadb436)
1 /*
2  * Common framework for low-level network console, dump, and debugger code
3  *
4  * Sep 8 2003  Matt Mackall <mpm@selenic.com>
5  *
6  * based on the netconsole code from:
7  *
8  * Copyright (C) 2001  Ingo Molnar <mingo@redhat.com>
9  * Copyright (C) 2002  Red Hat, Inc.
10  */
11 
12 #include <linux/smp_lock.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/string.h>
16 #include <linux/inetdevice.h>
17 #include <linux/inet.h>
18 #include <linux/interrupt.h>
19 #include <linux/netpoll.h>
20 #include <linux/sched.h>
21 #include <linux/delay.h>
22 #include <linux/rcupdate.h>
23 #include <linux/workqueue.h>
24 #include <net/tcp.h>
25 #include <net/udp.h>
26 #include <asm/unaligned.h>
27 
28 /*
29  * We maintain a small pool of fully-sized skbs, to make sure the
30  * message gets out even in extreme OOM situations.
31  */
32 
33 #define MAX_UDP_CHUNK 1460
34 #define MAX_SKBS 32
35 #define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
36 #define MAX_RETRIES 20000
37 
38 static DEFINE_SPINLOCK(skb_list_lock);
39 static int nr_skbs;
40 static struct sk_buff *skbs;
41 
42 static DEFINE_SPINLOCK(queue_lock);
43 static int queue_depth;
44 static struct sk_buff *queue_head, *queue_tail;
45 
46 static atomic_t trapped;
47 
48 #define NETPOLL_RX_ENABLED  1
49 #define NETPOLL_RX_DROP     2
50 
51 #define MAX_SKB_SIZE \
52 		(MAX_UDP_CHUNK + sizeof(struct udphdr) + \
53 				sizeof(struct iphdr) + sizeof(struct ethhdr))
54 
55 static void zap_completion_queue(void);
56 
57 static void queue_process(void *p)
58 {
59 	unsigned long flags;
60 	struct sk_buff *skb;
61 
62 	while (queue_head) {
63 		spin_lock_irqsave(&queue_lock, flags);
64 
65 		skb = queue_head;
66 		queue_head = skb->next;
67 		if (skb == queue_tail)
68 			queue_head = NULL;
69 
70 		queue_depth--;
71 
72 		spin_unlock_irqrestore(&queue_lock, flags);
73 
74 		dev_queue_xmit(skb);
75 	}
76 }
77 
78 static DECLARE_WORK(send_queue, queue_process, NULL);
79 
80 void netpoll_queue(struct sk_buff *skb)
81 {
82 	unsigned long flags;
83 
84 	if (queue_depth == MAX_QUEUE_DEPTH) {
85 		__kfree_skb(skb);
86 		return;
87 	}
88 
89 	spin_lock_irqsave(&queue_lock, flags);
90 	if (!queue_head)
91 		queue_head = skb;
92 	else
93 		queue_tail->next = skb;
94 	queue_tail = skb;
95 	queue_depth++;
96 	spin_unlock_irqrestore(&queue_lock, flags);
97 
98 	schedule_work(&send_queue);
99 }
100 
101 static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
102 			     unsigned short ulen, u32 saddr, u32 daddr)
103 {
104 	if (uh->check == 0)
105 		return 0;
106 
107 	if (skb->ip_summed == CHECKSUM_HW)
108 		return csum_tcpudp_magic(
109 			saddr, daddr, ulen, IPPROTO_UDP, skb->csum);
110 
111 	skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
112 
113 	return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
114 }
115 
116 /*
117  * Check whether delayed processing was scheduled for our NIC. If so,
118  * we attempt to grab the poll lock and use ->poll() to pump the card.
119  * If this fails, either we've recursed in ->poll() or it's already
120  * running on another CPU.
121  *
122  * Note: we don't mask interrupts with this lock because we're using
123  * trylock here and interrupts are already disabled in the softirq
124  * case. Further, we test the poll_owner to avoid recursion on UP
125  * systems where the lock doesn't exist.
126  *
127  * In cases where there is bi-directional communications, reading only
128  * one message at a time can lead to packets being dropped by the
129  * network adapter, forcing superfluous retries and possibly timeouts.
130  * Thus, we set our budget to greater than 1.
131  */
132 static void poll_napi(struct netpoll *np)
133 {
134 	struct netpoll_info *npinfo = np->dev->npinfo;
135 	int budget = 16;
136 
137 	if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
138 	    npinfo->poll_owner != smp_processor_id() &&
139 	    spin_trylock(&npinfo->poll_lock)) {
140 		npinfo->rx_flags |= NETPOLL_RX_DROP;
141 		atomic_inc(&trapped);
142 
143 		np->dev->poll(np->dev, &budget);
144 
145 		atomic_dec(&trapped);
146 		npinfo->rx_flags &= ~NETPOLL_RX_DROP;
147 		spin_unlock(&npinfo->poll_lock);
148 	}
149 }
150 
151 void netpoll_poll(struct netpoll *np)
152 {
153 	if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
154 		return;
155 
156 	/* Process pending work on NIC */
157 	np->dev->poll_controller(np->dev);
158 	if (np->dev->poll)
159 		poll_napi(np);
160 
161 	zap_completion_queue();
162 }
163 
164 static void refill_skbs(void)
165 {
166 	struct sk_buff *skb;
167 	unsigned long flags;
168 
169 	spin_lock_irqsave(&skb_list_lock, flags);
170 	while (nr_skbs < MAX_SKBS) {
171 		skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
172 		if (!skb)
173 			break;
174 
175 		skb->next = skbs;
176 		skbs = skb;
177 		nr_skbs++;
178 	}
179 	spin_unlock_irqrestore(&skb_list_lock, flags);
180 }
181 
182 static void zap_completion_queue(void)
183 {
184 	unsigned long flags;
185 	struct softnet_data *sd = &get_cpu_var(softnet_data);
186 
187 	if (sd->completion_queue) {
188 		struct sk_buff *clist;
189 
190 		local_irq_save(flags);
191 		clist = sd->completion_queue;
192 		sd->completion_queue = NULL;
193 		local_irq_restore(flags);
194 
195 		while (clist != NULL) {
196 			struct sk_buff *skb = clist;
197 			clist = clist->next;
198 			if(skb->destructor)
199 				dev_kfree_skb_any(skb); /* put this one back */
200 			else
201 				__kfree_skb(skb);
202 		}
203 	}
204 
205 	put_cpu_var(softnet_data);
206 }
207 
208 static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
209 {
210 	int once = 1, count = 0;
211 	unsigned long flags;
212 	struct sk_buff *skb = NULL;
213 
214 	zap_completion_queue();
215 repeat:
216 	if (nr_skbs < MAX_SKBS)
217 		refill_skbs();
218 
219 	skb = alloc_skb(len, GFP_ATOMIC);
220 
221 	if (!skb) {
222 		spin_lock_irqsave(&skb_list_lock, flags);
223 		skb = skbs;
224 		if (skb) {
225 			skbs = skb->next;
226 			skb->next = NULL;
227 			nr_skbs--;
228 		}
229 		spin_unlock_irqrestore(&skb_list_lock, flags);
230 	}
231 
232 	if(!skb) {
233 		count++;
234 		if (once && (count == 1000000)) {
235 			printk("out of netpoll skbs!\n");
236 			once = 0;
237 		}
238 		netpoll_poll(np);
239 		goto repeat;
240 	}
241 
242 	atomic_set(&skb->users, 1);
243 	skb_reserve(skb, reserve);
244 	return skb;
245 }
246 
247 static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
248 {
249 	int status;
250 	struct netpoll_info *npinfo;
251 
252 	if (!np || !np->dev || !netif_running(np->dev)) {
253 		__kfree_skb(skb);
254 		return;
255 	}
256 
257 	npinfo = np->dev->npinfo;
258 
259 	/* avoid recursion */
260 	if (npinfo->poll_owner == smp_processor_id() ||
261 	    np->dev->xmit_lock_owner == smp_processor_id()) {
262 		if (np->drop)
263 			np->drop(skb);
264 		else
265 			__kfree_skb(skb);
266 		return;
267 	}
268 
269 	do {
270 		npinfo->tries--;
271 		spin_lock(&np->dev->xmit_lock);
272 		np->dev->xmit_lock_owner = smp_processor_id();
273 
274 		/*
275 		 * network drivers do not expect to be called if the queue is
276 		 * stopped.
277 		 */
278 		if (netif_queue_stopped(np->dev)) {
279 			np->dev->xmit_lock_owner = -1;
280 			spin_unlock(&np->dev->xmit_lock);
281 			netpoll_poll(np);
282 			udelay(50);
283 			continue;
284 		}
285 
286 		status = np->dev->hard_start_xmit(skb, np->dev);
287 		np->dev->xmit_lock_owner = -1;
288 		spin_unlock(&np->dev->xmit_lock);
289 
290 		/* success */
291 		if(!status) {
292 			npinfo->tries = MAX_RETRIES; /* reset */
293 			return;
294 		}
295 
296 		/* transmit busy */
297 		netpoll_poll(np);
298 		udelay(50);
299 	} while (npinfo->tries > 0);
300 }
301 
302 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
303 {
304 	int total_len, eth_len, ip_len, udp_len;
305 	struct sk_buff *skb;
306 	struct udphdr *udph;
307 	struct iphdr *iph;
308 	struct ethhdr *eth;
309 
310 	udp_len = len + sizeof(*udph);
311 	ip_len = eth_len = udp_len + sizeof(*iph);
312 	total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
313 
314 	skb = find_skb(np, total_len, total_len - len);
315 	if (!skb)
316 		return;
317 
318 	memcpy(skb->data, msg, len);
319 	skb->len += len;
320 
321 	udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
322 	udph->source = htons(np->local_port);
323 	udph->dest = htons(np->remote_port);
324 	udph->len = htons(udp_len);
325 	udph->check = 0;
326 
327 	iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
328 
329 	/* iph->version = 4; iph->ihl = 5; */
330 	put_unaligned(0x45, (unsigned char *)iph);
331 	iph->tos      = 0;
332 	put_unaligned(htons(ip_len), &(iph->tot_len));
333 	iph->id       = 0;
334 	iph->frag_off = 0;
335 	iph->ttl      = 64;
336 	iph->protocol = IPPROTO_UDP;
337 	iph->check    = 0;
338 	put_unaligned(htonl(np->local_ip), &(iph->saddr));
339 	put_unaligned(htonl(np->remote_ip), &(iph->daddr));
340 	iph->check    = ip_fast_csum((unsigned char *)iph, iph->ihl);
341 
342 	eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
343 
344 	eth->h_proto = htons(ETH_P_IP);
345 	memcpy(eth->h_source, np->local_mac, 6);
346 	memcpy(eth->h_dest, np->remote_mac, 6);
347 
348 	skb->dev = np->dev;
349 
350 	netpoll_send_skb(np, skb);
351 }
352 
353 static void arp_reply(struct sk_buff *skb)
354 {
355 	struct netpoll_info *npinfo = skb->dev->npinfo;
356 	struct arphdr *arp;
357 	unsigned char *arp_ptr;
358 	int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
359 	u32 sip, tip;
360 	struct sk_buff *send_skb;
361 	struct netpoll *np = NULL;
362 
363 	if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
364 		np = npinfo->rx_np;
365 	if (!np)
366 		return;
367 
368 	/* No arp on this interface */
369 	if (skb->dev->flags & IFF_NOARP)
370 		return;
371 
372 	if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
373 				 (2 * skb->dev->addr_len) +
374 				 (2 * sizeof(u32)))))
375 		return;
376 
377 	skb->h.raw = skb->nh.raw = skb->data;
378 	arp = skb->nh.arph;
379 
380 	if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
381 	     arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
382 	    arp->ar_pro != htons(ETH_P_IP) ||
383 	    arp->ar_op != htons(ARPOP_REQUEST))
384 		return;
385 
386 	arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
387 	memcpy(&sip, arp_ptr, 4);
388 	arp_ptr += 4 + skb->dev->addr_len;
389 	memcpy(&tip, arp_ptr, 4);
390 
391 	/* Should we ignore arp? */
392 	if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
393 		return;
394 
395 	size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
396 	send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
397 			    LL_RESERVED_SPACE(np->dev));
398 
399 	if (!send_skb)
400 		return;
401 
402 	send_skb->nh.raw = send_skb->data;
403 	arp = (struct arphdr *) skb_put(send_skb, size);
404 	send_skb->dev = skb->dev;
405 	send_skb->protocol = htons(ETH_P_ARP);
406 
407 	/* Fill the device header for the ARP frame */
408 
409 	if (np->dev->hard_header &&
410 	    np->dev->hard_header(send_skb, skb->dev, ptype,
411 				       np->remote_mac, np->local_mac,
412 				       send_skb->len) < 0) {
413 		kfree_skb(send_skb);
414 		return;
415 	}
416 
417 	/*
418 	 * Fill out the arp protocol part.
419 	 *
420 	 * we only support ethernet device type,
421 	 * which (according to RFC 1390) should always equal 1 (Ethernet).
422 	 */
423 
424 	arp->ar_hrd = htons(np->dev->type);
425 	arp->ar_pro = htons(ETH_P_IP);
426 	arp->ar_hln = np->dev->addr_len;
427 	arp->ar_pln = 4;
428 	arp->ar_op = htons(type);
429 
430 	arp_ptr=(unsigned char *)(arp + 1);
431 	memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
432 	arp_ptr += np->dev->addr_len;
433 	memcpy(arp_ptr, &tip, 4);
434 	arp_ptr += 4;
435 	memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
436 	arp_ptr += np->dev->addr_len;
437 	memcpy(arp_ptr, &sip, 4);
438 
439 	netpoll_send_skb(np, send_skb);
440 }
441 
442 int __netpoll_rx(struct sk_buff *skb)
443 {
444 	int proto, len, ulen;
445 	struct iphdr *iph;
446 	struct udphdr *uh;
447 	struct netpoll *np = skb->dev->npinfo->rx_np;
448 
449 	if (!np)
450 		goto out;
451 	if (skb->dev->type != ARPHRD_ETHER)
452 		goto out;
453 
454 	/* check if netpoll clients need ARP */
455 	if (skb->protocol == __constant_htons(ETH_P_ARP) &&
456 	    atomic_read(&trapped)) {
457 		arp_reply(skb);
458 		return 1;
459 	}
460 
461 	proto = ntohs(eth_hdr(skb)->h_proto);
462 	if (proto != ETH_P_IP)
463 		goto out;
464 	if (skb->pkt_type == PACKET_OTHERHOST)
465 		goto out;
466 	if (skb_shared(skb))
467 		goto out;
468 
469 	iph = (struct iphdr *)skb->data;
470 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
471 		goto out;
472 	if (iph->ihl < 5 || iph->version != 4)
473 		goto out;
474 	if (!pskb_may_pull(skb, iph->ihl*4))
475 		goto out;
476 	if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
477 		goto out;
478 
479 	len = ntohs(iph->tot_len);
480 	if (skb->len < len || len < iph->ihl*4)
481 		goto out;
482 
483 	if (iph->protocol != IPPROTO_UDP)
484 		goto out;
485 
486 	len -= iph->ihl*4;
487 	uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
488 	ulen = ntohs(uh->len);
489 
490 	if (ulen != len)
491 		goto out;
492 	if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0)
493 		goto out;
494 	if (np->local_ip && np->local_ip != ntohl(iph->daddr))
495 		goto out;
496 	if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
497 		goto out;
498 	if (np->local_port && np->local_port != ntohs(uh->dest))
499 		goto out;
500 
501 	np->rx_hook(np, ntohs(uh->source),
502 		    (char *)(uh+1),
503 		    ulen - sizeof(struct udphdr));
504 
505 	kfree_skb(skb);
506 	return 1;
507 
508 out:
509 	if (atomic_read(&trapped)) {
510 		kfree_skb(skb);
511 		return 1;
512 	}
513 
514 	return 0;
515 }
516 
517 int netpoll_parse_options(struct netpoll *np, char *opt)
518 {
519 	char *cur=opt, *delim;
520 
521 	if(*cur != '@') {
522 		if ((delim = strchr(cur, '@')) == NULL)
523 			goto parse_failed;
524 		*delim=0;
525 		np->local_port=simple_strtol(cur, NULL, 10);
526 		cur=delim;
527 	}
528 	cur++;
529 	printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
530 
531 	if(*cur != '/') {
532 		if ((delim = strchr(cur, '/')) == NULL)
533 			goto parse_failed;
534 		*delim=0;
535 		np->local_ip=ntohl(in_aton(cur));
536 		cur=delim;
537 
538 		printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
539 		       np->name, HIPQUAD(np->local_ip));
540 	}
541 	cur++;
542 
543 	if ( *cur != ',') {
544 		/* parse out dev name */
545 		if ((delim = strchr(cur, ',')) == NULL)
546 			goto parse_failed;
547 		*delim=0;
548 		strlcpy(np->dev_name, cur, sizeof(np->dev_name));
549 		cur=delim;
550 	}
551 	cur++;
552 
553 	printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
554 
555 	if ( *cur != '@' ) {
556 		/* dst port */
557 		if ((delim = strchr(cur, '@')) == NULL)
558 			goto parse_failed;
559 		*delim=0;
560 		np->remote_port=simple_strtol(cur, NULL, 10);
561 		cur=delim;
562 	}
563 	cur++;
564 	printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
565 
566 	/* dst ip */
567 	if ((delim = strchr(cur, '/')) == NULL)
568 		goto parse_failed;
569 	*delim=0;
570 	np->remote_ip=ntohl(in_aton(cur));
571 	cur=delim+1;
572 
573 	printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
574 		       np->name, HIPQUAD(np->remote_ip));
575 
576 	if( *cur != 0 )
577 	{
578 		/* MAC address */
579 		if ((delim = strchr(cur, ':')) == NULL)
580 			goto parse_failed;
581 		*delim=0;
582 		np->remote_mac[0]=simple_strtol(cur, NULL, 16);
583 		cur=delim+1;
584 		if ((delim = strchr(cur, ':')) == NULL)
585 			goto parse_failed;
586 		*delim=0;
587 		np->remote_mac[1]=simple_strtol(cur, NULL, 16);
588 		cur=delim+1;
589 		if ((delim = strchr(cur, ':')) == NULL)
590 			goto parse_failed;
591 		*delim=0;
592 		np->remote_mac[2]=simple_strtol(cur, NULL, 16);
593 		cur=delim+1;
594 		if ((delim = strchr(cur, ':')) == NULL)
595 			goto parse_failed;
596 		*delim=0;
597 		np->remote_mac[3]=simple_strtol(cur, NULL, 16);
598 		cur=delim+1;
599 		if ((delim = strchr(cur, ':')) == NULL)
600 			goto parse_failed;
601 		*delim=0;
602 		np->remote_mac[4]=simple_strtol(cur, NULL, 16);
603 		cur=delim+1;
604 		np->remote_mac[5]=simple_strtol(cur, NULL, 16);
605 	}
606 
607 	printk(KERN_INFO "%s: remote ethernet address "
608 	       "%02x:%02x:%02x:%02x:%02x:%02x\n",
609 	       np->name,
610 	       np->remote_mac[0],
611 	       np->remote_mac[1],
612 	       np->remote_mac[2],
613 	       np->remote_mac[3],
614 	       np->remote_mac[4],
615 	       np->remote_mac[5]);
616 
617 	return 0;
618 
619  parse_failed:
620 	printk(KERN_INFO "%s: couldn't parse config at %s!\n",
621 	       np->name, cur);
622 	return -1;
623 }
624 
625 int netpoll_setup(struct netpoll *np)
626 {
627 	struct net_device *ndev = NULL;
628 	struct in_device *in_dev;
629 	struct netpoll_info *npinfo;
630 	unsigned long flags;
631 
632 	if (np->dev_name)
633 		ndev = dev_get_by_name(np->dev_name);
634 	if (!ndev) {
635 		printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
636 		       np->name, np->dev_name);
637 		return -1;
638 	}
639 
640 	np->dev = ndev;
641 	if (!ndev->npinfo) {
642 		npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
643 		if (!npinfo)
644 			goto release;
645 
646 		npinfo->rx_flags = 0;
647 		npinfo->rx_np = NULL;
648 		spin_lock_init(&npinfo->poll_lock);
649 		npinfo->poll_owner = -1;
650 		npinfo->tries = MAX_RETRIES;
651 		spin_lock_init(&npinfo->rx_lock);
652 	} else
653 		npinfo = ndev->npinfo;
654 
655 	if (!ndev->poll_controller) {
656 		printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
657 		       np->name, np->dev_name);
658 		goto release;
659 	}
660 
661 	if (!netif_running(ndev)) {
662 		unsigned long atmost, atleast;
663 
664 		printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
665 		       np->name, np->dev_name);
666 
667 		rtnl_shlock();
668 		if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) {
669 			printk(KERN_ERR "%s: failed to open %s\n",
670 			       np->name, np->dev_name);
671 			rtnl_shunlock();
672 			goto release;
673 		}
674 		rtnl_shunlock();
675 
676 		atleast = jiffies + HZ/10;
677  		atmost = jiffies + 4*HZ;
678 		while (!netif_carrier_ok(ndev)) {
679 			if (time_after(jiffies, atmost)) {
680 				printk(KERN_NOTICE
681 				       "%s: timeout waiting for carrier\n",
682 				       np->name);
683 				break;
684 			}
685 			cond_resched();
686 		}
687 
688 		/* If carrier appears to come up instantly, we don't
689 		 * trust it and pause so that we don't pump all our
690 		 * queued console messages into the bitbucket.
691 		 */
692 
693 		if (time_before(jiffies, atleast)) {
694 			printk(KERN_NOTICE "%s: carrier detect appears"
695 			       " untrustworthy, waiting 4 seconds\n",
696 			       np->name);
697 			msleep(4000);
698 		}
699 	}
700 
701 	if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr)
702 		memcpy(np->local_mac, ndev->dev_addr, 6);
703 
704 	if (!np->local_ip) {
705 		rcu_read_lock();
706 		in_dev = __in_dev_get(ndev);
707 
708 		if (!in_dev || !in_dev->ifa_list) {
709 			rcu_read_unlock();
710 			printk(KERN_ERR "%s: no IP address for %s, aborting\n",
711 			       np->name, np->dev_name);
712 			goto release;
713 		}
714 
715 		np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
716 		rcu_read_unlock();
717 		printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
718 		       np->name, HIPQUAD(np->local_ip));
719 	}
720 
721 	if (np->rx_hook) {
722 		spin_lock_irqsave(&npinfo->rx_lock, flags);
723 		npinfo->rx_flags |= NETPOLL_RX_ENABLED;
724 		npinfo->rx_np = np;
725 		spin_unlock_irqrestore(&npinfo->rx_lock, flags);
726 	}
727 
728 	/* fill up the skb queue */
729 	refill_skbs();
730 
731 	/* last thing to do is link it to the net device structure */
732 	ndev->npinfo = npinfo;
733 
734 	/* avoid racing with NAPI reading npinfo */
735 	synchronize_rcu();
736 
737 	return 0;
738 
739  release:
740 	if (!ndev->npinfo)
741 		kfree(npinfo);
742 	np->dev = NULL;
743 	dev_put(ndev);
744 	return -1;
745 }
746 
747 void netpoll_cleanup(struct netpoll *np)
748 {
749 	struct netpoll_info *npinfo;
750 	unsigned long flags;
751 
752 	if (np->dev) {
753 		npinfo = np->dev->npinfo;
754 		if (npinfo && npinfo->rx_np == np) {
755 			spin_lock_irqsave(&npinfo->rx_lock, flags);
756 			npinfo->rx_np = NULL;
757 			npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
758 			spin_unlock_irqrestore(&npinfo->rx_lock, flags);
759 		}
760 		dev_put(np->dev);
761 	}
762 
763 	np->dev = NULL;
764 }
765 
766 int netpoll_trap(void)
767 {
768 	return atomic_read(&trapped);
769 }
770 
771 void netpoll_set_trap(int trap)
772 {
773 	if (trap)
774 		atomic_inc(&trapped);
775 	else
776 		atomic_dec(&trapped);
777 }
778 
779 EXPORT_SYMBOL(netpoll_set_trap);
780 EXPORT_SYMBOL(netpoll_trap);
781 EXPORT_SYMBOL(netpoll_parse_options);
782 EXPORT_SYMBOL(netpoll_setup);
783 EXPORT_SYMBOL(netpoll_cleanup);
784 EXPORT_SYMBOL(netpoll_send_udp);
785 EXPORT_SYMBOL(netpoll_poll);
786 EXPORT_SYMBOL(netpoll_queue);
787