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