Merge branch 'qed-next'
[linux-2.6-block.git] / net / core / netpoll.c
CommitLineData
1da177e4
LT
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
e6ec2693
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
bff38771 14#include <linux/moduleparam.h>
4cd5773a 15#include <linux/kernel.h>
1da177e4
LT
16#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/string.h>
14c85021 19#include <linux/if_arp.h>
1da177e4
LT
20#include <linux/inetdevice.h>
21#include <linux/inet.h>
22#include <linux/interrupt.h>
23#include <linux/netpoll.h>
24#include <linux/sched.h>
25#include <linux/delay.h>
26#include <linux/rcupdate.h>
27#include <linux/workqueue.h>
5a0e3ad6 28#include <linux/slab.h>
bc3b2d7f 29#include <linux/export.h>
689971b4 30#include <linux/if_vlan.h>
1da177e4
LT
31#include <net/tcp.h>
32#include <net/udp.h>
b3d936f3
CW
33#include <net/addrconf.h>
34#include <net/ndisc.h>
35#include <net/ip6_checksum.h>
1da177e4 36#include <asm/unaligned.h>
9cbc1cb8 37#include <trace/events/napi.h>
1da177e4
LT
38
39/*
40 * We maintain a small pool of fully-sized skbs, to make sure the
41 * message gets out even in extreme OOM situations.
42 */
43
44#define MAX_UDP_CHUNK 1460
45#define MAX_SKBS 32
1da177e4 46
a1bcfacd 47static struct sk_buff_head skb_pool;
1da177e4 48
7f9421c2 49DEFINE_STATIC_SRCU(netpoll_srcu);
ca99ca14 50
2bdfe0ba 51#define USEC_PER_POLL 50
1da177e4 52
6f706245
JP
53#define MAX_SKB_SIZE \
54 (sizeof(struct ethhdr) + \
55 sizeof(struct iphdr) + \
56 sizeof(struct udphdr) + \
57 MAX_UDP_CHUNK)
1da177e4 58
3578b0c8 59static void zap_completion_queue(void);
1da177e4 60
bff38771
AV
61static unsigned int carrier_timeout = 4;
62module_param(carrier_timeout, uint, 0644);
63
e6ec2693
JP
64#define np_info(np, fmt, ...) \
65 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
66#define np_err(np, fmt, ...) \
67 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
68#define np_notice(np, fmt, ...) \
69 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
70
944e2948
EB
71static int netpoll_start_xmit(struct sk_buff *skb, struct net_device *dev,
72 struct netdev_queue *txq)
73{
944e2948
EB
74 int status = NETDEV_TX_OK;
75 netdev_features_t features;
76
77 features = netif_skb_features(skb);
78
df8a39de 79 if (skb_vlan_tag_present(skb) &&
944e2948 80 !vlan_hw_offload_capable(features, skb->vlan_proto)) {
5968250c 81 skb = __vlan_hwaccel_push_inside(skb);
944e2948
EB
82 if (unlikely(!skb)) {
83 /* This is actually a packet drop, but we
84 * don't want the code that calls this
85 * function to try and operate on a NULL skb.
86 */
87 goto out;
88 }
944e2948
EB
89 }
90
fa2dbdc2 91 status = netdev_start_xmit(skb, dev, txq, false);
944e2948
EB
92
93out:
94 return status;
95}
96
c4028958 97static void queue_process(struct work_struct *work)
1da177e4 98{
4c1ac1b4
DH
99 struct netpoll_info *npinfo =
100 container_of(work, struct netpoll_info, tx_work.work);
1da177e4 101 struct sk_buff *skb;
3640543d 102 unsigned long flags;
1da177e4 103
6c43ff18
SH
104 while ((skb = skb_dequeue(&npinfo->txq))) {
105 struct net_device *dev = skb->dev;
fd2ea0a7 106 struct netdev_queue *txq;
c70b17b7 107 unsigned int q_index;
1da177e4 108
6c43ff18 109 if (!netif_device_present(dev) || !netif_running(dev)) {
080b3c19 110 kfree_skb(skb);
6c43ff18
SH
111 continue;
112 }
1da177e4 113
3640543d 114 local_irq_save(flags);
c70b17b7
TD
115 /* check if skb->queue_mapping is still valid */
116 q_index = skb_get_queue_mapping(skb);
117 if (unlikely(q_index >= dev->real_num_tx_queues)) {
118 q_index = q_index % dev->real_num_tx_queues;
119 skb_set_queue_mapping(skb, q_index);
120 }
121 txq = netdev_get_tx_queue(dev, q_index);
5efeac44 122 HARD_TX_LOCK(dev, txq, smp_processor_id());
73466498 123 if (netif_xmit_frozen_or_stopped(txq) ||
944e2948 124 netpoll_start_xmit(skb, dev, txq) != NETDEV_TX_OK) {
6c43ff18 125 skb_queue_head(&npinfo->txq, skb);
5efeac44 126 HARD_TX_UNLOCK(dev, txq);
3640543d 127 local_irq_restore(flags);
1da177e4 128
25442caf 129 schedule_delayed_work(&npinfo->tx_work, HZ/10);
6c43ff18
SH
130 return;
131 }
5efeac44 132 HARD_TX_UNLOCK(dev, txq);
3640543d 133 local_irq_restore(flags);
1da177e4 134 }
1da177e4
LT
135}
136
822d54b9 137static void poll_one_napi(struct napi_struct *napi)
0a7606c1 138{
c24498c6 139 int work;
0a7606c1 140
2d8bff12
NH
141 /* If we set this bit but see that it has already been set,
142 * that indicates that napi has been disabled and we need
143 * to abort this operation
144 */
145 if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
822d54b9 146 return;
0a7606c1 147
822d54b9
AD
148 /* We explicilty pass the polling call a budget of 0 to
149 * indicate that we are clearing the Tx path only.
150 */
151 work = napi->poll(napi, 0);
152 WARN_ONCE(work, "%pF exceeded budget in poll\n", napi->poll);
1db19db7 153 trace_napi_poll(napi, work, 0);
0a7606c1 154
7b363e44 155 clear_bit(NAPI_STATE_NPSVC, &napi->state);
0a7606c1
DM
156}
157
822d54b9 158static void poll_napi(struct net_device *dev)
1da177e4 159{
bea3348e 160 struct napi_struct *napi;
89c4b442 161 int cpu = smp_processor_id();
1da177e4 162
f13d493d 163 list_for_each_entry(napi, &dev->napi_list, dev_list) {
89c4b442 164 if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) {
822d54b9 165 poll_one_napi(napi);
89c4b442 166 smp_store_release(&napi->poll_owner, -1);
bea3348e 167 }
1da177e4
LT
168 }
169}
170
ac3d9dd0 171void netpoll_poll_dev(struct net_device *dev)
1da177e4 172{
2899656b 173 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
ac3d9dd0 174 const struct net_device_ops *ops;
5106930b 175
ca99ca14
NH
176 /* Don't do any rx activity if the dev_lock mutex is held
177 * the dev_open/close paths use this to block netpoll activity
178 * while changing device state
179 */
ac3d9dd0 180 if (!ni || down_trylock(&ni->dev_lock))
ca99ca14
NH
181 return;
182
959d5fde 183 if (!netif_running(dev)) {
bd7c4b60 184 up(&ni->dev_lock);
5e392739 185 return;
959d5fde 186 }
5e392739
PE
187
188 ops = dev->netdev_ops;
ac3d9dd0
ED
189 if (ops->ndo_poll_controller)
190 ops->ndo_poll_controller(dev);
5106930b 191
822d54b9 192 poll_napi(dev);
1da177e4 193
bd7c4b60 194 up(&ni->dev_lock);
ca99ca14 195
3578b0c8 196 zap_completion_queue();
1da177e4 197}
ac3d9dd0 198EXPORT_SYMBOL(netpoll_poll_dev);
1da177e4 199
66b5552f 200void netpoll_poll_disable(struct net_device *dev)
ca99ca14
NH
201{
202 struct netpoll_info *ni;
203 int idx;
204 might_sleep();
205 idx = srcu_read_lock(&netpoll_srcu);
206 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
207 if (ni)
bd7c4b60 208 down(&ni->dev_lock);
ca99ca14 209 srcu_read_unlock(&netpoll_srcu, idx);
ca99ca14 210}
66b5552f 211EXPORT_SYMBOL(netpoll_poll_disable);
ca99ca14 212
66b5552f 213void netpoll_poll_enable(struct net_device *dev)
ca99ca14
NH
214{
215 struct netpoll_info *ni;
216 rcu_read_lock();
217 ni = rcu_dereference(dev->npinfo);
218 if (ni)
bd7c4b60 219 up(&ni->dev_lock);
ca99ca14
NH
220 rcu_read_unlock();
221}
66b5552f 222EXPORT_SYMBOL(netpoll_poll_enable);
ca99ca14 223
1da177e4
LT
224static void refill_skbs(void)
225{
226 struct sk_buff *skb;
227 unsigned long flags;
228
a1bcfacd
SH
229 spin_lock_irqsave(&skb_pool.lock, flags);
230 while (skb_pool.qlen < MAX_SKBS) {
1da177e4
LT
231 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
232 if (!skb)
233 break;
234
a1bcfacd 235 __skb_queue_tail(&skb_pool, skb);
1da177e4 236 }
a1bcfacd 237 spin_unlock_irqrestore(&skb_pool.lock, flags);
1da177e4
LT
238}
239
3578b0c8
DM
240static void zap_completion_queue(void)
241{
242 unsigned long flags;
243 struct softnet_data *sd = &get_cpu_var(softnet_data);
244
245 if (sd->completion_queue) {
246 struct sk_buff *clist;
247
248 local_irq_save(flags);
249 clist = sd->completion_queue;
250 sd->completion_queue = NULL;
251 local_irq_restore(flags);
252
253 while (clist != NULL) {
254 struct sk_buff *skb = clist;
255 clist = clist->next;
b1586f09 256 if (!skb_irq_freeable(skb)) {
230cd127 257 refcount_set(&skb->users, 1);
3578b0c8
DM
258 dev_kfree_skb_any(skb); /* put this one back */
259 } else {
260 __kfree_skb(skb);
261 }
262 }
263 }
264
265 put_cpu_var(softnet_data);
266}
267
a1bcfacd 268static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
1da177e4 269{
a1bcfacd
SH
270 int count = 0;
271 struct sk_buff *skb;
1da177e4 272
3578b0c8 273 zap_completion_queue();
a1bcfacd 274 refill_skbs();
1da177e4 275repeat:
1da177e4
LT
276
277 skb = alloc_skb(len, GFP_ATOMIC);
a1bcfacd
SH
278 if (!skb)
279 skb = skb_dequeue(&skb_pool);
1da177e4
LT
280
281 if (!skb) {
a1bcfacd 282 if (++count < 10) {
2a49e001 283 netpoll_poll_dev(np->dev);
a1bcfacd 284 goto repeat;
1da177e4 285 }
a1bcfacd 286 return NULL;
1da177e4
LT
287 }
288
63354797 289 refcount_set(&skb->users, 1);
1da177e4
LT
290 skb_reserve(skb, reserve);
291 return skb;
292}
293
bea3348e
SH
294static int netpoll_owner_active(struct net_device *dev)
295{
296 struct napi_struct *napi;
297
298 list_for_each_entry(napi, &dev->napi_list, dev_list) {
299 if (napi->poll_owner == smp_processor_id())
300 return 1;
301 }
302 return 0;
303}
304
2899656b 305/* call with IRQ disabled */
c2355e1a
NH
306void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
307 struct net_device *dev)
1da177e4 308{
2bdfe0ba
SH
309 int status = NETDEV_TX_BUSY;
310 unsigned long tries;
de85d99e 311 /* It is up to the caller to keep npinfo alive. */
2899656b 312 struct netpoll_info *npinfo;
2bdfe0ba 313
af073393 314 lockdep_assert_irqs_disabled();
2899656b
AW
315
316 npinfo = rcu_dereference_bh(np->dev->npinfo);
4ec93edb 317 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
080b3c19 318 dev_kfree_skb_irq(skb);
4ec93edb
YH
319 return;
320 }
2bdfe0ba
SH
321
322 /* don't get messages out of order, and no recursion */
bea3348e 323 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
fd2ea0a7 324 struct netdev_queue *txq;
a49f99ff 325
f663dd9a 326 txq = netdev_pick_tx(dev, skb, NULL);
fd2ea0a7 327
0db3dc73
SH
328 /* try until next clock tick */
329 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
330 tries > 0; --tries) {
5efeac44 331 if (HARD_TX_TRYLOCK(dev, txq)) {
944e2948
EB
332 if (!netif_xmit_stopped(txq))
333 status = netpoll_start_xmit(skb, dev, txq);
334
5efeac44 335 HARD_TX_UNLOCK(dev, txq);
e37b8d93
AM
336
337 if (status == NETDEV_TX_OK)
338 break;
339
e37b8d93 340 }
0db3dc73
SH
341
342 /* tickle device maybe there is some cleanup */
2a49e001 343 netpoll_poll_dev(np->dev);
0db3dc73
SH
344
345 udelay(USEC_PER_POLL);
0db1d6fc 346 }
79b1bee8
DD
347
348 WARN_ONCE(!irqs_disabled(),
2899656b 349 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
944e2948 350 dev->name, dev->netdev_ops->ndo_start_xmit);
79b1bee8 351
1da177e4 352 }
1da177e4 353
2bdfe0ba 354 if (status != NETDEV_TX_OK) {
5de4a473 355 skb_queue_tail(&npinfo->txq, skb);
4c1ac1b4 356 schedule_delayed_work(&npinfo->tx_work,0);
1da177e4 357 }
1da177e4 358}
c2355e1a 359EXPORT_SYMBOL(netpoll_send_skb_on_dev);
1da177e4
LT
360
361void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
362{
954fba02 363 int total_len, ip_len, udp_len;
1da177e4
LT
364 struct sk_buff *skb;
365 struct udphdr *udph;
366 struct iphdr *iph;
367 struct ethhdr *eth;
ee130409 368 static atomic_t ip_ident;
b3d936f3 369 struct ipv6hdr *ip6h;
1da177e4 370
c9fd56b3
NA
371 WARN_ON_ONCE(!irqs_disabled());
372
1da177e4 373 udp_len = len + sizeof(*udph);
b3d936f3
CW
374 if (np->ipv6)
375 ip_len = udp_len + sizeof(*ip6h);
376 else
b7394d24
CW
377 ip_len = udp_len + sizeof(*iph);
378
954fba02 379 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
1da177e4 380
954fba02
ED
381 skb = find_skb(np, total_len + np->dev->needed_tailroom,
382 total_len - len);
1da177e4
LT
383 if (!skb)
384 return;
385
27d7ff46 386 skb_copy_to_linear_data(skb, msg, len);
954fba02 387 skb_put(skb, len);
1da177e4 388
4bedb452
ACM
389 skb_push(skb, sizeof(*udph));
390 skb_reset_transport_header(skb);
391 udph = udp_hdr(skb);
1da177e4
LT
392 udph->source = htons(np->local_port);
393 udph->dest = htons(np->remote_port);
394 udph->len = htons(udp_len);
b7394d24 395
b3d936f3
CW
396 if (np->ipv6) {
397 udph->check = 0;
398 udph->check = csum_ipv6_magic(&np->local_ip.in6,
399 &np->remote_ip.in6,
400 udp_len, IPPROTO_UDP,
401 csum_partial(udph, udp_len, 0));
402 if (udph->check == 0)
403 udph->check = CSUM_MANGLED_0;
404
405 skb_push(skb, sizeof(*ip6h));
406 skb_reset_network_header(skb);
407 ip6h = ipv6_hdr(skb);
408
409 /* ip6h->version = 6; ip6h->priority = 0; */
410 put_unaligned(0x60, (unsigned char *)ip6h);
411 ip6h->flow_lbl[0] = 0;
412 ip6h->flow_lbl[1] = 0;
413 ip6h->flow_lbl[2] = 0;
414
415 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
416 ip6h->nexthdr = IPPROTO_UDP;
417 ip6h->hop_limit = 32;
418 ip6h->saddr = np->local_ip.in6;
419 ip6h->daddr = np->remote_ip.in6;
420
d58ff351 421 eth = skb_push(skb, ETH_HLEN);
b3d936f3
CW
422 skb_reset_mac_header(skb);
423 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
424 } else {
b7394d24
CW
425 udph->check = 0;
426 udph->check = csum_tcpudp_magic(np->local_ip.ip,
427 np->remote_ip.ip,
428 udp_len, IPPROTO_UDP,
429 csum_partial(udph, udp_len, 0));
430 if (udph->check == 0)
431 udph->check = CSUM_MANGLED_0;
432
433 skb_push(skb, sizeof(*iph));
434 skb_reset_network_header(skb);
435 iph = ip_hdr(skb);
436
437 /* iph->version = 4; iph->ihl = 5; */
438 put_unaligned(0x45, (unsigned char *)iph);
439 iph->tos = 0;
440 put_unaligned(htons(ip_len), &(iph->tot_len));
441 iph->id = htons(atomic_inc_return(&ip_ident));
442 iph->frag_off = 0;
443 iph->ttl = 64;
444 iph->protocol = IPPROTO_UDP;
445 iph->check = 0;
446 put_unaligned(np->local_ip.ip, &(iph->saddr));
447 put_unaligned(np->remote_ip.ip, &(iph->daddr));
448 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
449
d58ff351 450 eth = skb_push(skb, ETH_HLEN);
b7394d24
CW
451 skb_reset_mac_header(skb);
452 skb->protocol = eth->h_proto = htons(ETH_P_IP);
453 }
454
c62326ab
JP
455 ether_addr_copy(eth->h_source, np->dev->dev_addr);
456 ether_addr_copy(eth->h_dest, np->remote_mac);
1da177e4
LT
457
458 skb->dev = np->dev;
459
460 netpoll_send_skb(np, skb);
461}
9e34a5b5 462EXPORT_SYMBOL(netpoll_send_udp);
1da177e4 463
0bcc1816
SS
464void netpoll_print_options(struct netpoll *np)
465{
e6ec2693 466 np_info(np, "local port %d\n", np->local_port);
b3d936f3
CW
467 if (np->ipv6)
468 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
469 else
b7394d24 470 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
e6ec2693
JP
471 np_info(np, "interface '%s'\n", np->dev_name);
472 np_info(np, "remote port %d\n", np->remote_port);
b3d936f3
CW
473 if (np->ipv6)
474 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
475 else
b7394d24 476 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
e6ec2693 477 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
0bcc1816 478}
9e34a5b5 479EXPORT_SYMBOL(netpoll_print_options);
0bcc1816 480
b7394d24
CW
481static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
482{
483 const char *end;
484
485 if (!strchr(str, ':') &&
486 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
487 if (!*end)
488 return 0;
489 }
490 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
491#if IS_ENABLED(CONFIG_IPV6)
492 if (!*end)
493 return 1;
494#else
495 return -1;
496#endif
497 }
498 return -1;
499}
500
1da177e4
LT
501int netpoll_parse_options(struct netpoll *np, char *opt)
502{
503 char *cur=opt, *delim;
b7394d24 504 int ipv6;
00fe11b3 505 bool ipversion_set = false;
1da177e4 506
c68b9070 507 if (*cur != '@') {
1da177e4
LT
508 if ((delim = strchr(cur, '@')) == NULL)
509 goto parse_failed;
c68b9070 510 *delim = 0;
4b5511eb
AP
511 if (kstrtou16(cur, 10, &np->local_port))
512 goto parse_failed;
c68b9070 513 cur = delim;
1da177e4
LT
514 }
515 cur++;
1da177e4 516
c68b9070 517 if (*cur != '/') {
00fe11b3 518 ipversion_set = true;
1da177e4
LT
519 if ((delim = strchr(cur, '/')) == NULL)
520 goto parse_failed;
c68b9070 521 *delim = 0;
b7394d24
CW
522 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
523 if (ipv6 < 0)
524 goto parse_failed;
525 else
526 np->ipv6 = (bool)ipv6;
c68b9070 527 cur = delim;
1da177e4
LT
528 }
529 cur++;
530
c68b9070 531 if (*cur != ',') {
1da177e4
LT
532 /* parse out dev name */
533 if ((delim = strchr(cur, ',')) == NULL)
534 goto parse_failed;
c68b9070 535 *delim = 0;
1da177e4 536 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
c68b9070 537 cur = delim;
1da177e4
LT
538 }
539 cur++;
540
c68b9070 541 if (*cur != '@') {
1da177e4
LT
542 /* dst port */
543 if ((delim = strchr(cur, '@')) == NULL)
544 goto parse_failed;
c68b9070 545 *delim = 0;
5fc05f87 546 if (*cur == ' ' || *cur == '\t')
e6ec2693 547 np_info(np, "warning: whitespace is not allowed\n");
4b5511eb
AP
548 if (kstrtou16(cur, 10, &np->remote_port))
549 goto parse_failed;
c68b9070 550 cur = delim;
1da177e4
LT
551 }
552 cur++;
1da177e4
LT
553
554 /* dst ip */
555 if ((delim = strchr(cur, '/')) == NULL)
556 goto parse_failed;
c68b9070 557 *delim = 0;
b7394d24
CW
558 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
559 if (ipv6 < 0)
560 goto parse_failed;
00fe11b3 561 else if (ipversion_set && np->ipv6 != (bool)ipv6)
b7394d24
CW
562 goto parse_failed;
563 else
564 np->ipv6 = (bool)ipv6;
c68b9070 565 cur = delim + 1;
1da177e4 566
c68b9070 567 if (*cur != 0) {
1da177e4 568 /* MAC address */
4940fc88 569 if (!mac_pton(cur, np->remote_mac))
1da177e4 570 goto parse_failed;
1da177e4
LT
571 }
572
0bcc1816 573 netpoll_print_options(np);
1da177e4
LT
574
575 return 0;
576
577 parse_failed:
e6ec2693 578 np_info(np, "couldn't parse config at '%s'!\n", cur);
1da177e4
LT
579 return -1;
580}
9e34a5b5 581EXPORT_SYMBOL(netpoll_parse_options);
1da177e4 582
a8779ec1 583int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
1da177e4 584{
115c1d6e 585 struct netpoll_info *npinfo;
4247e161 586 const struct net_device_ops *ops;
b41848b6 587 int err;
1da177e4 588
727ceaa4 589 np->dev = ndev;
30fdd8a0
JP
590 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
591
ac3d9dd0 592 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
e6ec2693
JP
593 np_err(np, "%s doesn't support polling, aborting\n",
594 np->dev_name);
8fdd95ec
HX
595 err = -ENOTSUPP;
596 goto out;
597 }
598
599 if (!ndev->npinfo) {
a8779ec1 600 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
8fdd95ec
HX
601 if (!npinfo) {
602 err = -ENOMEM;
603 goto out;
604 }
605
bd7c4b60 606 sema_init(&npinfo->dev_lock, 1);
8fdd95ec
HX
607 skb_queue_head_init(&npinfo->txq);
608 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
609
433cea4d 610 refcount_set(&npinfo->refcnt, 1);
8fdd95ec
HX
611
612 ops = np->dev->netdev_ops;
613 if (ops->ndo_netpoll_setup) {
a8779ec1 614 err = ops->ndo_netpoll_setup(ndev, npinfo);
8fdd95ec
HX
615 if (err)
616 goto free_npinfo;
617 }
618 } else {
0790bbb6 619 npinfo = rtnl_dereference(ndev->npinfo);
433cea4d 620 refcount_inc(&npinfo->refcnt);
8fdd95ec
HX
621 }
622
623 npinfo->netpoll = np;
624
8fdd95ec 625 /* last thing to do is link it to the net device structure */
cf778b00 626 rcu_assign_pointer(ndev->npinfo, npinfo);
8fdd95ec
HX
627
628 return 0;
629
630free_npinfo:
631 kfree(npinfo);
632out:
633 return err;
634}
635EXPORT_SYMBOL_GPL(__netpoll_setup);
636
637int netpoll_setup(struct netpoll *np)
638{
639 struct net_device *ndev = NULL;
640 struct in_device *in_dev;
641 int err;
642
f92d3180 643 rtnl_lock();
0c3a8f8b 644 if (np->dev_name[0]) {
556e6256
CW
645 struct net *net = current->nsproxy->net_ns;
646 ndev = __dev_get_by_name(net, np->dev_name);
647 }
1da177e4 648 if (!ndev) {
e6ec2693 649 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
f92d3180
CW
650 err = -ENODEV;
651 goto unlock;
1da177e4 652 }
5bd30d39 653 dev_hold(ndev);
1da177e4 654
49bd8fb0 655 if (netdev_master_upper_dev_get(ndev)) {
e6ec2693 656 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
83fe32de
DC
657 err = -EBUSY;
658 goto put;
0c1ad04a
WC
659 }
660
1da177e4
LT
661 if (!netif_running(ndev)) {
662 unsigned long atmost, atleast;
663
e6ec2693 664 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
1da177e4 665
00f54e68 666 err = dev_open(ndev, NULL);
b41848b6
SH
667
668 if (err) {
e6ec2693 669 np_err(np, "failed to open %s\n", ndev->name);
dbaa1541 670 goto put;
1da177e4 671 }
1da177e4 672
f92d3180 673 rtnl_unlock();
1da177e4 674 atleast = jiffies + HZ/10;
bff38771 675 atmost = jiffies + carrier_timeout * HZ;
1da177e4
LT
676 while (!netif_carrier_ok(ndev)) {
677 if (time_after(jiffies, atmost)) {
e6ec2693 678 np_notice(np, "timeout waiting for carrier\n");
1da177e4
LT
679 break;
680 }
1b614fb9 681 msleep(1);
1da177e4
LT
682 }
683
684 /* If carrier appears to come up instantly, we don't
685 * trust it and pause so that we don't pump all our
686 * queued console messages into the bitbucket.
687 */
688
689 if (time_before(jiffies, atleast)) {
e6ec2693 690 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
1da177e4
LT
691 msleep(4000);
692 }
f92d3180 693 rtnl_lock();
1da177e4
LT
694 }
695
b7394d24
CW
696 if (!np->local_ip.ip) {
697 if (!np->ipv6) {
f92d3180 698 in_dev = __in_dev_get_rtnl(ndev);
b7394d24
CW
699
700 if (!in_dev || !in_dev->ifa_list) {
b7394d24
CW
701 np_err(np, "no IP address for %s, aborting\n",
702 np->dev_name);
703 err = -EDESTADDRREQ;
704 goto put;
705 }
706
707 np->local_ip.ip = in_dev->ifa_list->ifa_local;
b7394d24 708 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
b3d936f3
CW
709 } else {
710#if IS_ENABLED(CONFIG_IPV6)
711 struct inet6_dev *idev;
712
713 err = -EDESTADDRREQ;
b3d936f3
CW
714 idev = __in6_dev_get(ndev);
715 if (idev) {
716 struct inet6_ifaddr *ifp;
717
718 read_lock_bh(&idev->lock);
719 list_for_each_entry(ifp, &idev->addr_list, if_list) {
d016b4a3
MK
720 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
721 !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
b3d936f3
CW
722 continue;
723 np->local_ip.in6 = ifp->addr;
724 err = 0;
725 break;
726 }
727 read_unlock_bh(&idev->lock);
728 }
b3d936f3
CW
729 if (err) {
730 np_err(np, "no IPv6 address for %s, aborting\n",
731 np->dev_name);
732 goto put;
733 } else
734 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
735#else
736 np_err(np, "IPv6 is not supported %s, aborting\n",
737 np->dev_name);
e39363a9 738 err = -EINVAL;
b3d936f3
CW
739 goto put;
740#endif
1da177e4 741 }
1da177e4
LT
742 }
743
dbaa1541
HX
744 /* fill up the skb queue */
745 refill_skbs();
746
a8779ec1 747 err = __netpoll_setup(np, ndev);
8fdd95ec
HX
748 if (err)
749 goto put;
750
f92d3180 751 rtnl_unlock();
1da177e4
LT
752 return 0;
753
21edbb22 754put:
1da177e4 755 dev_put(ndev);
f92d3180
CW
756unlock:
757 rtnl_unlock();
b41848b6 758 return err;
1da177e4 759}
9e34a5b5 760EXPORT_SYMBOL(netpoll_setup);
1da177e4 761
c68b9070
DM
762static int __init netpoll_init(void)
763{
a1bcfacd
SH
764 skb_queue_head_init(&skb_pool);
765 return 0;
766}
767core_initcall(netpoll_init);
768
38e6bc18
AW
769static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
770{
771 struct netpoll_info *npinfo =
772 container_of(rcu_head, struct netpoll_info, rcu);
773
38e6bc18
AW
774 skb_queue_purge(&npinfo->txq);
775
776 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
777 cancel_delayed_work(&npinfo->tx_work);
778
779 /* clean after last, unfinished work */
780 __skb_queue_purge(&npinfo->txq);
781 /* now cancel it again */
782 cancel_delayed_work(&npinfo->tx_work);
783 kfree(npinfo);
784}
785
8fdd95ec 786void __netpoll_cleanup(struct netpoll *np)
1da177e4 787{
fbeec2e1 788 struct netpoll_info *npinfo;
fbeec2e1 789
0790bbb6 790 npinfo = rtnl_dereference(np->dev->npinfo);
8fdd95ec 791 if (!npinfo)
dbaa1541 792 return;
93ec2c72 793
ca99ca14
NH
794 synchronize_srcu(&netpoll_srcu);
795
433cea4d 796 if (refcount_dec_and_test(&npinfo->refcnt)) {
8fdd95ec 797 const struct net_device_ops *ops;
de85d99e 798
8fdd95ec
HX
799 ops = np->dev->netdev_ops;
800 if (ops->ndo_netpoll_cleanup)
801 ops->ndo_netpoll_cleanup(np->dev);
de85d99e 802
fcb144b5 803 RCU_INIT_POINTER(np->dev->npinfo, NULL);
5da54c18 804 call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
efa95b01 805 } else
806 RCU_INIT_POINTER(np->dev->npinfo, NULL);
38e6bc18
AW
807}
808EXPORT_SYMBOL_GPL(__netpoll_cleanup);
de85d99e 809
c9fbd71f 810void __netpoll_free(struct netpoll *np)
38e6bc18 811{
c9fbd71f 812 ASSERT_RTNL();
93ec2c72 813
c9fbd71f 814 /* Wait for transmitting packets to finish before freeing. */
5da54c18 815 synchronize_rcu();
38e6bc18
AW
816 __netpoll_cleanup(np);
817 kfree(np);
818}
c9fbd71f 819EXPORT_SYMBOL_GPL(__netpoll_free);
fbeec2e1 820
8fdd95ec
HX
821void netpoll_cleanup(struct netpoll *np)
822{
8fdd95ec 823 rtnl_lock();
d0fe8c88
NA
824 if (!np->dev)
825 goto out;
8fdd95ec 826 __netpoll_cleanup(np);
8fdd95ec 827 dev_put(np->dev);
1da177e4 828 np->dev = NULL;
d0fe8c88
NA
829out:
830 rtnl_unlock();
1da177e4 831}
9e34a5b5 832EXPORT_SYMBOL(netpoll_cleanup);