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/if_arp.h>
17 #include <linux/inetdevice.h>
18 #include <linux/inet.h>
19 #include <linux/interrupt.h>
20 #include <linux/netpoll.h>
21 #include <linux/sched.h>
22 #include <linux/delay.h>
23 #include <linux/rcupdate.h>
24 #include <linux/workqueue.h>
27 #include <asm/unaligned.h>
30 * We maintain a small pool of fully-sized skbs, to make sure the
31 * message gets out even in extreme OOM situations.
34 #define MAX_UDP_CHUNK 1460
36 #define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
37 #define MAX_RETRIES 20000
39 static struct sk_buff_head skb_pool;
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);
55 static void arp_reply(struct sk_buff *skb);
57 static void queue_process(void *p)
63 spin_lock_irqsave(&queue_lock, flags);
66 queue_head = skb->next;
67 if (skb == queue_tail)
72 spin_unlock_irqrestore(&queue_lock, flags);
78 static DECLARE_WORK(send_queue, queue_process, NULL);
80 void netpoll_queue(struct sk_buff *skb)
84 if (queue_depth == MAX_QUEUE_DEPTH) {
89 spin_lock_irqsave(&queue_lock, flags);
93 queue_tail->next = skb;
96 spin_unlock_irqrestore(&queue_lock, flags);
98 schedule_work(&send_queue);
101 static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
102 unsigned short ulen, u32 saddr, u32 daddr)
106 if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
109 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
111 if (skb->ip_summed == CHECKSUM_COMPLETE &&
112 !(u16)csum_fold(csum_add(psum, skb->csum)))
117 return __skb_checksum_complete(skb);
121 * Check whether delayed processing was scheduled for our NIC. If so,
122 * we attempt to grab the poll lock and use ->poll() to pump the card.
123 * If this fails, either we've recursed in ->poll() or it's already
124 * running on another CPU.
126 * Note: we don't mask interrupts with this lock because we're using
127 * trylock here and interrupts are already disabled in the softirq
128 * case. Further, we test the poll_owner to avoid recursion on UP
129 * systems where the lock doesn't exist.
131 * In cases where there is bi-directional communications, reading only
132 * one message at a time can lead to packets being dropped by the
133 * network adapter, forcing superfluous retries and possibly timeouts.
134 * Thus, we set our budget to greater than 1.
136 static void poll_napi(struct netpoll *np)
138 struct netpoll_info *npinfo = np->dev->npinfo;
141 if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
142 npinfo->poll_owner != smp_processor_id() &&
143 spin_trylock(&npinfo->poll_lock)) {
144 npinfo->rx_flags |= NETPOLL_RX_DROP;
145 atomic_inc(&trapped);
147 np->dev->poll(np->dev, &budget);
149 atomic_dec(&trapped);
150 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
151 spin_unlock(&npinfo->poll_lock);
155 static void service_arp_queue(struct netpoll_info *npi)
162 skb = skb_dequeue(&npi->arp_tx);
164 while (skb != NULL) {
166 skb = skb_dequeue(&npi->arp_tx);
171 void netpoll_poll(struct netpoll *np)
173 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
176 /* Process pending work on NIC */
177 np->dev->poll_controller(np->dev);
181 service_arp_queue(np->dev->npinfo);
183 zap_completion_queue();
186 static void refill_skbs(void)
191 spin_lock_irqsave(&skb_pool.lock, flags);
192 while (skb_pool.qlen < MAX_SKBS) {
193 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
197 __skb_queue_tail(&skb_pool, skb);
199 spin_unlock_irqrestore(&skb_pool.lock, flags);
202 static void zap_completion_queue(void)
205 struct softnet_data *sd = &get_cpu_var(softnet_data);
207 if (sd->completion_queue) {
208 struct sk_buff *clist;
210 local_irq_save(flags);
211 clist = sd->completion_queue;
212 sd->completion_queue = NULL;
213 local_irq_restore(flags);
215 while (clist != NULL) {
216 struct sk_buff *skb = clist;
219 dev_kfree_skb_any(skb); /* put this one back */
225 put_cpu_var(softnet_data);
228 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
233 zap_completion_queue();
237 skb = alloc_skb(len, GFP_ATOMIC);
239 skb = skb_dequeue(&skb_pool);
249 atomic_set(&skb->users, 1);
250 skb_reserve(skb, reserve);
254 static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
257 struct netpoll_info *npinfo;
259 if (!np || !np->dev || !netif_running(np->dev)) {
264 npinfo = np->dev->npinfo;
266 /* avoid recursion */
267 if (npinfo->poll_owner == smp_processor_id() ||
268 np->dev->xmit_lock_owner == smp_processor_id()) {
278 netif_tx_lock(np->dev);
281 * network drivers do not expect to be called if the queue is
284 status = NETDEV_TX_BUSY;
285 if (!netif_queue_stopped(np->dev))
286 status = np->dev->hard_start_xmit(skb, np->dev);
288 netif_tx_unlock(np->dev);
292 npinfo->tries = MAX_RETRIES; /* reset */
299 } while (npinfo->tries > 0);
302 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
304 int total_len, eth_len, ip_len, udp_len;
310 udp_len = len + sizeof(*udph);
311 ip_len = eth_len = udp_len + sizeof(*iph);
312 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
314 skb = find_skb(np, total_len, total_len - len);
318 memcpy(skb->data, msg, len);
321 skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
322 udph->source = htons(np->local_port);
323 udph->dest = htons(np->remote_port);
324 udph->len = htons(udp_len);
326 udph->check = csum_tcpudp_magic(htonl(np->local_ip),
327 htonl(np->remote_ip),
328 udp_len, IPPROTO_UDP,
329 csum_partial((unsigned char *)udph, udp_len, 0));
330 if (udph->check == 0)
333 skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
335 /* iph->version = 4; iph->ihl = 5; */
336 put_unaligned(0x45, (unsigned char *)iph);
338 put_unaligned(htons(ip_len), &(iph->tot_len));
342 iph->protocol = IPPROTO_UDP;
344 put_unaligned(htonl(np->local_ip), &(iph->saddr));
345 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
346 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
348 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
349 skb->mac.raw = skb->data;
350 skb->protocol = eth->h_proto = htons(ETH_P_IP);
351 memcpy(eth->h_source, np->local_mac, 6);
352 memcpy(eth->h_dest, np->remote_mac, 6);
356 netpoll_send_skb(np, skb);
359 static void arp_reply(struct sk_buff *skb)
361 struct netpoll_info *npinfo = skb->dev->npinfo;
363 unsigned char *arp_ptr;
364 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
366 struct sk_buff *send_skb;
367 struct netpoll *np = NULL;
369 if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
374 /* No arp on this interface */
375 if (skb->dev->flags & IFF_NOARP)
378 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
379 (2 * skb->dev->addr_len) +
383 skb->h.raw = skb->nh.raw = skb->data;
386 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
387 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
388 arp->ar_pro != htons(ETH_P_IP) ||
389 arp->ar_op != htons(ARPOP_REQUEST))
392 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
393 memcpy(&sip, arp_ptr, 4);
394 arp_ptr += 4 + skb->dev->addr_len;
395 memcpy(&tip, arp_ptr, 4);
397 /* Should we ignore arp? */
398 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
401 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
402 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
403 LL_RESERVED_SPACE(np->dev));
408 send_skb->nh.raw = send_skb->data;
409 arp = (struct arphdr *) skb_put(send_skb, size);
410 send_skb->dev = skb->dev;
411 send_skb->protocol = htons(ETH_P_ARP);
413 /* Fill the device header for the ARP frame */
415 if (np->dev->hard_header &&
416 np->dev->hard_header(send_skb, skb->dev, ptype,
417 np->remote_mac, np->local_mac,
418 send_skb->len) < 0) {
424 * Fill out the arp protocol part.
426 * we only support ethernet device type,
427 * which (according to RFC 1390) should always equal 1 (Ethernet).
430 arp->ar_hrd = htons(np->dev->type);
431 arp->ar_pro = htons(ETH_P_IP);
432 arp->ar_hln = np->dev->addr_len;
434 arp->ar_op = htons(type);
436 arp_ptr=(unsigned char *)(arp + 1);
437 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
438 arp_ptr += np->dev->addr_len;
439 memcpy(arp_ptr, &tip, 4);
441 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
442 arp_ptr += np->dev->addr_len;
443 memcpy(arp_ptr, &sip, 4);
445 netpoll_send_skb(np, send_skb);
448 int __netpoll_rx(struct sk_buff *skb)
450 int proto, len, ulen;
453 struct netpoll_info *npi = skb->dev->npinfo;
454 struct netpoll *np = npi->rx_np;
459 if (skb->dev->type != ARPHRD_ETHER)
462 /* check if netpoll clients need ARP */
463 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
464 atomic_read(&trapped)) {
465 skb_queue_tail(&npi->arp_tx, skb);
469 proto = ntohs(eth_hdr(skb)->h_proto);
470 if (proto != ETH_P_IP)
472 if (skb->pkt_type == PACKET_OTHERHOST)
477 iph = (struct iphdr *)skb->data;
478 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
480 if (iph->ihl < 5 || iph->version != 4)
482 if (!pskb_may_pull(skb, iph->ihl*4))
484 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
487 len = ntohs(iph->tot_len);
488 if (skb->len < len || len < iph->ihl*4)
491 if (iph->protocol != IPPROTO_UDP)
495 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
496 ulen = ntohs(uh->len);
500 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
502 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
504 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
506 if (np->local_port && np->local_port != ntohs(uh->dest))
509 np->rx_hook(np, ntohs(uh->source),
511 ulen - sizeof(struct udphdr));
517 if (atomic_read(&trapped)) {
525 int netpoll_parse_options(struct netpoll *np, char *opt)
527 char *cur=opt, *delim;
530 if ((delim = strchr(cur, '@')) == NULL)
533 np->local_port=simple_strtol(cur, NULL, 10);
537 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
540 if ((delim = strchr(cur, '/')) == NULL)
543 np->local_ip=ntohl(in_aton(cur));
546 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
547 np->name, HIPQUAD(np->local_ip));
552 /* parse out dev name */
553 if ((delim = strchr(cur, ',')) == NULL)
556 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
561 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
565 if ((delim = strchr(cur, '@')) == NULL)
568 np->remote_port=simple_strtol(cur, NULL, 10);
572 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
575 if ((delim = strchr(cur, '/')) == NULL)
578 np->remote_ip=ntohl(in_aton(cur));
581 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
582 np->name, HIPQUAD(np->remote_ip));
587 if ((delim = strchr(cur, ':')) == NULL)
590 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
592 if ((delim = strchr(cur, ':')) == NULL)
595 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
597 if ((delim = strchr(cur, ':')) == NULL)
600 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
602 if ((delim = strchr(cur, ':')) == NULL)
605 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
607 if ((delim = strchr(cur, ':')) == NULL)
610 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
612 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
615 printk(KERN_INFO "%s: remote ethernet address "
616 "%02x:%02x:%02x:%02x:%02x:%02x\n",
628 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
633 int netpoll_setup(struct netpoll *np)
635 struct net_device *ndev = NULL;
636 struct in_device *in_dev;
637 struct netpoll_info *npinfo;
641 ndev = dev_get_by_name(np->dev_name);
643 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
644 np->name, np->dev_name);
650 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
654 npinfo->rx_flags = 0;
655 npinfo->rx_np = NULL;
656 spin_lock_init(&npinfo->poll_lock);
657 npinfo->poll_owner = -1;
658 npinfo->tries = MAX_RETRIES;
659 spin_lock_init(&npinfo->rx_lock);
660 skb_queue_head_init(&npinfo->arp_tx);
661 atomic_set(&npinfo->refcnt, 1);
663 npinfo = ndev->npinfo;
664 atomic_inc(&npinfo->refcnt);
667 if (!ndev->poll_controller) {
668 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
669 np->name, np->dev_name);
673 if (!netif_running(ndev)) {
674 unsigned long atmost, atleast;
676 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
677 np->name, np->dev_name);
680 if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) {
681 printk(KERN_ERR "%s: failed to open %s\n",
682 np->name, np->dev_name);
688 atleast = jiffies + HZ/10;
689 atmost = jiffies + 4*HZ;
690 while (!netif_carrier_ok(ndev)) {
691 if (time_after(jiffies, atmost)) {
693 "%s: timeout waiting for carrier\n",
700 /* If carrier appears to come up instantly, we don't
701 * trust it and pause so that we don't pump all our
702 * queued console messages into the bitbucket.
705 if (time_before(jiffies, atleast)) {
706 printk(KERN_NOTICE "%s: carrier detect appears"
707 " untrustworthy, waiting 4 seconds\n",
713 if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
714 memcpy(np->local_mac, ndev->dev_addr, 6);
718 in_dev = __in_dev_get_rcu(ndev);
720 if (!in_dev || !in_dev->ifa_list) {
722 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
723 np->name, np->dev_name);
727 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
729 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
730 np->name, HIPQUAD(np->local_ip));
734 spin_lock_irqsave(&npinfo->rx_lock, flags);
735 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
737 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
740 /* fill up the skb queue */
743 /* last thing to do is link it to the net device structure */
744 ndev->npinfo = npinfo;
746 /* avoid racing with NAPI reading npinfo */
759 static int __init netpoll_init(void) {
760 skb_queue_head_init(&skb_pool);
763 core_initcall(netpoll_init);
765 void netpoll_cleanup(struct netpoll *np)
767 struct netpoll_info *npinfo;
771 npinfo = np->dev->npinfo;
773 if (npinfo->rx_np == np) {
774 spin_lock_irqsave(&npinfo->rx_lock, flags);
775 npinfo->rx_np = NULL;
776 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
777 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
780 np->dev->npinfo = NULL;
781 if (atomic_dec_and_test(&npinfo->refcnt)) {
782 skb_queue_purge(&npinfo->arp_tx);
794 int netpoll_trap(void)
796 return atomic_read(&trapped);
799 void netpoll_set_trap(int trap)
802 atomic_inc(&trapped);
804 atomic_dec(&trapped);
807 EXPORT_SYMBOL(netpoll_set_trap);
808 EXPORT_SYMBOL(netpoll_trap);
809 EXPORT_SYMBOL(netpoll_parse_options);
810 EXPORT_SYMBOL(netpoll_setup);
811 EXPORT_SYMBOL(netpoll_cleanup);
812 EXPORT_SYMBOL(netpoll_send_udp);
813 EXPORT_SYMBOL(netpoll_poll);
814 EXPORT_SYMBOL(netpoll_queue);