2 * Common framework for low-level network console, dump, and debugger code
4 * Sep 8 2003 Matt Mackall <mpm@selenic.com>
6 * based on the netconsole code from:
8 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2002 Red Hat, Inc.
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 <linux/delay.h>
22 #include <linux/rcupdate.h>
23 #include <linux/workqueue.h>
26 #include <asm/unaligned.h>
29 * We maintain a small pool of fully-sized skbs, to make sure the
30 * message gets out even in extreme OOM situations.
33 #define MAX_UDP_CHUNK 1460
35 #define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
37 static DEFINE_SPINLOCK(skb_list_lock);
39 static struct sk_buff *skbs;
41 static DEFINE_SPINLOCK(queue_lock);
42 static int queue_depth;
43 static struct sk_buff *queue_head, *queue_tail;
45 static atomic_t trapped;
47 #define NETPOLL_RX_ENABLED 1
48 #define NETPOLL_RX_DROP 2
50 #define MAX_SKB_SIZE \
51 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
52 sizeof(struct iphdr) + sizeof(struct ethhdr))
54 static void zap_completion_queue(void);
56 static void queue_process(void *p)
62 spin_lock_irqsave(&queue_lock, flags);
65 queue_head = skb->next;
66 if (skb == queue_tail)
71 spin_unlock_irqrestore(&queue_lock, flags);
77 static DECLARE_WORK(send_queue, queue_process, NULL);
79 void netpoll_queue(struct sk_buff *skb)
83 if (queue_depth == MAX_QUEUE_DEPTH) {
88 spin_lock_irqsave(&queue_lock, flags);
92 queue_tail->next = skb;
95 spin_unlock_irqrestore(&queue_lock, flags);
97 schedule_work(&send_queue);
100 static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
101 unsigned short ulen, u32 saddr, u32 daddr)
106 if (skb->ip_summed == CHECKSUM_HW)
107 return csum_tcpudp_magic(
108 saddr, daddr, ulen, IPPROTO_UDP, skb->csum);
110 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
112 return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
116 * Check whether delayed processing was scheduled for our NIC. If so,
117 * we attempt to grab the poll lock and use ->poll() to pump the card.
118 * If this fails, either we've recursed in ->poll() or it's already
119 * running on another CPU.
121 * Note: we don't mask interrupts with this lock because we're using
122 * trylock here and interrupts are already disabled in the softirq
123 * case. Further, we test the poll_owner to avoid recursion on UP
124 * systems where the lock doesn't exist.
126 * In cases where there is bi-directional communications, reading only
127 * one message at a time can lead to packets being dropped by the
128 * network adapter, forcing superfluous retries and possibly timeouts.
129 * Thus, we set our budget to greater than 1.
131 static void poll_napi(struct netpoll *np)
133 struct netpoll_info *npinfo = np->dev->npinfo;
136 if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
137 npinfo->poll_owner != smp_processor_id() &&
138 spin_trylock(&npinfo->poll_lock)) {
139 npinfo->rx_flags |= NETPOLL_RX_DROP;
140 atomic_inc(&trapped);
142 np->dev->poll(np->dev, &budget);
144 atomic_dec(&trapped);
145 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
146 spin_unlock(&npinfo->poll_lock);
150 void netpoll_poll(struct netpoll *np)
152 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
155 /* Process pending work on NIC */
156 np->dev->poll_controller(np->dev);
160 zap_completion_queue();
163 static void refill_skbs(void)
168 spin_lock_irqsave(&skb_list_lock, flags);
169 while (nr_skbs < MAX_SKBS) {
170 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
178 spin_unlock_irqrestore(&skb_list_lock, flags);
181 static void zap_completion_queue(void)
184 struct softnet_data *sd = &get_cpu_var(softnet_data);
186 if (sd->completion_queue) {
187 struct sk_buff *clist;
189 local_irq_save(flags);
190 clist = sd->completion_queue;
191 sd->completion_queue = NULL;
192 local_irq_restore(flags);
194 while (clist != NULL) {
195 struct sk_buff *skb = clist;
198 dev_kfree_skb_any(skb); /* put this one back */
204 put_cpu_var(softnet_data);
207 static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
209 int once = 1, count = 0;
211 struct sk_buff *skb = NULL;
213 zap_completion_queue();
215 if (nr_skbs < MAX_SKBS)
218 skb = alloc_skb(len, GFP_ATOMIC);
221 spin_lock_irqsave(&skb_list_lock, flags);
228 spin_unlock_irqrestore(&skb_list_lock, flags);
233 if (once && (count == 1000000)) {
234 printk("out of netpoll skbs!\n");
241 atomic_set(&skb->users, 1);
242 skb_reserve(skb, reserve);
246 static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
249 struct netpoll_info *npinfo;
252 if(!np || !np->dev || !netif_running(np->dev)) {
257 /* avoid recursion */
258 npinfo = np->dev->npinfo;
259 if (npinfo->poll_owner == smp_processor_id() ||
260 np->dev->xmit_lock_owner == smp_processor_id()) {
268 spin_lock(&np->dev->xmit_lock);
269 np->dev->xmit_lock_owner = smp_processor_id();
272 * network drivers do not expect to be called if the queue is
275 if (netif_queue_stopped(np->dev)) {
276 np->dev->xmit_lock_owner = -1;
277 spin_unlock(&np->dev->xmit_lock);
283 status = np->dev->hard_start_xmit(skb, np->dev);
284 np->dev->xmit_lock_owner = -1;
285 spin_unlock(&np->dev->xmit_lock);
294 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
296 int total_len, eth_len, ip_len, udp_len;
302 udp_len = len + sizeof(*udph);
303 ip_len = eth_len = udp_len + sizeof(*iph);
304 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
306 skb = find_skb(np, total_len, total_len - len);
310 memcpy(skb->data, msg, len);
313 udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
314 udph->source = htons(np->local_port);
315 udph->dest = htons(np->remote_port);
316 udph->len = htons(udp_len);
319 iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
321 /* iph->version = 4; iph->ihl = 5; */
322 put_unaligned(0x45, (unsigned char *)iph);
324 put_unaligned(htons(ip_len), &(iph->tot_len));
328 iph->protocol = IPPROTO_UDP;
330 put_unaligned(htonl(np->local_ip), &(iph->saddr));
331 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
332 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
334 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
336 eth->h_proto = htons(ETH_P_IP);
337 memcpy(eth->h_source, np->local_mac, 6);
338 memcpy(eth->h_dest, np->remote_mac, 6);
342 netpoll_send_skb(np, skb);
345 static void arp_reply(struct sk_buff *skb)
347 struct netpoll_info *npinfo = skb->dev->npinfo;
349 unsigned char *arp_ptr;
350 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
353 struct sk_buff *send_skb;
354 struct netpoll *np = NULL;
356 spin_lock_irqsave(&npinfo->rx_lock, flags);
357 if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
359 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
364 /* No arp on this interface */
365 if (skb->dev->flags & IFF_NOARP)
368 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
369 (2 * skb->dev->addr_len) +
373 skb->h.raw = skb->nh.raw = skb->data;
376 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
377 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
378 arp->ar_pro != htons(ETH_P_IP) ||
379 arp->ar_op != htons(ARPOP_REQUEST))
382 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
383 memcpy(&sip, arp_ptr, 4);
384 arp_ptr += 4 + skb->dev->addr_len;
385 memcpy(&tip, arp_ptr, 4);
387 /* Should we ignore arp? */
388 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
391 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
392 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
393 LL_RESERVED_SPACE(np->dev));
398 send_skb->nh.raw = send_skb->data;
399 arp = (struct arphdr *) skb_put(send_skb, size);
400 send_skb->dev = skb->dev;
401 send_skb->protocol = htons(ETH_P_ARP);
403 /* Fill the device header for the ARP frame */
405 if (np->dev->hard_header &&
406 np->dev->hard_header(send_skb, skb->dev, ptype,
407 np->remote_mac, np->local_mac,
408 send_skb->len) < 0) {
414 * Fill out the arp protocol part.
416 * we only support ethernet device type,
417 * which (according to RFC 1390) should always equal 1 (Ethernet).
420 arp->ar_hrd = htons(np->dev->type);
421 arp->ar_pro = htons(ETH_P_IP);
422 arp->ar_hln = np->dev->addr_len;
424 arp->ar_op = htons(type);
426 arp_ptr=(unsigned char *)(arp + 1);
427 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
428 arp_ptr += np->dev->addr_len;
429 memcpy(arp_ptr, &tip, 4);
431 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
432 arp_ptr += np->dev->addr_len;
433 memcpy(arp_ptr, &sip, 4);
435 netpoll_send_skb(np, send_skb);
438 int __netpoll_rx(struct sk_buff *skb)
440 int proto, len, ulen;
443 struct netpoll *np = skb->dev->npinfo->rx_np;
447 if (skb->dev->type != ARPHRD_ETHER)
450 /* check if netpoll clients need ARP */
451 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
452 atomic_read(&trapped)) {
457 proto = ntohs(eth_hdr(skb)->h_proto);
458 if (proto != ETH_P_IP)
460 if (skb->pkt_type == PACKET_OTHERHOST)
465 iph = (struct iphdr *)skb->data;
466 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
468 if (iph->ihl < 5 || iph->version != 4)
470 if (!pskb_may_pull(skb, iph->ihl*4))
472 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
475 len = ntohs(iph->tot_len);
476 if (skb->len < len || len < iph->ihl*4)
479 if (iph->protocol != IPPROTO_UDP)
483 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
484 ulen = ntohs(uh->len);
488 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0)
490 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
492 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
494 if (np->local_port && np->local_port != ntohs(uh->dest))
497 np->rx_hook(np, ntohs(uh->source),
499 ulen - sizeof(struct udphdr));
505 if (atomic_read(&trapped)) {
513 int netpoll_parse_options(struct netpoll *np, char *opt)
515 char *cur=opt, *delim;
518 if ((delim = strchr(cur, '@')) == NULL)
521 np->local_port=simple_strtol(cur, NULL, 10);
525 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
528 if ((delim = strchr(cur, '/')) == NULL)
531 np->local_ip=ntohl(in_aton(cur));
534 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
535 np->name, HIPQUAD(np->local_ip));
540 /* parse out dev name */
541 if ((delim = strchr(cur, ',')) == NULL)
544 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
549 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
553 if ((delim = strchr(cur, '@')) == NULL)
556 np->remote_port=simple_strtol(cur, NULL, 10);
560 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
563 if ((delim = strchr(cur, '/')) == NULL)
566 np->remote_ip=ntohl(in_aton(cur));
569 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
570 np->name, HIPQUAD(np->remote_ip));
575 if ((delim = strchr(cur, ':')) == NULL)
578 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
580 if ((delim = strchr(cur, ':')) == NULL)
583 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
585 if ((delim = strchr(cur, ':')) == NULL)
588 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
590 if ((delim = strchr(cur, ':')) == NULL)
593 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
595 if ((delim = strchr(cur, ':')) == NULL)
598 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
600 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
603 printk(KERN_INFO "%s: remote ethernet address "
604 "%02x:%02x:%02x:%02x:%02x:%02x\n",
616 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
621 int netpoll_setup(struct netpoll *np)
623 struct net_device *ndev = NULL;
624 struct in_device *in_dev;
625 struct netpoll_info *npinfo;
629 ndev = dev_get_by_name(np->dev_name);
631 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
632 np->name, np->dev_name);
638 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
642 npinfo->rx_np = NULL;
643 npinfo->poll_lock = SPIN_LOCK_UNLOCKED;
644 npinfo->poll_owner = -1;
645 npinfo->rx_lock = SPIN_LOCK_UNLOCKED;
647 npinfo = ndev->npinfo;
649 if (!ndev->poll_controller) {
650 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
651 np->name, np->dev_name);
655 if (!netif_running(ndev)) {
656 unsigned long atmost, atleast;
658 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
659 np->name, np->dev_name);
662 if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) {
663 printk(KERN_ERR "%s: failed to open %s\n",
664 np->name, np->dev_name);
670 atleast = jiffies + HZ/10;
671 atmost = jiffies + 4*HZ;
672 while (!netif_carrier_ok(ndev)) {
673 if (time_after(jiffies, atmost)) {
675 "%s: timeout waiting for carrier\n",
682 /* If carrier appears to come up instantly, we don't
683 * trust it and pause so that we don't pump all our
684 * queued console messages into the bitbucket.
687 if (time_before(jiffies, atleast)) {
688 printk(KERN_NOTICE "%s: carrier detect appears"
689 " untrustworthy, waiting 4 seconds\n",
695 if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr)
696 memcpy(np->local_mac, ndev->dev_addr, 6);
700 in_dev = __in_dev_get(ndev);
702 if (!in_dev || !in_dev->ifa_list) {
704 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
705 np->name, np->dev_name);
709 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
711 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
712 np->name, HIPQUAD(np->local_ip));
716 spin_lock_irqsave(&npinfo->rx_lock, flags);
717 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
719 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
721 /* last thing to do is link it to the net device structure */
722 ndev->npinfo = npinfo;
734 void netpoll_cleanup(struct netpoll *np)
736 struct netpoll_info *npinfo;
740 npinfo = np->dev->npinfo;
741 if (npinfo && npinfo->rx_np == np) {
742 spin_lock_irqsave(&npinfo->rx_lock, flags);
743 npinfo->rx_np = NULL;
744 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
745 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
753 int netpoll_trap(void)
755 return atomic_read(&trapped);
758 void netpoll_set_trap(int trap)
761 atomic_inc(&trapped);
763 atomic_dec(&trapped);
766 EXPORT_SYMBOL(netpoll_set_trap);
767 EXPORT_SYMBOL(netpoll_trap);
768 EXPORT_SYMBOL(netpoll_parse_options);
769 EXPORT_SYMBOL(netpoll_setup);
770 EXPORT_SYMBOL(netpoll_cleanup);
771 EXPORT_SYMBOL(netpoll_send_udp);
772 EXPORT_SYMBOL(netpoll_poll);
773 EXPORT_SYMBOL(netpoll_queue);