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/inetdevice.h>
17 #include <linux/inet.h>
18 #include <linux/interrupt.h>
19 #include <linux/netpoll.h>
20 #include <linux/sched.h>
21 #include <linux/delay.h>
22 #include <linux/rcupdate.h>
23 #include <linux/workqueue.h>
26 #include <asm/unaligned.h>
29 * We maintain a small pool of fully-sized skbs, to make sure the
30 * message gets out even in extreme OOM situations.
33 #define MAX_UDP_CHUNK 1460
35 #define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
36 #define MAX_RETRIES 20000
38 static DEFINE_SPINLOCK(skb_list_lock
);
40 static struct sk_buff
*skbs
;
42 static DEFINE_SPINLOCK(queue_lock
);
43 static int queue_depth
;
44 static struct sk_buff
*queue_head
, *queue_tail
;
46 static atomic_t trapped
;
48 #define NETPOLL_RX_ENABLED 1
49 #define NETPOLL_RX_DROP 2
51 #define MAX_SKB_SIZE \
52 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
53 sizeof(struct iphdr) + sizeof(struct ethhdr))
55 static void zap_completion_queue(void);
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_HW
&&
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 void netpoll_poll(struct netpoll
*np
)
157 if(!np
->dev
|| !netif_running(np
->dev
) || !np
->dev
->poll_controller
)
160 /* Process pending work on NIC */
161 np
->dev
->poll_controller(np
->dev
);
165 zap_completion_queue();
168 static void refill_skbs(void)
173 spin_lock_irqsave(&skb_list_lock
, flags
);
174 while (nr_skbs
< MAX_SKBS
) {
175 skb
= alloc_skb(MAX_SKB_SIZE
, GFP_ATOMIC
);
183 spin_unlock_irqrestore(&skb_list_lock
, flags
);
186 static void zap_completion_queue(void)
189 struct softnet_data
*sd
= &get_cpu_var(softnet_data
);
191 if (sd
->completion_queue
) {
192 struct sk_buff
*clist
;
194 local_irq_save(flags
);
195 clist
= sd
->completion_queue
;
196 sd
->completion_queue
= NULL
;
197 local_irq_restore(flags
);
199 while (clist
!= NULL
) {
200 struct sk_buff
*skb
= clist
;
203 dev_kfree_skb_any(skb
); /* put this one back */
209 put_cpu_var(softnet_data
);
212 static struct sk_buff
* find_skb(struct netpoll
*np
, int len
, int reserve
)
214 int once
= 1, count
= 0;
216 struct sk_buff
*skb
= NULL
;
218 zap_completion_queue();
220 if (nr_skbs
< MAX_SKBS
)
223 skb
= alloc_skb(len
, GFP_ATOMIC
);
226 spin_lock_irqsave(&skb_list_lock
, flags
);
233 spin_unlock_irqrestore(&skb_list_lock
, flags
);
238 if (once
&& (count
== 1000000)) {
239 printk("out of netpoll skbs!\n");
246 atomic_set(&skb
->users
, 1);
247 skb_reserve(skb
, reserve
);
251 static void netpoll_send_skb(struct netpoll
*np
, struct sk_buff
*skb
)
254 struct netpoll_info
*npinfo
;
256 if (!np
|| !np
->dev
|| !netif_running(np
->dev
)) {
261 npinfo
= np
->dev
->npinfo
;
263 /* avoid recursion */
264 if (npinfo
->poll_owner
== smp_processor_id() ||
265 np
->dev
->xmit_lock_owner
== smp_processor_id()) {
275 spin_lock(&np
->dev
->xmit_lock
);
276 np
->dev
->xmit_lock_owner
= smp_processor_id();
279 * network drivers do not expect to be called if the queue is
282 if (netif_queue_stopped(np
->dev
)) {
283 np
->dev
->xmit_lock_owner
= -1;
284 spin_unlock(&np
->dev
->xmit_lock
);
290 status
= np
->dev
->hard_start_xmit(skb
, np
->dev
);
291 np
->dev
->xmit_lock_owner
= -1;
292 spin_unlock(&np
->dev
->xmit_lock
);
296 npinfo
->tries
= MAX_RETRIES
; /* reset */
303 } while (npinfo
->tries
> 0);
306 void netpoll_send_udp(struct netpoll
*np
, const char *msg
, int len
)
308 int total_len
, eth_len
, ip_len
, udp_len
;
314 udp_len
= len
+ sizeof(*udph
);
315 ip_len
= eth_len
= udp_len
+ sizeof(*iph
);
316 total_len
= eth_len
+ ETH_HLEN
+ NET_IP_ALIGN
;
318 skb
= find_skb(np
, total_len
, total_len
- len
);
322 memcpy(skb
->data
, msg
, len
);
325 udph
= (struct udphdr
*) skb_push(skb
, sizeof(*udph
));
326 udph
->source
= htons(np
->local_port
);
327 udph
->dest
= htons(np
->remote_port
);
328 udph
->len
= htons(udp_len
);
331 iph
= (struct iphdr
*)skb_push(skb
, sizeof(*iph
));
333 /* iph->version = 4; iph->ihl = 5; */
334 put_unaligned(0x45, (unsigned char *)iph
);
336 put_unaligned(htons(ip_len
), &(iph
->tot_len
));
340 iph
->protocol
= IPPROTO_UDP
;
342 put_unaligned(htonl(np
->local_ip
), &(iph
->saddr
));
343 put_unaligned(htonl(np
->remote_ip
), &(iph
->daddr
));
344 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
346 eth
= (struct ethhdr
*) skb_push(skb
, ETH_HLEN
);
348 eth
->h_proto
= htons(ETH_P_IP
);
349 memcpy(eth
->h_source
, np
->local_mac
, 6);
350 memcpy(eth
->h_dest
, np
->remote_mac
, 6);
354 netpoll_send_skb(np
, skb
);
357 static void arp_reply(struct sk_buff
*skb
)
359 struct netpoll_info
*npinfo
= skb
->dev
->npinfo
;
361 unsigned char *arp_ptr
;
362 int size
, type
= ARPOP_REPLY
, ptype
= ETH_P_ARP
;
364 struct sk_buff
*send_skb
;
365 struct netpoll
*np
= NULL
;
367 if (npinfo
->rx_np
&& npinfo
->rx_np
->dev
== skb
->dev
)
372 /* No arp on this interface */
373 if (skb
->dev
->flags
& IFF_NOARP
)
376 if (!pskb_may_pull(skb
, (sizeof(struct arphdr
) +
377 (2 * skb
->dev
->addr_len
) +
381 skb
->h
.raw
= skb
->nh
.raw
= skb
->data
;
384 if ((arp
->ar_hrd
!= htons(ARPHRD_ETHER
) &&
385 arp
->ar_hrd
!= htons(ARPHRD_IEEE802
)) ||
386 arp
->ar_pro
!= htons(ETH_P_IP
) ||
387 arp
->ar_op
!= htons(ARPOP_REQUEST
))
390 arp_ptr
= (unsigned char *)(arp
+1) + skb
->dev
->addr_len
;
391 memcpy(&sip
, arp_ptr
, 4);
392 arp_ptr
+= 4 + skb
->dev
->addr_len
;
393 memcpy(&tip
, arp_ptr
, 4);
395 /* Should we ignore arp? */
396 if (tip
!= htonl(np
->local_ip
) || LOOPBACK(tip
) || MULTICAST(tip
))
399 size
= sizeof(struct arphdr
) + 2 * (skb
->dev
->addr_len
+ 4);
400 send_skb
= find_skb(np
, size
+ LL_RESERVED_SPACE(np
->dev
),
401 LL_RESERVED_SPACE(np
->dev
));
406 send_skb
->nh
.raw
= send_skb
->data
;
407 arp
= (struct arphdr
*) skb_put(send_skb
, size
);
408 send_skb
->dev
= skb
->dev
;
409 send_skb
->protocol
= htons(ETH_P_ARP
);
411 /* Fill the device header for the ARP frame */
413 if (np
->dev
->hard_header
&&
414 np
->dev
->hard_header(send_skb
, skb
->dev
, ptype
,
415 np
->remote_mac
, np
->local_mac
,
416 send_skb
->len
) < 0) {
422 * Fill out the arp protocol part.
424 * we only support ethernet device type,
425 * which (according to RFC 1390) should always equal 1 (Ethernet).
428 arp
->ar_hrd
= htons(np
->dev
->type
);
429 arp
->ar_pro
= htons(ETH_P_IP
);
430 arp
->ar_hln
= np
->dev
->addr_len
;
432 arp
->ar_op
= htons(type
);
434 arp_ptr
=(unsigned char *)(arp
+ 1);
435 memcpy(arp_ptr
, np
->dev
->dev_addr
, np
->dev
->addr_len
);
436 arp_ptr
+= np
->dev
->addr_len
;
437 memcpy(arp_ptr
, &tip
, 4);
439 memcpy(arp_ptr
, np
->remote_mac
, np
->dev
->addr_len
);
440 arp_ptr
+= np
->dev
->addr_len
;
441 memcpy(arp_ptr
, &sip
, 4);
443 netpoll_send_skb(np
, send_skb
);
446 int __netpoll_rx(struct sk_buff
*skb
)
448 int proto
, len
, ulen
;
451 struct netpoll
*np
= skb
->dev
->npinfo
->rx_np
;
455 if (skb
->dev
->type
!= ARPHRD_ETHER
)
458 /* check if netpoll clients need ARP */
459 if (skb
->protocol
== __constant_htons(ETH_P_ARP
) &&
460 atomic_read(&trapped
)) {
465 proto
= ntohs(eth_hdr(skb
)->h_proto
);
466 if (proto
!= ETH_P_IP
)
468 if (skb
->pkt_type
== PACKET_OTHERHOST
)
473 iph
= (struct iphdr
*)skb
->data
;
474 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
476 if (iph
->ihl
< 5 || iph
->version
!= 4)
478 if (!pskb_may_pull(skb
, iph
->ihl
*4))
480 if (ip_fast_csum((u8
*)iph
, iph
->ihl
) != 0)
483 len
= ntohs(iph
->tot_len
);
484 if (skb
->len
< len
|| len
< iph
->ihl
*4)
487 if (iph
->protocol
!= IPPROTO_UDP
)
491 uh
= (struct udphdr
*)(((char *)iph
) + iph
->ihl
*4);
492 ulen
= ntohs(uh
->len
);
496 if (checksum_udp(skb
, uh
, ulen
, iph
->saddr
, iph
->daddr
))
498 if (np
->local_ip
&& np
->local_ip
!= ntohl(iph
->daddr
))
500 if (np
->remote_ip
&& np
->remote_ip
!= ntohl(iph
->saddr
))
502 if (np
->local_port
&& np
->local_port
!= ntohs(uh
->dest
))
505 np
->rx_hook(np
, ntohs(uh
->source
),
507 ulen
- sizeof(struct udphdr
));
513 if (atomic_read(&trapped
)) {
521 int netpoll_parse_options(struct netpoll
*np
, char *opt
)
523 char *cur
=opt
, *delim
;
526 if ((delim
= strchr(cur
, '@')) == NULL
)
529 np
->local_port
=simple_strtol(cur
, NULL
, 10);
533 printk(KERN_INFO
"%s: local port %d\n", np
->name
, np
->local_port
);
536 if ((delim
= strchr(cur
, '/')) == NULL
)
539 np
->local_ip
=ntohl(in_aton(cur
));
542 printk(KERN_INFO
"%s: local IP %d.%d.%d.%d\n",
543 np
->name
, HIPQUAD(np
->local_ip
));
548 /* parse out dev name */
549 if ((delim
= strchr(cur
, ',')) == NULL
)
552 strlcpy(np
->dev_name
, cur
, sizeof(np
->dev_name
));
557 printk(KERN_INFO
"%s: interface %s\n", np
->name
, np
->dev_name
);
561 if ((delim
= strchr(cur
, '@')) == NULL
)
564 np
->remote_port
=simple_strtol(cur
, NULL
, 10);
568 printk(KERN_INFO
"%s: remote port %d\n", np
->name
, np
->remote_port
);
571 if ((delim
= strchr(cur
, '/')) == NULL
)
574 np
->remote_ip
=ntohl(in_aton(cur
));
577 printk(KERN_INFO
"%s: remote IP %d.%d.%d.%d\n",
578 np
->name
, HIPQUAD(np
->remote_ip
));
583 if ((delim
= strchr(cur
, ':')) == NULL
)
586 np
->remote_mac
[0]=simple_strtol(cur
, NULL
, 16);
588 if ((delim
= strchr(cur
, ':')) == NULL
)
591 np
->remote_mac
[1]=simple_strtol(cur
, NULL
, 16);
593 if ((delim
= strchr(cur
, ':')) == NULL
)
596 np
->remote_mac
[2]=simple_strtol(cur
, NULL
, 16);
598 if ((delim
= strchr(cur
, ':')) == NULL
)
601 np
->remote_mac
[3]=simple_strtol(cur
, NULL
, 16);
603 if ((delim
= strchr(cur
, ':')) == NULL
)
606 np
->remote_mac
[4]=simple_strtol(cur
, NULL
, 16);
608 np
->remote_mac
[5]=simple_strtol(cur
, NULL
, 16);
611 printk(KERN_INFO
"%s: remote ethernet address "
612 "%02x:%02x:%02x:%02x:%02x:%02x\n",
624 printk(KERN_INFO
"%s: couldn't parse config at %s!\n",
629 int netpoll_setup(struct netpoll
*np
)
631 struct net_device
*ndev
= NULL
;
632 struct in_device
*in_dev
;
633 struct netpoll_info
*npinfo
;
637 ndev
= dev_get_by_name(np
->dev_name
);
639 printk(KERN_ERR
"%s: %s doesn't exist, aborting.\n",
640 np
->name
, np
->dev_name
);
646 npinfo
= kmalloc(sizeof(*npinfo
), GFP_KERNEL
);
650 npinfo
->rx_flags
= 0;
651 npinfo
->rx_np
= NULL
;
652 spin_lock_init(&npinfo
->poll_lock
);
653 npinfo
->poll_owner
= -1;
654 npinfo
->tries
= MAX_RETRIES
;
655 spin_lock_init(&npinfo
->rx_lock
);
657 npinfo
= ndev
->npinfo
;
659 if (!ndev
->poll_controller
) {
660 printk(KERN_ERR
"%s: %s doesn't support polling, aborting.\n",
661 np
->name
, np
->dev_name
);
665 if (!netif_running(ndev
)) {
666 unsigned long atmost
, atleast
;
668 printk(KERN_INFO
"%s: device %s not up yet, forcing it\n",
669 np
->name
, np
->dev_name
);
672 if (dev_change_flags(ndev
, ndev
->flags
| IFF_UP
) < 0) {
673 printk(KERN_ERR
"%s: failed to open %s\n",
674 np
->name
, np
->dev_name
);
680 atleast
= jiffies
+ HZ
/10;
681 atmost
= jiffies
+ 4*HZ
;
682 while (!netif_carrier_ok(ndev
)) {
683 if (time_after(jiffies
, atmost
)) {
685 "%s: timeout waiting for carrier\n",
692 /* If carrier appears to come up instantly, we don't
693 * trust it and pause so that we don't pump all our
694 * queued console messages into the bitbucket.
697 if (time_before(jiffies
, atleast
)) {
698 printk(KERN_NOTICE
"%s: carrier detect appears"
699 " untrustworthy, waiting 4 seconds\n",
705 if (!memcmp(np
->local_mac
, "\0\0\0\0\0\0", 6) && ndev
->dev_addr
)
706 memcpy(np
->local_mac
, ndev
->dev_addr
, 6);
710 in_dev
= __in_dev_get_rcu(ndev
);
712 if (!in_dev
|| !in_dev
->ifa_list
) {
714 printk(KERN_ERR
"%s: no IP address for %s, aborting\n",
715 np
->name
, np
->dev_name
);
719 np
->local_ip
= ntohl(in_dev
->ifa_list
->ifa_local
);
721 printk(KERN_INFO
"%s: local IP %d.%d.%d.%d\n",
722 np
->name
, HIPQUAD(np
->local_ip
));
726 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
727 npinfo
->rx_flags
|= NETPOLL_RX_ENABLED
;
729 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
732 /* fill up the skb queue */
735 /* last thing to do is link it to the net device structure */
736 ndev
->npinfo
= npinfo
;
738 /* avoid racing with NAPI reading npinfo */
751 void netpoll_cleanup(struct netpoll
*np
)
753 struct netpoll_info
*npinfo
;
757 npinfo
= np
->dev
->npinfo
;
758 if (npinfo
&& npinfo
->rx_np
== np
) {
759 spin_lock_irqsave(&npinfo
->rx_lock
, flags
);
760 npinfo
->rx_np
= NULL
;
761 npinfo
->rx_flags
&= ~NETPOLL_RX_ENABLED
;
762 spin_unlock_irqrestore(&npinfo
->rx_lock
, flags
);
770 int netpoll_trap(void)
772 return atomic_read(&trapped
);
775 void netpoll_set_trap(int trap
)
778 atomic_inc(&trapped
);
780 atomic_dec(&trapped
);
783 EXPORT_SYMBOL(netpoll_set_trap
);
784 EXPORT_SYMBOL(netpoll_trap
);
785 EXPORT_SYMBOL(netpoll_parse_options
);
786 EXPORT_SYMBOL(netpoll_setup
);
787 EXPORT_SYMBOL(netpoll_cleanup
);
788 EXPORT_SYMBOL(netpoll_send_udp
);
789 EXPORT_SYMBOL(netpoll_poll
);
790 EXPORT_SYMBOL(netpoll_queue
);
This page took 0.062245 seconds and 5 git commands to generate.