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