blob: 03c8ec3edc7208715f76ab06c68154de30d089af [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/string.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020018#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/inetdevice.h>
20#include <linux/inet.h>
21#include <linux/interrupt.h>
22#include <linux/netpoll.h>
23#include <linux/sched.h>
24#include <linux/delay.h>
25#include <linux/rcupdate.h>
26#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040028#include <linux/export.h>
Amerigo Wang689971b2012-08-10 01:24:49 +000029#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/tcp.h>
31#include <net/udp.h>
Cong Wangb3d936f2013-01-07 20:52:41 +000032#include <net/addrconf.h>
33#include <net/ndisc.h>
34#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/unaligned.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -070036#include <trace/events/napi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * We maintain a small pool of fully-sized skbs, to make sure the
40 * message gets out even in extreme OOM situations.
41 */
42
43#define MAX_UDP_CHUNK 1460
44#define MAX_SKBS 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080046static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static atomic_t trapped;
49
Lai Jiangshan7f9421c2013-03-15 06:50:52 +000050DEFINE_STATIC_SRCU(netpoll_srcu);
Neil Hormanca99ca12013-02-05 08:05:43 +000051
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070052#define USEC_PER_POLL 50
David S. Millerd9452e92008-03-04 12:28:49 -080053#define NETPOLL_RX_ENABLED 1
54#define NETPOLL_RX_DROP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Joe Perches6f706242012-01-29 15:50:44 +000056#define MAX_SKB_SIZE \
57 (sizeof(struct ethhdr) + \
58 sizeof(struct iphdr) + \
59 sizeof(struct udphdr) + \
60 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
David S. Miller3578b0c2010-08-03 00:24:04 -070062static void zap_completion_queue(void);
Cong Wangb7394d22013-01-07 20:52:39 +000063static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
Neil Horman2cde6ac2013-02-11 10:25:30 +000064static void netpoll_async_cleanup(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Anton Vorontsovbff38772009-07-08 11:10:56 -070066static unsigned int carrier_timeout = 4;
67module_param(carrier_timeout, uint, 0644);
68
Joe Perchese6ec269352012-01-29 15:50:43 +000069#define np_info(np, fmt, ...) \
70 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
71#define np_err(np, fmt, ...) \
72 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
73#define np_notice(np, fmt, ...) \
74 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
75
David Howellsc4028952006-11-22 14:57:56 +000076static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
David Howells4c1ac1b2006-12-05 14:37:56 +000078 struct netpoll_info *npinfo =
79 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010081 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070083 while ((skb = skb_dequeue(&npinfo->txq))) {
84 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080085 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070086 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070088 if (!netif_device_present(dev) || !netif_running(dev)) {
89 __kfree_skb(skb);
90 continue;
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
David S. Millerfd2ea0a2008-07-17 01:56:23 -070093 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
94
Ingo Molnar36405432006-12-12 17:20:42 +010095 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070096 __netif_tx_lock(txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +000097 if (netif_xmit_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080098 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070099 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700100 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100101 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700103 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700104 return;
105 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700106 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100107 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109}
110
Al Virob51655b2006-11-14 21:40:42 -0800111static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
112 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Al Virod6f5493c2006-11-14 21:26:08 -0800114 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800115
Herbert Xu60476372007-04-09 11:59:39 -0700116 if (uh->check == 0 || skb_csum_unnecessary(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118
Herbert Xufb286bb2005-11-10 13:01:24 -0800119 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Patrick McHardy84fa7932006-08-29 16:44:56 -0700121 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -0800122 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -0800123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Herbert Xufb286bb2005-11-10 13:01:24 -0800125 skb->csum = psum;
126
127 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/*
131 * Check whether delayed processing was scheduled for our NIC. If so,
132 * we attempt to grab the poll lock and use ->poll() to pump the card.
133 * If this fails, either we've recursed in ->poll() or it's already
134 * running on another CPU.
135 *
136 * Note: we don't mask interrupts with this lock because we're using
137 * trylock here and interrupts are already disabled in the softirq
138 * case. Further, we test the poll_owner to avoid recursion on UP
139 * systems where the lock doesn't exist.
140 *
141 * In cases where there is bi-directional communications, reading only
142 * one message at a time can lead to packets being dropped by the
143 * network adapter, forcing superfluous retries and possibly timeouts.
144 * Thus, we set our budget to greater than 1.
145 */
David S. Miller0a7606c2007-10-29 21:28:47 -0700146static int poll_one_napi(struct netpoll_info *npinfo,
147 struct napi_struct *napi, int budget)
148{
149 int work;
150
151 /* net_rx_action's ->poll() invocations and our's are
152 * synchronized by this test which is only made while
153 * holding the napi->poll_lock.
154 */
155 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
156 return budget;
157
David S. Millerd9452e92008-03-04 12:28:49 -0800158 npinfo->rx_flags |= NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700159 atomic_inc(&trapped);
Neil Horman7b363e42008-12-09 23:22:26 -0800160 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700161
162 work = napi->poll(napi, budget);
David S. Miller7d18f112009-05-21 23:30:09 -0700163 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700164
Neil Horman7b363e42008-12-09 23:22:26 -0800165 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700166 atomic_dec(&trapped);
David S. Millerd9452e92008-03-04 12:28:49 -0800167 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700168
169 return budget - work;
170}
171
Stephen Hemminger51069302007-11-19 19:18:11 -0800172static void poll_napi(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700174 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int budget = 16;
176
Neil Hormanf13d4932010-10-19 07:04:26 +0000177 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700178 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700179 spin_trylock(&napi->poll_lock)) {
Amerigo Wang28996562012-08-10 01:24:42 +0000180 budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
181 napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700182 spin_unlock(&napi->poll_lock);
David S. Miller0a7606c2007-10-29 21:28:47 -0700183
Amerigo Wang072a9c42012-08-24 21:41:11 +0000184 if (!budget)
David S. Miller0a7606c2007-10-29 21:28:47 -0700185 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188}
189
Cong Wangb7394d22013-01-07 20:52:39 +0000190static void service_neigh_queue(struct netpoll_info *npi)
Neil Horman068c6e92006-06-26 00:04:27 -0700191{
Stephen Hemminger51069302007-11-19 19:18:11 -0800192 if (npi) {
193 struct sk_buff *skb;
Neil Horman068c6e92006-06-26 00:04:27 -0700194
Cong Wangb7394d22013-01-07 20:52:39 +0000195 while ((skb = skb_dequeue(&npi->neigh_tx)))
196 netpoll_neigh_reply(skb, npi);
Neil Horman068c6e92006-06-26 00:04:27 -0700197 }
Neil Horman068c6e92006-06-26 00:04:27 -0700198}
199
Joe Perches234b9212011-06-30 15:08:57 +0000200static void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000202 const struct net_device_ops *ops;
Amerigo Wang28996562012-08-10 01:24:42 +0000203 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Stephen Hemminger51069302007-11-19 19:18:11 -0800204
Neil Hormanca99ca12013-02-05 08:05:43 +0000205 /* Don't do any rx activity if the dev_lock mutex is held
206 * the dev_open/close paths use this to block netpoll activity
207 * while changing device state
208 */
Dan Carpentera3dbbc22013-05-06 02:15:13 +0000209 if (down_trylock(&ni->dev_lock))
Neil Hormanca99ca12013-02-05 08:05:43 +0000210 return;
211
Neil Horman959d5fd2013-02-13 11:32:42 -0500212 if (!netif_running(dev)) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000213 up(&ni->dev_lock);
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000214 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500215 }
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000216
217 ops = dev->netdev_ops;
Neil Horman959d5fd2013-02-13 11:32:42 -0500218 if (!ops->ndo_poll_controller) {
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000219 up(&ni->dev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return;
Neil Horman959d5fd2013-02-13 11:32:42 -0500221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800224 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Stephen Hemminger51069302007-11-19 19:18:11 -0800226 poll_napi(dev);
227
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000228 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000229
Eric Dumazet58e05f32012-02-14 10:11:59 +0000230 if (dev->flags & IFF_SLAVE) {
Amerigo Wang28996562012-08-10 01:24:42 +0000231 if (ni) {
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000232 struct net_device *bond_dev;
Amerigo Wang5a698af2011-02-17 23:43:34 +0000233 struct sk_buff *skb;
Jiri Pirko49bd8fb2013-01-03 22:48:55 +0000234 struct netpoll_info *bond_ni;
235
236 bond_dev = netdev_master_upper_dev_get_rcu(dev);
237 bond_ni = rcu_dereference_bh(bond_dev->npinfo);
Cong Wangb7394d22013-01-07 20:52:39 +0000238 while ((skb = skb_dequeue(&ni->neigh_tx))) {
Amerigo Wang5a698af2011-02-17 23:43:34 +0000239 skb->dev = bond_dev;
Cong Wangb7394d22013-01-07 20:52:39 +0000240 skb_queue_tail(&bond_ni->neigh_tx, skb);
Amerigo Wang5a698af2011-02-17 23:43:34 +0000241 }
242 }
243 }
244
Cong Wangb7394d22013-01-07 20:52:39 +0000245 service_neigh_queue(ni);
Neil Horman068c6e92006-06-26 00:04:27 -0700246
David S. Miller3578b0c2010-08-03 00:24:04 -0700247 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
dingtianhongda6e3782013-05-27 19:53:31 +0000250void netpoll_rx_disable(struct net_device *dev)
Neil Hormanca99ca12013-02-05 08:05:43 +0000251{
252 struct netpoll_info *ni;
253 int idx;
254 might_sleep();
255 idx = srcu_read_lock(&netpoll_srcu);
256 ni = srcu_dereference(dev->npinfo, &netpoll_srcu);
257 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000258 down(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000259 srcu_read_unlock(&netpoll_srcu, idx);
Neil Hormanca99ca12013-02-05 08:05:43 +0000260}
261EXPORT_SYMBOL(netpoll_rx_disable);
262
263void netpoll_rx_enable(struct net_device *dev)
264{
265 struct netpoll_info *ni;
266 rcu_read_lock();
267 ni = rcu_dereference(dev->npinfo);
268 if (ni)
Neil Hormanbd7c4b62013-04-30 05:35:05 +0000269 up(&ni->dev_lock);
Neil Hormanca99ca12013-02-05 08:05:43 +0000270 rcu_read_unlock();
271}
272EXPORT_SYMBOL(netpoll_rx_enable);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static void refill_skbs(void)
275{
276 struct sk_buff *skb;
277 unsigned long flags;
278
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800279 spin_lock_irqsave(&skb_pool.lock, flags);
280 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
282 if (!skb)
283 break;
284
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800285 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800287 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
David S. Miller3578b0c2010-08-03 00:24:04 -0700290static void zap_completion_queue(void)
291{
292 unsigned long flags;
293 struct softnet_data *sd = &get_cpu_var(softnet_data);
294
295 if (sd->completion_queue) {
296 struct sk_buff *clist;
297
298 local_irq_save(flags);
299 clist = sd->completion_queue;
300 sd->completion_queue = NULL;
301 local_irq_restore(flags);
302
303 while (clist != NULL) {
304 struct sk_buff *skb = clist;
305 clist = clist->next;
306 if (skb->destructor) {
307 atomic_inc(&skb->users);
308 dev_kfree_skb_any(skb); /* put this one back */
309 } else {
310 __kfree_skb(skb);
311 }
312 }
313 }
314
315 put_cpu_var(softnet_data);
316}
317
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800318static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800320 int count = 0;
321 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
David S. Miller3578b0c2010-08-03 00:24:04 -0700323 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800324 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800328 if (!skb)
329 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800332 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000333 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800334 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800336 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 atomic_set(&skb->users, 1);
340 skb_reserve(skb, reserve);
341 return skb;
342}
343
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700344static int netpoll_owner_active(struct net_device *dev)
345{
346 struct napi_struct *napi;
347
348 list_for_each_entry(napi, &dev->napi_list, dev_list) {
349 if (napi->poll_owner == smp_processor_id())
350 return 1;
351 }
352 return 0;
353}
354
Amerigo Wang28996562012-08-10 01:24:42 +0000355/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000356void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
357 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700359 int status = NETDEV_TX_BUSY;
360 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800361 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000362 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000363 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Amerigo Wang28996562012-08-10 01:24:42 +0000365 WARN_ON_ONCE(!irqs_disabled());
366
367 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900368 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
369 __kfree_skb(skb);
370 return;
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700373 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700374 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700375 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800376
Amerigo Wang8c4c49d2012-09-17 20:16:31 +0000377 txq = netdev_pick_tx(dev, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700378
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700379 /* try until next clock tick */
380 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
381 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700382 if (__netif_tx_trylock(txq)) {
Tom Herbert734664982011-11-28 16:32:44 +0000383 if (!netif_xmit_stopped(txq)) {
Amerigo Wang689971b2012-08-10 01:24:49 +0000384 if (vlan_tx_tag_present(skb) &&
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000385 !vlan_hw_offload_capable(netif_skb_features(skb),
386 skb->vlan_proto)) {
387 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
Amerigo Wang689971b2012-08-10 01:24:49 +0000388 if (unlikely(!skb))
389 break;
390 skb->vlan_tci = 0;
391 }
392
Stephen Hemminger00829822008-11-20 20:14:53 -0800393 status = ops->ndo_start_xmit(skb, dev);
Eric Dumazet08baf562009-05-25 22:58:01 -0700394 if (status == NETDEV_TX_OK)
395 txq_trans_update(txq);
396 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700397 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700398
Andrew Mortone37b8d92006-12-09 14:01:49 -0800399 if (status == NETDEV_TX_OK)
400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Andrew Mortone37b8d92006-12-09 14:01:49 -0800402 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700403
404 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000405 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700406
407 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700408 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000409
410 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000411 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000412 dev->name, ops->ndo_start_xmit);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700416 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700417 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000418 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
Neil Hormanc2355e12010-10-13 16:01:49 +0000421EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
424{
Eric Dumazet954fba02012-06-12 19:30:21 +0000425 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct sk_buff *skb;
427 struct udphdr *udph;
428 struct iphdr *iph;
429 struct ethhdr *eth;
Eric Dumazetee130402012-08-24 01:47:26 +0000430 static atomic_t ip_ident;
Cong Wangb3d936f2013-01-07 20:52:41 +0000431 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 udp_len = len + sizeof(*udph);
Cong Wangb3d936f2013-01-07 20:52:41 +0000434 if (np->ipv6)
435 ip_len = udp_len + sizeof(*ip6h);
436 else
Cong Wangb7394d22013-01-07 20:52:39 +0000437 ip_len = udp_len + sizeof(*iph);
438
Eric Dumazet954fba02012-06-12 19:30:21 +0000439 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Eric Dumazet954fba02012-06-12 19:30:21 +0000441 skb = find_skb(np, total_len + np->dev->needed_tailroom,
442 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (!skb)
444 return;
445
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300446 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba02012-06-12 19:30:21 +0000447 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300449 skb_push(skb, sizeof(*udph));
450 skb_reset_transport_header(skb);
451 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 udph->source = htons(np->local_port);
453 udph->dest = htons(np->remote_port);
454 udph->len = htons(udp_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Cong Wangb3d936f2013-01-07 20:52:41 +0000456 if (np->ipv6) {
457 udph->check = 0;
458 udph->check = csum_ipv6_magic(&np->local_ip.in6,
459 &np->remote_ip.in6,
460 udp_len, IPPROTO_UDP,
461 csum_partial(udph, udp_len, 0));
462 if (udph->check == 0)
463 udph->check = CSUM_MANGLED_0;
464
465 skb_push(skb, sizeof(*ip6h));
466 skb_reset_network_header(skb);
467 ip6h = ipv6_hdr(skb);
468
469 /* ip6h->version = 6; ip6h->priority = 0; */
470 put_unaligned(0x60, (unsigned char *)ip6h);
471 ip6h->flow_lbl[0] = 0;
472 ip6h->flow_lbl[1] = 0;
473 ip6h->flow_lbl[2] = 0;
474
475 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
476 ip6h->nexthdr = IPPROTO_UDP;
477 ip6h->hop_limit = 32;
478 ip6h->saddr = np->local_ip.in6;
479 ip6h->daddr = np->remote_ip.in6;
480
481 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
482 skb_reset_mac_header(skb);
483 skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
484 } else {
Cong Wangb7394d22013-01-07 20:52:39 +0000485 udph->check = 0;
486 udph->check = csum_tcpudp_magic(np->local_ip.ip,
487 np->remote_ip.ip,
488 udp_len, IPPROTO_UDP,
489 csum_partial(udph, udp_len, 0));
490 if (udph->check == 0)
491 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Cong Wangb7394d22013-01-07 20:52:39 +0000493 skb_push(skb, sizeof(*iph));
494 skb_reset_network_header(skb);
495 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Cong Wangb7394d22013-01-07 20:52:39 +0000497 /* iph->version = 4; iph->ihl = 5; */
498 put_unaligned(0x45, (unsigned char *)iph);
499 iph->tos = 0;
500 put_unaligned(htons(ip_len), &(iph->tot_len));
501 iph->id = htons(atomic_inc_return(&ip_ident));
502 iph->frag_off = 0;
503 iph->ttl = 64;
504 iph->protocol = IPPROTO_UDP;
505 iph->check = 0;
506 put_unaligned(np->local_ip.ip, &(iph->saddr));
507 put_unaligned(np->remote_ip.ip, &(iph->daddr));
508 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
509
510 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
511 skb_reset_mac_header(skb);
512 skb->protocol = eth->h_proto = htons(ETH_P_IP);
513 }
514
Stephen Hemminger09538642007-11-19 19:23:29 -0800515 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
516 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 skb->dev = np->dev;
519
520 netpoll_send_skb(np, skb);
521}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000522EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Cong Wangb7394d22013-01-07 20:52:39 +0000524static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Cong Wangb3d936f2013-01-07 20:52:41 +0000526 int size, type = ARPOP_REPLY;
Al Viro252e33462006-11-14 20:48:11 -0800527 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800528 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000530 struct netpoll *np, *tmp;
531 unsigned long flags;
Herbert Xuae641942011-11-18 02:20:04 +0000532 int hlen, tlen;
Cong Wangb7394d22013-01-07 20:52:39 +0000533 int hits = 0, proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000535 if (list_empty(&npinfo->rx_np))
536 return;
537
538 /* Before checking the packet, we do some early
539 inspection whether this is interesting at all */
540 spin_lock_irqsave(&npinfo->rx_lock, flags);
541 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
542 if (np->dev == skb->dev)
543 hits++;
544 }
545 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
546
547 /* No netpoll struct is using this dev */
548 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700549 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Cong Wangb7394d22013-01-07 20:52:39 +0000551 proto = ntohs(eth_hdr(skb)->h_proto);
552 if (proto == ETH_P_IP) {
Cong Wangb3d936f2013-01-07 20:52:41 +0000553 struct arphdr *arp;
554 unsigned char *arp_ptr;
Cong Wangb7394d22013-01-07 20:52:39 +0000555 /* No arp on this interface */
556 if (skb->dev->flags & IFF_NOARP)
557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Cong Wangb7394d22013-01-07 20:52:39 +0000559 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
560 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Cong Wangb7394d22013-01-07 20:52:39 +0000562 skb_reset_network_header(skb);
563 skb_reset_transport_header(skb);
564 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Cong Wangb7394d22013-01-07 20:52:39 +0000566 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
567 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
568 arp->ar_pro != htons(ETH_P_IP) ||
569 arp->ar_op != htons(ARPOP_REQUEST))
570 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Cong Wangb7394d22013-01-07 20:52:39 +0000572 arp_ptr = (unsigned char *)(arp+1);
573 /* save the location of the src hw addr */
574 sha = arp_ptr;
575 arp_ptr += skb->dev->addr_len;
576 memcpy(&sip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000577 arp_ptr += 4;
Cong Wangb7394d22013-01-07 20:52:39 +0000578 /* If we actually cared about dst hw addr,
579 it would get copied here */
580 arp_ptr += skb->dev->addr_len;
581 memcpy(&tip, arp_ptr, 4);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000582
Cong Wangb7394d22013-01-07 20:52:39 +0000583 /* Should we ignore arp? */
584 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
585 return;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000586
Cong Wangb7394d22013-01-07 20:52:39 +0000587 size = arp_hdr_len(skb->dev);
588
589 spin_lock_irqsave(&npinfo->rx_lock, flags);
590 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
591 if (tip != np->local_ip.ip)
592 continue;
593
594 hlen = LL_RESERVED_SPACE(np->dev);
595 tlen = np->dev->needed_tailroom;
596 send_skb = find_skb(np, size + hlen + tlen, hlen);
597 if (!send_skb)
598 continue;
599
600 skb_reset_network_header(send_skb);
601 arp = (struct arphdr *) skb_put(send_skb, size);
602 send_skb->dev = skb->dev;
603 send_skb->protocol = htons(ETH_P_ARP);
604
605 /* Fill the device header for the ARP frame */
Cong Wangb3d936f2013-01-07 20:52:41 +0000606 if (dev_hard_header(send_skb, skb->dev, ETH_P_ARP,
Cong Wangb7394d22013-01-07 20:52:39 +0000607 sha, np->dev->dev_addr,
608 send_skb->len) < 0) {
609 kfree_skb(send_skb);
610 continue;
611 }
612
613 /*
614 * Fill out the arp protocol part.
615 *
616 * we only support ethernet device type,
617 * which (according to RFC 1390) should
618 * always equal 1 (Ethernet).
619 */
620
621 arp->ar_hrd = htons(np->dev->type);
622 arp->ar_pro = htons(ETH_P_IP);
623 arp->ar_hln = np->dev->addr_len;
624 arp->ar_pln = 4;
625 arp->ar_op = htons(type);
626
627 arp_ptr = (unsigned char *)(arp + 1);
628 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
629 arp_ptr += np->dev->addr_len;
630 memcpy(arp_ptr, &tip, 4);
631 arp_ptr += 4;
632 memcpy(arp_ptr, sha, np->dev->addr_len);
633 arp_ptr += np->dev->addr_len;
634 memcpy(arp_ptr, &sip, 4);
635
636 netpoll_send_skb(np, send_skb);
637
638 /* If there are several rx_hooks for the same address,
639 we're fine by sending a single reply */
640 break;
641 }
642 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Cong Wangb3d936f2013-01-07 20:52:41 +0000643 } else if( proto == ETH_P_IPV6) {
644#if IS_ENABLED(CONFIG_IPV6)
645 struct nd_msg *msg;
646 u8 *lladdr = NULL;
647 struct ipv6hdr *hdr;
648 struct icmp6hdr *icmp6h;
649 const struct in6_addr *saddr;
650 const struct in6_addr *daddr;
651 struct inet6_dev *in6_dev = NULL;
652 struct in6_addr *target;
653
654 in6_dev = in6_dev_get(skb->dev);
655 if (!in6_dev || !in6_dev->cnf.accept_ra)
656 return;
657
658 if (!pskb_may_pull(skb, skb->len))
659 return;
660
661 msg = (struct nd_msg *)skb_transport_header(skb);
662
663 __skb_push(skb, skb->data - skb_transport_header(skb));
664
665 if (ipv6_hdr(skb)->hop_limit != 255)
666 return;
667 if (msg->icmph.icmp6_code != 0)
668 return;
669 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
670 return;
671
672 saddr = &ipv6_hdr(skb)->saddr;
673 daddr = &ipv6_hdr(skb)->daddr;
674
675 size = sizeof(struct icmp6hdr) + sizeof(struct in6_addr);
676
677 spin_lock_irqsave(&npinfo->rx_lock, flags);
678 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000679 if (!ipv6_addr_equal(daddr, &np->local_ip.in6))
Cong Wangb3d936f2013-01-07 20:52:41 +0000680 continue;
681
682 hlen = LL_RESERVED_SPACE(np->dev);
683 tlen = np->dev->needed_tailroom;
684 send_skb = find_skb(np, size + hlen + tlen, hlen);
685 if (!send_skb)
686 continue;
687
688 send_skb->protocol = htons(ETH_P_IPV6);
689 send_skb->dev = skb->dev;
690
691 skb_reset_network_header(send_skb);
Amerigo Wang00f97da2013-06-03 16:31:36 +0000692 hdr = (struct ipv6hdr *) skb_put(send_skb, sizeof(struct ipv6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000693 *(__be32*)hdr = htonl(0x60000000);
Cong Wangb3d936f2013-01-07 20:52:41 +0000694 hdr->payload_len = htons(size);
695 hdr->nexthdr = IPPROTO_ICMPV6;
696 hdr->hop_limit = 255;
697 hdr->saddr = *saddr;
698 hdr->daddr = *daddr;
699
Amerigo Wang00f97da2013-06-03 16:31:36 +0000700 icmp6h = (struct icmp6hdr *) skb_put(send_skb, sizeof(struct icmp6hdr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000701 icmp6h->icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
702 icmp6h->icmp6_router = 0;
703 icmp6h->icmp6_solicited = 1;
Amerigo Wang00f97da2013-06-03 16:31:36 +0000704
705 target = (struct in6_addr *) skb_put(send_skb, sizeof(struct in6_addr));
Cong Wangb3d936f2013-01-07 20:52:41 +0000706 *target = msg->target;
707 icmp6h->icmp6_cksum = csum_ipv6_magic(saddr, daddr, size,
708 IPPROTO_ICMPV6,
709 csum_partial(icmp6h,
710 size, 0));
711
712 if (dev_hard_header(send_skb, skb->dev, ETH_P_IPV6,
713 lladdr, np->dev->dev_addr,
714 send_skb->len) < 0) {
715 kfree_skb(send_skb);
716 continue;
717 }
718
719 netpoll_send_skb(np, send_skb);
720
721 /* If there are several rx_hooks for the same address,
722 we're fine by sending a single reply */
723 break;
724 }
725 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Cong Wangb3d936f2013-01-07 20:52:41 +0000730static bool pkt_is_ns(struct sk_buff *skb)
731{
732 struct nd_msg *msg;
733 struct ipv6hdr *hdr;
734
735 if (skb->protocol != htons(ETH_P_ARP))
736 return false;
737 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
738 return false;
739
740 msg = (struct nd_msg *)skb_transport_header(skb);
741 __skb_push(skb, skb->data - skb_transport_header(skb));
742 hdr = ipv6_hdr(skb);
743
744 if (hdr->nexthdr != IPPROTO_ICMPV6)
745 return false;
746 if (hdr->hop_limit != 255)
747 return false;
748 if (msg->icmph.icmp6_code != 0)
749 return false;
750 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
751 return false;
752
753 return true;
754}
755
Amerigo Wang57c5d462012-08-10 01:24:40 +0000756int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 int proto, len, ulen;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000759 int hits = 0;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000760 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct udphdr *uh;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000762 struct netpoll *np, *tmp;
Neil Horman068c6e92006-06-26 00:04:27 -0700763
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000764 if (list_empty(&npinfo->rx_np))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (skb->dev->type != ARPHRD_ETHER)
768 goto out;
769
David S. Millerd9452e92008-03-04 12:28:49 -0800770 /* check if netpoll clients need ARP */
Cong Wangb3d936f2013-01-07 20:52:41 +0000771 if (skb->protocol == htons(ETH_P_ARP) && atomic_read(&trapped)) {
772 skb_queue_tail(&npinfo->neigh_tx, skb);
773 return 1;
774 } else if (pkt_is_ns(skb) && atomic_read(&trapped)) {
Cong Wangb7394d22013-01-07 20:52:39 +0000775 skb_queue_tail(&npinfo->neigh_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 1;
777 }
778
Amerigo Wang689971b2012-08-10 01:24:49 +0000779 if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
780 skb = vlan_untag(skb);
781 if (unlikely(!skb))
782 goto out;
783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 proto = ntohs(eth_hdr(skb)->h_proto);
Cong Wangb7394d22013-01-07 20:52:39 +0000786 if (proto != ETH_P_IP && proto != ETH_P_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto out;
788 if (skb->pkt_type == PACKET_OTHERHOST)
789 goto out;
790 if (skb_shared(skb))
791 goto out;
792
Cong Wangb7394d22013-01-07 20:52:39 +0000793 if (proto == ETH_P_IP) {
794 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
795 goto out;
796 iph = (struct iphdr *)skb->data;
797 if (iph->ihl < 5 || iph->version != 4)
798 goto out;
799 if (!pskb_may_pull(skb, iph->ihl*4))
800 goto out;
801 iph = (struct iphdr *)skb->data;
802 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Cong Wangb7394d22013-01-07 20:52:39 +0000805 len = ntohs(iph->tot_len);
806 if (skb->len < len || len < iph->ihl*4)
807 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Cong Wangb7394d22013-01-07 20:52:39 +0000809 /*
810 * Our transport medium may have padded the buffer out.
811 * Now We trim to the true length of the frame.
812 */
813 if (pskb_trim_rcsum(skb, len))
814 goto out;
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700815
Cong Wangb7394d22013-01-07 20:52:39 +0000816 iph = (struct iphdr *)skb->data;
817 if (iph->protocol != IPPROTO_UDP)
818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Cong Wangb7394d22013-01-07 20:52:39 +0000820 len -= iph->ihl*4;
821 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
822 ulen = ntohs(uh->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Cong Wangb7394d22013-01-07 20:52:39 +0000824 if (ulen != len)
825 goto out;
826 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
827 goto out;
828 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
829 if (np->local_ip.ip && np->local_ip.ip != iph->daddr)
830 continue;
831 if (np->remote_ip.ip && np->remote_ip.ip != iph->saddr)
832 continue;
833 if (np->local_port && np->local_port != ntohs(uh->dest))
834 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Cong Wangb7394d22013-01-07 20:52:39 +0000836 np->rx_hook(np, ntohs(uh->source),
837 (char *)(uh+1),
838 ulen - sizeof(struct udphdr));
839 hits++;
840 }
Cong Wangb3d936f2013-01-07 20:52:41 +0000841 } else {
842#if IS_ENABLED(CONFIG_IPV6)
843 const struct ipv6hdr *ip6h;
844
845 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
846 goto out;
847 ip6h = (struct ipv6hdr *)skb->data;
848 if (ip6h->version != 6)
849 goto out;
850 len = ntohs(ip6h->payload_len);
851 if (!len)
852 goto out;
853 if (len + sizeof(struct ipv6hdr) > skb->len)
854 goto out;
855 if (pskb_trim_rcsum(skb, len + sizeof(struct ipv6hdr)))
856 goto out;
857 ip6h = ipv6_hdr(skb);
858 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
859 goto out;
860 uh = udp_hdr(skb);
861 ulen = ntohs(uh->len);
862 if (ulen != skb->len)
863 goto out;
864 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
865 goto out;
866 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
Cong Wangfaeed822013-01-27 15:55:20 +0000867 if (!ipv6_addr_equal(&np->local_ip.in6, &ip6h->daddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000868 continue;
Cong Wangfaeed822013-01-27 15:55:20 +0000869 if (!ipv6_addr_equal(&np->remote_ip.in6, &ip6h->saddr))
Cong Wangb3d936f2013-01-07 20:52:41 +0000870 continue;
871 if (np->local_port && np->local_port != ntohs(uh->dest))
872 continue;
873
874 np->rx_hook(np, ntohs(uh->source),
875 (char *)(uh+1),
876 ulen - sizeof(struct udphdr));
877 hits++;
878 }
879#endif
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000880 }
881
882 if (!hits)
883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 kfree_skb(skb);
886 return 1;
887
888out:
889 if (atomic_read(&trapped)) {
890 kfree_skb(skb);
891 return 1;
892 }
893
894 return 0;
895}
896
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700897void netpoll_print_options(struct netpoll *np)
898{
Joe Perchese6ec269352012-01-29 15:50:43 +0000899 np_info(np, "local port %d\n", np->local_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000900 if (np->ipv6)
901 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
902 else
Cong Wangb7394d22013-01-07 20:52:39 +0000903 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000904 np_info(np, "interface '%s'\n", np->dev_name);
905 np_info(np, "remote port %d\n", np->remote_port);
Cong Wangb3d936f2013-01-07 20:52:41 +0000906 if (np->ipv6)
907 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
908 else
Cong Wangb7394d22013-01-07 20:52:39 +0000909 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
Joe Perchese6ec269352012-01-29 15:50:43 +0000910 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700911}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000912EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700913
Cong Wangb7394d22013-01-07 20:52:39 +0000914static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
915{
916 const char *end;
917
918 if (!strchr(str, ':') &&
919 in4_pton(str, -1, (void *)addr, -1, &end) > 0) {
920 if (!*end)
921 return 0;
922 }
923 if (in6_pton(str, -1, addr->in6.s6_addr, -1, &end) > 0) {
924#if IS_ENABLED(CONFIG_IPV6)
925 if (!*end)
926 return 1;
927#else
928 return -1;
929#endif
930 }
931 return -1;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934int netpoll_parse_options(struct netpoll *np, char *opt)
935{
936 char *cur=opt, *delim;
Cong Wangb7394d22013-01-07 20:52:39 +0000937 int ipv6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
David S. Millerc68b9072006-11-14 20:40:49 -0800939 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if ((delim = strchr(cur, '@')) == NULL)
941 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800942 *delim = 0;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000943 if (kstrtou16(cur, 10, &np->local_port))
944 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800945 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
David S. Millerc68b9072006-11-14 20:40:49 -0800949 if (*cur != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if ((delim = strchr(cur, '/')) == NULL)
951 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800952 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000953 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
954 if (ipv6 < 0)
955 goto parse_failed;
956 else
957 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800958 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 cur++;
961
David S. Millerc68b9072006-11-14 20:40:49 -0800962 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* parse out dev name */
964 if ((delim = strchr(cur, ',')) == NULL)
965 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800966 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800968 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 cur++;
971
David S. Millerc68b9072006-11-14 20:40:49 -0800972 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* dst port */
974 if ((delim = strchr(cur, '@')) == NULL)
975 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800976 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000977 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000978 np_info(np, "warning: whitespace is not allowed\n");
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000979 if (kstrtou16(cur, 10, &np->remote_port))
980 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800981 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /* dst ip */
986 if ((delim = strchr(cur, '/')) == NULL)
987 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800988 *delim = 0;
Cong Wangb7394d22013-01-07 20:52:39 +0000989 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
990 if (ipv6 < 0)
991 goto parse_failed;
992 else if (np->ipv6 != (bool)ipv6)
993 goto parse_failed;
994 else
995 np->ipv6 = (bool)ipv6;
David S. Millerc68b9072006-11-14 20:40:49 -0800996 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
David S. Millerc68b9072006-11-14 20:40:49 -0800998 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +00001000 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
Satyam Sharma0bcc1812007-08-10 15:35:05 -07001004 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 return 0;
1007
1008 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +00001009 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -1;
1011}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001012EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Amerigo Wang47be03a22012-08-10 01:24:37 +00001014int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001015{
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001016 struct netpoll_info *npinfo;
1017 const struct net_device_ops *ops;
1018 unsigned long flags;
1019 int err;
1020
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001021 np->dev = ndev;
1022 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001023 INIT_WORK(&np->cleanup_work, netpoll_async_cleanup);
Jiri Pirko30fdd8a02012-07-17 05:22:35 +00001024
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001025 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
1026 !ndev->netdev_ops->ndo_poll_controller) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001027 np_err(np, "%s doesn't support polling, aborting\n",
1028 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001029 err = -ENOTSUPP;
1030 goto out;
1031 }
1032
1033 if (!ndev->npinfo) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001034 npinfo = kmalloc(sizeof(*npinfo), gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001035 if (!npinfo) {
1036 err = -ENOMEM;
1037 goto out;
1038 }
1039
1040 npinfo->rx_flags = 0;
1041 INIT_LIST_HEAD(&npinfo->rx_np);
1042
1043 spin_lock_init(&npinfo->rx_lock);
Neil Hormanbd7c4b62013-04-30 05:35:05 +00001044 sema_init(&npinfo->dev_lock, 1);
Cong Wangb7394d22013-01-07 20:52:39 +00001045 skb_queue_head_init(&npinfo->neigh_tx);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001046 skb_queue_head_init(&npinfo->txq);
1047 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
1048
1049 atomic_set(&npinfo->refcnt, 1);
1050
1051 ops = np->dev->netdev_ops;
1052 if (ops->ndo_netpoll_setup) {
Amerigo Wang47be03a22012-08-10 01:24:37 +00001053 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001054 if (err)
1055 goto free_npinfo;
1056 }
1057 } else {
Neil Horman0790bbb2013-02-11 10:25:31 +00001058 npinfo = rtnl_dereference(ndev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001059 atomic_inc(&npinfo->refcnt);
1060 }
1061
1062 npinfo->netpoll = np;
1063
1064 if (np->rx_hook) {
1065 spin_lock_irqsave(&npinfo->rx_lock, flags);
1066 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
1067 list_add_tail(&np->rx, &npinfo->rx_np);
1068 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1069 }
1070
1071 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +00001072 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001073
1074 return 0;
1075
1076free_npinfo:
1077 kfree(npinfo);
1078out:
1079 return err;
1080}
1081EXPORT_SYMBOL_GPL(__netpoll_setup);
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083int netpoll_setup(struct netpoll *np)
1084{
1085 struct net_device *ndev = NULL;
1086 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001087 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Cong Wangf92d3182013-01-14 23:34:06 +00001089 rtnl_lock();
Cong Wang556e6252013-01-27 15:55:21 +00001090 if (np->dev_name) {
1091 struct net *net = current->nsproxy->net_ns;
1092 ndev = __dev_get_by_name(net, np->dev_name);
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001095 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Cong Wangf92d3182013-01-14 23:34:06 +00001096 err = -ENODEV;
1097 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
Cong Wang5bd30d32013-01-17 12:21:08 +08001099 dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jiri Pirko49bd8fb2013-01-03 22:48:55 +00001101 if (netdev_master_upper_dev_get(ndev)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001102 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -07001103 err = -EBUSY;
1104 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -07001105 }
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!netif_running(ndev)) {
1108 unsigned long atmost, atleast;
1109
Joe Perchese6ec269352012-01-29 15:50:43 +00001110 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001112 err = dev_open(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001113
1114 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001115 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +00001116 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Cong Wangf92d3182013-01-14 23:34:06 +00001119 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -07001121 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 while (!netif_carrier_ok(ndev)) {
1123 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001124 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 break;
1126 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -07001127 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129
1130 /* If carrier appears to come up instantly, we don't
1131 * trust it and pause so that we don't pump all our
1132 * queued console messages into the bitbucket.
1133 */
1134
1135 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +00001136 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 msleep(4000);
1138 }
Cong Wangf92d3182013-01-14 23:34:06 +00001139 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
Cong Wangb7394d22013-01-07 20:52:39 +00001142 if (!np->local_ip.ip) {
1143 if (!np->ipv6) {
Cong Wangf92d3182013-01-14 23:34:06 +00001144 in_dev = __in_dev_get_rtnl(ndev);
Cong Wangb7394d22013-01-07 20:52:39 +00001145
1146 if (!in_dev || !in_dev->ifa_list) {
Cong Wangb7394d22013-01-07 20:52:39 +00001147 np_err(np, "no IP address for %s, aborting\n",
1148 np->dev_name);
1149 err = -EDESTADDRREQ;
1150 goto put;
1151 }
1152
1153 np->local_ip.ip = in_dev->ifa_list->ifa_local;
Cong Wangb7394d22013-01-07 20:52:39 +00001154 np_info(np, "local IP %pI4\n", &np->local_ip.ip);
Cong Wangb3d936f2013-01-07 20:52:41 +00001155 } else {
1156#if IS_ENABLED(CONFIG_IPV6)
1157 struct inet6_dev *idev;
1158
1159 err = -EDESTADDRREQ;
Cong Wangb3d936f2013-01-07 20:52:41 +00001160 idev = __in6_dev_get(ndev);
1161 if (idev) {
1162 struct inet6_ifaddr *ifp;
1163
1164 read_lock_bh(&idev->lock);
1165 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1166 if (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)
1167 continue;
1168 np->local_ip.in6 = ifp->addr;
1169 err = 0;
1170 break;
1171 }
1172 read_unlock_bh(&idev->lock);
1173 }
Cong Wangb3d936f2013-01-07 20:52:41 +00001174 if (err) {
1175 np_err(np, "no IPv6 address for %s, aborting\n",
1176 np->dev_name);
1177 goto put;
1178 } else
1179 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
1180#else
1181 np_err(np, "IPv6 is not supported %s, aborting\n",
1182 np->dev_name);
Cong Wange39363a2013-01-22 17:39:11 +00001183 err = -EINVAL;
Cong Wangb3d936f2013-01-07 20:52:41 +00001184 goto put;
1185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
Herbert Xudbaa1542010-06-10 16:12:46 +00001189 /* fill up the skb queue */
1190 refill_skbs();
1191
Amerigo Wang47be03a22012-08-10 01:24:37 +00001192 err = __netpoll_setup(np, ndev, GFP_KERNEL);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001193 if (err)
1194 goto put;
1195
Cong Wangf92d3182013-01-14 23:34:06 +00001196 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
1198
Jiri Slaby21edbb22010-03-16 05:29:54 +00001199put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 dev_put(ndev);
Cong Wangf92d3182013-01-14 23:34:06 +00001201unlock:
1202 rtnl_unlock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -07001203 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001205EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
David S. Millerc68b9072006-11-14 20:40:49 -08001207static int __init netpoll_init(void)
1208{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -08001209 skb_queue_head_init(&skb_pool);
1210 return 0;
1211}
1212core_initcall(netpoll_init);
1213
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001214static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
1215{
1216 struct netpoll_info *npinfo =
1217 container_of(rcu_head, struct netpoll_info, rcu);
1218
Cong Wangb7394d22013-01-07 20:52:39 +00001219 skb_queue_purge(&npinfo->neigh_tx);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001220 skb_queue_purge(&npinfo->txq);
1221
1222 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
1223 cancel_delayed_work(&npinfo->tx_work);
1224
1225 /* clean after last, unfinished work */
1226 __skb_queue_purge(&npinfo->txq);
1227 /* now cancel it again */
1228 cancel_delayed_work(&npinfo->tx_work);
1229 kfree(npinfo);
1230}
1231
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001232void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -07001234 struct netpoll_info *npinfo;
1235 unsigned long flags;
1236
Neil Horman0790bbb2013-02-11 10:25:31 +00001237 /* rtnl_dereference would be preferable here but
1238 * rcu_cleanup_netpoll path can put us in here safely without
1239 * holding the rtnl, so plain rcu_dereference it is
1240 */
1241 npinfo = rtnl_dereference(np->dev->npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001242 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +00001243 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -07001244
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001245 if (!list_empty(&npinfo->rx_np)) {
1246 spin_lock_irqsave(&npinfo->rx_lock, flags);
1247 list_del(&np->rx);
1248 if (list_empty(&npinfo->rx_np))
1249 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
1250 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Jeff Moyer115c1d62005-06-22 22:05:31 -07001251 }
Herbert Xudbaa1542010-06-10 16:12:46 +00001252
Neil Hormanca99ca12013-02-05 08:05:43 +00001253 synchronize_srcu(&netpoll_srcu);
1254
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001255 if (atomic_dec_and_test(&npinfo->refcnt)) {
1256 const struct net_device_ops *ops;
1257
1258 ops = np->dev->netdev_ops;
1259 if (ops->ndo_netpoll_cleanup)
1260 ops->ndo_netpoll_cleanup(np->dev);
1261
Neil Horman2cde6ac2013-02-11 10:25:30 +00001262 rcu_assign_pointer(np->dev->npinfo, NULL);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001263 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
Herbert Xudbaa1542010-06-10 16:12:46 +00001264 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001265}
1266EXPORT_SYMBOL_GPL(__netpoll_cleanup);
1267
Neil Horman2cde6ac2013-02-11 10:25:30 +00001268static void netpoll_async_cleanup(struct work_struct *work)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001269{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001270 struct netpoll *np = container_of(work, struct netpoll, cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001271
Neil Horman2cde6ac2013-02-11 10:25:30 +00001272 rtnl_lock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001273 __netpoll_cleanup(np);
Neil Horman2cde6ac2013-02-11 10:25:30 +00001274 rtnl_unlock();
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001275 kfree(np);
1276}
1277
Neil Horman2cde6ac2013-02-11 10:25:30 +00001278void __netpoll_free_async(struct netpoll *np)
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001279{
Neil Horman2cde6ac2013-02-11 10:25:30 +00001280 schedule_work(&np->cleanup_work);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001281}
Neil Horman2cde6ac2013-02-11 10:25:30 +00001282EXPORT_SYMBOL_GPL(__netpoll_free_async);
Amerigo Wang38e6bc12012-08-10 01:24:38 +00001283
Herbert Xu8fdd95e2010-06-10 16:12:48 +00001284void netpoll_cleanup(struct netpoll *np)
1285{
1286 if (!np->dev)
1287 return;
1288
1289 rtnl_lock();
1290 __netpoll_cleanup(np);
1291 rtnl_unlock();
Herbert Xudbaa1542010-06-10 16:12:46 +00001292
1293 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 np->dev = NULL;
1295}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001296EXPORT_SYMBOL(netpoll_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298int netpoll_trap(void)
1299{
1300 return atomic_read(&trapped);
1301}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +00001302EXPORT_SYMBOL(netpoll_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304void netpoll_set_trap(int trap)
1305{
1306 if (trap)
1307 atomic_inc(&trapped);
1308 else
1309 atomic_dec(&trapped);
1310}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311EXPORT_SYMBOL(netpoll_set_trap);