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