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>
24 #include <asm/unaligned.h>
27 * We maintain a small pool of fully-sized skbs, to make sure the
28 * message gets out even in extreme OOM situations.
32 #define MAX_UDP_CHUNK 1460
34 static spinlock_t skb_list_lock = SPIN_LOCK_UNLOCKED;
36 static struct sk_buff *skbs;
38 static spinlock_t rx_list_lock = SPIN_LOCK_UNLOCKED;
39 static LIST_HEAD(rx_list);
41 static atomic_t trapped;
42 spinlock_t netpoll_poll_lock = SPIN_LOCK_UNLOCKED;
44 #define NETPOLL_RX_ENABLED 1
45 #define NETPOLL_RX_DROP 2
47 #define MAX_SKB_SIZE \
48 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
49 sizeof(struct iphdr) + sizeof(struct ethhdr))
51 static void zap_completion_queue(void);
53 static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
54 unsigned short ulen, u32 saddr, u32 daddr)
59 if (skb->ip_summed == CHECKSUM_HW)
60 return csum_tcpudp_magic(
61 saddr, daddr, ulen, IPPROTO_UDP, skb->csum);
63 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
65 return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
68 void netpoll_poll(struct netpoll *np)
71 * In cases where there is bi-directional communications, reading
72 * only one message at a time can lead to packets being dropped by
73 * the network adapter, forcing superfluous retries and possibly
74 * timeouts. Thus, we set our budget to a more reasonable value.
79 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
82 /* Process pending work on NIC */
83 np->dev->poll_controller(np->dev);
85 /* If scheduling is stopped, tickle NAPI bits */
86 spin_lock_irqsave(&netpoll_poll_lock, flags);
88 test_bit(__LINK_STATE_RX_SCHED, &np->dev->state)) {
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);
99 zap_completion_queue();
102 static void refill_skbs(void)
107 spin_lock_irqsave(&skb_list_lock, flags);
108 while (nr_skbs < MAX_SKBS) {
109 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
117 spin_unlock_irqrestore(&skb_list_lock, flags);
120 static void zap_completion_queue(void)
123 struct softnet_data *sd = &get_cpu_var(softnet_data);
125 if (sd->completion_queue) {
126 struct sk_buff *clist;
128 local_irq_save(flags);
129 clist = sd->completion_queue;
130 sd->completion_queue = NULL;
131 local_irq_restore(flags);
133 while (clist != NULL) {
134 struct sk_buff *skb = clist;
140 put_cpu_var(softnet_data);
143 static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
145 int once = 1, count = 0;
147 struct sk_buff *skb = NULL;
149 zap_completion_queue();
151 if (nr_skbs < MAX_SKBS)
154 skb = alloc_skb(len, GFP_ATOMIC);
157 spin_lock_irqsave(&skb_list_lock, flags);
163 spin_unlock_irqrestore(&skb_list_lock, flags);
168 if (once && (count == 1000000)) {
169 printk("out of netpoll skbs!\n");
176 atomic_set(&skb->users, 1);
177 skb_reserve(skb, reserve);
181 void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
186 if(!np || !np->dev || !netif_running(np->dev)) {
191 spin_lock(&np->dev->xmit_lock);
192 np->dev->xmit_lock_owner = smp_processor_id();
195 * network drivers do not expect to be called if the queue is
198 if (netif_queue_stopped(np->dev)) {
199 np->dev->xmit_lock_owner = -1;
200 spin_unlock(&np->dev->xmit_lock);
206 status = np->dev->hard_start_xmit(skb, np->dev);
207 np->dev->xmit_lock_owner = -1;
208 spin_unlock(&np->dev->xmit_lock);
217 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
219 int total_len, eth_len, ip_len, udp_len;
225 udp_len = len + sizeof(*udph);
226 ip_len = eth_len = udp_len + sizeof(*iph);
227 total_len = eth_len + ETH_HLEN;
229 skb = find_skb(np, total_len, total_len - len);
233 memcpy(skb->data, msg, len);
236 udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
237 udph->source = htons(np->local_port);
238 udph->dest = htons(np->remote_port);
239 udph->len = htons(udp_len);
242 iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
244 /* iph->version = 4; iph->ihl = 5; */
245 put_unaligned(0x45, (unsigned char *)iph);
247 put_unaligned(htons(ip_len), &(iph->tot_len));
251 iph->protocol = IPPROTO_UDP;
253 put_unaligned(htonl(np->local_ip), &(iph->saddr));
254 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
255 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
257 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
259 eth->h_proto = htons(ETH_P_IP);
260 memcpy(eth->h_source, np->local_mac, 6);
261 memcpy(eth->h_dest, np->remote_mac, 6);
263 netpoll_send_skb(np, skb);
266 static void arp_reply(struct sk_buff *skb)
269 unsigned char *arp_ptr;
270 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
272 struct sk_buff *send_skb;
275 struct netpoll *np = NULL;
277 spin_lock_irqsave(&rx_list_lock, flags);
278 list_for_each(p, &rx_list) {
279 np = list_entry(p, struct netpoll, rx_list);
280 if ( np->dev == skb->dev )
284 spin_unlock_irqrestore(&rx_list_lock, flags);
288 /* No arp on this interface */
289 if (skb->dev->flags & IFF_NOARP)
292 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
293 (2 * skb->dev->addr_len) +
297 skb->h.raw = skb->nh.raw = skb->data;
300 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
301 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
302 arp->ar_pro != htons(ETH_P_IP) ||
303 arp->ar_op != htons(ARPOP_REQUEST))
306 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
307 memcpy(&sip, arp_ptr, 4);
308 arp_ptr += 4 + skb->dev->addr_len;
309 memcpy(&tip, arp_ptr, 4);
311 /* Should we ignore arp? */
312 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
315 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
316 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
317 LL_RESERVED_SPACE(np->dev));
322 send_skb->nh.raw = send_skb->data;
323 arp = (struct arphdr *) skb_put(send_skb, size);
324 send_skb->dev = skb->dev;
325 send_skb->protocol = htons(ETH_P_ARP);
327 /* Fill the device header for the ARP frame */
329 if (np->dev->hard_header &&
330 np->dev->hard_header(send_skb, skb->dev, ptype,
331 np->remote_mac, np->local_mac,
332 send_skb->len) < 0) {
338 * Fill out the arp protocol part.
340 * we only support ethernet device type,
341 * which (according to RFC 1390) should always equal 1 (Ethernet).
344 arp->ar_hrd = htons(np->dev->type);
345 arp->ar_pro = htons(ETH_P_IP);
346 arp->ar_hln = np->dev->addr_len;
348 arp->ar_op = htons(type);
350 arp_ptr=(unsigned char *)(arp + 1);
351 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
352 arp_ptr += np->dev->addr_len;
353 memcpy(arp_ptr, &tip, 4);
355 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
356 arp_ptr += np->dev->addr_len;
357 memcpy(arp_ptr, &sip, 4);
359 netpoll_send_skb(np, send_skb);
362 int netpoll_rx(struct sk_buff *skb)
364 int proto, len, ulen;
371 if (skb->dev->type != ARPHRD_ETHER)
374 /* check if netpoll clients need ARP */
375 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
376 atomic_read(&trapped)) {
381 proto = ntohs(eth_hdr(skb)->h_proto);
382 if (proto != ETH_P_IP)
384 if (skb->pkt_type == PACKET_OTHERHOST)
389 iph = (struct iphdr *)skb->data;
390 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
392 if (iph->ihl < 5 || iph->version != 4)
394 if (!pskb_may_pull(skb, iph->ihl*4))
396 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
399 len = ntohs(iph->tot_len);
400 if (skb->len < len || len < iph->ihl*4)
403 if (iph->protocol != IPPROTO_UDP)
407 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
408 ulen = ntohs(uh->len);
412 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0)
415 spin_lock_irqsave(&rx_list_lock, flags);
416 list_for_each(p, &rx_list) {
417 np = list_entry(p, struct netpoll, rx_list);
418 if (np->dev && np->dev != skb->dev)
420 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
422 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
424 if (np->local_port && np->local_port != ntohs(uh->dest))
427 spin_unlock_irqrestore(&rx_list_lock, flags);
430 np->rx_hook(np, ntohs(uh->source),
432 ulen - sizeof(struct udphdr));
436 spin_unlock_irqrestore(&rx_list_lock, flags);
439 return atomic_read(&trapped);
442 int netpoll_parse_options(struct netpoll *np, char *opt)
444 char *cur=opt, *delim;
447 if ((delim = strchr(cur, '@')) == NULL)
450 np->local_port=simple_strtol(cur, NULL, 10);
454 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
457 if ((delim = strchr(cur, '/')) == NULL)
460 np->local_ip=ntohl(in_aton(cur));
463 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
464 np->name, HIPQUAD(np->local_ip));
469 /* parse out dev name */
470 if ((delim = strchr(cur, ',')) == NULL)
473 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
478 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
482 if ((delim = strchr(cur, '@')) == NULL)
485 np->remote_port=simple_strtol(cur, NULL, 10);
489 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
492 if ((delim = strchr(cur, '/')) == NULL)
495 np->remote_ip=ntohl(in_aton(cur));
498 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
499 np->name, HIPQUAD(np->remote_ip));
504 if ((delim = strchr(cur, ':')) == NULL)
507 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
509 if ((delim = strchr(cur, ':')) == NULL)
512 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
514 if ((delim = strchr(cur, ':')) == NULL)
517 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
519 if ((delim = strchr(cur, ':')) == NULL)
522 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
524 if ((delim = strchr(cur, ':')) == NULL)
527 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
529 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
532 printk(KERN_INFO "%s: remote ethernet address "
533 "%02x:%02x:%02x:%02x:%02x:%02x\n",
545 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
550 int netpoll_setup(struct netpoll *np)
552 struct net_device *ndev = NULL;
553 struct in_device *in_dev;
556 ndev = dev_get_by_name(np->dev_name);
558 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
559 np->name, np->dev_name);
562 if (!ndev->poll_controller) {
563 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
564 np->name, np->dev_name);
568 if (!(ndev->flags & IFF_UP)) {
569 unsigned short oflags;
570 unsigned long atmost, atleast;
572 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
573 np->name, np->dev_name);
575 oflags = ndev->flags;
578 if (dev_change_flags(ndev, oflags | IFF_UP) < 0) {
579 printk(KERN_ERR "%s: failed to open %s\n",
580 np->name, np->dev_name);
586 atleast = jiffies + HZ/10;
587 atmost = jiffies + 10*HZ;
588 while (!netif_carrier_ok(ndev)) {
589 if (time_after(jiffies, atmost)) {
591 "%s: timeout waiting for carrier\n",
598 if (time_before(jiffies, atleast)) {
599 printk(KERN_NOTICE "%s: carrier detect appears flaky,"
600 " waiting 10 seconds\n",
602 while (time_before(jiffies, atmost))
607 if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr)
608 memcpy(np->local_mac, ndev->dev_addr, 6);
612 in_dev = __in_dev_get(ndev);
616 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
617 np->name, np->dev_name);
621 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
623 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
624 np->name, HIPQUAD(np->local_ip));
632 np->dev->netpoll_rx = NETPOLL_RX_ENABLED;
634 spin_lock_irqsave(&rx_list_lock, flags);
635 list_add(&np->rx_list, &rx_list);
636 spin_unlock_irqrestore(&rx_list_lock, flags);
645 void netpoll_cleanup(struct netpoll *np)
650 spin_lock_irqsave(&rx_list_lock, flags);
651 list_del(&np->rx_list);
652 spin_unlock_irqrestore(&rx_list_lock, flags);
656 np->dev->netpoll_rx = 0;
661 int netpoll_trap(void)
663 return atomic_read(&trapped);
666 void netpoll_set_trap(int trap)
669 atomic_inc(&trapped);
671 atomic_dec(&trapped);
674 EXPORT_SYMBOL(netpoll_set_trap);
675 EXPORT_SYMBOL(netpoll_trap);
676 EXPORT_SYMBOL(netpoll_parse_options);
677 EXPORT_SYMBOL(netpoll_setup);
678 EXPORT_SYMBOL(netpoll_cleanup);
679 EXPORT_SYMBOL(netpoll_send_skb);
680 EXPORT_SYMBOL(netpoll_send_udp);
681 EXPORT_SYMBOL(netpoll_poll);