blob: 174346ac15a0a9edd79b4dc6d8dc071eca40694f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/tcp.h>
30#include <net/udp.h>
31#include <asm/unaligned.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -070032#include <trace/events/napi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
35 * We maintain a small pool of fully-sized skbs, to make sure the
36 * message gets out even in extreme OOM situations.
37 */
38
39#define MAX_UDP_CHUNK 1460
40#define MAX_SKBS 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080042static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static atomic_t trapped;
45
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070046#define USEC_PER_POLL 50
David S. Millerd9452e92008-03-04 12:28:49 -080047#define NETPOLL_RX_ENABLED 1
48#define NETPOLL_RX_DROP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Joe Perches6f706242012-01-29 15:50:44 +000050#define MAX_SKB_SIZE \
51 (sizeof(struct ethhdr) + \
52 sizeof(struct iphdr) + \
53 sizeof(struct udphdr) + \
54 MAX_UDP_CHUNK)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David S. Miller3578b0c2010-08-03 00:24:04 -070056static void zap_completion_queue(void);
Amerigo Wang28996562012-08-10 01:24:42 +000057static void netpoll_arp_reply(struct sk_buff *skb, struct netpoll_info *npinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Anton Vorontsovbff38772009-07-08 11:10:56 -070059static unsigned int carrier_timeout = 4;
60module_param(carrier_timeout, uint, 0644);
61
Joe Perchese6ec269352012-01-29 15:50:43 +000062#define np_info(np, fmt, ...) \
63 pr_info("%s: " fmt, np->name, ##__VA_ARGS__)
64#define np_err(np, fmt, ...) \
65 pr_err("%s: " fmt, np->name, ##__VA_ARGS__)
66#define np_notice(np, fmt, ...) \
67 pr_notice("%s: " fmt, np->name, ##__VA_ARGS__)
68
David Howellsc4028952006-11-22 14:57:56 +000069static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
David Howells4c1ac1b2006-12-05 14:37:56 +000071 struct netpoll_info *npinfo =
72 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010074 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070076 while ((skb = skb_dequeue(&npinfo->txq))) {
77 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080078 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070079 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070081 if (!netif_device_present(dev) || !netif_running(dev)) {
82 __kfree_skb(skb);
83 continue;
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
David S. Millerfd2ea0a2008-07-17 01:56:23 -070086 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
87
Ingo Molnar36405432006-12-12 17:20:42 +010088 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070089 __netif_tx_lock(txq, smp_processor_id());
Tom Herbert734664982011-11-28 16:32:44 +000090 if (netif_xmit_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080091 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070092 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070093 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +010094 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Jarek Poplawski25442ca2007-07-05 17:42:44 -070096 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070097 return;
98 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -070099 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +0100100 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102}
103
Al Virob51655b2006-11-14 21:40:42 -0800104static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
105 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Al Virod6f5493c2006-11-14 21:26:08 -0800107 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -0800108
Herbert Xu60476372007-04-09 11:59:39 -0700109 if (uh->check == 0 || skb_csum_unnecessary(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111
Herbert Xufb286bb2005-11-10 13:01:24 -0800112 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Patrick McHardy84fa7932006-08-29 16:44:56 -0700114 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -0800115 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -0800116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Herbert Xufb286bb2005-11-10 13:01:24 -0800118 skb->csum = psum;
119
120 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/*
124 * Check whether delayed processing was scheduled for our NIC. If so,
125 * we attempt to grab the poll lock and use ->poll() to pump the card.
126 * If this fails, either we've recursed in ->poll() or it's already
127 * running on another CPU.
128 *
129 * Note: we don't mask interrupts with this lock because we're using
130 * trylock here and interrupts are already disabled in the softirq
131 * case. Further, we test the poll_owner to avoid recursion on UP
132 * systems where the lock doesn't exist.
133 *
134 * In cases where there is bi-directional communications, reading only
135 * one message at a time can lead to packets being dropped by the
136 * network adapter, forcing superfluous retries and possibly timeouts.
137 * Thus, we set our budget to greater than 1.
138 */
David S. Miller0a7606c2007-10-29 21:28:47 -0700139static int poll_one_napi(struct netpoll_info *npinfo,
140 struct napi_struct *napi, int budget)
141{
142 int work;
143
144 /* net_rx_action's ->poll() invocations and our's are
145 * synchronized by this test which is only made while
146 * holding the napi->poll_lock.
147 */
148 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
149 return budget;
150
David S. Millerd9452e92008-03-04 12:28:49 -0800151 npinfo->rx_flags |= NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700152 atomic_inc(&trapped);
Neil Horman7b363e42008-12-09 23:22:26 -0800153 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700154
155 work = napi->poll(napi, budget);
David S. Miller7d18f112009-05-21 23:30:09 -0700156 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700157
Neil Horman7b363e42008-12-09 23:22:26 -0800158 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700159 atomic_dec(&trapped);
David S. Millerd9452e92008-03-04 12:28:49 -0800160 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700161
162 return budget - work;
163}
164
Stephen Hemminger51069302007-11-19 19:18:11 -0800165static void poll_napi(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700167 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int budget = 16;
169
Neil Hormanf13d4932010-10-19 07:04:26 +0000170 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700171 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700172 spin_trylock(&napi->poll_lock)) {
Amerigo Wang28996562012-08-10 01:24:42 +0000173 budget = poll_one_napi(rcu_dereference_bh(dev->npinfo),
174 napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700175 spin_unlock(&napi->poll_lock);
David S. Miller0a7606c2007-10-29 21:28:47 -0700176
177 if (!budget)
178 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181}
182
Neil Horman068c6e92006-06-26 00:04:27 -0700183static void service_arp_queue(struct netpoll_info *npi)
184{
Stephen Hemminger51069302007-11-19 19:18:11 -0800185 if (npi) {
186 struct sk_buff *skb;
Neil Horman068c6e92006-06-26 00:04:27 -0700187
Stephen Hemminger51069302007-11-19 19:18:11 -0800188 while ((skb = skb_dequeue(&npi->arp_tx)))
Amerigo Wang28996562012-08-10 01:24:42 +0000189 netpoll_arp_reply(skb, npi);
Neil Horman068c6e92006-06-26 00:04:27 -0700190 }
Neil Horman068c6e92006-06-26 00:04:27 -0700191}
192
Joe Perches234b9212011-06-30 15:08:57 +0000193static void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000195 const struct net_device_ops *ops;
Amerigo Wang28996562012-08-10 01:24:42 +0000196 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo);
Stephen Hemminger51069302007-11-19 19:18:11 -0800197
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000198 if (!dev || !netif_running(dev))
199 return;
200
201 ops = dev->netdev_ops;
202 if (!ops->ndo_poll_controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return;
204
205 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800206 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Stephen Hemminger51069302007-11-19 19:18:11 -0800208 poll_napi(dev);
209
Eric Dumazet58e05f32012-02-14 10:11:59 +0000210 if (dev->flags & IFF_SLAVE) {
Amerigo Wang28996562012-08-10 01:24:42 +0000211 if (ni) {
Amerigo Wang5a698af2011-02-17 23:43:34 +0000212 struct net_device *bond_dev = dev->master;
213 struct sk_buff *skb;
Amerigo Wang28996562012-08-10 01:24:42 +0000214 struct netpoll_info *bond_ni = rcu_dereference_bh(bond_dev->npinfo);
215 while ((skb = skb_dequeue(&ni->arp_tx))) {
Amerigo Wang5a698af2011-02-17 23:43:34 +0000216 skb->dev = bond_dev;
Amerigo Wang28996562012-08-10 01:24:42 +0000217 skb_queue_tail(&bond_ni->arp_tx, skb);
Amerigo Wang5a698af2011-02-17 23:43:34 +0000218 }
219 }
220 }
221
Amerigo Wang28996562012-08-10 01:24:42 +0000222 service_arp_queue(ni);
Neil Horman068c6e92006-06-26 00:04:27 -0700223
David S. Miller3578b0c2010-08-03 00:24:04 -0700224 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227static void refill_skbs(void)
228{
229 struct sk_buff *skb;
230 unsigned long flags;
231
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800232 spin_lock_irqsave(&skb_pool.lock, flags);
233 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
235 if (!skb)
236 break;
237
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800238 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800240 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
David S. Miller3578b0c2010-08-03 00:24:04 -0700243static void zap_completion_queue(void)
244{
245 unsigned long flags;
246 struct softnet_data *sd = &get_cpu_var(softnet_data);
247
248 if (sd->completion_queue) {
249 struct sk_buff *clist;
250
251 local_irq_save(flags);
252 clist = sd->completion_queue;
253 sd->completion_queue = NULL;
254 local_irq_restore(flags);
255
256 while (clist != NULL) {
257 struct sk_buff *skb = clist;
258 clist = clist->next;
259 if (skb->destructor) {
260 atomic_inc(&skb->users);
261 dev_kfree_skb_any(skb); /* put this one back */
262 } else {
263 __kfree_skb(skb);
264 }
265 }
266 }
267
268 put_cpu_var(softnet_data);
269}
270
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800271static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800273 int count = 0;
274 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David S. Miller3578b0c2010-08-03 00:24:04 -0700276 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800277 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800281 if (!skb)
282 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800285 if (++count < 10) {
Joe Perches2a49e002011-06-30 15:08:58 +0000286 netpoll_poll_dev(np->dev);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800287 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800289 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
292 atomic_set(&skb->users, 1);
293 skb_reserve(skb, reserve);
294 return skb;
295}
296
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700297static int netpoll_owner_active(struct net_device *dev)
298{
299 struct napi_struct *napi;
300
301 list_for_each_entry(napi, &dev->napi_list, dev_list) {
302 if (napi->poll_owner == smp_processor_id())
303 return 1;
304 }
305 return 0;
306}
307
Amerigo Wang28996562012-08-10 01:24:42 +0000308/* call with IRQ disabled */
Neil Hormanc2355e12010-10-13 16:01:49 +0000309void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
310 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700312 int status = NETDEV_TX_BUSY;
313 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800314 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000315 /* It is up to the caller to keep npinfo alive. */
Amerigo Wang28996562012-08-10 01:24:42 +0000316 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Amerigo Wang28996562012-08-10 01:24:42 +0000318 WARN_ON_ONCE(!irqs_disabled());
319
320 npinfo = rcu_dereference_bh(np->dev->npinfo);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900321 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
322 __kfree_skb(skb);
323 return;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700326 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700327 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700328 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800329
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700330 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
331
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700332 /* try until next clock tick */
333 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
334 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700335 if (__netif_tx_trylock(txq)) {
Tom Herbert734664982011-11-28 16:32:44 +0000336 if (!netif_xmit_stopped(txq)) {
Stephen Hemminger00829822008-11-20 20:14:53 -0800337 status = ops->ndo_start_xmit(skb, dev);
Eric Dumazet08baf562009-05-25 22:58:01 -0700338 if (status == NETDEV_TX_OK)
339 txq_trans_update(txq);
340 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700341 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700342
Andrew Mortone37b8d92006-12-09 14:01:49 -0800343 if (status == NETDEV_TX_OK)
344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Andrew Mortone37b8d92006-12-09 14:01:49 -0800346 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700347
348 /* tickle device maybe there is some cleanup */
Joe Perches2a49e002011-06-30 15:08:58 +0000349 netpoll_poll_dev(np->dev);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700350
351 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700352 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000353
354 WARN_ONCE(!irqs_disabled(),
Amerigo Wang28996562012-08-10 01:24:42 +0000355 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pF)\n",
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000356 dev->name, ops->ndo_start_xmit);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700360 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700361 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000362 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
Neil Hormanc2355e12010-10-13 16:01:49 +0000365EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
368{
Eric Dumazet954fba02012-06-12 19:30:21 +0000369 int total_len, ip_len, udp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct sk_buff *skb;
371 struct udphdr *udph;
372 struct iphdr *iph;
373 struct ethhdr *eth;
374
375 udp_len = len + sizeof(*udph);
Eric Dumazet954fba02012-06-12 19:30:21 +0000376 ip_len = udp_len + sizeof(*iph);
377 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Eric Dumazet954fba02012-06-12 19:30:21 +0000379 skb = find_skb(np, total_len + np->dev->needed_tailroom,
380 total_len - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!skb)
382 return;
383
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300384 skb_copy_to_linear_data(skb, msg, len);
Eric Dumazet954fba02012-06-12 19:30:21 +0000385 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300387 skb_push(skb, sizeof(*udph));
388 skb_reset_transport_header(skb);
389 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 udph->source = htons(np->local_port);
391 udph->dest = htons(np->remote_port);
392 udph->len = htons(udp_len);
393 udph->check = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000394 udph->check = csum_tcpudp_magic(np->local_ip,
395 np->remote_ip,
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800396 udp_len, IPPROTO_UDP,
Joe Perches07f07572008-11-19 15:44:53 -0800397 csum_partial(udph, udp_len, 0));
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800398 if (udph->check == 0)
Al Viro5e57dff2006-11-20 18:08:13 -0800399 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700401 skb_push(skb, sizeof(*iph));
402 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700403 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* iph->version = 4; iph->ihl = 5; */
406 put_unaligned(0x45, (unsigned char *)iph);
407 iph->tos = 0;
408 put_unaligned(htons(ip_len), &(iph->tot_len));
409 iph->id = 0;
410 iph->frag_off = 0;
411 iph->ttl = 64;
412 iph->protocol = IPPROTO_UDP;
413 iph->check = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000414 put_unaligned(np->local_ip, &(iph->saddr));
415 put_unaligned(np->remote_ip, &(iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
417
418 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700419 skb_reset_mac_header(skb);
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700420 skb->protocol = eth->h_proto = htons(ETH_P_IP);
Stephen Hemminger09538642007-11-19 19:23:29 -0800421 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
422 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 skb->dev = np->dev;
425
426 netpoll_send_skb(np, skb);
427}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000428EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Amerigo Wang28996562012-08-10 01:24:42 +0000430static void netpoll_arp_reply(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct arphdr *arp;
433 unsigned char *arp_ptr;
434 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
Al Viro252e33462006-11-14 20:48:11 -0800435 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800436 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000438 struct netpoll *np, *tmp;
439 unsigned long flags;
Herbert Xuae641942011-11-18 02:20:04 +0000440 int hlen, tlen;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000441 int hits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000443 if (list_empty(&npinfo->rx_np))
444 return;
445
446 /* Before checking the packet, we do some early
447 inspection whether this is interesting at all */
448 spin_lock_irqsave(&npinfo->rx_lock, flags);
449 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
450 if (np->dev == skb->dev)
451 hits++;
452 }
453 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
454
455 /* No netpoll struct is using this dev */
456 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700457 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* No arp on this interface */
460 if (skb->dev->flags & IFF_NOARP)
461 return;
462
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800463 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return;
465
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700466 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300467 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300468 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
471 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
472 arp->ar_pro != htons(ETH_P_IP) ||
473 arp->ar_op != htons(ARPOP_REQUEST))
474 return;
475
Neil Horman47bbec02006-12-08 00:05:55 -0800476 arp_ptr = (unsigned char *)(arp+1);
477 /* save the location of the src hw addr */
478 sha = arp_ptr;
479 arp_ptr += skb->dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 memcpy(&sip, arp_ptr, 4);
Neil Horman47bbec02006-12-08 00:05:55 -0800481 arp_ptr += 4;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000482 /* If we actually cared about dst hw addr,
483 it would get copied here */
Neil Horman47bbec02006-12-08 00:05:55 -0800484 arp_ptr += skb->dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 memcpy(&tip, arp_ptr, 4);
486
487 /* Should we ignore arp? */
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000488 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return;
490
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800491 size = arp_hdr_len(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000493 spin_lock_irqsave(&npinfo->rx_lock, flags);
494 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
495 if (tip != np->local_ip)
496 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Herbert Xuae641942011-11-18 02:20:04 +0000498 hlen = LL_RESERVED_SPACE(np->dev);
499 tlen = np->dev->needed_tailroom;
500 send_skb = find_skb(np, size + hlen + tlen, hlen);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000501 if (!send_skb)
502 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000504 skb_reset_network_header(send_skb);
505 arp = (struct arphdr *) skb_put(send_skb, size);
506 send_skb->dev = skb->dev;
507 send_skb->protocol = htons(ETH_P_ARP);
508
509 /* Fill the device header for the ARP frame */
510 if (dev_hard_header(send_skb, skb->dev, ptype,
511 sha, np->dev->dev_addr,
512 send_skb->len) < 0) {
513 kfree_skb(send_skb);
514 continue;
515 }
516
517 /*
518 * Fill out the arp protocol part.
519 *
520 * we only support ethernet device type,
521 * which (according to RFC 1390) should
522 * always equal 1 (Ethernet).
523 */
524
525 arp->ar_hrd = htons(np->dev->type);
526 arp->ar_pro = htons(ETH_P_IP);
527 arp->ar_hln = np->dev->addr_len;
528 arp->ar_pln = 4;
529 arp->ar_op = htons(type);
530
531 arp_ptr = (unsigned char *)(arp + 1);
532 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
533 arp_ptr += np->dev->addr_len;
534 memcpy(arp_ptr, &tip, 4);
535 arp_ptr += 4;
536 memcpy(arp_ptr, sha, np->dev->addr_len);
537 arp_ptr += np->dev->addr_len;
538 memcpy(arp_ptr, &sip, 4);
539
540 netpoll_send_skb(np, send_skb);
541
542 /* If there are several rx_hooks for the same address,
543 we're fine by sending a single reply */
544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000546 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Amerigo Wang57c5d462012-08-10 01:24:40 +0000549int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 int proto, len, ulen;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000552 int hits = 0;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000553 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct udphdr *uh;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000555 struct netpoll *np, *tmp;
Neil Horman068c6e92006-06-26 00:04:27 -0700556
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000557 if (list_empty(&npinfo->rx_np))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (skb->dev->type != ARPHRD_ETHER)
561 goto out;
562
David S. Millerd9452e92008-03-04 12:28:49 -0800563 /* check if netpoll clients need ARP */
YOSHIFUJI Hideaki724800d2007-03-25 20:13:04 -0700564 if (skb->protocol == htons(ETH_P_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 atomic_read(&trapped)) {
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000566 skb_queue_tail(&npinfo->arp_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 1;
568 }
569
570 proto = ntohs(eth_hdr(skb)->h_proto);
571 if (proto != ETH_P_IP)
572 goto out;
573 if (skb->pkt_type == PACKET_OTHERHOST)
574 goto out;
575 if (skb_shared(skb))
576 goto out;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
579 goto out;
Eric Dumazete9278a42011-08-26 06:26:15 +0000580 iph = (struct iphdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (iph->ihl < 5 || iph->version != 4)
582 goto out;
583 if (!pskb_may_pull(skb, iph->ihl*4))
584 goto out;
Eric Dumazete9278a42011-08-26 06:26:15 +0000585 iph = (struct iphdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
587 goto out;
588
589 len = ntohs(iph->tot_len);
590 if (skb->len < len || len < iph->ihl*4)
591 goto out;
592
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700593 /*
594 * Our transport medium may have padded the buffer out.
595 * Now We trim to the true length of the frame.
596 */
597 if (pskb_trim_rcsum(skb, len))
598 goto out;
599
Eric Dumazete9278a42011-08-26 06:26:15 +0000600 iph = (struct iphdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (iph->protocol != IPPROTO_UDP)
602 goto out;
603
604 len -= iph->ihl*4;
605 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
606 ulen = ntohs(uh->len);
607
608 if (ulen != len)
609 goto out;
Herbert Xufb286bb2005-11-10 13:01:24 -0800610 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000613 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
614 if (np->local_ip && np->local_ip != iph->daddr)
615 continue;
616 if (np->remote_ip && np->remote_ip != iph->saddr)
617 continue;
618 if (np->local_port && np->local_port != ntohs(uh->dest))
619 continue;
620
621 np->rx_hook(np, ntohs(uh->source),
622 (char *)(uh+1),
623 ulen - sizeof(struct udphdr));
624 hits++;
625 }
626
627 if (!hits)
628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 kfree_skb(skb);
631 return 1;
632
633out:
634 if (atomic_read(&trapped)) {
635 kfree_skb(skb);
636 return 1;
637 }
638
639 return 0;
640}
641
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700642void netpoll_print_options(struct netpoll *np)
643{
Joe Perchese6ec269352012-01-29 15:50:43 +0000644 np_info(np, "local port %d\n", np->local_port);
645 np_info(np, "local IP %pI4\n", &np->local_ip);
646 np_info(np, "interface '%s'\n", np->dev_name);
647 np_info(np, "remote port %d\n", np->remote_port);
648 np_info(np, "remote IP %pI4\n", &np->remote_ip);
649 np_info(np, "remote ethernet address %pM\n", np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700650}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000651EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653int netpoll_parse_options(struct netpoll *np, char *opt)
654{
655 char *cur=opt, *delim;
656
David S. Millerc68b9072006-11-14 20:40:49 -0800657 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if ((delim = strchr(cur, '@')) == NULL)
659 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800660 *delim = 0;
661 np->local_port = simple_strtol(cur, NULL, 10);
662 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
David S. Millerc68b9072006-11-14 20:40:49 -0800666 if (*cur != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if ((delim = strchr(cur, '/')) == NULL)
668 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800669 *delim = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000670 np->local_ip = in_aton(cur);
David S. Millerc68b9072006-11-14 20:40:49 -0800671 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 cur++;
674
David S. Millerc68b9072006-11-14 20:40:49 -0800675 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* parse out dev name */
677 if ((delim = strchr(cur, ',')) == NULL)
678 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800679 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800681 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683 cur++;
684
David S. Millerc68b9072006-11-14 20:40:49 -0800685 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* dst port */
687 if ((delim = strchr(cur, '@')) == NULL)
688 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800689 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000690 if (*cur == ' ' || *cur == '\t')
Joe Perchese6ec269352012-01-29 15:50:43 +0000691 np_info(np, "warning: whitespace is not allowed\n");
David S. Millerc68b9072006-11-14 20:40:49 -0800692 np->remote_port = simple_strtol(cur, NULL, 10);
693 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* dst ip */
698 if ((delim = strchr(cur, '/')) == NULL)
699 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800700 *delim = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000701 np->remote_ip = in_aton(cur);
David S. Millerc68b9072006-11-14 20:40:49 -0800702 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David S. Millerc68b9072006-11-14 20:40:49 -0800704 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* MAC address */
Alexey Dobriyan4940fc82011-05-07 23:00:07 +0000706 if (!mac_pton(cur, np->remote_mac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto parse_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700710 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 return 0;
713
714 parse_failed:
Joe Perchese6ec269352012-01-29 15:50:43 +0000715 np_info(np, "couldn't parse config at '%s'!\n", cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return -1;
717}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000718EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Amerigo Wang47be03a22012-08-10 01:24:37 +0000720int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000721{
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000722 struct netpoll_info *npinfo;
723 const struct net_device_ops *ops;
724 unsigned long flags;
725 int err;
726
Jiri Pirko30fdd8a02012-07-17 05:22:35 +0000727 np->dev = ndev;
728 strlcpy(np->dev_name, ndev->name, IFNAMSIZ);
729
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000730 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
731 !ndev->netdev_ops->ndo_poll_controller) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000732 np_err(np, "%s doesn't support polling, aborting\n",
733 np->dev_name);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000734 err = -ENOTSUPP;
735 goto out;
736 }
737
738 if (!ndev->npinfo) {
Amerigo Wang47be03a22012-08-10 01:24:37 +0000739 npinfo = kmalloc(sizeof(*npinfo), gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000740 if (!npinfo) {
741 err = -ENOMEM;
742 goto out;
743 }
744
745 npinfo->rx_flags = 0;
746 INIT_LIST_HEAD(&npinfo->rx_np);
747
748 spin_lock_init(&npinfo->rx_lock);
749 skb_queue_head_init(&npinfo->arp_tx);
750 skb_queue_head_init(&npinfo->txq);
751 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
752
753 atomic_set(&npinfo->refcnt, 1);
754
755 ops = np->dev->netdev_ops;
756 if (ops->ndo_netpoll_setup) {
Amerigo Wang47be03a22012-08-10 01:24:37 +0000757 err = ops->ndo_netpoll_setup(ndev, npinfo, gfp);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000758 if (err)
759 goto free_npinfo;
760 }
761 } else {
762 npinfo = ndev->npinfo;
763 atomic_inc(&npinfo->refcnt);
764 }
765
766 npinfo->netpoll = np;
767
768 if (np->rx_hook) {
769 spin_lock_irqsave(&npinfo->rx_lock, flags);
770 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
771 list_add_tail(&np->rx, &npinfo->rx_np);
772 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
773 }
774
775 /* last thing to do is link it to the net device structure */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000776 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000777
778 return 0;
779
780free_npinfo:
781 kfree(npinfo);
782out:
783 return err;
784}
785EXPORT_SYMBOL_GPL(__netpoll_setup);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787int netpoll_setup(struct netpoll *np)
788{
789 struct net_device *ndev = NULL;
790 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700791 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (np->dev_name)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700794 ndev = dev_get_by_name(&init_net, np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!ndev) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000796 np_err(np, "%s doesn't exist, aborting\n", np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700797 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
WANG Cong0c1ad042011-06-09 00:28:13 -0700800 if (ndev->master) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000801 np_err(np, "%s is a slave device, aborting\n", np->dev_name);
Dan Carpenter83fe32d2011-06-11 18:55:22 -0700802 err = -EBUSY;
803 goto put;
WANG Cong0c1ad042011-06-09 00:28:13 -0700804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (!netif_running(ndev)) {
807 unsigned long atmost, atleast;
808
Joe Perchese6ec269352012-01-29 15:50:43 +0000809 np_info(np, "device %s not up yet, forcing it\n", np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800811 rtnl_lock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700812 err = dev_open(ndev);
813 rtnl_unlock();
814
815 if (err) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000816 np_err(np, "failed to open %s\n", ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +0000817 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -0700821 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 while (!netif_carrier_ok(ndev)) {
823 if (time_after(jiffies, atmost)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000824 np_notice(np, "timeout waiting for carrier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 break;
826 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -0700827 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
830 /* If carrier appears to come up instantly, we don't
831 * trust it and pause so that we don't pump all our
832 * queued console messages into the bitbucket.
833 */
834
835 if (time_before(jiffies, atleast)) {
Joe Perchese6ec269352012-01-29 15:50:43 +0000836 np_notice(np, "carrier detect appears untrustworthy, waiting 4 seconds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 msleep(4000);
838 }
839 }
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (!np->local_ip) {
842 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700843 in_dev = __in_dev_get_rcu(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (!in_dev || !in_dev->ifa_list) {
846 rcu_read_unlock();
Joe Perchese6ec269352012-01-29 15:50:43 +0000847 np_err(np, "no IP address for %s, aborting\n",
848 np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700849 err = -EDESTADDRREQ;
Herbert Xudbaa1542010-06-10 16:12:46 +0000850 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Harvey Harrisone7557af2009-03-28 15:38:31 +0000853 np->local_ip = in_dev->ifa_list->ifa_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 rcu_read_unlock();
Joe Perchese6ec269352012-01-29 15:50:43 +0000855 np_info(np, "local IP %pI4\n", &np->local_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
Herbert Xudbaa1542010-06-10 16:12:46 +0000858 /* fill up the skb queue */
859 refill_skbs();
860
861 rtnl_lock();
Amerigo Wang47be03a22012-08-10 01:24:37 +0000862 err = __netpoll_setup(np, ndev, GFP_KERNEL);
Herbert Xudbaa1542010-06-10 16:12:46 +0000863 rtnl_unlock();
Matt Mackall53fb95d2005-08-11 19:27:43 -0700864
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000865 if (err)
866 goto put;
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869
Jiri Slaby21edbb22010-03-16 05:29:54 +0000870put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 dev_put(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700872 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000874EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
David S. Millerc68b9072006-11-14 20:40:49 -0800876static int __init netpoll_init(void)
877{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800878 skb_queue_head_init(&skb_pool);
879 return 0;
880}
881core_initcall(netpoll_init);
882
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000883static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head)
884{
885 struct netpoll_info *npinfo =
886 container_of(rcu_head, struct netpoll_info, rcu);
887
888 skb_queue_purge(&npinfo->arp_tx);
889 skb_queue_purge(&npinfo->txq);
890
891 /* we can't call cancel_delayed_work_sync here, as we are in softirq */
892 cancel_delayed_work(&npinfo->tx_work);
893
894 /* clean after last, unfinished work */
895 __skb_queue_purge(&npinfo->txq);
896 /* now cancel it again */
897 cancel_delayed_work(&npinfo->tx_work);
898 kfree(npinfo);
899}
900
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000901void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700903 struct netpoll_info *npinfo;
904 unsigned long flags;
905
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000906 npinfo = np->dev->npinfo;
907 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +0000908 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700909
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000910 if (!list_empty(&npinfo->rx_np)) {
911 spin_lock_irqsave(&npinfo->rx_lock, flags);
912 list_del(&np->rx);
913 if (list_empty(&npinfo->rx_np))
914 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
915 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700916 }
Herbert Xudbaa1542010-06-10 16:12:46 +0000917
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000918 if (atomic_dec_and_test(&npinfo->refcnt)) {
919 const struct net_device_ops *ops;
920
921 ops = np->dev->netdev_ops;
922 if (ops->ndo_netpoll_cleanup)
923 ops->ndo_netpoll_cleanup(np->dev);
924
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000925 RCU_INIT_POINTER(np->dev->npinfo, NULL);
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000926 call_rcu_bh(&npinfo->rcu, rcu_cleanup_netpoll_info);
Herbert Xudbaa1542010-06-10 16:12:46 +0000927 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000928}
929EXPORT_SYMBOL_GPL(__netpoll_cleanup);
930
Amerigo Wang38e6bc12012-08-10 01:24:38 +0000931static void rcu_cleanup_netpoll(struct rcu_head *rcu_head)
932{
933 struct netpoll *np = container_of(rcu_head, struct netpoll, rcu);
934
935 __netpoll_cleanup(np);
936 kfree(np);
937}
938
939void __netpoll_free_rcu(struct netpoll *np)
940{
941 call_rcu_bh(&np->rcu, rcu_cleanup_netpoll);
942}
943EXPORT_SYMBOL_GPL(__netpoll_free_rcu);
944
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000945void netpoll_cleanup(struct netpoll *np)
946{
947 if (!np->dev)
948 return;
949
950 rtnl_lock();
951 __netpoll_cleanup(np);
952 rtnl_unlock();
Herbert Xudbaa1542010-06-10 16:12:46 +0000953
954 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 np->dev = NULL;
956}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000957EXPORT_SYMBOL(netpoll_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959int netpoll_trap(void)
960{
961 return atomic_read(&trapped);
962}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000963EXPORT_SYMBOL(netpoll_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965void netpoll_set_trap(int trap)
966{
967 if (trap)
968 atomic_inc(&trapped);
969 else
970 atomic_dec(&trapped);
971}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972EXPORT_SYMBOL(netpoll_set_trap);