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/rcupdate.h>
22 #include <linux/nmi.h>
25 #include <asm/unaligned.h>
26 #include <asm/byteorder.h>
29 * We maintain a small pool of fully-sized skbs, to make sure the
30 * message gets out even in extreme OOM situations.
34 #define MAX_UDP_CHUNK 1460
36 static spinlock_t skb_list_lock = SPIN_LOCK_UNLOCKED;
38 static struct sk_buff *skbs;
40 static spinlock_t rx_list_lock = SPIN_LOCK_UNLOCKED;
41 static LIST_HEAD(rx_list);
43 static atomic_t trapped;
44 spinlock_t netpoll_poll_lock = SPIN_LOCK_UNLOCKED;
46 #define NETPOLL_RX_ENABLED 1
47 #define NETPOLL_RX_DROP 2
49 #define MAX_SKB_SIZE \
50 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
51 sizeof(struct iphdr) + sizeof(struct ethhdr))
53 static void zap_completion_queue(void);
55 static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
56 unsigned short ulen, u32 saddr, u32 daddr)
61 if (skb->ip_summed == CHECKSUM_HW)
62 return csum_tcpudp_magic(
63 saddr, daddr, ulen, IPPROTO_UDP, skb->csum);
65 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
67 return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
71 * Check whether delayed processing was scheduled for our current CPU,
72 * and then manually invoke NAPI polling to pump data off the card.
74 * In cases where there is bi-directional communications, reading only
75 * one message at a time can lead to packets being dropped by the
76 * network adapter, forcing superfluous retries and possibly timeouts.
77 * Thus, we set our budget to greater than 1.
79 static void poll_napi(struct netpoll *np)
81 int budget = netdump_mode ? 64 : 16;
83 struct softnet_data *queue;
85 spin_lock_irqsave(&netpoll_poll_lock, flags);
86 queue = &__get_cpu_var(softnet_data);
87 if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
88 !list_empty(&queue->poll_list)) {
89 np->dev->netpoll_rx |= NETPOLL_RX_DROP;
92 np->dev->poll(np->dev, &budget);
95 np->dev->netpoll_rx &= ~NETPOLL_RX_DROP;
97 spin_unlock_irqrestore(&netpoll_poll_lock, flags);
100 void netpoll_poll(struct netpoll *np)
102 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
105 /* Process pending work on NIC */
106 np->dev->poll_controller(np->dev);
110 zap_completion_queue();
113 static void refill_skbs(void)
118 spin_lock_irqsave(&skb_list_lock, flags);
119 while (nr_skbs < MAX_SKBS) {
120 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
128 spin_unlock_irqrestore(&skb_list_lock, flags);
131 static void zap_completion_queue(void)
134 struct softnet_data *sd = &get_cpu_var(softnet_data);
136 if (sd->completion_queue) {
137 struct sk_buff *clist;
139 local_irq_save(flags);
140 clist = sd->completion_queue;
141 sd->completion_queue = NULL;
142 local_irq_restore(flags);
144 while (clist != NULL) {
145 struct sk_buff *skb = clist;
151 put_cpu_var(softnet_data);
152 touch_nmi_watchdog();
155 static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
157 int once = 1, count = 0;
159 struct sk_buff *skb = NULL;
161 zap_completion_queue();
163 if (nr_skbs < MAX_SKBS)
166 skb = alloc_skb(len, GFP_ATOMIC);
169 spin_lock_irqsave(&skb_list_lock, flags);
175 spin_unlock_irqrestore(&skb_list_lock, flags);
180 if (once && (count == 1000000)) {
181 printk("out of netpoll skbs!\n");
188 atomic_set(&skb->users, 1);
189 skb_reserve(skb, reserve);
193 void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
198 if(!np || !np->dev || !netif_running(np->dev)) {
203 spin_lock(&np->dev->xmit_lock);
204 np->dev->xmit_lock_owner = smp_processor_id();
207 * network drivers do not expect to be called if the queue is
210 if (netif_queue_stopped(np->dev)) {
211 np->dev->xmit_lock_owner = -1;
212 spin_unlock(&np->dev->xmit_lock);
218 status = np->dev->hard_start_xmit(skb, np->dev);
219 np->dev->xmit_lock_owner = -1;
220 spin_unlock(&np->dev->xmit_lock);
229 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
231 int total_len, eth_len, ip_len, udp_len;
237 udp_len = len + sizeof(*udph);
238 ip_len = eth_len = udp_len + sizeof(*iph);
239 total_len = eth_len + ETH_HLEN;
241 skb = find_skb(np, total_len, total_len - len);
245 memcpy(skb->data, msg, len);
248 udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
249 udph->source = htons(np->local_port);
250 udph->dest = htons(np->remote_port);
251 udph->len = htons(udp_len);
254 iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
256 /* iph->version = 4; iph->ihl = 5; */
257 put_unaligned(0x45, (unsigned char *)iph);
259 put_unaligned(htons(ip_len), &(iph->tot_len));
263 iph->protocol = IPPROTO_UDP;
265 put_unaligned(htonl(np->local_ip), &(iph->saddr));
266 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
267 iph->check = ip_fast_csum((unsigned char *)iph, 5);
269 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
271 eth->h_proto = htons(ETH_P_IP);
272 memcpy(eth->h_source, np->local_mac, 6);
273 memcpy(eth->h_dest, np->remote_mac, 6);
275 netpoll_send_skb(np, skb);
278 static void arp_reply(struct sk_buff *skb)
281 unsigned char *arp_ptr;
282 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
284 struct sk_buff *send_skb;
287 struct netpoll *np = NULL;
289 spin_lock_irqsave(&rx_list_lock, flags);
290 list_for_each(p, &rx_list) {
291 np = list_entry(p, struct netpoll, rx_list);
292 if ( np->dev == skb->dev )
296 spin_unlock_irqrestore(&rx_list_lock, flags);
300 /* No arp on this interface */
301 if (skb->dev->flags & IFF_NOARP)
304 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
305 (2 * skb->dev->addr_len) +
309 skb->h.raw = skb->nh.raw = skb->data;
312 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
313 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
314 arp->ar_pro != htons(ETH_P_IP) ||
315 arp->ar_op != htons(ARPOP_REQUEST))
318 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
319 memcpy(&sip, arp_ptr, 4);
320 arp_ptr += 4 + skb->dev->addr_len;
321 memcpy(&tip, arp_ptr, 4);
323 /* Should we ignore arp? */
324 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
327 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
328 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
329 LL_RESERVED_SPACE(np->dev));
334 send_skb->nh.raw = send_skb->data;
335 arp = (struct arphdr *) skb_put(send_skb, size);
336 send_skb->dev = skb->dev;
337 send_skb->protocol = htons(ETH_P_ARP);
339 /* Fill the device header for the ARP frame */
341 if (np->dev->hard_header &&
342 np->dev->hard_header(send_skb, skb->dev, ptype,
343 np->remote_mac, np->local_mac,
344 send_skb->len) < 0) {
350 * Fill out the arp protocol part.
352 * we only support ethernet device type,
353 * which (according to RFC 1390) should always equal 1 (Ethernet).
356 arp->ar_hrd = htons(np->dev->type);
357 arp->ar_pro = htons(ETH_P_IP);
358 arp->ar_hln = np->dev->addr_len;
360 arp->ar_op = htons(type);
362 arp_ptr=(unsigned char *)(arp + 1);
363 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
364 arp_ptr += np->dev->addr_len;
365 memcpy(arp_ptr, &tip, 4);
367 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
368 arp_ptr += np->dev->addr_len;
369 memcpy(arp_ptr, &sip, 4);
371 netpoll_send_skb(np, send_skb);
374 int netpoll_rx(struct sk_buff *skb)
376 int proto, len, ulen;
383 if (!(skb->dev->netpoll_rx & NETPOLL_RX_ENABLED))
386 if (skb->dev->type != ARPHRD_ETHER)
389 /* check if netpoll clients need ARP */
390 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
391 atomic_read(&trapped)) {
396 proto = ntohs(eth_hdr(skb)->h_proto);
397 if (proto != ETH_P_IP)
399 if (skb->pkt_type == PACKET_OTHERHOST)
404 iph = (struct iphdr *)skb->data;
405 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
407 if (iph->ihl < 5 || iph->version != 4)
409 if (!pskb_may_pull(skb, iph->ihl*4))
411 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
414 len = ntohs(iph->tot_len);
415 if (skb->len < len || len < iph->ihl*4)
418 if (iph->protocol != IPPROTO_UDP)
422 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
423 ulen = ntohs(uh->len);
427 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0)
430 spin_lock_irqsave(&rx_list_lock, flags);
431 list_for_each(p, &rx_list) {
432 np = list_entry(p, struct netpoll, rx_list);
433 if (np->dev && np->dev != skb->dev)
435 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
437 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
439 if (np->local_port && np->local_port != ntohs(uh->dest))
442 spin_unlock_irqrestore(&rx_list_lock, flags);
445 np->rx_hook(np, ntohs(uh->source),
447 ulen - sizeof(struct udphdr));
451 spin_unlock_irqrestore(&rx_list_lock, flags);
454 return atomic_read(&trapped);
457 int netpoll_parse_options(struct netpoll *np, char *opt)
459 char *cur=opt, *delim;
462 if ((delim = strchr(cur, '@')) == NULL)
465 np->local_port=simple_strtol(cur, NULL, 10);
469 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
472 if ((delim = strchr(cur, '/')) == NULL)
475 np->local_ip=ntohl(in_aton(cur));
478 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
479 np->name, HIPQUAD(np->local_ip));
484 /* parse out dev name */
485 if ((delim = strchr(cur, ',')) == NULL)
488 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
493 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
497 if ((delim = strchr(cur, '@')) == NULL)
500 np->remote_port=simple_strtol(cur, NULL, 10);
504 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
507 if ((delim = strchr(cur, '/')) == NULL)
510 np->remote_ip=ntohl(in_aton(cur));
513 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
514 np->name, HIPQUAD(np->remote_ip));
519 if ((delim = strchr(cur, ':')) == NULL)
522 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
524 if ((delim = strchr(cur, ':')) == NULL)
527 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
529 if ((delim = strchr(cur, ':')) == NULL)
532 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
534 if ((delim = strchr(cur, ':')) == NULL)
537 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
539 if ((delim = strchr(cur, ':')) == NULL)
542 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
544 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
547 printk(KERN_INFO "%s: remote ethernet address "
548 "%02x:%02x:%02x:%02x:%02x:%02x\n",
560 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
565 int netpoll_setup(struct netpoll *np)
567 struct net_device *ndev = NULL;
568 struct in_device *in_dev;
571 ndev = dev_get_by_name(np->dev_name);
573 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
574 np->name, np->dev_name);
577 if (!ndev->poll_controller) {
578 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
579 np->name, np->dev_name);
583 if (!netif_running(ndev)) {
584 unsigned short oflags;
585 unsigned long atmost, atleast;
587 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
588 np->name, np->dev_name);
590 oflags = ndev->flags;
593 if (dev_change_flags(ndev, oflags | IFF_UP) < 0) {
594 printk(KERN_ERR "%s: failed to open %s\n",
595 np->name, np->dev_name);
601 atleast = jiffies + HZ/10;
602 atmost = jiffies + 10*HZ;
603 while (!netif_carrier_ok(ndev)) {
604 if (time_after(jiffies, atmost)) {
606 "%s: timeout waiting for carrier\n",
613 if (time_before(jiffies, atleast)) {
614 printk(KERN_NOTICE "%s: carrier detect appears flaky,"
615 " waiting 10 seconds\n",
617 while (time_before(jiffies, atmost))
622 if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr)
623 memcpy(np->local_mac, ndev->dev_addr, 6);
627 in_dev = __in_dev_get(ndev);
629 if (!in_dev || !in_dev->ifa_list) {
631 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
632 np->name, np->dev_name);
636 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
638 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
639 np->name, HIPQUAD(np->local_ip));
647 np->dev->netpoll_rx = NETPOLL_RX_ENABLED;
649 spin_lock_irqsave(&rx_list_lock, flags);
650 list_add(&np->rx_list, &rx_list);
651 spin_unlock_irqrestore(&rx_list_lock, flags);
655 netdump_func = np->dump_func;
663 void netpoll_cleanup(struct netpoll *np)
668 spin_lock_irqsave(&rx_list_lock, flags);
669 list_del(&np->rx_list);
670 spin_unlock_irqrestore(&rx_list_lock, flags);
674 np->dev->netpoll_rx = 0;
679 int netpoll_trap(void)
681 return atomic_read(&trapped);
684 void netpoll_set_trap(int trap)
687 atomic_inc(&trapped);
689 atomic_dec(&trapped);
692 void netpoll_reset_locks(struct netpoll *np)
694 spin_lock_init(&rx_list_lock);
695 spin_lock_init(&skb_list_lock);
696 spin_lock_init(&np->dev->xmit_lock);
699 EXPORT_SYMBOL(netpoll_set_trap);
700 EXPORT_SYMBOL(netpoll_trap);
701 EXPORT_SYMBOL(netpoll_parse_options);
702 EXPORT_SYMBOL(netpoll_setup);
703 EXPORT_SYMBOL(netpoll_cleanup);
704 EXPORT_SYMBOL(netpoll_send_skb);
705 EXPORT_SYMBOL(netpoll_send_udp);
706 EXPORT_SYMBOL(netpoll_poll);
707 EXPORT_SYMBOL_GPL(netpoll_reset_locks);