blob: dd8b1a460d64d2d609afe3a8b24a4227edb8e005 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Joe Perchese6ec269352012-01-29 15:50:43 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Anton Vorontsovbff38772009-07-08 11:10:56 -070015#include <linux/moduleparam.h>
Andy Shevchenko4cd57732013-06-04 19:46:26 +030016#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/netdevice.h>
18#include <linux/etherdevice.h>
19#include <linux/string.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020020#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040030#include <linux/export.h>
Amerigo Wang689971b2012-08-10 01:24:49 +000031#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/tcp.h>
33#include <net/udp.h>
Cong Wangb3d936f2013-01-07 20:52:41 +000034#include <net/addrconf.h>
35#include <net/ndisc.h>
36#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/unaligned.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -070038#include <trace/events/napi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080048static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Lai Jiangshan7f9421c2013-03-15 06:50:52 +000050DEFINE_STATIC_SRCU(netpoll_srcu);
Neil Hormanca99ca12013-02-05 08:05:43 +000051
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070052#define USEC_PER_POLL 50
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Joe Perches6f706242012-01-29 15:50:44 +000054#define MAX_SKB_SIZE \
55 (sizeof(struct ethhdr) + \
56 sizeof(struct iphdr) + \
57 sizeof(struct udphdr) + \
58 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David S. Miller3578b0c2010-08-03 00:24:04 -070060static void zap_completion_queue(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Anton Vorontsovbff38772009-07-08 11:10:56 -070062static unsigned int carrier_timeout = 4;
63module_param(carrier_timeout, uint, 0644);
64
Joe Perchese6ec269352012-01-29 15:50:43 +000065#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
Eric W. Biederman944e2942014-03-27 15:37:28 -070072static int netpoll_start_xmit(struct sk_buff *skb, struct net_device *dev,
73 struct netdev_queue *txq)
74{
Eric W. Biederman944e2942014-03-27 15:37:28 -070075 int status = NETDEV_TX_OK;
76 netdev_features_t features;
77
78 features = netif_skb_features(skb);
79
Jiri Pirkodf8a39d2015-01-13 17:13:44 +010080 if (skb_vlan_tag_present(skb) &&
Eric W. Biederman944e2942014-03-27 15:37:28 -070081 !vlan_hw_offload_capable(features, skb->vlan_proto)) {
Jiri Pirko59682502014-11-19 14:04:59 +010082 skb = __vlan_hwaccel_push_inside(skb);
Eric W. Biederman944e2942014-03-27 15:37:28 -070083 if (unlikely(!skb)) {
84 /* This is actually a packet drop, but we
85 * don't want the code that calls this
86 * function to try and operate on a NULL skb.
87 */
88 goto out;
89 }
Eric W. Biederman944e2942014-03-27 15:37:28 -070090 }
91
David S. Millerfa2dbdc22014-08-29 21:55:22 -070092 status = netdev_start_xmit(skb, dev, txq, false);
Eric W. Biederman944e2942014-03-27 15:37:28 -070093
94out:
95 return status;
96}
97
David Howellsc4028952006-11-22 14:57:56 +000098static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
David Howells4c1ac1b2006-12-05 14:37:56 +0000100 struct netpoll_info *npinfo =
101 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +0100103 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700105 while ((skb = skb_dequeue(&npinfo->txq))) {
106 struct net_device *dev = skb->dev;
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700107 struct netdev_queue *txq;
Tushar Davec70b17b72017-04-20 15:57:31 -0700108 unsigned int q_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700110 if (!netif_device_present(dev) || !netif_running(dev)) {
Eric W. Biederman080b3c12014-03-27 15:41:04 -0700111 kfree_skb(skb);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700112 continue;
113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Ingo Molnar36405432006-12-12 17:20:42 +0100115 local_irq_save(flags);
Tushar Davec70b17b72017-04-20 15:57:31 -0700116 /* check if skb->queue_mapping is still valid */
117 q_index = skb_get_queue_mapping(skb);
118 if (unlikely(q_index >= dev->real_num_tx_queues)) {
119 q_index = q_index % dev->real_num_tx_queues;
120 skb_set_queue_mapping(skb, q_index);
121 }
122 txq = netdev_get_tx_queue(dev, q_index);
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700123 HARD_TX_LOCK(dev, txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +0000124 if (netif_xmit_frozen_or_stopped(txq) ||
Eric W. Biederman944e2942014-03-27 15:37:28 -0700125 netpoll_start_xmit(skb, dev, txq) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700126 skb_queue_head(&npinfo->txq, skb);
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700127 HARD_TX_UNLOCK(dev, txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100128 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700130 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700131 return;
132 }
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700133 HARD_TX_UNLOCK(dev, txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100134 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136}
137
Alexander Duyck822d54b2015-09-28 09:16:17 -0700138static void poll_one_napi(struct napi_struct *napi)
David S. Miller0a7606c2007-10-29 21:28:47 -0700139{
Eric Dumazetc24498c2018-09-27 09:31:51 -0700140 int work;
David S. Miller0a7606c2007-10-29 21:28:47 -0700141
Neil Horman2d8bff1262015-09-23 14:57:58 -0400142 /* If we set this bit but see that it has already been set,
143 * that indicates that napi has been disabled and we need
144 * to abort this operation
145 */
146 if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
Alexander Duyck822d54b2015-09-28 09:16:17 -0700147 return;
David S. Miller0a7606c2007-10-29 21:28:47 -0700148
Alexander Duyck822d54b2015-09-28 09:16:17 -0700149 /* We explicilty pass the polling call a budget of 0 to
150 * indicate that we are clearing the Tx path only.
151 */
152 work = napi->poll(napi, 0);
Sakari Ailusd75f7732019-03-25 21:32:28 +0200153 WARN_ONCE(work, "%pS exceeded budget in poll\n", napi->poll);
Jesper Dangaard Brouer1db19db2016-07-07 18:01:32 +0200154 trace_napi_poll(napi, work, 0);
David S. Miller0a7606c2007-10-29 21:28:47 -0700155
Neil Horman7b363e42008-12-09 23:22:26 -0800156 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700157}
158
Alexander Duyck822d54b2015-09-28 09:16:17 -0700159static void poll_napi(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700161 struct napi_struct *napi;
Eric Dumazet89c4b442016-11-16 14:54:50 -0800162 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Neil Hormanf13d4932010-10-19 07:04:26 +0000164 list_for_each_entry(napi, &dev->napi_list, dev_list) {
Eric Dumazet89c4b442016-11-16 14:54:50 -0800165 if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) {
Alexander Duyck822d54b2015-09-28 09:16:17 -0700166 poll_one_napi(napi);
Eric Dumazet89c4b442016-11-16 14:54:50 -0800167 smp_store_release(&napi->poll_owner, -1);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170}
171
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700172void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Amerigo Wang28996562012-08-10 01:24:42 +0000174 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700175 const struct net_device_ops *ops;
Stephen Hemminger51069302007-11-19 19:18:11 -0800176
Neil Hormanca99ca12013-02-05 08:05:43 +0000177 /* Don't do any rx activity if the dev_lock mutex is held
178 * the dev_open/close paths use this to block netpoll activity
179 * while changing device state
180 */
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700181 if (!ni || down_trylock(&ni->dev_lock))
Neil Hormanca99ca12013-02-05 08:05:43 +0000182 return;
183
Neil Horman959d5fd2013-02-13 11:32:42 -0500184 if (!netif_running(dev)) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000185 up(&ni->dev_lock);
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000186 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500187 }
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000188
189 ops = dev->netdev_ops;
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700190 if (ops->ndo_poll_controller)
191 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Alexander Duyck822d54b2015-09-28 09:16:17 -0700193 poll_napi(dev);
Stephen Hemminger51069302007-11-19 19:18:11 -0800194
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000195 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000196
David S. Miller3578b0c2010-08-03 00:24:04 -0700197 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700199EXPORT_SYMBOL(netpoll_poll_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Eric W. Biederman66b55522014-03-27 15:39:03 -0700201void netpoll_poll_disable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000202{
203 struct netpoll_info *ni;
204 int idx;
205 might_sleep();
206 idx = srcu_read_lock(&netpoll_srcu);
207 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
208 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000209 down(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000210 srcu_read_unlock(&netpoll_srcu, idx);
Neil Hormanca99ca12013-02-05 08:05:43 +0000211}
Eric W. Biederman66b55522014-03-27 15:39:03 -0700212EXPORT_SYMBOL(netpoll_poll_disable);
Neil Hormanca99ca12013-02-05 08:05:43 +0000213
Eric W. Biederman66b55522014-03-27 15:39:03 -0700214void netpoll_poll_enable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000215{
216 struct netpoll_info *ni;
217 rcu_read_lock();
218 ni = rcu_dereference(dev->npinfo);
219 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000220 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000221 rcu_read_unlock();
222}
Eric W. Biederman66b55522014-03-27 15:39:03 -0700223EXPORT_SYMBOL(netpoll_poll_enable);
Neil Hormanca99ca12013-02-05 08:05:43 +0000224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static void refill_skbs(void)
226{
227 struct sk_buff *skb;
228 unsigned long flags;
229
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800230 spin_lock_irqsave(&skb_pool.lock, flags);
231 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
233 if (!skb)
234 break;
235
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800236 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800238 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
David S. Miller3578b0c2010-08-03 00:24:04 -0700241static void zap_completion_queue(void)
242{
243 unsigned long flags;
244 struct softnet_data *sd = &get_cpu_var(softnet_data);
245
246 if (sd->completion_queue) {
247 struct sk_buff *clist;
248
249 local_irq_save(flags);
250 clist = sd->completion_queue;
251 sd->completion_queue = NULL;
252 local_irq_restore(flags);
253
254 while (clist != NULL) {
255 struct sk_buff *skb = clist;
256 clist = clist->next;
Eric W. Biedermanb1586f02014-04-01 12:21:02 -0700257 if (!skb_irq_freeable(skb)) {
WANG Cong230cd122017-07-12 15:56:41 -0700258 refcount_set(&skb->users, 1);
David S. Miller3578b0c2010-08-03 00:24:04 -0700259 dev_kfree_skb_any(skb); /* put this one back */
260 } else {
261 __kfree_skb(skb);
262 }
263 }
264 }
265
266 put_cpu_var(softnet_data);
267}
268
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800269static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800271 int count = 0;
272 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David S. Miller3578b0c2010-08-03 00:24:04 -0700274 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800275 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800279 if (!skb)
280 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800283 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000284 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800285 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800287 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Reshetova, Elena63354792017-06-30 13:07:58 +0300290 refcount_set(&skb->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 skb_reserve(skb, reserve);
292 return skb;
293}
294
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700295static int netpoll_owner_active(struct net_device *dev)
296{
297 struct napi_struct *napi;
298
299 list_for_each_entry(napi, &dev->napi_list, dev_list) {
300 if (napi->poll_owner == smp_processor_id())
301 return 1;
302 }
303 return 0;
304}
305
Amerigo Wang28996562012-08-10 01:24:42 +0000306/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000307void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
308 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700310 int status = NETDEV_TX_BUSY;
311 unsigned long tries;
Herbert Xude85d992010-06-10 16:12:44 +0000312 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000313 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Frederic Weisbeckeraf073392017-11-06 16:01:29 +0100315 lockdep_assert_irqs_disabled();
Amerigo Wang28996562012-08-10 01:24:42 +0000316
317 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900318 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
Eric W. Biederman080b3c12014-03-27 15:41:04 -0700319 dev_kfree_skb_irq(skb);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900320 return;
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700323 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700324 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700325 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800326
Paolo Abeni4bd97d52019-03-20 11:02:04 +0100327 txq = netdev_core_pick_tx(dev, skb, NULL);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700328
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700329 /* try until next clock tick */
330 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
331 tries > 0; --tries) {
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700332 if (HARD_TX_TRYLOCK(dev, txq)) {
Eric W. Biederman944e2942014-03-27 15:37:28 -0700333 if (!netif_xmit_stopped(txq))
334 status = netpoll_start_xmit(skb, dev, txq);
Amerigo Wang689971b2012-08-10 01:24:49 +0000335
Eric W. Biederman5efeac42014-03-27 15:42:20 -0700336 HARD_TX_UNLOCK(dev, txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700337
Andrew Mortone37b8d92006-12-09 14:01:49 -0800338 if (status == NETDEV_TX_OK)
339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Andrew Mortone37b8d92006-12-09 14:01:49 -0800341 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700342
343 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000344 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700345
346 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700347 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000348
349 WARN_ONCE(!irqs_disabled(),
Sakari Ailusd75f7732019-03-25 21:32:28 +0200350 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n",
Eric W. Biederman944e2942014-03-27 15:37:28 -0700351 dev->name, dev->netdev_ops->ndo_start_xmit);
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700355 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700356 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000357 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
Neil Hormanc2355e12010-10-13 16:01:49 +0000360EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
363{
Eric Dumazet954fba022012-06-12 19:30:21 +0000364 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct sk_buff *skb;
366 struct udphdr *udph;
367 struct iphdr *iph;
368 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000369 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000370 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Nikolay Aleksandrovc9fd56b2015-08-28 15:44:25 -0700372 WARN_ON_ONCE(!irqs_disabled());
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000375 if (np->ipv6)
376 ip_len = udp_len + sizeof(*ip6h);
377 else
Cong Wangb7394d22013-01-07 20:52:39 +0000378 ip_len = udp_len + sizeof(*iph);
379
Eric Dumazet954fba022012-06-12 19:30:21 +0000380 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Eric Dumazet954fba022012-06-12 19:30:21 +0000382 skb = find_skb(np, total_len + np->dev->needed_tailroom,
383 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!skb)
385 return;
386
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300387 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba022012-06-12 19:30:21 +0000388 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300390 skb_push(skb, sizeof(*udph));
391 skb_reset_transport_header(skb);
392 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 udph->source = htons(np->local_port);
394 udph->dest = htons(np->remote_port);
395 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Cong Wangb3d936f2013-01-07 20:52:41 +0000397 if (np->ipv6) {
398 udph->check = 0;
399 udph->check = csum_ipv6_magic(&np->local_ip.in6,
400 &np->remote_ip.in6,
401 udp_len, IPPROTO_UDP,
402 csum_partial(udph, udp_len, 0));
403 if (udph->check == 0)
404 udph->check = CSUM_MANGLED_0;
405
406 skb_push(skb, sizeof(*ip6h));
407 skb_reset_network_header(skb);
408 ip6h = ipv6_hdr(skb);
409
410 /* ip6h->version = 6; ip6h->priority = 0; */
411 put_unaligned(0x60, (unsigned char *)ip6h);
412 ip6h->flow_lbl[0] = 0;
413 ip6h->flow_lbl[1] = 0;
414 ip6h->flow_lbl[2] = 0;
415
416 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
417 ip6h->nexthdr = IPPROTO_UDP;
418 ip6h->hop_limit = 32;
419 ip6h->saddr = np->local_ip.in6;
420 ip6h->daddr = np->remote_ip.in6;
421
Johannes Bergd58ff352017-06-16 14:29:23 +0200422 eth = skb_push(skb, ETH_HLEN);
Cong Wangb3d936f2013-01-07 20:52:41 +0000423 skb_reset_mac_header(skb);
424 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
425 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000426 udph->check = 0;
427 udph->check = csum_tcpudp_magic(np->local_ip.ip,
428 np->remote_ip.ip,
429 udp_len, IPPROTO_UDP,
430 csum_partial(udph, udp_len, 0));
431 if (udph->check == 0)
432 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Cong Wangb7394d22013-01-07 20:52:39 +0000434 skb_push(skb, sizeof(*iph));
435 skb_reset_network_header(skb);
436 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Cong Wangb7394d22013-01-07 20:52:39 +0000438 /* iph->version = 4; iph->ihl = 5; */
439 put_unaligned(0x45, (unsigned char *)iph);
440 iph->tos = 0;
441 put_unaligned(htons(ip_len), &(iph->tot_len));
442 iph->id = htons(atomic_inc_return(&ip_ident));
443 iph->frag_off = 0;
444 iph->ttl = 64;
445 iph->protocol = IPPROTO_UDP;
446 iph->check = 0;
447 put_unaligned(np->local_ip.ip, &(iph->saddr));
448 put_unaligned(np->remote_ip.ip, &(iph->daddr));
449 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
450
Johannes Bergd58ff352017-06-16 14:29:23 +0200451 eth = skb_push(skb, ETH_HLEN);
Cong Wangb7394d22013-01-07 20:52:39 +0000452 skb_reset_mac_header(skb);
453 skb->protocol = eth->h_proto = htons(ETH_P_IP);
454 }
455
Joe Perchesc62326a2014-01-20 09:52:18 -0800456 ether_addr_copy(eth->h_source, np->dev->dev_addr);
457 ether_addr_copy(eth->h_dest, np->remote_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 skb->dev = np->dev;
460
461 netpoll_send_skb(np, skb);
462}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000463EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700465void netpoll_print_options(struct netpoll *np)
466{
Joe Perchese6ec269352012-01-29 15:50:43 +0000467 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000468 if (np->ipv6)
469 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
470 else
Cong Wangb7394d22013-01-07 20:52:39 +0000471 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000472 np_info(np, "interface '%s'\n", np->dev_name);
473 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000474 if (np->ipv6)
475 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
476 else
Cong Wangb7394d22013-01-07 20:52:39 +0000477 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000478 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700479}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000480EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700481
Cong Wangb7394d22013-01-07 20:52:39 +0000482static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
483{
484 const char *end;
485
486 if (!strchr(str, ':') &&
487 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
488 if (!*end)
489 return 0;
490 }
491 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
492#if IS_ENABLED(CONFIG_IPV6)
493 if (!*end)
494 return 1;
495#else
496 return -1;
497#endif
498 }
499 return -1;
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502int netpoll_parse_options(struct netpoll *np, char *opt)
503{
504 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +0000505 int ipv6;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100506 bool ipversion_set = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
David S. Millerc68b9072006-11-14 20:40:49 -0800508 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if ((delim = strchr(cur, '@')) == NULL)
510 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800511 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000512 if (kstrtou16(cur, 10, &np->local_port))
513 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800514 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
David S. Millerc68b9072006-11-14 20:40:49 -0800518 if (*cur != '/') {
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100519 ipversion_set = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if ((delim = strchr(cur, '/')) == NULL)
521 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800522 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000523 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
524 if (ipv6 < 0)
525 goto parse_failed;
526 else
527 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800528 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 cur++;
531
David S. Millerc68b9072006-11-14 20:40:49 -0800532 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* parse out dev name */
534 if ((delim = strchr(cur, ',')) == NULL)
535 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800536 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800538 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 cur++;
541
David S. Millerc68b9072006-11-14 20:40:49 -0800542 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* dst port */
544 if ((delim = strchr(cur, '@')) == NULL)
545 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800546 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000547 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000548 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000549 if (kstrtou16(cur, 10, &np->remote_port))
550 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800551 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* dst ip */
556 if ((delim = strchr(cur, '/')) == NULL)
557 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800558 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000559 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
560 if (ipv6 < 0)
561 goto parse_failed;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100562 else if (ipversion_set && np->ipv6 != (bool)ipv6)
Cong Wangb7394d22013-01-07 20:52:39 +0000563 goto parse_failed;
564 else
565 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800566 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
David S. Millerc68b9072006-11-14 20:40:49 -0800568 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +0000570 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700574 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 return 0;
577
578 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +0000579 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -1;
581}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000582EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700584int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000585{
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000586 struct netpoll_info *npinfo;
587 const struct net_device_ops *ops;
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000588 int err;
589
Bjorn Helgaas727ceaa2016-04-05 15:58:22 -0500590 np->dev = ndev;
Jiri Pirko30fdd8a02012-07-17 05:22:35 +0000591 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
592
Eric Dumazetac3d9dd2018-09-21 15:27:38 -0700593 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000594 np_err(np, "%s doesn't support polling, aborting\n",
595 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000596 err = -ENOTSUPP;
597 goto out;
598 }
599
600 if (!ndev->npinfo) {
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700601 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000602 if (!npinfo) {
603 err = -ENOMEM;
604 goto out;
605 }
606
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000607 sema_init(&npinfo->dev_lock, 1);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000608 skb_queue_head_init(&npinfo->txq);
609 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
610
Reshetova, Elena433cea42017-06-30 13:08:04 +0300611 refcount_set(&npinfo->refcnt, 1);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000612
613 ops = np->dev->netdev_ops;
614 if (ops->ndo_netpoll_setup) {
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700615 err = ops->ndo_netpoll_setup(ndev, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000616 if (err)
617 goto free_npinfo;
618 }
619 } else {
Neil Horman0790bbb2013-02-11 10:25:31 +0000620 npinfo = rtnl_dereference(ndev->npinfo);
Reshetova, Elena433cea42017-06-30 13:08:04 +0300621 refcount_inc(&npinfo->refcnt);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000622 }
623
624 npinfo->netpoll = np;
625
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000626 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000627 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000628
629 return 0;
630
631free_npinfo:
632 kfree(npinfo);
633out:
634 return err;
635}
636EXPORT_SYMBOL_GPL(__netpoll_setup);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638int netpoll_setup(struct netpoll *np)
639{
640 struct net_device *ndev = NULL;
641 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700642 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Cong Wangf92d3182013-01-14 23:34:06 +0000644 rtnl_lock();
Matthias Kaehlcke0c3a8f82017-07-25 11:36:25 -0700645 if (np->dev_name[0]) {
Cong Wang556e6252013-01-27 15:55:21 +0000646 struct net *net = current->nsproxy->net_ns;
647 ndev = __dev_get_by_name(net, np->dev_name);
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000650 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +0000651 err = -ENODEV;
652 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Cong Wang5bd30d32013-01-17 12:21:08 +0800654 dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000656 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000657 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -0700658 err = -EBUSY;
659 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -0700660 }
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (!netif_running(ndev)) {
663 unsigned long atmost, atleast;
664
Joe Perchese6ec269352012-01-29 15:50:43 +0000665 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Petr Machata00f54e62018-12-06 17:05:36 +0000667 err = dev_open(ndev, NULL);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700668
669 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000670 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +0000671 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Cong Wangf92d3182013-01-14 23:34:06 +0000674 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -0700676 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 while (!netif_carrier_ok(ndev)) {
678 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000679 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 break;
681 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -0700682 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 /* If carrier appears to come up instantly, we don't
686 * trust it and pause so that we don't pump all our
687 * queued console messages into the bitbucket.
688 */
689
690 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000691 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 msleep(4000);
693 }
Cong Wangf92d3182013-01-14 23:34:06 +0000694 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
Cong Wangb7394d22013-01-07 20:52:39 +0000697 if (!np->local_ip.ip) {
698 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +0000699 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +0000700
701 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +0000702 np_err(np, "no IP address for %s, aborting\n",
703 np->dev_name);
704 err = -EDESTADDRREQ;
705 goto put;
706 }
707
708 np->local_ip.ip = in_dev->ifa_list->ifa_local;
Cong Wangb7394d22013-01-07 20:52:39 +0000709 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +0000710 } else {
711#if IS_ENABLED(CONFIG_IPV6)
712 struct inet6_dev *idev;
713
714 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +0000715 idev = __in6_dev_get(ndev);
716 if (idev) {
717 struct inet6_ifaddr *ifp;
718
719 read_lock_bh(&idev->lock);
720 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Matwey V. Kornilovd016b4a2018-11-02 21:19:36 +0300721 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
722 !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
Cong Wangb3d936f2013-01-07 20:52:41 +0000723 continue;
724 np->local_ip.in6 = ifp->addr;
725 err = 0;
726 break;
727 }
728 read_unlock_bh(&idev->lock);
729 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000730 if (err) {
731 np_err(np, "no IPv6 address for %s, aborting\n",
732 np->dev_name);
733 goto put;
734 } else
735 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
736#else
737 np_err(np, "IPv6 is not supported %s, aborting\n",
738 np->dev_name);
Cong Wange39363a2013-01-22 17:39:11 +0000739 err = -EINVAL;
Cong Wangb3d936f2013-01-07 20:52:41 +0000740 goto put;
741#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Herbert Xudbaa1542010-06-10 16:12:46 +0000745 /* fill up the skb queue */
746 refill_skbs();
747
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700748 err = __netpoll_setup(np, ndev);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000749 if (err)
750 goto put;
751
Cong Wangf92d3182013-01-14 23:34:06 +0000752 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return 0;
754
Jiri Slaby21edbb22010-03-16 05:29:54 +0000755put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +0000757unlock:
758 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700759 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000761EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
David S. Millerc68b9072006-11-14 20:40:49 -0800763static int __init netpoll_init(void)
764{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800765 skb_queue_head_init(&skb_pool);
766 return 0;
767}
768core_initcall(netpoll_init);
769
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000770static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
771{
772 struct netpoll_info *npinfo =
773 container_of(rcu_head, struct netpoll_info, rcu);
774
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000775 skb_queue_purge(&npinfo->txq);
776
777 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
778 cancel_delayed_work(&npinfo->tx_work);
779
780 /* clean after last, unfinished work */
781 __skb_queue_purge(&npinfo->txq);
782 /* now cancel it again */
783 cancel_delayed_work(&npinfo->tx_work);
784 kfree(npinfo);
785}
786
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000787void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700789 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700790
Neil Horman0790bbb2013-02-11 10:25:31 +0000791 npinfo = rtnl_dereference(np->dev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000792 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +0000793 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700794
Neil Hormanca99ca12013-02-05 08:05:43 +0000795 synchronize_srcu(&netpoll_srcu);
796
Reshetova, Elena433cea42017-06-30 13:08:04 +0300797 if (refcount_dec_and_test(&npinfo->refcnt)) {
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000798 const struct net_device_ops *ops;
799
800 ops = np->dev->netdev_ops;
801 if (ops->ndo_netpoll_cleanup)
802 ops->ndo_netpoll_cleanup(np->dev);
803
Monam Agarwalfcb144b2014-03-24 00:42:46 +0530804 RCU_INIT_POINTER(np->dev->npinfo, NULL);
Paul E. McKenney5da54c12018-11-06 19:43:32 -0800805 call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info);
david decotignyefa95b02014-07-08 15:14:41 -0700806 } else
807 RCU_INIT_POINTER(np->dev->npinfo, NULL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000808}
809EXPORT_SYMBOL_GPL(__netpoll_cleanup);
810
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400811void __netpoll_free(struct netpoll *np)
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000812{
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400813 ASSERT_RTNL();
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000814
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400815 /* Wait for transmitting packets to finish before freeing. */
Paul E. McKenney5da54c12018-11-06 19:43:32 -0800816 synchronize_rcu();
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000817 __netpoll_cleanup(np);
818 kfree(np);
819}
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400820EXPORT_SYMBOL_GPL(__netpoll_free);
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000821
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000822void netpoll_cleanup(struct netpoll *np)
823{
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000824 rtnl_lock();
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +0200825 if (!np->dev)
826 goto out;
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000827 __netpoll_cleanup(np);
Herbert Xudbaa1542010-06-10 16:12:46 +0000828 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 np->dev = NULL;
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +0200830out:
831 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000833EXPORT_SYMBOL(netpoll_cleanup);