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>
25 * We maintain a small pool of fully-sized skbs, to make sure the
26 * message gets out even in extreme OOM situations.
30 #define MAX_UDP_CHUNK 1460
32 static spinlock_t skb_list_lock = SPIN_LOCK_UNLOCKED;
34 static struct sk_buff *skbs;
36 static spinlock_t rx_list_lock = SPIN_LOCK_UNLOCKED;
37 static LIST_HEAD(rx_list);
41 #define MAX_SKB_SIZE \
42 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
43 sizeof(struct iphdr) + sizeof(struct ethhdr))
45 static void zap_completion_queue(void);
47 static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
48 unsigned short ulen, u32 saddr, u32 daddr)
53 if (skb->ip_summed == CHECKSUM_HW)
54 return csum_tcpudp_magic(
55 saddr, daddr, ulen, IPPROTO_UDP, skb->csum);
57 skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
59 return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
62 void netpoll_poll(struct netpoll *np)
66 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
69 /* Process pending work on NIC */
70 np->dev->poll_controller(np->dev);
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();
79 static void refill_skbs(void)
84 spin_lock_irqsave(&skb_list_lock, flags);
85 while (nr_skbs < MAX_SKBS) {
86 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
94 spin_unlock_irqrestore(&skb_list_lock, flags);
97 static void zap_completion_queue(void)
100 struct softnet_data *sd = &get_cpu_var(softnet_data);
102 if (sd->completion_queue) {
103 struct sk_buff *clist;
105 local_irq_save(flags);
106 clist = sd->completion_queue;
107 sd->completion_queue = NULL;
108 local_irq_restore(flags);
110 while (clist != NULL) {
111 struct sk_buff *skb = clist;
117 put_cpu_var(softnet_data);
120 static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
122 int once = 1, count = 0;
124 struct sk_buff *skb = NULL;
126 zap_completion_queue();
128 if (nr_skbs < MAX_SKBS)
131 skb = alloc_skb(len, GFP_ATOMIC);
134 spin_lock_irqsave(&skb_list_lock, flags);
140 spin_unlock_irqrestore(&skb_list_lock, flags);
145 if (once && (count == 1000000)) {
146 printk("out of netpoll skbs!\n");
153 atomic_set(&skb->users, 1);
154 skb_reserve(skb, reserve);
158 void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
163 if(!np || !np->dev || !netif_running(np->dev)) {
168 spin_lock(&np->dev->xmit_lock);
169 np->dev->xmit_lock_owner = smp_processor_id();
171 status = np->dev->hard_start_xmit(skb, np->dev);
172 np->dev->xmit_lock_owner = -1;
173 spin_unlock(&np->dev->xmit_lock);
182 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
184 int total_len, eth_len, ip_len, udp_len;
190 udp_len = len + sizeof(*udph);
191 ip_len = eth_len = udp_len + sizeof(*iph);
192 total_len = eth_len + ETH_HLEN;
194 skb = find_skb(np, total_len, total_len - len);
198 memcpy(skb->data, msg, len);
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);
207 iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
212 iph->tot_len = htons(ip_len);
216 iph->protocol = IPPROTO_UDP;
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);
222 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
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);
228 netpoll_send_skb(np, skb);
231 static void arp_reply(struct sk_buff *skb)
234 unsigned char *arp_ptr;
235 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
237 struct sk_buff *send_skb;
240 struct netpoll *np = 0;
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 )
249 spin_unlock_irqrestore(&rx_list_lock, flags);
253 /* No arp on this interface */
254 if (skb->dev->flags & IFF_NOARP)
257 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
258 (2 * skb->dev->addr_len) +
262 skb->h.raw = skb->nh.raw = skb->data;
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))
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);
276 /* Should we ignore arp? */
277 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
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));
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);
292 /* Fill the device header for the ARP frame */
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) {
303 * Fill out the arp protocol part.
305 * we only support ethernet device type,
306 * which (according to RFC 1390) should always equal 1 (Ethernet).
309 arp->ar_hrd = htons(np->dev->type);
310 arp->ar_pro = htons(ETH_P_IP);
311 arp->ar_hln = np->dev->addr_len;
313 arp->ar_op = htons(type);
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);
320 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
321 arp_ptr += np->dev->addr_len;
322 memcpy(arp_ptr, &sip, 4);
324 netpoll_send_skb(np, send_skb);
327 int netpoll_rx(struct sk_buff *skb)
329 int proto, len, ulen;
336 if (skb->dev->type != ARPHRD_ETHER)
339 /* check if netpoll clients need ARP */
340 if (skb->protocol == __constant_htons(ETH_P_ARP) && trapped) {
345 proto = ntohs(skb->mac.ethernet->h_proto);
346 if (proto != ETH_P_IP)
348 if (skb->pkt_type == PACKET_OTHERHOST)
353 iph = (struct iphdr *)skb->data;
354 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
356 if (iph->ihl < 5 || iph->version != 4)
358 if (!pskb_may_pull(skb, iph->ihl*4))
360 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
363 len = ntohs(iph->tot_len);
364 if (skb->len < len || len < iph->ihl*4)
367 if (iph->protocol != IPPROTO_UDP)
371 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
372 ulen = ntohs(uh->len);
376 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0)
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)
384 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
386 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
388 if (np->local_port && np->local_port != ntohs(uh->dest))
391 spin_unlock_irqrestore(&rx_list_lock, flags);
394 np->rx_hook(np, ntohs(uh->source),
396 ulen - sizeof(struct udphdr));
400 spin_unlock_irqrestore(&rx_list_lock, flags);
406 int netpoll_parse_options(struct netpoll *np, char *opt)
408 char *cur=opt, *delim;
411 if ((delim = strchr(cur, '@')) == NULL)
414 np->local_port=simple_strtol(cur, 0, 10);
418 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
421 if ((delim = strchr(cur, '/')) == NULL)
424 np->local_ip=ntohl(in_aton(cur));
427 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
428 np->name, HIPQUAD(np->local_ip));
433 /* parse out dev name */
434 if ((delim = strchr(cur, ',')) == NULL)
437 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
442 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
446 if ((delim = strchr(cur, '@')) == NULL)
449 np->remote_port=simple_strtol(cur, 0, 10);
453 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
456 if ((delim = strchr(cur, '/')) == NULL)
459 np->remote_ip=ntohl(in_aton(cur));
462 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
463 np->name, HIPQUAD(np->remote_ip));
468 if ((delim = strchr(cur, ':')) == NULL)
471 np->remote_mac[0]=simple_strtol(cur, 0, 16);
473 if ((delim = strchr(cur, ':')) == NULL)
476 np->remote_mac[1]=simple_strtol(cur, 0, 16);
478 if ((delim = strchr(cur, ':')) == NULL)
481 np->remote_mac[2]=simple_strtol(cur, 0, 16);
483 if ((delim = strchr(cur, ':')) == NULL)
486 np->remote_mac[3]=simple_strtol(cur, 0, 16);
488 if ((delim = strchr(cur, ':')) == NULL)
491 np->remote_mac[4]=simple_strtol(cur, 0, 16);
493 np->remote_mac[5]=simple_strtol(cur, 0, 16);
496 printk(KERN_INFO "%s: remote ethernet address "
497 "%02x:%02x:%02x:%02x:%02x:%02x\n",
509 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
514 int netpoll_setup(struct netpoll *np)
516 struct net_device *ndev = NULL;
517 struct in_device *in_dev;
520 ndev = dev_get_by_name(np->dev_name);
522 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
523 np->name, np->dev_name);
526 if (!ndev->poll_controller) {
527 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
528 np->name, np->dev_name);
532 if (!(ndev->flags & IFF_UP)) {
533 unsigned short oflags;
534 unsigned long atmost, atleast;
536 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
537 np->name, np->dev_name);
539 oflags = ndev->flags;
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);
550 atleast = jiffies + HZ/10;
551 atmost = jiffies + 10*HZ;
552 while (!netif_carrier_ok(ndev)) {
553 if (time_after(jiffies, atmost)) {
555 "%s: timeout waiting for carrier\n",
562 if (time_before(jiffies, atleast)) {
563 printk(KERN_NOTICE "%s: carrier detect appears flaky,"
564 " waiting 10 seconds\n",
566 while (time_before(jiffies, atmost))
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);
575 in_dev = in_dev_get(ndev);
578 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
579 np->name, np->dev_name);
583 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
585 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
586 np->name, HIPQUAD(np->local_ip));
594 #ifdef CONFIG_NETPOLL_RX
595 np->dev->netpoll_rx = 1;
598 spin_lock_irqsave(&rx_list_lock, flags);
599 list_add(&np->rx_list, &rx_list);
600 spin_unlock_irqrestore(&rx_list_lock, flags);
609 void netpoll_cleanup(struct netpoll *np)
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;
619 spin_unlock_irqrestore(&rx_list_lock, flags);
631 void netpoll_set_trap(int trap)
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);