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
)))
63 void netpoll_queue(struct sk_buff
*skb
)
65 struct net_device
*dev
= skb
->dev
;
66 struct netpoll_info
*npinfo
= dev
->npinfo
;
71 skb_queue_tail(&npinfo
->txq
, skb
);
72 schedule_work(&npinfo
->tx_work
);
76 static int checksum_udp(struct sk_buff
*skb
, struct udphdr
*uh
,
77 unsigned short ulen
, u32 saddr
, u32 daddr
)
81 if (uh
->check
== 0 || skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
84 psum
= csum_tcpudp_nofold(saddr
, daddr
, ulen
, IPPROTO_UDP
, 0);
86 if (skb
->ip_summed
== CHECKSUM_COMPLETE
&&
87 !(u16
)csum_fold(csum_add(psum
, skb
->csum
)))
92 return __skb_checksum_complete(skb
);
96 * Check whether delayed processing was scheduled for our NIC. If so,
97 * we attempt to grab the poll lock and use ->poll() to pump the card.
98 * If this fails, either we've recursed in ->poll() or it's already
99 * running on another CPU.
101 * Note: we don't mask interrupts with this lock because we're using
102 * trylock here and interrupts are already disabled in the softirq
103 * case. Further, we test the poll_owner to avoid recursion on UP
104 * systems where the lock doesn't exist.
106 * In cases where there is bi-directional communications, reading only
107 * one message at a time can lead to packets being dropped by the
108 * network adapter, forcing superfluous retries and possibly timeouts.
109 * Thus, we set our budget to greater than 1.
111 static void poll_napi(struct netpoll
*np
)
113 struct netpoll_info
*npinfo
= np
->dev
->npinfo
;
116 if (test_bit(__LINK_STATE_RX_SCHED
, &np
->dev
->state
) &&
117 npinfo
->poll_owner
!= smp_processor_id() &&
118 spin_trylock(&npinfo
->poll_lock
)) {
119 npinfo
->rx_flags
|= NETPOLL_RX_DROP
;
120 atomic_inc(&trapped
);
122 np
->dev
->poll(np
->dev
, &budget
);
124 atomic_dec(&trapped
);
125 npinfo
->rx_flags
&= ~NETPOLL_RX_DROP
;
126 spin_unlock(&npinfo
->poll_lock
);
130 static void service_arp_queue(struct netpoll_info
*npi
)
137 skb
= skb_dequeue(&npi
->arp_tx
);
139 while (skb
!= NULL
) {
141 skb
= skb_dequeue(&npi
->arp_tx
);
146 void netpoll_poll(struct netpoll
*np
)
148 if(!np
->dev
|| !netif_running(np
->dev
) || !np
->dev
->poll_controller
)
151 /* Process pending work on NIC */
152 np
->dev
->poll_controller(np
->dev
);
156 service_arp_queue(np
->dev
->npinfo
);
158 zap_completion_queue();
161 static void refill_skbs(void)
166 spin_lock_irqsave(&skb_pool
.lock
, flags
);
167 while (skb_pool
.qlen
< MAX_SKBS
) {
168 skb
= alloc_skb(MAX_SKB_SIZE
, GFP_ATOMIC
);
172 __skb_queue_tail(&skb_pool
, skb
);
174 spin_unlock_irqrestore(&skb_pool
.lock
, flags
);
177 static void zap_completion_queue(void)
180 struct softnet_data
*sd
= &get_cpu_var(softnet_data
);
182 if (sd
->completion_queue
) {
183 struct sk_buff
*clist
;
185 local_irq_save(flags
);
186 clist
= sd
->completion_queue
;
187 sd
->completion_queue
= NULL
;
188 local_irq_restore(flags
);
190 while (clist
!= NULL
) {
191 struct sk_buff
*skb
= clist
;
194 dev_kfree_skb_any(skb
); /* put this one back */
200 put_cpu_var(softnet_data
);
203 static struct sk_buff
*find_skb(struct netpoll
*np
, int len
, int reserve
)
208 zap_completion_queue();
212 skb
= alloc_skb(len
, GFP_ATOMIC
);
214 skb
= skb_dequeue(&skb_pool
);
224 atomic_set(&skb
->users
, 1);
225 skb_reserve(skb
, reserve
);
229 static void netpoll_send_skb(struct netpoll
*np
, struct sk_buff
*skb
)
232 struct netpoll_info
*npinfo
;
234 if (!np
|| !np
->dev
|| !netif_running(np
->dev
)) {
239 npinfo
= np
->dev
->npinfo
;
241 /* avoid recursion */
242 if (npinfo
->poll_owner
== smp_processor_id() ||
243 np
->dev
->xmit_lock_owner
== smp_processor_id()) {
253 netif_tx_lock(np
->dev
);
256 * network drivers do not expect to be called if the queue is
259 status
= NETDEV_TX_BUSY
;
260 if (!netif_queue_stopped(np
->dev
))
261 status
= np
->dev
->hard_start_xmit(skb
, np
->dev
);
263 netif_tx_unlock(np
->dev
);
267 npinfo
->tries
= MAX_RETRIES
; /* reset */
274 } while (npinfo
->tries
> 0);
277 void netpoll_send_udp(struct netpoll
*np
, const char *msg
, int len
)
279 int total_len
, eth_len
, ip_len
, udp_len
;
285 udp_len
= len
+ sizeof(*udph
);
286 ip_len
= eth_len
= udp_len
+ sizeof(*iph
);
287 total_len
= eth_len
+ ETH_HLEN
+ NET_IP_ALIGN
;
289 skb
= find_skb(np
, total_len
, total_len
- len
);
293 memcpy(skb
->data
, msg
, len
);
296 skb
->h
.uh
= udph
= (struct udphdr
*) skb_push(skb
, sizeof(*udph
));
297 udph
->source
= htons(np
->local_port
);
298 udph
->dest
= htons(np
->remote_port
);
299 udph
->len
= htons(udp_len
);
301 udph
->check
= csum_tcpudp_magic(htonl(np
->local_ip
),
302 htonl(np
->remote_ip
),
303 udp_len
, IPPROTO_UDP
,
304 csum_partial((unsigned char *)udph
, udp_len
, 0));
305 if (udph
->check
== 0)
308 skb
->nh
.iph
= iph
= (struct iphdr
*)skb_push(skb
, sizeof(*iph
));
310 /* iph->version = 4; iph->ihl = 5; */
311 put_unaligned(0x45, (unsigned char *)iph
);
313 put_unaligned(htons(ip_len
), &(iph
->tot_len
));
317 iph
->protocol
= IPPROTO_UDP
;
319 put_unaligned(htonl(np
->local_ip
), &(iph
->saddr
));
320 put_unaligned(htonl(np
->remote_ip
), &(iph
->daddr
));
321 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
323 eth
= (struct ethhdr
*) skb_push(skb
, ETH_HLEN
);
324 skb
->mac
.raw
= skb
->data
;
325 skb
->protocol
= eth
->h_proto
= htons(ETH_P_IP
);
326 memcpy(eth
->h_source
, np
->local_mac
, 6);
327 memcpy(eth
->h_dest
, np
->remote_mac
, 6);
331 netpoll_send_skb(np
, skb
);
334 static void arp_reply(struct sk_buff
*skb
)
336 struct netpoll_info
*npinfo
= skb
->dev
->npinfo
;
338 unsigned char *arp_ptr
;
339 int size
, type
= ARPOP_REPLY
, ptype
= ETH_P_ARP
;
341 struct sk_buff
*send_skb
;
342 struct netpoll
*np
= NULL
;
344 if (npinfo
->rx_np
&& npinfo
->rx_np
->dev
== skb
->dev
)
349 /* No arp on this interface */
350 if (skb
->dev
->flags
& IFF_NOARP
)
353 if (!pskb_may_pull(skb
, (sizeof(struct arphdr
) +
354 (2 * skb
->dev
->addr_len
) +
358 skb
->h
.raw
= skb
->nh
.raw
= skb
->data
;
361 if ((arp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
362 arp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
363 arp
->ar_pro
!= htons(ETH_P_IP
) ||
364 arp
->ar_op
!= htons(ARPOP_REQUEST
))
367 arp_ptr
= (unsigned char *)(arp
+1) + skb
->dev
->addr_len
;
368 memcpy(&sip
, arp_ptr
, 4);
369 arp_ptr
+= 4 + skb
->dev
->addr_len
;
370 memcpy(&tip
, arp_ptr
, 4);
372 /* Should we ignore arp? */
373 if (tip
!= htonl(np
->local_ip
) || LOOPBACK(tip
) || MULTICAST(tip
))
376 size
= sizeof(struct arphdr
) + 2 * (skb
->dev
->addr_len
+ 4);
377 send_skb
= find_skb(np
, size
+ LL_RESERVED_SPACE(np
->dev
),
378 LL_RESERVED_SPACE(np
->dev
));
383 send_skb
->nh
.raw
= send_skb
->data
;
384 arp
= (struct arphdr
*) skb_put(send_skb
, size
);
385 send_skb
->dev
= skb
->dev
;
386 send_skb
->protocol
= htons(ETH_P_ARP
);
388 /* Fill the device header for the ARP frame */
390 if (np
->dev
->hard_header
&&
391 np
->dev
->hard_header(send_skb
, skb
->dev
, ptype
,
392 np
->remote_mac
, np
->local_mac
,
393 send_skb
->len
) < 0) {
399 * Fill out the arp protocol part.
401 * we only support ethernet device type,
402 * which (according to RFC 1390) should always equal 1 (Ethernet).
405 arp
->ar_hrd
= htons(np
->dev
->type
);
406 arp
->ar_pro
= htons(ETH_P_IP
);
407 arp
->ar_hln
= np
->dev
->addr_len
;
409 arp
->ar_op
= htons(type
);
411 arp_ptr
=(unsigned char *)(arp
+ 1);
412 memcpy(arp_ptr
, np
->dev
->dev_addr
, np
->dev
->addr_len
);
413 arp_ptr
+= np
->dev
->addr_len
;
414 memcpy(arp_ptr
, &tip
, 4);
416 memcpy(arp_ptr
, np
->remote_mac
, np
->dev
->addr_len
);
417 arp_ptr
+= np
->dev
->addr_len
;
418 memcpy(arp_ptr
, &sip
, 4);
420 netpoll_send_skb(np
, send_skb
);
423 int __netpoll_rx(struct sk_buff
*skb
)
425 int proto
, len
, ulen
;
428 struct netpoll_info
*npi
= skb
->dev
->npinfo
;
429 struct netpoll
*np
= npi
->rx_np
;
434 if (skb
->dev
->type
!= ARPHRD_ETHER
)
437 /* check if netpoll clients need ARP */
438 if (skb
->protocol
== __constant_htons(ETH_P_ARP
) &&
439 atomic_read(&trapped
)) {
440 skb_queue_tail(&npi
->arp_tx
, skb
);
444 proto
= ntohs(eth_hdr(skb
)->h_proto
);
445 if (proto
!= ETH_P_IP
)
447 if (skb
->pkt_type
== PACKET_OTHERHOST
)
452 iph
= (struct iphdr
*)skb
->data
;
453 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
455 if (iph
->ihl
< 5 || iph
->version
!= 4)
457 if (!pskb_may_pull(skb
, iph
->ihl
*4))
459 if (ip_fast_csum((u8
*)iph
, iph
->ihl
) != 0)
462 len
= ntohs(iph
->tot_len
);
463 if (skb
->len
< len
|| len
< iph
->ihl
*4)
466 if (iph
->protocol
!= IPPROTO_UDP
)
470 uh
= (struct udphdr
*)(((char *)iph
) + iph
->ihl
*4);
471 ulen
= ntohs(uh
->len
);
475 if (checksum_udp(skb
, uh
, ulen
, iph
->saddr
, iph
->daddr
))
477 if (np
->local_ip
&& np
->local_ip
!= ntohl(iph
->daddr
))
479 if (np
->remote_ip
&& np
->remote_ip
!= ntohl(iph
->saddr
))
481 if (np
->local_port
&& np
->local_port
!= ntohs(uh
->dest
))
484 np
->rx_hook(np
, ntohs(uh
->source
),
486 ulen
- sizeof(struct udphdr
));
492 if (atomic_read(&trapped
)) {
500 int netpoll_parse_options(struct netpoll
*np
, char *opt
)
502 char *cur
=opt
, *delim
;
505 if ((delim
= strchr(cur
, '@')) == NULL
)
508 np
->local_port
=simple_strtol(cur
, NULL
, 10);
512 printk(KERN_INFO
"%s: local port %d\n", np
->name
, np
->local_port
);
515 if ((delim
= strchr(cur
, '/')) == NULL
)
518 np
->local_ip
=ntohl(in_aton(cur
));
521 printk(KERN_INFO
"%s: local IP %d.%d.%d.%d\n",
522 np
->name
, HIPQUAD(np
->local_ip
));
527 /* parse out dev name */
528 if ((delim
= strchr(cur
, ',')) == NULL
)
531 strlcpy(np
->dev_name
, cur
, sizeof(np
->dev_name
));
536 printk(KERN_INFO
"%s: interface %s\n", np
->name
, np
->dev_name
);
540 if ((delim
= strchr(cur
, '@')) == NULL
)
543 np
->remote_port
=simple_strtol(cur
, NULL
, 10);
547 printk(KERN_INFO
"%s: remote port %d\n", np
->name
, np
->remote_port
);
550 if ((delim
= strchr(cur
, '/')) == NULL
)
553 np
->remote_ip
=ntohl(in_aton(cur
));
556 printk(KERN_INFO
"%s: remote IP %d.%d.%d.%d\n",
557 np
->name
, HIPQUAD(np
->remote_ip
));
562 if ((delim
= strchr(cur
, ':')) == NULL
)
565 np
->remote_mac
[0]=simple_strtol(cur
, NULL
, 16);
567 if ((delim
= strchr(cur
, ':')) == NULL
)
570 np
->remote_mac
[1]=simple_strtol(cur
, NULL
, 16);
572 if ((delim
= strchr(cur
, ':')) == NULL
)
575 np
->remote_mac
[2]=simple_strtol(cur
, NULL
, 16);
577 if ((delim
= strchr(cur
, ':')) == NULL
)
580 np
->remote_mac
[3]=simple_strtol(cur
, NULL
, 16);
582 if ((delim
= strchr(cur
, ':')) == NULL
)
585 np
->remote_mac
[4]=simple_strtol(cur
, NULL
, 16);
587 np
->remote_mac
[5]=simple_strtol(cur
, NULL
, 16);
590 printk(KERN_INFO
"%s: remote ethernet address "
591 "%02x:%02x:%02x:%02x:%02x:%02x\n",
603 printk(KERN_INFO
"%s: couldn't parse config at %s!\n",
608 int netpoll_setup(struct netpoll
*np
)
610 struct net_device
*ndev
= NULL
;
611 struct in_device
*in_dev
;
612 struct netpoll_info
*npinfo
;
617 ndev
= dev_get_by_name(np
->dev_name
);
619 printk(KERN_ERR
"%s: %s doesn't exist, aborting.\n",
620 np
->name
, np
->dev_name
);
626 npinfo
= kmalloc(sizeof(*npinfo
), GFP_KERNEL
);
632 npinfo
->rx_flags
= 0;
633 npinfo
->rx_np
= NULL
;
634 spin_lock_init(&npinfo
->poll_lock
);
635 npinfo
->poll_owner
= -1;
636 npinfo
->tries
= MAX_RETRIES
;
637 spin_lock_init(&npinfo
->rx_lock
);
638 skb_queue_head_init(&npinfo
->arp_tx
);
639 skb_queue_head_init(&npinfo
->txq
);
640 INIT_WORK(&npinfo
->tx_work
, queue_process
, npinfo
);
642 atomic_set(&npinfo
->refcnt
, 1);
644 npinfo
= ndev
->npinfo
;
645 atomic_inc(&npinfo
->refcnt
);
648 if (!ndev
->poll_controller
) {
649 printk(KERN_ERR
"%s: %s doesn't support polling, aborting.\n",
650 np
->name
, np
->dev_name
);
655 if (!netif_running(ndev
)) {
656 unsigned long atmost
, atleast
;
658 printk(KERN_INFO
"%s: device %s not up yet, forcing it\n",
659 np
->name
, np
->dev_name
);
662 err
= dev_open(ndev
);
666 printk(KERN_ERR
"%s: failed to open %s\n",
667 np
->name
, ndev
->name
);
671 atleast
= jiffies
+ HZ
/10;
672 atmost
= jiffies
+ 4*HZ
;
673 while (!netif_carrier_ok(ndev
)) {
674 if (time_after(jiffies
, atmost
)) {
676 "%s: timeout waiting for carrier\n",
683 /* If carrier appears to come up instantly, we don't
684 * trust it and pause so that we don't pump all our
685 * queued console messages into the bitbucket.
688 if (time_before(jiffies
, atleast
)) {
689 printk(KERN_NOTICE
"%s: carrier detect appears"
690 " untrustworthy, waiting 4 seconds\n",
696 if (is_zero_ether_addr(np
->local_mac
) && ndev
->dev_addr
)
697 memcpy(np
->local_mac
, ndev
->dev_addr
, 6);
701 in_dev
= __in_dev_get_rcu(ndev
);
703 if (!in_dev
|| !in_dev
->ifa_list
) {
705 printk(KERN_ERR
"%s: no IP address for %s, aborting\n",
706 np
->name
, np
->dev_name
);
711 np
->local_ip
= ntohl(in_dev
->ifa_list
->ifa_local
);
713 printk(KERN_INFO
"%s: local IP %d.%d.%d.%d\n",
714 np
->name
, HIPQUAD(np
->local_ip
));
718 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
719 npinfo
->rx_flags
|= NETPOLL_RX_ENABLED
;
721 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
724 /* fill up the skb queue */
727 /* last thing to do is link it to the net device structure */
728 ndev
->npinfo
= npinfo
;
730 /* avoid racing with NAPI reading npinfo */
743 static int __init
netpoll_init(void) {
744 skb_queue_head_init(&skb_pool
);
747 core_initcall(netpoll_init
);
749 void netpoll_cleanup(struct netpoll
*np
)
751 struct netpoll_info
*npinfo
;
755 npinfo
= np
->dev
->npinfo
;
757 if (npinfo
->rx_np
== np
) {
758 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
759 npinfo
->rx_np
= NULL
;
760 npinfo
->rx_flags
&= ~NETPOLL_RX_ENABLED
;
761 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
764 np
->dev
->npinfo
= NULL
;
765 if (atomic_dec_and_test(&npinfo
->refcnt
)) {
766 skb_queue_purge(&npinfo
->arp_tx
);
767 skb_queue_purge(&npinfo
->txq
);
768 flush_scheduled_work();
780 int netpoll_trap(void)
782 return atomic_read(&trapped
);
785 void netpoll_set_trap(int trap
)
788 atomic_inc(&trapped
);
790 atomic_dec(&trapped
);
793 EXPORT_SYMBOL(netpoll_set_trap
);
794 EXPORT_SYMBOL(netpoll_trap
);
795 EXPORT_SYMBOL(netpoll_parse_options
);
796 EXPORT_SYMBOL(netpoll_setup
);
797 EXPORT_SYMBOL(netpoll_cleanup
);
798 EXPORT_SYMBOL(netpoll_send_udp
);
799 EXPORT_SYMBOL(netpoll_poll
);
800 EXPORT_SYMBOL(netpoll_queue
);