blob: 4de62f1f41340e33901dd4ea52e571a9e0604677 [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
12#include <linux/smp_lock.h>
13#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>
25#include <net/tcp.h>
26#include <net/udp.h>
27#include <asm/unaligned.h>
28
29/*
30 * We maintain a small pool of fully-sized skbs, to make sure the
31 * message gets out even in extreme OOM situations.
32 */
33
34#define MAX_UDP_CHUNK 1460
35#define MAX_SKBS 32
36#define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
Matt Mackall0db1d6f2005-08-11 19:25:54 -070037#define MAX_RETRIES 20000
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080039static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static DEFINE_SPINLOCK(queue_lock);
42static int queue_depth;
43static struct sk_buff *queue_head, *queue_tail;
44
45static atomic_t trapped;
46
47#define NETPOLL_RX_ENABLED 1
48#define NETPOLL_RX_DROP 2
49
50#define MAX_SKB_SIZE \
51 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
52 sizeof(struct iphdr) + sizeof(struct ethhdr))
53
54static void zap_completion_queue(void);
Neil Horman068c6e92006-06-26 00:04:27 -070055static void arp_reply(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static void queue_process(void *p)
58{
59 unsigned long flags;
60 struct sk_buff *skb;
61
62 while (queue_head) {
63 spin_lock_irqsave(&queue_lock, flags);
64
65 skb = queue_head;
66 queue_head = skb->next;
67 if (skb == queue_tail)
68 queue_head = NULL;
69
70 queue_depth--;
71
72 spin_unlock_irqrestore(&queue_lock, flags);
73
74 dev_queue_xmit(skb);
75 }
76}
77
78static DECLARE_WORK(send_queue, queue_process, NULL);
79
80void netpoll_queue(struct sk_buff *skb)
81{
82 unsigned long flags;
83
84 if (queue_depth == MAX_QUEUE_DEPTH) {
85 __kfree_skb(skb);
86 return;
87 }
88
89 spin_lock_irqsave(&queue_lock, flags);
90 if (!queue_head)
91 queue_head = skb;
92 else
93 queue_tail->next = skb;
94 queue_tail = skb;
95 queue_depth++;
96 spin_unlock_irqrestore(&queue_lock, flags);
97
98 schedule_work(&send_queue);
99}
100
101static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
102 unsigned short ulen, u32 saddr, u32 daddr)
103{
Herbert Xufb286bb2005-11-10 13:01:24 -0800104 unsigned int psum;
105
106 if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108
Herbert Xufb286bb2005-11-10 13:01:24 -0800109 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Patrick McHardy84fa7932006-08-29 16:44:56 -0700111 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Herbert Xufb286bb2005-11-10 13:01:24 -0800112 !(u16)csum_fold(csum_add(psum, skb->csum)))
113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Herbert Xufb286bb2005-11-10 13:01:24 -0800115 skb->csum = psum;
116
117 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/*
121 * Check whether delayed processing was scheduled for our NIC. If so,
122 * we attempt to grab the poll lock and use ->poll() to pump the card.
123 * If this fails, either we've recursed in ->poll() or it's already
124 * running on another CPU.
125 *
126 * Note: we don't mask interrupts with this lock because we're using
127 * trylock here and interrupts are already disabled in the softirq
128 * case. Further, we test the poll_owner to avoid recursion on UP
129 * systems where the lock doesn't exist.
130 *
131 * In cases where there is bi-directional communications, reading only
132 * one message at a time can lead to packets being dropped by the
133 * network adapter, forcing superfluous retries and possibly timeouts.
134 * Thus, we set our budget to greater than 1.
135 */
136static void poll_napi(struct netpoll *np)
137{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700138 struct netpoll_info *npinfo = np->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int budget = 16;
140
141 if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
Jeff Moyer115c1d62005-06-22 22:05:31 -0700142 npinfo->poll_owner != smp_processor_id() &&
143 spin_trylock(&npinfo->poll_lock)) {
144 npinfo->rx_flags |= NETPOLL_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 atomic_inc(&trapped);
146
147 np->dev->poll(np->dev, &budget);
148
149 atomic_dec(&trapped);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700150 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
151 spin_unlock(&npinfo->poll_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153}
154
Neil Horman068c6e92006-06-26 00:04:27 -0700155static void service_arp_queue(struct netpoll_info *npi)
156{
157 struct sk_buff *skb;
158
159 if (unlikely(!npi))
160 return;
161
162 skb = skb_dequeue(&npi->arp_tx);
163
164 while (skb != NULL) {
165 arp_reply(skb);
166 skb = skb_dequeue(&npi->arp_tx);
167 }
168 return;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171void netpoll_poll(struct netpoll *np)
172{
173 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
174 return;
175
176 /* Process pending work on NIC */
177 np->dev->poll_controller(np->dev);
178 if (np->dev->poll)
179 poll_napi(np);
180
Neil Horman068c6e92006-06-26 00:04:27 -0700181 service_arp_queue(np->dev->npinfo);
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 zap_completion_queue();
184}
185
186static void refill_skbs(void)
187{
188 struct sk_buff *skb;
189 unsigned long flags;
190
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800191 spin_lock_irqsave(&skb_pool.lock, flags);
192 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
194 if (!skb)
195 break;
196
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800197 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800199 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static void zap_completion_queue(void)
203{
204 unsigned long flags;
205 struct softnet_data *sd = &get_cpu_var(softnet_data);
206
207 if (sd->completion_queue) {
208 struct sk_buff *clist;
209
210 local_irq_save(flags);
211 clist = sd->completion_queue;
212 sd->completion_queue = NULL;
213 local_irq_restore(flags);
214
215 while (clist != NULL) {
216 struct sk_buff *skb = clist;
217 clist = clist->next;
218 if(skb->destructor)
219 dev_kfree_skb_any(skb); /* put this one back */
220 else
221 __kfree_skb(skb);
222 }
223 }
224
225 put_cpu_var(softnet_data);
226}
227
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800228static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800230 int count = 0;
231 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800234 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800238 if (!skb)
239 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if(!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800242 if (++count < 10) {
243 netpoll_poll(np);
244 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800246 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 atomic_set(&skb->users, 1);
250 skb_reserve(skb, reserve);
251 return skb;
252}
253
254static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
255{
256 int status;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700257 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Matt Mackallf0d34592005-08-11 19:25:11 -0700259 if (!np || !np->dev || !netif_running(np->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 __kfree_skb(skb);
261 return;
262 }
263
Jeff Moyer115c1d62005-06-22 22:05:31 -0700264 npinfo = np->dev->npinfo;
Matt Mackallf0d34592005-08-11 19:25:11 -0700265
266 /* avoid recursion */
Jeff Moyer115c1d62005-06-22 22:05:31 -0700267 if (npinfo->poll_owner == smp_processor_id() ||
268 np->dev->xmit_lock_owner == smp_processor_id()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (np->drop)
270 np->drop(skb);
271 else
272 __kfree_skb(skb);
273 return;
274 }
275
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700276 do {
277 npinfo->tries--;
Herbert Xu932ff272006-06-09 12:20:56 -0700278 netif_tx_lock(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Matt Mackallf0d34592005-08-11 19:25:11 -0700280 /*
281 * network drivers do not expect to be called if the queue is
282 * stopped.
283 */
Jeremy Fitzhardinge88348072006-06-26 00:03:40 -0700284 status = NETDEV_TX_BUSY;
285 if (!netif_queue_stopped(np->dev))
286 status = np->dev->hard_start_xmit(skb, np->dev);
Matt Mackallf0d34592005-08-11 19:25:11 -0700287
Herbert Xu932ff272006-06-09 12:20:56 -0700288 netif_tx_unlock(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Matt Mackallf0d34592005-08-11 19:25:11 -0700290 /* success */
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700291 if(!status) {
292 npinfo->tries = MAX_RETRIES; /* reset */
Matt Mackallf0d34592005-08-11 19:25:11 -0700293 return;
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Matt Mackallf0d34592005-08-11 19:25:11 -0700296 /* transmit busy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 netpoll_poll(np);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700298 udelay(50);
299 } while (npinfo->tries > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
303{
304 int total_len, eth_len, ip_len, udp_len;
305 struct sk_buff *skb;
306 struct udphdr *udph;
307 struct iphdr *iph;
308 struct ethhdr *eth;
309
310 udp_len = len + sizeof(*udph);
311 ip_len = eth_len = udp_len + sizeof(*iph);
312 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
313
314 skb = find_skb(np, total_len, total_len - len);
315 if (!skb)
316 return;
317
318 memcpy(skb->data, msg, len);
319 skb->len += len;
320
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700321 skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 udph->source = htons(np->local_port);
323 udph->dest = htons(np->remote_port);
324 udph->len = htons(udp_len);
325 udph->check = 0;
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800326 udph->check = csum_tcpudp_magic(htonl(np->local_ip),
327 htonl(np->remote_ip),
328 udp_len, IPPROTO_UDP,
329 csum_partial((unsigned char *)udph, udp_len, 0));
330 if (udph->check == 0)
331 udph->check = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700333 skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* iph->version = 4; iph->ihl = 5; */
336 put_unaligned(0x45, (unsigned char *)iph);
337 iph->tos = 0;
338 put_unaligned(htons(ip_len), &(iph->tot_len));
339 iph->id = 0;
340 iph->frag_off = 0;
341 iph->ttl = 64;
342 iph->protocol = IPPROTO_UDP;
343 iph->check = 0;
344 put_unaligned(htonl(np->local_ip), &(iph->saddr));
345 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
346 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
347
348 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700349 skb->mac.raw = skb->data;
350 skb->protocol = eth->h_proto = htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 memcpy(eth->h_source, np->local_mac, 6);
352 memcpy(eth->h_dest, np->remote_mac, 6);
353
354 skb->dev = np->dev;
355
356 netpoll_send_skb(np, skb);
357}
358
359static void arp_reply(struct sk_buff *skb)
360{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700361 struct netpoll_info *npinfo = skb->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct arphdr *arp;
363 unsigned char *arp_ptr;
364 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
365 u32 sip, tip;
366 struct sk_buff *send_skb;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700367 struct netpoll *np = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700369 if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
370 np = npinfo->rx_np;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700371 if (!np)
372 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* No arp on this interface */
375 if (skb->dev->flags & IFF_NOARP)
376 return;
377
378 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
379 (2 * skb->dev->addr_len) +
380 (2 * sizeof(u32)))))
381 return;
382
383 skb->h.raw = skb->nh.raw = skb->data;
384 arp = skb->nh.arph;
385
386 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
387 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
388 arp->ar_pro != htons(ETH_P_IP) ||
389 arp->ar_op != htons(ARPOP_REQUEST))
390 return;
391
392 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
393 memcpy(&sip, arp_ptr, 4);
394 arp_ptr += 4 + skb->dev->addr_len;
395 memcpy(&tip, arp_ptr, 4);
396
397 /* Should we ignore arp? */
398 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
399 return;
400
401 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
402 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
403 LL_RESERVED_SPACE(np->dev));
404
405 if (!send_skb)
406 return;
407
408 send_skb->nh.raw = send_skb->data;
409 arp = (struct arphdr *) skb_put(send_skb, size);
410 send_skb->dev = skb->dev;
411 send_skb->protocol = htons(ETH_P_ARP);
412
413 /* Fill the device header for the ARP frame */
414
415 if (np->dev->hard_header &&
416 np->dev->hard_header(send_skb, skb->dev, ptype,
417 np->remote_mac, np->local_mac,
418 send_skb->len) < 0) {
419 kfree_skb(send_skb);
420 return;
421 }
422
423 /*
424 * Fill out the arp protocol part.
425 *
426 * we only support ethernet device type,
427 * which (according to RFC 1390) should always equal 1 (Ethernet).
428 */
429
430 arp->ar_hrd = htons(np->dev->type);
431 arp->ar_pro = htons(ETH_P_IP);
432 arp->ar_hln = np->dev->addr_len;
433 arp->ar_pln = 4;
434 arp->ar_op = htons(type);
435
436 arp_ptr=(unsigned char *)(arp + 1);
437 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
438 arp_ptr += np->dev->addr_len;
439 memcpy(arp_ptr, &tip, 4);
440 arp_ptr += 4;
441 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
442 arp_ptr += np->dev->addr_len;
443 memcpy(arp_ptr, &sip, 4);
444
445 netpoll_send_skb(np, send_skb);
446}
447
448int __netpoll_rx(struct sk_buff *skb)
449{
450 int proto, len, ulen;
451 struct iphdr *iph;
452 struct udphdr *uh;
Neil Horman068c6e92006-06-26 00:04:27 -0700453 struct netpoll_info *npi = skb->dev->npinfo;
454 struct netpoll *np = npi->rx_np;
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700457 if (!np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto out;
459 if (skb->dev->type != ARPHRD_ETHER)
460 goto out;
461
462 /* check if netpoll clients need ARP */
463 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
464 atomic_read(&trapped)) {
Neil Horman068c6e92006-06-26 00:04:27 -0700465 skb_queue_tail(&npi->arp_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 1;
467 }
468
469 proto = ntohs(eth_hdr(skb)->h_proto);
470 if (proto != ETH_P_IP)
471 goto out;
472 if (skb->pkt_type == PACKET_OTHERHOST)
473 goto out;
474 if (skb_shared(skb))
475 goto out;
476
477 iph = (struct iphdr *)skb->data;
478 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
479 goto out;
480 if (iph->ihl < 5 || iph->version != 4)
481 goto out;
482 if (!pskb_may_pull(skb, iph->ihl*4))
483 goto out;
484 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
485 goto out;
486
487 len = ntohs(iph->tot_len);
488 if (skb->len < len || len < iph->ihl*4)
489 goto out;
490
491 if (iph->protocol != IPPROTO_UDP)
492 goto out;
493
494 len -= iph->ihl*4;
495 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
496 ulen = ntohs(uh->len);
497
498 if (ulen != len)
499 goto out;
Herbert Xufb286bb2005-11-10 13:01:24 -0800500 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto out;
502 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
503 goto out;
504 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
505 goto out;
506 if (np->local_port && np->local_port != ntohs(uh->dest))
507 goto out;
508
509 np->rx_hook(np, ntohs(uh->source),
510 (char *)(uh+1),
511 ulen - sizeof(struct udphdr));
512
513 kfree_skb(skb);
514 return 1;
515
516out:
517 if (atomic_read(&trapped)) {
518 kfree_skb(skb);
519 return 1;
520 }
521
522 return 0;
523}
524
525int netpoll_parse_options(struct netpoll *np, char *opt)
526{
527 char *cur=opt, *delim;
528
529 if(*cur != '@') {
530 if ((delim = strchr(cur, '@')) == NULL)
531 goto parse_failed;
532 *delim=0;
533 np->local_port=simple_strtol(cur, NULL, 10);
534 cur=delim;
535 }
536 cur++;
537 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
538
539 if(*cur != '/') {
540 if ((delim = strchr(cur, '/')) == NULL)
541 goto parse_failed;
542 *delim=0;
543 np->local_ip=ntohl(in_aton(cur));
544 cur=delim;
545
546 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
547 np->name, HIPQUAD(np->local_ip));
548 }
549 cur++;
550
551 if ( *cur != ',') {
552 /* parse out dev name */
553 if ((delim = strchr(cur, ',')) == NULL)
554 goto parse_failed;
555 *delim=0;
556 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
557 cur=delim;
558 }
559 cur++;
560
561 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
562
563 if ( *cur != '@' ) {
564 /* dst port */
565 if ((delim = strchr(cur, '@')) == NULL)
566 goto parse_failed;
567 *delim=0;
568 np->remote_port=simple_strtol(cur, NULL, 10);
569 cur=delim;
570 }
571 cur++;
572 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
573
574 /* dst ip */
575 if ((delim = strchr(cur, '/')) == NULL)
576 goto parse_failed;
577 *delim=0;
578 np->remote_ip=ntohl(in_aton(cur));
579 cur=delim+1;
580
581 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
582 np->name, HIPQUAD(np->remote_ip));
583
584 if( *cur != 0 )
585 {
586 /* MAC address */
587 if ((delim = strchr(cur, ':')) == NULL)
588 goto parse_failed;
589 *delim=0;
590 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
591 cur=delim+1;
592 if ((delim = strchr(cur, ':')) == NULL)
593 goto parse_failed;
594 *delim=0;
595 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
596 cur=delim+1;
597 if ((delim = strchr(cur, ':')) == NULL)
598 goto parse_failed;
599 *delim=0;
600 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
601 cur=delim+1;
602 if ((delim = strchr(cur, ':')) == NULL)
603 goto parse_failed;
604 *delim=0;
605 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
606 cur=delim+1;
607 if ((delim = strchr(cur, ':')) == NULL)
608 goto parse_failed;
609 *delim=0;
610 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
611 cur=delim+1;
612 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
613 }
614
615 printk(KERN_INFO "%s: remote ethernet address "
616 "%02x:%02x:%02x:%02x:%02x:%02x\n",
617 np->name,
618 np->remote_mac[0],
619 np->remote_mac[1],
620 np->remote_mac[2],
621 np->remote_mac[3],
622 np->remote_mac[4],
623 np->remote_mac[5]);
624
625 return 0;
626
627 parse_failed:
628 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
629 np->name, cur);
630 return -1;
631}
632
633int netpoll_setup(struct netpoll *np)
634{
635 struct net_device *ndev = NULL;
636 struct in_device *in_dev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700637 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700638 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (np->dev_name)
641 ndev = dev_get_by_name(np->dev_name);
642 if (!ndev) {
643 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
644 np->name, np->dev_name);
645 return -1;
646 }
647
648 np->dev = ndev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700649 if (!ndev->npinfo) {
650 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
651 if (!npinfo)
652 goto release;
653
Jeff Moyer11513122005-08-11 19:23:04 -0700654 npinfo->rx_flags = 0;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700655 npinfo->rx_np = NULL;
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700656 spin_lock_init(&npinfo->poll_lock);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700657 npinfo->poll_owner = -1;
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700658 npinfo->tries = MAX_RETRIES;
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700659 spin_lock_init(&npinfo->rx_lock);
Neil Horman068c6e92006-06-26 00:04:27 -0700660 skb_queue_head_init(&npinfo->arp_tx);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700661 } else
662 npinfo = ndev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (!ndev->poll_controller) {
665 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
666 np->name, np->dev_name);
667 goto release;
668 }
669
670 if (!netif_running(ndev)) {
671 unsigned long atmost, atleast;
672
673 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
674 np->name, np->dev_name);
675
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800676 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) {
678 printk(KERN_ERR "%s: failed to open %s\n",
679 np->name, np->dev_name);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800680 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto release;
682 }
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800683 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 atleast = jiffies + HZ/10;
686 atmost = jiffies + 4*HZ;
687 while (!netif_carrier_ok(ndev)) {
688 if (time_after(jiffies, atmost)) {
689 printk(KERN_NOTICE
690 "%s: timeout waiting for carrier\n",
691 np->name);
692 break;
693 }
694 cond_resched();
695 }
696
697 /* If carrier appears to come up instantly, we don't
698 * trust it and pause so that we don't pump all our
699 * queued console messages into the bitbucket.
700 */
701
702 if (time_before(jiffies, atleast)) {
703 printk(KERN_NOTICE "%s: carrier detect appears"
704 " untrustworthy, waiting 4 seconds\n",
705 np->name);
706 msleep(4000);
707 }
708 }
709
Kris Katterjohn38602882006-01-17 15:15:38 -0800710 if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 memcpy(np->local_mac, ndev->dev_addr, 6);
712
713 if (!np->local_ip) {
714 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700715 in_dev = __in_dev_get_rcu(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (!in_dev || !in_dev->ifa_list) {
718 rcu_read_unlock();
719 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
720 np->name, np->dev_name);
721 goto release;
722 }
723
724 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
725 rcu_read_unlock();
726 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
727 np->name, HIPQUAD(np->local_ip));
728 }
729
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700730 if (np->rx_hook) {
731 spin_lock_irqsave(&npinfo->rx_lock, flags);
732 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
733 npinfo->rx_np = np;
734 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
735 }
Ingo Molnar26520762005-08-11 19:26:42 -0700736
737 /* fill up the skb queue */
738 refill_skbs();
739
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700740 /* last thing to do is link it to the net device structure */
Jeff Moyer115c1d62005-06-22 22:05:31 -0700741 ndev->npinfo = npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Matt Mackall53fb95d2005-08-11 19:27:43 -0700743 /* avoid racing with NAPI reading npinfo */
744 synchronize_rcu();
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747
748 release:
Jeff Moyer115c1d62005-06-22 22:05:31 -0700749 if (!ndev->npinfo)
750 kfree(npinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 np->dev = NULL;
752 dev_put(ndev);
753 return -1;
754}
755
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800756static int __init netpoll_init(void) {
757 skb_queue_head_init(&skb_pool);
758 return 0;
759}
760core_initcall(netpoll_init);
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762void netpoll_cleanup(struct netpoll *np)
763{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700764 struct netpoll_info *npinfo;
765 unsigned long flags;
766
Jeff Moyer115c1d62005-06-22 22:05:31 -0700767 if (np->dev) {
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700768 npinfo = np->dev->npinfo;
769 if (npinfo && npinfo->rx_np == np) {
770 spin_lock_irqsave(&npinfo->rx_lock, flags);
771 npinfo->rx_np = NULL;
772 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
773 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
774 }
Jeff Moyer115c1d62005-06-22 22:05:31 -0700775 dev_put(np->dev);
776 }
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 np->dev = NULL;
779}
780
781int netpoll_trap(void)
782{
783 return atomic_read(&trapped);
784}
785
786void netpoll_set_trap(int trap)
787{
788 if (trap)
789 atomic_inc(&trapped);
790 else
791 atomic_dec(&trapped);
792}
793
794EXPORT_SYMBOL(netpoll_set_trap);
795EXPORT_SYMBOL(netpoll_trap);
796EXPORT_SYMBOL(netpoll_parse_options);
797EXPORT_SYMBOL(netpoll_setup);
798EXPORT_SYMBOL(netpoll_cleanup);
799EXPORT_SYMBOL(netpoll_send_udp);
800EXPORT_SYMBOL(netpoll_poll);
801EXPORT_SYMBOL(netpoll_queue);