]>
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> | |
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> | |
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) | |
0db1d6fc | 37 | #define MAX_RETRIES 20000 |
1da177e4 | 38 | |
a1bcfacd | 39 | static struct sk_buff_head skb_pool; |
1da177e4 LT |
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); | |
068c6e98 | 55 | static void arp_reply(struct sk_buff *skb); |
1da177e4 LT |
56 | |
57 | static 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 | ||
78 | static DECLARE_WORK(send_queue, queue_process, NULL); | |
79 | ||
80 | void 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 | ||
101 | static int checksum_udp(struct sk_buff *skb, struct udphdr *uh, | |
102 | unsigned short ulen, u32 saddr, u32 daddr) | |
103 | { | |
fb286bb2 HX |
104 | unsigned int psum; |
105 | ||
106 | if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY) | |
1da177e4 LT |
107 | return 0; |
108 | ||
fb286bb2 HX |
109 | psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0); |
110 | ||
84fa7933 | 111 | if (skb->ip_summed == CHECKSUM_COMPLETE && |
fb286bb2 HX |
112 | !(u16)csum_fold(csum_add(psum, skb->csum))) |
113 | return 0; | |
1da177e4 | 114 | |
fb286bb2 | 115 | skb->csum = psum; |
1da177e4 | 116 | |
fb286bb2 | 117 | return __skb_checksum_complete(skb); |
1da177e4 LT |
118 | } |
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 | */ | |
136 | static void poll_napi(struct netpoll *np) | |
137 | { | |
115c1d6e | 138 | struct netpoll_info *npinfo = np->dev->npinfo; |
1da177e4 LT |
139 | int budget = 16; |
140 | ||
141 | if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) && | |
115c1d6e JM |
142 | npinfo->poll_owner != smp_processor_id() && |
143 | spin_trylock(&npinfo->poll_lock)) { | |
144 | npinfo->rx_flags |= NETPOLL_RX_DROP; | |
1da177e4 LT |
145 | atomic_inc(&trapped); |
146 | ||
147 | np->dev->poll(np->dev, &budget); | |
148 | ||
149 | atomic_dec(&trapped); | |
115c1d6e JM |
150 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
151 | spin_unlock(&npinfo->poll_lock); | |
1da177e4 LT |
152 | } |
153 | } | |
154 | ||
068c6e98 NH |
155 | static 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 | ||
1da177e4 LT |
171 | void 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 | ||
068c6e98 NH |
181 | service_arp_queue(np->dev->npinfo); |
182 | ||
1da177e4 LT |
183 | zap_completion_queue(); |
184 | } | |
185 | ||
186 | static void refill_skbs(void) | |
187 | { | |
188 | struct sk_buff *skb; | |
189 | unsigned long flags; | |
190 | ||
a1bcfacd SH |
191 | spin_lock_irqsave(&skb_pool.lock, flags); |
192 | while (skb_pool.qlen < MAX_SKBS) { | |
1da177e4 LT |
193 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); |
194 | if (!skb) | |
195 | break; | |
196 | ||
a1bcfacd | 197 | __skb_queue_tail(&skb_pool, skb); |
1da177e4 | 198 | } |
a1bcfacd | 199 | spin_unlock_irqrestore(&skb_pool.lock, flags); |
1da177e4 LT |
200 | } |
201 | ||
202 | static 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 | ||
a1bcfacd | 228 | static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve) |
1da177e4 | 229 | { |
a1bcfacd SH |
230 | int count = 0; |
231 | struct sk_buff *skb; | |
1da177e4 LT |
232 | |
233 | zap_completion_queue(); | |
a1bcfacd | 234 | refill_skbs(); |
1da177e4 | 235 | repeat: |
1da177e4 LT |
236 | |
237 | skb = alloc_skb(len, GFP_ATOMIC); | |
a1bcfacd SH |
238 | if (!skb) |
239 | skb = skb_dequeue(&skb_pool); | |
1da177e4 LT |
240 | |
241 | if(!skb) { | |
a1bcfacd SH |
242 | if (++count < 10) { |
243 | netpoll_poll(np); | |
244 | goto repeat; | |
1da177e4 | 245 | } |
a1bcfacd | 246 | return NULL; |
1da177e4 LT |
247 | } |
248 | ||
249 | atomic_set(&skb->users, 1); | |
250 | skb_reserve(skb, reserve); | |
251 | return skb; | |
252 | } | |
253 | ||
254 | static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) | |
255 | { | |
256 | int status; | |
115c1d6e | 257 | struct netpoll_info *npinfo; |
1da177e4 | 258 | |
f0d3459d | 259 | if (!np || !np->dev || !netif_running(np->dev)) { |
1da177e4 LT |
260 | __kfree_skb(skb); |
261 | return; | |
262 | } | |
263 | ||
115c1d6e | 264 | npinfo = np->dev->npinfo; |
f0d3459d MM |
265 | |
266 | /* avoid recursion */ | |
115c1d6e JM |
267 | if (npinfo->poll_owner == smp_processor_id() || |
268 | np->dev->xmit_lock_owner == smp_processor_id()) { | |
1da177e4 LT |
269 | if (np->drop) |
270 | np->drop(skb); | |
271 | else | |
272 | __kfree_skb(skb); | |
273 | return; | |
274 | } | |
275 | ||
0db1d6fc MM |
276 | do { |
277 | npinfo->tries--; | |
932ff279 | 278 | netif_tx_lock(np->dev); |
f0d3459d MM |
279 | |
280 | /* | |
281 | * network drivers do not expect to be called if the queue is | |
282 | * stopped. | |
283 | */ | |
8834807b JF |
284 | status = NETDEV_TX_BUSY; |
285 | if (!netif_queue_stopped(np->dev)) | |
286 | status = np->dev->hard_start_xmit(skb, np->dev); | |
1da177e4 | 287 | |
932ff279 | 288 | netif_tx_unlock(np->dev); |
1da177e4 | 289 | |
f0d3459d | 290 | /* success */ |
0db1d6fc MM |
291 | if(!status) { |
292 | npinfo->tries = MAX_RETRIES; /* reset */ | |
f0d3459d | 293 | return; |
0db1d6fc | 294 | } |
1da177e4 | 295 | |
f0d3459d | 296 | /* transmit busy */ |
1da177e4 | 297 | netpoll_poll(np); |
0db1d6fc MM |
298 | udelay(50); |
299 | } while (npinfo->tries > 0); | |
1da177e4 LT |
300 | } |
301 | ||
302 | void 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 | ||
206daaf7 | 321 | skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph)); |
1da177e4 LT |
322 | udph->source = htons(np->local_port); |
323 | udph->dest = htons(np->remote_port); | |
324 | udph->len = htons(udp_len); | |
325 | udph->check = 0; | |
8e365eec CL |
326 | 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; | |
1da177e4 | 332 | |
206daaf7 | 333 | skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph)); |
1da177e4 LT |
334 | |
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); | |
206daaf7 SH |
349 | skb->mac.raw = skb->data; |
350 | skb->protocol = eth->h_proto = htons(ETH_P_IP); | |
1da177e4 LT |
351 | 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 | ||
359 | static void arp_reply(struct sk_buff *skb) | |
360 | { | |
115c1d6e | 361 | struct netpoll_info *npinfo = skb->dev->npinfo; |
1da177e4 LT |
362 | 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; | |
115c1d6e | 367 | struct netpoll *np = NULL; |
1da177e4 | 368 | |
fbeec2e1 JM |
369 | if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev) |
370 | np = npinfo->rx_np; | |
115c1d6e JM |
371 | if (!np) |
372 | return; | |
1da177e4 LT |
373 | |
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 | ||
448 | int __netpoll_rx(struct sk_buff *skb) | |
449 | { | |
450 | int proto, len, ulen; | |
451 | struct iphdr *iph; | |
452 | struct udphdr *uh; | |
068c6e98 NH |
453 | struct netpoll_info *npi = skb->dev->npinfo; |
454 | struct netpoll *np = npi->rx_np; | |
455 | ||
1da177e4 | 456 | |
fbeec2e1 | 457 | if (!np) |
1da177e4 LT |
458 | 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)) { | |
068c6e98 | 465 | skb_queue_tail(&npi->arp_tx, skb); |
1da177e4 LT |
466 | 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; | |
fb286bb2 | 500 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr)) |
1da177e4 LT |
501 | 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 | ||
516 | out: | |
517 | if (atomic_read(&trapped)) { | |
518 | kfree_skb(skb); | |
519 | return 1; | |
520 | } | |
521 | ||
522 | return 0; | |
523 | } | |
524 | ||
525 | int 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 | ||
633 | int netpoll_setup(struct netpoll *np) | |
634 | { | |
635 | struct net_device *ndev = NULL; | |
636 | struct in_device *in_dev; | |
115c1d6e | 637 | struct netpoll_info *npinfo; |
fbeec2e1 | 638 | unsigned long flags; |
1da177e4 LT |
639 | |
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; | |
115c1d6e JM |
649 | if (!ndev->npinfo) { |
650 | npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); | |
651 | if (!npinfo) | |
652 | goto release; | |
653 | ||
11513128 | 654 | npinfo->rx_flags = 0; |
fbeec2e1 | 655 | npinfo->rx_np = NULL; |
a9f6a0dd | 656 | spin_lock_init(&npinfo->poll_lock); |
115c1d6e | 657 | npinfo->poll_owner = -1; |
0db1d6fc | 658 | npinfo->tries = MAX_RETRIES; |
a9f6a0dd | 659 | spin_lock_init(&npinfo->rx_lock); |
068c6e98 | 660 | skb_queue_head_init(&npinfo->arp_tx); |
93ec2c72 SH |
661 | atomic_set(&npinfo->refcnt, 1); |
662 | } else { | |
115c1d6e | 663 | npinfo = ndev->npinfo; |
93ec2c72 SH |
664 | atomic_inc(&npinfo->refcnt); |
665 | } | |
1da177e4 LT |
666 | |
667 | if (!ndev->poll_controller) { | |
668 | printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n", | |
669 | np->name, np->dev_name); | |
670 | goto release; | |
671 | } | |
672 | ||
673 | if (!netif_running(ndev)) { | |
674 | unsigned long atmost, atleast; | |
675 | ||
676 | printk(KERN_INFO "%s: device %s not up yet, forcing it\n", | |
677 | np->name, np->dev_name); | |
678 | ||
6756ae4b | 679 | rtnl_lock(); |
1da177e4 LT |
680 | if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) { |
681 | printk(KERN_ERR "%s: failed to open %s\n", | |
682 | np->name, np->dev_name); | |
6756ae4b | 683 | rtnl_unlock(); |
1da177e4 LT |
684 | goto release; |
685 | } | |
6756ae4b | 686 | rtnl_unlock(); |
1da177e4 LT |
687 | |
688 | atleast = jiffies + HZ/10; | |
689 | atmost = jiffies + 4*HZ; | |
690 | while (!netif_carrier_ok(ndev)) { | |
691 | if (time_after(jiffies, atmost)) { | |
692 | printk(KERN_NOTICE | |
693 | "%s: timeout waiting for carrier\n", | |
694 | np->name); | |
695 | break; | |
696 | } | |
697 | cond_resched(); | |
698 | } | |
699 | ||
700 | /* If carrier appears to come up instantly, we don't | |
701 | * trust it and pause so that we don't pump all our | |
702 | * queued console messages into the bitbucket. | |
703 | */ | |
704 | ||
705 | if (time_before(jiffies, atleast)) { | |
706 | printk(KERN_NOTICE "%s: carrier detect appears" | |
707 | " untrustworthy, waiting 4 seconds\n", | |
708 | np->name); | |
709 | msleep(4000); | |
710 | } | |
711 | } | |
712 | ||
3860288e | 713 | if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr) |
1da177e4 LT |
714 | memcpy(np->local_mac, ndev->dev_addr, 6); |
715 | ||
716 | if (!np->local_ip) { | |
717 | rcu_read_lock(); | |
e5ed6399 | 718 | in_dev = __in_dev_get_rcu(ndev); |
1da177e4 LT |
719 | |
720 | if (!in_dev || !in_dev->ifa_list) { | |
721 | rcu_read_unlock(); | |
722 | printk(KERN_ERR "%s: no IP address for %s, aborting\n", | |
723 | np->name, np->dev_name); | |
724 | goto release; | |
725 | } | |
726 | ||
727 | np->local_ip = ntohl(in_dev->ifa_list->ifa_local); | |
728 | rcu_read_unlock(); | |
729 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", | |
730 | np->name, HIPQUAD(np->local_ip)); | |
731 | } | |
732 | ||
fbeec2e1 JM |
733 | if (np->rx_hook) { |
734 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
735 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; | |
736 | npinfo->rx_np = np; | |
737 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
738 | } | |
26520765 IM |
739 | |
740 | /* fill up the skb queue */ | |
741 | refill_skbs(); | |
742 | ||
fbeec2e1 | 743 | /* last thing to do is link it to the net device structure */ |
115c1d6e | 744 | ndev->npinfo = npinfo; |
1da177e4 | 745 | |
53fb95d3 MM |
746 | /* avoid racing with NAPI reading npinfo */ |
747 | synchronize_rcu(); | |
748 | ||
1da177e4 LT |
749 | return 0; |
750 | ||
751 | release: | |
115c1d6e JM |
752 | if (!ndev->npinfo) |
753 | kfree(npinfo); | |
1da177e4 LT |
754 | np->dev = NULL; |
755 | dev_put(ndev); | |
756 | return -1; | |
757 | } | |
758 | ||
a1bcfacd SH |
759 | static int __init netpoll_init(void) { |
760 | skb_queue_head_init(&skb_pool); | |
761 | return 0; | |
762 | } | |
763 | core_initcall(netpoll_init); | |
764 | ||
1da177e4 LT |
765 | void netpoll_cleanup(struct netpoll *np) |
766 | { | |
fbeec2e1 JM |
767 | struct netpoll_info *npinfo; |
768 | unsigned long flags; | |
769 | ||
115c1d6e | 770 | if (np->dev) { |
fbeec2e1 | 771 | npinfo = np->dev->npinfo; |
93ec2c72 SH |
772 | if (npinfo) { |
773 | if (npinfo->rx_np == np) { | |
774 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
775 | npinfo->rx_np = NULL; | |
776 | npinfo->rx_flags &= ~NETPOLL_RX_ENABLED; | |
777 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
778 | } | |
779 | ||
780 | np->dev->npinfo = NULL; | |
781 | if (atomic_dec_and_test(&npinfo->refcnt)) { | |
782 | skb_queue_purge(&npinfo->arp_tx); | |
783 | ||
784 | kfree(npinfo); | |
785 | } | |
fbeec2e1 | 786 | } |
93ec2c72 | 787 | |
115c1d6e JM |
788 | dev_put(np->dev); |
789 | } | |
fbeec2e1 | 790 | |
1da177e4 LT |
791 | np->dev = NULL; |
792 | } | |
793 | ||
794 | int netpoll_trap(void) | |
795 | { | |
796 | return atomic_read(&trapped); | |
797 | } | |
798 | ||
799 | void netpoll_set_trap(int trap) | |
800 | { | |
801 | if (trap) | |
802 | atomic_inc(&trapped); | |
803 | else | |
804 | atomic_dec(&trapped); | |
805 | } | |
806 | ||
807 | EXPORT_SYMBOL(netpoll_set_trap); | |
808 | EXPORT_SYMBOL(netpoll_trap); | |
809 | EXPORT_SYMBOL(netpoll_parse_options); | |
810 | EXPORT_SYMBOL(netpoll_setup); | |
811 | EXPORT_SYMBOL(netpoll_cleanup); | |
812 | EXPORT_SYMBOL(netpoll_send_udp); | |
813 | EXPORT_SYMBOL(netpoll_poll); | |
814 | EXPORT_SYMBOL(netpoll_queue); |