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