blob: e9ab4f0c454c4bc1e626214549f4e1cac7ea7c9e [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
Neil Horman068c6e92006-06-26 00:04:27 -070052static void arp_reply(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Anton Vorontsovbff38772009-07-08 11:10:56 -070054static unsigned int carrier_timeout = 4;
55module_param(carrier_timeout, uint, 0644);
56
David Howellsc4028952006-11-22 14:57:56 +000057static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
David Howells4c1ac1b2006-12-05 14:37:56 +000059 struct netpoll_info *npinfo =
60 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct sk_buff *skb;
Ingo Molnar36405432006-12-12 17:20:42 +010062 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070064 while ((skb = skb_dequeue(&npinfo->txq))) {
65 struct net_device *dev = skb->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -080066 const struct net_device_ops *ops = dev->netdev_ops;
David S. Millerfd2ea0a2008-07-17 01:56:23 -070067 struct netdev_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070069 if (!netif_device_present(dev) || !netif_running(dev)) {
70 __kfree_skb(skb);
71 continue;
72 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
David S. Millerfd2ea0a2008-07-17 01:56:23 -070074 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
75
Ingo Molnar36405432006-12-12 17:20:42 +010076 local_irq_save(flags);
David S. Millerfd2ea0a2008-07-17 01:56:23 -070077 __netif_tx_lock(txq, smp_processor_id());
78 if (netif_tx_queue_stopped(txq) ||
David S. Millerc3f26a22008-07-31 16:58:50 -070079 netif_tx_queue_frozen(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 Virod6f5493c2006-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
Stephen Hemminger51069302007-11-19 19:18:11 -0800159 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
WANG Cong0e34e932010-05-06 00:47:21 -0700201void netpoll_poll(struct netpoll *np)
202{
203 netpoll_poll_dev(np->dev);
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static void refill_skbs(void)
207{
208 struct sk_buff *skb;
209 unsigned long flags;
210
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800211 spin_lock_irqsave(&skb_pool.lock, flags);
212 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
214 if (!skb)
215 break;
216
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800217 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800219 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800222static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800224 int count = 0;
225 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800227 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800231 if (!skb)
232 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800235 if (++count < 10) {
236 netpoll_poll(np);
237 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800239 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
242 atomic_set(&skb->users, 1);
243 skb_reserve(skb, reserve);
244 return skb;
245}
246
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700247static int netpoll_owner_active(struct net_device *dev)
248{
249 struct napi_struct *napi;
250
251 list_for_each_entry(napi, &dev->napi_list, dev_list) {
252 if (napi->poll_owner == smp_processor_id())
253 return 1;
254 }
255 return 0;
256}
257
WANG Cong0e34e932010-05-06 00:47:21 -0700258void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700260 int status = NETDEV_TX_BUSY;
261 unsigned long tries;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900262 struct net_device *dev = np->dev;
Stephen Hemminger00829822008-11-20 20:14:53 -0800263 const struct net_device_ops *ops = dev->netdev_ops;
Herbert Xude85d992010-06-10 16:12:44 +0000264 /* It is up to the caller to keep npinfo alive. */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900265 struct netpoll_info *npinfo = np->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900267 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
268 __kfree_skb(skb);
269 return;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700272 /* don't get messages out of order, and no recursion */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700273 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700274 struct netdev_queue *txq;
Andrew Mortona49f99f2006-12-11 17:24:46 -0800275 unsigned long flags;
276
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700277 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
278
Andrew Mortona49f99f2006-12-11 17:24:46 -0800279 local_irq_save(flags);
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700280 /* try until next clock tick */
281 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
282 tries > 0; --tries) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700283 if (__netif_tx_trylock(txq)) {
Eric Dumazet08baf562009-05-25 22:58:01 -0700284 if (!netif_tx_queue_stopped(txq)) {
WANG Cong0e34e932010-05-06 00:47:21 -0700285 dev->priv_flags |= IFF_IN_NETPOLL;
Stephen Hemminger00829822008-11-20 20:14:53 -0800286 status = ops->ndo_start_xmit(skb, dev);
WANG Cong0e34e932010-05-06 00:47:21 -0700287 dev->priv_flags &= ~IFF_IN_NETPOLL;
Eric Dumazet08baf562009-05-25 22:58:01 -0700288 if (status == NETDEV_TX_OK)
289 txq_trans_update(txq);
290 }
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700291 __netif_tx_unlock(txq);
Matt Mackallf0d34592005-08-11 19:25:11 -0700292
Andrew Mortone37b8d92006-12-09 14:01:49 -0800293 if (status == NETDEV_TX_OK)
294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Andrew Mortone37b8d92006-12-09 14:01:49 -0800296 }
Stephen Hemminger0db3dc72007-06-27 00:39:42 -0700297
298 /* tickle device maybe there is some cleanup */
299 netpoll_poll(np);
300
301 udelay(USEC_PER_POLL);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700302 }
Dongdong Deng79b1bee2009-08-21 03:33:36 +0000303
304 WARN_ONCE(!irqs_disabled(),
305 "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n",
306 dev->name, ops->ndo_start_xmit);
307
Andrew Mortona49f99f2006-12-11 17:24:46 -0800308 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700311 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700312 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000313 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
318{
319 int total_len, eth_len, ip_len, udp_len;
320 struct sk_buff *skb;
321 struct udphdr *udph;
322 struct iphdr *iph;
323 struct ethhdr *eth;
324
325 udp_len = len + sizeof(*udph);
326 ip_len = eth_len = udp_len + sizeof(*iph);
327 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
328
329 skb = find_skb(np, total_len, total_len - len);
330 if (!skb)
331 return;
332
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300333 skb_copy_to_linear_data(skb, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 skb->len += len;
335
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300336 skb_push(skb, sizeof(*udph));
337 skb_reset_transport_header(skb);
338 udph = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 udph->source = htons(np->local_port);
340 udph->dest = htons(np->remote_port);
341 udph->len = htons(udp_len);
342 udph->check = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000343 udph->check = csum_tcpudp_magic(np->local_ip,
344 np->remote_ip,
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800345 udp_len, IPPROTO_UDP,
Joe Perches07f07572008-11-19 15:44:53 -0800346 csum_partial(udph, udp_len, 0));
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800347 if (udph->check == 0)
Al Viro5e57dff2006-11-20 18:08:13 -0800348 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700350 skb_push(skb, sizeof(*iph));
351 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700352 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* iph->version = 4; iph->ihl = 5; */
355 put_unaligned(0x45, (unsigned char *)iph);
356 iph->tos = 0;
357 put_unaligned(htons(ip_len), &(iph->tot_len));
358 iph->id = 0;
359 iph->frag_off = 0;
360 iph->ttl = 64;
361 iph->protocol = IPPROTO_UDP;
362 iph->check = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000363 put_unaligned(np->local_ip, &(iph->saddr));
364 put_unaligned(np->remote_ip, &(iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
366
367 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700368 skb_reset_mac_header(skb);
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700369 skb->protocol = eth->h_proto = htons(ETH_P_IP);
Stephen Hemminger09538642007-11-19 19:23:29 -0800370 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
371 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 skb->dev = np->dev;
374
375 netpoll_send_skb(np, skb);
376}
377
378static void arp_reply(struct sk_buff *skb)
379{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700380 struct netpoll_info *npinfo = skb->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct arphdr *arp;
382 unsigned char *arp_ptr;
383 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
Al Viro252e3342006-11-14 20:48:11 -0800384 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800385 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct sk_buff *send_skb;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000387 struct netpoll *np, *tmp;
388 unsigned long flags;
389 int hits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000391 if (list_empty(&npinfo->rx_np))
392 return;
393
394 /* Before checking the packet, we do some early
395 inspection whether this is interesting at all */
396 spin_lock_irqsave(&npinfo->rx_lock, flags);
397 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
398 if (np->dev == skb->dev)
399 hits++;
400 }
401 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
402
403 /* No netpoll struct is using this dev */
404 if (!hits)
Jeff Moyer115c1d62005-06-22 22:05:31 -0700405 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* No arp on this interface */
408 if (skb->dev->flags & IFF_NOARP)
409 return;
410
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800411 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return;
413
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700414 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300415 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melod0a92be2007-03-12 20:56:31 -0300416 arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
419 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
420 arp->ar_pro != htons(ETH_P_IP) ||
421 arp->ar_op != htons(ARPOP_REQUEST))
422 return;
423
Neil Horman47bbec02006-12-08 00:05:55 -0800424 arp_ptr = (unsigned char *)(arp+1);
425 /* save the location of the src hw addr */
426 sha = arp_ptr;
427 arp_ptr += skb->dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 memcpy(&sip, arp_ptr, 4);
Neil Horman47bbec02006-12-08 00:05:55 -0800429 arp_ptr += 4;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000430 /* If we actually cared about dst hw addr,
431 it would get copied here */
Neil Horman47bbec02006-12-08 00:05:55 -0800432 arp_ptr += skb->dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 memcpy(&tip, arp_ptr, 4);
434
435 /* Should we ignore arp? */
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000436 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
438
Pavel Emelyanov988b7052008-03-03 12:20:57 -0800439 size = arp_hdr_len(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000441 spin_lock_irqsave(&npinfo->rx_lock, flags);
442 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
443 if (tip != np->local_ip)
444 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000446 send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
447 LL_RESERVED_SPACE(np->dev));
448 if (!send_skb)
449 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000451 skb_reset_network_header(send_skb);
452 arp = (struct arphdr *) skb_put(send_skb, size);
453 send_skb->dev = skb->dev;
454 send_skb->protocol = htons(ETH_P_ARP);
455
456 /* Fill the device header for the ARP frame */
457 if (dev_hard_header(send_skb, skb->dev, ptype,
458 sha, np->dev->dev_addr,
459 send_skb->len) < 0) {
460 kfree_skb(send_skb);
461 continue;
462 }
463
464 /*
465 * Fill out the arp protocol part.
466 *
467 * we only support ethernet device type,
468 * which (according to RFC 1390) should
469 * always equal 1 (Ethernet).
470 */
471
472 arp->ar_hrd = htons(np->dev->type);
473 arp->ar_pro = htons(ETH_P_IP);
474 arp->ar_hln = np->dev->addr_len;
475 arp->ar_pln = 4;
476 arp->ar_op = htons(type);
477
478 arp_ptr = (unsigned char *)(arp + 1);
479 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
480 arp_ptr += np->dev->addr_len;
481 memcpy(arp_ptr, &tip, 4);
482 arp_ptr += 4;
483 memcpy(arp_ptr, sha, np->dev->addr_len);
484 arp_ptr += np->dev->addr_len;
485 memcpy(arp_ptr, &sip, 4);
486
487 netpoll_send_skb(np, send_skb);
488
489 /* If there are several rx_hooks for the same address,
490 we're fine by sending a single reply */
491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000493 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496int __netpoll_rx(struct sk_buff *skb)
497{
498 int proto, len, ulen;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000499 int hits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct iphdr *iph;
501 struct udphdr *uh;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000502 struct netpoll_info *npinfo = skb->dev->npinfo;
503 struct netpoll *np, *tmp;
Neil Horman068c6e92006-06-26 00:04:27 -0700504
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000505 if (list_empty(&npinfo->rx_np))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto out;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (skb->dev->type != ARPHRD_ETHER)
509 goto out;
510
David S. Millerd9452e92008-03-04 12:28:49 -0800511 /* check if netpoll clients need ARP */
YOSHIFUJI Hideaki724800d2007-03-25 20:13:04 -0700512 if (skb->protocol == htons(ETH_P_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 atomic_read(&trapped)) {
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000514 skb_queue_tail(&npinfo->arp_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 1;
516 }
517
518 proto = ntohs(eth_hdr(skb)->h_proto);
519 if (proto != ETH_P_IP)
520 goto out;
521 if (skb->pkt_type == PACKET_OTHERHOST)
522 goto out;
523 if (skb_shared(skb))
524 goto out;
525
526 iph = (struct iphdr *)skb->data;
527 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
528 goto out;
529 if (iph->ihl < 5 || iph->version != 4)
530 goto out;
531 if (!pskb_may_pull(skb, iph->ihl*4))
532 goto out;
533 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
534 goto out;
535
536 len = ntohs(iph->tot_len);
537 if (skb->len < len || len < iph->ihl*4)
538 goto out;
539
Aubrey.Li5e7d7fa2007-04-17 12:40:20 -0700540 /*
541 * Our transport medium may have padded the buffer out.
542 * Now We trim to the true length of the frame.
543 */
544 if (pskb_trim_rcsum(skb, len))
545 goto out;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (iph->protocol != IPPROTO_UDP)
548 goto out;
549
550 len -= iph->ihl*4;
551 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
552 ulen = ntohs(uh->len);
553
554 if (ulen != len)
555 goto out;
Herbert Xufb286bb2005-11-10 13:01:24 -0800556 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000559 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
560 if (np->local_ip && np->local_ip != iph->daddr)
561 continue;
562 if (np->remote_ip && np->remote_ip != iph->saddr)
563 continue;
564 if (np->local_port && np->local_port != ntohs(uh->dest))
565 continue;
566
567 np->rx_hook(np, ntohs(uh->source),
568 (char *)(uh+1),
569 ulen - sizeof(struct udphdr));
570 hits++;
571 }
572
573 if (!hits)
574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 kfree_skb(skb);
577 return 1;
578
579out:
580 if (atomic_read(&trapped)) {
581 kfree_skb(skb);
582 return 1;
583 }
584
585 return 0;
586}
587
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700588void netpoll_print_options(struct netpoll *np)
589{
590 printk(KERN_INFO "%s: local port %d\n",
591 np->name, np->local_port);
Harvey Harrisone7557af2009-03-28 15:38:31 +0000592 printk(KERN_INFO "%s: local IP %pI4\n",
593 np->name, &np->local_ip);
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000594 printk(KERN_INFO "%s: interface '%s'\n",
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700595 np->name, np->dev_name);
596 printk(KERN_INFO "%s: remote port %d\n",
597 np->name, np->remote_port);
Harvey Harrisone7557af2009-03-28 15:38:31 +0000598 printk(KERN_INFO "%s: remote IP %pI4\n",
599 np->name, &np->remote_ip);
Johannes Berge1749612008-10-27 15:59:26 -0700600 printk(KERN_INFO "%s: remote ethernet address %pM\n",
601 np->name, np->remote_mac);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604int netpoll_parse_options(struct netpoll *np, char *opt)
605{
606 char *cur=opt, *delim;
607
David S. Millerc68b9072006-11-14 20:40:49 -0800608 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if ((delim = strchr(cur, '@')) == NULL)
610 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800611 *delim = 0;
612 np->local_port = simple_strtol(cur, NULL, 10);
613 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
David S. Millerc68b9072006-11-14 20:40:49 -0800617 if (*cur != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if ((delim = strchr(cur, '/')) == NULL)
619 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800620 *delim = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000621 np->local_ip = in_aton(cur);
David S. Millerc68b9072006-11-14 20:40:49 -0800622 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 cur++;
625
David S. Millerc68b9072006-11-14 20:40:49 -0800626 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* parse out dev name */
628 if ((delim = strchr(cur, ',')) == NULL)
629 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800630 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800632 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 cur++;
635
David S. Millerc68b9072006-11-14 20:40:49 -0800636 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* dst port */
638 if ((delim = strchr(cur, '@')) == NULL)
639 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800640 *delim = 0;
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000641 if (*cur == ' ' || *cur == '\t')
642 printk(KERN_INFO "%s: warning: whitespace"
643 "is not allowed\n", np->name);
David S. Millerc68b9072006-11-14 20:40:49 -0800644 np->remote_port = simple_strtol(cur, NULL, 10);
645 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 cur++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /* dst ip */
650 if ((delim = strchr(cur, '/')) == NULL)
651 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800652 *delim = 0;
Harvey Harrisone7557af2009-03-28 15:38:31 +0000653 np->remote_ip = in_aton(cur);
David S. Millerc68b9072006-11-14 20:40:49 -0800654 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
David S. Millerc68b9072006-11-14 20:40:49 -0800656 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* MAC address */
658 if ((delim = strchr(cur, ':')) == NULL)
659 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800660 *delim = 0;
661 np->remote_mac[0] = simple_strtol(cur, NULL, 16);
662 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if ((delim = strchr(cur, ':')) == NULL)
664 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800665 *delim = 0;
666 np->remote_mac[1] = simple_strtol(cur, NULL, 16);
667 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if ((delim = strchr(cur, ':')) == NULL)
669 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800670 *delim = 0;
671 np->remote_mac[2] = simple_strtol(cur, NULL, 16);
672 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if ((delim = strchr(cur, ':')) == NULL)
674 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800675 *delim = 0;
676 np->remote_mac[3] = simple_strtol(cur, NULL, 16);
677 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if ((delim = strchr(cur, ':')) == NULL)
679 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800680 *delim = 0;
681 np->remote_mac[4] = simple_strtol(cur, NULL, 16);
682 cur = delim + 1;
683 np->remote_mac[5] = simple_strtol(cur, NULL, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700686 netpoll_print_options(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 return 0;
689
690 parse_failed:
Amerigo Wang5fc05f82010-03-21 22:59:58 +0000691 printk(KERN_INFO "%s: couldn't parse config at '%s'!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 np->name, cur);
693 return -1;
694}
695
696int netpoll_setup(struct netpoll *np)
697{
698 struct net_device *ndev = NULL;
699 struct in_device *in_dev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700700 struct netpoll_info *npinfo;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000701 struct netpoll *npe, *tmp;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700702 unsigned long flags;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700703 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (np->dev_name)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700706 ndev = dev_get_by_name(&init_net, np->dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!ndev) {
708 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
709 np->name, np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700710 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
713 np->dev = ndev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700714 if (!ndev->npinfo) {
715 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700716 if (!npinfo) {
717 err = -ENOMEM;
Jiri Slaby21edbb22010-03-16 05:29:54 +0000718 goto put;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700719 }
Jeff Moyer115c1d62005-06-22 22:05:31 -0700720
David S. Millerd9452e92008-03-04 12:28:49 -0800721 npinfo->rx_flags = 0;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000722 INIT_LIST_HEAD(&npinfo->rx_np);
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700723
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700724 spin_lock_init(&npinfo->rx_lock);
Neil Horman068c6e92006-06-26 00:04:27 -0700725 skb_queue_head_init(&npinfo->arp_tx);
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -0700726 skb_queue_head_init(&npinfo->txq);
David Howells4c1ac1b2006-12-05 14:37:56 +0000727 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -0700728
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700729 atomic_set(&npinfo->refcnt, 1);
730 } else {
Jeff Moyer115c1d62005-06-22 22:05:31 -0700731 npinfo = ndev->npinfo;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700732 atomic_inc(&npinfo->refcnt);
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
WANG Cong0e34e932010-05-06 00:47:21 -0700735 npinfo->netpoll = np;
736
737 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
738 !ndev->netdev_ops->ndo_poll_controller) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
740 np->name, np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700741 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 goto release;
743 }
744
745 if (!netif_running(ndev)) {
746 unsigned long atmost, atleast;
747
748 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
749 np->name, np->dev_name);
750
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800751 rtnl_lock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700752 err = dev_open(ndev);
753 rtnl_unlock();
754
755 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 printk(KERN_ERR "%s: failed to open %s\n",
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700757 np->name, ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 goto release;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 atleast = jiffies + HZ/10;
Anton Vorontsovbff38772009-07-08 11:10:56 -0700762 atmost = jiffies + carrier_timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 while (!netif_carrier_ok(ndev)) {
764 if (time_after(jiffies, atmost)) {
765 printk(KERN_NOTICE
766 "%s: timeout waiting for carrier\n",
767 np->name);
768 break;
769 }
Anton Vorontsov1b614fb2009-07-08 20:09:44 -0700770 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773 /* If carrier appears to come up instantly, we don't
774 * trust it and pause so that we don't pump all our
775 * queued console messages into the bitbucket.
776 */
777
778 if (time_before(jiffies, atleast)) {
779 printk(KERN_NOTICE "%s: carrier detect appears"
780 " untrustworthy, waiting 4 seconds\n",
781 np->name);
782 msleep(4000);
783 }
784 }
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (!np->local_ip) {
787 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700788 in_dev = __in_dev_get_rcu(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (!in_dev || !in_dev->ifa_list) {
791 rcu_read_unlock();
792 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
793 np->name, np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700794 err = -EDESTADDRREQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto release;
796 }
797
Harvey Harrisone7557af2009-03-28 15:38:31 +0000798 np->local_ip = in_dev->ifa_list->ifa_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 rcu_read_unlock();
Harvey Harrisone7557af2009-03-28 15:38:31 +0000800 printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700803 if (np->rx_hook) {
804 spin_lock_irqsave(&npinfo->rx_lock, flags);
David S. Millerd9452e92008-03-04 12:28:49 -0800805 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000806 list_add_tail(&np->rx, &npinfo->rx_np);
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700807 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
808 }
Ingo Molnar26520762005-08-11 19:26:42 -0700809
810 /* fill up the skb queue */
811 refill_skbs();
812
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700813 /* last thing to do is link it to the net device structure */
Herbert Xude85d992010-06-10 16:12:44 +0000814 rcu_assign_pointer(ndev->npinfo, npinfo);
Matt Mackall53fb95d2005-08-11 19:27:43 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 0;
817
818 release:
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000819 if (!ndev->npinfo) {
820 spin_lock_irqsave(&npinfo->rx_lock, flags);
821 list_for_each_entry_safe(npe, tmp, &npinfo->rx_np, rx) {
822 npe->dev = NULL;
823 }
824 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
825
Jeff Moyer115c1d62005-06-22 22:05:31 -0700826 kfree(npinfo);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000827 }
Jiri Slaby21edbb22010-03-16 05:29:54 +0000828put:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 dev_put(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700830 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
David S. Millerc68b9072006-11-14 20:40:49 -0800833static int __init netpoll_init(void)
834{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800835 skb_queue_head_init(&skb_pool);
836 return 0;
837}
838core_initcall(netpoll_init);
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840void netpoll_cleanup(struct netpoll *np)
841{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700842 struct netpoll_info *npinfo;
843 unsigned long flags;
844
Jeff Moyer115c1d62005-06-22 22:05:31 -0700845 if (np->dev) {
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700846 npinfo = np->dev->npinfo;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700847 if (npinfo) {
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000848 if (!list_empty(&npinfo->rx_np)) {
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700849 spin_lock_irqsave(&npinfo->rx_lock, flags);
Daniel Borkmann508e14b2010-01-12 14:27:30 +0000850 list_del(&np->rx);
851 if (list_empty(&npinfo->rx_np))
852 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700853 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
854 }
855
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700856 if (atomic_dec_and_test(&npinfo->refcnt)) {
WANG Cong0e34e932010-05-06 00:47:21 -0700857 const struct net_device_ops *ops;
Herbert Xude85d992010-06-10 16:12:44 +0000858
859 ops = np->dev->netdev_ops;
860 if (ops->ndo_netpoll_cleanup)
861 ops->ndo_netpoll_cleanup(np->dev);
862
863 rcu_assign_pointer(np->dev->npinfo, NULL);
864
865 /* avoid racing with NAPI reading npinfo */
866 synchronize_rcu_bh();
867
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700868 skb_queue_purge(&npinfo->arp_tx);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900869 skb_queue_purge(&npinfo->txq);
Jarek Poplawski25442ca2007-07-05 17:42:44 -0700870 cancel_rearming_delayed_work(&npinfo->tx_work);
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700871
Jarek Poplawski17200812007-06-28 22:11:47 -0700872 /* clean after last, unfinished work */
Stephen Hemminger0adc9ad2007-11-19 19:15:03 -0800873 __skb_queue_purge(&npinfo->txq);
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700874 kfree(npinfo);
875 }
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700876 }
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700877
Jeff Moyer115c1d62005-06-22 22:05:31 -0700878 dev_put(np->dev);
879 }
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 np->dev = NULL;
882}
883
884int netpoll_trap(void)
885{
886 return atomic_read(&trapped);
887}
888
889void netpoll_set_trap(int trap)
890{
891 if (trap)
892 atomic_inc(&trapped);
893 else
894 atomic_dec(&trapped);
895}
896
WANG Cong0e34e932010-05-06 00:47:21 -0700897EXPORT_SYMBOL(netpoll_send_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898EXPORT_SYMBOL(netpoll_set_trap);
899EXPORT_SYMBOL(netpoll_trap);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700900EXPORT_SYMBOL(netpoll_print_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901EXPORT_SYMBOL(netpoll_parse_options);
902EXPORT_SYMBOL(netpoll_setup);
903EXPORT_SYMBOL(netpoll_cleanup);
904EXPORT_SYMBOL(netpoll_send_udp);
WANG Cong0e34e932010-05-06 00:47:21 -0700905EXPORT_SYMBOL(netpoll_poll_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906EXPORT_SYMBOL(netpoll_poll);