blob: 8f971990677cd48026f651b2dba01d01190bfff6 [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
49static atomic_t trapped;
50
Lai Jiangshan7f9421c2013-03-15 06:50:52 +000051DEFINE_STATIC_SRCU(netpoll_srcu);
Neil Hormanca99ca12013-02-05 08:05:43 +000052
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070053#define USEC_PER_POLL 50
David S. Millerd9452e92008-03-04 12:28:49 -080054#define NETPOLL_RX_ENABLED 1
55#define NETPOLL_RX_DROP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Joe Perches6f706242012-01-29 15:50:44 +000057#define MAX_SKB_SIZE \
58 (sizeof(struct ethhdr) + \
59 sizeof(struct iphdr) + \
60 sizeof(struct udphdr) + \
61 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
David S. Miller3578b0c2010-08-03 00:24:04 -070063static void zap_completion_queue(void);
Cong Wangb7394d22013-01-07 20:52:39 +000064static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
Neil Horman2cde6ac2013-02-11 10:25:30 +000065static void netpoll_async_cleanup(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Anton Vorontsovbff38772009-07-08 11:10:56 -070067static unsigned int carrier_timeout = 4;
68module_param(carrier_timeout, uint, 0644);
69
Joe Perchese6ec269352012-01-29 15:50:43 +000070#define np_info(np, fmt, ...) \
71 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
72#define np_err(np, fmt, ...) \
73 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
74#define np_notice(np, fmt, ...) \
75 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
76
David Howellsc4028952006-11-22 14:57:56 +000077static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
David Howells4c1ac1b2006-12-05 14:37:56 +000079 struct netpoll_info *npinfo =
80 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010082 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070084 while ((skb = skb_dequeue(&npinfo->txq))) {
85 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080086 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070087 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070089 if (!netif_device_present(dev) || !netif_running(dev)) {
90 __kfree_skb(skb);
91 continue;
92 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David S. Millerfd2ea0a2008-07-17 01:56:23 -070094 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
95
Ingo Molnar36405432006-12-12 17:20:42 +010096 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070097 __netif_tx_lock(txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +000098 if (netif_xmit_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080099 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700100 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700101 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100102 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700104 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700105 return;
106 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700107 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100108 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110}
111
Al Virob51655b2006-11-14 21:40:42 -0800112static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
113 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Al Virod6f5493c2006-11-14 21:26:08 -0800115 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800116
Herbert Xu60476372007-04-09 11:59:39 -0700117 if (uh->check == 0 || skb_csum_unnecessary(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119
Herbert Xufb286bb2005-11-10 13:01:24 -0800120 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Patrick McHardy84fa7932006-08-29 16:44:56 -0700122 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -0800123 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -0800124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Herbert Xufb286bb2005-11-10 13:01:24 -0800126 skb->csum = psum;
127
128 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/*
132 * Check whether delayed processing was scheduled for our NIC. If so,
133 * we attempt to grab the poll lock and use ->poll() to pump the card.
134 * If this fails, either we've recursed in ->poll() or it's already
135 * running on another CPU.
136 *
137 * Note: we don't mask interrupts with this lock because we're using
138 * trylock here and interrupts are already disabled in the softirq
139 * case. Further, we test the poll_owner to avoid recursion on UP
140 * systems where the lock doesn't exist.
141 *
142 * In cases where there is bi-directional communications, reading only
143 * one message at a time can lead to packets being dropped by the
144 * network adapter, forcing superfluous retries and possibly timeouts.
145 * Thus, we set our budget to greater than 1.
146 */
David S. Miller0a7606c2007-10-29 21:28:47 -0700147static int poll_one_napi(struct netpoll_info *npinfo,
148 struct napi_struct *napi, int budget)
149{
150 int work;
151
152 /* net_rx_action's ->poll() invocations and our's are
153 * synchronized by this test which is only made while
154 * holding the napi->poll_lock.
155 */
156 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
157 return budget;
158
David S. Millerd9452e92008-03-04 12:28:49 -0800159 npinfo->rx_flags |= NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700160 atomic_inc(&trapped);
Neil Horman7b363e42008-12-09 23:22:26 -0800161 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700162
163 work = napi->poll(napi, budget);
David S. Miller7d18f112009-05-21 23:30:09 -0700164 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700165
Neil Horman7b363e42008-12-09 23:22:26 -0800166 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700167 atomic_dec(&trapped);
David S. Millerd9452e92008-03-04 12:28:49 -0800168 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700169
170 return budget - work;
171}
172
Stephen Hemminger51069302007-11-19 19:18:11 -0800173static void poll_napi(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700175 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int budget = 16;
177
Neil Hormanf13d4932010-10-19 07:04:26 +0000178 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700179 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700180 spin_trylock(&napi->poll_lock)) {
Amerigo Wang28996562012-08-10 01:24:42 +0000181 budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
182 napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700183 spin_unlock(&napi->poll_lock);
David S. Miller0a7606c2007-10-29 21:28:47 -0700184
Amerigo Wang072a9c42012-08-24 21:41:11 +0000185 if (!budget)
David S. Miller0a7606c2007-10-29 21:28:47 -0700186 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189}
190
Cong Wangb7394d22013-01-07 20:52:39 +0000191static void service_neigh_queue(struct netpoll_info *npi)
Neil Horman068c6e92006-06-26 00:04:27 -0700192{
Stephen Hemminger51069302007-11-19 19:18:11 -0800193 if (npi) {
194 struct sk_buff *skb;
Neil Horman068c6e92006-06-26 00:04:27 -0700195
Cong Wangb7394d22013-01-07 20:52:39 +0000196 while ((skb = skb_dequeue(&npi->neigh_tx)))
197 netpoll_neigh_reply(skb, npi);
Neil Horman068c6e92006-06-26 00:04:27 -0700198 }
Neil Horman068c6e92006-06-26 00:04:27 -0700199}
200
Joe Perches234b9212011-06-30 15:08:57 +0000201static void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000203 const struct net_device_ops *ops;
Amerigo Wang28996562012-08-10 01:24:42 +0000204 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Stephen Hemminger51069302007-11-19 19:18:11 -0800205
Neil Hormanca99ca12013-02-05 08:05:43 +0000206 /* Don't do any rx activity if the dev_lock mutex is held
207 * the dev_open/close paths use this to block netpoll activity
208 * while changing device state
209 */
Dan Carpentera3dbbc22013-05-06 02:15:13 +0000210 if (down_trylock(&ni->dev_lock))
Neil Hormanca99ca12013-02-05 08:05:43 +0000211 return;
212
Neil Horman959d5fd2013-02-13 11:32:42 -0500213 if (!netif_running(dev)) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000214 up(&ni->dev_lock);
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000215 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500216 }
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000217
218 ops = dev->netdev_ops;
Neil Horman959d5fd2013-02-13 11:32:42 -0500219 if (!ops->ndo_poll_controller) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000220 up(&ni->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800225 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Stephen Hemminger51069302007-11-19 19:18:11 -0800227 poll_napi(dev);
228
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000229 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000230
Eric Dumazet58e05f32012-02-14 10:11:59 +0000231 if (dev->flags & IFF_SLAVE) {
Amerigo Wang28996562012-08-10 01:24:42 +0000232 if (ni) {
Jiri Pirko49bd8fb02013-01-03 22:48:55 +0000233 struct net_device *bond_dev;
Amerigo Wang5a698af2011-02-17 23:43:34 +0000234 struct sk_buff *skb;
Jiri Pirko49bd8fb02013-01-03 22:48:55 +0000235 struct netpoll_info *bond_ni;
236
237 bond_dev = netdev_master_upper_dev_get_rcu(dev);
238 bond_ni = rcu_dereference_bh(bond_dev->npinfo);
Cong Wangb7394d22013-01-07 20:52:39 +0000239 while ((skb = skb_dequeue(&ni->neigh_tx))) {
Amerigo Wang5a698af2011-02-17 23:43:34 +0000240 skb->dev = bond_dev;
Cong Wangb7394d22013-01-07 20:52:39 +0000241 skb_queue_tail(&bond_ni->neigh_tx, skb);
Amerigo Wang5a698af2011-02-17 23:43:34 +0000242 }
243 }
244 }
245
Cong Wangb7394d22013-01-07 20:52:39 +0000246 service_neigh_queue(ni);
Neil Horman068c6e92006-06-26 00:04:27 -0700247
David S. Miller3578b0c2010-08-03 00:24:04 -0700248 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
dingtianhongda6e3782013-05-27 19:53:31 +0000251void netpoll_rx_disable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000252{
253 struct netpoll_info *ni;
254 int idx;
255 might_sleep();
256 idx = srcu_read_lock(&netpoll_srcu);
257 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
258 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000259 down(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000260 srcu_read_unlock(&netpoll_srcu, idx);
Neil Hormanca99ca12013-02-05 08:05:43 +0000261}
262EXPORT_SYMBOL(netpoll_rx_disable);
263
264void netpoll_rx_enable(struct net_device *dev)
265{
266 struct netpoll_info *ni;
267 rcu_read_lock();
268 ni = rcu_dereference(dev->npinfo);
269 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000270 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000271 rcu_read_unlock();
272}
273EXPORT_SYMBOL(netpoll_rx_enable);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static void refill_skbs(void)
276{
277 struct sk_buff *skb;
278 unsigned long flags;
279
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800280 spin_lock_irqsave(&skb_pool.lock, flags);
281 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
283 if (!skb)
284 break;
285
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800286 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800288 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
David S. Miller3578b0c2010-08-03 00:24:04 -0700291static void zap_completion_queue(void)
292{
293 unsigned long flags;
294 struct softnet_data *sd = &get_cpu_var(softnet_data);
295
296 if (sd->completion_queue) {
297 struct sk_buff *clist;
298
299 local_irq_save(flags);
300 clist = sd->completion_queue;
301 sd->completion_queue = NULL;
302 local_irq_restore(flags);
303
304 while (clist != NULL) {
305 struct sk_buff *skb = clist;
306 clist = clist->next;
307 if (skb->destructor) {
308 atomic_inc(&skb->users);
309 dev_kfree_skb_any(skb); /* put this one back */
310 } else {
311 __kfree_skb(skb);
312 }
313 }
314 }
315
316 put_cpu_var(softnet_data);
317}
318
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800319static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800321 int count = 0;
322 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
David S. Miller3578b0c2010-08-03 00:24:04 -0700324 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800325 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800329 if (!skb)
330 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800333 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000334 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800335 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800337 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 atomic_set(&skb->users, 1);
341 skb_reserve(skb, reserve);
342 return skb;
343}
344
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700345static int netpoll_owner_active(struct net_device *dev)
346{
347 struct napi_struct *napi;
348
349 list_for_each_entry(napi, &dev->napi_list, dev_list) {
350 if (napi->poll_owner == smp_processor_id())
351 return 1;
352 }
353 return 0;
354}
355
Amerigo Wang28996562012-08-10 01:24:42 +0000356/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000357void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
358 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700360 int status = NETDEV_TX_BUSY;
361 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800362 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000363 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000364 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Amerigo Wang28996562012-08-10 01:24:42 +0000366 WARN_ON_ONCE(!irqs_disabled());
367
368 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900369 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
370 __kfree_skb(skb);
371 return;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700374 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700375 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700376 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800377
Amerigo Wang8c4c49d2012-09-17 20:16:31 +0000378 txq = netdev_pick_tx(dev, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700379
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700380 /* try until next clock tick */
381 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
382 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700383 if (__netif_tx_trylock(txq)) {
Tom Herbert734664982011-11-28 16:32:44 +0000384 if (!netif_xmit_stopped(txq)) {
Amerigo Wang689971b2012-08-10 01:24:49 +0000385 if (vlan_tx_tag_present(skb) &&
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000386 !vlan_hw_offload_capable(netif_skb_features(skb),
387 skb->vlan_proto)) {
388 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
Amerigo Wang689971b2012-08-10 01:24:49 +0000389 if (unlikely(!skb))
390 break;
391 skb->vlan_tci = 0;
392 }
393
Stephen Hemminger00829822008-11-20 20:14:53 -0800394 status = ops->ndo_start_xmit(skb, dev);
Eric Dumazet08baf562009-05-25 22:58:01 -0700395 if (status == NETDEV_TX_OK)
396 txq_trans_update(txq);
397 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700398 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700399
Andrew Mortone37b8d92006-12-09 14:01:49 -0800400 if (status == NETDEV_TX_OK)
401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Andrew Mortone37b8d92006-12-09 14:01:49 -0800403 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700404
405 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000406 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700407
408 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700409 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000410
411 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000412 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000413 dev->name, ops->ndo_start_xmit);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700417 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700418 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000419 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
Neil Hormanc2355e12010-10-13 16:01:49 +0000422EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
425{
Eric Dumazet954fba02012-06-12 19:30:21 +0000426 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct sk_buff *skb;
428 struct udphdr *udph;
429 struct iphdr *iph;
430 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000431 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000432 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000435 if (np->ipv6)
436 ip_len = udp_len + sizeof(*ip6h);
437 else
Cong Wangb7394d22013-01-07 20:52:39 +0000438 ip_len = udp_len + sizeof(*iph);
439
Eric Dumazet954fba02012-06-12 19:30:21 +0000440 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Eric Dumazet954fba02012-06-12 19:30:21 +0000442 skb = find_skb(np, total_len + np->dev->needed_tailroom,
443 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (!skb)
445 return;
446
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300447 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba02012-06-12 19:30:21 +0000448 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300450 skb_push(skb, sizeof(*udph));
451 skb_reset_transport_header(skb);
452 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 udph->source = htons(np->local_port);
454 udph->dest = htons(np->remote_port);
455 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Cong Wangb3d936f2013-01-07 20:52:41 +0000457 if (np->ipv6) {
458 udph->check = 0;
459 udph->check = csum_ipv6_magic(&np->local_ip.in6,
460 &np->remote_ip.in6,
461 udp_len, IPPROTO_UDP,
462 csum_partial(udph, udp_len, 0));
463 if (udph->check == 0)
464 udph->check = CSUM_MANGLED_0;
465
466 skb_push(skb, sizeof(*ip6h));
467 skb_reset_network_header(skb);
468 ip6h = ipv6_hdr(skb);
469
470 /* ip6h->version = 6; ip6h->priority = 0; */
471 put_unaligned(0x60, (unsigned char *)ip6h);
472 ip6h->flow_lbl[0] = 0;
473 ip6h->flow_lbl[1] = 0;
474 ip6h->flow_lbl[2] = 0;
475
476 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
477 ip6h->nexthdr = IPPROTO_UDP;
478 ip6h->hop_limit = 32;
479 ip6h->saddr = np->local_ip.in6;
480 ip6h->daddr = np->remote_ip.in6;
481
482 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
483 skb_reset_mac_header(skb);
484 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
485 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000486 udph->check = 0;
487 udph->check = csum_tcpudp_magic(np->local_ip.ip,
488 np->remote_ip.ip,
489 udp_len, IPPROTO_UDP,
490 csum_partial(udph, udp_len, 0));
491 if (udph->check == 0)
492 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Cong Wangb7394d22013-01-07 20:52:39 +0000494 skb_push(skb, sizeof(*iph));
495 skb_reset_network_header(skb);
496 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Cong Wangb7394d22013-01-07 20:52:39 +0000498 /* iph->version = 4; iph->ihl = 5; */
499 put_unaligned(0x45, (unsigned char *)iph);
500 iph->tos = 0;
501 put_unaligned(htons(ip_len), &(iph->tot_len));
502 iph->id = htons(atomic_inc_return(&ip_ident));
503 iph->frag_off = 0;
504 iph->ttl = 64;
505 iph->protocol = IPPROTO_UDP;
506 iph->check = 0;
507 put_unaligned(np->local_ip.ip, &(iph->saddr));
508 put_unaligned(np->remote_ip.ip, &(iph->daddr));
509 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
510
511 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
512 skb_reset_mac_header(skb);
513 skb->protocol = eth->h_proto = htons(ETH_P_IP);
514 }
515
Stephen Hemminger09538642007-11-19 19:23:29 -0800516 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
517 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 skb->dev = np->dev;
520
521 netpoll_send_skb(np, skb);
522}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000523EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Cong Wangb7394d22013-01-07 20:52:39 +0000525static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Cong Wangb3d936f2013-01-07 20:52:41 +0000527 int size, type = ARPOP_REPLY;
Al Viro252e3342006-11-14 20:48:11 -0800528 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800529 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000531 struct netpoll *np, *tmp;
532 unsigned long flags;
Herbert Xuae641942011-11-18 02:20:04 +0000533 int hlen, tlen;
Cong Wangb7394d22013-01-07 20:52:39 +0000534 int hits = 0, proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000536 if (list_empty(&npinfo->rx_np))
537 return;
538
539 /* Before checking the packet, we do some early
540 inspection whether this is interesting at all */
541 spin_lock_irqsave(&npinfo->rx_lock, flags);
542 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
543 if (np->dev == skb->dev)
544 hits++;
545 }
546 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
547
548 /* No netpoll struct is using this dev */
549 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700550 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Cong Wangb7394d22013-01-07 20:52:39 +0000552 proto = ntohs(eth_hdr(skb)->h_proto);
Sonic Zhangb0dd6632013-09-11 11:31:53 +0800553 if (proto == ETH_P_ARP) {
Cong Wangb3d936f2013-01-07 20:52:41 +0000554 struct arphdr *arp;
555 unsigned char *arp_ptr;
Cong Wangb7394d22013-01-07 20:52:39 +0000556 /* No arp on this interface */
557 if (skb->dev->flags & IFF_NOARP)
558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Cong Wangb7394d22013-01-07 20:52:39 +0000560 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
561 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Cong Wangb7394d22013-01-07 20:52:39 +0000563 skb_reset_network_header(skb);
564 skb_reset_transport_header(skb);
565 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Cong Wangb7394d22013-01-07 20:52:39 +0000567 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
568 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
569 arp->ar_pro != htons(ETH_P_IP) ||
570 arp->ar_op != htons(ARPOP_REQUEST))
571 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Cong Wangb7394d22013-01-07 20:52:39 +0000573 arp_ptr = (unsigned char *)(arp+1);
574 /* save the location of the src hw addr */
575 sha = arp_ptr;
576 arp_ptr += skb->dev->addr_len;
577 memcpy(&sip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000578 arp_ptr += 4;
Cong Wangb7394d22013-01-07 20:52:39 +0000579 /* If we actually cared about dst hw addr,
580 it would get copied here */
581 arp_ptr += skb->dev->addr_len;
582 memcpy(&tip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000583
Cong Wangb7394d22013-01-07 20:52:39 +0000584 /* Should we ignore arp? */
585 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
586 return;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000587
Cong Wangb7394d22013-01-07 20:52:39 +0000588 size = arp_hdr_len(skb->dev);
589
590 spin_lock_irqsave(&npinfo->rx_lock, flags);
591 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
592 if (tip != np->local_ip.ip)
593 continue;
594
595 hlen = LL_RESERVED_SPACE(np->dev);
596 tlen = np->dev->needed_tailroom;
597 send_skb = find_skb(np, size + hlen + tlen, hlen);
598 if (!send_skb)
599 continue;
600
601 skb_reset_network_header(send_skb);
602 arp = (struct arphdr *) skb_put(send_skb, size);
603 send_skb->dev = skb->dev;
604 send_skb->protocol = htons(ETH_P_ARP);
605
606 /* Fill the device header for the ARP frame */
Cong Wangb3d936f2013-01-07 20:52:41 +0000607 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
Cong Wangb7394d22013-01-07 20:52:39 +0000608 sha, np->dev->dev_addr,
609 send_skb->len) < 0) {
610 kfree_skb(send_skb);
611 continue;
612 }
613
614 /*
615 * Fill out the arp protocol part.
616 *
617 * we only support ethernet device type,
618 * which (according to RFC 1390) should
619 * always equal 1 (Ethernet).
620 */
621
622 arp->ar_hrd = htons(np->dev->type);
623 arp->ar_pro = htons(ETH_P_IP);
624 arp->ar_hln = np->dev->addr_len;
625 arp->ar_pln = 4;
626 arp->ar_op = htons(type);
627
628 arp_ptr = (unsigned char *)(arp + 1);
629 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
630 arp_ptr += np->dev->addr_len;
631 memcpy(arp_ptr, &tip, 4);
632 arp_ptr += 4;
633 memcpy(arp_ptr, sha, np->dev->addr_len);
634 arp_ptr += np->dev->addr_len;
635 memcpy(arp_ptr, &sip, 4);
636
637 netpoll_send_skb(np, send_skb);
638
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200639 /* If there are several rx_skb_hooks for the same
640 * address we're fine by sending a single reply
641 */
Cong Wangb7394d22013-01-07 20:52:39 +0000642 break;
643 }
644 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Cong Wangb3d936f2013-01-07 20:52:41 +0000645 } else if( proto == ETH_P_IPV6) {
646#if IS_ENABLED(CONFIG_IPV6)
647 struct nd_msg *msg;
648 u8 *lladdr = NULL;
649 struct ipv6hdr *hdr;
650 struct icmp6hdr *icmp6h;
651 const struct in6_addr *saddr;
652 const struct in6_addr *daddr;
653 struct inet6_dev *in6_dev = NULL;
654 struct in6_addr *target;
655
656 in6_dev = in6_dev_get(skb->dev);
657 if (!in6_dev || !in6_dev->cnf.accept_ra)
658 return;
659
660 if (!pskb_may_pull(skb, skb->len))
661 return;
662
663 msg = (struct nd_msg *)skb_transport_header(skb);
664
665 __skb_push(skb, skb->data - skb_transport_header(skb));
666
667 if (ipv6_hdr(skb)->hop_limit != 255)
668 return;
669 if (msg->icmph.icmp6_code != 0)
670 return;
671 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
672 return;
673
674 saddr = &ipv6_hdr(skb)->saddr;
675 daddr = &ipv6_hdr(skb)->daddr;
676
677 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
678
679 spin_lock_irqsave(&npinfo->rx_lock, flags);
680 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000681 if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
Cong Wangb3d936f2013-01-07 20:52:41 +0000682 continue;
683
684 hlen = LL_RESERVED_SPACE(np->dev);
685 tlen = np->dev->needed_tailroom;
686 send_skb = find_skb(np, size + hlen + tlen, hlen);
687 if (!send_skb)
688 continue;
689
690 send_skb->protocol = htons(ETH_P_IPV6);
691 send_skb->dev = skb->dev;
692
693 skb_reset_network_header(send_skb);
Amerigo Wang00f97da2013-06-03 16:31:36 +0000694 hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000695 *(__be32*)hdr = htonl(0x60000000);
Cong Wangb3d936f2013-01-07 20:52:41 +0000696 hdr->payload_len = htons(size);
697 hdr->nexthdr = IPPROTO_ICMPV6;
698 hdr->hop_limit = 255;
699 hdr->saddr = *saddr;
700 hdr->daddr = *daddr;
701
Amerigo Wang00f97da2013-06-03 16:31:36 +0000702 icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000703 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
704 icmp6h->icmp6_router = 0;
705 icmp6h->icmp6_solicited = 1;
Amerigo Wang00f97da2013-06-03 16:31:36 +0000706
707 target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000708 *target = msg->target;
709 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
710 IPPROTO_ICMPV6,
711 csum_partial(icmp6h,
712 size, 0));
713
714 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
715 lladdr, np->dev->dev_addr,
716 send_skb->len) < 0) {
717 kfree_skb(send_skb);
718 continue;
719 }
720
721 netpoll_send_skb(np, send_skb);
722
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200723 /* If there are several rx_skb_hooks for the same
724 * address, we're fine by sending a single reply
725 */
Cong Wangb3d936f2013-01-07 20:52:41 +0000726 break;
727 }
728 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
729#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Cong Wangb3d936f2013-01-07 20:52:41 +0000733static bool pkt_is_ns(struct sk_buff *skb)
734{
735 struct nd_msg *msg;
736 struct ipv6hdr *hdr;
737
738 if (skb->protocol != htons(ETH_P_ARP))
739 return false;
740 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
741 return false;
742
743 msg = (struct nd_msg *)skb_transport_header(skb);
744 __skb_push(skb, skb->data - skb_transport_header(skb));
745 hdr = ipv6_hdr(skb);
746
747 if (hdr->nexthdr != IPPROTO_ICMPV6)
748 return false;
749 if (hdr->hop_limit != 255)
750 return false;
751 if (msg->icmph.icmp6_code != 0)
752 return false;
753 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
754 return false;
755
756 return true;
757}
758
Amerigo Wang57c5d462012-08-10 01:24:40 +0000759int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200761 int proto, len, ulen, data_len;
762 int hits = 0, offset;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000763 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct udphdr *uh;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000765 struct netpoll *np, *tmp;
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200766 uint16_t source;
Neil Horman068c6e92006-06-26 00:04:27 -0700767
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000768 if (list_empty(&npinfo->rx_np))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (skb->dev->type != ARPHRD_ETHER)
772 goto out;
773
David S. Millerd9452e92008-03-04 12:28:49 -0800774 /* check if netpoll clients need ARP */
Cong Wangb3d936f2013-01-07 20:52:41 +0000775 if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
776 skb_queue_tail(&npinfo->neigh_tx, skb);
777 return 1;
778 } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
Cong Wangb7394d22013-01-07 20:52:39 +0000779 skb_queue_tail(&npinfo->neigh_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 1;
781 }
782
Amerigo Wang689971b2012-08-10 01:24:49 +0000783 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
784 skb = vlan_untag(skb);
785 if (unlikely(!skb))
786 goto out;
787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 proto = ntohs(eth_hdr(skb)->h_proto);
Cong Wangb7394d22013-01-07 20:52:39 +0000790 if (proto != ETH_P_IP && proto != ETH_P_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto out;
792 if (skb->pkt_type == PACKET_OTHERHOST)
793 goto out;
794 if (skb_shared(skb))
795 goto out;
796
Cong Wangb7394d22013-01-07 20:52:39 +0000797 if (proto == ETH_P_IP) {
798 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
799 goto out;
800 iph = (struct iphdr *)skb->data;
801 if (iph->ihl < 5 || iph->version != 4)
802 goto out;
803 if (!pskb_may_pull(skb, iph->ihl*4))
804 goto out;
805 iph = (struct iphdr *)skb->data;
806 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Cong Wangb7394d22013-01-07 20:52:39 +0000809 len = ntohs(iph->tot_len);
810 if (skb->len < len || len < iph->ihl*4)
811 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Cong Wangb7394d22013-01-07 20:52:39 +0000813 /*
814 * Our transport medium may have padded the buffer out.
815 * Now We trim to the true length of the frame.
816 */
817 if (pskb_trim_rcsum(skb, len))
818 goto out;
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700819
Cong Wangb7394d22013-01-07 20:52:39 +0000820 iph = (struct iphdr *)skb->data;
821 if (iph->protocol != IPPROTO_UDP)
822 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Cong Wangb7394d22013-01-07 20:52:39 +0000824 len -= iph->ihl*4;
825 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200826 offset = (unsigned char *)(uh + 1) - skb->data;
Cong Wangb7394d22013-01-07 20:52:39 +0000827 ulen = ntohs(uh->len);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200828 data_len = skb->len - offset;
829 source = ntohs(uh->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Cong Wangb7394d22013-01-07 20:52:39 +0000831 if (ulen != len)
832 goto out;
833 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
834 goto out;
835 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
836 if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
837 continue;
838 if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
839 continue;
840 if (np->local_port && np->local_port != ntohs(uh->dest))
841 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200843 np->rx_skb_hook(np, source, skb, offset, data_len);
Cong Wangb7394d22013-01-07 20:52:39 +0000844 hits++;
845 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000846 } else {
847#if IS_ENABLED(CONFIG_IPV6)
848 const struct ipv6hdr *ip6h;
849
850 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
851 goto out;
852 ip6h = (struct ipv6hdr *)skb->data;
853 if (ip6h->version != 6)
854 goto out;
855 len = ntohs(ip6h->payload_len);
856 if (!len)
857 goto out;
858 if (len + sizeof(struct ipv6hdr) > skb->len)
859 goto out;
860 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
861 goto out;
862 ip6h = ipv6_hdr(skb);
863 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
864 goto out;
865 uh = udp_hdr(skb);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200866 offset = (unsigned char *)(uh + 1) - skb->data;
Cong Wangb3d936f2013-01-07 20:52:41 +0000867 ulen = ntohs(uh->len);
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200868 data_len = skb->len - offset;
869 source = ntohs(uh->source);
Cong Wangb3d936f2013-01-07 20:52:41 +0000870 if (ulen != skb->len)
871 goto out;
872 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
873 goto out;
874 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000875 if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000876 continue;
Cong Wangfaeed822013-01-27 15:55:20 +0000877 if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000878 continue;
879 if (np->local_port && np->local_port != ntohs(uh->dest))
880 continue;
881
Antonio Quartulli8fb479a2013-10-23 23:36:30 +0200882 np->rx_skb_hook(np, source, skb, offset, data_len);
Cong Wangb3d936f2013-01-07 20:52:41 +0000883 hits++;
884 }
885#endif
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000886 }
887
888 if (!hits)
889 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 kfree_skb(skb);
892 return 1;
893
894out:
895 if (atomic_read(&trapped)) {
896 kfree_skb(skb);
897 return 1;
898 }
899
900 return 0;
901}
902
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700903void netpoll_print_options(struct netpoll *np)
904{
Joe Perchese6ec269352012-01-29 15:50:43 +0000905 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000906 if (np->ipv6)
907 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
908 else
Cong Wangb7394d22013-01-07 20:52:39 +0000909 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000910 np_info(np, "interface '%s'\n", np->dev_name);
911 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000912 if (np->ipv6)
913 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
914 else
Cong Wangb7394d22013-01-07 20:52:39 +0000915 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000916 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700917}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000918EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700919
Cong Wangb7394d22013-01-07 20:52:39 +0000920static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
921{
922 const char *end;
923
924 if (!strchr(str, ':') &&
925 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
926 if (!*end)
927 return 0;
928 }
929 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
930#if IS_ENABLED(CONFIG_IPV6)
931 if (!*end)
932 return 1;
933#else
934 return -1;
935#endif
936 }
937 return -1;
938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940int netpoll_parse_options(struct netpoll *np, char *opt)
941{
942 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +0000943 int ipv6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
David S. Millerc68b9072006-11-14 20:40:49 -0800945 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if ((delim = strchr(cur, '@')) == NULL)
947 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800948 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000949 if (kstrtou16(cur, 10, &np->local_port))
950 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800951 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
David S. Millerc68b9072006-11-14 20:40:49 -0800955 if (*cur != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if ((delim = strchr(cur, '/')) == NULL)
957 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800958 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000959 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
960 if (ipv6 < 0)
961 goto parse_failed;
962 else
963 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800964 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 cur++;
967
David S. Millerc68b9072006-11-14 20:40:49 -0800968 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* parse out dev name */
970 if ((delim = strchr(cur, ',')) == NULL)
971 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800972 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800974 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 cur++;
977
David S. Millerc68b9072006-11-14 20:40:49 -0800978 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* dst port */
980 if ((delim = strchr(cur, '@')) == NULL)
981 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800982 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000983 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000984 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000985 if (kstrtou16(cur, 10, &np->remote_port))
986 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800987 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* dst ip */
992 if ((delim = strchr(cur, '/')) == NULL)
993 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800994 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000995 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
996 if (ipv6 < 0)
997 goto parse_failed;
998 else if (np->ipv6 != (bool)ipv6)
999 goto parse_failed;
1000 else
1001 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -08001002 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
David S. Millerc68b9072006-11-14 20:40:49 -08001004 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001006 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
Satyam Sharma0bcc1812007-08-10 15:35:05 -07001010 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 return 0;
1013
1014 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +00001015 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -1;
1017}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001018EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Amerigo Wang47be03a22012-08-10 01:24:37 +00001020int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001021{
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001022 struct netpoll_info *npinfo;
1023 const struct net_device_ops *ops;
1024 unsigned long flags;
1025 int err;
1026
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001027 np->dev = ndev;
1028 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001029 INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001030
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001031 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
1032 !ndev->netdev_ops->ndo_poll_controller) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001033 np_err(np, "%s doesn't support polling, aborting\n",
1034 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001035 err = -ENOTSUPP;
1036 goto out;
1037 }
1038
1039 if (!ndev->npinfo) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001040 npinfo = kmalloc(sizeof(*npinfo), gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001041 if (!npinfo) {
1042 err = -ENOMEM;
1043 goto out;
1044 }
1045
1046 npinfo->rx_flags = 0;
1047 INIT_LIST_HEAD(&npinfo->rx_np);
1048
1049 spin_lock_init(&npinfo->rx_lock);
Neil Hormanbd7c4b62013-04-30 05:35:05 +00001050 sema_init(&npinfo->dev_lock, 1);
Cong Wangb7394d22013-01-07 20:52:39 +00001051 skb_queue_head_init(&npinfo->neigh_tx);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001052 skb_queue_head_init(&npinfo->txq);
1053 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1054
1055 atomic_set(&npinfo->refcnt, 1);
1056
1057 ops = np->dev->netdev_ops;
1058 if (ops->ndo_netpoll_setup) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001059 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001060 if (err)
1061 goto free_npinfo;
1062 }
1063 } else {
Neil Horman0790bbb2013-02-11 10:25:31 +00001064 npinfo = rtnl_dereference(ndev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001065 atomic_inc(&npinfo->refcnt);
1066 }
1067
1068 npinfo->netpoll = np;
1069
Antonio Quartulli8fb479a2013-10-23 23:36:30 +02001070 if (np->rx_skb_hook) {
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001071 spin_lock_irqsave(&npinfo->rx_lock, flags);
1072 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
1073 list_add_tail(&np->rx, &npinfo->rx_np);
1074 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1075 }
1076
1077 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +00001078 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001079
1080 return 0;
1081
1082free_npinfo:
1083 kfree(npinfo);
1084out:
1085 return err;
1086}
1087EXPORT_SYMBOL_GPL(__netpoll_setup);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089int netpoll_setup(struct netpoll *np)
1090{
1091 struct net_device *ndev = NULL;
1092 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001093 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Cong Wangf92d3182013-01-14 23:34:06 +00001095 rtnl_lock();
Cong Wang556e6252013-01-27 15:55:21 +00001096 if (np->dev_name) {
1097 struct net *net = current->nsproxy->net_ns;
1098 ndev = __dev_get_by_name(net, np->dev_name);
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001101 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +00001102 err = -ENODEV;
1103 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Cong Wang5bd30d32013-01-17 12:21:08 +08001105 dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jiri Pirko49bd8fb02013-01-03 22:48:55 +00001107 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001108 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -07001109 err = -EBUSY;
1110 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -07001111 }
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!netif_running(ndev)) {
1114 unsigned long atmost, atleast;
1115
Joe Perchese6ec269352012-01-29 15:50:43 +00001116 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001118 err = dev_open(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001119
1120 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001121 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +00001122 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Cong Wangf92d3182013-01-14 23:34:06 +00001125 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -07001127 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 while (!netif_carrier_ok(ndev)) {
1129 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001130 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 break;
1132 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -07001133 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 /* If carrier appears to come up instantly, we don't
1137 * trust it and pause so that we don't pump all our
1138 * queued console messages into the bitbucket.
1139 */
1140
1141 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001142 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 msleep(4000);
1144 }
Cong Wangf92d3182013-01-14 23:34:06 +00001145 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
Cong Wangb7394d22013-01-07 20:52:39 +00001148 if (!np->local_ip.ip) {
1149 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +00001150 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +00001151
1152 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +00001153 np_err(np, "no IP address for %s, aborting\n",
1154 np->dev_name);
1155 err = -EDESTADDRREQ;
1156 goto put;
1157 }
1158
1159 np->local_ip.ip = in_dev->ifa_list->ifa_local;
Cong Wangb7394d22013-01-07 20:52:39 +00001160 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +00001161 } else {
1162#if IS_ENABLED(CONFIG_IPV6)
1163 struct inet6_dev *idev;
1164
1165 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +00001166 idev = __in6_dev_get(ndev);
1167 if (idev) {
1168 struct inet6_ifaddr *ifp;
1169
1170 read_lock_bh(&idev->lock);
1171 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1172 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1173 continue;
1174 np->local_ip.in6 = ifp->addr;
1175 err = 0;
1176 break;
1177 }
1178 read_unlock_bh(&idev->lock);
1179 }
Cong Wangb3d936f2013-01-07 20:52:41 +00001180 if (err) {
1181 np_err(np, "no IPv6 address for %s, aborting\n",
1182 np->dev_name);
1183 goto put;
1184 } else
1185 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1186#else
1187 np_err(np, "IPv6 is not supported %s, aborting\n",
1188 np->dev_name);
Cong Wange39363a2013-01-22 17:39:11 +00001189 err = -EINVAL;
Cong Wangb3d936f2013-01-07 20:52:41 +00001190 goto put;
1191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194
Herbert Xudbaa1542010-06-10 16:12:46 +00001195 /* fill up the skb queue */
1196 refill_skbs();
1197
Amerigo Wang47be03a22012-08-10 01:24:37 +00001198 err = __netpoll_setup(np, ndev, GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001199 if (err)
1200 goto put;
1201
Cong Wangf92d3182013-01-14 23:34:06 +00001202 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return 0;
1204
Jiri Slaby21edbb22010-03-16 05:29:54 +00001205put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +00001207unlock:
1208 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001211EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
David S. Millerc68b9072006-11-14 20:40:49 -08001213static int __init netpoll_init(void)
1214{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -08001215 skb_queue_head_init(&skb_pool);
1216 return 0;
1217}
1218core_initcall(netpoll_init);
1219
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001220static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1221{
1222 struct netpoll_info *npinfo =
1223 container_of(rcu_head, struct netpoll_info, rcu);
1224
Cong Wangb7394d22013-01-07 20:52:39 +00001225 skb_queue_purge(&npinfo->neigh_tx);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001226 skb_queue_purge(&npinfo->txq);
1227
1228 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1229 cancel_delayed_work(&npinfo->tx_work);
1230
1231 /* clean after last, unfinished work */
1232 __skb_queue_purge(&npinfo->txq);
1233 /* now cancel it again */
1234 cancel_delayed_work(&npinfo->tx_work);
1235 kfree(npinfo);
1236}
1237
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001238void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -07001240 struct netpoll_info *npinfo;
1241 unsigned long flags;
1242
Neil Horman0790bbb2013-02-11 10:25:31 +00001243 /* rtnl_dereference would be preferable here but
1244 * rcu_cleanup_netpoll path can put us in here safely without
1245 * holding the rtnl, so plain rcu_dereference it is
1246 */
1247 npinfo = rtnl_dereference(np->dev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001248 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +00001249 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -07001250
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001251 if (!list_empty(&npinfo->rx_np)) {
1252 spin_lock_irqsave(&npinfo->rx_lock, flags);
1253 list_del(&np->rx);
1254 if (list_empty(&npinfo->rx_np))
1255 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
1256 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Jeff Moyer115c1d62005-06-22 22:05:31 -07001257 }
Herbert Xudbaa1542010-06-10 16:12:46 +00001258
Neil Hormanca99ca12013-02-05 08:05:43 +00001259 synchronize_srcu(&netpoll_srcu);
1260
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001261 if (atomic_dec_and_test(&npinfo->refcnt)) {
1262 const struct net_device_ops *ops;
1263
1264 ops = np->dev->netdev_ops;
1265 if (ops->ndo_netpoll_cleanup)
1266 ops->ndo_netpoll_cleanup(np->dev);
1267
Neil Horman2cde6ac2013-02-11 10:25:30 +00001268 rcu_assign_pointer(np->dev->npinfo, NULL);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001269 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
Herbert Xudbaa1542010-06-10 16:12:46 +00001270 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001271}
1272EXPORT_SYMBOL_GPL(__netpoll_cleanup);
1273
Neil Horman2cde6ac2013-02-11 10:25:30 +00001274static void netpoll_async_cleanup(struct work_struct *work)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001275{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001276 struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001277
Neil Horman2cde6ac2013-02-11 10:25:30 +00001278 rtnl_lock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001279 __netpoll_cleanup(np);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001280 rtnl_unlock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001281 kfree(np);
1282}
1283
Neil Horman2cde6ac2013-02-11 10:25:30 +00001284void __netpoll_free_async(struct netpoll *np)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001285{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001286 schedule_work(&np->cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001287}
Neil Horman2cde6ac2013-02-11 10:25:30 +00001288EXPORT_SYMBOL_GPL(__netpoll_free_async);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001289
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001290void netpoll_cleanup(struct netpoll *np)
1291{
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001292 rtnl_lock();
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +02001293 if (!np->dev)
1294 goto out;
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001295 __netpoll_cleanup(np);
Herbert Xudbaa1542010-06-10 16:12:46 +00001296 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 np->dev = NULL;
Nikolay Aleksandrovd0fe8c882013-09-19 15:02:35 +02001298out:
1299 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001301EXPORT_SYMBOL(netpoll_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303int netpoll_trap(void)
1304{
1305 return atomic_read(&trapped);
1306}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001307EXPORT_SYMBOL(netpoll_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309void netpoll_set_trap(int trap)
1310{
1311 if (trap)
1312 atomic_inc(&trapped);
1313 else
1314 atomic_dec(&trapped);
1315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316EXPORT_SYMBOL(netpoll_set_trap);