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