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