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/moduleparam.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>
25 #include <linux/slab.h>
28 #include <asm/unaligned.h>
29 #include <trace/events/napi.h>
32 * We maintain a small pool of fully-sized skbs, to make sure the
33 * message gets out even in extreme OOM situations.
36 #define MAX_UDP_CHUNK 1460
38 #define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
40 static struct sk_buff_head skb_pool;
42 static atomic_t trapped;
44 #define USEC_PER_POLL 50
45 #define NETPOLL_RX_ENABLED 1
46 #define NETPOLL_RX_DROP 2
48 #define MAX_SKB_SIZE \
49 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
50 sizeof(struct iphdr) + sizeof(struct ethhdr))
52 static void arp_reply(struct sk_buff *skb);
54 static unsigned int carrier_timeout = 4;
55 module_param(carrier_timeout, uint, 0644);
57 static void queue_process(struct work_struct *work)
59 struct netpoll_info *npinfo =
60 container_of(work, struct netpoll_info, tx_work.work);
64 while ((skb = skb_dequeue(&npinfo->txq))) {
65 struct net_device *dev = skb->dev;
66 const struct net_device_ops *ops = dev->netdev_ops;
67 struct netdev_queue *txq;
69 if (!netif_device_present(dev) || !netif_running(dev)) {
74 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
76 local_irq_save(flags);
77 __netif_tx_lock(txq, smp_processor_id());
78 if (netif_tx_queue_stopped(txq) ||
79 netif_tx_queue_frozen(txq) ||
80 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
81 skb_queue_head(&npinfo->txq, skb);
82 __netif_tx_unlock(txq);
83 local_irq_restore(flags);
85 schedule_delayed_work(&npinfo->tx_work, HZ/10);
88 __netif_tx_unlock(txq);
89 local_irq_restore(flags);
93 static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
94 unsigned short ulen, __be32 saddr, __be32 daddr)
98 if (uh->check == 0 || skb_csum_unnecessary(skb))
101 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
103 if (skb->ip_summed == CHECKSUM_COMPLETE &&
104 !csum_fold(csum_add(psum, skb->csum)))
109 return __skb_checksum_complete(skb);
113 * Check whether delayed processing was scheduled for our NIC. If so,
114 * we attempt to grab the poll lock and use ->poll() to pump the card.
115 * If this fails, either we've recursed in ->poll() or it's already
116 * running on another CPU.
118 * Note: we don't mask interrupts with this lock because we're using
119 * trylock here and interrupts are already disabled in the softirq
120 * case. Further, we test the poll_owner to avoid recursion on UP
121 * systems where the lock doesn't exist.
123 * In cases where there is bi-directional communications, reading only
124 * one message at a time can lead to packets being dropped by the
125 * network adapter, forcing superfluous retries and possibly timeouts.
126 * Thus, we set our budget to greater than 1.
128 static int poll_one_napi(struct netpoll_info *npinfo,
129 struct napi_struct *napi, int budget)
133 /* net_rx_action's ->poll() invocations and our's are
134 * synchronized by this test which is only made while
135 * holding the napi->poll_lock.
137 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
140 npinfo->rx_flags |= NETPOLL_RX_DROP;
141 atomic_inc(&trapped);
142 set_bit(NAPI_STATE_NPSVC, &napi->state);
144 work = napi->poll(napi, budget);
145 trace_napi_poll(napi);
147 clear_bit(NAPI_STATE_NPSVC, &napi->state);
148 atomic_dec(&trapped);
149 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
151 return budget - work;
154 static void poll_napi(struct net_device *dev)
156 struct napi_struct *napi;
159 list_for_each_entry(napi, &dev->napi_list, dev_list) {
160 if (napi->poll_owner != smp_processor_id() &&
161 spin_trylock(&napi->poll_lock)) {
162 budget = poll_one_napi(dev->npinfo, napi, budget);
163 spin_unlock(&napi->poll_lock);
171 static void service_arp_queue(struct netpoll_info *npi)
176 while ((skb = skb_dequeue(&npi->arp_tx)))
181 void netpoll_poll_dev(struct net_device *dev)
183 const struct net_device_ops *ops;
185 if (!dev || !netif_running(dev))
188 ops = dev->netdev_ops;
189 if (!ops->ndo_poll_controller)
192 /* Process pending work on NIC */
193 ops->ndo_poll_controller(dev);
197 service_arp_queue(dev->npinfo);
201 void netpoll_poll(struct netpoll *np)
203 netpoll_poll_dev(np->dev);
206 static void refill_skbs(void)
211 spin_lock_irqsave(&skb_pool.lock, flags);
212 while (skb_pool.qlen < MAX_SKBS) {
213 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
217 __skb_queue_tail(&skb_pool, skb);
219 spin_unlock_irqrestore(&skb_pool.lock, flags);
222 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
230 skb = alloc_skb(len, GFP_ATOMIC);
232 skb = skb_dequeue(&skb_pool);
242 atomic_set(&skb->users, 1);
243 skb_reserve(skb, reserve);
247 static int netpoll_owner_active(struct net_device *dev)
249 struct napi_struct *napi;
251 list_for_each_entry(napi, &dev->napi_list, dev_list) {
252 if (napi->poll_owner == smp_processor_id())
258 void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
260 int status = NETDEV_TX_BUSY;
262 struct net_device *dev = np->dev;
263 const struct net_device_ops *ops = dev->netdev_ops;
264 struct netpoll_info *npinfo = np->dev->npinfo;
266 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
271 /* don't get messages out of order, and no recursion */
272 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
273 struct netdev_queue *txq;
276 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
278 local_irq_save(flags);
279 /* try until next clock tick */
280 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
281 tries > 0; --tries) {
282 if (__netif_tx_trylock(txq)) {
283 if (!netif_tx_queue_stopped(txq)) {
284 dev->priv_flags |= IFF_IN_NETPOLL;
285 status = ops->ndo_start_xmit(skb, dev);
286 dev->priv_flags &= ~IFF_IN_NETPOLL;
287 if (status == NETDEV_TX_OK)
288 txq_trans_update(txq);
290 __netif_tx_unlock(txq);
292 if (status == NETDEV_TX_OK)
297 /* tickle device maybe there is some cleanup */
300 udelay(USEC_PER_POLL);
303 WARN_ONCE(!irqs_disabled(),
304 "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n",
305 dev->name, ops->ndo_start_xmit);
307 local_irq_restore(flags);
310 if (status != NETDEV_TX_OK) {
311 skb_queue_tail(&npinfo->txq, skb);
312 schedule_delayed_work(&npinfo->tx_work,0);
316 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
318 int total_len, eth_len, ip_len, udp_len;
324 udp_len = len + sizeof(*udph);
325 ip_len = eth_len = udp_len + sizeof(*iph);
326 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
328 skb = find_skb(np, total_len, total_len - len);
332 skb_copy_to_linear_data(skb, msg, len);
335 skb_push(skb, sizeof(*udph));
336 skb_reset_transport_header(skb);
338 udph->source = htons(np->local_port);
339 udph->dest = htons(np->remote_port);
340 udph->len = htons(udp_len);
342 udph->check = csum_tcpudp_magic(np->local_ip,
344 udp_len, IPPROTO_UDP,
345 csum_partial(udph, udp_len, 0));
346 if (udph->check == 0)
347 udph->check = CSUM_MANGLED_0;
349 skb_push(skb, sizeof(*iph));
350 skb_reset_network_header(skb);
353 /* iph->version = 4; iph->ihl = 5; */
354 put_unaligned(0x45, (unsigned char *)iph);
356 put_unaligned(htons(ip_len), &(iph->tot_len));
360 iph->protocol = IPPROTO_UDP;
362 put_unaligned(np->local_ip, &(iph->saddr));
363 put_unaligned(np->remote_ip, &(iph->daddr));
364 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
366 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
367 skb_reset_mac_header(skb);
368 skb->protocol = eth->h_proto = htons(ETH_P_IP);
369 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
370 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
374 netpoll_send_skb(np, skb);
377 static void arp_reply(struct sk_buff *skb)
379 struct netpoll_info *npinfo = skb->dev->npinfo;
381 unsigned char *arp_ptr;
382 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
385 struct sk_buff *send_skb;
386 struct netpoll *np, *tmp;
390 if (list_empty(&npinfo->rx_np))
393 /* Before checking the packet, we do some early
394 inspection whether this is interesting at all */
395 spin_lock_irqsave(&npinfo->rx_lock, flags);
396 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
397 if (np->dev == skb->dev)
400 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
402 /* No netpoll struct is using this dev */
406 /* No arp on this interface */
407 if (skb->dev->flags & IFF_NOARP)
410 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
413 skb_reset_network_header(skb);
414 skb_reset_transport_header(skb);
417 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
418 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
419 arp->ar_pro != htons(ETH_P_IP) ||
420 arp->ar_op != htons(ARPOP_REQUEST))
423 arp_ptr = (unsigned char *)(arp+1);
424 /* save the location of the src hw addr */
426 arp_ptr += skb->dev->addr_len;
427 memcpy(&sip, arp_ptr, 4);
429 /* If we actually cared about dst hw addr,
430 it would get copied here */
431 arp_ptr += skb->dev->addr_len;
432 memcpy(&tip, arp_ptr, 4);
434 /* Should we ignore arp? */
435 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
438 size = arp_hdr_len(skb->dev);
440 spin_lock_irqsave(&npinfo->rx_lock, flags);
441 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
442 if (tip != np->local_ip)
445 send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
446 LL_RESERVED_SPACE(np->dev));
450 skb_reset_network_header(send_skb);
451 arp = (struct arphdr *) skb_put(send_skb, size);
452 send_skb->dev = skb->dev;
453 send_skb->protocol = htons(ETH_P_ARP);
455 /* Fill the device header for the ARP frame */
456 if (dev_hard_header(send_skb, skb->dev, ptype,
457 sha, np->dev->dev_addr,
458 send_skb->len) < 0) {
464 * Fill out the arp protocol part.
466 * we only support ethernet device type,
467 * which (according to RFC 1390) should
468 * always equal 1 (Ethernet).
471 arp->ar_hrd = htons(np->dev->type);
472 arp->ar_pro = htons(ETH_P_IP);
473 arp->ar_hln = np->dev->addr_len;
475 arp->ar_op = htons(type);
477 arp_ptr = (unsigned char *)(arp + 1);
478 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
479 arp_ptr += np->dev->addr_len;
480 memcpy(arp_ptr, &tip, 4);
482 memcpy(arp_ptr, sha, np->dev->addr_len);
483 arp_ptr += np->dev->addr_len;
484 memcpy(arp_ptr, &sip, 4);
486 netpoll_send_skb(np, send_skb);
488 /* If there are several rx_hooks for the same address,
489 we're fine by sending a single reply */
492 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
495 int __netpoll_rx(struct sk_buff *skb)
497 int proto, len, ulen;
501 struct netpoll_info *npinfo = skb->dev->npinfo;
502 struct netpoll *np, *tmp;
504 if (list_empty(&npinfo->rx_np))
507 if (skb->dev->type != ARPHRD_ETHER)
510 /* check if netpoll clients need ARP */
511 if (skb->protocol == htons(ETH_P_ARP) &&
512 atomic_read(&trapped)) {
513 skb_queue_tail(&npinfo->arp_tx, skb);
517 proto = ntohs(eth_hdr(skb)->h_proto);
518 if (proto != ETH_P_IP)
520 if (skb->pkt_type == PACKET_OTHERHOST)
525 iph = (struct iphdr *)skb->data;
526 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
528 if (iph->ihl < 5 || iph->version != 4)
530 if (!pskb_may_pull(skb, iph->ihl*4))
532 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
535 len = ntohs(iph->tot_len);
536 if (skb->len < len || len < iph->ihl*4)
540 * Our transport medium may have padded the buffer out.
541 * Now We trim to the true length of the frame.
543 if (pskb_trim_rcsum(skb, len))
546 if (iph->protocol != IPPROTO_UDP)
550 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
551 ulen = ntohs(uh->len);
555 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
558 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
559 if (np->local_ip && np->local_ip != iph->daddr)
561 if (np->remote_ip && np->remote_ip != iph->saddr)
563 if (np->local_port && np->local_port != ntohs(uh->dest))
566 np->rx_hook(np, ntohs(uh->source),
568 ulen - sizeof(struct udphdr));
579 if (atomic_read(&trapped)) {
587 void netpoll_print_options(struct netpoll *np)
589 printk(KERN_INFO "%s: local port %d\n",
590 np->name, np->local_port);
591 printk(KERN_INFO "%s: local IP %pI4\n",
592 np->name, &np->local_ip);
593 printk(KERN_INFO "%s: interface '%s'\n",
594 np->name, np->dev_name);
595 printk(KERN_INFO "%s: remote port %d\n",
596 np->name, np->remote_port);
597 printk(KERN_INFO "%s: remote IP %pI4\n",
598 np->name, &np->remote_ip);
599 printk(KERN_INFO "%s: remote ethernet address %pM\n",
600 np->name, np->remote_mac);
603 int netpoll_parse_options(struct netpoll *np, char *opt)
605 char *cur=opt, *delim;
608 if ((delim = strchr(cur, '@')) == NULL)
611 np->local_port = simple_strtol(cur, NULL, 10);
617 if ((delim = strchr(cur, '/')) == NULL)
620 np->local_ip = in_aton(cur);
626 /* parse out dev name */
627 if ((delim = strchr(cur, ',')) == NULL)
630 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
637 if ((delim = strchr(cur, '@')) == NULL)
640 if (*cur == ' ' || *cur == '\t')
641 printk(KERN_INFO "%s: warning: whitespace"
642 "is not allowed\n", np->name);
643 np->remote_port = simple_strtol(cur, NULL, 10);
649 if ((delim = strchr(cur, '/')) == NULL)
652 np->remote_ip = in_aton(cur);
657 if ((delim = strchr(cur, ':')) == NULL)
660 np->remote_mac[0] = simple_strtol(cur, NULL, 16);
662 if ((delim = strchr(cur, ':')) == NULL)
665 np->remote_mac[1] = simple_strtol(cur, NULL, 16);
667 if ((delim = strchr(cur, ':')) == NULL)
670 np->remote_mac[2] = simple_strtol(cur, NULL, 16);
672 if ((delim = strchr(cur, ':')) == NULL)
675 np->remote_mac[3] = simple_strtol(cur, NULL, 16);
677 if ((delim = strchr(cur, ':')) == NULL)
680 np->remote_mac[4] = simple_strtol(cur, NULL, 16);
682 np->remote_mac[5] = simple_strtol(cur, NULL, 16);
685 netpoll_print_options(np);
690 printk(KERN_INFO "%s: couldn't parse config at '%s'!\n",
695 int netpoll_setup(struct netpoll *np)
697 struct net_device *ndev = NULL;
698 struct in_device *in_dev;
699 struct netpoll_info *npinfo;
700 struct netpoll *npe, *tmp;
705 ndev = dev_get_by_name(&init_net, np->dev_name);
707 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
708 np->name, np->dev_name);
714 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
720 npinfo->rx_flags = 0;
721 INIT_LIST_HEAD(&npinfo->rx_np);
723 spin_lock_init(&npinfo->rx_lock);
724 skb_queue_head_init(&npinfo->arp_tx);
725 skb_queue_head_init(&npinfo->txq);
726 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
728 atomic_set(&npinfo->refcnt, 1);
730 npinfo = ndev->npinfo;
731 atomic_inc(&npinfo->refcnt);
734 npinfo->netpoll = np;
736 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
737 !ndev->netdev_ops->ndo_poll_controller) {
738 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
739 np->name, np->dev_name);
744 if (!netif_running(ndev)) {
745 unsigned long atmost, atleast;
747 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
748 np->name, np->dev_name);
751 err = dev_open(ndev);
755 printk(KERN_ERR "%s: failed to open %s\n",
756 np->name, ndev->name);
760 atleast = jiffies + HZ/10;
761 atmost = jiffies + carrier_timeout * HZ;
762 while (!netif_carrier_ok(ndev)) {
763 if (time_after(jiffies, atmost)) {
765 "%s: timeout waiting for carrier\n",
772 /* If carrier appears to come up instantly, we don't
773 * trust it and pause so that we don't pump all our
774 * queued console messages into the bitbucket.
777 if (time_before(jiffies, atleast)) {
778 printk(KERN_NOTICE "%s: carrier detect appears"
779 " untrustworthy, waiting 4 seconds\n",
787 in_dev = __in_dev_get_rcu(ndev);
789 if (!in_dev || !in_dev->ifa_list) {
791 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
792 np->name, np->dev_name);
797 np->local_ip = in_dev->ifa_list->ifa_local;
799 printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
803 spin_lock_irqsave(&npinfo->rx_lock, flags);
804 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
805 list_add_tail(&np->rx, &npinfo->rx_np);
806 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
809 /* fill up the skb queue */
812 /* last thing to do is link it to the net device structure */
813 ndev->npinfo = npinfo;
815 /* avoid racing with NAPI reading npinfo */
822 spin_lock_irqsave(&npinfo->rx_lock, flags);
823 list_for_each_entry_safe(npe, tmp, &npinfo->rx_np, rx) {
826 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
835 static int __init netpoll_init(void)
837 skb_queue_head_init(&skb_pool);
840 core_initcall(netpoll_init);
842 void netpoll_cleanup(struct netpoll *np)
844 struct netpoll_info *npinfo;
848 npinfo = np->dev->npinfo;
850 if (!list_empty(&npinfo->rx_np)) {
851 spin_lock_irqsave(&npinfo->rx_lock, flags);
853 if (list_empty(&npinfo->rx_np))
854 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
855 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
858 if (atomic_dec_and_test(&npinfo->refcnt)) {
859 const struct net_device_ops *ops;
860 skb_queue_purge(&npinfo->arp_tx);
861 skb_queue_purge(&npinfo->txq);
862 cancel_rearming_delayed_work(&npinfo->tx_work);
864 /* clean after last, unfinished work */
865 __skb_queue_purge(&npinfo->txq);
867 ops = np->dev->netdev_ops;
868 if (ops->ndo_netpoll_cleanup)
869 ops->ndo_netpoll_cleanup(np->dev);
871 np->dev->npinfo = NULL;
881 int netpoll_trap(void)
883 return atomic_read(&trapped);
886 void netpoll_set_trap(int trap)
889 atomic_inc(&trapped);
891 atomic_dec(&trapped);
894 EXPORT_SYMBOL(netpoll_send_skb);
895 EXPORT_SYMBOL(netpoll_set_trap);
896 EXPORT_SYMBOL(netpoll_trap);
897 EXPORT_SYMBOL(netpoll_print_options);
898 EXPORT_SYMBOL(netpoll_parse_options);
899 EXPORT_SYMBOL(netpoll_setup);
900 EXPORT_SYMBOL(netpoll_cleanup);
901 EXPORT_SYMBOL(netpoll_send_udp);
902 EXPORT_SYMBOL(netpoll_poll_dev);
903 EXPORT_SYMBOL(netpoll_poll);