Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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/netdevice.h>
 13 #include <linux/etherdevice.h>
 14 #include <linux/string.h>
 15 #include <linux/if_arp.h>
 16 #include <linux/inetdevice.h>
 17 #include <linux/inet.h>
 18 #include <linux/interrupt.h>
 19 #include <linux/netpoll.h>
 20 #include <linux/sched.h>
 21 #include <linux/delay.h>
 22 #include <linux/rcupdate.h>
 23 #include <linux/workqueue.h>
 24 #include <net/tcp.h>
 25 #include <net/udp.h>
 26 #include <asm/unaligned.h>
 27 
 28 /*
 29  * We maintain a small pool of fully-sized skbs, to make sure the
 30  * message gets out even in extreme OOM situations.
 31  */
 32 
 33 #define MAX_UDP_CHUNK 1460
 34 #define MAX_SKBS 32
 35 #define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
 36 
 37 static struct sk_buff_head skb_pool;
 38 
 39 static atomic_t trapped;
 40 
 41 #define USEC_PER_POLL   50
 42 #define NETPOLL_RX_ENABLED  1
 43 #define NETPOLL_RX_DROP     2
 44 
 45 #define MAX_SKB_SIZE \
 46                 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
 47                                 sizeof(struct iphdr) + sizeof(struct ethhdr))
 48 
 49 static void zap_completion_queue(void);
 50 static void arp_reply(struct sk_buff *skb);
 51 
 52 static void queue_process(struct work_struct *work)
 53 {
 54         struct netpoll_info *npinfo =
 55                 container_of(work, struct netpoll_info, tx_work.work);
 56         struct sk_buff *skb;
 57         unsigned long flags;
 58 
 59         while ((skb = skb_dequeue(&npinfo->txq))) {
 60                 struct net_device *dev = skb->dev;
 61 
 62                 if (!netif_device_present(dev) || !netif_running(dev)) {
 63                         __kfree_skb(skb);
 64                         continue;
 65                 }
 66 
 67                 local_irq_save_nort(flags);
 68                 netif_tx_lock(dev);
 69                 if ((netif_queue_stopped(dev) ||
 70                      netif_subqueue_stopped(dev, skb)) ||
 71                      dev->hard_start_xmit(skb, dev) != NETDEV_TX_OK) {
 72                         skb_queue_head(&npinfo->txq, skb);
 73                         netif_tx_unlock(dev);
 74                         local_irq_restore_nort(flags);
 75 
 76                         schedule_delayed_work(&npinfo->tx_work, HZ/10);
 77                         return;
 78                 }
 79                 netif_tx_unlock(dev);
 80                 local_irq_restore_nort(flags);
 81         }
 82 }
 83 
 84 static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
 85                             unsigned short ulen, __be32 saddr, __be32 daddr)
 86 {
 87         __wsum psum;
 88 
 89         if (uh->check == 0 || skb_csum_unnecessary(skb))
 90                 return 0;
 91 
 92         psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
 93 
 94         if (skb->ip_summed == CHECKSUM_COMPLETE &&
 95             !csum_fold(csum_add(psum, skb->csum)))
 96                 return 0;
 97 
 98         skb->csum = psum;
 99 
100         return __skb_checksum_complete(skb);
101 }
102 
103 /*
104  * Check whether delayed processing was scheduled for our NIC. If so,
105  * we attempt to grab the poll lock and use ->poll() to pump the card.
106  * If this fails, either we've recursed in ->poll() or it's already
107  * running on another CPU.
108  *
109  * Note: we don't mask interrupts with this lock because we're using
110  * trylock here and interrupts are already disabled in the softirq
111  * case. Further, we test the poll_owner to avoid recursion on UP
112  * systems where the lock doesn't exist.
113  *
114  * In cases where there is bi-directional communications, reading only
115  * one message at a time can lead to packets being dropped by the
116  * network adapter, forcing superfluous retries and possibly timeouts.
117  * Thus, we set our budget to greater than 1.
118  */
119 static int poll_one_napi(struct netpoll_info *npinfo,
120                          struct napi_struct *napi, int budget)
121 {
122         int work;
123 
124         /* net_rx_action's ->poll() invocations and our's are
125          * synchronized by this test which is only made while
126          * holding the napi->poll_lock.
127          */
128         if (!test_bit(NAPI_STATE_SCHED, &napi->state))
129                 return budget;
130 
131         npinfo->rx_flags |= NETPOLL_RX_DROP;
132         atomic_inc(&trapped);
133 
134         work = napi->poll(napi, budget);
135 
136         atomic_dec(&trapped);
137         npinfo->rx_flags &= ~NETPOLL_RX_DROP;
138 
139         return budget - work;
140 }
141 
142 static void poll_napi(struct net_device *dev)
143 {
144         struct napi_struct *napi;
145         int budget = 16;
146 
147         list_for_each_entry(napi, &dev->napi_list, dev_list) {
148                 if (napi->poll_owner != raw_smp_processor_id() &&
149                     spin_trylock(&napi->poll_lock)) {
150                         budget = poll_one_napi(dev->npinfo, napi, budget);
151                         spin_unlock(&napi->poll_lock);
152 
153                         if (!budget)
154                                 break;
155                 }
156         }
157 }
158 
159 static void service_arp_queue(struct netpoll_info *npi)
160 {
161         if (npi) {
162                 struct sk_buff *skb;
163 
164                 while ((skb = skb_dequeue(&npi->arp_tx)))
165                         arp_reply(skb);
166         }
167 }
168 
169 void netpoll_poll(struct netpoll *np)
170 {
171         struct net_device *dev = np->dev;
172 
173         if (!dev || !netif_running(dev) || !dev->poll_controller)
174                 return;
175 
176         /* Process pending work on NIC */
177         dev->poll_controller(dev);
178 
179         poll_napi(dev);
180 
181         service_arp_queue(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         struct softnet_data *sd = &get_cpu_var(softnet_data);
205         struct sk_buff *clist = NULL;
206         unsigned long flags;
207 
208         if (sd->completion_queue) {
209 
210                 local_irq_save(flags);
211                 clist = sd->completion_queue;
212                 sd->completion_queue = NULL;
213                 local_irq_restore(flags);
214         }
215 
216 
217         /*
218          * Took the list private, can drop our softnet
219          * reference:
220          */
221         put_cpu_var(softnet_data);
222 
223         while (clist != NULL) {
224                 struct sk_buff *skb = clist;
225                 clist = clist->next;
226                 if (skb->destructor) {
227                         atomic_inc(&skb->users);
228                         dev_kfree_skb_any(skb); /* put this one back */
229                 } else {
230                         __kfree_skb(skb);
231                 }
232         }
233 }
234 
235 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
236 {
237         int count = 0;
238         struct sk_buff *skb;
239 
240 #ifdef CONFIG_PREEMPT_RT
241         /*
242          * On -rt skb_pool.lock is schedulable, so if we are
243          * in an atomic context we just try to dequeue from the
244          * pool and fail if we cannot get one.
245          */
246         if (in_atomic() || irqs_disabled())
247                 goto pick_atomic;
248 #endif
249         zap_completion_queue();
250         refill_skbs();
251 repeat:
252 
253         skb = alloc_skb(len, GFP_ATOMIC);
254         if (!skb) {
255 #ifdef CONFIG_PREEMPT_RT
256 pick_atomic:
257 #endif
258                 skb = skb_dequeue(&skb_pool);
259         }
260 
261         if (!skb) {
262                 if (++count < 10) {
263                         netpoll_poll(np);
264                         goto repeat;
265                 }
266                 return NULL;
267         }
268 
269         atomic_set(&skb->users, 1);
270         skb_reserve(skb, reserve);
271         return skb;
272 }
273 
274 static int netpoll_owner_active(struct net_device *dev)
275 {
276         struct napi_struct *napi;
277 
278         list_for_each_entry(napi, &dev->napi_list, dev_list) {
279                 if (napi->poll_owner == raw_smp_processor_id())
280                         return 1;
281         }
282         return 0;
283 }
284 
285 static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
286 {
287         int status = NETDEV_TX_BUSY;
288         unsigned long tries;
289         struct net_device *dev = np->dev;
290         struct netpoll_info *npinfo = np->dev->npinfo;
291 
292         if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
293                 __kfree_skb(skb);
294                 return;
295         }
296 
297         /* don't get messages out of order, and no recursion */
298         if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
299                 unsigned long flags;
300 
301                 local_irq_save_nort(flags);
302                 /* try until next clock tick */
303                 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
304                      tries > 0; --tries) {
305                         if (netif_tx_trylock(dev)) {
306                                 if (!netif_queue_stopped(dev) &&
307                                     !netif_subqueue_stopped(dev, skb))
308                                         status = dev->hard_start_xmit(skb, dev);
309                                 netif_tx_unlock(dev);
310 
311                                 if (status == NETDEV_TX_OK)
312                                         break;
313 
314                         }
315 
316                         /* tickle device maybe there is some cleanup */
317                         netpoll_poll(np);
318 
319                         udelay(USEC_PER_POLL);
320                 }
321                 local_irq_restore_nort(flags);
322         }
323 
324         if (status != NETDEV_TX_OK) {
325                 skb_queue_tail(&npinfo->txq, skb);
326                 schedule_delayed_work(&npinfo->tx_work,0);
327         }
328 }
329 
330 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
331 {
332         int total_len, eth_len, ip_len, udp_len;
333         struct sk_buff *skb;
334         struct udphdr *udph;
335         struct iphdr *iph;
336         struct ethhdr *eth;
337 
338         udp_len = len + sizeof(*udph);
339         ip_len = eth_len = udp_len + sizeof(*iph);
340         total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
341 
342         skb = find_skb(np, total_len, total_len - len);
343         if (!skb)
344                 return;
345 
346         skb_copy_to_linear_data(skb, msg, len);
347         skb->len += len;
348 
349         skb_push(skb, sizeof(*udph));
350         skb_reset_transport_header(skb);
351         udph = udp_hdr(skb);
352         udph->source = htons(np->local_port);
353         udph->dest = htons(np->remote_port);
354         udph->len = htons(udp_len);
355         udph->check = 0;
356         udph->check = csum_tcpudp_magic(htonl(np->local_ip),
357                                         htonl(np->remote_ip),
358                                         udp_len, IPPROTO_UDP,
359                                         csum_partial((unsigned char *)udph, udp_len, 0));
360         if (udph->check == 0)
361                 udph->check = CSUM_MANGLED_0;
362 
363         skb_push(skb, sizeof(*iph));
364         skb_reset_network_header(skb);
365         iph = ip_hdr(skb);
366 
367         /* iph->version = 4; iph->ihl = 5; */
368         put_unaligned(0x45, (unsigned char *)iph);
369         iph->tos      = 0;
370         put_unaligned(htons(ip_len), &(iph->tot_len));
371         iph->id       = 0;
372         iph->frag_off = 0;
373         iph->ttl      = 64;
374         iph->protocol = IPPROTO_UDP;
375         iph->check    = 0;
376         put_unaligned(htonl(np->local_ip), &(iph->saddr));
377         put_unaligned(htonl(np->remote_ip), &(iph->daddr));
378         iph->check    = ip_fast_csum((unsigned char *)iph, iph->ihl);
379 
380         eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
381         skb_reset_mac_header(skb);
382         skb->protocol = eth->h_proto = htons(ETH_P_IP);
383         memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
384         memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
385 
386         skb->dev = np->dev;
387 
388         netpoll_send_skb(np, skb);
389 }
390 
391 static void arp_reply(struct sk_buff *skb)
392 {
393         struct netpoll_info *npinfo = skb->dev->npinfo;
394         struct arphdr *arp;
395         unsigned char *arp_ptr;
396         int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
397         __be32 sip, tip;
398         unsigned char *sha;
399         struct sk_buff *send_skb;
400         struct netpoll *np = NULL;
401 
402         if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
403                 np = npinfo->rx_np;
404         if (!np)
405                 return;
406 
407         /* No arp on this interface */
408         if (skb->dev->flags & IFF_NOARP)
409                 return;
410 
411         if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
412                                  (2 * skb->dev->addr_len) +
413                                  (2 * sizeof(u32)))))
414                 return;
415 
416         skb_reset_network_header(skb);
417         skb_reset_transport_header(skb);
418         arp = arp_hdr(skb);
419 
420         if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
421              arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
422             arp->ar_pro != htons(ETH_P_IP) ||
423             arp->ar_op != htons(ARPOP_REQUEST))
424                 return;
425 
426         arp_ptr = (unsigned char *)(arp+1);
427         /* save the location of the src hw addr */
428         sha = arp_ptr;
429         arp_ptr += skb->dev->addr_len;
430         memcpy(&sip, arp_ptr, 4);
431         arp_ptr += 4;
432         /* if we actually cared about dst hw addr, it would get copied here */
433         arp_ptr += skb->dev->addr_len;
434         memcpy(&tip, arp_ptr, 4);
435 
436         /* Should we ignore arp? */
437         if (tip != htonl(np->local_ip) ||
438             ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
439                 return;
440 
441         size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
442         send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
443                             LL_RESERVED_SPACE(np->dev));
444 
445         if (!send_skb)
446                 return;
447 
448         skb_reset_network_header(send_skb);
449         arp = (struct arphdr *) skb_put(send_skb, size);
450         send_skb->dev = skb->dev;
451         send_skb->protocol = htons(ETH_P_ARP);
452 
453         /* Fill the device header for the ARP frame */
454         if (dev_hard_header(send_skb, skb->dev, ptype,
455                             sha, np->dev->dev_addr,
456                             send_skb->len) < 0) {
457                 kfree_skb(send_skb);
458                 return;
459         }
460 
461         /*
462          * Fill out the arp protocol part.
463          *
464          * we only support ethernet device type,
465          * which (according to RFC 1390) should always equal 1 (Ethernet).
466          */
467 
468         arp->ar_hrd = htons(np->dev->type);
469         arp->ar_pro = htons(ETH_P_IP);
470         arp->ar_hln = np->dev->addr_len;
471         arp->ar_pln = 4;
472         arp->ar_op = htons(type);
473 
474         arp_ptr=(unsigned char *)(arp + 1);
475         memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
476         arp_ptr += np->dev->addr_len;
477         memcpy(arp_ptr, &tip, 4);
478         arp_ptr += 4;
479         memcpy(arp_ptr, sha, np->dev->addr_len);
480         arp_ptr += np->dev->addr_len;
481         memcpy(arp_ptr, &sip, 4);
482 
483         netpoll_send_skb(np, send_skb);
484 }
485 
486 int __netpoll_rx(struct sk_buff *skb)
487 {
488         int proto, len, ulen;
489         struct iphdr *iph;
490         struct udphdr *uh;
491         struct netpoll_info *npi = skb->dev->npinfo;
492         struct netpoll *np = npi->rx_np;
493 
494         if (!np)
495                 goto out;
496         if (skb->dev->type != ARPHRD_ETHER)
497                 goto out;
498 
499         /* check if netpoll clients need ARP */
500         if (skb->protocol == htons(ETH_P_ARP) &&
501             atomic_read(&trapped)) {
502                 skb_queue_tail(&npi->arp_tx, skb);
503                 return 1;
504         }
505 
506         proto = ntohs(eth_hdr(skb)->h_proto);
507         if (proto != ETH_P_IP)
508                 goto out;
509         if (skb->pkt_type == PACKET_OTHERHOST)
510                 goto out;
511         if (skb_shared(skb))
512                 goto out;
513 
514         iph = (struct iphdr *)skb->data;
515         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
516                 goto out;
517         if (iph->ihl < 5 || iph->version != 4)
518                 goto out;
519         if (!pskb_may_pull(skb, iph->ihl*4))
520                 goto out;
521         if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
522                 goto out;
523 
524         len = ntohs(iph->tot_len);
525         if (skb->len < len || len < iph->ihl*4)
526                 goto out;
527 
528         /*
529          * Our transport medium may have padded the buffer out.
530          * Now We trim to the true length of the frame.
531          */
532         if (pskb_trim_rcsum(skb, len))
533                 goto out;
534 
535         if (iph->protocol != IPPROTO_UDP)
536                 goto out;
537 
538         len -= iph->ihl*4;
539         uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
540         ulen = ntohs(uh->len);
541 
542         if (ulen != len)
543                 goto out;
544         if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
545                 goto out;
546         if (np->local_ip && np->local_ip != ntohl(iph->daddr))
547                 goto out;
548         if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
549                 goto out;
550         if (np->local_port && np->local_port != ntohs(uh->dest))
551                 goto out;
552 
553         np->rx_hook(np, ntohs(uh->source),
554                     (char *)(uh+1),
555                     ulen - sizeof(struct udphdr));
556 
557         kfree_skb(skb);
558         return 1;
559 
560 out:
561         if (atomic_read(&trapped)) {
562                 kfree_skb(skb);
563                 return 1;
564         }
565 
566         return 0;
567 }
568 
569 void netpoll_print_options(struct netpoll *np)
570 {
571         DECLARE_MAC_BUF(mac);
572         printk(KERN_INFO "%s: local port %d\n",
573                          np->name, np->local_port);
574         printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
575                          np->name, HIPQUAD(np->local_ip));
576         printk(KERN_INFO "%s: interface %s\n",
577                          np->name, np->dev_name);
578         printk(KERN_INFO "%s: remote port %d\n",
579                          np->name, np->remote_port);
580         printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
581                          np->name, HIPQUAD(np->remote_ip));
582         printk(KERN_INFO "%s: remote ethernet address %s\n",
583                          np->name, print_mac(mac, np->remote_mac));
584 }
585 
586 int netpoll_parse_options(struct netpoll *np, char *opt)
587 {
588         char *cur=opt, *delim;
589 
590         if (*cur != '@') {
591                 if ((delim = strchr(cur, '@')) == NULL)
592                         goto parse_failed;
593                 *delim = 0;
594                 np->local_port = simple_strtol(cur, NULL, 10);
595                 cur = delim;
596         }
597         cur++;
598 
599         if (*cur != '/') {
600                 if ((delim = strchr(cur, '/')) == NULL)
601                         goto parse_failed;
602                 *delim = 0;
603                 np->local_ip = ntohl(in_aton(cur));
604                 cur = delim;
605         }
606         cur++;
607 
608         if (*cur != ',') {
609                 /* parse out dev name */
610                 if ((delim = strchr(cur, ',')) == NULL)
611                         goto parse_failed;
612                 *delim = 0;
613                 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
614                 cur = delim;
615         }
616         cur++;
617 
618         if (*cur != '@') {
619                 /* dst port */
620                 if ((delim = strchr(cur, '@')) == NULL)
621                         goto parse_failed;
622                 *delim = 0;
623                 np->remote_port = simple_strtol(cur, NULL, 10);
624                 cur = delim;
625         }
626         cur++;
627 
628         /* dst ip */
629         if ((delim = strchr(cur, '/')) == NULL)
630                 goto parse_failed;
631         *delim = 0;
632         np->remote_ip = ntohl(in_aton(cur));
633         cur = delim + 1;
634 
635         if (*cur != 0) {
636                 /* MAC address */
637                 if ((delim = strchr(cur, ':')) == NULL)
638                         goto parse_failed;
639                 *delim = 0;
640                 np->remote_mac[0] = simple_strtol(cur, NULL, 16);
641                 cur = delim + 1;
642                 if ((delim = strchr(cur, ':')) == NULL)
643                         goto parse_failed;
644                 *delim = 0;
645                 np->remote_mac[1] = simple_strtol(cur, NULL, 16);
646                 cur = delim + 1;
647                 if ((delim = strchr(cur, ':')) == NULL)
648                         goto parse_failed;
649                 *delim = 0;
650                 np->remote_mac[2] = simple_strtol(cur, NULL, 16);
651                 cur = delim + 1;
652                 if ((delim = strchr(cur, ':')) == NULL)
653                         goto parse_failed;
654                 *delim = 0;
655                 np->remote_mac[3] = simple_strtol(cur, NULL, 16);
656                 cur = delim + 1;
657                 if ((delim = strchr(cur, ':')) == NULL)
658                         goto parse_failed;
659                 *delim = 0;
660                 np->remote_mac[4] = simple_strtol(cur, NULL, 16);
661                 cur = delim + 1;
662                 np->remote_mac[5] = simple_strtol(cur, NULL, 16);
663         }
664 
665         netpoll_print_options(np);
666 
667         return 0;
668 
669  parse_failed:
670         printk(KERN_INFO "%s: couldn't parse config at %s!\n",
671                np->name, cur);
672         return -1;
673 }
674 
675 int netpoll_setup(struct netpoll *np)
676 {
677         struct net_device *ndev = NULL;
678         struct in_device *in_dev;
679         struct netpoll_info *npinfo;
680         unsigned long flags;
681         int err;
682 
683         if (np->dev_name)
684                 ndev = dev_get_by_name(&init_net, np->dev_name);
685         if (!ndev) {
686                 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
687                        np->name, np->dev_name);
688                 return -ENODEV;
689         }
690 
691         np->dev = ndev;
692         if (!ndev->npinfo) {
693                 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
694                 if (!npinfo) {
695                         err = -ENOMEM;
696                         goto release;
697                 }
698 
699                 npinfo->rx_flags = 0;
700                 npinfo->rx_np = NULL;
701 
702                 spin_lock_init(&npinfo->rx_lock);
703                 skb_queue_head_init(&npinfo->arp_tx);
704                 skb_queue_head_init(&npinfo->txq);
705                 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
706 
707                 atomic_set(&npinfo->refcnt, 1);
708         } else {
709                 npinfo = ndev->npinfo;
710                 atomic_inc(&npinfo->refcnt);
711         }
712 
713         if (!ndev->poll_controller) {
714                 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
715                        np->name, np->dev_name);
716                 err = -ENOTSUPP;
717                 goto release;
718         }
719 
720         if (!netif_running(ndev)) {
721                 unsigned long atmost, atleast;
722 
723                 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
724                        np->name, np->dev_name);
725 
726                 rtnl_lock();
727                 err = dev_open(ndev);
728                 rtnl_unlock();
729 
730                 if (err) {
731                         printk(KERN_ERR "%s: failed to open %s\n",
732                                np->name, ndev->name);
733                         goto release;
734                 }
735 
736                 atleast = jiffies + HZ/10;
737                 atmost = jiffies + 4*HZ;
738                 while (!netif_carrier_ok(ndev)) {
739                         if (time_after(jiffies, atmost)) {
740                                 printk(KERN_NOTICE
741                                        "%s: timeout waiting for carrier\n",
742                                        np->name);
743                                 break;
744                         }
745                         schedule_timeout_uninterruptible(1);
746                 }
747 
748                 /* If carrier appears to come up instantly, we don't
749                  * trust it and pause so that we don't pump all our
750                  * queued console messages into the bitbucket.
751                  */
752 
753                 if (time_before(jiffies, atleast)) {
754                         printk(KERN_NOTICE "%s: carrier detect appears"
755                                " untrustworthy, waiting 4 seconds\n",
756                                np->name);
757                         msleep(4000);
758                 }
759         }
760 
761         if (!np->local_ip) {
762                 rcu_read_lock();
763                 in_dev = __in_dev_get_rcu(ndev);
764 
765                 if (!in_dev || !in_dev->ifa_list) {
766                         rcu_read_unlock();
767                         printk(KERN_ERR "%s: no IP address for %s, aborting\n",
768                                np->name, np->dev_name);
769                         err = -EDESTADDRREQ;
770                         goto release;
771                 }
772 
773                 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
774                 rcu_read_unlock();
775                 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
776                        np->name, HIPQUAD(np->local_ip));
777         }
778 
779         if (np->rx_hook) {
780                 spin_lock_irqsave(&npinfo->rx_lock, flags);
781                 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
782                 npinfo->rx_np = np;
783                 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
784         }
785 
786         /* fill up the skb queue */
787         refill_skbs();
788 
789         /* last thing to do is link it to the net device structure */
790         ndev->npinfo = npinfo;
791 
792         /* avoid racing with NAPI reading npinfo */
793         synchronize_rcu();
794 
795         return 0;
796 
797  release:
798         if (!ndev->npinfo)
799                 kfree(npinfo);
800         np->dev = NULL;
801         dev_put(ndev);
802         return err;
803 }
804 
805 static int __init netpoll_init(void)
806 {
807         skb_queue_head_init(&skb_pool);
808         return 0;
809 }
810 core_initcall(netpoll_init);
811 
812 void netpoll_cleanup(struct netpoll *np)
813 {
814         struct netpoll_info *npinfo;
815         unsigned long flags;
816 
817         if (np->dev) {
818                 npinfo = np->dev->npinfo;
819                 if (npinfo) {
820                         if (npinfo->rx_np == np) {
821                                 spin_lock_irqsave(&npinfo->rx_lock, flags);
822                                 npinfo->rx_np = NULL;
823                                 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
824                                 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
825                         }
826 
827                         if (atomic_dec_and_test(&npinfo->refcnt)) {
828                                 skb_queue_purge(&npinfo->arp_tx);
829                                 skb_queue_purge(&npinfo->txq);
830                                 cancel_rearming_delayed_work(&npinfo->tx_work);
831 
832                                 /* clean after last, unfinished work */
833                                 __skb_queue_purge(&npinfo->txq);
834                                 kfree(npinfo);
835                                 np->dev->npinfo = NULL;
836                         }
837                 }
838 
839                 dev_put(np->dev);
840         }
841 
842         np->dev = NULL;
843 }
844 
845 int netpoll_trap(void)
846 {
847         return atomic_read(&trapped);
848 }
849 
850 void netpoll_set_trap(int trap)
851 {
852         if (trap)
853                 atomic_inc(&trapped);
854         else
855                 atomic_dec(&trapped);
856 }
857 
858 EXPORT_SYMBOL(netpoll_set_trap);
859 EXPORT_SYMBOL(netpoll_trap);
860 EXPORT_SYMBOL(netpoll_print_options);
861 EXPORT_SYMBOL(netpoll_parse_options);
862 EXPORT_SYMBOL(netpoll_setup);
863 EXPORT_SYMBOL(netpoll_cleanup);
864 EXPORT_SYMBOL(netpoll_send_udp);
865 EXPORT_SYMBOL(netpoll_poll);
866 
  This page was automatically generated by the LXR engine.