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)
38 static struct sk_buff_head skb_pool
;
40 static atomic_t trapped
;
42 #define USEC_PER_POLL 50
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);
76 netif_tx_unlock_bh(dev
);
80 static int checksum_udp(struct sk_buff
*skb
, struct udphdr
*uh
,
81 unsigned short ulen
, __be32 saddr
, __be32 daddr
)
85 if (uh
->check
== 0 || skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
88 psum
= csum_tcpudp_nofold(saddr
, daddr
, ulen
, IPPROTO_UDP
, 0);
90 if (skb
->ip_summed
== CHECKSUM_COMPLETE
&&
91 !csum_fold(csum_add(psum
, skb
->csum
)))
96 return __skb_checksum_complete(skb
);
100 * Check whether delayed processing was scheduled for our NIC. If so,
101 * we attempt to grab the poll lock and use ->poll() to pump the card.
102 * If this fails, either we've recursed in ->poll() or it's already
103 * running on another CPU.
105 * Note: we don't mask interrupts with this lock because we're using
106 * trylock here and interrupts are already disabled in the softirq
107 * case. Further, we test the poll_owner to avoid recursion on UP
108 * systems where the lock doesn't exist.
110 * In cases where there is bi-directional communications, reading only
111 * one message at a time can lead to packets being dropped by the
112 * network adapter, forcing superfluous retries and possibly timeouts.
113 * Thus, we set our budget to greater than 1.
115 static void poll_napi(struct netpoll
*np
)
117 struct netpoll_info
*npinfo
= np
->dev
->npinfo
;
120 if (test_bit(__LINK_STATE_RX_SCHED
, &np
->dev
->state
) &&
121 npinfo
->poll_owner
!= smp_processor_id() &&
122 spin_trylock(&npinfo
->poll_lock
)) {
123 npinfo
->rx_flags
|= NETPOLL_RX_DROP
;
124 atomic_inc(&trapped
);
126 np
->dev
->poll(np
->dev
, &budget
);
128 atomic_dec(&trapped
);
129 npinfo
->rx_flags
&= ~NETPOLL_RX_DROP
;
130 spin_unlock(&npinfo
->poll_lock
);
134 static void service_arp_queue(struct netpoll_info
*npi
)
141 skb
= skb_dequeue(&npi
->arp_tx
);
143 while (skb
!= NULL
) {
145 skb
= skb_dequeue(&npi
->arp_tx
);
149 void netpoll_poll(struct netpoll
*np
)
151 if (!np
->dev
|| !netif_running(np
->dev
) || !np
->dev
->poll_controller
)
154 /* Process pending work on NIC */
155 np
->dev
->poll_controller(np
->dev
);
159 service_arp_queue(np
->dev
->npinfo
);
161 zap_completion_queue();
164 static void refill_skbs(void)
169 spin_lock_irqsave(&skb_pool
.lock
, flags
);
170 while (skb_pool
.qlen
< MAX_SKBS
) {
171 skb
= alloc_skb(MAX_SKB_SIZE
, GFP_ATOMIC
);
175 __skb_queue_tail(&skb_pool
, skb
);
177 spin_unlock_irqrestore(&skb_pool
.lock
, flags
);
180 static void zap_completion_queue(void)
183 struct softnet_data
*sd
= &get_cpu_var(softnet_data
);
185 if (sd
->completion_queue
) {
186 struct sk_buff
*clist
;
188 local_irq_save(flags
);
189 clist
= sd
->completion_queue
;
190 sd
->completion_queue
= NULL
;
191 local_irq_restore(flags
);
193 while (clist
!= NULL
) {
194 struct sk_buff
*skb
= clist
;
197 dev_kfree_skb_any(skb
); /* put this one back */
203 put_cpu_var(softnet_data
);
206 static struct sk_buff
*find_skb(struct netpoll
*np
, int len
, int reserve
)
211 zap_completion_queue();
215 skb
= alloc_skb(len
, GFP_ATOMIC
);
217 skb
= skb_dequeue(&skb_pool
);
227 atomic_set(&skb
->users
, 1);
228 skb_reserve(skb
, reserve
);
232 static void netpoll_send_skb(struct netpoll
*np
, struct sk_buff
*skb
)
234 int status
= NETDEV_TX_BUSY
;
236 struct net_device
*dev
= np
->dev
;
237 struct netpoll_info
*npinfo
= np
->dev
->npinfo
;
239 if (!npinfo
|| !netif_running(dev
) || !netif_device_present(dev
)) {
244 /* don't get messages out of order, and no recursion */
245 if (skb_queue_len(&npinfo
->txq
) == 0 &&
246 npinfo
->poll_owner
!= smp_processor_id() &&
247 netif_tx_trylock(dev
)) {
248 /* try until next clock tick */
249 for (tries
= jiffies_to_usecs(1)/USEC_PER_POLL
; tries
> 0; --tries
) {
250 if (!netif_queue_stopped(dev
))
251 status
= dev
->hard_start_xmit(skb
, dev
);
253 if (status
== NETDEV_TX_OK
)
256 /* tickle device maybe there is some cleanup */
259 udelay(USEC_PER_POLL
);
261 netif_tx_unlock(dev
);
264 if (status
!= NETDEV_TX_OK
) {
265 skb_queue_tail(&npinfo
->txq
, skb
);
266 schedule_work(&npinfo
->tx_work
);
270 void netpoll_send_udp(struct netpoll
*np
, const char *msg
, int len
)
272 int total_len
, eth_len
, ip_len
, udp_len
;
278 udp_len
= len
+ sizeof(*udph
);
279 ip_len
= eth_len
= udp_len
+ sizeof(*iph
);
280 total_len
= eth_len
+ ETH_HLEN
+ NET_IP_ALIGN
;
282 skb
= find_skb(np
, total_len
, total_len
- len
);
286 memcpy(skb
->data
, msg
, len
);
289 skb
->h
.uh
= udph
= (struct udphdr
*) skb_push(skb
, sizeof(*udph
));
290 udph
->source
= htons(np
->local_port
);
291 udph
->dest
= htons(np
->remote_port
);
292 udph
->len
= htons(udp_len
);
294 udph
->check
= csum_tcpudp_magic(htonl(np
->local_ip
),
295 htonl(np
->remote_ip
),
296 udp_len
, IPPROTO_UDP
,
297 csum_partial((unsigned char *)udph
, udp_len
, 0));
298 if (udph
->check
== 0)
301 skb
->nh
.iph
= iph
= (struct iphdr
*)skb_push(skb
, sizeof(*iph
));
303 /* iph->version = 4; iph->ihl = 5; */
304 put_unaligned(0x45, (unsigned char *)iph
);
306 put_unaligned(htons(ip_len
), &(iph
->tot_len
));
310 iph
->protocol
= IPPROTO_UDP
;
312 put_unaligned(htonl(np
->local_ip
), &(iph
->saddr
));
313 put_unaligned(htonl(np
->remote_ip
), &(iph
->daddr
));
314 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
316 eth
= (struct ethhdr
*) skb_push(skb
, ETH_HLEN
);
317 skb
->mac
.raw
= skb
->data
;
318 skb
->protocol
= eth
->h_proto
= htons(ETH_P_IP
);
319 memcpy(eth
->h_source
, np
->local_mac
, 6);
320 memcpy(eth
->h_dest
, np
->remote_mac
, 6);
324 netpoll_send_skb(np
, skb
);
327 static void arp_reply(struct sk_buff
*skb
)
329 struct netpoll_info
*npinfo
= skb
->dev
->npinfo
;
331 unsigned char *arp_ptr
;
332 int size
, type
= ARPOP_REPLY
, ptype
= ETH_P_ARP
;
334 struct sk_buff
*send_skb
;
335 struct netpoll
*np
= NULL
;
337 if (npinfo
->rx_np
&& npinfo
->rx_np
->dev
== skb
->dev
)
342 /* No arp on this interface */
343 if (skb
->dev
->flags
& IFF_NOARP
)
346 if (!pskb_may_pull(skb
, (sizeof(struct arphdr
) +
347 (2 * skb
->dev
->addr_len
) +
351 skb
->h
.raw
= skb
->nh
.raw
= skb
->data
;
354 if ((arp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
355 arp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
356 arp
->ar_pro
!= htons(ETH_P_IP
) ||
357 arp
->ar_op
!= htons(ARPOP_REQUEST
))
360 arp_ptr
= (unsigned char *)(arp
+1) + skb
->dev
->addr_len
;
361 memcpy(&sip
, arp_ptr
, 4);
362 arp_ptr
+= 4 + skb
->dev
->addr_len
;
363 memcpy(&tip
, arp_ptr
, 4);
365 /* Should we ignore arp? */
366 if (tip
!= htonl(np
->local_ip
) || LOOPBACK(tip
) || MULTICAST(tip
))
369 size
= sizeof(struct arphdr
) + 2 * (skb
->dev
->addr_len
+ 4);
370 send_skb
= find_skb(np
, size
+ LL_RESERVED_SPACE(np
->dev
),
371 LL_RESERVED_SPACE(np
->dev
));
376 send_skb
->nh
.raw
= send_skb
->data
;
377 arp
= (struct arphdr
*) skb_put(send_skb
, size
);
378 send_skb
->dev
= skb
->dev
;
379 send_skb
->protocol
= htons(ETH_P_ARP
);
381 /* Fill the device header for the ARP frame */
383 if (np
->dev
->hard_header
&&
384 np
->dev
->hard_header(send_skb
, skb
->dev
, ptype
,
385 np
->remote_mac
, np
->local_mac
,
386 send_skb
->len
) < 0) {
392 * Fill out the arp protocol part.
394 * we only support ethernet device type,
395 * which (according to RFC 1390) should always equal 1 (Ethernet).
398 arp
->ar_hrd
= htons(np
->dev
->type
);
399 arp
->ar_pro
= htons(ETH_P_IP
);
400 arp
->ar_hln
= np
->dev
->addr_len
;
402 arp
->ar_op
= htons(type
);
404 arp_ptr
=(unsigned char *)(arp
+ 1);
405 memcpy(arp_ptr
, np
->dev
->dev_addr
, np
->dev
->addr_len
);
406 arp_ptr
+= np
->dev
->addr_len
;
407 memcpy(arp_ptr
, &tip
, 4);
409 memcpy(arp_ptr
, np
->remote_mac
, np
->dev
->addr_len
);
410 arp_ptr
+= np
->dev
->addr_len
;
411 memcpy(arp_ptr
, &sip
, 4);
413 netpoll_send_skb(np
, send_skb
);
416 int __netpoll_rx(struct sk_buff
*skb
)
418 int proto
, len
, ulen
;
421 struct netpoll_info
*npi
= skb
->dev
->npinfo
;
422 struct netpoll
*np
= npi
->rx_np
;
426 if (skb
->dev
->type
!= ARPHRD_ETHER
)
429 /* check if netpoll clients need ARP */
430 if (skb
->protocol
== __constant_htons(ETH_P_ARP
) &&
431 atomic_read(&trapped
)) {
432 skb_queue_tail(&npi
->arp_tx
, skb
);
436 proto
= ntohs(eth_hdr(skb
)->h_proto
);
437 if (proto
!= ETH_P_IP
)
439 if (skb
->pkt_type
== PACKET_OTHERHOST
)
444 iph
= (struct iphdr
*)skb
->data
;
445 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
447 if (iph
->ihl
< 5 || iph
->version
!= 4)
449 if (!pskb_may_pull(skb
, iph
->ihl
*4))
451 if (ip_fast_csum((u8
*)iph
, iph
->ihl
) != 0)
454 len
= ntohs(iph
->tot_len
);
455 if (skb
->len
< len
|| len
< iph
->ihl
*4)
458 if (iph
->protocol
!= IPPROTO_UDP
)
462 uh
= (struct udphdr
*)(((char *)iph
) + iph
->ihl
*4);
463 ulen
= ntohs(uh
->len
);
467 if (checksum_udp(skb
, uh
, ulen
, iph
->saddr
, iph
->daddr
))
469 if (np
->local_ip
&& np
->local_ip
!= ntohl(iph
->daddr
))
471 if (np
->remote_ip
&& np
->remote_ip
!= ntohl(iph
->saddr
))
473 if (np
->local_port
&& np
->local_port
!= ntohs(uh
->dest
))
476 np
->rx_hook(np
, ntohs(uh
->source
),
478 ulen
- sizeof(struct udphdr
));
484 if (atomic_read(&trapped
)) {
492 int netpoll_parse_options(struct netpoll
*np
, char *opt
)
494 char *cur
=opt
, *delim
;
497 if ((delim
= strchr(cur
, '@')) == NULL
)
500 np
->local_port
= simple_strtol(cur
, NULL
, 10);
504 printk(KERN_INFO
"%s: local port %d\n", np
->name
, np
->local_port
);
507 if ((delim
= strchr(cur
, '/')) == NULL
)
510 np
->local_ip
= ntohl(in_aton(cur
));
513 printk(KERN_INFO
"%s: local IP %d.%d.%d.%d\n",
514 np
->name
, HIPQUAD(np
->local_ip
));
519 /* parse out dev name */
520 if ((delim
= strchr(cur
, ',')) == NULL
)
523 strlcpy(np
->dev_name
, cur
, sizeof(np
->dev_name
));
528 printk(KERN_INFO
"%s: interface %s\n", np
->name
, np
->dev_name
);
532 if ((delim
= strchr(cur
, '@')) == NULL
)
535 np
->remote_port
= simple_strtol(cur
, NULL
, 10);
539 printk(KERN_INFO
"%s: remote port %d\n", np
->name
, np
->remote_port
);
542 if ((delim
= strchr(cur
, '/')) == NULL
)
545 np
->remote_ip
= ntohl(in_aton(cur
));
548 printk(KERN_INFO
"%s: remote IP %d.%d.%d.%d\n",
549 np
->name
, HIPQUAD(np
->remote_ip
));
553 if ((delim
= strchr(cur
, ':')) == NULL
)
556 np
->remote_mac
[0] = simple_strtol(cur
, NULL
, 16);
558 if ((delim
= strchr(cur
, ':')) == NULL
)
561 np
->remote_mac
[1] = simple_strtol(cur
, NULL
, 16);
563 if ((delim
= strchr(cur
, ':')) == NULL
)
566 np
->remote_mac
[2] = simple_strtol(cur
, NULL
, 16);
568 if ((delim
= strchr(cur
, ':')) == NULL
)
571 np
->remote_mac
[3] = simple_strtol(cur
, NULL
, 16);
573 if ((delim
= strchr(cur
, ':')) == NULL
)
576 np
->remote_mac
[4] = simple_strtol(cur
, NULL
, 16);
578 np
->remote_mac
[5] = simple_strtol(cur
, NULL
, 16);
581 printk(KERN_INFO
"%s: remote ethernet address "
582 "%02x:%02x:%02x:%02x:%02x:%02x\n",
594 printk(KERN_INFO
"%s: couldn't parse config at %s!\n",
599 int netpoll_setup(struct netpoll
*np
)
601 struct net_device
*ndev
= NULL
;
602 struct in_device
*in_dev
;
603 struct netpoll_info
*npinfo
;
608 ndev
= dev_get_by_name(np
->dev_name
);
610 printk(KERN_ERR
"%s: %s doesn't exist, aborting.\n",
611 np
->name
, np
->dev_name
);
617 npinfo
= kmalloc(sizeof(*npinfo
), GFP_KERNEL
);
623 npinfo
->rx_flags
= 0;
624 npinfo
->rx_np
= NULL
;
625 spin_lock_init(&npinfo
->poll_lock
);
626 npinfo
->poll_owner
= -1;
628 spin_lock_init(&npinfo
->rx_lock
);
629 skb_queue_head_init(&npinfo
->arp_tx
);
630 skb_queue_head_init(&npinfo
->txq
);
631 INIT_WORK(&npinfo
->tx_work
, queue_process
, npinfo
);
633 atomic_set(&npinfo
->refcnt
, 1);
635 npinfo
= ndev
->npinfo
;
636 atomic_inc(&npinfo
->refcnt
);
639 if (!ndev
->poll_controller
) {
640 printk(KERN_ERR
"%s: %s doesn't support polling, aborting.\n",
641 np
->name
, np
->dev_name
);
646 if (!netif_running(ndev
)) {
647 unsigned long atmost
, atleast
;
649 printk(KERN_INFO
"%s: device %s not up yet, forcing it\n",
650 np
->name
, np
->dev_name
);
653 err
= dev_open(ndev
);
657 printk(KERN_ERR
"%s: failed to open %s\n",
658 np
->name
, ndev
->name
);
662 atleast
= jiffies
+ HZ
/10;
663 atmost
= jiffies
+ 4*HZ
;
664 while (!netif_carrier_ok(ndev
)) {
665 if (time_after(jiffies
, atmost
)) {
667 "%s: timeout waiting for carrier\n",
674 /* If carrier appears to come up instantly, we don't
675 * trust it and pause so that we don't pump all our
676 * queued console messages into the bitbucket.
679 if (time_before(jiffies
, atleast
)) {
680 printk(KERN_NOTICE
"%s: carrier detect appears"
681 " untrustworthy, waiting 4 seconds\n",
687 if (is_zero_ether_addr(np
->local_mac
) && ndev
->dev_addr
)
688 memcpy(np
->local_mac
, ndev
->dev_addr
, 6);
692 in_dev
= __in_dev_get_rcu(ndev
);
694 if (!in_dev
|| !in_dev
->ifa_list
) {
696 printk(KERN_ERR
"%s: no IP address for %s, aborting\n",
697 np
->name
, np
->dev_name
);
702 np
->local_ip
= ntohl(in_dev
->ifa_list
->ifa_local
);
704 printk(KERN_INFO
"%s: local IP %d.%d.%d.%d\n",
705 np
->name
, HIPQUAD(np
->local_ip
));
709 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
710 npinfo
->rx_flags
|= NETPOLL_RX_ENABLED
;
712 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
715 /* fill up the skb queue */
718 /* last thing to do is link it to the net device structure */
719 ndev
->npinfo
= npinfo
;
721 /* avoid racing with NAPI reading npinfo */
734 static int __init
netpoll_init(void)
736 skb_queue_head_init(&skb_pool
);
739 core_initcall(netpoll_init
);
741 void netpoll_cleanup(struct netpoll
*np
)
743 struct netpoll_info
*npinfo
;
747 npinfo
= np
->dev
->npinfo
;
749 if (npinfo
->rx_np
== np
) {
750 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
751 npinfo
->rx_np
= NULL
;
752 npinfo
->rx_flags
&= ~NETPOLL_RX_ENABLED
;
753 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
756 np
->dev
->npinfo
= NULL
;
757 if (atomic_dec_and_test(&npinfo
->refcnt
)) {
758 skb_queue_purge(&npinfo
->arp_tx
);
759 skb_queue_purge(&npinfo
->txq
);
760 cancel_rearming_delayed_work(&npinfo
->tx_work
);
761 flush_scheduled_work();
773 int netpoll_trap(void)
775 return atomic_read(&trapped
);
778 void netpoll_set_trap(int trap
)
781 atomic_inc(&trapped
);
783 atomic_dec(&trapped
);
786 EXPORT_SYMBOL(netpoll_set_trap
);
787 EXPORT_SYMBOL(netpoll_trap
);
788 EXPORT_SYMBOL(netpoll_parse_options
);
789 EXPORT_SYMBOL(netpoll_setup
);
790 EXPORT_SYMBOL(netpoll_cleanup
);
791 EXPORT_SYMBOL(netpoll_send_udp
);
792 EXPORT_SYMBOL(netpoll_poll
);