blob: ee38acb6d4632fbb4c766ad666fe4b5187535034 [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
Anton Vorontsovbff38772009-07-08 11:10:56 -070012#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/string.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020016#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/inetdevice.h>
18#include <linux/inet.h>
19#include <linux/interrupt.h>
20#include <linux/netpoll.h>
21#include <linux/sched.h>
22#include <linux/delay.h>
23#include <linux/rcupdate.h>
24#include <linux/workqueue.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/tcp.h>
27#include <net/udp.h>
28#include <asm/unaligned.h>
David S. Miller9cbc1cb2009-06-15 03:02:23 -070029#include <trace/events/napi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * We maintain a small pool of fully-sized skbs, to make sure the
33 * message gets out even in extreme OOM situations.
34 */
35
36#define MAX_UDP_CHUNK 1460
37#define MAX_SKBS 32
38#define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
39
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080040static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static atomic_t trapped;
43
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070044#define USEC_PER_POLL 50
David S. Millerd9452e92008-03-04 12:28:49 -080045#define NETPOLL_RX_ENABLED 1
46#define NETPOLL_RX_DROP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define MAX_SKB_SIZE \
49 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
50 sizeof(struct iphdr) + sizeof(struct ethhdr))
51
David S. Miller3578b0c2010-08-03 00:24:04 -070052static void zap_completion_queue(void);
Neil Horman068c6e92006-06-26 00:04:27 -070053static void arp_reply(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Anton Vorontsovbff38772009-07-08 11:10:56 -070055static unsigned int carrier_timeout = 4;
56module_param(carrier_timeout, uint, 0644);
57
David Howellsc4028952006-11-22 14:57:56 +000058static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
David Howells4c1ac1b2006-12-05 14:37:56 +000060 struct netpoll_info *npinfo =
61 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010063 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070065 while ((skb = skb_dequeue(&npinfo->txq))) {
66 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080067 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070068 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070070 if (!netif_device_present(dev) || !netif_running(dev)) {
71 __kfree_skb(skb);
72 continue;
73 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
David S. Millerfd2ea0a2008-07-17 01:56:23 -070075 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
76
Ingo Molnar36405432006-12-12 17:20:42 +010077 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070078 __netif_tx_lock(txq, smp_processor_id());
Eric Dumazet5a0d2262010-11-23 10:42:02 +000079 if (netif_tx_queue_frozen_or_stopped(txq) ||
Stephen Hemminger00829822008-11-20 20:14:53 -080080 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070081 skb_queue_head(&npinfo->txq, skb);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070082 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +010083 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jarek Poplawski25442ca2007-07-05 17:42:44 -070085 schedule_delayed_work(&npinfo->tx_work, HZ/10);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070086 return;
87 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -070088 __netif_tx_unlock(txq);
Ingo Molnar36405432006-12-12 17:20:42 +010089 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91}
92
Al Virob51655b2006-11-14 21:40:42 -080093static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
94 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Al Virod6f54932006-11-14 21:26:08 -080096 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -080097
Herbert Xu60476372007-04-09 11:59:39 -070098 if (uh->check == 0 || skb_csum_unnecessary(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100
Herbert Xufb286bb2005-11-10 13:01:24 -0800101 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Patrick McHardy84fa7932006-08-29 16:44:56 -0700103 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -0800104 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -0800105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Herbert Xufb286bb2005-11-10 13:01:24 -0800107 skb->csum = psum;
108
109 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/*
113 * Check whether delayed processing was scheduled for our NIC. If so,
114 * we attempt to grab the poll lock and use ->poll() to pump the card.
115 * If this fails, either we've recursed in ->poll() or it's already
116 * running on another CPU.
117 *
118 * Note: we don't mask interrupts with this lock because we're using
119 * trylock here and interrupts are already disabled in the softirq
120 * case. Further, we test the poll_owner to avoid recursion on UP
121 * systems where the lock doesn't exist.
122 *
123 * In cases where there is bi-directional communications, reading only
124 * one message at a time can lead to packets being dropped by the
125 * network adapter, forcing superfluous retries and possibly timeouts.
126 * Thus, we set our budget to greater than 1.
127 */
David S. Miller0a7606c2007-10-29 21:28:47 -0700128static int poll_one_napi(struct netpoll_info *npinfo,
129 struct napi_struct *napi, int budget)
130{
131 int work;
132
133 /* net_rx_action's ->poll() invocations and our's are
134 * synchronized by this test which is only made while
135 * holding the napi->poll_lock.
136 */
137 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
138 return budget;
139
David S. Millerd9452e92008-03-04 12:28:49 -0800140 npinfo->rx_flags |= NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700141 atomic_inc(&trapped);
Neil Horman7b363e42008-12-09 23:22:26 -0800142 set_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700143
144 work = napi->poll(napi, budget);
David S. Miller7d18f112009-05-21 23:30:09 -0700145 trace_napi_poll(napi);
David S. Miller0a7606c2007-10-29 21:28:47 -0700146
Neil Horman7b363e42008-12-09 23:22:26 -0800147 clear_bit(NAPI_STATE_NPSVC, &napi->state);
David S. Miller0a7606c2007-10-29 21:28:47 -0700148 atomic_dec(&trapped);
David S. Millerd9452e92008-03-04 12:28:49 -0800149 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
David S. Miller0a7606c2007-10-29 21:28:47 -0700150
151 return budget - work;
152}
153
Stephen Hemminger51069302007-11-19 19:18:11 -0800154static void poll_napi(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700156 struct napi_struct *napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 int budget = 16;
158
Neil Hormanf13d4932010-10-19 07:04:26 +0000159 list_for_each_entry(napi, &dev->napi_list, dev_list) {
David S. Miller0a7606c2007-10-29 21:28:47 -0700160 if (napi->poll_owner != smp_processor_id() &&
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700161 spin_trylock(&napi->poll_lock)) {
Stephen Hemminger51069302007-11-19 19:18:11 -0800162 budget = poll_one_napi(dev->npinfo, napi, budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700163 spin_unlock(&napi->poll_lock);
David S. Miller0a7606c2007-10-29 21:28:47 -0700164
165 if (!budget)
166 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169}
170
Neil Horman068c6e92006-06-26 00:04:27 -0700171static void service_arp_queue(struct netpoll_info *npi)
172{
Stephen Hemminger51069302007-11-19 19:18:11 -0800173 if (npi) {
174 struct sk_buff *skb;
Neil Horman068c6e92006-06-26 00:04:27 -0700175
Stephen Hemminger51069302007-11-19 19:18:11 -0800176 while ((skb = skb_dequeue(&npi->arp_tx)))
177 arp_reply(skb);
Neil Horman068c6e92006-06-26 00:04:27 -0700178 }
Neil Horman068c6e92006-06-26 00:04:27 -0700179}
180
WANG Cong0e34e932010-05-06 00:47:21 -0700181void netpoll_poll_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000183 const struct net_device_ops *ops;
Stephen Hemminger51069302007-11-19 19:18:11 -0800184
Pavel Emelyanov5e392732009-05-11 00:36:35 +0000185 if (!dev || !netif_running(dev))
186 return;
187
188 ops = dev->netdev_ops;
189 if (!ops->ndo_poll_controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return;
191
192 /* Process pending work on NIC */
Stephen Hemmingerd3147742008-11-19 21:32:24 -0800193 ops->ndo_poll_controller(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Stephen Hemminger51069302007-11-19 19:18:11 -0800195 poll_napi(dev);
196
197 service_arp_queue(dev->npinfo);
Neil Horman068c6e92006-06-26 00:04:27 -0700198
David S. Miller3578b0c2010-08-03 00:24:04 -0700199 zap_completion_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000201EXPORT_SYMBOL(netpoll_poll_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
WANG Cong0e34e932010-05-06 00:47:21 -0700203void netpoll_poll(struct netpoll *np)
204{
205 netpoll_poll_dev(np->dev);
206}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000207EXPORT_SYMBOL(netpoll_poll);
WANG Cong0e34e932010-05-06 00:47:21 -0700208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static void refill_skbs(void)
210{
211 struct sk_buff *skb;
212 unsigned long flags;
213
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800214 spin_lock_irqsave(&skb_pool.lock, flags);
215 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
217 if (!skb)
218 break;
219
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800220 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800222 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
David S. Miller3578b0c2010-08-03 00:24:04 -0700225static void zap_completion_queue(void)
226{
227 unsigned long flags;
228 struct softnet_data *sd = &get_cpu_var(softnet_data);
229
230 if (sd->completion_queue) {
231 struct sk_buff *clist;
232
233 local_irq_save(flags);
234 clist = sd->completion_queue;
235 sd->completion_queue = NULL;
236 local_irq_restore(flags);
237
238 while (clist != NULL) {
239 struct sk_buff *skb = clist;
240 clist = clist->next;
241 if (skb->destructor) {
242 atomic_inc(&skb->users);
243 dev_kfree_skb_any(skb); /* put this one back */
244 } else {
245 __kfree_skb(skb);
246 }
247 }
248 }
249
250 put_cpu_var(softnet_data);
251}
252
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800253static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800255 int count = 0;
256 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David S. Miller3578b0c2010-08-03 00:24:04 -0700258 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800259 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800263 if (!skb)
264 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800267 if (++count < 10) {
268 netpoll_poll(np);
269 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800271 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 atomic_set(&skb->users, 1);
275 skb_reserve(skb, reserve);
276 return skb;
277}
278
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700279static int netpoll_owner_active(struct net_device *dev)
280{
281 struct napi_struct *napi;
282
283 list_for_each_entry(napi, &dev->napi_list, dev_list) {
284 if (napi->poll_owner == smp_processor_id())
285 return 1;
286 }
287 return 0;
288}
289
Neil Hormanc2355e12010-10-13 16:01:49 +0000290void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
291 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700293 int status = NETDEV_TX_BUSY;
294 unsigned long tries;
Stephen Hemminger00829822008-11-20 20:14:53 -0800295 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000296 /* It is up to the caller to keep npinfo alive. */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900297 struct netpoll_info *npinfo = np->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900299 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
300 __kfree_skb(skb);
301 return;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700304 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700305 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700306 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800307 unsigned long flags;
308
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700309 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
310
Andrew Mortona49f99f2006-12-11 17:24:46 -0800311 local_irq_save(flags);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700312 /* try until next clock tick */
313 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
314 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700315 if (__netif_tx_trylock(txq)) {
Eric Dumazet08baf562009-05-25 22:58:01 -0700316 if (!netif_tx_queue_stopped(txq)) {
WANG Cong0e34e932010-05-06 00:47:21 -0700317 dev->priv_flags |= IFF_IN_NETPOLL;
Stephen Hemminger00829822008-11-20 20:14:53 -0800318 status = ops->ndo_start_xmit(skb, dev);
WANG Cong0e34e932010-05-06 00:47:21 -0700319 dev->priv_flags &= ~IFF_IN_NETPOLL;
Eric Dumazet08baf562009-05-25 22:58:01 -0700320 if (status == NETDEV_TX_OK)
321 txq_trans_update(txq);
322 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700323 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700324
Andrew Mortone37b8d92006-12-09 14:01:49 -0800325 if (status == NETDEV_TX_OK)
326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Andrew Mortone37b8d92006-12-09 14:01:49 -0800328 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700329
330 /* tickle device maybe there is some cleanup */
331 netpoll_poll(np);
332
333 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700334 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000335
336 WARN_ONCE(!irqs_disabled(),
337 "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n",
338 dev->name, ops->ndo_start_xmit);
339
Andrew Mortona49f99f2006-12-11 17:24:46 -0800340 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700343 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700344 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000345 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
Neil Hormanc2355e12010-10-13 16:01:49 +0000348EXPORT_SYMBOL(netpoll_send_skb_on_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
351{
352 int total_len, eth_len, ip_len, udp_len;
353 struct sk_buff *skb;
354 struct udphdr *udph;
355 struct iphdr *iph;
356 struct ethhdr *eth;
357
358 udp_len = len + sizeof(*udph);
359 ip_len = eth_len = udp_len + sizeof(*iph);
360 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
361
362 skb = find_skb(np, total_len, total_len - len);
363 if (!skb)
364 return;
365
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300366 skb_copy_to_linear_data(skb, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 skb->len += len;
368
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300369 skb_push(skb, sizeof(*udph));
370 skb_reset_transport_header(skb);
371 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 udph->source = htons(np->local_port);
373 udph->dest = htons(np->remote_port);
374 udph->len = htons(udp_len);
375 udph->check = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000376 udph->check = csum_tcpudp_magic(np->local_ip,
377 np->remote_ip,
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800378 udp_len, IPPROTO_UDP,
Joe Perches07f07572008-11-19 15:44:53 -0800379 csum_partial(udph, udp_len, 0));
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800380 if (udph->check == 0)
Al Viro5e57dff2006-11-20 18:08:13 -0800381 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700383 skb_push(skb, sizeof(*iph));
384 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700385 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* iph->version = 4; iph->ihl = 5; */
388 put_unaligned(0x45, (unsigned char *)iph);
389 iph->tos = 0;
390 put_unaligned(htons(ip_len), &(iph->tot_len));
391 iph->id = 0;
392 iph->frag_off = 0;
393 iph->ttl = 64;
394 iph->protocol = IPPROTO_UDP;
395 iph->check = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000396 put_unaligned(np->local_ip, &(iph->saddr));
397 put_unaligned(np->remote_ip, &(iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
399
400 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700401 skb_reset_mac_header(skb);
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700402 skb->protocol = eth->h_proto = htons(ETH_P_IP);
Stephen Hemminger09538642007-11-19 19:23:29 -0800403 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
404 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 skb->dev = np->dev;
407
408 netpoll_send_skb(np, skb);
409}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000410EXPORT_SYMBOL(netpoll_send_udp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412static void arp_reply(struct sk_buff *skb)
413{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700414 struct netpoll_info *npinfo = skb->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct arphdr *arp;
416 unsigned char *arp_ptr;
417 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
Al Viro252e3342006-11-14 20:48:11 -0800418 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800419 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000421 struct netpoll *np, *tmp;
422 unsigned long flags;
423 int hits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000425 if (list_empty(&npinfo->rx_np))
426 return;
427
428 /* Before checking the packet, we do some early
429 inspection whether this is interesting at all */
430 spin_lock_irqsave(&npinfo->rx_lock, flags);
431 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
432 if (np->dev == skb->dev)
433 hits++;
434 }
435 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
436
437 /* No netpoll struct is using this dev */
438 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700439 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* No arp on this interface */
442 if (skb->dev->flags & IFF_NOARP)
443 return;
444
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800445 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return;
447
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700448 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300449 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300450 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
453 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
454 arp->ar_pro != htons(ETH_P_IP) ||
455 arp->ar_op != htons(ARPOP_REQUEST))
456 return;
457
Neil Horman47bbec02006-12-08 00:05:55 -0800458 arp_ptr = (unsigned char *)(arp+1);
459 /* save the location of the src hw addr */
460 sha = arp_ptr;
461 arp_ptr += skb->dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 memcpy(&sip, arp_ptr, 4);
Neil Horman47bbec02006-12-08 00:05:55 -0800463 arp_ptr += 4;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000464 /* If we actually cared about dst hw addr,
465 it would get copied here */
Neil Horman47bbec02006-12-08 00:05:55 -0800466 arp_ptr += skb->dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 memcpy(&tip, arp_ptr, 4);
468
469 /* Should we ignore arp? */
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000470 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return;
472
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800473 size = arp_hdr_len(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000475 spin_lock_irqsave(&npinfo->rx_lock, flags);
476 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
477 if (tip != np->local_ip)
478 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000480 send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
481 LL_RESERVED_SPACE(np->dev));
482 if (!send_skb)
483 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000485 skb_reset_network_header(send_skb);
486 arp = (struct arphdr *) skb_put(send_skb, size);
487 send_skb->dev = skb->dev;
488 send_skb->protocol = htons(ETH_P_ARP);
489
490 /* Fill the device header for the ARP frame */
491 if (dev_hard_header(send_skb, skb->dev, ptype,
492 sha, np->dev->dev_addr,
493 send_skb->len) < 0) {
494 kfree_skb(send_skb);
495 continue;
496 }
497
498 /*
499 * Fill out the arp protocol part.
500 *
501 * we only support ethernet device type,
502 * which (according to RFC 1390) should
503 * always equal 1 (Ethernet).
504 */
505
506 arp->ar_hrd = htons(np->dev->type);
507 arp->ar_pro = htons(ETH_P_IP);
508 arp->ar_hln = np->dev->addr_len;
509 arp->ar_pln = 4;
510 arp->ar_op = htons(type);
511
512 arp_ptr = (unsigned char *)(arp + 1);
513 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
514 arp_ptr += np->dev->addr_len;
515 memcpy(arp_ptr, &tip, 4);
516 arp_ptr += 4;
517 memcpy(arp_ptr, sha, np->dev->addr_len);
518 arp_ptr += np->dev->addr_len;
519 memcpy(arp_ptr, &sip, 4);
520
521 netpoll_send_skb(np, send_skb);
522
523 /* If there are several rx_hooks for the same address,
524 we're fine by sending a single reply */
525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000527 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530int __netpoll_rx(struct sk_buff *skb)
531{
532 int proto, len, ulen;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000533 int hits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct iphdr *iph;
535 struct udphdr *uh;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000536 struct netpoll_info *npinfo = skb->dev->npinfo;
537 struct netpoll *np, *tmp;
Neil Horman068c6e92006-06-26 00:04:27 -0700538
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000539 if (list_empty(&npinfo->rx_np))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (skb->dev->type != ARPHRD_ETHER)
543 goto out;
544
David S. Millerd9452e92008-03-04 12:28:49 -0800545 /* check if netpoll clients need ARP */
YOSHIFUJI Hideaki724800d2007-03-25 20:13:04 -0700546 if (skb->protocol == htons(ETH_P_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 atomic_read(&trapped)) {
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000548 skb_queue_tail(&npinfo->arp_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 1;
550 }
551
552 proto = ntohs(eth_hdr(skb)->h_proto);
553 if (proto != ETH_P_IP)
554 goto out;
555 if (skb->pkt_type == PACKET_OTHERHOST)
556 goto out;
557 if (skb_shared(skb))
558 goto out;
559
560 iph = (struct iphdr *)skb->data;
561 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
562 goto out;
563 if (iph->ihl < 5 || iph->version != 4)
564 goto out;
565 if (!pskb_may_pull(skb, iph->ihl*4))
566 goto out;
567 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
568 goto out;
569
570 len = ntohs(iph->tot_len);
571 if (skb->len < len || len < iph->ihl*4)
572 goto out;
573
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700574 /*
575 * Our transport medium may have padded the buffer out.
576 * Now We trim to the true length of the frame.
577 */
578 if (pskb_trim_rcsum(skb, len))
579 goto out;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (iph->protocol != IPPROTO_UDP)
582 goto out;
583
584 len -= iph->ihl*4;
585 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
586 ulen = ntohs(uh->len);
587
588 if (ulen != len)
589 goto out;
Herbert Xufb286bb2005-11-10 13:01:24 -0800590 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000593 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
594 if (np->local_ip && np->local_ip != iph->daddr)
595 continue;
596 if (np->remote_ip && np->remote_ip != iph->saddr)
597 continue;
598 if (np->local_port && np->local_port != ntohs(uh->dest))
599 continue;
600
601 np->rx_hook(np, ntohs(uh->source),
602 (char *)(uh+1),
603 ulen - sizeof(struct udphdr));
604 hits++;
605 }
606
607 if (!hits)
608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 kfree_skb(skb);
611 return 1;
612
613out:
614 if (atomic_read(&trapped)) {
615 kfree_skb(skb);
616 return 1;
617 }
618
619 return 0;
620}
621
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700622void netpoll_print_options(struct netpoll *np)
623{
624 printk(KERN_INFO "%s: local port %d\n",
625 np->name, np->local_port);
Harvey Harrisone7557af2009-03-28 15:38:31 +0000626 printk(KERN_INFO "%s: local IP %pI4\n",
627 np->name, &np->local_ip);
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000628 printk(KERN_INFO "%s: interface '%s'\n",
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700629 np->name, np->dev_name);
630 printk(KERN_INFO "%s: remote port %d\n",
631 np->name, np->remote_port);
Harvey Harrisone7557af2009-03-28 15:38:31 +0000632 printk(KERN_INFO "%s: remote IP %pI4\n",
633 np->name, &np->remote_ip);
Johannes Berge1749612008-10-27 15:59:26 -0700634 printk(KERN_INFO "%s: remote ethernet address %pM\n",
635 np->name, np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700636}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000637EXPORT_SYMBOL(netpoll_print_options);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639int netpoll_parse_options(struct netpoll *np, char *opt)
640{
641 char *cur=opt, *delim;
642
David S. Millerc68b9072006-11-14 20:40:49 -0800643 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if ((delim = strchr(cur, '@')) == NULL)
645 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800646 *delim = 0;
647 np->local_port = simple_strtol(cur, NULL, 10);
648 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
David S. Millerc68b9072006-11-14 20:40:49 -0800652 if (*cur != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if ((delim = strchr(cur, '/')) == NULL)
654 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800655 *delim = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000656 np->local_ip = in_aton(cur);
David S. Millerc68b9072006-11-14 20:40:49 -0800657 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 cur++;
660
David S. Millerc68b9072006-11-14 20:40:49 -0800661 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /* parse out dev name */
663 if ((delim = strchr(cur, ',')) == NULL)
664 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800665 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800667 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 cur++;
670
David S. Millerc68b9072006-11-14 20:40:49 -0800671 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* dst port */
673 if ((delim = strchr(cur, '@')) == NULL)
674 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800675 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000676 if (*cur == ' ' || *cur == '\t')
677 printk(KERN_INFO "%s: warning: whitespace"
678 "is not allowed\n", np->name);
David S. Millerc68b9072006-11-14 20:40:49 -0800679 np->remote_port = simple_strtol(cur, NULL, 10);
680 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /* dst ip */
685 if ((delim = strchr(cur, '/')) == NULL)
686 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800687 *delim = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000688 np->remote_ip = in_aton(cur);
David S. Millerc68b9072006-11-14 20:40:49 -0800689 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
David S. Millerc68b9072006-11-14 20:40:49 -0800691 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* MAC address */
693 if ((delim = strchr(cur, ':')) == NULL)
694 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800695 *delim = 0;
696 np->remote_mac[0] = simple_strtol(cur, NULL, 16);
697 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if ((delim = strchr(cur, ':')) == NULL)
699 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800700 *delim = 0;
701 np->remote_mac[1] = simple_strtol(cur, NULL, 16);
702 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if ((delim = strchr(cur, ':')) == NULL)
704 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800705 *delim = 0;
706 np->remote_mac[2] = simple_strtol(cur, NULL, 16);
707 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if ((delim = strchr(cur, ':')) == NULL)
709 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800710 *delim = 0;
711 np->remote_mac[3] = simple_strtol(cur, NULL, 16);
712 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if ((delim = strchr(cur, ':')) == NULL)
714 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800715 *delim = 0;
716 np->remote_mac[4] = simple_strtol(cur, NULL, 16);
717 cur = delim + 1;
718 np->remote_mac[5] = simple_strtol(cur, NULL, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700721 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 return 0;
724
725 parse_failed:
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000726 printk(KERN_INFO "%s: couldn't parse config at '%s'!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 np->name, cur);
728 return -1;
729}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000730EXPORT_SYMBOL(netpoll_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000732int __netpoll_setup(struct netpoll *np)
733{
734 struct net_device *ndev = np->dev;
735 struct netpoll_info *npinfo;
736 const struct net_device_ops *ops;
737 unsigned long flags;
738 int err;
739
740 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
741 !ndev->netdev_ops->ndo_poll_controller) {
742 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
743 np->name, np->dev_name);
744 err = -ENOTSUPP;
745 goto out;
746 }
747
748 if (!ndev->npinfo) {
749 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
750 if (!npinfo) {
751 err = -ENOMEM;
752 goto out;
753 }
754
755 npinfo->rx_flags = 0;
756 INIT_LIST_HEAD(&npinfo->rx_np);
757
758 spin_lock_init(&npinfo->rx_lock);
759 skb_queue_head_init(&npinfo->arp_tx);
760 skb_queue_head_init(&npinfo->txq);
761 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
762
763 atomic_set(&npinfo->refcnt, 1);
764
765 ops = np->dev->netdev_ops;
766 if (ops->ndo_netpoll_setup) {
767 err = ops->ndo_netpoll_setup(ndev, npinfo);
768 if (err)
769 goto free_npinfo;
770 }
771 } else {
772 npinfo = ndev->npinfo;
773 atomic_inc(&npinfo->refcnt);
774 }
775
776 npinfo->netpoll = np;
777
778 if (np->rx_hook) {
779 spin_lock_irqsave(&npinfo->rx_lock, flags);
780 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
781 list_add_tail(&np->rx, &npinfo->rx_np);
782 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
783 }
784
785 /* last thing to do is link it to the net device structure */
786 rcu_assign_pointer(ndev->npinfo, npinfo);
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000787
788 return 0;
789
790free_npinfo:
791 kfree(npinfo);
792out:
793 return err;
794}
795EXPORT_SYMBOL_GPL(__netpoll_setup);
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797int netpoll_setup(struct netpoll *np)
798{
799 struct net_device *ndev = NULL;
800 struct in_device *in_dev;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700801 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if (np->dev_name)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700804 ndev = dev_get_by_name(&init_net, np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!ndev) {
806 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
807 np->name, np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700808 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!netif_running(ndev)) {
812 unsigned long atmost, atleast;
813
814 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
815 np->name, np->dev_name);
816
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800817 rtnl_lock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700818 err = dev_open(ndev);
819 rtnl_unlock();
820
821 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 printk(KERN_ERR "%s: failed to open %s\n",
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700823 np->name, ndev->name);
Herbert Xudbaa1542010-06-10 16:12:46 +0000824 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -0700828 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 while (!netif_carrier_ok(ndev)) {
830 if (time_after(jiffies, atmost)) {
831 printk(KERN_NOTICE
832 "%s: timeout waiting for carrier\n",
833 np->name);
834 break;
835 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -0700836 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
839 /* If carrier appears to come up instantly, we don't
840 * trust it and pause so that we don't pump all our
841 * queued console messages into the bitbucket.
842 */
843
844 if (time_before(jiffies, atleast)) {
845 printk(KERN_NOTICE "%s: carrier detect appears"
846 " untrustworthy, waiting 4 seconds\n",
847 np->name);
848 msleep(4000);
849 }
850 }
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!np->local_ip) {
853 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700854 in_dev = __in_dev_get_rcu(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 if (!in_dev || !in_dev->ifa_list) {
857 rcu_read_unlock();
858 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
859 np->name, np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700860 err = -EDESTADDRREQ;
Herbert Xudbaa1542010-06-10 16:12:46 +0000861 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
Harvey Harrisone7557af2009-03-28 15:38:31 +0000864 np->local_ip = in_dev->ifa_list->ifa_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 rcu_read_unlock();
Harvey Harrisone7557af2009-03-28 15:38:31 +0000866 printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
Herbert Xudbaa1542010-06-10 16:12:46 +0000869 np->dev = ndev;
870
871 /* fill up the skb queue */
872 refill_skbs();
873
874 rtnl_lock();
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000875 err = __netpoll_setup(np);
Herbert Xudbaa1542010-06-10 16:12:46 +0000876 rtnl_unlock();
Matt Mackall53fb95d2005-08-11 19:27:43 -0700877
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000878 if (err)
879 goto put;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 0;
882
Jiri Slaby21edbb22010-03-16 05:29:54 +0000883put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 dev_put(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000887EXPORT_SYMBOL(netpoll_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
David S. Millerc68b9072006-11-14 20:40:49 -0800889static int __init netpoll_init(void)
890{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800891 skb_queue_head_init(&skb_pool);
892 return 0;
893}
894core_initcall(netpoll_init);
895
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000896void __netpoll_cleanup(struct netpoll *np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700898 struct netpoll_info *npinfo;
899 unsigned long flags;
900
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000901 npinfo = np->dev->npinfo;
902 if (!npinfo)
Herbert Xudbaa1542010-06-10 16:12:46 +0000903 return;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700904
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000905 if (!list_empty(&npinfo->rx_np)) {
906 spin_lock_irqsave(&npinfo->rx_lock, flags);
907 list_del(&np->rx);
908 if (list_empty(&npinfo->rx_np))
909 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
910 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700911 }
Herbert Xudbaa1542010-06-10 16:12:46 +0000912
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000913 if (atomic_dec_and_test(&npinfo->refcnt)) {
914 const struct net_device_ops *ops;
915
916 ops = np->dev->netdev_ops;
917 if (ops->ndo_netpoll_cleanup)
918 ops->ndo_netpoll_cleanup(np->dev);
919
920 rcu_assign_pointer(np->dev->npinfo, NULL);
921
Herbert Xudbaa1542010-06-10 16:12:46 +0000922 /* avoid racing with NAPI reading npinfo */
923 synchronize_rcu_bh();
924
925 skb_queue_purge(&npinfo->arp_tx);
926 skb_queue_purge(&npinfo->txq);
927 cancel_rearming_delayed_work(&npinfo->tx_work);
928
929 /* clean after last, unfinished work */
930 __skb_queue_purge(&npinfo->txq);
931 kfree(npinfo);
932 }
Herbert Xu8fdd95e2010-06-10 16:12:48 +0000933}
934EXPORT_SYMBOL_GPL(__netpoll_cleanup);
935
936void netpoll_cleanup(struct netpoll *np)
937{
938 if (!np->dev)
939 return;
940
941 rtnl_lock();
942 __netpoll_cleanup(np);
943 rtnl_unlock();
Herbert Xudbaa1542010-06-10 16:12:46 +0000944
945 dev_put(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 np->dev = NULL;
947}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000948EXPORT_SYMBOL(netpoll_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950int netpoll_trap(void)
951{
952 return atomic_read(&trapped);
953}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000954EXPORT_SYMBOL(netpoll_trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956void netpoll_set_trap(int trap)
957{
958 if (trap)
959 atomic_inc(&trapped);
960 else
961 atomic_dec(&trapped);
962}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963EXPORT_SYMBOL(netpoll_set_trap);