Commit | Line | Data |
---|---|---|
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 | ||
12 | #include <linux/smp_lock.h> | |
13 | #include <linux/netdevice.h> | |
14 | #include <linux/etherdevice.h> | |
15 | #include <linux/string.h> | |
16 | #include <linux/inetdevice.h> | |
17 | #include <linux/inet.h> | |
18 | #include <linux/interrupt.h> | |
19 | #include <linux/netpoll.h> | |
20 | #include <linux/sched.h> | |
21 | #include <linux/delay.h> | |
22 | #include <linux/rcupdate.h> | |
23 | #include <linux/workqueue.h> | |
24 | #include <net/tcp.h> | |
25 | #include <net/udp.h> | |
26 | #include <asm/unaligned.h> | |
27 | ||
28 | /* | |
29 | * We maintain a small pool of fully-sized skbs, to make sure the | |
30 | * message gets out even in extreme OOM situations. | |
31 | */ | |
32 | ||
33 | #define MAX_UDP_CHUNK 1460 | |
34 | #define MAX_SKBS 32 | |
35 | #define MAX_QUEUE_DEPTH (MAX_SKBS / 2) | |
36 | ||
37 | static DEFINE_SPINLOCK(skb_list_lock); | |
38 | static int nr_skbs; | |
39 | static struct sk_buff *skbs; | |
40 | ||
41 | static DEFINE_SPINLOCK(queue_lock); | |
42 | static int queue_depth; | |
43 | static struct sk_buff *queue_head, *queue_tail; | |
44 | ||
45 | static 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 | ||
54 | static void zap_completion_queue(void); | |
55 | ||
56 | static void queue_process(void *p) | |
57 | { | |
58 | unsigned long flags; | |
59 | struct sk_buff *skb; | |
60 | ||
61 | while (queue_head) { | |
62 | spin_lock_irqsave(&queue_lock, flags); | |
63 | ||
64 | skb = queue_head; | |
65 | queue_head = skb->next; | |
66 | if (skb == queue_tail) | |
67 | queue_head = NULL; | |
68 | ||
69 | queue_depth--; | |
70 | ||
71 | spin_unlock_irqrestore(&queue_lock, flags); | |
72 | ||
73 | dev_queue_xmit(skb); | |
74 | } | |
75 | } | |
76 | ||
77 | static DECLARE_WORK(send_queue, queue_process, NULL); | |
78 | ||
79 | void netpoll_queue(struct sk_buff *skb) | |
80 | { | |
81 | unsigned long flags; | |
82 | ||
83 | if (queue_depth == MAX_QUEUE_DEPTH) { | |
84 | __kfree_skb(skb); | |
85 | return; | |
86 | } | |
87 | ||
88 | spin_lock_irqsave(&queue_lock, flags); | |
89 | if (!queue_head) | |
90 | queue_head = skb; | |
91 | else | |
92 | queue_tail->next = skb; | |
93 | queue_tail = skb; | |
94 | queue_depth++; | |
95 | spin_unlock_irqrestore(&queue_lock, flags); | |
96 | ||
97 | schedule_work(&send_queue); | |
98 | } | |
99 | ||
100 | static int checksum_udp(struct sk_buff *skb, struct udphdr *uh, | |
101 | unsigned short ulen, u32 saddr, u32 daddr) | |
102 | { | |
103 | if (uh->check == 0) | |
104 | return 0; | |
105 | ||
106 | if (skb->ip_summed == CHECKSUM_HW) | |
107 | return csum_tcpudp_magic( | |
108 | saddr, daddr, ulen, IPPROTO_UDP, skb->csum); | |
109 | ||
110 | skb->csum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0); | |
111 | ||
112 | return csum_fold(skb_checksum(skb, 0, skb->len, skb->csum)); | |
113 | } | |
114 | ||
115 | /* | |
116 | * Check whether delayed processing was scheduled for our NIC. If so, | |
117 | * we attempt to grab the poll lock and use ->poll() to pump the card. | |
118 | * If this fails, either we've recursed in ->poll() or it's already | |
119 | * running on another CPU. | |
120 | * | |
121 | * Note: we don't mask interrupts with this lock because we're using | |
122 | * trylock here and interrupts are already disabled in the softirq | |
123 | * case. Further, we test the poll_owner to avoid recursion on UP | |
124 | * systems where the lock doesn't exist. | |
125 | * | |
126 | * In cases where there is bi-directional communications, reading only | |
127 | * one message at a time can lead to packets being dropped by the | |
128 | * network adapter, forcing superfluous retries and possibly timeouts. | |
129 | * Thus, we set our budget to greater than 1. | |
130 | */ | |
131 | static void poll_napi(struct netpoll *np) | |
132 | { | |
115c1d6e | 133 | struct netpoll_info *npinfo = np->dev->npinfo; |
1da177e4 LT |
134 | int budget = 16; |
135 | ||
136 | if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) && | |
115c1d6e JM |
137 | npinfo->poll_owner != smp_processor_id() && |
138 | spin_trylock(&npinfo->poll_lock)) { | |
139 | npinfo->rx_flags |= NETPOLL_RX_DROP; | |
1da177e4 LT |
140 | atomic_inc(&trapped); |
141 | ||
142 | np->dev->poll(np->dev, &budget); | |
143 | ||
144 | atomic_dec(&trapped); | |
115c1d6e JM |
145 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
146 | spin_unlock(&npinfo->poll_lock); | |
1da177e4 LT |
147 | } |
148 | } | |
149 | ||
150 | void netpoll_poll(struct netpoll *np) | |
151 | { | |
152 | if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller) | |
153 | return; | |
154 | ||
155 | /* Process pending work on NIC */ | |
156 | np->dev->poll_controller(np->dev); | |
157 | if (np->dev->poll) | |
158 | poll_napi(np); | |
159 | ||
160 | zap_completion_queue(); | |
161 | } | |
162 | ||
163 | static void refill_skbs(void) | |
164 | { | |
165 | struct sk_buff *skb; | |
166 | unsigned long flags; | |
167 | ||
168 | spin_lock_irqsave(&skb_list_lock, flags); | |
169 | while (nr_skbs < MAX_SKBS) { | |
170 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); | |
171 | if (!skb) | |
172 | break; | |
173 | ||
174 | skb->next = skbs; | |
175 | skbs = skb; | |
176 | nr_skbs++; | |
177 | } | |
178 | spin_unlock_irqrestore(&skb_list_lock, flags); | |
179 | } | |
180 | ||
181 | static void zap_completion_queue(void) | |
182 | { | |
183 | unsigned long flags; | |
184 | struct softnet_data *sd = &get_cpu_var(softnet_data); | |
185 | ||
186 | if (sd->completion_queue) { | |
187 | struct sk_buff *clist; | |
188 | ||
189 | local_irq_save(flags); | |
190 | clist = sd->completion_queue; | |
191 | sd->completion_queue = NULL; | |
192 | local_irq_restore(flags); | |
193 | ||
194 | while (clist != NULL) { | |
195 | struct sk_buff *skb = clist; | |
196 | clist = clist->next; | |
197 | if(skb->destructor) | |
198 | dev_kfree_skb_any(skb); /* put this one back */ | |
199 | else | |
200 | __kfree_skb(skb); | |
201 | } | |
202 | } | |
203 | ||
204 | put_cpu_var(softnet_data); | |
205 | } | |
206 | ||
207 | static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve) | |
208 | { | |
209 | int once = 1, count = 0; | |
210 | unsigned long flags; | |
211 | struct sk_buff *skb = NULL; | |
212 | ||
213 | zap_completion_queue(); | |
214 | repeat: | |
215 | if (nr_skbs < MAX_SKBS) | |
216 | refill_skbs(); | |
217 | ||
218 | skb = alloc_skb(len, GFP_ATOMIC); | |
219 | ||
220 | if (!skb) { | |
221 | spin_lock_irqsave(&skb_list_lock, flags); | |
222 | skb = skbs; | |
223 | if (skb) { | |
224 | skbs = skb->next; | |
225 | skb->next = NULL; | |
226 | nr_skbs--; | |
227 | } | |
228 | spin_unlock_irqrestore(&skb_list_lock, flags); | |
229 | } | |
230 | ||
231 | if(!skb) { | |
232 | count++; | |
233 | if (once && (count == 1000000)) { | |
234 | printk("out of netpoll skbs!\n"); | |
235 | once = 0; | |
236 | } | |
237 | netpoll_poll(np); | |
238 | goto repeat; | |
239 | } | |
240 | ||
241 | atomic_set(&skb->users, 1); | |
242 | skb_reserve(skb, reserve); | |
243 | return skb; | |
244 | } | |
245 | ||
246 | static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) | |
247 | { | |
248 | int status; | |
115c1d6e | 249 | struct netpoll_info *npinfo; |
1da177e4 LT |
250 | |
251 | repeat: | |
252 | if(!np || !np->dev || !netif_running(np->dev)) { | |
253 | __kfree_skb(skb); | |
254 | return; | |
255 | } | |
256 | ||
257 | /* avoid recursion */ | |
115c1d6e JM |
258 | npinfo = np->dev->npinfo; |
259 | if (npinfo->poll_owner == smp_processor_id() || | |
260 | np->dev->xmit_lock_owner == smp_processor_id()) { | |
1da177e4 LT |
261 | if (np->drop) |
262 | np->drop(skb); | |
263 | else | |
264 | __kfree_skb(skb); | |
265 | return; | |
266 | } | |
267 | ||
268 | spin_lock(&np->dev->xmit_lock); | |
269 | np->dev->xmit_lock_owner = smp_processor_id(); | |
270 | ||
271 | /* | |
272 | * network drivers do not expect to be called if the queue is | |
273 | * stopped. | |
274 | */ | |
275 | if (netif_queue_stopped(np->dev)) { | |
276 | np->dev->xmit_lock_owner = -1; | |
277 | spin_unlock(&np->dev->xmit_lock); | |
278 | ||
279 | netpoll_poll(np); | |
280 | goto repeat; | |
281 | } | |
282 | ||
283 | status = np->dev->hard_start_xmit(skb, np->dev); | |
284 | np->dev->xmit_lock_owner = -1; | |
285 | spin_unlock(&np->dev->xmit_lock); | |
286 | ||
287 | /* transmit busy */ | |
288 | if(status) { | |
289 | netpoll_poll(np); | |
290 | goto repeat; | |
291 | } | |
292 | } | |
293 | ||
294 | void netpoll_send_udp(struct netpoll *np, const char *msg, int len) | |
295 | { | |
296 | int total_len, eth_len, ip_len, udp_len; | |
297 | struct sk_buff *skb; | |
298 | struct udphdr *udph; | |
299 | struct iphdr *iph; | |
300 | struct ethhdr *eth; | |
301 | ||
302 | udp_len = len + sizeof(*udph); | |
303 | ip_len = eth_len = udp_len + sizeof(*iph); | |
304 | total_len = eth_len + ETH_HLEN + NET_IP_ALIGN; | |
305 | ||
306 | skb = find_skb(np, total_len, total_len - len); | |
307 | if (!skb) | |
308 | return; | |
309 | ||
310 | memcpy(skb->data, msg, len); | |
311 | skb->len += len; | |
312 | ||
313 | udph = (struct udphdr *) skb_push(skb, sizeof(*udph)); | |
314 | udph->source = htons(np->local_port); | |
315 | udph->dest = htons(np->remote_port); | |
316 | udph->len = htons(udp_len); | |
317 | udph->check = 0; | |
318 | ||
319 | iph = (struct iphdr *)skb_push(skb, sizeof(*iph)); | |
320 | ||
321 | /* iph->version = 4; iph->ihl = 5; */ | |
322 | put_unaligned(0x45, (unsigned char *)iph); | |
323 | iph->tos = 0; | |
324 | put_unaligned(htons(ip_len), &(iph->tot_len)); | |
325 | iph->id = 0; | |
326 | iph->frag_off = 0; | |
327 | iph->ttl = 64; | |
328 | iph->protocol = IPPROTO_UDP; | |
329 | iph->check = 0; | |
330 | put_unaligned(htonl(np->local_ip), &(iph->saddr)); | |
331 | put_unaligned(htonl(np->remote_ip), &(iph->daddr)); | |
332 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); | |
333 | ||
334 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); | |
335 | ||
336 | eth->h_proto = htons(ETH_P_IP); | |
337 | memcpy(eth->h_source, np->local_mac, 6); | |
338 | memcpy(eth->h_dest, np->remote_mac, 6); | |
339 | ||
340 | skb->dev = np->dev; | |
341 | ||
342 | netpoll_send_skb(np, skb); | |
343 | } | |
344 | ||
345 | static void arp_reply(struct sk_buff *skb) | |
346 | { | |
115c1d6e | 347 | struct netpoll_info *npinfo = skb->dev->npinfo; |
1da177e4 LT |
348 | struct arphdr *arp; |
349 | unsigned char *arp_ptr; | |
350 | int size, type = ARPOP_REPLY, ptype = ETH_P_ARP; | |
351 | u32 sip, tip; | |
fbeec2e1 | 352 | unsigned long flags; |
1da177e4 | 353 | struct sk_buff *send_skb; |
115c1d6e | 354 | struct netpoll *np = NULL; |
1da177e4 | 355 | |
fbeec2e1 JM |
356 | if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev) |
357 | np = npinfo->rx_np; | |
115c1d6e JM |
358 | if (!np) |
359 | return; | |
1da177e4 LT |
360 | |
361 | /* No arp on this interface */ | |
362 | if (skb->dev->flags & IFF_NOARP) | |
363 | return; | |
364 | ||
365 | if (!pskb_may_pull(skb, (sizeof(struct arphdr) + | |
366 | (2 * skb->dev->addr_len) + | |
367 | (2 * sizeof(u32))))) | |
368 | return; | |
369 | ||
370 | skb->h.raw = skb->nh.raw = skb->data; | |
371 | arp = skb->nh.arph; | |
372 | ||
373 | if ((arp->ar_hrd != htons(ARPHRD_ETHER) && | |
374 | arp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
375 | arp->ar_pro != htons(ETH_P_IP) || | |
376 | arp->ar_op != htons(ARPOP_REQUEST)) | |
377 | return; | |
378 | ||
379 | arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len; | |
380 | memcpy(&sip, arp_ptr, 4); | |
381 | arp_ptr += 4 + skb->dev->addr_len; | |
382 | memcpy(&tip, arp_ptr, 4); | |
383 | ||
384 | /* Should we ignore arp? */ | |
385 | if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip)) | |
386 | return; | |
387 | ||
388 | size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4); | |
389 | send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev), | |
390 | LL_RESERVED_SPACE(np->dev)); | |
391 | ||
392 | if (!send_skb) | |
393 | return; | |
394 | ||
395 | send_skb->nh.raw = send_skb->data; | |
396 | arp = (struct arphdr *) skb_put(send_skb, size); | |
397 | send_skb->dev = skb->dev; | |
398 | send_skb->protocol = htons(ETH_P_ARP); | |
399 | ||
400 | /* Fill the device header for the ARP frame */ | |
401 | ||
402 | if (np->dev->hard_header && | |
403 | np->dev->hard_header(send_skb, skb->dev, ptype, | |
404 | np->remote_mac, np->local_mac, | |
405 | send_skb->len) < 0) { | |
406 | kfree_skb(send_skb); | |
407 | return; | |
408 | } | |
409 | ||
410 | /* | |
411 | * Fill out the arp protocol part. | |
412 | * | |
413 | * we only support ethernet device type, | |
414 | * which (according to RFC 1390) should always equal 1 (Ethernet). | |
415 | */ | |
416 | ||
417 | arp->ar_hrd = htons(np->dev->type); | |
418 | arp->ar_pro = htons(ETH_P_IP); | |
419 | arp->ar_hln = np->dev->addr_len; | |
420 | arp->ar_pln = 4; | |
421 | arp->ar_op = htons(type); | |
422 | ||
423 | arp_ptr=(unsigned char *)(arp + 1); | |
424 | memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len); | |
425 | arp_ptr += np->dev->addr_len; | |
426 | memcpy(arp_ptr, &tip, 4); | |
427 | arp_ptr += 4; | |
428 | memcpy(arp_ptr, np->remote_mac, np->dev->addr_len); | |
429 | arp_ptr += np->dev->addr_len; | |
430 | memcpy(arp_ptr, &sip, 4); | |
431 | ||
432 | netpoll_send_skb(np, send_skb); | |
433 | } | |
434 | ||
435 | int __netpoll_rx(struct sk_buff *skb) | |
436 | { | |
437 | int proto, len, ulen; | |
438 | struct iphdr *iph; | |
439 | struct udphdr *uh; | |
fbeec2e1 | 440 | struct netpoll *np = skb->dev->npinfo->rx_np; |
1da177e4 | 441 | |
fbeec2e1 | 442 | if (!np) |
1da177e4 LT |
443 | goto out; |
444 | if (skb->dev->type != ARPHRD_ETHER) | |
445 | goto out; | |
446 | ||
447 | /* check if netpoll clients need ARP */ | |
448 | if (skb->protocol == __constant_htons(ETH_P_ARP) && | |
449 | atomic_read(&trapped)) { | |
450 | arp_reply(skb); | |
451 | return 1; | |
452 | } | |
453 | ||
454 | proto = ntohs(eth_hdr(skb)->h_proto); | |
455 | if (proto != ETH_P_IP) | |
456 | goto out; | |
457 | if (skb->pkt_type == PACKET_OTHERHOST) | |
458 | goto out; | |
459 | if (skb_shared(skb)) | |
460 | goto out; | |
461 | ||
462 | iph = (struct iphdr *)skb->data; | |
463 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) | |
464 | goto out; | |
465 | if (iph->ihl < 5 || iph->version != 4) | |
466 | goto out; | |
467 | if (!pskb_may_pull(skb, iph->ihl*4)) | |
468 | goto out; | |
469 | if (ip_fast_csum((u8 *)iph, iph->ihl) != 0) | |
470 | goto out; | |
471 | ||
472 | len = ntohs(iph->tot_len); | |
473 | if (skb->len < len || len < iph->ihl*4) | |
474 | goto out; | |
475 | ||
476 | if (iph->protocol != IPPROTO_UDP) | |
477 | goto out; | |
478 | ||
479 | len -= iph->ihl*4; | |
480 | uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); | |
481 | ulen = ntohs(uh->len); | |
482 | ||
483 | if (ulen != len) | |
484 | goto out; | |
485 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr) < 0) | |
486 | goto out; | |
487 | if (np->local_ip && np->local_ip != ntohl(iph->daddr)) | |
488 | goto out; | |
489 | if (np->remote_ip && np->remote_ip != ntohl(iph->saddr)) | |
490 | goto out; | |
491 | if (np->local_port && np->local_port != ntohs(uh->dest)) | |
492 | goto out; | |
493 | ||
494 | np->rx_hook(np, ntohs(uh->source), | |
495 | (char *)(uh+1), | |
496 | ulen - sizeof(struct udphdr)); | |
497 | ||
498 | kfree_skb(skb); | |
499 | return 1; | |
500 | ||
501 | out: | |
502 | if (atomic_read(&trapped)) { | |
503 | kfree_skb(skb); | |
504 | return 1; | |
505 | } | |
506 | ||
507 | return 0; | |
508 | } | |
509 | ||
510 | int netpoll_parse_options(struct netpoll *np, char *opt) | |
511 | { | |
512 | char *cur=opt, *delim; | |
513 | ||
514 | if(*cur != '@') { | |
515 | if ((delim = strchr(cur, '@')) == NULL) | |
516 | goto parse_failed; | |
517 | *delim=0; | |
518 | np->local_port=simple_strtol(cur, NULL, 10); | |
519 | cur=delim; | |
520 | } | |
521 | cur++; | |
522 | printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port); | |
523 | ||
524 | if(*cur != '/') { | |
525 | if ((delim = strchr(cur, '/')) == NULL) | |
526 | goto parse_failed; | |
527 | *delim=0; | |
528 | np->local_ip=ntohl(in_aton(cur)); | |
529 | cur=delim; | |
530 | ||
531 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", | |
532 | np->name, HIPQUAD(np->local_ip)); | |
533 | } | |
534 | cur++; | |
535 | ||
536 | if ( *cur != ',') { | |
537 | /* parse out dev name */ | |
538 | if ((delim = strchr(cur, ',')) == NULL) | |
539 | goto parse_failed; | |
540 | *delim=0; | |
541 | strlcpy(np->dev_name, cur, sizeof(np->dev_name)); | |
542 | cur=delim; | |
543 | } | |
544 | cur++; | |
545 | ||
546 | printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name); | |
547 | ||
548 | if ( *cur != '@' ) { | |
549 | /* dst port */ | |
550 | if ((delim = strchr(cur, '@')) == NULL) | |
551 | goto parse_failed; | |
552 | *delim=0; | |
553 | np->remote_port=simple_strtol(cur, NULL, 10); | |
554 | cur=delim; | |
555 | } | |
556 | cur++; | |
557 | printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port); | |
558 | ||
559 | /* dst ip */ | |
560 | if ((delim = strchr(cur, '/')) == NULL) | |
561 | goto parse_failed; | |
562 | *delim=0; | |
563 | np->remote_ip=ntohl(in_aton(cur)); | |
564 | cur=delim+1; | |
565 | ||
566 | printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n", | |
567 | np->name, HIPQUAD(np->remote_ip)); | |
568 | ||
569 | if( *cur != 0 ) | |
570 | { | |
571 | /* MAC address */ | |
572 | if ((delim = strchr(cur, ':')) == NULL) | |
573 | goto parse_failed; | |
574 | *delim=0; | |
575 | np->remote_mac[0]=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[1]=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[2]=simple_strtol(cur, NULL, 16); | |
586 | cur=delim+1; | |
587 | if ((delim = strchr(cur, ':')) == NULL) | |
588 | goto parse_failed; | |
589 | *delim=0; | |
590 | np->remote_mac[3]=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[4]=simple_strtol(cur, NULL, 16); | |
596 | cur=delim+1; | |
597 | np->remote_mac[5]=simple_strtol(cur, NULL, 16); | |
598 | } | |
599 | ||
600 | printk(KERN_INFO "%s: remote ethernet address " | |
601 | "%02x:%02x:%02x:%02x:%02x:%02x\n", | |
602 | np->name, | |
603 | np->remote_mac[0], | |
604 | np->remote_mac[1], | |
605 | np->remote_mac[2], | |
606 | np->remote_mac[3], | |
607 | np->remote_mac[4], | |
608 | np->remote_mac[5]); | |
609 | ||
610 | return 0; | |
611 | ||
612 | parse_failed: | |
613 | printk(KERN_INFO "%s: couldn't parse config at %s!\n", | |
614 | np->name, cur); | |
615 | return -1; | |
616 | } | |
617 | ||
618 | int netpoll_setup(struct netpoll *np) | |
619 | { | |
620 | struct net_device *ndev = NULL; | |
621 | struct in_device *in_dev; | |
115c1d6e | 622 | struct netpoll_info *npinfo; |
fbeec2e1 | 623 | unsigned long flags; |
1da177e4 LT |
624 | |
625 | if (np->dev_name) | |
626 | ndev = dev_get_by_name(np->dev_name); | |
627 | if (!ndev) { | |
628 | printk(KERN_ERR "%s: %s doesn't exist, aborting.\n", | |
629 | np->name, np->dev_name); | |
630 | return -1; | |
631 | } | |
632 | ||
633 | np->dev = ndev; | |
115c1d6e JM |
634 | if (!ndev->npinfo) { |
635 | npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); | |
636 | if (!npinfo) | |
637 | goto release; | |
638 | ||
11513128 | 639 | npinfo->rx_flags = 0; |
fbeec2e1 | 640 | npinfo->rx_np = NULL; |
115c1d6e JM |
641 | npinfo->poll_lock = SPIN_LOCK_UNLOCKED; |
642 | npinfo->poll_owner = -1; | |
fbeec2e1 | 643 | npinfo->rx_lock = SPIN_LOCK_UNLOCKED; |
115c1d6e JM |
644 | } else |
645 | npinfo = ndev->npinfo; | |
1da177e4 LT |
646 | |
647 | if (!ndev->poll_controller) { | |
648 | printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n", | |
649 | np->name, np->dev_name); | |
650 | goto release; | |
651 | } | |
652 | ||
653 | if (!netif_running(ndev)) { | |
654 | unsigned long atmost, atleast; | |
655 | ||
656 | printk(KERN_INFO "%s: device %s not up yet, forcing it\n", | |
657 | np->name, np->dev_name); | |
658 | ||
659 | rtnl_shlock(); | |
660 | if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) { | |
661 | printk(KERN_ERR "%s: failed to open %s\n", | |
662 | np->name, np->dev_name); | |
663 | rtnl_shunlock(); | |
664 | goto release; | |
665 | } | |
666 | rtnl_shunlock(); | |
667 | ||
668 | atleast = jiffies + HZ/10; | |
669 | atmost = jiffies + 4*HZ; | |
670 | while (!netif_carrier_ok(ndev)) { | |
671 | if (time_after(jiffies, atmost)) { | |
672 | printk(KERN_NOTICE | |
673 | "%s: timeout waiting for carrier\n", | |
674 | np->name); | |
675 | break; | |
676 | } | |
677 | cond_resched(); | |
678 | } | |
679 | ||
680 | /* If carrier appears to come up instantly, we don't | |
681 | * trust it and pause so that we don't pump all our | |
682 | * queued console messages into the bitbucket. | |
683 | */ | |
684 | ||
685 | if (time_before(jiffies, atleast)) { | |
686 | printk(KERN_NOTICE "%s: carrier detect appears" | |
687 | " untrustworthy, waiting 4 seconds\n", | |
688 | np->name); | |
689 | msleep(4000); | |
690 | } | |
691 | } | |
692 | ||
693 | if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr) | |
694 | memcpy(np->local_mac, ndev->dev_addr, 6); | |
695 | ||
696 | if (!np->local_ip) { | |
697 | rcu_read_lock(); | |
698 | in_dev = __in_dev_get(ndev); | |
699 | ||
700 | if (!in_dev || !in_dev->ifa_list) { | |
701 | rcu_read_unlock(); | |
702 | printk(KERN_ERR "%s: no IP address for %s, aborting\n", | |
703 | np->name, np->dev_name); | |
704 | goto release; | |
705 | } | |
706 | ||
707 | np->local_ip = ntohl(in_dev->ifa_list->ifa_local); | |
708 | rcu_read_unlock(); | |
709 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", | |
710 | np->name, HIPQUAD(np->local_ip)); | |
711 | } | |
712 | ||
fbeec2e1 JM |
713 | if (np->rx_hook) { |
714 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
715 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; | |
716 | npinfo->rx_np = np; | |
717 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
718 | } | |
719 | /* last thing to do is link it to the net device structure */ | |
115c1d6e | 720 | ndev->npinfo = npinfo; |
1da177e4 LT |
721 | |
722 | return 0; | |
723 | ||
724 | release: | |
115c1d6e JM |
725 | if (!ndev->npinfo) |
726 | kfree(npinfo); | |
1da177e4 LT |
727 | np->dev = NULL; |
728 | dev_put(ndev); | |
729 | return -1; | |
730 | } | |
731 | ||
732 | void netpoll_cleanup(struct netpoll *np) | |
733 | { | |
fbeec2e1 JM |
734 | struct netpoll_info *npinfo; |
735 | unsigned long flags; | |
736 | ||
115c1d6e | 737 | if (np->dev) { |
fbeec2e1 JM |
738 | npinfo = np->dev->npinfo; |
739 | if (npinfo && npinfo->rx_np == np) { | |
740 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
741 | npinfo->rx_np = NULL; | |
742 | npinfo->rx_flags &= ~NETPOLL_RX_ENABLED; | |
743 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
744 | } | |
115c1d6e JM |
745 | dev_put(np->dev); |
746 | } | |
fbeec2e1 | 747 | |
1da177e4 LT |
748 | np->dev = NULL; |
749 | } | |
750 | ||
751 | int netpoll_trap(void) | |
752 | { | |
753 | return atomic_read(&trapped); | |
754 | } | |
755 | ||
756 | void netpoll_set_trap(int trap) | |
757 | { | |
758 | if (trap) | |
759 | atomic_inc(&trapped); | |
760 | else | |
761 | atomic_dec(&trapped); | |
762 | } | |
763 | ||
764 | EXPORT_SYMBOL(netpoll_set_trap); | |
765 | EXPORT_SYMBOL(netpoll_trap); | |
766 | EXPORT_SYMBOL(netpoll_parse_options); | |
767 | EXPORT_SYMBOL(netpoll_setup); | |
768 | EXPORT_SYMBOL(netpoll_cleanup); | |
769 | EXPORT_SYMBOL(netpoll_send_udp); | |
770 | EXPORT_SYMBOL(netpoll_poll); | |
771 | EXPORT_SYMBOL(netpoll_queue); |