patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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/inetdevice.h>
17 #include <linux/inet.h>
18 #include <linux/interrupt.h>
19 #include <linux/netpoll.h>
20 #include <linux/sched.h>
21 #include <net/tcp.h>
22 #include <net/udp.h>
23
24 /*
25  * We maintain a small pool of fully-sized skbs, to make sure the
26  * message gets out even in extreme OOM situations.
27  */
28
29 #define MAX_SKBS 32
30 #define MAX_UDP_CHUNK 1460
31
32 static spinlock_t skb_list_lock = SPIN_LOCK_UNLOCKED;
33 static int nr_skbs;
34 static struct sk_buff *skbs;
35
36 static spinlock_t rx_list_lock = SPIN_LOCK_UNLOCKED;
37 static LIST_HEAD(rx_list);
38
39 static int trapped;
40
41 #define MAX_SKB_SIZE \
42                 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
43                                 sizeof(struct iphdr) + sizeof(struct ethhdr))
44
45 static void zap_completion_queue(void);
46
47 static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
48                              unsigned short ulen, u32 saddr, u32 daddr)
49 {
50         if (uh->check == 0)
51                 return 0;
52
53         if (skb->ip_summed == CHECKSUM_HW)
54                 return csum_tcpudp_magic(
55                         saddr, daddr, ulen, IPPROTO_UDP, skb->csum);
56
57         skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
58
59         return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
60 }
61
62 void netpoll_poll(struct netpoll *np)
63 {
64         int budget = 1;
65
66         if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
67                 return;
68
69         /* Process pending work on NIC */
70         np->dev->poll_controller(np->dev);
71
72         /* If scheduling is stopped, tickle NAPI bits */
73         if(trapped && np->dev->poll &&
74            test_bit(__LINK_STATE_RX_SCHED, &np->dev->state))
75                 np->dev->poll(np->dev, &budget);
76         zap_completion_queue();
77 }
78
79 static void refill_skbs(void)
80 {
81         struct sk_buff *skb;
82         unsigned long flags;
83
84         spin_lock_irqsave(&skb_list_lock, flags);
85         while (nr_skbs < MAX_SKBS) {
86                 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
87                 if (!skb)
88                         break;
89
90                 skb->next = skbs;
91                 skbs = skb;
92                 nr_skbs++;
93         }
94         spin_unlock_irqrestore(&skb_list_lock, flags);
95 }
96
97 static void zap_completion_queue(void)
98 {
99         unsigned long flags;
100         struct softnet_data *sd = &get_cpu_var(softnet_data);
101
102         if (sd->completion_queue) {
103                 struct sk_buff *clist;
104
105                 local_irq_save(flags);
106                 clist = sd->completion_queue;
107                 sd->completion_queue = NULL;
108                 local_irq_restore(flags);
109
110                 while (clist != NULL) {
111                         struct sk_buff *skb = clist;
112                         clist = clist->next;
113                         __kfree_skb(skb);
114                 }
115         }
116
117         put_cpu_var(softnet_data);
118 }
119
120 static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
121 {
122         int once = 1, count = 0;
123         unsigned long flags;
124         struct sk_buff *skb = NULL;
125
126         zap_completion_queue();
127 repeat:
128         if (nr_skbs < MAX_SKBS)
129                 refill_skbs();
130
131         skb = alloc_skb(len, GFP_ATOMIC);
132
133         if (!skb) {
134                 spin_lock_irqsave(&skb_list_lock, flags);
135                 skb = skbs;
136                 if (skb)
137                         skbs = skb->next;
138                 skb->next = NULL;
139                 nr_skbs--;
140                 spin_unlock_irqrestore(&skb_list_lock, flags);
141         }
142
143         if(!skb) {
144                 count++;
145                 if (once && (count == 1000000)) {
146                         printk("out of netpoll skbs!\n");
147                         once = 0;
148                 }
149                 netpoll_poll(np);
150                 goto repeat;
151         }
152
153         atomic_set(&skb->users, 1);
154         skb_reserve(skb, reserve);
155         return skb;
156 }
157
158 void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
159 {
160         int status;
161
162 repeat:
163         if(!np || !np->dev || !netif_running(np->dev)) {
164                 __kfree_skb(skb);
165                 return;
166         }
167
168         spin_lock(&np->dev->xmit_lock);
169         np->dev->xmit_lock_owner = smp_processor_id();
170
171         status = np->dev->hard_start_xmit(skb, np->dev);
172         np->dev->xmit_lock_owner = -1;
173         spin_unlock(&np->dev->xmit_lock);
174
175         /* transmit busy */
176         if(status) {
177                 netpoll_poll(np);
178                 goto repeat;
179         }
180 }
181
182 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
183 {
184         int total_len, eth_len, ip_len, udp_len;
185         struct sk_buff *skb;
186         struct udphdr *udph;
187         struct iphdr *iph;
188         struct ethhdr *eth;
189
190         udp_len = len + sizeof(*udph);
191         ip_len = eth_len = udp_len + sizeof(*iph);
192         total_len = eth_len + ETH_HLEN;
193
194         skb = find_skb(np, total_len, total_len - len);
195         if (!skb)
196                 return;
197
198         memcpy(skb->data, msg, len);
199         skb->len += len;
200
201         udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
202         udph->source = htons(np->local_port);
203         udph->dest = htons(np->remote_port);
204         udph->len = htons(udp_len);
205         udph->check = 0;
206
207         iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
208
209         iph->version  = 4;
210         iph->ihl      = 5;
211         iph->tos      = 0;
212         iph->tot_len  = htons(ip_len);
213         iph->id       = 0;
214         iph->frag_off = 0;
215         iph->ttl      = 64;
216         iph->protocol = IPPROTO_UDP;
217         iph->check    = 0;
218         iph->saddr    = htonl(np->local_ip);
219         iph->daddr    = htonl(np->remote_ip);
220         iph->check    = ip_fast_csum((unsigned char *)iph, iph->ihl);
221
222         eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
223
224         eth->h_proto = htons(ETH_P_IP);
225         memcpy(eth->h_source, np->local_mac, 6);
226         memcpy(eth->h_dest, np->remote_mac, 6);
227
228         netpoll_send_skb(np, skb);
229 }
230
231 static void arp_reply(struct sk_buff *skb)
232 {
233         struct arphdr *arp;
234         unsigned char *arp_ptr;
235         int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
236         u32 sip, tip;
237         struct sk_buff *send_skb;
238         unsigned long flags;
239         struct list_head *p;
240         struct netpoll *np = 0;
241
242         spin_lock_irqsave(&rx_list_lock, flags);
243         list_for_each(p, &rx_list) {
244                 np = list_entry(p, struct netpoll, rx_list);
245                 if ( np->dev == skb->dev )
246                         break;
247                 np = 0;
248         }
249         spin_unlock_irqrestore(&rx_list_lock, flags);
250
251         if (!np) return;
252
253         /* No arp on this interface */
254         if (skb->dev->flags & IFF_NOARP)
255                 return;
256
257         if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
258                                  (2 * skb->dev->addr_len) +
259                                  (2 * sizeof(u32)))))
260                 return;
261
262         skb->h.raw = skb->nh.raw = skb->data;
263         arp = skb->nh.arph;
264
265         if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
266              arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
267             arp->ar_pro != htons(ETH_P_IP) ||
268             arp->ar_op != htons(ARPOP_REQUEST))
269                 return;
270
271         arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
272         memcpy(&sip, arp_ptr, 4);
273         arp_ptr += 4 + skb->dev->addr_len;
274         memcpy(&tip, arp_ptr, 4);
275
276         /* Should we ignore arp? */
277         if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
278                 return;
279
280         size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
281         send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
282                             LL_RESERVED_SPACE(np->dev));
283
284         if (!send_skb)
285                 return;
286
287         send_skb->nh.raw = send_skb->data;
288         arp = (struct arphdr *) skb_put(send_skb, size);
289         send_skb->dev = skb->dev;
290         send_skb->protocol = htons(ETH_P_ARP);
291
292         /* Fill the device header for the ARP frame */
293
294         if (np->dev->hard_header &&
295             np->dev->hard_header(send_skb, skb->dev, ptype,
296                                        np->remote_mac, np->local_mac,
297                                        send_skb->len) < 0) {
298                 kfree_skb(send_skb);
299                 return;
300         }
301
302         /*
303          * Fill out the arp protocol part.
304          *
305          * we only support ethernet device type,
306          * which (according to RFC 1390) should always equal 1 (Ethernet).
307          */
308
309         arp->ar_hrd = htons(np->dev->type);
310         arp->ar_pro = htons(ETH_P_IP);
311         arp->ar_hln = np->dev->addr_len;
312         arp->ar_pln = 4;
313         arp->ar_op = htons(type);
314
315         arp_ptr=(unsigned char *)(arp + 1);
316         memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
317         arp_ptr += np->dev->addr_len;
318         memcpy(arp_ptr, &tip, 4);
319         arp_ptr += 4;
320         memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
321         arp_ptr += np->dev->addr_len;
322         memcpy(arp_ptr, &sip, 4);
323
324         netpoll_send_skb(np, send_skb);
325 }
326
327 int netpoll_rx(struct sk_buff *skb)
328 {
329         int proto, len, ulen;
330         struct iphdr *iph;
331         struct udphdr *uh;
332         struct netpoll *np;
333         struct list_head *p;
334         unsigned long flags;
335
336         if (skb->dev->type != ARPHRD_ETHER)
337                 goto out;
338
339         /* check if netpoll clients need ARP */
340         if (skb->protocol == __constant_htons(ETH_P_ARP) && trapped) {
341                 arp_reply(skb);
342                 return 1;
343         }
344
345         proto = ntohs(skb->mac.ethernet->h_proto);
346         if (proto != ETH_P_IP)
347                 goto out;
348         if (skb->pkt_type == PACKET_OTHERHOST)
349                 goto out;
350         if (skb_shared(skb))
351                 goto out;
352
353         iph = (struct iphdr *)skb->data;
354         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
355                 goto out;
356         if (iph->ihl < 5 || iph->version != 4)
357                 goto out;
358         if (!pskb_may_pull(skb, iph->ihl*4))
359                 goto out;
360         if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
361                 goto out;
362
363         len = ntohs(iph->tot_len);
364         if (skb->len < len || len < iph->ihl*4)
365                 goto out;
366
367         if (iph->protocol != IPPROTO_UDP)
368                 goto out;
369
370         len -= iph->ihl*4;
371         uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
372         ulen = ntohs(uh->len);
373
374         if (ulen != len)
375                 goto out;
376         if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0)
377                 goto out;
378
379         spin_lock_irqsave(&rx_list_lock, flags);
380         list_for_each(p, &rx_list) {
381                 np = list_entry(p, struct netpoll, rx_list);
382                 if (np->dev && np->dev != skb->dev)
383                         continue;
384                 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
385                         continue;
386                 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
387                         continue;
388                 if (np->local_port && np->local_port != ntohs(uh->dest))
389                         continue;
390
391                 spin_unlock_irqrestore(&rx_list_lock, flags);
392
393                 if (np->rx_hook)
394                         np->rx_hook(np, ntohs(uh->source),
395                                     (char *)(uh+1),
396                                     ulen - sizeof(struct udphdr));
397
398                 return 1;
399         }
400         spin_unlock_irqrestore(&rx_list_lock, flags);
401
402 out:
403         return trapped;
404 }
405
406 int netpoll_parse_options(struct netpoll *np, char *opt)
407 {
408         char *cur=opt, *delim;
409
410         if(*cur != '@') {
411                 if ((delim = strchr(cur, '@')) == NULL)
412                         goto parse_failed;
413                 *delim=0;
414                 np->local_port=simple_strtol(cur, 0, 10);
415                 cur=delim;
416         }
417         cur++;
418         printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
419
420         if(*cur != '/') {
421                 if ((delim = strchr(cur, '/')) == NULL)
422                         goto parse_failed;
423                 *delim=0;
424                 np->local_ip=ntohl(in_aton(cur));
425                 cur=delim;
426
427                 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
428                        np->name, HIPQUAD(np->local_ip));
429         }
430         cur++;
431
432         if ( *cur != ',') {
433                 /* parse out dev name */
434                 if ((delim = strchr(cur, ',')) == NULL)
435                         goto parse_failed;
436                 *delim=0;
437                 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
438                 cur=delim;
439         }
440         cur++;
441
442         printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
443
444         if ( *cur != '@' ) {
445                 /* dst port */
446                 if ((delim = strchr(cur, '@')) == NULL)
447                         goto parse_failed;
448                 *delim=0;
449                 np->remote_port=simple_strtol(cur, 0, 10);
450                 cur=delim;
451         }
452         cur++;
453         printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
454
455         /* dst ip */
456         if ((delim = strchr(cur, '/')) == NULL)
457                 goto parse_failed;
458         *delim=0;
459         np->remote_ip=ntohl(in_aton(cur));
460         cur=delim+1;
461
462         printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
463                        np->name, HIPQUAD(np->remote_ip));
464
465         if( *cur != 0 )
466         {
467                 /* MAC address */
468                 if ((delim = strchr(cur, ':')) == NULL)
469                         goto parse_failed;
470                 *delim=0;
471                 np->remote_mac[0]=simple_strtol(cur, 0, 16);
472                 cur=delim+1;
473                 if ((delim = strchr(cur, ':')) == NULL)
474                         goto parse_failed;
475                 *delim=0;
476                 np->remote_mac[1]=simple_strtol(cur, 0, 16);
477                 cur=delim+1;
478                 if ((delim = strchr(cur, ':')) == NULL)
479                         goto parse_failed;
480                 *delim=0;
481                 np->remote_mac[2]=simple_strtol(cur, 0, 16);
482                 cur=delim+1;
483                 if ((delim = strchr(cur, ':')) == NULL)
484                         goto parse_failed;
485                 *delim=0;
486                 np->remote_mac[3]=simple_strtol(cur, 0, 16);
487                 cur=delim+1;
488                 if ((delim = strchr(cur, ':')) == NULL)
489                         goto parse_failed;
490                 *delim=0;
491                 np->remote_mac[4]=simple_strtol(cur, 0, 16);
492                 cur=delim+1;
493                 np->remote_mac[5]=simple_strtol(cur, 0, 16);
494         }
495
496         printk(KERN_INFO "%s: remote ethernet address "
497                "%02x:%02x:%02x:%02x:%02x:%02x\n",
498                np->name,
499                np->remote_mac[0],
500                np->remote_mac[1],
501                np->remote_mac[2],
502                np->remote_mac[3],
503                np->remote_mac[4],
504                np->remote_mac[5]);
505
506         return 0;
507
508  parse_failed:
509         printk(KERN_INFO "%s: couldn't parse config at %s!\n",
510                np->name, cur);
511         return -1;
512 }
513
514 int netpoll_setup(struct netpoll *np)
515 {
516         struct net_device *ndev = NULL;
517         struct in_device *in_dev;
518
519         if (np->dev_name)
520                 ndev = dev_get_by_name(np->dev_name);
521         if (!ndev) {
522                 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
523                        np->name, np->dev_name);
524                 return -1;
525         }
526         if (!ndev->poll_controller) {
527                 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
528                        np->name, np->dev_name);
529                 goto release;
530         }
531
532         if (!(ndev->flags & IFF_UP)) {
533                 unsigned short oflags;
534                 unsigned long atmost, atleast;
535
536                 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
537                        np->name, np->dev_name);
538
539                 oflags = ndev->flags;
540
541                 rtnl_shlock();
542                 if (dev_change_flags(ndev, oflags | IFF_UP) < 0) {
543                         printk(KERN_ERR "%s: failed to open %s\n",
544                                np->name, np->dev_name);
545                         rtnl_shunlock();
546                         goto release;
547                 }
548                 rtnl_shunlock();
549
550                 atleast = jiffies + HZ/10;
551                 atmost = jiffies + 10*HZ;
552                 while (!netif_carrier_ok(ndev)) {
553                         if (time_after(jiffies, atmost)) {
554                                 printk(KERN_NOTICE
555                                        "%s: timeout waiting for carrier\n",
556                                        np->name);
557                                 break;
558                         }
559                         cond_resched();
560                 }
561
562                 if (time_before(jiffies, atleast)) {
563                         printk(KERN_NOTICE "%s: carrier detect appears flaky,"
564                                " waiting 10 seconds\n",
565                                np->name);
566                         while (time_before(jiffies, atmost))
567                                 cond_resched();
568                 }
569         }
570
571         if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr)
572                 memcpy(np->local_mac, ndev->dev_addr, 6);
573
574         if (!np->local_ip) {
575                 in_dev = in_dev_get(ndev);
576
577                 if (!in_dev) {
578                         printk(KERN_ERR "%s: no IP address for %s, aborting\n",
579                                np->name, np->dev_name);
580                         goto release;
581                 }
582
583                 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
584                 in_dev_put(in_dev);
585                 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
586                        np->name, HIPQUAD(np->local_ip));
587         }
588
589         np->dev = ndev;
590
591         if(np->rx_hook) {
592                 unsigned long flags;
593
594 #ifdef CONFIG_NETPOLL_RX
595                 np->dev->netpoll_rx = 1;
596 #endif
597
598                 spin_lock_irqsave(&rx_list_lock, flags);
599                 list_add(&np->rx_list, &rx_list);
600                 spin_unlock_irqrestore(&rx_list_lock, flags);
601         }
602
603         return 0;
604  release:
605         dev_put(ndev);
606         return -1;
607 }
608
609 void netpoll_cleanup(struct netpoll *np)
610 {
611         if(np->rx_hook) {
612                 unsigned long flags;
613
614                 spin_lock_irqsave(&rx_list_lock, flags);
615                 list_del(&np->rx_list);
616 #ifdef CONFIG_NETPOLL_RX
617                 np->dev->netpoll_rx = 0;
618 #endif
619                 spin_unlock_irqrestore(&rx_list_lock, flags);
620         }
621
622         dev_put(np->dev);
623         np->dev = 0;
624 }
625
626 int netpoll_trap(void)
627 {
628         return trapped;
629 }
630
631 void netpoll_set_trap(int trap)
632 {
633         trapped = trap;
634 }
635
636 EXPORT_SYMBOL(netpoll_set_trap);
637 EXPORT_SYMBOL(netpoll_trap);
638 EXPORT_SYMBOL(netpoll_parse_options);
639 EXPORT_SYMBOL(netpoll_setup);
640 EXPORT_SYMBOL(netpoll_cleanup);
641 EXPORT_SYMBOL(netpoll_send_skb);
642 EXPORT_SYMBOL(netpoll_send_udp);
643 EXPORT_SYMBOL(netpoll_poll);