netpoll: remove IFF_IN_NETPOLL flag
[linux-2.6-block.git] / net / core / netpoll.c
CommitLineData
1da177e4
LT
1/*
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
bff38771 12#include <linux/moduleparam.h>
1da177e4
LT
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/string.h>
14c85021 16#include <linux/if_arp.h>
1da177e4
LT
17#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>
5a0e3ad6 25#include <linux/slab.h>
1da177e4
LT
26#include <net/tcp.h>
27#include <net/udp.h>
28#include <asm/unaligned.h>
9cbc1cb8 29#include <trace/events/napi.h>
1da177e4
LT
30
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
1da177e4 38
a1bcfacd 39static struct sk_buff_head skb_pool;
1da177e4
LT
40
41static atomic_t trapped;
42
2bdfe0ba 43#define USEC_PER_POLL 50
d9452e9f
DM
44#define NETPOLL_RX_ENABLED 1
45#define NETPOLL_RX_DROP 2
1da177e4
LT
46
47#define MAX_SKB_SIZE \
48 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
49 sizeof(struct iphdr) + sizeof(struct ethhdr))
50
3578b0c8 51static void zap_completion_queue(void);
068c6e98 52static void arp_reply(struct sk_buff *skb);
1da177e4 53
bff38771
AV
54static unsigned int carrier_timeout = 4;
55module_param(carrier_timeout, uint, 0644);
56
c4028958 57static void queue_process(struct work_struct *work)
1da177e4 58{
4c1ac1b4
DH
59 struct netpoll_info *npinfo =
60 container_of(work, struct netpoll_info, tx_work.work);
1da177e4 61 struct sk_buff *skb;
3640543d 62 unsigned long flags;
1da177e4 63
6c43ff18
SH
64 while ((skb = skb_dequeue(&npinfo->txq))) {
65 struct net_device *dev = skb->dev;
00829823 66 const struct net_device_ops *ops = dev->netdev_ops;
fd2ea0a7 67 struct netdev_queue *txq;
1da177e4 68
6c43ff18
SH
69 if (!netif_device_present(dev) || !netif_running(dev)) {
70 __kfree_skb(skb);
71 continue;
72 }
1da177e4 73
fd2ea0a7
DM
74 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
75
3640543d 76 local_irq_save(flags);
fd2ea0a7 77 __netif_tx_lock(txq, smp_processor_id());
5a0d2268 78 if (netif_tx_queue_frozen_or_stopped(txq) ||
00829823 79 ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) {
6c43ff18 80 skb_queue_head(&npinfo->txq, skb);
fd2ea0a7 81 __netif_tx_unlock(txq);
3640543d 82 local_irq_restore(flags);
1da177e4 83
25442caf 84 schedule_delayed_work(&npinfo->tx_work, HZ/10);
6c43ff18
SH
85 return;
86 }
fd2ea0a7 87 __netif_tx_unlock(txq);
3640543d 88 local_irq_restore(flags);
1da177e4 89 }
1da177e4
LT
90}
91
b51655b9
AV
92static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
93 unsigned short ulen, __be32 saddr, __be32 daddr)
1da177e4 94{
d6f5493c 95 __wsum psum;
fb286bb2 96
60476372 97 if (uh->check == 0 || skb_csum_unnecessary(skb))
1da177e4
LT
98 return 0;
99
fb286bb2
HX
100 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
101
84fa7933 102 if (skb->ip_summed == CHECKSUM_COMPLETE &&
d3bc23e7 103 !csum_fold(csum_add(psum, skb->csum)))
fb286bb2 104 return 0;
1da177e4 105
fb286bb2 106 skb->csum = psum;
1da177e4 107
fb286bb2 108 return __skb_checksum_complete(skb);
1da177e4
LT
109}
110
111/*
112 * Check whether delayed processing was scheduled for our NIC. If so,
113 * we attempt to grab the poll lock and use ->poll() to pump the card.
114 * If this fails, either we've recursed in ->poll() or it's already
115 * running on another CPU.
116 *
117 * Note: we don't mask interrupts with this lock because we're using
118 * trylock here and interrupts are already disabled in the softirq
119 * case. Further, we test the poll_owner to avoid recursion on UP
120 * systems where the lock doesn't exist.
121 *
122 * In cases where there is bi-directional communications, reading only
123 * one message at a time can lead to packets being dropped by the
124 * network adapter, forcing superfluous retries and possibly timeouts.
125 * Thus, we set our budget to greater than 1.
126 */
0a7606c1
DM
127static int poll_one_napi(struct netpoll_info *npinfo,
128 struct napi_struct *napi, int budget)
129{
130 int work;
131
132 /* net_rx_action's ->poll() invocations and our's are
133 * synchronized by this test which is only made while
134 * holding the napi->poll_lock.
135 */
136 if (!test_bit(NAPI_STATE_SCHED, &napi->state))
137 return budget;
138
d9452e9f 139 npinfo->rx_flags |= NETPOLL_RX_DROP;
0a7606c1 140 atomic_inc(&trapped);
7b363e44 141 set_bit(NAPI_STATE_NPSVC, &napi->state);
0a7606c1
DM
142
143 work = napi->poll(napi, budget);
7d18f114 144 trace_napi_poll(napi);
0a7606c1 145
7b363e44 146 clear_bit(NAPI_STATE_NPSVC, &napi->state);
0a7606c1 147 atomic_dec(&trapped);
d9452e9f 148 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
0a7606c1
DM
149
150 return budget - work;
151}
152
5106930b 153static void poll_napi(struct net_device *dev)
1da177e4 154{
bea3348e 155 struct napi_struct *napi;
1da177e4
LT
156 int budget = 16;
157
f13d493d 158 list_for_each_entry(napi, &dev->napi_list, dev_list) {
0a7606c1 159 if (napi->poll_owner != smp_processor_id() &&
bea3348e 160 spin_trylock(&napi->poll_lock)) {
5106930b 161 budget = poll_one_napi(dev->npinfo, napi, budget);
bea3348e 162 spin_unlock(&napi->poll_lock);
0a7606c1
DM
163
164 if (!budget)
165 break;
bea3348e 166 }
1da177e4
LT
167 }
168}
169
068c6e98
NH
170static void service_arp_queue(struct netpoll_info *npi)
171{
5106930b
SH
172 if (npi) {
173 struct sk_buff *skb;
068c6e98 174
5106930b
SH
175 while ((skb = skb_dequeue(&npi->arp_tx)))
176 arp_reply(skb);
068c6e98 177 }
068c6e98
NH
178}
179
0e34e931 180void netpoll_poll_dev(struct net_device *dev)
1da177e4 181{
5e392739 182 const struct net_device_ops *ops;
5106930b 183
5e392739
PE
184 if (!dev || !netif_running(dev))
185 return;
186
187 ops = dev->netdev_ops;
188 if (!ops->ndo_poll_controller)
1da177e4
LT
189 return;
190
191 /* Process pending work on NIC */
d314774c 192 ops->ndo_poll_controller(dev);
5106930b
SH
193
194 poll_napi(dev);
1da177e4 195
5106930b 196 service_arp_queue(dev->npinfo);
068c6e98 197
3578b0c8 198 zap_completion_queue();
1da177e4 199}
9e34a5b5 200EXPORT_SYMBOL(netpoll_poll_dev);
1da177e4 201
0e34e931
WC
202void netpoll_poll(struct netpoll *np)
203{
204 netpoll_poll_dev(np->dev);
205}
9e34a5b5 206EXPORT_SYMBOL(netpoll_poll);
0e34e931 207
1da177e4
LT
208static void refill_skbs(void)
209{
210 struct sk_buff *skb;
211 unsigned long flags;
212
a1bcfacd
SH
213 spin_lock_irqsave(&skb_pool.lock, flags);
214 while (skb_pool.qlen < MAX_SKBS) {
1da177e4
LT
215 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
216 if (!skb)
217 break;
218
a1bcfacd 219 __skb_queue_tail(&skb_pool, skb);
1da177e4 220 }
a1bcfacd 221 spin_unlock_irqrestore(&skb_pool.lock, flags);
1da177e4
LT
222}
223
3578b0c8
DM
224static void zap_completion_queue(void)
225{
226 unsigned long flags;
227 struct softnet_data *sd = &get_cpu_var(softnet_data);
228
229 if (sd->completion_queue) {
230 struct sk_buff *clist;
231
232 local_irq_save(flags);
233 clist = sd->completion_queue;
234 sd->completion_queue = NULL;
235 local_irq_restore(flags);
236
237 while (clist != NULL) {
238 struct sk_buff *skb = clist;
239 clist = clist->next;
240 if (skb->destructor) {
241 atomic_inc(&skb->users);
242 dev_kfree_skb_any(skb); /* put this one back */
243 } else {
244 __kfree_skb(skb);
245 }
246 }
247 }
248
249 put_cpu_var(softnet_data);
250}
251
a1bcfacd 252static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
1da177e4 253{
a1bcfacd
SH
254 int count = 0;
255 struct sk_buff *skb;
1da177e4 256
3578b0c8 257 zap_completion_queue();
a1bcfacd 258 refill_skbs();
1da177e4 259repeat:
1da177e4
LT
260
261 skb = alloc_skb(len, GFP_ATOMIC);
a1bcfacd
SH
262 if (!skb)
263 skb = skb_dequeue(&skb_pool);
1da177e4
LT
264
265 if (!skb) {
a1bcfacd
SH
266 if (++count < 10) {
267 netpoll_poll(np);
268 goto repeat;
1da177e4 269 }
a1bcfacd 270 return NULL;
1da177e4
LT
271 }
272
273 atomic_set(&skb->users, 1);
274 skb_reserve(skb, reserve);
275 return skb;
276}
277
bea3348e
SH
278static int netpoll_owner_active(struct net_device *dev)
279{
280 struct napi_struct *napi;
281
282 list_for_each_entry(napi, &dev->napi_list, dev_list) {
283 if (napi->poll_owner == smp_processor_id())
284 return 1;
285 }
286 return 0;
287}
288
c2355e1a
NH
289void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
290 struct net_device *dev)
1da177e4 291{
2bdfe0ba
SH
292 int status = NETDEV_TX_BUSY;
293 unsigned long tries;
00829823 294 const struct net_device_ops *ops = dev->netdev_ops;
de85d99e 295 /* It is up to the caller to keep npinfo alive. */
4ec93edb 296 struct netpoll_info *npinfo = np->dev->npinfo;
2bdfe0ba 297
4ec93edb
YH
298 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
299 __kfree_skb(skb);
300 return;
301 }
2bdfe0ba
SH
302
303 /* don't get messages out of order, and no recursion */
bea3348e 304 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
fd2ea0a7 305 struct netdev_queue *txq;
a49f99ff
AM
306 unsigned long flags;
307
fd2ea0a7
DM
308 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
309
a49f99ff 310 local_irq_save(flags);
0db3dc73
SH
311 /* try until next clock tick */
312 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
313 tries > 0; --tries) {
fd2ea0a7 314 if (__netif_tx_trylock(txq)) {
08baf561 315 if (!netif_tx_queue_stopped(txq)) {
00829823 316 status = ops->ndo_start_xmit(skb, dev);
08baf561
ED
317 if (status == NETDEV_TX_OK)
318 txq_trans_update(txq);
319 }
fd2ea0a7 320 __netif_tx_unlock(txq);
e37b8d93
AM
321
322 if (status == NETDEV_TX_OK)
323 break;
324
e37b8d93 325 }
0db3dc73
SH
326
327 /* tickle device maybe there is some cleanup */
328 netpoll_poll(np);
329
330 udelay(USEC_PER_POLL);
0db1d6fc 331 }
79b1bee8
DD
332
333 WARN_ONCE(!irqs_disabled(),
334 "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n",
335 dev->name, ops->ndo_start_xmit);
336
a49f99ff 337 local_irq_restore(flags);
1da177e4 338 }
1da177e4 339
2bdfe0ba 340 if (status != NETDEV_TX_OK) {
5de4a473 341 skb_queue_tail(&npinfo->txq, skb);
4c1ac1b4 342 schedule_delayed_work(&npinfo->tx_work,0);
1da177e4 343 }
1da177e4 344}
c2355e1a 345EXPORT_SYMBOL(netpoll_send_skb_on_dev);
1da177e4
LT
346
347void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
348{
349 int total_len, eth_len, ip_len, udp_len;
350 struct sk_buff *skb;
351 struct udphdr *udph;
352 struct iphdr *iph;
353 struct ethhdr *eth;
354
355 udp_len = len + sizeof(*udph);
356 ip_len = eth_len = udp_len + sizeof(*iph);
357 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
358
359 skb = find_skb(np, total_len, total_len - len);
360 if (!skb)
361 return;
362
27d7ff46 363 skb_copy_to_linear_data(skb, msg, len);
1da177e4
LT
364 skb->len += len;
365
4bedb452
ACM
366 skb_push(skb, sizeof(*udph));
367 skb_reset_transport_header(skb);
368 udph = udp_hdr(skb);
1da177e4
LT
369 udph->source = htons(np->local_port);
370 udph->dest = htons(np->remote_port);
371 udph->len = htons(udp_len);
372 udph->check = 0;
e7557af5
HH
373 udph->check = csum_tcpudp_magic(np->local_ip,
374 np->remote_ip,
8e365eec 375 udp_len, IPPROTO_UDP,
07f0757a 376 csum_partial(udph, udp_len, 0));
8e365eec 377 if (udph->check == 0)
5e57dff2 378 udph->check = CSUM_MANGLED_0;
1da177e4 379
e2d1bca7
ACM
380 skb_push(skb, sizeof(*iph));
381 skb_reset_network_header(skb);
eddc9ec5 382 iph = ip_hdr(skb);
1da177e4
LT
383
384 /* iph->version = 4; iph->ihl = 5; */
385 put_unaligned(0x45, (unsigned char *)iph);
386 iph->tos = 0;
387 put_unaligned(htons(ip_len), &(iph->tot_len));
388 iph->id = 0;
389 iph->frag_off = 0;
390 iph->ttl = 64;
391 iph->protocol = IPPROTO_UDP;
392 iph->check = 0;
e7557af5
HH
393 put_unaligned(np->local_ip, &(iph->saddr));
394 put_unaligned(np->remote_ip, &(iph->daddr));
1da177e4
LT
395 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
396
397 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
459a98ed 398 skb_reset_mac_header(skb);
206daaf7 399 skb->protocol = eth->h_proto = htons(ETH_P_IP);
09538641
SH
400 memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN);
401 memcpy(eth->h_dest, np->remote_mac, ETH_ALEN);
1da177e4
LT
402
403 skb->dev = np->dev;
404
405 netpoll_send_skb(np, skb);
406}
9e34a5b5 407EXPORT_SYMBOL(netpoll_send_udp);
1da177e4
LT
408
409static void arp_reply(struct sk_buff *skb)
410{
115c1d6e 411 struct netpoll_info *npinfo = skb->dev->npinfo;
1da177e4
LT
412 struct arphdr *arp;
413 unsigned char *arp_ptr;
414 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
252e3346 415 __be32 sip, tip;
47bbec02 416 unsigned char *sha;
1da177e4 417 struct sk_buff *send_skb;
508e14b4
DB
418 struct netpoll *np, *tmp;
419 unsigned long flags;
420 int hits = 0;
421
422 if (list_empty(&npinfo->rx_np))
423 return;
424
425 /* Before checking the packet, we do some early
426 inspection whether this is interesting at all */
427 spin_lock_irqsave(&npinfo->rx_lock, flags);
428 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
429 if (np->dev == skb->dev)
430 hits++;
431 }
432 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1da177e4 433
508e14b4
DB
434 /* No netpoll struct is using this dev */
435 if (!hits)
115c1d6e 436 return;
1da177e4
LT
437
438 /* No arp on this interface */
439 if (skb->dev->flags & IFF_NOARP)
440 return;
441
988b7050 442 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
1da177e4
LT
443 return;
444
c1d2bbe1 445 skb_reset_network_header(skb);
badff6d0 446 skb_reset_transport_header(skb);
d0a92be0 447 arp = arp_hdr(skb);
1da177e4
LT
448
449 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
450 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
451 arp->ar_pro != htons(ETH_P_IP) ||
452 arp->ar_op != htons(ARPOP_REQUEST))
453 return;
454
47bbec02
NH
455 arp_ptr = (unsigned char *)(arp+1);
456 /* save the location of the src hw addr */
457 sha = arp_ptr;
458 arp_ptr += skb->dev->addr_len;
1da177e4 459 memcpy(&sip, arp_ptr, 4);
47bbec02 460 arp_ptr += 4;
508e14b4
DB
461 /* If we actually cared about dst hw addr,
462 it would get copied here */
47bbec02 463 arp_ptr += skb->dev->addr_len;
1da177e4
LT
464 memcpy(&tip, arp_ptr, 4);
465
466 /* Should we ignore arp? */
508e14b4 467 if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip))
1da177e4
LT
468 return;
469
988b7050 470 size = arp_hdr_len(skb->dev);
1da177e4 471
508e14b4
DB
472 spin_lock_irqsave(&npinfo->rx_lock, flags);
473 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
474 if (tip != np->local_ip)
475 continue;
1da177e4 476
508e14b4
DB
477 send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev),
478 LL_RESERVED_SPACE(np->dev));
479 if (!send_skb)
480 continue;
1da177e4 481
508e14b4
DB
482 skb_reset_network_header(send_skb);
483 arp = (struct arphdr *) skb_put(send_skb, size);
484 send_skb->dev = skb->dev;
485 send_skb->protocol = htons(ETH_P_ARP);
1da177e4 486
508e14b4
DB
487 /* Fill the device header for the ARP frame */
488 if (dev_hard_header(send_skb, skb->dev, ptype,
489 sha, np->dev->dev_addr,
490 send_skb->len) < 0) {
491 kfree_skb(send_skb);
492 continue;
493 }
1da177e4 494
508e14b4
DB
495 /*
496 * Fill out the arp protocol part.
497 *
498 * we only support ethernet device type,
499 * which (according to RFC 1390) should
500 * always equal 1 (Ethernet).
501 */
1da177e4 502
508e14b4
DB
503 arp->ar_hrd = htons(np->dev->type);
504 arp->ar_pro = htons(ETH_P_IP);
505 arp->ar_hln = np->dev->addr_len;
506 arp->ar_pln = 4;
507 arp->ar_op = htons(type);
508
509 arp_ptr = (unsigned char *)(arp + 1);
510 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
511 arp_ptr += np->dev->addr_len;
512 memcpy(arp_ptr, &tip, 4);
513 arp_ptr += 4;
514 memcpy(arp_ptr, sha, np->dev->addr_len);
515 arp_ptr += np->dev->addr_len;
516 memcpy(arp_ptr, &sip, 4);
517
518 netpoll_send_skb(np, send_skb);
519
520 /* If there are several rx_hooks for the same address,
521 we're fine by sending a single reply */
522 break;
523 }
524 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
1da177e4
LT
525}
526
527int __netpoll_rx(struct sk_buff *skb)
528{
529 int proto, len, ulen;
508e14b4 530 int hits = 0;
1da177e4
LT
531 struct iphdr *iph;
532 struct udphdr *uh;
508e14b4
DB
533 struct netpoll_info *npinfo = skb->dev->npinfo;
534 struct netpoll *np, *tmp;
068c6e98 535
508e14b4 536 if (list_empty(&npinfo->rx_np))
1da177e4 537 goto out;
508e14b4 538
1da177e4
LT
539 if (skb->dev->type != ARPHRD_ETHER)
540 goto out;
541
d9452e9f 542 /* check if netpoll clients need ARP */
724800d6 543 if (skb->protocol == htons(ETH_P_ARP) &&
1da177e4 544 atomic_read(&trapped)) {
508e14b4 545 skb_queue_tail(&npinfo->arp_tx, skb);
1da177e4
LT
546 return 1;
547 }
548
549 proto = ntohs(eth_hdr(skb)->h_proto);
550 if (proto != ETH_P_IP)
551 goto out;
552 if (skb->pkt_type == PACKET_OTHERHOST)
553 goto out;
554 if (skb_shared(skb))
555 goto out;
556
557 iph = (struct iphdr *)skb->data;
558 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
559 goto out;
560 if (iph->ihl < 5 || iph->version != 4)
561 goto out;
562 if (!pskb_may_pull(skb, iph->ihl*4))
563 goto out;
564 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
565 goto out;
566
567 len = ntohs(iph->tot_len);
568 if (skb->len < len || len < iph->ihl*4)
569 goto out;
570
5e7d7fa5
AL
571 /*
572 * Our transport medium may have padded the buffer out.
573 * Now We trim to the true length of the frame.
574 */
575 if (pskb_trim_rcsum(skb, len))
576 goto out;
577
1da177e4
LT
578 if (iph->protocol != IPPROTO_UDP)
579 goto out;
580
581 len -= iph->ihl*4;
582 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
583 ulen = ntohs(uh->len);
584
585 if (ulen != len)
586 goto out;
fb286bb2 587 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
1da177e4 588 goto out;
1da177e4 589
508e14b4
DB
590 list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) {
591 if (np->local_ip && np->local_ip != iph->daddr)
592 continue;
593 if (np->remote_ip && np->remote_ip != iph->saddr)
594 continue;
595 if (np->local_port && np->local_port != ntohs(uh->dest))
596 continue;
597
598 np->rx_hook(np, ntohs(uh->source),
599 (char *)(uh+1),
600 ulen - sizeof(struct udphdr));
601 hits++;
602 }
603
604 if (!hits)
605 goto out;
1da177e4
LT
606
607 kfree_skb(skb);
608 return 1;
609
610out:
611 if (atomic_read(&trapped)) {
612 kfree_skb(skb);
613 return 1;
614 }
615
616 return 0;
617}
618
0bcc1816
SS
619void netpoll_print_options(struct netpoll *np)
620{
621 printk(KERN_INFO "%s: local port %d\n",
622 np->name, np->local_port);
e7557af5
HH
623 printk(KERN_INFO "%s: local IP %pI4\n",
624 np->name, &np->local_ip);
5fc05f87 625 printk(KERN_INFO "%s: interface '%s'\n",
0bcc1816
SS
626 np->name, np->dev_name);
627 printk(KERN_INFO "%s: remote port %d\n",
628 np->name, np->remote_port);
e7557af5
HH
629 printk(KERN_INFO "%s: remote IP %pI4\n",
630 np->name, &np->remote_ip);
e174961c
JB
631 printk(KERN_INFO "%s: remote ethernet address %pM\n",
632 np->name, np->remote_mac);
0bcc1816 633}
9e34a5b5 634EXPORT_SYMBOL(netpoll_print_options);
0bcc1816 635
1da177e4
LT
636int netpoll_parse_options(struct netpoll *np, char *opt)
637{
638 char *cur=opt, *delim;
639
c68b9070 640 if (*cur != '@') {
1da177e4
LT
641 if ((delim = strchr(cur, '@')) == NULL)
642 goto parse_failed;
c68b9070
DM
643 *delim = 0;
644 np->local_port = simple_strtol(cur, NULL, 10);
645 cur = delim;
1da177e4
LT
646 }
647 cur++;
1da177e4 648
c68b9070 649 if (*cur != '/') {
1da177e4
LT
650 if ((delim = strchr(cur, '/')) == NULL)
651 goto parse_failed;
c68b9070 652 *delim = 0;
e7557af5 653 np->local_ip = in_aton(cur);
c68b9070 654 cur = delim;
1da177e4
LT
655 }
656 cur++;
657
c68b9070 658 if (*cur != ',') {
1da177e4
LT
659 /* parse out dev name */
660 if ((delim = strchr(cur, ',')) == NULL)
661 goto parse_failed;
c68b9070 662 *delim = 0;
1da177e4 663 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
c68b9070 664 cur = delim;
1da177e4
LT
665 }
666 cur++;
667
c68b9070 668 if (*cur != '@') {
1da177e4
LT
669 /* dst port */
670 if ((delim = strchr(cur, '@')) == NULL)
671 goto parse_failed;
c68b9070 672 *delim = 0;
5fc05f87
AW
673 if (*cur == ' ' || *cur == '\t')
674 printk(KERN_INFO "%s: warning: whitespace"
675 "is not allowed\n", np->name);
c68b9070
DM
676 np->remote_port = simple_strtol(cur, NULL, 10);
677 cur = delim;
1da177e4
LT
678 }
679 cur++;
1da177e4
LT
680
681 /* dst ip */
682 if ((delim = strchr(cur, '/')) == NULL)
683 goto parse_failed;
c68b9070 684 *delim = 0;
e7557af5 685 np->remote_ip = in_aton(cur);
c68b9070 686 cur = delim + 1;
1da177e4 687
c68b9070 688 if (*cur != 0) {
1da177e4
LT
689 /* MAC address */
690 if ((delim = strchr(cur, ':')) == NULL)
691 goto parse_failed;
c68b9070
DM
692 *delim = 0;
693 np->remote_mac[0] = simple_strtol(cur, NULL, 16);
694 cur = delim + 1;
1da177e4
LT
695 if ((delim = strchr(cur, ':')) == NULL)
696 goto parse_failed;
c68b9070
DM
697 *delim = 0;
698 np->remote_mac[1] = simple_strtol(cur, NULL, 16);
699 cur = delim + 1;
1da177e4
LT
700 if ((delim = strchr(cur, ':')) == NULL)
701 goto parse_failed;
c68b9070
DM
702 *delim = 0;
703 np->remote_mac[2] = simple_strtol(cur, NULL, 16);
704 cur = delim + 1;
1da177e4
LT
705 if ((delim = strchr(cur, ':')) == NULL)
706 goto parse_failed;
c68b9070
DM
707 *delim = 0;
708 np->remote_mac[3] = simple_strtol(cur, NULL, 16);
709 cur = delim + 1;
1da177e4
LT
710 if ((delim = strchr(cur, ':')) == NULL)
711 goto parse_failed;
c68b9070
DM
712 *delim = 0;
713 np->remote_mac[4] = simple_strtol(cur, NULL, 16);
714 cur = delim + 1;
715 np->remote_mac[5] = simple_strtol(cur, NULL, 16);
1da177e4
LT
716 }
717
0bcc1816 718 netpoll_print_options(np);
1da177e4
LT
719
720 return 0;
721
722 parse_failed:
5fc05f87 723 printk(KERN_INFO "%s: couldn't parse config at '%s'!\n",
1da177e4
LT
724 np->name, cur);
725 return -1;
726}
9e34a5b5 727EXPORT_SYMBOL(netpoll_parse_options);
1da177e4 728
8fdd95ec 729int __netpoll_setup(struct netpoll *np)
1da177e4 730{
8fdd95ec 731 struct net_device *ndev = np->dev;
115c1d6e 732 struct netpoll_info *npinfo;
4247e161 733 const struct net_device_ops *ops;
fbeec2e1 734 unsigned long flags;
b41848b6 735 int err;
1da177e4 736
8fdd95ec
HX
737 if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) ||
738 !ndev->netdev_ops->ndo_poll_controller) {
739 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
740 np->name, np->dev_name);
741 err = -ENOTSUPP;
742 goto out;
743 }
744
745 if (!ndev->npinfo) {
746 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
747 if (!npinfo) {
748 err = -ENOMEM;
749 goto out;
750 }
751
752 npinfo->rx_flags = 0;
753 INIT_LIST_HEAD(&npinfo->rx_np);
754
755 spin_lock_init(&npinfo->rx_lock);
756 skb_queue_head_init(&npinfo->arp_tx);
757 skb_queue_head_init(&npinfo->txq);
758 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
759
760 atomic_set(&npinfo->refcnt, 1);
761
762 ops = np->dev->netdev_ops;
763 if (ops->ndo_netpoll_setup) {
764 err = ops->ndo_netpoll_setup(ndev, npinfo);
765 if (err)
766 goto free_npinfo;
767 }
768 } else {
769 npinfo = ndev->npinfo;
770 atomic_inc(&npinfo->refcnt);
771 }
772
773 npinfo->netpoll = np;
774
775 if (np->rx_hook) {
776 spin_lock_irqsave(&npinfo->rx_lock, flags);
777 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
778 list_add_tail(&np->rx, &npinfo->rx_np);
779 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
780 }
781
782 /* last thing to do is link it to the net device structure */
783 rcu_assign_pointer(ndev->npinfo, npinfo);
8fdd95ec
HX
784
785 return 0;
786
787free_npinfo:
788 kfree(npinfo);
789out:
790 return err;
791}
792EXPORT_SYMBOL_GPL(__netpoll_setup);
793
794int netpoll_setup(struct netpoll *np)
795{
796 struct net_device *ndev = NULL;
797 struct in_device *in_dev;
798 int err;
799
1da177e4 800 if (np->dev_name)
881d966b 801 ndev = dev_get_by_name(&init_net, np->dev_name);
1da177e4
LT
802 if (!ndev) {
803 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
804 np->name, np->dev_name);
b41848b6 805 return -ENODEV;
1da177e4
LT
806 }
807
1da177e4
LT
808 if (!netif_running(ndev)) {
809 unsigned long atmost, atleast;
810
811 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
812 np->name, np->dev_name);
813
6756ae4b 814 rtnl_lock();
b41848b6
SH
815 err = dev_open(ndev);
816 rtnl_unlock();
817
818 if (err) {
1da177e4 819 printk(KERN_ERR "%s: failed to open %s\n",
b41848b6 820 np->name, ndev->name);
dbaa1541 821 goto put;
1da177e4 822 }
1da177e4
LT
823
824 atleast = jiffies + HZ/10;
bff38771 825 atmost = jiffies + carrier_timeout * HZ;
1da177e4
LT
826 while (!netif_carrier_ok(ndev)) {
827 if (time_after(jiffies, atmost)) {
828 printk(KERN_NOTICE
829 "%s: timeout waiting for carrier\n",
830 np->name);
831 break;
832 }
1b614fb9 833 msleep(1);
1da177e4
LT
834 }
835
836 /* If carrier appears to come up instantly, we don't
837 * trust it and pause so that we don't pump all our
838 * queued console messages into the bitbucket.
839 */
840
841 if (time_before(jiffies, atleast)) {
842 printk(KERN_NOTICE "%s: carrier detect appears"
843 " untrustworthy, waiting 4 seconds\n",
844 np->name);
845 msleep(4000);
846 }
847 }
848
1da177e4
LT
849 if (!np->local_ip) {
850 rcu_read_lock();
e5ed6399 851 in_dev = __in_dev_get_rcu(ndev);
1da177e4
LT
852
853 if (!in_dev || !in_dev->ifa_list) {
854 rcu_read_unlock();
855 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
856 np->name, np->dev_name);
b41848b6 857 err = -EDESTADDRREQ;
dbaa1541 858 goto put;
1da177e4
LT
859 }
860
e7557af5 861 np->local_ip = in_dev->ifa_list->ifa_local;
1da177e4 862 rcu_read_unlock();
e7557af5 863 printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip);
1da177e4
LT
864 }
865
dbaa1541
HX
866 np->dev = ndev;
867
868 /* fill up the skb queue */
869 refill_skbs();
870
871 rtnl_lock();
8fdd95ec 872 err = __netpoll_setup(np);
dbaa1541 873 rtnl_unlock();
53fb95d3 874
8fdd95ec
HX
875 if (err)
876 goto put;
877
1da177e4
LT
878 return 0;
879
21edbb22 880put:
1da177e4 881 dev_put(ndev);
b41848b6 882 return err;
1da177e4 883}
9e34a5b5 884EXPORT_SYMBOL(netpoll_setup);
1da177e4 885
c68b9070
DM
886static int __init netpoll_init(void)
887{
a1bcfacd
SH
888 skb_queue_head_init(&skb_pool);
889 return 0;
890}
891core_initcall(netpoll_init);
892
8fdd95ec 893void __netpoll_cleanup(struct netpoll *np)
1da177e4 894{
fbeec2e1
JM
895 struct netpoll_info *npinfo;
896 unsigned long flags;
897
8fdd95ec
HX
898 npinfo = np->dev->npinfo;
899 if (!npinfo)
dbaa1541 900 return;
93ec2c72 901
8fdd95ec
HX
902 if (!list_empty(&npinfo->rx_np)) {
903 spin_lock_irqsave(&npinfo->rx_lock, flags);
904 list_del(&np->rx);
905 if (list_empty(&npinfo->rx_np))
906 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
907 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
908 }
de85d99e 909
8fdd95ec
HX
910 if (atomic_dec_and_test(&npinfo->refcnt)) {
911 const struct net_device_ops *ops;
de85d99e 912
8fdd95ec
HX
913 ops = np->dev->netdev_ops;
914 if (ops->ndo_netpoll_cleanup)
915 ops->ndo_netpoll_cleanup(np->dev);
de85d99e 916
8fdd95ec 917 rcu_assign_pointer(np->dev->npinfo, NULL);
de85d99e 918
dbaa1541
HX
919 /* avoid racing with NAPI reading npinfo */
920 synchronize_rcu_bh();
93ec2c72 921
dbaa1541
HX
922 skb_queue_purge(&npinfo->arp_tx);
923 skb_queue_purge(&npinfo->txq);
afe2c511 924 cancel_delayed_work_sync(&npinfo->tx_work);
93ec2c72 925
dbaa1541
HX
926 /* clean after last, unfinished work */
927 __skb_queue_purge(&npinfo->txq);
928 kfree(npinfo);
115c1d6e 929 }
8fdd95ec
HX
930}
931EXPORT_SYMBOL_GPL(__netpoll_cleanup);
fbeec2e1 932
8fdd95ec
HX
933void netpoll_cleanup(struct netpoll *np)
934{
935 if (!np->dev)
936 return;
dbaa1541 937
8fdd95ec
HX
938 rtnl_lock();
939 __netpoll_cleanup(np);
940 rtnl_unlock();
941
942 dev_put(np->dev);
1da177e4
LT
943 np->dev = NULL;
944}
9e34a5b5 945EXPORT_SYMBOL(netpoll_cleanup);
1da177e4
LT
946
947int netpoll_trap(void)
948{
949 return atomic_read(&trapped);
950}
9e34a5b5 951EXPORT_SYMBOL(netpoll_trap);
1da177e4
LT
952
953void netpoll_set_trap(int trap)
954{
955 if (trap)
956 atomic_inc(&trapped);
957 else
958 atomic_dec(&trapped);
959}
1da177e4 960EXPORT_SYMBOL(netpoll_set_trap);