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 atomic_t trapped;
43 #define NETPOLL_RX_ENABLED 1
44 #define NETPOLL_RX_DROP 2
46 #define MAX_SKB_SIZE \
47 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
48 sizeof(struct iphdr) + sizeof(struct ethhdr))
50 static void zap_completion_queue(void);
51 static void arp_reply(struct sk_buff *skb);
53 static void queue_process(void *p)
55 struct netpoll_info *npinfo = p;
58 while ((skb = skb_dequeue(&npinfo->txq))) {
59 struct net_device *dev = skb->dev;
61 if (!netif_device_present(dev) || !netif_running(dev)) {
66 netif_tx_lock_bh(dev);
67 if (netif_queue_stopped(dev) ||
68 dev->hard_start_xmit(skb, dev) != NETDEV_TX_OK) {
69 skb_queue_head(&npinfo->txq, skb);
70 netif_tx_unlock_bh(dev);
72 schedule_delayed_work(&npinfo->tx_work, HZ/10);
75 netif_tx_unlock_bh(dev);
79 void netpoll_queue(struct sk_buff *skb)
81 struct net_device *dev = skb->dev;
82 struct netpoll_info *npinfo = dev->npinfo;
87 skb_queue_tail(&npinfo->txq, skb);
88 schedule_work(&npinfo->tx_work);
92 static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
93 unsigned short ulen, u32 saddr, u32 daddr)
97 if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
100 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
102 if (skb->ip_summed == CHECKSUM_COMPLETE &&
103 !(u16)csum_fold(csum_add(psum, skb->csum)))
108 return __skb_checksum_complete(skb);
112 * Check whether delayed processing was scheduled for our NIC. If so,
113 * we attempt to grab the poll lock and use ->poll() to pump the card.
114 * If this fails, either we've recursed in ->poll() or it's already
115 * running on another CPU.
117 * Note: we don't mask interrupts with this lock because we're using
118 * trylock here and interrupts are already disabled in the softirq
119 * case. Further, we test the poll_owner to avoid recursion on UP
120 * systems where the lock doesn't exist.
122 * In cases where there is bi-directional communications, reading only
123 * one message at a time can lead to packets being dropped by the
124 * network adapter, forcing superfluous retries and possibly timeouts.
125 * Thus, we set our budget to greater than 1.
127 static void poll_napi(struct netpoll *np)
129 struct netpoll_info *npinfo = np->dev->npinfo;
132 if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
133 npinfo->poll_owner != smp_processor_id() &&
134 spin_trylock(&npinfo->poll_lock)) {
135 npinfo->rx_flags |= NETPOLL_RX_DROP;
136 atomic_inc(&trapped);
138 np->dev->poll(np->dev, &budget);
140 atomic_dec(&trapped);
141 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
142 spin_unlock(&npinfo->poll_lock);
146 static void service_arp_queue(struct netpoll_info *npi)
153 skb = skb_dequeue(&npi->arp_tx);
155 while (skb != NULL) {
157 skb = skb_dequeue(&npi->arp_tx);
162 void netpoll_poll(struct netpoll *np)
164 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
167 /* Process pending work on NIC */
168 np->dev->poll_controller(np->dev);
172 service_arp_queue(np->dev->npinfo);
174 zap_completion_queue();
177 static void refill_skbs(void)
182 spin_lock_irqsave(&skb_pool.lock, flags);
183 while (skb_pool.qlen < MAX_SKBS) {
184 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
188 __skb_queue_tail(&skb_pool, skb);
190 spin_unlock_irqrestore(&skb_pool.lock, flags);
193 static void zap_completion_queue(void)
196 struct softnet_data *sd = &get_cpu_var(softnet_data);
198 if (sd->completion_queue) {
199 struct sk_buff *clist;
201 local_irq_save(flags);
202 clist = sd->completion_queue;
203 sd->completion_queue = NULL;
204 local_irq_restore(flags);
206 while (clist != NULL) {
207 struct sk_buff *skb = clist;
210 dev_kfree_skb_any(skb); /* put this one back */
216 put_cpu_var(softnet_data);
219 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
224 zap_completion_queue();
228 skb = alloc_skb(len, GFP_ATOMIC);
230 skb = skb_dequeue(&skb_pool);
240 atomic_set(&skb->users, 1);
241 skb_reserve(skb, reserve);
245 static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
248 struct netpoll_info *npinfo;
250 if (!np || !np->dev || !netif_running(np->dev)) {
255 npinfo = np->dev->npinfo;
257 /* avoid recursion */
258 if (npinfo->poll_owner == smp_processor_id() ||
259 np->dev->xmit_lock_owner == smp_processor_id()) {
269 netif_tx_lock(np->dev);
272 * network drivers do not expect to be called if the queue is
275 status = NETDEV_TX_BUSY;
276 if (!netif_queue_stopped(np->dev))
277 status = np->dev->hard_start_xmit(skb, np->dev);
279 netif_tx_unlock(np->dev);
283 npinfo->tries = MAX_RETRIES; /* reset */
290 } while (npinfo->tries > 0);
293 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
295 int total_len, eth_len, ip_len, udp_len;
301 udp_len = len + sizeof(*udph);
302 ip_len = eth_len = udp_len + sizeof(*iph);
303 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
305 skb = find_skb(np, total_len, total_len - len);
309 memcpy(skb->data, msg, len);
312 skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
313 udph->source = htons(np->local_port);
314 udph->dest = htons(np->remote_port);
315 udph->len = htons(udp_len);
317 udph->check = csum_tcpudp_magic(htonl(np->local_ip),
318 htonl(np->remote_ip),
319 udp_len, IPPROTO_UDP,
320 csum_partial((unsigned char *)udph, udp_len, 0));
321 if (udph->check == 0)
324 skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
326 /* iph->version = 4; iph->ihl = 5; */
327 put_unaligned(0x45, (unsigned char *)iph);
329 put_unaligned(htons(ip_len), &(iph->tot_len));
333 iph->protocol = IPPROTO_UDP;
335 put_unaligned(htonl(np->local_ip), &(iph->saddr));
336 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
337 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
339 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
340 skb->mac.raw = skb->data;
341 skb->protocol = eth->h_proto = htons(ETH_P_IP);
342 memcpy(eth->h_source, np->local_mac, 6);
343 memcpy(eth->h_dest, np->remote_mac, 6);
347 netpoll_send_skb(np, skb);
350 static void arp_reply(struct sk_buff *skb)
352 struct netpoll_info *npinfo = skb->dev->npinfo;
354 unsigned char *arp_ptr;
355 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
357 struct sk_buff *send_skb;
358 struct netpoll *np = NULL;
360 if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
365 /* No arp on this interface */
366 if (skb->dev->flags & IFF_NOARP)
369 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
370 (2 * skb->dev->addr_len) +
374 skb->h.raw = skb->nh.raw = skb->data;
377 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
378 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
379 arp->ar_pro != htons(ETH_P_IP) ||
380 arp->ar_op != htons(ARPOP_REQUEST))
383 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
384 memcpy(&sip, arp_ptr, 4);
385 arp_ptr += 4 + skb->dev->addr_len;
386 memcpy(&tip, arp_ptr, 4);
388 /* Should we ignore arp? */
389 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
392 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
393 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
394 LL_RESERVED_SPACE(np->dev));
399 send_skb->nh.raw = send_skb->data;
400 arp = (struct arphdr *) skb_put(send_skb, size);
401 send_skb->dev = skb->dev;
402 send_skb->protocol = htons(ETH_P_ARP);
404 /* Fill the device header for the ARP frame */
406 if (np->dev->hard_header &&
407 np->dev->hard_header(send_skb, skb->dev, ptype,
408 np->remote_mac, np->local_mac,
409 send_skb->len) < 0) {
415 * Fill out the arp protocol part.
417 * we only support ethernet device type,
418 * which (according to RFC 1390) should always equal 1 (Ethernet).
421 arp->ar_hrd = htons(np->dev->type);
422 arp->ar_pro = htons(ETH_P_IP);
423 arp->ar_hln = np->dev->addr_len;
425 arp->ar_op = htons(type);
427 arp_ptr=(unsigned char *)(arp + 1);
428 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
429 arp_ptr += np->dev->addr_len;
430 memcpy(arp_ptr, &tip, 4);
432 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
433 arp_ptr += np->dev->addr_len;
434 memcpy(arp_ptr, &sip, 4);
436 netpoll_send_skb(np, send_skb);
439 int __netpoll_rx(struct sk_buff *skb)
441 int proto, len, ulen;
444 struct netpoll_info *npi = skb->dev->npinfo;
445 struct netpoll *np = npi->rx_np;
450 if (skb->dev->type != ARPHRD_ETHER)
453 /* check if netpoll clients need ARP */
454 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
455 atomic_read(&trapped)) {
456 skb_queue_tail(&npi->arp_tx, skb);
460 proto = ntohs(eth_hdr(skb)->h_proto);
461 if (proto != ETH_P_IP)
463 if (skb->pkt_type == PACKET_OTHERHOST)
468 iph = (struct iphdr *)skb->data;
469 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
471 if (iph->ihl < 5 || iph->version != 4)
473 if (!pskb_may_pull(skb, iph->ihl*4))
475 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
478 len = ntohs(iph->tot_len);
479 if (skb->len < len || len < iph->ihl*4)
482 if (iph->protocol != IPPROTO_UDP)
486 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
487 ulen = ntohs(uh->len);
491 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
493 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
495 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
497 if (np->local_port && np->local_port != ntohs(uh->dest))
500 np->rx_hook(np, ntohs(uh->source),
502 ulen - sizeof(struct udphdr));
508 if (atomic_read(&trapped)) {
516 int netpoll_parse_options(struct netpoll *np, char *opt)
518 char *cur=opt, *delim;
521 if ((delim = strchr(cur, '@')) == NULL)
524 np->local_port=simple_strtol(cur, NULL, 10);
528 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
531 if ((delim = strchr(cur, '/')) == NULL)
534 np->local_ip=ntohl(in_aton(cur));
537 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
538 np->name, HIPQUAD(np->local_ip));
543 /* parse out dev name */
544 if ((delim = strchr(cur, ',')) == NULL)
547 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
552 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
556 if ((delim = strchr(cur, '@')) == NULL)
559 np->remote_port=simple_strtol(cur, NULL, 10);
563 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
566 if ((delim = strchr(cur, '/')) == NULL)
569 np->remote_ip=ntohl(in_aton(cur));
572 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
573 np->name, HIPQUAD(np->remote_ip));
578 if ((delim = strchr(cur, ':')) == NULL)
581 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
583 if ((delim = strchr(cur, ':')) == NULL)
586 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
588 if ((delim = strchr(cur, ':')) == NULL)
591 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
593 if ((delim = strchr(cur, ':')) == NULL)
596 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
598 if ((delim = strchr(cur, ':')) == NULL)
601 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
603 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
606 printk(KERN_INFO "%s: remote ethernet address "
607 "%02x:%02x:%02x:%02x:%02x:%02x\n",
619 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
624 int netpoll_setup(struct netpoll *np)
626 struct net_device *ndev = NULL;
627 struct in_device *in_dev;
628 struct netpoll_info *npinfo;
633 ndev = dev_get_by_name(np->dev_name);
635 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
636 np->name, np->dev_name);
642 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
648 npinfo->rx_flags = 0;
649 npinfo->rx_np = NULL;
650 spin_lock_init(&npinfo->poll_lock);
651 npinfo->poll_owner = -1;
652 npinfo->tries = MAX_RETRIES;
653 spin_lock_init(&npinfo->rx_lock);
654 skb_queue_head_init(&npinfo->arp_tx);
655 skb_queue_head_init(&npinfo->txq);
656 INIT_WORK(&npinfo->tx_work, queue_process, npinfo);
658 atomic_set(&npinfo->refcnt, 1);
660 npinfo = ndev->npinfo;
661 atomic_inc(&npinfo->refcnt);
664 if (!ndev->poll_controller) {
665 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
666 np->name, np->dev_name);
671 if (!netif_running(ndev)) {
672 unsigned long atmost, atleast;
674 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
675 np->name, np->dev_name);
678 err = dev_open(ndev);
682 printk(KERN_ERR "%s: failed to open %s\n",
683 np->name, ndev->name);
687 atleast = jiffies + HZ/10;
688 atmost = jiffies + 4*HZ;
689 while (!netif_carrier_ok(ndev)) {
690 if (time_after(jiffies, atmost)) {
692 "%s: timeout waiting for carrier\n",
699 /* If carrier appears to come up instantly, we don't
700 * trust it and pause so that we don't pump all our
701 * queued console messages into the bitbucket.
704 if (time_before(jiffies, atleast)) {
705 printk(KERN_NOTICE "%s: carrier detect appears"
706 " untrustworthy, waiting 4 seconds\n",
712 if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
713 memcpy(np->local_mac, ndev->dev_addr, 6);
717 in_dev = __in_dev_get_rcu(ndev);
719 if (!in_dev || !in_dev->ifa_list) {
721 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
722 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);
783 skb_queue_purge(&npinfo->txq);
784 cancel_rearming_delayed_work(&npinfo->tx_work);
785 flush_scheduled_work();
797 int netpoll_trap(void)
799 return atomic_read(&trapped);
802 void netpoll_set_trap(int trap)
805 atomic_inc(&trapped);
807 atomic_dec(&trapped);
810 EXPORT_SYMBOL(netpoll_set_trap);
811 EXPORT_SYMBOL(netpoll_trap);
812 EXPORT_SYMBOL(netpoll_parse_options);
813 EXPORT_SYMBOL(netpoll_setup);
814 EXPORT_SYMBOL(netpoll_cleanup);
815 EXPORT_SYMBOL(netpoll_send_udp);
816 EXPORT_SYMBOL(netpoll_poll);
817 EXPORT_SYMBOL(netpoll_queue);