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