blob: 361aabffb8c0cb8ce55b10c9a4539315ea10946a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Common framework for low-level network console, dump, and debugger code
3 *
4 * Sep 8 2003 Matt Mackall <mpm@selenic.com>
5 *
6 * based on the netconsole code from:
7 *
8 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2002 Red Hat, Inc.
10 */
11
Joe Perchese6ec269352012-01-29 15:50:43 +000012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Anton Vorontsovbff38772009-07-08 11:10:56 -070014#include <linux/moduleparam.h>
Andy Shevchenko4cd57732013-06-04 19:46:26 +030015#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/netdevice.h>
17#include <linux/etherdevice.h>
18#include <linux/string.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020019#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040029#include <linux/export.h>
Amerigo Wang689971b2012-08-10 01:24:49 +000030#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/tcp.h>
32#include <net/udp.h>
Cong Wangb3d936f2013-01-07 20:52:41 +000033#include <net/addrconf.h>
34#include <net/ndisc.h>
35#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/unaligned.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -070037#include <trace/events/napi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080047static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Lai Jiangshan7f9421c2013-03-15 06:50:52 +000049DEFINE_STATIC_SRCU(netpoll_srcu);
Neil Hormanca99ca12013-02-05 08:05:43 +000050
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070051#define USEC_PER_POLL 50
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Joe Perches6f706242012-01-29 15:50:44 +000053#define MAX_SKB_SIZE \
54 (sizeof(struct ethhdr) + \
55 sizeof(struct iphdr) + \
56 sizeof(struct udphdr) + \
57 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David S. Miller3578b0c2010-08-03 00:24:04 -070059static void zap_completion_queue(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Anton Vorontsovbff38772009-07-08 11:10:56 -070061static unsigned int carrier_timeout = 4;
62module_param(carrier_timeout, uint, 0644);
63
Joe Perchese6ec269352012-01-29 15:50:43 +000064#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
Eric W. Biederman944e2942014-03-27 15:37:28 -070071static int netpoll_start_xmit(struct sk_buff *skb, struct net_device *dev,
72 struct netdev_queue *txq)
73{
Eric W. Biederman944e2942014-03-27 15:37:28 -070074 int status = NETDEV_TX_OK;
75 netdev_features_t features;
76
77 features = netif_skb_features(skb);
78
Jiri Pirkodf8a39d2015-01-13 17:13:44 +010079 if (skb_vlan_tag_present(skb) &&
Eric W. Biederman944e2942014-03-27 15:37:28 -070080 !vlan_hw_offload_capable(features, skb->vlan_proto)) {
Jiri Pirko59682502014-11-19 14:04:59 +010081 skb = __vlan_hwaccel_push_inside(skb);
Eric W. Biederman944e2942014-03-27 15:37:28 -070082 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 }
Eric W. Biederman944e2942014-03-27 15:37:28 -070089 }
90
David S. Millerfa2dbdc2014-08-29 21:55:22 -070091 status = netdev_start_xmit(skb, dev, txq, false);
Eric W. Biederman944e2942014-03-27 15:37:28 -070092
93out:
94 return status;
95}
96
David Howellsc4028952006-11-22 14:57:56 +000097static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
David Howells4c1ac1b2006-12-05 14:37:56 +000099 struct netpoll_info *npinfo =
100 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +0100102 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700104 while ((skb = skb_dequeue(&npinfo->txq))) {
105 struct net_device *dev = skb->dev;
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700106 struct netdev_queue *txq;
Tushar Davec70b17b72017-04-20 15:57:31 -0700107 unsigned int q_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700109 if (!netif_device_present(dev) || !netif_running(dev)) {
Eric W. Biederman080b3c12014-03-27 15:41:04 -0700110 kfree_skb(skb);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700111 continue;
112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Ingo Molnar36405432006-12-12 17:20:42 +0100114 local_irq_save(flags);
Tushar Davec70b17b72017-04-20 15:57:31 -0700115 /* 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);
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700122 HARD_TX_LOCK(dev, txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +0000123 if (netif_xmit_frozen_or_stopped(txq) ||
Eric W. Biederman944e2942014-03-27 15:37:28 -0700124 netpoll_start_xmit(skb, dev, txq) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700125 skb_queue_head(&npinfo->txq, skb);
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700126 HARD_TX_UNLOCK(dev, txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100127 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700129 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700130 return;
131 }
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700132 HARD_TX_UNLOCK(dev, txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100133 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135}
136
Alexander Duyck822d54b2015-09-28 09:16:17 -0700137static void poll_one_napi(struct napi_struct *napi)
David S. Miller0a7606c2007-10-29 21:28:47 -0700138{
Eric Dumazetc24498c2018-09-27 09:31:51 -0700139 int work;
David S. Miller0a7606c2007-10-29 21:28:47 -0700140
Neil Horman2d8bff1262015-09-23 14:57:58 -0400141 /* 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))
Alexander Duyck822d54b2015-09-28 09:16:17 -0700146 return;
David S. Miller0a7606c2007-10-29 21:28:47 -0700147
Alexander Duyck822d54b2015-09-28 09:16:17 -0700148 /* 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);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +0200153 trace_napi_poll(napi, work, 0);
David S. Miller0a7606c2007-10-29 21:28:47 -0700154
Neil Horman7b363e42008-12-09 23:22:26 -0800155 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700156}
157
Alexander Duyck822d54b2015-09-28 09:16:17 -0700158static void poll_napi(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700160 struct napi_struct *napi;
Eric Dumazet89c4b442016-11-16 14:54:50 -0800161 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Neil Hormanf13d4932010-10-19 07:04:26 +0000163 list_for_each_entry(napi, &dev->napi_list, dev_list) {
Eric Dumazet89c4b442016-11-16 14:54:50 -0800164 if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) {
Alexander Duyck822d54b2015-09-28 09:16:17 -0700165 poll_one_napi(napi);
Eric Dumazet89c4b442016-11-16 14:54:50 -0800166 smp_store_release(&napi->poll_owner, -1);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169}
170
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700171void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Amerigo Wang28996562012-08-10 01:24:42 +0000173 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700174 const struct net_device_ops *ops;
Stephen Hemminger51069302007-11-19 19:18:11 -0800175
Neil Hormanca99ca12013-02-05 08:05:43 +0000176 /* 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 */
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700180 if (!ni || down_trylock(&ni->dev_lock))
Neil Hormanca99ca12013-02-05 08:05:43 +0000181 return;
182
Neil Horman959d5fd2013-02-13 11:32:42 -0500183 if (!netif_running(dev)) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000184 up(&ni->dev_lock);
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000185 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500186 }
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000187
188 ops = dev->netdev_ops;
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700189 if (ops->ndo_poll_controller)
190 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Alexander Duyck822d54b2015-09-28 09:16:17 -0700192 poll_napi(dev);
Stephen Hemminger51069302007-11-19 19:18:11 -0800193
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000194 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000195
David S. Miller3578b0c2010-08-03 00:24:04 -0700196 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700198EXPORT_SYMBOL(netpoll_poll_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Eric W. Biederman66b55522014-03-27 15:39:03 -0700200void netpoll_poll_disable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000201{
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)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000208 down(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000209 srcu_read_unlock(&netpoll_srcu, idx);
Neil Hormanca99ca12013-02-05 08:05:43 +0000210}
Eric W. Biederman66b55522014-03-27 15:39:03 -0700211EXPORT_SYMBOL(netpoll_poll_disable);
Neil Hormanca99ca12013-02-05 08:05:43 +0000212
Eric W. Biederman66b55522014-03-27 15:39:03 -0700213void netpoll_poll_enable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000214{
215 struct netpoll_info *ni;
216 rcu_read_lock();
217 ni = rcu_dereference(dev->npinfo);
218 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000219 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000220 rcu_read_unlock();
221}
Eric W. Biederman66b55522014-03-27 15:39:03 -0700222EXPORT_SYMBOL(netpoll_poll_enable);
Neil Hormanca99ca12013-02-05 08:05:43 +0000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static void refill_skbs(void)
225{
226 struct sk_buff *skb;
227 unsigned long flags;
228
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800229 spin_lock_irqsave(&skb_pool.lock, flags);
230 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
232 if (!skb)
233 break;
234
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800235 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800237 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
David S. Miller3578b0c2010-08-03 00:24:04 -0700240static 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;
Eric W. Biedermanb1586f02014-04-01 12:21:02 -0700256 if (!skb_irq_freeable(skb)) {
WANG Cong230cd122017-07-12 15:56:41 -0700257 refcount_set(&skb->users, 1);
David S. Miller3578b0c2010-08-03 00:24:04 -0700258 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
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800268static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800270 int count = 0;
271 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
David S. Miller3578b0c2010-08-03 00:24:04 -0700273 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800274 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800278 if (!skb)
279 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800282 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000283 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800284 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800286 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Reshetova, Elena63354792017-06-30 13:07:58 +0300289 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 skb_reserve(skb, reserve);
291 return skb;
292}
293
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700294static 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
Amerigo Wang28996562012-08-10 01:24:42 +0000305/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000306void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
307 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700309 int status = NETDEV_TX_BUSY;
310 unsigned long tries;
Herbert Xude85d992010-06-10 16:12:44 +0000311 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000312 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Frederic Weisbeckeraf073392017-11-06 16:01:29 +0100314 lockdep_assert_irqs_disabled();
Amerigo Wang28996562012-08-10 01:24:42 +0000315
316 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900317 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
Eric W. Biederman080b3c12014-03-27 15:41:04 -0700318 dev_kfree_skb_irq(skb);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900319 return;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700322 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700323 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700324 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800325
Jason Wangf663dd92014-01-10 16:18:26 +0800326 txq = netdev_pick_tx(dev, skb, NULL);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700327
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700328 /* try until next clock tick */
329 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
330 tries > 0; --tries) {
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700331 if (HARD_TX_TRYLOCK(dev, txq)) {
Eric W. Biederman944e2942014-03-27 15:37:28 -0700332 if (!netif_xmit_stopped(txq))
333 status = netpoll_start_xmit(skb, dev, txq);
Amerigo Wang689971b2012-08-10 01:24:49 +0000334
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700335 HARD_TX_UNLOCK(dev, txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700336
Andrew Mortone37b8d92006-12-09 14:01:49 -0800337 if (status == NETDEV_TX_OK)
338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Andrew Mortone37b8d92006-12-09 14:01:49 -0800340 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700341
342 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000343 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700344
345 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700346 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000347
348 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000349 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Eric W. Biederman944e2942014-03-27 15:37:28 -0700350 dev->name, dev->netdev_ops->ndo_start_xmit);
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700354 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700355 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000356 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
Neil Hormanc2355e12010-10-13 16:01:49 +0000359EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
362{
Eric Dumazet954fba02012-06-12 19:30:21 +0000363 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct sk_buff *skb;
365 struct udphdr *udph;
366 struct iphdr *iph;
367 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000368 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000369 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Nikolay Aleksandrovc9fd56b2015-08-28 15:44:25 -0700371 WARN_ON_ONCE(!irqs_disabled());
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000374 if (np->ipv6)
375 ip_len = udp_len + sizeof(*ip6h);
376 else
Cong Wangb7394d22013-01-07 20:52:39 +0000377 ip_len = udp_len + sizeof(*iph);
378
Eric Dumazet954fba02012-06-12 19:30:21 +0000379 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Eric Dumazet954fba02012-06-12 19:30:21 +0000381 skb = find_skb(np, total_len + np->dev->needed_tailroom,
382 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!skb)
384 return;
385
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300386 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba02012-06-12 19:30:21 +0000387 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300389 skb_push(skb, sizeof(*udph));
390 skb_reset_transport_header(skb);
391 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 udph->source = htons(np->local_port);
393 udph->dest = htons(np->remote_port);
394 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Cong Wangb3d936f2013-01-07 20:52:41 +0000396 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
Johannes Bergd58ff352017-06-16 14:29:23 +0200421 eth = skb_push(skb, ETH_HLEN);
Cong Wangb3d936f2013-01-07 20:52:41 +0000422 skb_reset_mac_header(skb);
423 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
424 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000425 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Cong Wangb7394d22013-01-07 20:52:39 +0000433 skb_push(skb, sizeof(*iph));
434 skb_reset_network_header(skb);
435 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Cong Wangb7394d22013-01-07 20:52:39 +0000437 /* 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
Johannes Bergd58ff352017-06-16 14:29:23 +0200450 eth = skb_push(skb, ETH_HLEN);
Cong Wangb7394d22013-01-07 20:52:39 +0000451 skb_reset_mac_header(skb);
452 skb->protocol = eth->h_proto = htons(ETH_P_IP);
453 }
454
Joe Perchesc62326a2014-01-20 09:52:18 -0800455 ether_addr_copy(eth->h_source, np->dev->dev_addr);
456 ether_addr_copy(eth->h_dest, np->remote_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 skb->dev = np->dev;
459
460 netpoll_send_skb(np, skb);
461}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000462EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700464void netpoll_print_options(struct netpoll *np)
465{
Joe Perchese6ec269352012-01-29 15:50:43 +0000466 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000467 if (np->ipv6)
468 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
469 else
Cong Wangb7394d22013-01-07 20:52:39 +0000470 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000471 np_info(np, "interface '%s'\n", np->dev_name);
472 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000473 if (np->ipv6)
474 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
475 else
Cong Wangb7394d22013-01-07 20:52:39 +0000476 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000477 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700478}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000479EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700480
Cong Wangb7394d22013-01-07 20:52:39 +0000481static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501int netpoll_parse_options(struct netpoll *np, char *opt)
502{
503 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +0000504 int ipv6;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100505 bool ipversion_set = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
David S. Millerc68b9072006-11-14 20:40:49 -0800507 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if ((delim = strchr(cur, '@')) == NULL)
509 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800510 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000511 if (kstrtou16(cur, 10, &np->local_port))
512 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800513 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
David S. Millerc68b9072006-11-14 20:40:49 -0800517 if (*cur != '/') {
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100518 ipversion_set = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if ((delim = strchr(cur, '/')) == NULL)
520 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800521 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000522 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
523 if (ipv6 < 0)
524 goto parse_failed;
525 else
526 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800527 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 cur++;
530
David S. Millerc68b9072006-11-14 20:40:49 -0800531 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* parse out dev name */
533 if ((delim = strchr(cur, ',')) == NULL)
534 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800535 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800537 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 cur++;
540
David S. Millerc68b9072006-11-14 20:40:49 -0800541 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* dst port */
543 if ((delim = strchr(cur, '@')) == NULL)
544 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800545 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000546 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000547 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000548 if (kstrtou16(cur, 10, &np->remote_port))
549 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800550 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* dst ip */
555 if ((delim = strchr(cur, '/')) == NULL)
556 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800557 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000558 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
559 if (ipv6 < 0)
560 goto parse_failed;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100561 else if (ipversion_set && np->ipv6 != (bool)ipv6)
Cong Wangb7394d22013-01-07 20:52:39 +0000562 goto parse_failed;
563 else
564 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800565 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
David S. Millerc68b9072006-11-14 20:40:49 -0800567 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +0000569 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700573 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 return 0;
576
577 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +0000578 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -1;
580}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000581EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700583int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000584{
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000585 struct netpoll_info *npinfo;
586 const struct net_device_ops *ops;
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000587 int err;
588
Bjorn Helgaas727ceaa2016-04-05 15:58:22 -0500589 np->dev = ndev;
Jiri Pirko30fdd8a02012-07-17 05:22:35 +0000590 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
591
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700592 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000593 np_err(np, "%s doesn't support polling, aborting\n",
594 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000595 err = -ENOTSUPP;
596 goto out;
597 }
598
599 if (!ndev->npinfo) {
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700600 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000601 if (!npinfo) {
602 err = -ENOMEM;
603 goto out;
604 }
605
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000606 sema_init(&npinfo->dev_lock, 1);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000607 skb_queue_head_init(&npinfo->txq);
608 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
609
Reshetova, Elena433cea42017-06-30 13:08:04 +0300610 refcount_set(&npinfo->refcnt, 1);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000611
612 ops = np->dev->netdev_ops;
613 if (ops->ndo_netpoll_setup) {
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700614 err = ops->ndo_netpoll_setup(ndev, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000615 if (err)
616 goto free_npinfo;
617 }
618 } else {
Neil Horman0790bbb2013-02-11 10:25:31 +0000619 npinfo = rtnl_dereference(ndev->npinfo);
Reshetova, Elena433cea42017-06-30 13:08:04 +0300620 refcount_inc(&npinfo->refcnt);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000621 }
622
623 npinfo->netpoll = np;
624
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000625 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000626 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000627
628 return 0;
629
630free_npinfo:
631 kfree(npinfo);
632out:
633 return err;
634}
635EXPORT_SYMBOL_GPL(__netpoll_setup);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637int netpoll_setup(struct netpoll *np)
638{
639 struct net_device *ndev = NULL;
640 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700641 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Cong Wangf92d3182013-01-14 23:34:06 +0000643 rtnl_lock();
Matthias Kaehlcke0c3a8f82017-07-25 11:36:25 -0700644 if (np->dev_name[0]) {
Cong Wang556e6252013-01-27 15:55:21 +0000645 struct net *net = current->nsproxy->net_ns;
646 ndev = __dev_get_by_name(net, np->dev_name);
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000649 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +0000650 err = -ENODEV;
651 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Cong Wang5bd30d32013-01-17 12:21:08 +0800653 dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000655 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000656 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -0700657 err = -EBUSY;
658 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -0700659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!netif_running(ndev)) {
662 unsigned long atmost, atleast;
663
Joe Perchese6ec269352012-01-29 15:50:43 +0000664 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Petr Machata00f54e62018-12-06 17:05:36 +0000666 err = dev_open(ndev, NULL);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700667
668 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000669 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +0000670 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Cong Wangf92d3182013-01-14 23:34:06 +0000673 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -0700675 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 while (!netif_carrier_ok(ndev)) {
677 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000678 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
680 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -0700681 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
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)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000690 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 msleep(4000);
692 }
Cong Wangf92d3182013-01-14 23:34:06 +0000693 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
Cong Wangb7394d22013-01-07 20:52:39 +0000696 if (!np->local_ip.ip) {
697 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +0000698 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +0000699
700 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +0000701 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;
Cong Wangb7394d22013-01-07 20:52:39 +0000708 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +0000709 } else {
710#if IS_ENABLED(CONFIG_IPV6)
711 struct inet6_dev *idev;
712
713 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +0000714 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) {
Matwey V. Kornilovd016b4a2018-11-02 21:19:36 +0300720 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
721 !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
Cong Wangb3d936f2013-01-07 20:52:41 +0000722 continue;
723 np->local_ip.in6 = ifp->addr;
724 err = 0;
725 break;
726 }
727 read_unlock_bh(&idev->lock);
728 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000729 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);
Cong Wange39363a2013-01-22 17:39:11 +0000738 err = -EINVAL;
Cong Wangb3d936f2013-01-07 20:52:41 +0000739 goto put;
740#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Herbert Xudbaa1542010-06-10 16:12:46 +0000744 /* fill up the skb queue */
745 refill_skbs();
746
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700747 err = __netpoll_setup(np, ndev);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000748 if (err)
749 goto put;
750
Cong Wangf92d3182013-01-14 23:34:06 +0000751 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753
Jiri Slaby21edbb22010-03-16 05:29:54 +0000754put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +0000756unlock:
757 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700758 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000760EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
David S. Millerc68b9072006-11-14 20:40:49 -0800762static int __init netpoll_init(void)
763{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800764 skb_queue_head_init(&skb_pool);
765 return 0;
766}
767core_initcall(netpoll_init);
768
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000769static 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
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000774 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
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000786void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700788 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700789
Neil Horman0790bbb2013-02-11 10:25:31 +0000790 npinfo = rtnl_dereference(np->dev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000791 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +0000792 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700793
Neil Hormanca99ca12013-02-05 08:05:43 +0000794 synchronize_srcu(&netpoll_srcu);
795
Reshetova, Elena433cea42017-06-30 13:08:04 +0300796 if (refcount_dec_and_test(&npinfo->refcnt)) {
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000797 const struct net_device_ops *ops;
798
799 ops = np->dev->netdev_ops;
800 if (ops->ndo_netpoll_cleanup)
801 ops->ndo_netpoll_cleanup(np->dev);
802
Monam Agarwalfcb144b2014-03-24 00:42:46 +0530803 RCU_INIT_POINTER(np->dev->npinfo, NULL);
Paul E. McKenney5da54c12018-11-06 19:43:32 -0800804 call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
david decotignyefa95b02014-07-08 15:14:41 -0700805 } else
806 RCU_INIT_POINTER(np->dev->npinfo, NULL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000807}
808EXPORT_SYMBOL_GPL(__netpoll_cleanup);
809
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400810void __netpoll_free(struct netpoll *np)
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000811{
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400812 ASSERT_RTNL();
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000813
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400814 /* Wait for transmitting packets to finish before freeing. */
Paul E. McKenney5da54c12018-11-06 19:43:32 -0800815 synchronize_rcu();
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000816 __netpoll_cleanup(np);
817 kfree(np);
818}
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400819EXPORT_SYMBOL_GPL(__netpoll_free);
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000820
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000821void netpoll_cleanup(struct netpoll *np)
822{
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000823 rtnl_lock();
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +0200824 if (!np->dev)
825 goto out;
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000826 __netpoll_cleanup(np);
Herbert Xudbaa1542010-06-10 16:12:46 +0000827 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 np->dev = NULL;
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +0200829out:
830 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000832EXPORT_SYMBOL(netpoll_cleanup);