blob: 2c6379da295ca7850fa49dce7196f515fb2ab326 [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);
Neil Horman2cde6ac2013-02-11 10:25:30 +000060static void netpoll_async_cleanup(struct work_struct *work);
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
David Howellsc4028952006-11-22 14:57:56 +000072static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
David Howells4c1ac1b2006-12-05 14:37:56 +000074 struct netpoll_info *npinfo =
75 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010077 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070079 while ((skb = skb_dequeue(&npinfo->txq))) {
80 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080081 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070082 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070084 if (!netif_device_present(dev) || !netif_running(dev)) {
85 __kfree_skb(skb);
86 continue;
87 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
David S. Millerfd2ea0a2008-07-17 01:56:23 -070089 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
90
Ingo Molnar36405432006-12-12 17:20:42 +010091 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070092 __netif_tx_lock(txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +000093 if (netif_xmit_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080094 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070095 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070096 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +010097 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Jarek Poplawski25442ca2007-07-05 17:42:44 -070099 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700100 return;
101 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700102 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100103 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * Check whether delayed processing was scheduled for our NIC. If so,
109 * we attempt to grab the poll lock and use ->poll() to pump the card.
110 * If this fails, either we've recursed in ->poll() or it's already
111 * running on another CPU.
112 *
113 * Note: we don't mask interrupts with this lock because we're using
114 * trylock here and interrupts are already disabled in the softirq
115 * case. Further, we test the poll_owner to avoid recursion on UP
116 * systems where the lock doesn't exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700118static int poll_one_napi(struct napi_struct *napi, int budget)
David S. Miller0a7606c2007-10-29 21:28:47 -0700119{
120 int work;
121
122 /* net_rx_action's ->poll() invocations and our's are
123 * synchronized by this test which is only made while
124 * holding the napi->poll_lock.
125 */
126 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
127 return budget;
128
Neil Horman7b363e42008-12-09 23:22:26 -0800129 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700130
131 work = napi->poll(napi, budget);
Eric W. Biedermane97dc3f2014-03-14 20:47:15 -0700132 WARN_ONCE(work > budget, "%pF exceeded budget in poll\n", napi->poll);
David S. Miller7d18f112009-05-21 23:30:09 -0700133 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700134
Neil Horman7b363e42008-12-09 23:22:26 -0800135 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700136
137 return budget - work;
138}
139
Eric W. Biederman9852fbe2014-03-14 20:45:17 -0700140static void poll_napi(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700142 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Neil Hormanf13d4932010-10-19 07:04:26 +0000144 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700145 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700146 spin_trylock(&napi->poll_lock)) {
Eric W. Biedermanb249b512014-03-14 20:44:37 -0700147 budget = poll_one_napi(napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700148 spin_unlock(&napi->poll_lock);
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151}
152
Joe Perches234b9212011-06-30 15:08:57 +0000153static void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000155 const struct net_device_ops *ops;
Amerigo Wang28996562012-08-10 01:24:42 +0000156 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Eric W. Biederman9c62a682014-03-14 20:51:52 -0700157 int budget = 0;
Stephen Hemminger51069302007-11-19 19:18:11 -0800158
Neil Hormanca99ca12013-02-05 08:05:43 +0000159 /* Don't do any rx activity if the dev_lock mutex is held
160 * the dev_open/close paths use this to block netpoll activity
161 * while changing device state
162 */
Dan Carpentera3dbbc22013-05-06 02:15:13 +0000163 if (down_trylock(&ni->dev_lock))
Neil Hormanca99ca12013-02-05 08:05:43 +0000164 return;
165
Neil Horman959d5fd2013-02-13 11:32:42 -0500166 if (!netif_running(dev)) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000167 up(&ni->dev_lock);
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000168 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500169 }
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000170
171 ops = dev->netdev_ops;
Neil Horman959d5fd2013-02-13 11:32:42 -0500172 if (!ops->ndo_poll_controller) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000173 up(&ni->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800178 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Eric W. Biederman9852fbe2014-03-14 20:45:17 -0700180 poll_napi(dev, budget);
Stephen Hemminger51069302007-11-19 19:18:11 -0800181
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000182 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000183
David S. Miller3578b0c2010-08-03 00:24:04 -0700184 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
dingtianhongda6e3782013-05-27 19:53:31 +0000187void netpoll_rx_disable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000188{
189 struct netpoll_info *ni;
190 int idx;
191 might_sleep();
192 idx = srcu_read_lock(&netpoll_srcu);
193 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
194 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000195 down(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000196 srcu_read_unlock(&netpoll_srcu, idx);
Neil Hormanca99ca12013-02-05 08:05:43 +0000197}
198EXPORT_SYMBOL(netpoll_rx_disable);
199
200void netpoll_rx_enable(struct net_device *dev)
201{
202 struct netpoll_info *ni;
203 rcu_read_lock();
204 ni = rcu_dereference(dev->npinfo);
205 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000206 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000207 rcu_read_unlock();
208}
209EXPORT_SYMBOL(netpoll_rx_enable);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static void refill_skbs(void)
212{
213 struct sk_buff *skb;
214 unsigned long flags;
215
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800216 spin_lock_irqsave(&skb_pool.lock, flags);
217 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
219 if (!skb)
220 break;
221
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800222 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800224 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
David S. Miller3578b0c2010-08-03 00:24:04 -0700227static void zap_completion_queue(void)
228{
229 unsigned long flags;
230 struct softnet_data *sd = &get_cpu_var(softnet_data);
231
232 if (sd->completion_queue) {
233 struct sk_buff *clist;
234
235 local_irq_save(flags);
236 clist = sd->completion_queue;
237 sd->completion_queue = NULL;
238 local_irq_restore(flags);
239
240 while (clist != NULL) {
241 struct sk_buff *skb = clist;
242 clist = clist->next;
243 if (skb->destructor) {
244 atomic_inc(&skb->users);
245 dev_kfree_skb_any(skb); /* put this one back */
246 } else {
247 __kfree_skb(skb);
248 }
249 }
250 }
251
252 put_cpu_var(softnet_data);
253}
254
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800255static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800257 int count = 0;
258 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
David S. Miller3578b0c2010-08-03 00:24:04 -0700260 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800261 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800265 if (!skb)
266 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800269 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000270 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800271 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800273 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
276 atomic_set(&skb->users, 1);
277 skb_reserve(skb, reserve);
278 return skb;
279}
280
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700281static int netpoll_owner_active(struct net_device *dev)
282{
283 struct napi_struct *napi;
284
285 list_for_each_entry(napi, &dev->napi_list, dev_list) {
286 if (napi->poll_owner == smp_processor_id())
287 return 1;
288 }
289 return 0;
290}
291
Amerigo Wang28996562012-08-10 01:24:42 +0000292/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000293void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
294 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700296 int status = NETDEV_TX_BUSY;
297 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800298 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000299 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000300 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Amerigo Wang28996562012-08-10 01:24:42 +0000302 WARN_ON_ONCE(!irqs_disabled());
303
304 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900305 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
306 __kfree_skb(skb);
307 return;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700310 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700311 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700312 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800313
Jason Wangf663dd92014-01-10 16:18:26 +0800314 txq = netdev_pick_tx(dev, skb, NULL);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700315
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700316 /* try until next clock tick */
317 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
318 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700319 if (__netif_tx_trylock(txq)) {
Tom Herbert734664982011-11-28 16:32:44 +0000320 if (!netif_xmit_stopped(txq)) {
Amerigo Wang689971b2012-08-10 01:24:49 +0000321 if (vlan_tx_tag_present(skb) &&
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000322 !vlan_hw_offload_capable(netif_skb_features(skb),
323 skb->vlan_proto)) {
324 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
David S. Milleraca5f582014-01-02 19:50:52 -0500325 if (unlikely(!skb)) {
326 /* This is actually a packet drop, but we
327 * don't want the code at the end of this
328 * function to try and re-queue a NULL skb.
329 */
330 status = NETDEV_TX_OK;
331 goto unlock_txq;
332 }
Amerigo Wang689971b2012-08-10 01:24:49 +0000333 skb->vlan_tci = 0;
334 }
335
Stephen Hemminger00829822008-11-20 20:14:53 -0800336 status = ops->ndo_start_xmit(skb, dev);
Eric Dumazet08baf562009-05-25 22:58:01 -0700337 if (status == NETDEV_TX_OK)
338 txq_trans_update(txq);
339 }
David S. Milleraca5f582014-01-02 19:50:52 -0500340 unlock_txq:
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700341 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700342
Andrew Mortone37b8d92006-12-09 14:01:49 -0800343 if (status == NETDEV_TX_OK)
344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Andrew Mortone37b8d92006-12-09 14:01:49 -0800346 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700347
348 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000349 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700350
351 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700352 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000353
354 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000355 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000356 dev->name, ops->ndo_start_xmit);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700360 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700361 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000362 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Neil Hormanc2355e12010-10-13 16:01:49 +0000365EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
368{
Eric Dumazet954fba02012-06-12 19:30:21 +0000369 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct sk_buff *skb;
371 struct udphdr *udph;
372 struct iphdr *iph;
373 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000374 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000375 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000378 if (np->ipv6)
379 ip_len = udp_len + sizeof(*ip6h);
380 else
Cong Wangb7394d22013-01-07 20:52:39 +0000381 ip_len = udp_len + sizeof(*iph);
382
Eric Dumazet954fba02012-06-12 19:30:21 +0000383 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Eric Dumazet954fba02012-06-12 19:30:21 +0000385 skb = find_skb(np, total_len + np->dev->needed_tailroom,
386 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!skb)
388 return;
389
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300390 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba02012-06-12 19:30:21 +0000391 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300393 skb_push(skb, sizeof(*udph));
394 skb_reset_transport_header(skb);
395 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 udph->source = htons(np->local_port);
397 udph->dest = htons(np->remote_port);
398 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Cong Wangb3d936f2013-01-07 20:52:41 +0000400 if (np->ipv6) {
401 udph->check = 0;
402 udph->check = csum_ipv6_magic(&np->local_ip.in6,
403 &np->remote_ip.in6,
404 udp_len, IPPROTO_UDP,
405 csum_partial(udph, udp_len, 0));
406 if (udph->check == 0)
407 udph->check = CSUM_MANGLED_0;
408
409 skb_push(skb, sizeof(*ip6h));
410 skb_reset_network_header(skb);
411 ip6h = ipv6_hdr(skb);
412
413 /* ip6h->version = 6; ip6h->priority = 0; */
414 put_unaligned(0x60, (unsigned char *)ip6h);
415 ip6h->flow_lbl[0] = 0;
416 ip6h->flow_lbl[1] = 0;
417 ip6h->flow_lbl[2] = 0;
418
419 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
420 ip6h->nexthdr = IPPROTO_UDP;
421 ip6h->hop_limit = 32;
422 ip6h->saddr = np->local_ip.in6;
423 ip6h->daddr = np->remote_ip.in6;
424
425 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
426 skb_reset_mac_header(skb);
427 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
428 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000429 udph->check = 0;
430 udph->check = csum_tcpudp_magic(np->local_ip.ip,
431 np->remote_ip.ip,
432 udp_len, IPPROTO_UDP,
433 csum_partial(udph, udp_len, 0));
434 if (udph->check == 0)
435 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Cong Wangb7394d22013-01-07 20:52:39 +0000437 skb_push(skb, sizeof(*iph));
438 skb_reset_network_header(skb);
439 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Cong Wangb7394d22013-01-07 20:52:39 +0000441 /* iph->version = 4; iph->ihl = 5; */
442 put_unaligned(0x45, (unsigned char *)iph);
443 iph->tos = 0;
444 put_unaligned(htons(ip_len), &(iph->tot_len));
445 iph->id = htons(atomic_inc_return(&ip_ident));
446 iph->frag_off = 0;
447 iph->ttl = 64;
448 iph->protocol = IPPROTO_UDP;
449 iph->check = 0;
450 put_unaligned(np->local_ip.ip, &(iph->saddr));
451 put_unaligned(np->remote_ip.ip, &(iph->daddr));
452 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
453
454 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
455 skb_reset_mac_header(skb);
456 skb->protocol = eth->h_proto = htons(ETH_P_IP);
457 }
458
Joe Perchesc62326a2014-01-20 09:52:18 -0800459 ether_addr_copy(eth->h_source, np->dev->dev_addr);
460 ether_addr_copy(eth->h_dest, np->remote_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 skb->dev = np->dev;
463
464 netpoll_send_skb(np, skb);
465}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000466EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700468void netpoll_print_options(struct netpoll *np)
469{
Joe Perchese6ec269352012-01-29 15:50:43 +0000470 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000471 if (np->ipv6)
472 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
473 else
Cong Wangb7394d22013-01-07 20:52:39 +0000474 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000475 np_info(np, "interface '%s'\n", np->dev_name);
476 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000477 if (np->ipv6)
478 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
479 else
Cong Wangb7394d22013-01-07 20:52:39 +0000480 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000481 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700482}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000483EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700484
Cong Wangb7394d22013-01-07 20:52:39 +0000485static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
486{
487 const char *end;
488
489 if (!strchr(str, ':') &&
490 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
491 if (!*end)
492 return 0;
493 }
494 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
495#if IS_ENABLED(CONFIG_IPV6)
496 if (!*end)
497 return 1;
498#else
499 return -1;
500#endif
501 }
502 return -1;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505int netpoll_parse_options(struct netpoll *np, char *opt)
506{
507 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +0000508 int ipv6;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100509 bool ipversion_set = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
David S. Millerc68b9072006-11-14 20:40:49 -0800511 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if ((delim = strchr(cur, '@')) == NULL)
513 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800514 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000515 if (kstrtou16(cur, 10, &np->local_port))
516 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800517 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
David S. Millerc68b9072006-11-14 20:40:49 -0800521 if (*cur != '/') {
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100522 ipversion_set = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if ((delim = strchr(cur, '/')) == NULL)
524 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800525 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000526 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
527 if (ipv6 < 0)
528 goto parse_failed;
529 else
530 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800531 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 cur++;
534
David S. Millerc68b9072006-11-14 20:40:49 -0800535 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* parse out dev name */
537 if ((delim = strchr(cur, ',')) == NULL)
538 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800539 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800541 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 cur++;
544
David S. Millerc68b9072006-11-14 20:40:49 -0800545 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* dst port */
547 if ((delim = strchr(cur, '@')) == NULL)
548 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800549 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000550 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000551 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000552 if (kstrtou16(cur, 10, &np->remote_port))
553 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800554 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* dst ip */
559 if ((delim = strchr(cur, '/')) == NULL)
560 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800561 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000562 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
563 if (ipv6 < 0)
564 goto parse_failed;
Sabrina Dubroca00fe11b2014-02-06 18:34:12 +0100565 else if (ipversion_set && np->ipv6 != (bool)ipv6)
Cong Wangb7394d22013-01-07 20:52:39 +0000566 goto parse_failed;
567 else
568 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800569 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
David S. Millerc68b9072006-11-14 20:40:49 -0800571 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +0000573 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700577 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 return 0;
580
581 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +0000582 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -1;
584}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000585EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700587int __netpoll_setup(struct netpoll *np, struct net_device *ndev)
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000588{
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000589 struct netpoll_info *npinfo;
590 const struct net_device_ops *ops;
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000591 int err;
592
Jiri Pirko30fdd8a02012-07-17 05:22:35 +0000593 np->dev = ndev;
594 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
Neil Horman2cde6ac2013-02-11 10:25:30 +0000595 INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
Jiri Pirko30fdd8a02012-07-17 05:22:35 +0000596
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000597 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
598 !ndev->netdev_ops->ndo_poll_controller) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000599 np_err(np, "%s doesn't support polling, aborting\n",
600 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000601 err = -ENOTSUPP;
602 goto out;
603 }
604
605 if (!ndev->npinfo) {
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700606 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000607 if (!npinfo) {
608 err = -ENOMEM;
609 goto out;
610 }
611
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000612 sema_init(&npinfo->dev_lock, 1);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000613 skb_queue_head_init(&npinfo->txq);
614 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
615
616 atomic_set(&npinfo->refcnt, 1);
617
618 ops = np->dev->netdev_ops;
619 if (ops->ndo_netpoll_setup) {
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700620 err = ops->ndo_netpoll_setup(ndev, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000621 if (err)
622 goto free_npinfo;
623 }
624 } else {
Neil Horman0790bbb2013-02-11 10:25:31 +0000625 npinfo = rtnl_dereference(ndev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000626 atomic_inc(&npinfo->refcnt);
627 }
628
629 npinfo->netpoll = np;
630
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000631 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000632 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000633
634 return 0;
635
636free_npinfo:
637 kfree(npinfo);
638out:
639 return err;
640}
641EXPORT_SYMBOL_GPL(__netpoll_setup);
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643int netpoll_setup(struct netpoll *np)
644{
645 struct net_device *ndev = NULL;
646 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700647 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Cong Wangf92d3182013-01-14 23:34:06 +0000649 rtnl_lock();
Cong Wang556e6252013-01-27 15:55:21 +0000650 if (np->dev_name) {
651 struct net *net = current->nsproxy->net_ns;
652 ndev = __dev_get_by_name(net, np->dev_name);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000655 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +0000656 err = -ENODEV;
657 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Cong Wang5bd30d32013-01-17 12:21:08 +0800659 dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000661 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000662 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -0700663 err = -EBUSY;
664 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -0700665 }
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!netif_running(ndev)) {
668 unsigned long atmost, atleast;
669
Joe Perchese6ec269352012-01-29 15:50:43 +0000670 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700672 err = dev_open(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700673
674 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000675 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +0000676 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Cong Wangf92d3182013-01-14 23:34:06 +0000679 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -0700681 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 while (!netif_carrier_ok(ndev)) {
683 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000684 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 break;
686 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -0700687 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
690 /* If carrier appears to come up instantly, we don't
691 * trust it and pause so that we don't pump all our
692 * queued console messages into the bitbucket.
693 */
694
695 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000696 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 msleep(4000);
698 }
Cong Wangf92d3182013-01-14 23:34:06 +0000699 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701
Cong Wangb7394d22013-01-07 20:52:39 +0000702 if (!np->local_ip.ip) {
703 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +0000704 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +0000705
706 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +0000707 np_err(np, "no IP address for %s, aborting\n",
708 np->dev_name);
709 err = -EDESTADDRREQ;
710 goto put;
711 }
712
713 np->local_ip.ip = in_dev->ifa_list->ifa_local;
Cong Wangb7394d22013-01-07 20:52:39 +0000714 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +0000715 } else {
716#if IS_ENABLED(CONFIG_IPV6)
717 struct inet6_dev *idev;
718
719 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +0000720 idev = __in6_dev_get(ndev);
721 if (idev) {
722 struct inet6_ifaddr *ifp;
723
724 read_lock_bh(&idev->lock);
725 list_for_each_entry(ifp, &idev->addr_list, if_list) {
726 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
727 continue;
728 np->local_ip.in6 = ifp->addr;
729 err = 0;
730 break;
731 }
732 read_unlock_bh(&idev->lock);
733 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000734 if (err) {
735 np_err(np, "no IPv6 address for %s, aborting\n",
736 np->dev_name);
737 goto put;
738 } else
739 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
740#else
741 np_err(np, "IPv6 is not supported %s, aborting\n",
742 np->dev_name);
Cong Wange39363a2013-01-22 17:39:11 +0000743 err = -EINVAL;
Cong Wangb3d936f2013-01-07 20:52:41 +0000744 goto put;
745#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Herbert Xudbaa1542010-06-10 16:12:46 +0000749 /* fill up the skb queue */
750 refill_skbs();
751
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700752 err = __netpoll_setup(np, ndev);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000753 if (err)
754 goto put;
755
Cong Wangf92d3182013-01-14 23:34:06 +0000756 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758
Jiri Slaby21edbb22010-03-16 05:29:54 +0000759put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +0000761unlock:
762 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700763 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000765EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
David S. Millerc68b9072006-11-14 20:40:49 -0800767static int __init netpoll_init(void)
768{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800769 skb_queue_head_init(&skb_pool);
770 return 0;
771}
772core_initcall(netpoll_init);
773
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000774static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
775{
776 struct netpoll_info *npinfo =
777 container_of(rcu_head, struct netpoll_info, rcu);
778
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000779 skb_queue_purge(&npinfo->txq);
780
781 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
782 cancel_delayed_work(&npinfo->tx_work);
783
784 /* clean after last, unfinished work */
785 __skb_queue_purge(&npinfo->txq);
786 /* now cancel it again */
787 cancel_delayed_work(&npinfo->tx_work);
788 kfree(npinfo);
789}
790
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000791void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700793 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700794
Neil Horman0790bbb2013-02-11 10:25:31 +0000795 /* rtnl_dereference would be preferable here but
796 * rcu_cleanup_netpoll path can put us in here safely without
797 * holding the rtnl, so plain rcu_dereference it is
798 */
799 npinfo = rtnl_dereference(np->dev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000800 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +0000801 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700802
Neil Hormanca99ca12013-02-05 08:05:43 +0000803 synchronize_srcu(&netpoll_srcu);
804
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000805 if (atomic_dec_and_test(&npinfo->refcnt)) {
806 const struct net_device_ops *ops;
807
808 ops = np->dev->netdev_ops;
809 if (ops->ndo_netpoll_cleanup)
810 ops->ndo_netpoll_cleanup(np->dev);
811
Monam Agarwalfcb144b2014-03-24 00:42:46 +0530812 RCU_INIT_POINTER(np->dev->npinfo, NULL);
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000813 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
Herbert Xudbaa1542010-06-10 16:12:46 +0000814 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000815}
816EXPORT_SYMBOL_GPL(__netpoll_cleanup);
817
Neil Horman2cde6ac2013-02-11 10:25:30 +0000818static void netpoll_async_cleanup(struct work_struct *work)
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000819{
Neil Horman2cde6ac2013-02-11 10:25:30 +0000820 struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000821
Neil Horman2cde6ac2013-02-11 10:25:30 +0000822 rtnl_lock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000823 __netpoll_cleanup(np);
Neil Horman2cde6ac2013-02-11 10:25:30 +0000824 rtnl_unlock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000825 kfree(np);
826}
827
Neil Horman2cde6ac2013-02-11 10:25:30 +0000828void __netpoll_free_async(struct netpoll *np)
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000829{
Neil Horman2cde6ac2013-02-11 10:25:30 +0000830 schedule_work(&np->cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000831}
Neil Horman2cde6ac2013-02-11 10:25:30 +0000832EXPORT_SYMBOL_GPL(__netpoll_free_async);
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000833
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000834void netpoll_cleanup(struct netpoll *np)
835{
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000836 rtnl_lock();
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +0200837 if (!np->dev)
838 goto out;
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000839 __netpoll_cleanup(np);
Herbert Xudbaa1542010-06-10 16:12:46 +0000840 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 np->dev = NULL;
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +0200842out:
843 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000845EXPORT_SYMBOL(netpoll_cleanup);