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