netpoll: take rcu_read_lock_bh() in netpoll_send_skb_on_dev()
[platform/adaptation/renesas_rcar/renesas_kernel.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/moduleparam.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/string.h>
18 #include <linux/if_arp.h>
19 #include <linux/inetdevice.h>
20 #include <linux/inet.h>
21 #include <linux/interrupt.h>
22 #include <linux/netpoll.h>
23 #include <linux/sched.h>
24 #include <linux/delay.h>
25 #include <linux/rcupdate.h>
26 #include <linux/workqueue.h>
27 #include <linux/slab.h>
28 #include <linux/export.h>
29 #include <net/tcp.h>
30 #include <net/udp.h>
31 #include <asm/unaligned.h>
32 #include <trace/events/napi.h>
33
34 /*
35  * We maintain a small pool of fully-sized skbs, to make sure the
36  * message gets out even in extreme OOM situations.
37  */
38
39 #define MAX_UDP_CHUNK 1460
40 #define MAX_SKBS 32
41
42 static struct sk_buff_head skb_pool;
43
44 static atomic_t trapped;
45
46 #define USEC_PER_POLL   50
47 #define NETPOLL_RX_ENABLED  1
48 #define NETPOLL_RX_DROP     2
49
50 #define MAX_SKB_SIZE                                                    \
51         (sizeof(struct ethhdr) +                                        \
52          sizeof(struct iphdr) +                                         \
53          sizeof(struct udphdr) +                                        \
54          MAX_UDP_CHUNK)
55
56 static void zap_completion_queue(void);
57 static void netpoll_arp_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
58
59 static unsigned int carrier_timeout = 4;
60 module_param(carrier_timeout, uint, 0644);
61
62 #define np_info(np, fmt, ...)                           \
63         pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
64 #define np_err(np, fmt, ...)                            \
65         pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
66 #define np_notice(np, fmt, ...)                         \
67         pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
68
69 static void queue_process(struct work_struct *work)
70 {
71         struct netpoll_info *npinfo =
72                 container_of(work, struct netpoll_info, tx_work.work);
73         struct sk_buff *skb;
74         unsigned long flags;
75
76         while ((skb = skb_dequeue(&npinfo->txq))) {
77                 struct net_device *dev = skb->dev;
78                 const struct net_device_ops *ops = dev->netdev_ops;
79                 struct netdev_queue *txq;
80
81                 if (!netif_device_present(dev) || !netif_running(dev)) {
82                         __kfree_skb(skb);
83                         continue;
84                 }
85
86                 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
87
88                 local_irq_save(flags);
89                 __netif_tx_lock(txq, smp_processor_id());
90                 if (netif_xmit_frozen_or_stopped(txq) ||
91                     ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
92                         skb_queue_head(&npinfo->txq, skb);
93                         __netif_tx_unlock(txq);
94                         local_irq_restore(flags);
95
96                         schedule_delayed_work(&npinfo->tx_work, HZ/10);
97                         return;
98                 }
99                 __netif_tx_unlock(txq);
100                 local_irq_restore(flags);
101         }
102 }
103
104 static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
105                             unsigned short ulen, __be32 saddr, __be32 daddr)
106 {
107         __wsum psum;
108
109         if (uh->check == 0 || skb_csum_unnecessary(skb))
110                 return 0;
111
112         psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
113
114         if (skb->ip_summed == CHECKSUM_COMPLETE &&
115             !csum_fold(csum_add(psum, skb->csum)))
116                 return 0;
117
118         skb->csum = psum;
119
120         return __skb_checksum_complete(skb);
121 }
122
123 /*
124  * Check whether delayed processing was scheduled for our NIC. If so,
125  * we attempt to grab the poll lock and use ->poll() to pump the card.
126  * If this fails, either we've recursed in ->poll() or it's already
127  * running on another CPU.
128  *
129  * Note: we don't mask interrupts with this lock because we're using
130  * trylock here and interrupts are already disabled in the softirq
131  * case. Further, we test the poll_owner to avoid recursion on UP
132  * systems where the lock doesn't exist.
133  *
134  * In cases where there is bi-directional communications, reading only
135  * one message at a time can lead to packets being dropped by the
136  * network adapter, forcing superfluous retries and possibly timeouts.
137  * Thus, we set our budget to greater than 1.
138  */
139 static int poll_one_napi(struct netpoll_info *npinfo,
140                          struct napi_struct *napi, int budget)
141 {
142         int work;
143
144         /* net_rx_action's ->poll() invocations and our's are
145          * synchronized by this test which is only made while
146          * holding the napi->poll_lock.
147          */
148         if (!test_bit(NAPI_STATE_SCHED, &napi->state))
149                 return budget;
150
151         npinfo->rx_flags |= NETPOLL_RX_DROP;
152         atomic_inc(&trapped);
153         set_bit(NAPI_STATE_NPSVC, &napi->state);
154
155         work = napi->poll(napi, budget);
156         trace_napi_poll(napi);
157
158         clear_bit(NAPI_STATE_NPSVC, &napi->state);
159         atomic_dec(&trapped);
160         npinfo->rx_flags &= ~NETPOLL_RX_DROP;
161
162         return budget - work;
163 }
164
165 static void poll_napi(struct net_device *dev)
166 {
167         struct napi_struct *napi;
168         int budget = 16;
169
170         list_for_each_entry(napi, &dev->napi_list, dev_list) {
171                 if (napi->poll_owner != smp_processor_id() &&
172                     spin_trylock(&napi->poll_lock)) {
173                         budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
174                                                napi, budget);
175                         spin_unlock(&napi->poll_lock);
176
177                         if (!budget)
178                                 break;
179                 }
180         }
181 }
182
183 static void service_arp_queue(struct netpoll_info *npi)
184 {
185         if (npi) {
186                 struct sk_buff *skb;
187
188                 while ((skb = skb_dequeue(&npi->arp_tx)))
189                         netpoll_arp_reply(skb, npi);
190         }
191 }
192
193 static void netpoll_poll_dev(struct net_device *dev)
194 {
195         const struct net_device_ops *ops;
196         struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
197
198         if (!dev || !netif_running(dev))
199                 return;
200
201         ops = dev->netdev_ops;
202         if (!ops->ndo_poll_controller)
203                 return;
204
205         /* Process pending work on NIC */
206         ops->ndo_poll_controller(dev);
207
208         poll_napi(dev);
209
210         if (dev->flags & IFF_SLAVE) {
211                 if (ni) {
212                         struct net_device *bond_dev = dev->master;
213                         struct sk_buff *skb;
214                         struct netpoll_info *bond_ni = rcu_dereference_bh(bond_dev->npinfo);
215                         while ((skb = skb_dequeue(&ni->arp_tx))) {
216                                 skb->dev = bond_dev;
217                                 skb_queue_tail(&bond_ni->arp_tx, skb);
218                         }
219                 }
220         }
221
222         service_arp_queue(ni);
223
224         zap_completion_queue();
225 }
226
227 static void refill_skbs(void)
228 {
229         struct sk_buff *skb;
230         unsigned long flags;
231
232         spin_lock_irqsave(&skb_pool.lock, flags);
233         while (skb_pool.qlen < MAX_SKBS) {
234                 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
235                 if (!skb)
236                         break;
237
238                 __skb_queue_tail(&skb_pool, skb);
239         }
240         spin_unlock_irqrestore(&skb_pool.lock, flags);
241 }
242
243 static void zap_completion_queue(void)
244 {
245         unsigned long flags;
246         struct softnet_data *sd = &get_cpu_var(softnet_data);
247
248         if (sd->completion_queue) {
249                 struct sk_buff *clist;
250
251                 local_irq_save(flags);
252                 clist = sd->completion_queue;
253                 sd->completion_queue = NULL;
254                 local_irq_restore(flags);
255
256                 while (clist != NULL) {
257                         struct sk_buff *skb = clist;
258                         clist = clist->next;
259                         if (skb->destructor) {
260                                 atomic_inc(&skb->users);
261                                 dev_kfree_skb_any(skb); /* put this one back */
262                         } else {
263                                 __kfree_skb(skb);
264                         }
265                 }
266         }
267
268         put_cpu_var(softnet_data);
269 }
270
271 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
272 {
273         int count = 0;
274         struct sk_buff *skb;
275
276         zap_completion_queue();
277         refill_skbs();
278 repeat:
279
280         skb = alloc_skb(len, GFP_ATOMIC);
281         if (!skb)
282                 skb = skb_dequeue(&skb_pool);
283
284         if (!skb) {
285                 if (++count < 10) {
286                         netpoll_poll_dev(np->dev);
287                         goto repeat;
288                 }
289                 return NULL;
290         }
291
292         atomic_set(&skb->users, 1);
293         skb_reserve(skb, reserve);
294         return skb;
295 }
296
297 static int netpoll_owner_active(struct net_device *dev)
298 {
299         struct napi_struct *napi;
300
301         list_for_each_entry(napi, &dev->napi_list, dev_list) {
302                 if (napi->poll_owner == smp_processor_id())
303                         return 1;
304         }
305         return 0;
306 }
307
308 /* call with IRQ disabled */
309 void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
310                              struct net_device *dev)
311 {
312         int status = NETDEV_TX_BUSY;
313         unsigned long tries;
314         const struct net_device_ops *ops = dev->netdev_ops;
315         /* It is up to the caller to keep npinfo alive. */
316         struct netpoll_info *npinfo;
317
318         WARN_ON_ONCE(!irqs_disabled());
319
320         npinfo = rcu_dereference_bh(np->dev->npinfo);
321         if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
322                 __kfree_skb(skb);
323                 return;
324         }
325
326         /* don't get messages out of order, and no recursion */
327         if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
328                 struct netdev_queue *txq;
329
330                 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
331
332                 /* try until next clock tick */
333                 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
334                      tries > 0; --tries) {
335                         if (__netif_tx_trylock(txq)) {
336                                 if (!netif_xmit_stopped(txq)) {
337                                         status = ops->ndo_start_xmit(skb, dev);
338                                         if (status == NETDEV_TX_OK)
339                                                 txq_trans_update(txq);
340                                 }
341                                 __netif_tx_unlock(txq);
342
343                                 if (status == NETDEV_TX_OK)
344                                         break;
345
346                         }
347
348                         /* tickle device maybe there is some cleanup */
349                         netpoll_poll_dev(np->dev);
350
351                         udelay(USEC_PER_POLL);
352                 }
353
354                 WARN_ONCE(!irqs_disabled(),
355                         "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
356                         dev->name, ops->ndo_start_xmit);
357
358         }
359
360         if (status != NETDEV_TX_OK) {
361                 skb_queue_tail(&npinfo->txq, skb);
362                 schedule_delayed_work(&npinfo->tx_work,0);
363         }
364 }
365 EXPORT_SYMBOL(netpoll_send_skb_on_dev);
366
367 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
368 {
369         int total_len, ip_len, udp_len;
370         struct sk_buff *skb;
371         struct udphdr *udph;
372         struct iphdr *iph;
373         struct ethhdr *eth;
374
375         udp_len = len + sizeof(*udph);
376         ip_len = udp_len + sizeof(*iph);
377         total_len = ip_len + LL_RESERVED_SPACE(np->dev);
378
379         skb = find_skb(np, total_len + np->dev->needed_tailroom,
380                        total_len - len);
381         if (!skb)
382                 return;
383
384         skb_copy_to_linear_data(skb, msg, len);
385         skb_put(skb, len);
386
387         skb_push(skb, sizeof(*udph));
388         skb_reset_transport_header(skb);
389         udph = udp_hdr(skb);
390         udph->source = htons(np->local_port);
391         udph->dest = htons(np->remote_port);
392         udph->len = htons(udp_len);
393         udph->check = 0;
394         udph->check = csum_tcpudp_magic(np->local_ip,
395                                         np->remote_ip,
396                                         udp_len, IPPROTO_UDP,
397                                         csum_partial(udph, udp_len, 0));
398         if (udph->check == 0)
399                 udph->check = CSUM_MANGLED_0;
400
401         skb_push(skb, sizeof(*iph));
402         skb_reset_network_header(skb);
403         iph = ip_hdr(skb);
404
405         /* iph->version = 4; iph->ihl = 5; */
406         put_unaligned(0x45, (unsigned char *)iph);
407         iph->tos      = 0;
408         put_unaligned(htons(ip_len), &(iph->tot_len));
409         iph->id       = 0;
410         iph->frag_off = 0;
411         iph->ttl      = 64;
412         iph->protocol = IPPROTO_UDP;
413         iph->check    = 0;
414         put_unaligned(np->local_ip, &(iph->saddr));
415         put_unaligned(np->remote_ip, &(iph->daddr));
416         iph->check    = ip_fast_csum((unsigned char *)iph, iph->ihl);
417
418         eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
419         skb_reset_mac_header(skb);
420         skb->protocol = eth->h_proto = htons(ETH_P_IP);
421         memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
422         memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
423
424         skb->dev = np->dev;
425
426         netpoll_send_skb(np, skb);
427 }
428 EXPORT_SYMBOL(netpoll_send_udp);
429
430 static void netpoll_arp_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
431 {
432         struct arphdr *arp;
433         unsigned char *arp_ptr;
434         int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
435         __be32 sip, tip;
436         unsigned char *sha;
437         struct sk_buff *send_skb;
438         struct netpoll *np, *tmp;
439         unsigned long flags;
440         int hlen, tlen;
441         int hits = 0;
442
443         if (list_empty(&npinfo->rx_np))
444                 return;
445
446         /* Before checking the packet, we do some early
447            inspection whether this is interesting at all */
448         spin_lock_irqsave(&npinfo->rx_lock, flags);
449         list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
450                 if (np->dev == skb->dev)
451                         hits++;
452         }
453         spin_unlock_irqrestore(&npinfo->rx_lock, flags);
454
455         /* No netpoll struct is using this dev */
456         if (!hits)
457                 return;
458
459         /* No arp on this interface */
460         if (skb->dev->flags & IFF_NOARP)
461                 return;
462
463         if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
464                 return;
465
466         skb_reset_network_header(skb);
467         skb_reset_transport_header(skb);
468         arp = arp_hdr(skb);
469
470         if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
471              arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
472             arp->ar_pro != htons(ETH_P_IP) ||
473             arp->ar_op != htons(ARPOP_REQUEST))
474                 return;
475
476         arp_ptr = (unsigned char *)(arp+1);
477         /* save the location of the src hw addr */
478         sha = arp_ptr;
479         arp_ptr += skb->dev->addr_len;
480         memcpy(&sip, arp_ptr, 4);
481         arp_ptr += 4;
482         /* If we actually cared about dst hw addr,
483            it would get copied here */
484         arp_ptr += skb->dev->addr_len;
485         memcpy(&tip, arp_ptr, 4);
486
487         /* Should we ignore arp? */
488         if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
489                 return;
490
491         size = arp_hdr_len(skb->dev);
492
493         spin_lock_irqsave(&npinfo->rx_lock, flags);
494         list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
495                 if (tip != np->local_ip)
496                         continue;
497
498                 hlen = LL_RESERVED_SPACE(np->dev);
499                 tlen = np->dev->needed_tailroom;
500                 send_skb = find_skb(np, size + hlen + tlen, hlen);
501                 if (!send_skb)
502                         continue;
503
504                 skb_reset_network_header(send_skb);
505                 arp = (struct arphdr *) skb_put(send_skb, size);
506                 send_skb->dev = skb->dev;
507                 send_skb->protocol = htons(ETH_P_ARP);
508
509                 /* Fill the device header for the ARP frame */
510                 if (dev_hard_header(send_skb, skb->dev, ptype,
511                                     sha, np->dev->dev_addr,
512                                     send_skb->len) < 0) {
513                         kfree_skb(send_skb);
514                         continue;
515                 }
516
517                 /*
518                  * Fill out the arp protocol part.
519                  *
520                  * we only support ethernet device type,
521                  * which (according to RFC 1390) should
522                  * always equal 1 (Ethernet).
523                  */
524
525                 arp->ar_hrd = htons(np->dev->type);
526                 arp->ar_pro = htons(ETH_P_IP);
527                 arp->ar_hln = np->dev->addr_len;
528                 arp->ar_pln = 4;
529                 arp->ar_op = htons(type);
530
531                 arp_ptr = (unsigned char *)(arp + 1);
532                 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
533                 arp_ptr += np->dev->addr_len;
534                 memcpy(arp_ptr, &tip, 4);
535                 arp_ptr += 4;
536                 memcpy(arp_ptr, sha, np->dev->addr_len);
537                 arp_ptr += np->dev->addr_len;
538                 memcpy(arp_ptr, &sip, 4);
539
540                 netpoll_send_skb(np, send_skb);
541
542                 /* If there are several rx_hooks for the same address,
543                    we're fine by sending a single reply */
544                 break;
545         }
546         spin_unlock_irqrestore(&npinfo->rx_lock, flags);
547 }
548
549 int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
550 {
551         int proto, len, ulen;
552         int hits = 0;
553         const struct iphdr *iph;
554         struct udphdr *uh;
555         struct netpoll *np, *tmp;
556
557         if (list_empty(&npinfo->rx_np))
558                 goto out;
559
560         if (skb->dev->type != ARPHRD_ETHER)
561                 goto out;
562
563         /* check if netpoll clients need ARP */
564         if (skb->protocol == htons(ETH_P_ARP) &&
565             atomic_read(&trapped)) {
566                 skb_queue_tail(&npinfo->arp_tx, skb);
567                 return 1;
568         }
569
570         proto = ntohs(eth_hdr(skb)->h_proto);
571         if (proto != ETH_P_IP)
572                 goto out;
573         if (skb->pkt_type == PACKET_OTHERHOST)
574                 goto out;
575         if (skb_shared(skb))
576                 goto out;
577
578         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
579                 goto out;
580         iph = (struct iphdr *)skb->data;
581         if (iph->ihl < 5 || iph->version != 4)
582                 goto out;
583         if (!pskb_may_pull(skb, iph->ihl*4))
584                 goto out;
585         iph = (struct iphdr *)skb->data;
586         if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
587                 goto out;
588
589         len = ntohs(iph->tot_len);
590         if (skb->len < len || len < iph->ihl*4)
591                 goto out;
592
593         /*
594          * Our transport medium may have padded the buffer out.
595          * Now We trim to the true length of the frame.
596          */
597         if (pskb_trim_rcsum(skb, len))
598                 goto out;
599
600         iph = (struct iphdr *)skb->data;
601         if (iph->protocol != IPPROTO_UDP)
602                 goto out;
603
604         len -= iph->ihl*4;
605         uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
606         ulen = ntohs(uh->len);
607
608         if (ulen != len)
609                 goto out;
610         if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
611                 goto out;
612
613         list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
614                 if (np->local_ip && np->local_ip != iph->daddr)
615                         continue;
616                 if (np->remote_ip && np->remote_ip != iph->saddr)
617                         continue;
618                 if (np->local_port && np->local_port != ntohs(uh->dest))
619                         continue;
620
621                 np->rx_hook(np, ntohs(uh->source),
622                                (char *)(uh+1),
623                                ulen - sizeof(struct udphdr));
624                 hits++;
625         }
626
627         if (!hits)
628                 goto out;
629
630         kfree_skb(skb);
631         return 1;
632
633 out:
634         if (atomic_read(&trapped)) {
635                 kfree_skb(skb);
636                 return 1;
637         }
638
639         return 0;
640 }
641
642 void netpoll_print_options(struct netpoll *np)
643 {
644         np_info(np, "local port %d\n", np->local_port);
645         np_info(np, "local IP %pI4\n", &np->local_ip);
646         np_info(np, "interface '%s'\n", np->dev_name);
647         np_info(np, "remote port %d\n", np->remote_port);
648         np_info(np, "remote IP %pI4\n", &np->remote_ip);
649         np_info(np, "remote ethernet address %pM\n", np->remote_mac);
650 }
651 EXPORT_SYMBOL(netpoll_print_options);
652
653 int netpoll_parse_options(struct netpoll *np, char *opt)
654 {
655         char *cur=opt, *delim;
656
657         if (*cur != '@') {
658                 if ((delim = strchr(cur, '@')) == NULL)
659                         goto parse_failed;
660                 *delim = 0;
661                 np->local_port = simple_strtol(cur, NULL, 10);
662                 cur = delim;
663         }
664         cur++;
665
666         if (*cur != '/') {
667                 if ((delim = strchr(cur, '/')) == NULL)
668                         goto parse_failed;
669                 *delim = 0;
670                 np->local_ip = in_aton(cur);
671                 cur = delim;
672         }
673         cur++;
674
675         if (*cur != ',') {
676                 /* parse out dev name */
677                 if ((delim = strchr(cur, ',')) == NULL)
678                         goto parse_failed;
679                 *delim = 0;
680                 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
681                 cur = delim;
682         }
683         cur++;
684
685         if (*cur != '@') {
686                 /* dst port */
687                 if ((delim = strchr(cur, '@')) == NULL)
688                         goto parse_failed;
689                 *delim = 0;
690                 if (*cur == ' ' || *cur == '\t')
691                         np_info(np, "warning: whitespace is not allowed\n");
692                 np->remote_port = simple_strtol(cur, NULL, 10);
693                 cur = delim;
694         }
695         cur++;
696
697         /* dst ip */
698         if ((delim = strchr(cur, '/')) == NULL)
699                 goto parse_failed;
700         *delim = 0;
701         np->remote_ip = in_aton(cur);
702         cur = delim + 1;
703
704         if (*cur != 0) {
705                 /* MAC address */
706                 if (!mac_pton(cur, np->remote_mac))
707                         goto parse_failed;
708         }
709
710         netpoll_print_options(np);
711
712         return 0;
713
714  parse_failed:
715         np_info(np, "couldn't parse config at '%s'!\n", cur);
716         return -1;
717 }
718 EXPORT_SYMBOL(netpoll_parse_options);
719
720 int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
721 {
722         struct netpoll_info *npinfo;
723         const struct net_device_ops *ops;
724         unsigned long flags;
725         int err;
726
727         np->dev = ndev;
728         strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
729
730         if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
731             !ndev->netdev_ops->ndo_poll_controller) {
732                 np_err(np, "%s doesn't support polling, aborting\n",
733                        np->dev_name);
734                 err = -ENOTSUPP;
735                 goto out;
736         }
737
738         if (!ndev->npinfo) {
739                 npinfo = kmalloc(sizeof(*npinfo), gfp);
740                 if (!npinfo) {
741                         err = -ENOMEM;
742                         goto out;
743                 }
744
745                 npinfo->rx_flags = 0;
746                 INIT_LIST_HEAD(&npinfo->rx_np);
747
748                 spin_lock_init(&npinfo->rx_lock);
749                 skb_queue_head_init(&npinfo->arp_tx);
750                 skb_queue_head_init(&npinfo->txq);
751                 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
752
753                 atomic_set(&npinfo->refcnt, 1);
754
755                 ops = np->dev->netdev_ops;
756                 if (ops->ndo_netpoll_setup) {
757                         err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
758                         if (err)
759                                 goto free_npinfo;
760                 }
761         } else {
762                 npinfo = ndev->npinfo;
763                 atomic_inc(&npinfo->refcnt);
764         }
765
766         npinfo->netpoll = np;
767
768         if (np->rx_hook) {
769                 spin_lock_irqsave(&npinfo->rx_lock, flags);
770                 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
771                 list_add_tail(&np->rx, &npinfo->rx_np);
772                 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
773         }
774
775         /* last thing to do is link it to the net device structure */
776         rcu_assign_pointer(ndev->npinfo, npinfo);
777
778         return 0;
779
780 free_npinfo:
781         kfree(npinfo);
782 out:
783         return err;
784 }
785 EXPORT_SYMBOL_GPL(__netpoll_setup);
786
787 int netpoll_setup(struct netpoll *np)
788 {
789         struct net_device *ndev = NULL;
790         struct in_device *in_dev;
791         int err;
792
793         if (np->dev_name)
794                 ndev = dev_get_by_name(&init_net, np->dev_name);
795         if (!ndev) {
796                 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
797                 return -ENODEV;
798         }
799
800         if (ndev->master) {
801                 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
802                 err = -EBUSY;
803                 goto put;
804         }
805
806         if (!netif_running(ndev)) {
807                 unsigned long atmost, atleast;
808
809                 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
810
811                 rtnl_lock();
812                 err = dev_open(ndev);
813                 rtnl_unlock();
814
815                 if (err) {
816                         np_err(np, "failed to open %s\n", ndev->name);
817                         goto put;
818                 }
819
820                 atleast = jiffies + HZ/10;
821                 atmost = jiffies + carrier_timeout * HZ;
822                 while (!netif_carrier_ok(ndev)) {
823                         if (time_after(jiffies, atmost)) {
824                                 np_notice(np, "timeout waiting for carrier\n");
825                                 break;
826                         }
827                         msleep(1);
828                 }
829
830                 /* If carrier appears to come up instantly, we don't
831                  * trust it and pause so that we don't pump all our
832                  * queued console messages into the bitbucket.
833                  */
834
835                 if (time_before(jiffies, atleast)) {
836                         np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
837                         msleep(4000);
838                 }
839         }
840
841         if (!np->local_ip) {
842                 rcu_read_lock();
843                 in_dev = __in_dev_get_rcu(ndev);
844
845                 if (!in_dev || !in_dev->ifa_list) {
846                         rcu_read_unlock();
847                         np_err(np, "no IP address for %s, aborting\n",
848                                np->dev_name);
849                         err = -EDESTADDRREQ;
850                         goto put;
851                 }
852
853                 np->local_ip = in_dev->ifa_list->ifa_local;
854                 rcu_read_unlock();
855                 np_info(np, "local IP %pI4\n", &np->local_ip);
856         }
857
858         /* fill up the skb queue */
859         refill_skbs();
860
861         rtnl_lock();
862         err = __netpoll_setup(np, ndev, GFP_KERNEL);
863         rtnl_unlock();
864
865         if (err)
866                 goto put;
867
868         return 0;
869
870 put:
871         dev_put(ndev);
872         return err;
873 }
874 EXPORT_SYMBOL(netpoll_setup);
875
876 static int __init netpoll_init(void)
877 {
878         skb_queue_head_init(&skb_pool);
879         return 0;
880 }
881 core_initcall(netpoll_init);
882
883 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
884 {
885         struct netpoll_info *npinfo =
886                         container_of(rcu_head, struct netpoll_info, rcu);
887
888         skb_queue_purge(&npinfo->arp_tx);
889         skb_queue_purge(&npinfo->txq);
890
891         /* we can't call cancel_delayed_work_sync here, as we are in softirq */
892         cancel_delayed_work(&npinfo->tx_work);
893
894         /* clean after last, unfinished work */
895         __skb_queue_purge(&npinfo->txq);
896         /* now cancel it again */
897         cancel_delayed_work(&npinfo->tx_work);
898         kfree(npinfo);
899 }
900
901 void __netpoll_cleanup(struct netpoll *np)
902 {
903         struct netpoll_info *npinfo;
904         unsigned long flags;
905
906         npinfo = np->dev->npinfo;
907         if (!npinfo)
908                 return;
909
910         if (!list_empty(&npinfo->rx_np)) {
911                 spin_lock_irqsave(&npinfo->rx_lock, flags);
912                 list_del(&np->rx);
913                 if (list_empty(&npinfo->rx_np))
914                         npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
915                 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
916         }
917
918         if (atomic_dec_and_test(&npinfo->refcnt)) {
919                 const struct net_device_ops *ops;
920
921                 ops = np->dev->netdev_ops;
922                 if (ops->ndo_netpoll_cleanup)
923                         ops->ndo_netpoll_cleanup(np->dev);
924
925                 RCU_INIT_POINTER(np->dev->npinfo, NULL);
926                 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
927         }
928 }
929 EXPORT_SYMBOL_GPL(__netpoll_cleanup);
930
931 static void rcu_cleanup_netpoll(struct rcu_head *rcu_head)
932 {
933         struct netpoll *np = container_of(rcu_head, struct netpoll, rcu);
934
935         __netpoll_cleanup(np);
936         kfree(np);
937 }
938
939 void __netpoll_free_rcu(struct netpoll *np)
940 {
941         call_rcu_bh(&np->rcu, rcu_cleanup_netpoll);
942 }
943 EXPORT_SYMBOL_GPL(__netpoll_free_rcu);
944
945 void netpoll_cleanup(struct netpoll *np)
946 {
947         if (!np->dev)
948                 return;
949
950         rtnl_lock();
951         __netpoll_cleanup(np);
952         rtnl_unlock();
953
954         dev_put(np->dev);
955         np->dev = NULL;
956 }
957 EXPORT_SYMBOL(netpoll_cleanup);
958
959 int netpoll_trap(void)
960 {
961         return atomic_read(&trapped);
962 }
963 EXPORT_SYMBOL(netpoll_trap);
964
965 void netpoll_set_trap(int trap)
966 {
967         if (trap)
968                 atomic_inc(&trapped);
969         else
970                 atomic_dec(&trapped);
971 }
972 EXPORT_SYMBOL(netpoll_set_trap);