netpoll: Set npinfo to NULL even with ndo_netpoll_cleanup
[deliverable/linux.git] / net / core / netpoll.c
1 /*
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/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>
26 #include <net/tcp.h>
27 #include <net/udp.h>
28 #include <asm/unaligned.h>
29 #include <trace/events/napi.h>
30
31 /*
32 * We maintain a small pool of fully-sized skbs, to make sure the
33 * message gets out even in extreme OOM situations.
34 */
35
36 #define MAX_UDP_CHUNK 1460
37 #define MAX_SKBS 32
38 #define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
39
40 static struct sk_buff_head skb_pool;
41
42 static atomic_t trapped;
43
44 #define USEC_PER_POLL 50
45 #define NETPOLL_RX_ENABLED 1
46 #define NETPOLL_RX_DROP 2
47
48 #define MAX_SKB_SIZE \
49 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
50 sizeof(struct iphdr) + sizeof(struct ethhdr))
51
52 static void arp_reply(struct sk_buff *skb);
53
54 static unsigned int carrier_timeout = 4;
55 module_param(carrier_timeout, uint, 0644);
56
57 static void queue_process(struct work_struct *work)
58 {
59 struct netpoll_info *npinfo =
60 container_of(work, struct netpoll_info, tx_work.work);
61 struct sk_buff *skb;
62 unsigned long flags;
63
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;
68
69 if (!netif_device_present(dev) || !netif_running(dev)) {
70 __kfree_skb(skb);
71 continue;
72 }
73
74 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
75
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);
84
85 schedule_delayed_work(&npinfo->tx_work, HZ/10);
86 return;
87 }
88 __netif_tx_unlock(txq);
89 local_irq_restore(flags);
90 }
91 }
92
93 static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
94 unsigned short ulen, __be32 saddr, __be32 daddr)
95 {
96 __wsum psum;
97
98 if (uh->check == 0 || skb_csum_unnecessary(skb))
99 return 0;
100
101 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
102
103 if (skb->ip_summed == CHECKSUM_COMPLETE &&
104 !csum_fold(csum_add(psum, skb->csum)))
105 return 0;
106
107 skb->csum = psum;
108
109 return __skb_checksum_complete(skb);
110 }
111
112 /*
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.
117 *
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.
122 *
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.
127 */
128 static int poll_one_napi(struct netpoll_info *npinfo,
129 struct napi_struct *napi, int budget)
130 {
131 int work;
132
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.
136 */
137 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
138 return budget;
139
140 npinfo->rx_flags |= NETPOLL_RX_DROP;
141 atomic_inc(&trapped);
142 set_bit(NAPI_STATE_NPSVC, &napi->state);
143
144 work = napi->poll(napi, budget);
145 trace_napi_poll(napi);
146
147 clear_bit(NAPI_STATE_NPSVC, &napi->state);
148 atomic_dec(&trapped);
149 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
150
151 return budget - work;
152 }
153
154 static void poll_napi(struct net_device *dev)
155 {
156 struct napi_struct *napi;
157 int budget = 16;
158
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);
164
165 if (!budget)
166 break;
167 }
168 }
169 }
170
171 static void service_arp_queue(struct netpoll_info *npi)
172 {
173 if (npi) {
174 struct sk_buff *skb;
175
176 while ((skb = skb_dequeue(&npi->arp_tx)))
177 arp_reply(skb);
178 }
179 }
180
181 void netpoll_poll_dev(struct net_device *dev)
182 {
183 const struct net_device_ops *ops;
184
185 if (!dev || !netif_running(dev))
186 return;
187
188 ops = dev->netdev_ops;
189 if (!ops->ndo_poll_controller)
190 return;
191
192 /* Process pending work on NIC */
193 ops->ndo_poll_controller(dev);
194
195 poll_napi(dev);
196
197 service_arp_queue(dev->npinfo);
198
199 }
200
201 void netpoll_poll(struct netpoll *np)
202 {
203 netpoll_poll_dev(np->dev);
204 }
205
206 static void refill_skbs(void)
207 {
208 struct sk_buff *skb;
209 unsigned long flags;
210
211 spin_lock_irqsave(&skb_pool.lock, flags);
212 while (skb_pool.qlen < MAX_SKBS) {
213 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
214 if (!skb)
215 break;
216
217 __skb_queue_tail(&skb_pool, skb);
218 }
219 spin_unlock_irqrestore(&skb_pool.lock, flags);
220 }
221
222 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
223 {
224 int count = 0;
225 struct sk_buff *skb;
226
227 refill_skbs();
228 repeat:
229
230 skb = alloc_skb(len, GFP_ATOMIC);
231 if (!skb)
232 skb = skb_dequeue(&skb_pool);
233
234 if (!skb) {
235 if (++count < 10) {
236 netpoll_poll(np);
237 goto repeat;
238 }
239 return NULL;
240 }
241
242 atomic_set(&skb->users, 1);
243 skb_reserve(skb, reserve);
244 return skb;
245 }
246
247 static int netpoll_owner_active(struct net_device *dev)
248 {
249 struct napi_struct *napi;
250
251 list_for_each_entry(napi, &dev->napi_list, dev_list) {
252 if (napi->poll_owner == smp_processor_id())
253 return 1;
254 }
255 return 0;
256 }
257
258 void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
259 {
260 int status = NETDEV_TX_BUSY;
261 unsigned long tries;
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;
265
266 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
267 __kfree_skb(skb);
268 return;
269 }
270
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;
274 unsigned long flags;
275
276 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
277
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);
289 }
290 __netif_tx_unlock(txq);
291
292 if (status == NETDEV_TX_OK)
293 break;
294
295 }
296
297 /* tickle device maybe there is some cleanup */
298 netpoll_poll(np);
299
300 udelay(USEC_PER_POLL);
301 }
302
303 WARN_ONCE(!irqs_disabled(),
304 "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n",
305 dev->name, ops->ndo_start_xmit);
306
307 local_irq_restore(flags);
308 }
309
310 if (status != NETDEV_TX_OK) {
311 skb_queue_tail(&npinfo->txq, skb);
312 schedule_delayed_work(&npinfo->tx_work,0);
313 }
314 }
315
316 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
317 {
318 int total_len, eth_len, ip_len, udp_len;
319 struct sk_buff *skb;
320 struct udphdr *udph;
321 struct iphdr *iph;
322 struct ethhdr *eth;
323
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;
327
328 skb = find_skb(np, total_len, total_len - len);
329 if (!skb)
330 return;
331
332 skb_copy_to_linear_data(skb, msg, len);
333 skb->len += len;
334
335 skb_push(skb, sizeof(*udph));
336 skb_reset_transport_header(skb);
337 udph = udp_hdr(skb);
338 udph->source = htons(np->local_port);
339 udph->dest = htons(np->remote_port);
340 udph->len = htons(udp_len);
341 udph->check = 0;
342 udph->check = csum_tcpudp_magic(np->local_ip,
343 np->remote_ip,
344 udp_len, IPPROTO_UDP,
345 csum_partial(udph, udp_len, 0));
346 if (udph->check == 0)
347 udph->check = CSUM_MANGLED_0;
348
349 skb_push(skb, sizeof(*iph));
350 skb_reset_network_header(skb);
351 iph = ip_hdr(skb);
352
353 /* iph->version = 4; iph->ihl = 5; */
354 put_unaligned(0x45, (unsigned char *)iph);
355 iph->tos = 0;
356 put_unaligned(htons(ip_len), &(iph->tot_len));
357 iph->id = 0;
358 iph->frag_off = 0;
359 iph->ttl = 64;
360 iph->protocol = IPPROTO_UDP;
361 iph->check = 0;
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);
365
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);
371
372 skb->dev = np->dev;
373
374 netpoll_send_skb(np, skb);
375 }
376
377 static void arp_reply(struct sk_buff *skb)
378 {
379 struct netpoll_info *npinfo = skb->dev->npinfo;
380 struct arphdr *arp;
381 unsigned char *arp_ptr;
382 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
383 __be32 sip, tip;
384 unsigned char *sha;
385 struct sk_buff *send_skb;
386 struct netpoll *np, *tmp;
387 unsigned long flags;
388 int hits = 0;
389
390 if (list_empty(&npinfo->rx_np))
391 return;
392
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)
398 hits++;
399 }
400 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
401
402 /* No netpoll struct is using this dev */
403 if (!hits)
404 return;
405
406 /* No arp on this interface */
407 if (skb->dev->flags & IFF_NOARP)
408 return;
409
410 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
411 return;
412
413 skb_reset_network_header(skb);
414 skb_reset_transport_header(skb);
415 arp = arp_hdr(skb);
416
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))
421 return;
422
423 arp_ptr = (unsigned char *)(arp+1);
424 /* save the location of the src hw addr */
425 sha = arp_ptr;
426 arp_ptr += skb->dev->addr_len;
427 memcpy(&sip, arp_ptr, 4);
428 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);
433
434 /* Should we ignore arp? */
435 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
436 return;
437
438 size = arp_hdr_len(skb->dev);
439
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)
443 continue;
444
445 send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
446 LL_RESERVED_SPACE(np->dev));
447 if (!send_skb)
448 continue;
449
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);
454
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) {
459 kfree_skb(send_skb);
460 continue;
461 }
462
463 /*
464 * Fill out the arp protocol part.
465 *
466 * we only support ethernet device type,
467 * which (according to RFC 1390) should
468 * always equal 1 (Ethernet).
469 */
470
471 arp->ar_hrd = htons(np->dev->type);
472 arp->ar_pro = htons(ETH_P_IP);
473 arp->ar_hln = np->dev->addr_len;
474 arp->ar_pln = 4;
475 arp->ar_op = htons(type);
476
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);
481 arp_ptr += 4;
482 memcpy(arp_ptr, sha, np->dev->addr_len);
483 arp_ptr += np->dev->addr_len;
484 memcpy(arp_ptr, &sip, 4);
485
486 netpoll_send_skb(np, send_skb);
487
488 /* If there are several rx_hooks for the same address,
489 we're fine by sending a single reply */
490 break;
491 }
492 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
493 }
494
495 int __netpoll_rx(struct sk_buff *skb)
496 {
497 int proto, len, ulen;
498 int hits = 0;
499 struct iphdr *iph;
500 struct udphdr *uh;
501 struct netpoll_info *npinfo = skb->dev->npinfo;
502 struct netpoll *np, *tmp;
503
504 if (list_empty(&npinfo->rx_np))
505 goto out;
506
507 if (skb->dev->type != ARPHRD_ETHER)
508 goto out;
509
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);
514 return 1;
515 }
516
517 proto = ntohs(eth_hdr(skb)->h_proto);
518 if (proto != ETH_P_IP)
519 goto out;
520 if (skb->pkt_type == PACKET_OTHERHOST)
521 goto out;
522 if (skb_shared(skb))
523 goto out;
524
525 iph = (struct iphdr *)skb->data;
526 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
527 goto out;
528 if (iph->ihl < 5 || iph->version != 4)
529 goto out;
530 if (!pskb_may_pull(skb, iph->ihl*4))
531 goto out;
532 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
533 goto out;
534
535 len = ntohs(iph->tot_len);
536 if (skb->len < len || len < iph->ihl*4)
537 goto out;
538
539 /*
540 * Our transport medium may have padded the buffer out.
541 * Now We trim to the true length of the frame.
542 */
543 if (pskb_trim_rcsum(skb, len))
544 goto out;
545
546 if (iph->protocol != IPPROTO_UDP)
547 goto out;
548
549 len -= iph->ihl*4;
550 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
551 ulen = ntohs(uh->len);
552
553 if (ulen != len)
554 goto out;
555 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
556 goto out;
557
558 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
559 if (np->local_ip && np->local_ip != iph->daddr)
560 continue;
561 if (np->remote_ip && np->remote_ip != iph->saddr)
562 continue;
563 if (np->local_port && np->local_port != ntohs(uh->dest))
564 continue;
565
566 np->rx_hook(np, ntohs(uh->source),
567 (char *)(uh+1),
568 ulen - sizeof(struct udphdr));
569 hits++;
570 }
571
572 if (!hits)
573 goto out;
574
575 kfree_skb(skb);
576 return 1;
577
578 out:
579 if (atomic_read(&trapped)) {
580 kfree_skb(skb);
581 return 1;
582 }
583
584 return 0;
585 }
586
587 void netpoll_print_options(struct netpoll *np)
588 {
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);
601 }
602
603 int netpoll_parse_options(struct netpoll *np, char *opt)
604 {
605 char *cur=opt, *delim;
606
607 if (*cur != '@') {
608 if ((delim = strchr(cur, '@')) == NULL)
609 goto parse_failed;
610 *delim = 0;
611 np->local_port = simple_strtol(cur, NULL, 10);
612 cur = delim;
613 }
614 cur++;
615
616 if (*cur != '/') {
617 if ((delim = strchr(cur, '/')) == NULL)
618 goto parse_failed;
619 *delim = 0;
620 np->local_ip = in_aton(cur);
621 cur = delim;
622 }
623 cur++;
624
625 if (*cur != ',') {
626 /* parse out dev name */
627 if ((delim = strchr(cur, ',')) == NULL)
628 goto parse_failed;
629 *delim = 0;
630 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
631 cur = delim;
632 }
633 cur++;
634
635 if (*cur != '@') {
636 /* dst port */
637 if ((delim = strchr(cur, '@')) == NULL)
638 goto parse_failed;
639 *delim = 0;
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);
644 cur = delim;
645 }
646 cur++;
647
648 /* dst ip */
649 if ((delim = strchr(cur, '/')) == NULL)
650 goto parse_failed;
651 *delim = 0;
652 np->remote_ip = in_aton(cur);
653 cur = delim + 1;
654
655 if (*cur != 0) {
656 /* MAC address */
657 if ((delim = strchr(cur, ':')) == NULL)
658 goto parse_failed;
659 *delim = 0;
660 np->remote_mac[0] = simple_strtol(cur, NULL, 16);
661 cur = delim + 1;
662 if ((delim = strchr(cur, ':')) == NULL)
663 goto parse_failed;
664 *delim = 0;
665 np->remote_mac[1] = simple_strtol(cur, NULL, 16);
666 cur = delim + 1;
667 if ((delim = strchr(cur, ':')) == NULL)
668 goto parse_failed;
669 *delim = 0;
670 np->remote_mac[2] = simple_strtol(cur, NULL, 16);
671 cur = delim + 1;
672 if ((delim = strchr(cur, ':')) == NULL)
673 goto parse_failed;
674 *delim = 0;
675 np->remote_mac[3] = simple_strtol(cur, NULL, 16);
676 cur = delim + 1;
677 if ((delim = strchr(cur, ':')) == NULL)
678 goto parse_failed;
679 *delim = 0;
680 np->remote_mac[4] = simple_strtol(cur, NULL, 16);
681 cur = delim + 1;
682 np->remote_mac[5] = simple_strtol(cur, NULL, 16);
683 }
684
685 netpoll_print_options(np);
686
687 return 0;
688
689 parse_failed:
690 printk(KERN_INFO "%s: couldn't parse config at '%s'!\n",
691 np->name, cur);
692 return -1;
693 }
694
695 int netpoll_setup(struct netpoll *np)
696 {
697 struct net_device *ndev = NULL;
698 struct in_device *in_dev;
699 struct netpoll_info *npinfo;
700 struct netpoll *npe, *tmp;
701 unsigned long flags;
702 int err;
703
704 if (np->dev_name)
705 ndev = dev_get_by_name(&init_net, np->dev_name);
706 if (!ndev) {
707 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
708 np->name, np->dev_name);
709 return -ENODEV;
710 }
711
712 np->dev = ndev;
713 if (!ndev->npinfo) {
714 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
715 if (!npinfo) {
716 err = -ENOMEM;
717 goto put;
718 }
719
720 npinfo->rx_flags = 0;
721 INIT_LIST_HEAD(&npinfo->rx_np);
722
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);
727
728 atomic_set(&npinfo->refcnt, 1);
729 } else {
730 npinfo = ndev->npinfo;
731 atomic_inc(&npinfo->refcnt);
732 }
733
734 npinfo->netpoll = np;
735
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);
740 err = -ENOTSUPP;
741 goto release;
742 }
743
744 if (!netif_running(ndev)) {
745 unsigned long atmost, atleast;
746
747 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
748 np->name, np->dev_name);
749
750 rtnl_lock();
751 err = dev_open(ndev);
752 rtnl_unlock();
753
754 if (err) {
755 printk(KERN_ERR "%s: failed to open %s\n",
756 np->name, ndev->name);
757 goto release;
758 }
759
760 atleast = jiffies + HZ/10;
761 atmost = jiffies + carrier_timeout * HZ;
762 while (!netif_carrier_ok(ndev)) {
763 if (time_after(jiffies, atmost)) {
764 printk(KERN_NOTICE
765 "%s: timeout waiting for carrier\n",
766 np->name);
767 break;
768 }
769 msleep(1);
770 }
771
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.
775 */
776
777 if (time_before(jiffies, atleast)) {
778 printk(KERN_NOTICE "%s: carrier detect appears"
779 " untrustworthy, waiting 4 seconds\n",
780 np->name);
781 msleep(4000);
782 }
783 }
784
785 if (!np->local_ip) {
786 rcu_read_lock();
787 in_dev = __in_dev_get_rcu(ndev);
788
789 if (!in_dev || !in_dev->ifa_list) {
790 rcu_read_unlock();
791 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
792 np->name, np->dev_name);
793 err = -EDESTADDRREQ;
794 goto release;
795 }
796
797 np->local_ip = in_dev->ifa_list->ifa_local;
798 rcu_read_unlock();
799 printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
800 }
801
802 if (np->rx_hook) {
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);
807 }
808
809 /* fill up the skb queue */
810 refill_skbs();
811
812 /* last thing to do is link it to the net device structure */
813 ndev->npinfo = npinfo;
814
815 /* avoid racing with NAPI reading npinfo */
816 synchronize_rcu();
817
818 return 0;
819
820 release:
821 if (!ndev->npinfo) {
822 spin_lock_irqsave(&npinfo->rx_lock, flags);
823 list_for_each_entry_safe(npe, tmp, &npinfo->rx_np, rx) {
824 npe->dev = NULL;
825 }
826 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
827
828 kfree(npinfo);
829 }
830 put:
831 dev_put(ndev);
832 return err;
833 }
834
835 static int __init netpoll_init(void)
836 {
837 skb_queue_head_init(&skb_pool);
838 return 0;
839 }
840 core_initcall(netpoll_init);
841
842 void netpoll_cleanup(struct netpoll *np)
843 {
844 struct netpoll_info *npinfo;
845 unsigned long flags;
846
847 if (np->dev) {
848 npinfo = np->dev->npinfo;
849 if (npinfo) {
850 if (!list_empty(&npinfo->rx_np)) {
851 spin_lock_irqsave(&npinfo->rx_lock, flags);
852 list_del(&np->rx);
853 if (list_empty(&npinfo->rx_np))
854 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
855 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
856 }
857
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);
863
864 /* clean after last, unfinished work */
865 __skb_queue_purge(&npinfo->txq);
866 kfree(npinfo);
867 ops = np->dev->netdev_ops;
868 if (ops->ndo_netpoll_cleanup)
869 ops->ndo_netpoll_cleanup(np->dev);
870 np->dev->npinfo = NULL;
871 }
872 }
873
874 dev_put(np->dev);
875 }
876
877 np->dev = NULL;
878 }
879
880 int netpoll_trap(void)
881 {
882 return atomic_read(&trapped);
883 }
884
885 void netpoll_set_trap(int trap)
886 {
887 if (trap)
888 atomic_inc(&trapped);
889 else
890 atomic_dec(&trapped);
891 }
892
893 EXPORT_SYMBOL(netpoll_send_skb);
894 EXPORT_SYMBOL(netpoll_set_trap);
895 EXPORT_SYMBOL(netpoll_trap);
896 EXPORT_SYMBOL(netpoll_print_options);
897 EXPORT_SYMBOL(netpoll_parse_options);
898 EXPORT_SYMBOL(netpoll_setup);
899 EXPORT_SYMBOL(netpoll_cleanup);
900 EXPORT_SYMBOL(netpoll_send_udp);
901 EXPORT_SYMBOL(netpoll_poll_dev);
902 EXPORT_SYMBOL(netpoll_poll);
This page took 0.050151 seconds and 6 git commands to generate.