blob: 9cb781830380242cc16b9204cfdb103381dc4234 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common framework for low-level network console, dump, and debugger code
3 *
4 * Sep 8 2003 Matt Mackall <mpm@selenic.com>
5 *
6 * based on the netconsole code from:
7 *
8 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2002 Red Hat, Inc.
10 */
11
12#include <linux/smp_lock.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/string.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020016#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 <net/tcp.h>
26#include <net/udp.h>
27#include <asm/unaligned.h>
28
29/*
30 * We maintain a small pool of fully-sized skbs, to make sure the
31 * message gets out even in extreme OOM situations.
32 */
33
34#define MAX_UDP_CHUNK 1460
35#define MAX_SKBS 32
36#define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
Matt Mackall0db1d6f2005-08-11 19:25:54 -070037#define MAX_RETRIES 20000
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static DEFINE_SPINLOCK(skb_list_lock);
40static int nr_skbs;
41static struct sk_buff *skbs;
42
43static DEFINE_SPINLOCK(queue_lock);
44static int queue_depth;
45static struct sk_buff *queue_head, *queue_tail;
46
47static atomic_t trapped;
48
49#define NETPOLL_RX_ENABLED 1
50#define NETPOLL_RX_DROP 2
51
52#define MAX_SKB_SIZE \
53 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
54 sizeof(struct iphdr) + sizeof(struct ethhdr))
55
56static void zap_completion_queue(void);
57
58static void queue_process(void *p)
59{
60 unsigned long flags;
61 struct sk_buff *skb;
62
63 while (queue_head) {
64 spin_lock_irqsave(&queue_lock, flags);
65
66 skb = queue_head;
67 queue_head = skb->next;
68 if (skb == queue_tail)
69 queue_head = NULL;
70
71 queue_depth--;
72
73 spin_unlock_irqrestore(&queue_lock, flags);
74
75 dev_queue_xmit(skb);
76 }
77}
78
79static DECLARE_WORK(send_queue, queue_process, NULL);
80
81void netpoll_queue(struct sk_buff *skb)
82{
83 unsigned long flags;
84
85 if (queue_depth == MAX_QUEUE_DEPTH) {
86 __kfree_skb(skb);
87 return;
88 }
89
90 spin_lock_irqsave(&queue_lock, flags);
91 if (!queue_head)
92 queue_head = skb;
93 else
94 queue_tail->next = skb;
95 queue_tail = skb;
96 queue_depth++;
97 spin_unlock_irqrestore(&queue_lock, flags);
98
99 schedule_work(&send_queue);
100}
101
102static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
103 unsigned short ulen, u32 saddr, u32 daddr)
104{
Herbert Xufb286bb2005-11-10 13:01:24 -0800105 unsigned int psum;
106
107 if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109
Herbert Xufb286bb2005-11-10 13:01:24 -0800110 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Herbert Xufb286bb2005-11-10 13:01:24 -0800112 if (skb->ip_summed == CHECKSUM_HW &&
113 !(u16)csum_fold(csum_add(psum, skb->csum)))
114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Herbert Xufb286bb2005-11-10 13:01:24 -0800116 skb->csum = psum;
117
118 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121/*
122 * Check whether delayed processing was scheduled for our NIC. If so,
123 * we attempt to grab the poll lock and use ->poll() to pump the card.
124 * If this fails, either we've recursed in ->poll() or it's already
125 * running on another CPU.
126 *
127 * Note: we don't mask interrupts with this lock because we're using
128 * trylock here and interrupts are already disabled in the softirq
129 * case. Further, we test the poll_owner to avoid recursion on UP
130 * systems where the lock doesn't exist.
131 *
132 * In cases where there is bi-directional communications, reading only
133 * one message at a time can lead to packets being dropped by the
134 * network adapter, forcing superfluous retries and possibly timeouts.
135 * Thus, we set our budget to greater than 1.
136 */
137static void poll_napi(struct netpoll *np)
138{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700139 struct netpoll_info *npinfo = np->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int budget = 16;
141
142 if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
Jeff Moyer115c1d62005-06-22 22:05:31 -0700143 npinfo->poll_owner != smp_processor_id() &&
144 spin_trylock(&npinfo->poll_lock)) {
145 npinfo->rx_flags |= NETPOLL_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 atomic_inc(&trapped);
147
148 np->dev->poll(np->dev, &budget);
149
150 atomic_dec(&trapped);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700151 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
152 spin_unlock(&npinfo->poll_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154}
155
156void netpoll_poll(struct netpoll *np)
157{
158 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
159 return;
160
161 /* Process pending work on NIC */
162 np->dev->poll_controller(np->dev);
163 if (np->dev->poll)
164 poll_napi(np);
165
166 zap_completion_queue();
167}
168
169static void refill_skbs(void)
170{
171 struct sk_buff *skb;
172 unsigned long flags;
173
174 spin_lock_irqsave(&skb_list_lock, flags);
175 while (nr_skbs < MAX_SKBS) {
176 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
177 if (!skb)
178 break;
179
180 skb->next = skbs;
181 skbs = skb;
182 nr_skbs++;
183 }
184 spin_unlock_irqrestore(&skb_list_lock, flags);
185}
186
187static void zap_completion_queue(void)
188{
189 unsigned long flags;
190 struct softnet_data *sd = &get_cpu_var(softnet_data);
191
192 if (sd->completion_queue) {
193 struct sk_buff *clist;
194
195 local_irq_save(flags);
196 clist = sd->completion_queue;
197 sd->completion_queue = NULL;
198 local_irq_restore(flags);
199
200 while (clist != NULL) {
201 struct sk_buff *skb = clist;
202 clist = clist->next;
203 if(skb->destructor)
204 dev_kfree_skb_any(skb); /* put this one back */
205 else
206 __kfree_skb(skb);
207 }
208 }
209
210 put_cpu_var(softnet_data);
211}
212
213static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve)
214{
215 int once = 1, count = 0;
216 unsigned long flags;
217 struct sk_buff *skb = NULL;
218
219 zap_completion_queue();
220repeat:
221 if (nr_skbs < MAX_SKBS)
222 refill_skbs();
223
224 skb = alloc_skb(len, GFP_ATOMIC);
225
226 if (!skb) {
227 spin_lock_irqsave(&skb_list_lock, flags);
228 skb = skbs;
229 if (skb) {
230 skbs = skb->next;
231 skb->next = NULL;
232 nr_skbs--;
233 }
234 spin_unlock_irqrestore(&skb_list_lock, flags);
235 }
236
237 if(!skb) {
238 count++;
239 if (once && (count == 1000000)) {
240 printk("out of netpoll skbs!\n");
241 once = 0;
242 }
243 netpoll_poll(np);
244 goto repeat;
245 }
246
247 atomic_set(&skb->users, 1);
248 skb_reserve(skb, reserve);
249 return skb;
250}
251
252static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
253{
254 int status;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700255 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Matt Mackallf0d34592005-08-11 19:25:11 -0700257 if (!np || !np->dev || !netif_running(np->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 __kfree_skb(skb);
259 return;
260 }
261
Jeff Moyer115c1d62005-06-22 22:05:31 -0700262 npinfo = np->dev->npinfo;
Matt Mackallf0d34592005-08-11 19:25:11 -0700263
264 /* avoid recursion */
Jeff Moyer115c1d62005-06-22 22:05:31 -0700265 if (npinfo->poll_owner == smp_processor_id() ||
266 np->dev->xmit_lock_owner == smp_processor_id()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (np->drop)
268 np->drop(skb);
269 else
270 __kfree_skb(skb);
271 return;
272 }
273
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700274 do {
275 npinfo->tries--;
Herbert Xu932ff272006-06-09 12:20:56 -0700276 netif_tx_lock(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Matt Mackallf0d34592005-08-11 19:25:11 -0700278 /*
279 * network drivers do not expect to be called if the queue is
280 * stopped.
281 */
282 if (netif_queue_stopped(np->dev)) {
Herbert Xu932ff272006-06-09 12:20:56 -0700283 netif_tx_unlock(np->dev);
Matt Mackallf0d34592005-08-11 19:25:11 -0700284 netpoll_poll(np);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700285 udelay(50);
Matt Mackallf0d34592005-08-11 19:25:11 -0700286 continue;
287 }
288
289 status = np->dev->hard_start_xmit(skb, np->dev);
Herbert Xu932ff272006-06-09 12:20:56 -0700290 netif_tx_unlock(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Matt Mackallf0d34592005-08-11 19:25:11 -0700292 /* success */
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700293 if(!status) {
294 npinfo->tries = MAX_RETRIES; /* reset */
Matt Mackallf0d34592005-08-11 19:25:11 -0700295 return;
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Matt Mackallf0d34592005-08-11 19:25:11 -0700298 /* transmit busy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 netpoll_poll(np);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700300 udelay(50);
301 } while (npinfo->tries > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
305{
306 int total_len, eth_len, ip_len, udp_len;
307 struct sk_buff *skb;
308 struct udphdr *udph;
309 struct iphdr *iph;
310 struct ethhdr *eth;
311
312 udp_len = len + sizeof(*udph);
313 ip_len = eth_len = udp_len + sizeof(*iph);
314 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
315
316 skb = find_skb(np, total_len, total_len - len);
317 if (!skb)
318 return;
319
320 memcpy(skb->data, msg, len);
321 skb->len += len;
322
323 udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
324 udph->source = htons(np->local_port);
325 udph->dest = htons(np->remote_port);
326 udph->len = htons(udp_len);
327 udph->check = 0;
328
329 iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
330
331 /* iph->version = 4; iph->ihl = 5; */
332 put_unaligned(0x45, (unsigned char *)iph);
333 iph->tos = 0;
334 put_unaligned(htons(ip_len), &(iph->tot_len));
335 iph->id = 0;
336 iph->frag_off = 0;
337 iph->ttl = 64;
338 iph->protocol = IPPROTO_UDP;
339 iph->check = 0;
340 put_unaligned(htonl(np->local_ip), &(iph->saddr));
341 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
342 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
343
344 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
345
346 eth->h_proto = htons(ETH_P_IP);
347 memcpy(eth->h_source, np->local_mac, 6);
348 memcpy(eth->h_dest, np->remote_mac, 6);
349
350 skb->dev = np->dev;
351
352 netpoll_send_skb(np, skb);
353}
354
355static void arp_reply(struct sk_buff *skb)
356{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700357 struct netpoll_info *npinfo = skb->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct arphdr *arp;
359 unsigned char *arp_ptr;
360 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
361 u32 sip, tip;
362 struct sk_buff *send_skb;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700363 struct netpoll *np = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700365 if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
366 np = npinfo->rx_np;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700367 if (!np)
368 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* No arp on this interface */
371 if (skb->dev->flags & IFF_NOARP)
372 return;
373
374 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
375 (2 * skb->dev->addr_len) +
376 (2 * sizeof(u32)))))
377 return;
378
379 skb->h.raw = skb->nh.raw = skb->data;
380 arp = skb->nh.arph;
381
382 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
383 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
384 arp->ar_pro != htons(ETH_P_IP) ||
385 arp->ar_op != htons(ARPOP_REQUEST))
386 return;
387
388 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
389 memcpy(&sip, arp_ptr, 4);
390 arp_ptr += 4 + skb->dev->addr_len;
391 memcpy(&tip, arp_ptr, 4);
392
393 /* Should we ignore arp? */
394 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
395 return;
396
397 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
398 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
399 LL_RESERVED_SPACE(np->dev));
400
401 if (!send_skb)
402 return;
403
404 send_skb->nh.raw = send_skb->data;
405 arp = (struct arphdr *) skb_put(send_skb, size);
406 send_skb->dev = skb->dev;
407 send_skb->protocol = htons(ETH_P_ARP);
408
409 /* Fill the device header for the ARP frame */
410
411 if (np->dev->hard_header &&
412 np->dev->hard_header(send_skb, skb->dev, ptype,
413 np->remote_mac, np->local_mac,
414 send_skb->len) < 0) {
415 kfree_skb(send_skb);
416 return;
417 }
418
419 /*
420 * Fill out the arp protocol part.
421 *
422 * we only support ethernet device type,
423 * which (according to RFC 1390) should always equal 1 (Ethernet).
424 */
425
426 arp->ar_hrd = htons(np->dev->type);
427 arp->ar_pro = htons(ETH_P_IP);
428 arp->ar_hln = np->dev->addr_len;
429 arp->ar_pln = 4;
430 arp->ar_op = htons(type);
431
432 arp_ptr=(unsigned char *)(arp + 1);
433 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
434 arp_ptr += np->dev->addr_len;
435 memcpy(arp_ptr, &tip, 4);
436 arp_ptr += 4;
437 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
438 arp_ptr += np->dev->addr_len;
439 memcpy(arp_ptr, &sip, 4);
440
441 netpoll_send_skb(np, send_skb);
442}
443
444int __netpoll_rx(struct sk_buff *skb)
445{
446 int proto, len, ulen;
447 struct iphdr *iph;
448 struct udphdr *uh;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700449 struct netpoll *np = skb->dev->npinfo->rx_np;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700451 if (!np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto out;
453 if (skb->dev->type != ARPHRD_ETHER)
454 goto out;
455
456 /* check if netpoll clients need ARP */
457 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
458 atomic_read(&trapped)) {
459 arp_reply(skb);
460 return 1;
461 }
462
463 proto = ntohs(eth_hdr(skb)->h_proto);
464 if (proto != ETH_P_IP)
465 goto out;
466 if (skb->pkt_type == PACKET_OTHERHOST)
467 goto out;
468 if (skb_shared(skb))
469 goto out;
470
471 iph = (struct iphdr *)skb->data;
472 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
473 goto out;
474 if (iph->ihl < 5 || iph->version != 4)
475 goto out;
476 if (!pskb_may_pull(skb, iph->ihl*4))
477 goto out;
478 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
479 goto out;
480
481 len = ntohs(iph->tot_len);
482 if (skb->len < len || len < iph->ihl*4)
483 goto out;
484
485 if (iph->protocol != IPPROTO_UDP)
486 goto out;
487
488 len -= iph->ihl*4;
489 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
490 ulen = ntohs(uh->len);
491
492 if (ulen != len)
493 goto out;
Herbert Xufb286bb2005-11-10 13:01:24 -0800494 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto out;
496 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
497 goto out;
498 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
499 goto out;
500 if (np->local_port && np->local_port != ntohs(uh->dest))
501 goto out;
502
503 np->rx_hook(np, ntohs(uh->source),
504 (char *)(uh+1),
505 ulen - sizeof(struct udphdr));
506
507 kfree_skb(skb);
508 return 1;
509
510out:
511 if (atomic_read(&trapped)) {
512 kfree_skb(skb);
513 return 1;
514 }
515
516 return 0;
517}
518
519int netpoll_parse_options(struct netpoll *np, char *opt)
520{
521 char *cur=opt, *delim;
522
523 if(*cur != '@') {
524 if ((delim = strchr(cur, '@')) == NULL)
525 goto parse_failed;
526 *delim=0;
527 np->local_port=simple_strtol(cur, NULL, 10);
528 cur=delim;
529 }
530 cur++;
531 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
532
533 if(*cur != '/') {
534 if ((delim = strchr(cur, '/')) == NULL)
535 goto parse_failed;
536 *delim=0;
537 np->local_ip=ntohl(in_aton(cur));
538 cur=delim;
539
540 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
541 np->name, HIPQUAD(np->local_ip));
542 }
543 cur++;
544
545 if ( *cur != ',') {
546 /* parse out dev name */
547 if ((delim = strchr(cur, ',')) == NULL)
548 goto parse_failed;
549 *delim=0;
550 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
551 cur=delim;
552 }
553 cur++;
554
555 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
556
557 if ( *cur != '@' ) {
558 /* dst port */
559 if ((delim = strchr(cur, '@')) == NULL)
560 goto parse_failed;
561 *delim=0;
562 np->remote_port=simple_strtol(cur, NULL, 10);
563 cur=delim;
564 }
565 cur++;
566 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
567
568 /* dst ip */
569 if ((delim = strchr(cur, '/')) == NULL)
570 goto parse_failed;
571 *delim=0;
572 np->remote_ip=ntohl(in_aton(cur));
573 cur=delim+1;
574
575 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
576 np->name, HIPQUAD(np->remote_ip));
577
578 if( *cur != 0 )
579 {
580 /* MAC address */
581 if ((delim = strchr(cur, ':')) == NULL)
582 goto parse_failed;
583 *delim=0;
584 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
585 cur=delim+1;
586 if ((delim = strchr(cur, ':')) == NULL)
587 goto parse_failed;
588 *delim=0;
589 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
590 cur=delim+1;
591 if ((delim = strchr(cur, ':')) == NULL)
592 goto parse_failed;
593 *delim=0;
594 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
595 cur=delim+1;
596 if ((delim = strchr(cur, ':')) == NULL)
597 goto parse_failed;
598 *delim=0;
599 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
600 cur=delim+1;
601 if ((delim = strchr(cur, ':')) == NULL)
602 goto parse_failed;
603 *delim=0;
604 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
605 cur=delim+1;
606 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
607 }
608
609 printk(KERN_INFO "%s: remote ethernet address "
610 "%02x:%02x:%02x:%02x:%02x:%02x\n",
611 np->name,
612 np->remote_mac[0],
613 np->remote_mac[1],
614 np->remote_mac[2],
615 np->remote_mac[3],
616 np->remote_mac[4],
617 np->remote_mac[5]);
618
619 return 0;
620
621 parse_failed:
622 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
623 np->name, cur);
624 return -1;
625}
626
627int netpoll_setup(struct netpoll *np)
628{
629 struct net_device *ndev = NULL;
630 struct in_device *in_dev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700631 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700632 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (np->dev_name)
635 ndev = dev_get_by_name(np->dev_name);
636 if (!ndev) {
637 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
638 np->name, np->dev_name);
639 return -1;
640 }
641
642 np->dev = ndev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700643 if (!ndev->npinfo) {
644 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
645 if (!npinfo)
646 goto release;
647
Jeff Moyer11513122005-08-11 19:23:04 -0700648 npinfo->rx_flags = 0;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700649 npinfo->rx_np = NULL;
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700650 spin_lock_init(&npinfo->poll_lock);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700651 npinfo->poll_owner = -1;
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700652 npinfo->tries = MAX_RETRIES;
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700653 spin_lock_init(&npinfo->rx_lock);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700654 } else
655 npinfo = ndev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 if (!ndev->poll_controller) {
658 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
659 np->name, np->dev_name);
660 goto release;
661 }
662
663 if (!netif_running(ndev)) {
664 unsigned long atmost, atleast;
665
666 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
667 np->name, np->dev_name);
668
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800669 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) {
671 printk(KERN_ERR "%s: failed to open %s\n",
672 np->name, np->dev_name);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800673 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto release;
675 }
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800676 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 atleast = jiffies + HZ/10;
679 atmost = jiffies + 4*HZ;
680 while (!netif_carrier_ok(ndev)) {
681 if (time_after(jiffies, atmost)) {
682 printk(KERN_NOTICE
683 "%s: timeout waiting for carrier\n",
684 np->name);
685 break;
686 }
687 cond_resched();
688 }
689
690 /* If carrier appears to come up instantly, we don't
691 * trust it and pause so that we don't pump all our
692 * queued console messages into the bitbucket.
693 */
694
695 if (time_before(jiffies, atleast)) {
696 printk(KERN_NOTICE "%s: carrier detect appears"
697 " untrustworthy, waiting 4 seconds\n",
698 np->name);
699 msleep(4000);
700 }
701 }
702
Kris Katterjohn38602882006-01-17 15:15:38 -0800703 if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 memcpy(np->local_mac, ndev->dev_addr, 6);
705
706 if (!np->local_ip) {
707 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700708 in_dev = __in_dev_get_rcu(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (!in_dev || !in_dev->ifa_list) {
711 rcu_read_unlock();
712 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
713 np->name, np->dev_name);
714 goto release;
715 }
716
717 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
718 rcu_read_unlock();
719 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
720 np->name, HIPQUAD(np->local_ip));
721 }
722
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700723 if (np->rx_hook) {
724 spin_lock_irqsave(&npinfo->rx_lock, flags);
725 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
726 npinfo->rx_np = np;
727 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
728 }
Ingo Molnar26520762005-08-11 19:26:42 -0700729
730 /* fill up the skb queue */
731 refill_skbs();
732
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700733 /* last thing to do is link it to the net device structure */
Jeff Moyer115c1d62005-06-22 22:05:31 -0700734 ndev->npinfo = npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Matt Mackall53fb95d2005-08-11 19:27:43 -0700736 /* avoid racing with NAPI reading npinfo */
737 synchronize_rcu();
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return 0;
740
741 release:
Jeff Moyer115c1d62005-06-22 22:05:31 -0700742 if (!ndev->npinfo)
743 kfree(npinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 np->dev = NULL;
745 dev_put(ndev);
746 return -1;
747}
748
749void netpoll_cleanup(struct netpoll *np)
750{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700751 struct netpoll_info *npinfo;
752 unsigned long flags;
753
Jeff Moyer115c1d62005-06-22 22:05:31 -0700754 if (np->dev) {
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700755 npinfo = np->dev->npinfo;
756 if (npinfo && npinfo->rx_np == np) {
757 spin_lock_irqsave(&npinfo->rx_lock, flags);
758 npinfo->rx_np = NULL;
759 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
760 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
761 }
Jeff Moyer115c1d62005-06-22 22:05:31 -0700762 dev_put(np->dev);
763 }
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 np->dev = NULL;
766}
767
768int netpoll_trap(void)
769{
770 return atomic_read(&trapped);
771}
772
773void netpoll_set_trap(int trap)
774{
775 if (trap)
776 atomic_inc(&trapped);
777 else
778 atomic_dec(&trapped);
779}
780
781EXPORT_SYMBOL(netpoll_set_trap);
782EXPORT_SYMBOL(netpoll_trap);
783EXPORT_SYMBOL(netpoll_parse_options);
784EXPORT_SYMBOL(netpoll_setup);
785EXPORT_SYMBOL(netpoll_cleanup);
786EXPORT_SYMBOL(netpoll_send_udp);
787EXPORT_SYMBOL(netpoll_poll);
788EXPORT_SYMBOL(netpoll_queue);