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/nmi.h>
26 * We maintain a small pool of fully-sized skbs, to make sure the
27 * message gets out even in extreme OOM situations.
31 #define MAX_UDP_CHUNK 1460
33 #define NETPOLL_RX_ENABLED 1
34 #define NETPOLL_RX_DROP 2
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);
45 extern void (*netdump_func) (struct pt_regs *regs);
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)
70 int budget = netdump_mode ? 64 : 16;
72 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
75 /* Process pending work on NIC */
76 np->dev->poll_controller(np->dev);
78 /* If scheduling is stopped, tickle NAPI bits */
80 test_bit(__LINK_STATE_RX_SCHED, &np->dev->state)) {
81 np->dev->netpoll_rx |= NETPOLL_RX_DROP;
83 np->dev->poll(np->dev, &budget);
86 np->dev->poll(np->dev, &budget);
89 np->dev->netpoll_rx &= ~NETPOLL_RX_DROP;
92 zap_completion_queue();
95 static void refill_skbs(void)
100 spin_lock_irqsave(&skb_list_lock, flags);
101 while (nr_skbs < MAX_SKBS) {
102 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
110 spin_unlock_irqrestore(&skb_list_lock, flags);
113 static void zap_completion_queue(void)
116 struct softnet_data *sd = &get_cpu_var(softnet_data);
118 if (sd->completion_queue) {
119 struct sk_buff *clist;
121 local_irq_save(flags);
122 clist = sd->completion_queue;
123 sd->completion_queue = NULL;
124 local_irq_restore(flags);
126 while (clist != NULL) {
127 struct sk_buff *skb = clist;
133 put_cpu_var(softnet_data);
134 touch_nmi_watchdog();
137 static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
139 int once = 1, count = 0;
141 struct sk_buff *skb = NULL;
143 zap_completion_queue();
145 if (nr_skbs < MAX_SKBS)
148 skb = alloc_skb(len, GFP_ATOMIC);
151 spin_lock_irqsave(&skb_list_lock, flags);
157 spin_unlock_irqrestore(&skb_list_lock, flags);
162 if (once && (count == 1000000)) {
163 printk("out of netpoll skbs!\n");
170 atomic_set(&skb->users, 1);
171 skb_reserve(skb, reserve);
175 void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
180 if(!np || !np->dev || !netif_running(np->dev)) {
185 spin_lock(&np->dev->xmit_lock);
186 np->dev->xmit_lock_owner = smp_processor_id();
188 status = np->dev->hard_start_xmit(skb, np->dev);
189 np->dev->xmit_lock_owner = -1;
190 spin_unlock(&np->dev->xmit_lock);
199 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
201 int total_len, eth_len, ip_len, udp_len;
207 udp_len = len + sizeof(*udph);
208 ip_len = eth_len = udp_len + sizeof(*iph);
209 total_len = eth_len + ETH_HLEN;
211 skb = find_skb(np, total_len, total_len - len);
215 memcpy(skb->data, msg, len);
218 udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
219 udph->source = htons(np->local_port);
220 udph->dest = htons(np->remote_port);
221 udph->len = htons(udp_len);
224 iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
229 iph->tot_len = htons(ip_len);
233 iph->protocol = IPPROTO_UDP;
235 iph->saddr = htonl(np->local_ip);
236 iph->daddr = htonl(np->remote_ip);
237 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
239 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
241 eth->h_proto = htons(ETH_P_IP);
242 memcpy(eth->h_source, np->local_mac, 6);
243 memcpy(eth->h_dest, np->remote_mac, 6);
245 netpoll_send_skb(np, skb);
248 static void arp_reply(struct sk_buff *skb)
251 unsigned char *arp_ptr;
252 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
254 struct sk_buff *send_skb;
257 struct netpoll *np = NULL;
259 spin_lock_irqsave(&rx_list_lock, flags);
260 list_for_each(p, &rx_list) {
261 np = list_entry(p, struct netpoll, rx_list);
262 if ( np->dev == skb->dev )
266 spin_unlock_irqrestore(&rx_list_lock, flags);
270 /* No arp on this interface */
271 if (skb->dev->flags & IFF_NOARP)
274 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
275 (2 * skb->dev->addr_len) +
279 skb->h.raw = skb->nh.raw = skb->data;
282 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
283 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
284 arp->ar_pro != htons(ETH_P_IP) ||
285 arp->ar_op != htons(ARPOP_REQUEST))
288 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
289 memcpy(&sip, arp_ptr, 4);
290 arp_ptr += 4 + skb->dev->addr_len;
291 memcpy(&tip, arp_ptr, 4);
293 /* Should we ignore arp? */
294 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
297 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
298 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
299 LL_RESERVED_SPACE(np->dev));
304 send_skb->nh.raw = send_skb->data;
305 arp = (struct arphdr *) skb_put(send_skb, size);
306 send_skb->dev = skb->dev;
307 send_skb->protocol = htons(ETH_P_ARP);
309 /* Fill the device header for the ARP frame */
311 if (np->dev->hard_header &&
312 np->dev->hard_header(send_skb, skb->dev, ptype,
313 np->remote_mac, np->local_mac,
314 send_skb->len) < 0) {
320 * Fill out the arp protocol part.
322 * we only support ethernet device type,
323 * which (according to RFC 1390) should always equal 1 (Ethernet).
326 arp->ar_hrd = htons(np->dev->type);
327 arp->ar_pro = htons(ETH_P_IP);
328 arp->ar_hln = np->dev->addr_len;
330 arp->ar_op = htons(type);
332 arp_ptr=(unsigned char *)(arp + 1);
333 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
334 arp_ptr += np->dev->addr_len;
335 memcpy(arp_ptr, &tip, 4);
337 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
338 arp_ptr += np->dev->addr_len;
339 memcpy(arp_ptr, &sip, 4);
341 netpoll_send_skb(np, send_skb);
344 int netpoll_rx(struct sk_buff *skb)
346 int proto, len, ulen;
353 if (!(skb->dev->netpoll_rx & NETPOLL_RX_ENABLED))
356 if (skb->dev->type != ARPHRD_ETHER)
359 /* check if netpoll clients need ARP */
360 if (skb->protocol == __constant_htons(ETH_P_ARP) && trapped) {
365 proto = ntohs(skb->mac.ethernet->h_proto);
366 if (proto != ETH_P_IP)
368 if (skb->pkt_type == PACKET_OTHERHOST)
373 iph = (struct iphdr *)skb->data;
374 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
376 if (iph->ihl < 5 || iph->version != 4)
378 if (!pskb_may_pull(skb, iph->ihl*4))
380 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
383 len = ntohs(iph->tot_len);
384 if (skb->len < len || len < iph->ihl*4)
387 if (iph->protocol != IPPROTO_UDP)
391 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
392 ulen = ntohs(uh->len);
396 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0)
399 spin_lock_irqsave(&rx_list_lock, flags);
400 list_for_each(p, &rx_list) {
401 np = list_entry(p, struct netpoll, rx_list);
402 if (np->dev && np->dev != skb->dev)
404 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
406 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
408 if (np->local_port && np->local_port != ntohs(uh->dest))
411 spin_unlock_irqrestore(&rx_list_lock, flags);
414 np->rx_hook(np, ntohs(uh->source),
416 ulen - sizeof(struct udphdr));
420 spin_unlock_irqrestore(&rx_list_lock, flags);
426 int netpoll_parse_options(struct netpoll *np, char *opt)
428 char *cur=opt, *delim;
431 if ((delim = strchr(cur, '@')) == NULL)
434 np->local_port=simple_strtol(cur, NULL, 10);
438 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
441 if ((delim = strchr(cur, '/')) == NULL)
444 np->local_ip=ntohl(in_aton(cur));
447 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
448 np->name, HIPQUAD(np->local_ip));
453 /* parse out dev name */
454 if ((delim = strchr(cur, ',')) == NULL)
457 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
462 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
466 if ((delim = strchr(cur, '@')) == NULL)
469 np->remote_port=simple_strtol(cur, NULL, 10);
473 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
476 if ((delim = strchr(cur, '/')) == NULL)
479 np->remote_ip=ntohl(in_aton(cur));
482 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
483 np->name, HIPQUAD(np->remote_ip));
488 if ((delim = strchr(cur, ':')) == NULL)
491 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
493 if ((delim = strchr(cur, ':')) == NULL)
496 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
498 if ((delim = strchr(cur, ':')) == NULL)
501 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
503 if ((delim = strchr(cur, ':')) == NULL)
506 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
508 if ((delim = strchr(cur, ':')) == NULL)
511 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
513 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
516 printk(KERN_INFO "%s: remote ethernet address "
517 "%02x:%02x:%02x:%02x:%02x:%02x\n",
529 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
534 int netpoll_setup(struct netpoll *np)
536 struct net_device *ndev = NULL;
537 struct in_device *in_dev;
540 ndev = dev_get_by_name(np->dev_name);
542 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
543 np->name, np->dev_name);
546 if (!ndev->poll_controller) {
547 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
548 np->name, np->dev_name);
552 if (!(ndev->flags & IFF_UP)) {
553 unsigned short oflags;
554 unsigned long atmost, atleast;
556 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
557 np->name, np->dev_name);
559 oflags = ndev->flags;
562 if (dev_change_flags(ndev, oflags | IFF_UP) < 0) {
563 printk(KERN_ERR "%s: failed to open %s\n",
564 np->name, np->dev_name);
570 atleast = jiffies + HZ/10;
571 atmost = jiffies + 10*HZ;
572 while (!netif_carrier_ok(ndev)) {
573 if (time_after(jiffies, atmost)) {
575 "%s: timeout waiting for carrier\n",
582 if (time_before(jiffies, atleast)) {
583 printk(KERN_NOTICE "%s: carrier detect appears flaky,"
584 " waiting 10 seconds\n",
586 while (time_before(jiffies, atmost))
591 if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr)
592 memcpy(np->local_mac, ndev->dev_addr, 6);
595 in_dev = in_dev_get(ndev);
598 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
599 np->name, np->dev_name);
603 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
605 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
606 np->name, HIPQUAD(np->local_ip));
614 np->dev->netpoll_rx = NETPOLL_RX_ENABLED;
616 spin_lock_irqsave(&rx_list_lock, flags);
617 list_add(&np->rx_list, &rx_list);
618 spin_unlock_irqrestore(&rx_list_lock, flags);
622 netdump_func = np->dump_func;
630 void netpoll_cleanup(struct netpoll *np)
635 spin_lock_irqsave(&rx_list_lock, flags);
636 list_del(&np->rx_list);
637 np->dev->netpoll_rx = 0;
638 spin_unlock_irqrestore(&rx_list_lock, flags);
645 int netpoll_trap(void)
650 void netpoll_set_trap(int trap)
655 void netpoll_reset_locks(struct netpoll *np)
657 spin_lock_init(&rx_list_lock);
658 spin_lock_init(&skb_list_lock);
659 spin_lock_init(&np->dev->xmit_lock);
662 EXPORT_SYMBOL(netpoll_set_trap);
663 EXPORT_SYMBOL(netpoll_trap);
664 EXPORT_SYMBOL(netpoll_parse_options);
665 EXPORT_SYMBOL(netpoll_setup);
666 EXPORT_SYMBOL(netpoll_cleanup);
667 EXPORT_SYMBOL(netpoll_send_skb);
668 EXPORT_SYMBOL(netpoll_send_udp);
669 EXPORT_SYMBOL(netpoll_poll);
670 EXPORT_SYMBOL_GPL(netpoll_reset_locks);