blob: ac4e8b8f57d1d603840997e1d92ca419ce67bde5 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static atomic_t trapped;
42
43#define NETPOLL_RX_ENABLED 1
44#define NETPOLL_RX_DROP 2
45
46#define MAX_SKB_SIZE \
47 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
48 sizeof(struct iphdr) + sizeof(struct ethhdr))
49
50static void zap_completion_queue(void);
Neil Horman068c6e92006-06-26 00:04:27 -070051static void arp_reply(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static void queue_process(void *p)
54{
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -070055 struct netpoll_info *npinfo = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct sk_buff *skb;
57
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -070058 while ((skb = skb_dequeue(&npinfo->txq)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -070061}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63void netpoll_queue(struct sk_buff *skb)
64{
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -070065 struct net_device *dev = skb->dev;
66 struct netpoll_info *npinfo = dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -070068 if (!npinfo)
69 kfree_skb(skb);
70 else {
71 skb_queue_tail(&npinfo->txq, skb);
72 schedule_work(&npinfo->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76static int checksum_udp(struct sk_buff *skb, struct udphdr *uh,
77 unsigned short ulen, u32 saddr, u32 daddr)
78{
Herbert Xufb286bb2005-11-10 13:01:24 -080079 unsigned int psum;
80
81 if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83
Herbert Xufb286bb2005-11-10 13:01:24 -080084 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Patrick McHardy84fa7932006-08-29 16:44:56 -070086 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Herbert Xufb286bb2005-11-10 13:01:24 -080087 !(u16)csum_fold(csum_add(psum, skb->csum)))
88 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Herbert Xufb286bb2005-11-10 13:01:24 -080090 skb->csum = psum;
91
92 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95/*
96 * Check whether delayed processing was scheduled for our NIC. If so,
97 * we attempt to grab the poll lock and use ->poll() to pump the card.
98 * If this fails, either we've recursed in ->poll() or it's already
99 * running on another CPU.
100 *
101 * Note: we don't mask interrupts with this lock because we're using
102 * trylock here and interrupts are already disabled in the softirq
103 * case. Further, we test the poll_owner to avoid recursion on UP
104 * systems where the lock doesn't exist.
105 *
106 * In cases where there is bi-directional communications, reading only
107 * one message at a time can lead to packets being dropped by the
108 * network adapter, forcing superfluous retries and possibly timeouts.
109 * Thus, we set our budget to greater than 1.
110 */
111static void poll_napi(struct netpoll *np)
112{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700113 struct netpoll_info *npinfo = np->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int budget = 16;
115
116 if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
Jeff Moyer115c1d62005-06-22 22:05:31 -0700117 npinfo->poll_owner != smp_processor_id() &&
118 spin_trylock(&npinfo->poll_lock)) {
119 npinfo->rx_flags |= NETPOLL_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 atomic_inc(&trapped);
121
122 np->dev->poll(np->dev, &budget);
123
124 atomic_dec(&trapped);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700125 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
126 spin_unlock(&npinfo->poll_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128}
129
Neil Horman068c6e92006-06-26 00:04:27 -0700130static void service_arp_queue(struct netpoll_info *npi)
131{
132 struct sk_buff *skb;
133
134 if (unlikely(!npi))
135 return;
136
137 skb = skb_dequeue(&npi->arp_tx);
138
139 while (skb != NULL) {
140 arp_reply(skb);
141 skb = skb_dequeue(&npi->arp_tx);
142 }
143 return;
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146void netpoll_poll(struct netpoll *np)
147{
148 if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
149 return;
150
151 /* Process pending work on NIC */
152 np->dev->poll_controller(np->dev);
153 if (np->dev->poll)
154 poll_napi(np);
155
Neil Horman068c6e92006-06-26 00:04:27 -0700156 service_arp_queue(np->dev->npinfo);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 zap_completion_queue();
159}
160
161static void refill_skbs(void)
162{
163 struct sk_buff *skb;
164 unsigned long flags;
165
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800166 spin_lock_irqsave(&skb_pool.lock, flags);
167 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
169 if (!skb)
170 break;
171
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800172 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800174 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static void zap_completion_queue(void)
178{
179 unsigned long flags;
180 struct softnet_data *sd = &get_cpu_var(softnet_data);
181
182 if (sd->completion_queue) {
183 struct sk_buff *clist;
184
185 local_irq_save(flags);
186 clist = sd->completion_queue;
187 sd->completion_queue = NULL;
188 local_irq_restore(flags);
189
190 while (clist != NULL) {
191 struct sk_buff *skb = clist;
192 clist = clist->next;
193 if(skb->destructor)
194 dev_kfree_skb_any(skb); /* put this one back */
195 else
196 __kfree_skb(skb);
197 }
198 }
199
200 put_cpu_var(softnet_data);
201}
202
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800203static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800205 int count = 0;
206 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800209 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800213 if (!skb)
214 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if(!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800217 if (++count < 10) {
218 netpoll_poll(np);
219 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800221 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
224 atomic_set(&skb->users, 1);
225 skb_reserve(skb, reserve);
226 return skb;
227}
228
229static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
230{
231 int status;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700232 struct netpoll_info *npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Matt Mackallf0d34592005-08-11 19:25:11 -0700234 if (!np || !np->dev || !netif_running(np->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 __kfree_skb(skb);
236 return;
237 }
238
Jeff Moyer115c1d62005-06-22 22:05:31 -0700239 npinfo = np->dev->npinfo;
Matt Mackallf0d34592005-08-11 19:25:11 -0700240
241 /* avoid recursion */
Jeff Moyer115c1d62005-06-22 22:05:31 -0700242 if (npinfo->poll_owner == smp_processor_id() ||
243 np->dev->xmit_lock_owner == smp_processor_id()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (np->drop)
245 np->drop(skb);
246 else
247 __kfree_skb(skb);
248 return;
249 }
250
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700251 do {
252 npinfo->tries--;
Herbert Xu932ff272006-06-09 12:20:56 -0700253 netif_tx_lock(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Matt Mackallf0d34592005-08-11 19:25:11 -0700255 /*
256 * network drivers do not expect to be called if the queue is
257 * stopped.
258 */
Jeremy Fitzhardinge88348072006-06-26 00:03:40 -0700259 status = NETDEV_TX_BUSY;
260 if (!netif_queue_stopped(np->dev))
261 status = np->dev->hard_start_xmit(skb, np->dev);
Matt Mackallf0d34592005-08-11 19:25:11 -0700262
Herbert Xu932ff272006-06-09 12:20:56 -0700263 netif_tx_unlock(np->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Matt Mackallf0d34592005-08-11 19:25:11 -0700265 /* success */
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700266 if(!status) {
267 npinfo->tries = MAX_RETRIES; /* reset */
Matt Mackallf0d34592005-08-11 19:25:11 -0700268 return;
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Matt Mackallf0d34592005-08-11 19:25:11 -0700271 /* transmit busy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 netpoll_poll(np);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700273 udelay(50);
274 } while (npinfo->tries > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
278{
279 int total_len, eth_len, ip_len, udp_len;
280 struct sk_buff *skb;
281 struct udphdr *udph;
282 struct iphdr *iph;
283 struct ethhdr *eth;
284
285 udp_len = len + sizeof(*udph);
286 ip_len = eth_len = udp_len + sizeof(*iph);
287 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
288
289 skb = find_skb(np, total_len, total_len - len);
290 if (!skb)
291 return;
292
293 memcpy(skb->data, msg, len);
294 skb->len += len;
295
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700296 skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 udph->source = htons(np->local_port);
298 udph->dest = htons(np->remote_port);
299 udph->len = htons(udp_len);
300 udph->check = 0;
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800301 udph->check = csum_tcpudp_magic(htonl(np->local_ip),
302 htonl(np->remote_ip),
303 udp_len, IPPROTO_UDP,
304 csum_partial((unsigned char *)udph, udp_len, 0));
305 if (udph->check == 0)
306 udph->check = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700308 skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* iph->version = 4; iph->ihl = 5; */
311 put_unaligned(0x45, (unsigned char *)iph);
312 iph->tos = 0;
313 put_unaligned(htons(ip_len), &(iph->tot_len));
314 iph->id = 0;
315 iph->frag_off = 0;
316 iph->ttl = 64;
317 iph->protocol = IPPROTO_UDP;
318 iph->check = 0;
319 put_unaligned(htonl(np->local_ip), &(iph->saddr));
320 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
321 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
322
323 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700324 skb->mac.raw = skb->data;
325 skb->protocol = eth->h_proto = htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 memcpy(eth->h_source, np->local_mac, 6);
327 memcpy(eth->h_dest, np->remote_mac, 6);
328
329 skb->dev = np->dev;
330
331 netpoll_send_skb(np, skb);
332}
333
334static void arp_reply(struct sk_buff *skb)
335{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700336 struct netpoll_info *npinfo = skb->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct arphdr *arp;
338 unsigned char *arp_ptr;
339 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
340 u32 sip, tip;
341 struct sk_buff *send_skb;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700342 struct netpoll *np = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700344 if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
345 np = npinfo->rx_np;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700346 if (!np)
347 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /* No arp on this interface */
350 if (skb->dev->flags & IFF_NOARP)
351 return;
352
353 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
354 (2 * skb->dev->addr_len) +
355 (2 * sizeof(u32)))))
356 return;
357
358 skb->h.raw = skb->nh.raw = skb->data;
359 arp = skb->nh.arph;
360
361 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
362 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
363 arp->ar_pro != htons(ETH_P_IP) ||
364 arp->ar_op != htons(ARPOP_REQUEST))
365 return;
366
367 arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
368 memcpy(&sip, arp_ptr, 4);
369 arp_ptr += 4 + skb->dev->addr_len;
370 memcpy(&tip, arp_ptr, 4);
371
372 /* Should we ignore arp? */
373 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
374 return;
375
376 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
377 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
378 LL_RESERVED_SPACE(np->dev));
379
380 if (!send_skb)
381 return;
382
383 send_skb->nh.raw = send_skb->data;
384 arp = (struct arphdr *) skb_put(send_skb, size);
385 send_skb->dev = skb->dev;
386 send_skb->protocol = htons(ETH_P_ARP);
387
388 /* Fill the device header for the ARP frame */
389
390 if (np->dev->hard_header &&
391 np->dev->hard_header(send_skb, skb->dev, ptype,
392 np->remote_mac, np->local_mac,
393 send_skb->len) < 0) {
394 kfree_skb(send_skb);
395 return;
396 }
397
398 /*
399 * Fill out the arp protocol part.
400 *
401 * we only support ethernet device type,
402 * which (according to RFC 1390) should always equal 1 (Ethernet).
403 */
404
405 arp->ar_hrd = htons(np->dev->type);
406 arp->ar_pro = htons(ETH_P_IP);
407 arp->ar_hln = np->dev->addr_len;
408 arp->ar_pln = 4;
409 arp->ar_op = htons(type);
410
411 arp_ptr=(unsigned char *)(arp + 1);
412 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
413 arp_ptr += np->dev->addr_len;
414 memcpy(arp_ptr, &tip, 4);
415 arp_ptr += 4;
416 memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
417 arp_ptr += np->dev->addr_len;
418 memcpy(arp_ptr, &sip, 4);
419
420 netpoll_send_skb(np, send_skb);
421}
422
423int __netpoll_rx(struct sk_buff *skb)
424{
425 int proto, len, ulen;
426 struct iphdr *iph;
427 struct udphdr *uh;
Neil Horman068c6e92006-06-26 00:04:27 -0700428 struct netpoll_info *npi = skb->dev->npinfo;
429 struct netpoll *np = npi->rx_np;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700432 if (!np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto out;
434 if (skb->dev->type != ARPHRD_ETHER)
435 goto out;
436
437 /* check if netpoll clients need ARP */
438 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
439 atomic_read(&trapped)) {
Neil Horman068c6e92006-06-26 00:04:27 -0700440 skb_queue_tail(&npi->arp_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 1;
442 }
443
444 proto = ntohs(eth_hdr(skb)->h_proto);
445 if (proto != ETH_P_IP)
446 goto out;
447 if (skb->pkt_type == PACKET_OTHERHOST)
448 goto out;
449 if (skb_shared(skb))
450 goto out;
451
452 iph = (struct iphdr *)skb->data;
453 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
454 goto out;
455 if (iph->ihl < 5 || iph->version != 4)
456 goto out;
457 if (!pskb_may_pull(skb, iph->ihl*4))
458 goto out;
459 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
460 goto out;
461
462 len = ntohs(iph->tot_len);
463 if (skb->len < len || len < iph->ihl*4)
464 goto out;
465
466 if (iph->protocol != IPPROTO_UDP)
467 goto out;
468
469 len -= iph->ihl*4;
470 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
471 ulen = ntohs(uh->len);
472
473 if (ulen != len)
474 goto out;
Herbert Xufb286bb2005-11-10 13:01:24 -0800475 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto out;
477 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
478 goto out;
479 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
480 goto out;
481 if (np->local_port && np->local_port != ntohs(uh->dest))
482 goto out;
483
484 np->rx_hook(np, ntohs(uh->source),
485 (char *)(uh+1),
486 ulen - sizeof(struct udphdr));
487
488 kfree_skb(skb);
489 return 1;
490
491out:
492 if (atomic_read(&trapped)) {
493 kfree_skb(skb);
494 return 1;
495 }
496
497 return 0;
498}
499
500int netpoll_parse_options(struct netpoll *np, char *opt)
501{
502 char *cur=opt, *delim;
503
504 if(*cur != '@') {
505 if ((delim = strchr(cur, '@')) == NULL)
506 goto parse_failed;
507 *delim=0;
508 np->local_port=simple_strtol(cur, NULL, 10);
509 cur=delim;
510 }
511 cur++;
512 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
513
514 if(*cur != '/') {
515 if ((delim = strchr(cur, '/')) == NULL)
516 goto parse_failed;
517 *delim=0;
518 np->local_ip=ntohl(in_aton(cur));
519 cur=delim;
520
521 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
522 np->name, HIPQUAD(np->local_ip));
523 }
524 cur++;
525
526 if ( *cur != ',') {
527 /* parse out dev name */
528 if ((delim = strchr(cur, ',')) == NULL)
529 goto parse_failed;
530 *delim=0;
531 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
532 cur=delim;
533 }
534 cur++;
535
536 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
537
538 if ( *cur != '@' ) {
539 /* dst port */
540 if ((delim = strchr(cur, '@')) == NULL)
541 goto parse_failed;
542 *delim=0;
543 np->remote_port=simple_strtol(cur, NULL, 10);
544 cur=delim;
545 }
546 cur++;
547 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
548
549 /* dst ip */
550 if ((delim = strchr(cur, '/')) == NULL)
551 goto parse_failed;
552 *delim=0;
553 np->remote_ip=ntohl(in_aton(cur));
554 cur=delim+1;
555
556 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
557 np->name, HIPQUAD(np->remote_ip));
558
559 if( *cur != 0 )
560 {
561 /* MAC address */
562 if ((delim = strchr(cur, ':')) == NULL)
563 goto parse_failed;
564 *delim=0;
565 np->remote_mac[0]=simple_strtol(cur, NULL, 16);
566 cur=delim+1;
567 if ((delim = strchr(cur, ':')) == NULL)
568 goto parse_failed;
569 *delim=0;
570 np->remote_mac[1]=simple_strtol(cur, NULL, 16);
571 cur=delim+1;
572 if ((delim = strchr(cur, ':')) == NULL)
573 goto parse_failed;
574 *delim=0;
575 np->remote_mac[2]=simple_strtol(cur, NULL, 16);
576 cur=delim+1;
577 if ((delim = strchr(cur, ':')) == NULL)
578 goto parse_failed;
579 *delim=0;
580 np->remote_mac[3]=simple_strtol(cur, NULL, 16);
581 cur=delim+1;
582 if ((delim = strchr(cur, ':')) == NULL)
583 goto parse_failed;
584 *delim=0;
585 np->remote_mac[4]=simple_strtol(cur, NULL, 16);
586 cur=delim+1;
587 np->remote_mac[5]=simple_strtol(cur, NULL, 16);
588 }
589
590 printk(KERN_INFO "%s: remote ethernet address "
591 "%02x:%02x:%02x:%02x:%02x:%02x\n",
592 np->name,
593 np->remote_mac[0],
594 np->remote_mac[1],
595 np->remote_mac[2],
596 np->remote_mac[3],
597 np->remote_mac[4],
598 np->remote_mac[5]);
599
600 return 0;
601
602 parse_failed:
603 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
604 np->name, cur);
605 return -1;
606}
607
608int netpoll_setup(struct netpoll *np)
609{
610 struct net_device *ndev = NULL;
611 struct in_device *in_dev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700612 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700613 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (np->dev_name)
616 ndev = dev_get_by_name(np->dev_name);
617 if (!ndev) {
618 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
619 np->name, np->dev_name);
620 return -1;
621 }
622
623 np->dev = ndev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700624 if (!ndev->npinfo) {
625 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
626 if (!npinfo)
627 goto release;
628
Jeff Moyer11513122005-08-11 19:23:04 -0700629 npinfo->rx_flags = 0;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700630 npinfo->rx_np = NULL;
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700631 spin_lock_init(&npinfo->poll_lock);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700632 npinfo->poll_owner = -1;
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700633 npinfo->tries = MAX_RETRIES;
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700634 spin_lock_init(&npinfo->rx_lock);
Neil Horman068c6e92006-06-26 00:04:27 -0700635 skb_queue_head_init(&npinfo->arp_tx);
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -0700636 skb_queue_head_init(&npinfo->txq);
637 INIT_WORK(&npinfo->tx_work, queue_process, npinfo);
638
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700639 atomic_set(&npinfo->refcnt, 1);
640 } else {
Jeff Moyer115c1d62005-06-22 22:05:31 -0700641 npinfo = ndev->npinfo;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700642 atomic_inc(&npinfo->refcnt);
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (!ndev->poll_controller) {
646 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
647 np->name, np->dev_name);
648 goto release;
649 }
650
651 if (!netif_running(ndev)) {
652 unsigned long atmost, atleast;
653
654 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
655 np->name, np->dev_name);
656
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800657 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) {
659 printk(KERN_ERR "%s: failed to open %s\n",
660 np->name, np->dev_name);
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800661 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto release;
663 }
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800664 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 atleast = jiffies + HZ/10;
667 atmost = jiffies + 4*HZ;
668 while (!netif_carrier_ok(ndev)) {
669 if (time_after(jiffies, atmost)) {
670 printk(KERN_NOTICE
671 "%s: timeout waiting for carrier\n",
672 np->name);
673 break;
674 }
675 cond_resched();
676 }
677
678 /* If carrier appears to come up instantly, we don't
679 * trust it and pause so that we don't pump all our
680 * queued console messages into the bitbucket.
681 */
682
683 if (time_before(jiffies, atleast)) {
684 printk(KERN_NOTICE "%s: carrier detect appears"
685 " untrustworthy, waiting 4 seconds\n",
686 np->name);
687 msleep(4000);
688 }
689 }
690
Kris Katterjohn38602882006-01-17 15:15:38 -0800691 if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 memcpy(np->local_mac, ndev->dev_addr, 6);
693
694 if (!np->local_ip) {
695 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700696 in_dev = __in_dev_get_rcu(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (!in_dev || !in_dev->ifa_list) {
699 rcu_read_unlock();
700 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
701 np->name, np->dev_name);
702 goto release;
703 }
704
705 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
706 rcu_read_unlock();
707 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
708 np->name, HIPQUAD(np->local_ip));
709 }
710
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700711 if (np->rx_hook) {
712 spin_lock_irqsave(&npinfo->rx_lock, flags);
713 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
714 npinfo->rx_np = np;
715 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
716 }
Ingo Molnar26520762005-08-11 19:26:42 -0700717
718 /* fill up the skb queue */
719 refill_skbs();
720
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700721 /* last thing to do is link it to the net device structure */
Jeff Moyer115c1d62005-06-22 22:05:31 -0700722 ndev->npinfo = npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Matt Mackall53fb95d2005-08-11 19:27:43 -0700724 /* avoid racing with NAPI reading npinfo */
725 synchronize_rcu();
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 0;
728
729 release:
Jeff Moyer115c1d62005-06-22 22:05:31 -0700730 if (!ndev->npinfo)
731 kfree(npinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 np->dev = NULL;
733 dev_put(ndev);
734 return -1;
735}
736
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800737static int __init netpoll_init(void) {
738 skb_queue_head_init(&skb_pool);
739 return 0;
740}
741core_initcall(netpoll_init);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743void netpoll_cleanup(struct netpoll *np)
744{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700745 struct netpoll_info *npinfo;
746 unsigned long flags;
747
Jeff Moyer115c1d62005-06-22 22:05:31 -0700748 if (np->dev) {
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700749 npinfo = np->dev->npinfo;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700750 if (npinfo) {
751 if (npinfo->rx_np == np) {
752 spin_lock_irqsave(&npinfo->rx_lock, flags);
753 npinfo->rx_np = NULL;
754 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
755 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
756 }
757
758 np->dev->npinfo = NULL;
759 if (atomic_dec_and_test(&npinfo->refcnt)) {
760 skb_queue_purge(&npinfo->arp_tx);
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -0700761 skb_queue_purge(&npinfo->txq);
762 flush_scheduled_work();
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700763
764 kfree(npinfo);
765 }
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700766 }
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700767
Jeff Moyer115c1d62005-06-22 22:05:31 -0700768 dev_put(np->dev);
769 }
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 np->dev = NULL;
772}
773
774int netpoll_trap(void)
775{
776 return atomic_read(&trapped);
777}
778
779void netpoll_set_trap(int trap)
780{
781 if (trap)
782 atomic_inc(&trapped);
783 else
784 atomic_dec(&trapped);
785}
786
787EXPORT_SYMBOL(netpoll_set_trap);
788EXPORT_SYMBOL(netpoll_trap);
789EXPORT_SYMBOL(netpoll_parse_options);
790EXPORT_SYMBOL(netpoll_setup);
791EXPORT_SYMBOL(netpoll_cleanup);
792EXPORT_SYMBOL(netpoll_send_udp);
793EXPORT_SYMBOL(netpoll_poll);
794EXPORT_SYMBOL(netpoll_queue);