]>
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) | |
37 | ||
a1bcfacd | 38 | static struct sk_buff_head skb_pool; |
1da177e4 | 39 | |
1da177e4 LT |
40 | static atomic_t trapped; |
41 | ||
2bdfe0ba | 42 | #define USEC_PER_POLL 50 |
1da177e4 LT |
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 | ||
50 | static void zap_completion_queue(void); | |
068c6e98 | 51 | static void arp_reply(struct sk_buff *skb); |
1da177e4 LT |
52 | |
53 | static void queue_process(void *p) | |
54 | { | |
b6cd27ed | 55 | struct netpoll_info *npinfo = p; |
1da177e4 LT |
56 | struct sk_buff *skb; |
57 | ||
6c43ff18 SH |
58 | while ((skb = skb_dequeue(&npinfo->txq))) { |
59 | struct net_device *dev = skb->dev; | |
1da177e4 | 60 | |
6c43ff18 SH |
61 | if (!netif_device_present(dev) || !netif_running(dev)) { |
62 | __kfree_skb(skb); | |
63 | continue; | |
64 | } | |
65 | ||
66 | netif_tx_lock_bh(dev); | |
67 | if (netif_queue_stopped(dev) || | |
68 | dev->hard_start_xmit(skb, dev) != NETDEV_TX_OK) { | |
69 | skb_queue_head(&npinfo->txq, skb); | |
70 | netif_tx_unlock_bh(dev); | |
71 | ||
72 | schedule_delayed_work(&npinfo->tx_work, HZ/10); | |
73 | return; | |
74 | } | |
2bdfe0ba | 75 | |
6c43ff18 SH |
76 | netif_tx_unlock_bh(dev); |
77 | } | |
b6cd27ed | 78 | } |
1da177e4 LT |
79 | |
80 | void netpoll_queue(struct sk_buff *skb) | |
81 | { | |
b6cd27ed SH |
82 | struct net_device *dev = skb->dev; |
83 | struct netpoll_info *npinfo = dev->npinfo; | |
1da177e4 | 84 | |
b6cd27ed SH |
85 | if (!npinfo) |
86 | kfree_skb(skb); | |
87 | else { | |
88 | skb_queue_tail(&npinfo->txq, skb); | |
89 | schedule_work(&npinfo->tx_work); | |
1da177e4 | 90 | } |
1da177e4 LT |
91 | } |
92 | ||
93 | static int checksum_udp(struct sk_buff *skb, struct udphdr *uh, | |
94 | unsigned short ulen, u32 saddr, u32 daddr) | |
95 | { | |
fb286bb2 HX |
96 | unsigned int psum; |
97 | ||
98 | if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY) | |
1da177e4 LT |
99 | return 0; |
100 | ||
fb286bb2 HX |
101 | psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0); |
102 | ||
84fa7933 | 103 | if (skb->ip_summed == CHECKSUM_COMPLETE && |
fb286bb2 HX |
104 | !(u16)csum_fold(csum_add(psum, skb->csum))) |
105 | return 0; | |
1da177e4 | 106 | |
fb286bb2 | 107 | skb->csum = psum; |
1da177e4 | 108 | |
fb286bb2 | 109 | return __skb_checksum_complete(skb); |
1da177e4 LT |
110 | } |
111 | ||
112 | /* | |
113 | * Check whether delayed processing was scheduled for our NIC. If so, | |
114 | * we attempt to grab the poll lock and use ->poll() to pump the card. | |
115 | * If this fails, either we've recursed in ->poll() or it's already | |
116 | * running on another CPU. | |
117 | * | |
118 | * Note: we don't mask interrupts with this lock because we're using | |
119 | * trylock here and interrupts are already disabled in the softirq | |
120 | * case. Further, we test the poll_owner to avoid recursion on UP | |
121 | * systems where the lock doesn't exist. | |
122 | * | |
123 | * In cases where there is bi-directional communications, reading only | |
124 | * one message at a time can lead to packets being dropped by the | |
125 | * network adapter, forcing superfluous retries and possibly timeouts. | |
126 | * Thus, we set our budget to greater than 1. | |
127 | */ | |
128 | static void poll_napi(struct netpoll *np) | |
129 | { | |
115c1d6e | 130 | struct netpoll_info *npinfo = np->dev->npinfo; |
1da177e4 LT |
131 | int budget = 16; |
132 | ||
133 | if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) && | |
115c1d6e JM |
134 | npinfo->poll_owner != smp_processor_id() && |
135 | spin_trylock(&npinfo->poll_lock)) { | |
136 | npinfo->rx_flags |= NETPOLL_RX_DROP; | |
1da177e4 LT |
137 | atomic_inc(&trapped); |
138 | ||
139 | np->dev->poll(np->dev, &budget); | |
140 | ||
141 | atomic_dec(&trapped); | |
115c1d6e JM |
142 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
143 | spin_unlock(&npinfo->poll_lock); | |
1da177e4 LT |
144 | } |
145 | } | |
146 | ||
068c6e98 NH |
147 | static void service_arp_queue(struct netpoll_info *npi) |
148 | { | |
149 | struct sk_buff *skb; | |
150 | ||
151 | if (unlikely(!npi)) | |
152 | return; | |
153 | ||
154 | skb = skb_dequeue(&npi->arp_tx); | |
155 | ||
156 | while (skb != NULL) { | |
157 | arp_reply(skb); | |
158 | skb = skb_dequeue(&npi->arp_tx); | |
159 | } | |
160 | return; | |
161 | } | |
162 | ||
1da177e4 LT |
163 | void netpoll_poll(struct netpoll *np) |
164 | { | |
165 | if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller) | |
166 | return; | |
167 | ||
168 | /* Process pending work on NIC */ | |
169 | np->dev->poll_controller(np->dev); | |
170 | if (np->dev->poll) | |
171 | poll_napi(np); | |
172 | ||
068c6e98 NH |
173 | service_arp_queue(np->dev->npinfo); |
174 | ||
1da177e4 LT |
175 | zap_completion_queue(); |
176 | } | |
177 | ||
178 | static void refill_skbs(void) | |
179 | { | |
180 | struct sk_buff *skb; | |
181 | unsigned long flags; | |
182 | ||
a1bcfacd SH |
183 | spin_lock_irqsave(&skb_pool.lock, flags); |
184 | while (skb_pool.qlen < MAX_SKBS) { | |
1da177e4 LT |
185 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); |
186 | if (!skb) | |
187 | break; | |
188 | ||
a1bcfacd | 189 | __skb_queue_tail(&skb_pool, skb); |
1da177e4 | 190 | } |
a1bcfacd | 191 | spin_unlock_irqrestore(&skb_pool.lock, flags); |
1da177e4 LT |
192 | } |
193 | ||
194 | static void zap_completion_queue(void) | |
195 | { | |
196 | unsigned long flags; | |
197 | struct softnet_data *sd = &get_cpu_var(softnet_data); | |
198 | ||
199 | if (sd->completion_queue) { | |
200 | struct sk_buff *clist; | |
201 | ||
202 | local_irq_save(flags); | |
203 | clist = sd->completion_queue; | |
204 | sd->completion_queue = NULL; | |
205 | local_irq_restore(flags); | |
206 | ||
207 | while (clist != NULL) { | |
208 | struct sk_buff *skb = clist; | |
209 | clist = clist->next; | |
210 | if(skb->destructor) | |
211 | dev_kfree_skb_any(skb); /* put this one back */ | |
212 | else | |
213 | __kfree_skb(skb); | |
214 | } | |
215 | } | |
216 | ||
217 | put_cpu_var(softnet_data); | |
218 | } | |
219 | ||
a1bcfacd | 220 | static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve) |
1da177e4 | 221 | { |
a1bcfacd SH |
222 | int count = 0; |
223 | struct sk_buff *skb; | |
1da177e4 LT |
224 | |
225 | zap_completion_queue(); | |
a1bcfacd | 226 | refill_skbs(); |
1da177e4 | 227 | repeat: |
1da177e4 LT |
228 | |
229 | skb = alloc_skb(len, GFP_ATOMIC); | |
a1bcfacd SH |
230 | if (!skb) |
231 | skb = skb_dequeue(&skb_pool); | |
1da177e4 LT |
232 | |
233 | if(!skb) { | |
a1bcfacd SH |
234 | if (++count < 10) { |
235 | netpoll_poll(np); | |
236 | goto repeat; | |
1da177e4 | 237 | } |
a1bcfacd | 238 | return NULL; |
1da177e4 LT |
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 | { | |
2bdfe0ba SH |
248 | int status = NETDEV_TX_BUSY; |
249 | unsigned long tries; | |
250 | struct net_device *dev = np->dev; | |
251 | struct netpoll_info *npinfo = np->dev->npinfo; | |
252 | ||
253 | if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) { | |
254 | __kfree_skb(skb); | |
255 | return; | |
256 | } | |
257 | ||
258 | /* don't get messages out of order, and no recursion */ | |
259 | if ( !(np->drop == netpoll_queue && skb_queue_len(&npinfo->txq)) | |
260 | && npinfo->poll_owner != smp_processor_id() | |
261 | && netif_tx_trylock(dev)) { | |
262 | ||
263 | /* try until next clock tick */ | |
264 | for(tries = jiffies_to_usecs(1)/USEC_PER_POLL; tries > 0; --tries) { | |
265 | if (!netif_queue_stopped(dev)) | |
266 | status = dev->hard_start_xmit(skb, dev); | |
267 | ||
268 | if (status == NETDEV_TX_OK) | |
269 | break; | |
270 | ||
271 | /* tickle device maybe there is some cleanup */ | |
272 | netpoll_poll(np); | |
1da177e4 | 273 | |
2bdfe0ba SH |
274 | udelay(USEC_PER_POLL); |
275 | } | |
276 | netif_tx_unlock(dev); | |
1da177e4 LT |
277 | } |
278 | ||
2bdfe0ba SH |
279 | if (status != NETDEV_TX_OK) { |
280 | /* requeue for later */ | |
1da177e4 LT |
281 | if (np->drop) |
282 | np->drop(skb); | |
283 | else | |
284 | __kfree_skb(skb); | |
1da177e4 | 285 | } |
1da177e4 LT |
286 | } |
287 | ||
288 | void netpoll_send_udp(struct netpoll *np, const char *msg, int len) | |
289 | { | |
290 | int total_len, eth_len, ip_len, udp_len; | |
291 | struct sk_buff *skb; | |
292 | struct udphdr *udph; | |
293 | struct iphdr *iph; | |
294 | struct ethhdr *eth; | |
295 | ||
296 | udp_len = len + sizeof(*udph); | |
297 | ip_len = eth_len = udp_len + sizeof(*iph); | |
298 | total_len = eth_len + ETH_HLEN + NET_IP_ALIGN; | |
299 | ||
300 | skb = find_skb(np, total_len, total_len - len); | |
301 | if (!skb) | |
302 | return; | |
303 | ||
304 | memcpy(skb->data, msg, len); | |
305 | skb->len += len; | |
306 | ||
206daaf7 | 307 | skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph)); |
1da177e4 LT |
308 | udph->source = htons(np->local_port); |
309 | udph->dest = htons(np->remote_port); | |
310 | udph->len = htons(udp_len); | |
311 | udph->check = 0; | |
8e365eec CL |
312 | udph->check = csum_tcpudp_magic(htonl(np->local_ip), |
313 | htonl(np->remote_ip), | |
314 | udp_len, IPPROTO_UDP, | |
315 | csum_partial((unsigned char *)udph, udp_len, 0)); | |
316 | if (udph->check == 0) | |
317 | udph->check = -1; | |
1da177e4 | 318 | |
206daaf7 | 319 | skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph)); |
1da177e4 LT |
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); | |
206daaf7 SH |
335 | skb->mac.raw = skb->data; |
336 | skb->protocol = eth->h_proto = htons(ETH_P_IP); | |
1da177e4 LT |
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; | |
352 | struct sk_buff *send_skb; | |
115c1d6e | 353 | struct netpoll *np = NULL; |
1da177e4 | 354 | |
fbeec2e1 JM |
355 | if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev) |
356 | np = npinfo->rx_np; | |
115c1d6e JM |
357 | if (!np) |
358 | return; | |
1da177e4 LT |
359 | |
360 | /* No arp on this interface */ | |
361 | if (skb->dev->flags & IFF_NOARP) | |
362 | return; | |
363 | ||
364 | if (!pskb_may_pull(skb, (sizeof(struct arphdr) + | |
365 | (2 * skb->dev->addr_len) + | |
366 | (2 * sizeof(u32))))) | |
367 | return; | |
368 | ||
369 | skb->h.raw = skb->nh.raw = skb->data; | |
370 | arp = skb->nh.arph; | |
371 | ||
372 | if ((arp->ar_hrd != htons(ARPHRD_ETHER) && | |
373 | arp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
374 | arp->ar_pro != htons(ETH_P_IP) || | |
375 | arp->ar_op != htons(ARPOP_REQUEST)) | |
376 | return; | |
377 | ||
378 | arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len; | |
379 | memcpy(&sip, arp_ptr, 4); | |
380 | arp_ptr += 4 + skb->dev->addr_len; | |
381 | memcpy(&tip, arp_ptr, 4); | |
382 | ||
383 | /* Should we ignore arp? */ | |
384 | if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip)) | |
385 | return; | |
386 | ||
387 | size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4); | |
388 | send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev), | |
389 | LL_RESERVED_SPACE(np->dev)); | |
390 | ||
391 | if (!send_skb) | |
392 | return; | |
393 | ||
394 | send_skb->nh.raw = send_skb->data; | |
395 | arp = (struct arphdr *) skb_put(send_skb, size); | |
396 | send_skb->dev = skb->dev; | |
397 | send_skb->protocol = htons(ETH_P_ARP); | |
398 | ||
399 | /* Fill the device header for the ARP frame */ | |
400 | ||
401 | if (np->dev->hard_header && | |
402 | np->dev->hard_header(send_skb, skb->dev, ptype, | |
403 | np->remote_mac, np->local_mac, | |
404 | send_skb->len) < 0) { | |
405 | kfree_skb(send_skb); | |
406 | return; | |
407 | } | |
408 | ||
409 | /* | |
410 | * Fill out the arp protocol part. | |
411 | * | |
412 | * we only support ethernet device type, | |
413 | * which (according to RFC 1390) should always equal 1 (Ethernet). | |
414 | */ | |
415 | ||
416 | arp->ar_hrd = htons(np->dev->type); | |
417 | arp->ar_pro = htons(ETH_P_IP); | |
418 | arp->ar_hln = np->dev->addr_len; | |
419 | arp->ar_pln = 4; | |
420 | arp->ar_op = htons(type); | |
421 | ||
422 | arp_ptr=(unsigned char *)(arp + 1); | |
423 | memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len); | |
424 | arp_ptr += np->dev->addr_len; | |
425 | memcpy(arp_ptr, &tip, 4); | |
426 | arp_ptr += 4; | |
427 | memcpy(arp_ptr, np->remote_mac, np->dev->addr_len); | |
428 | arp_ptr += np->dev->addr_len; | |
429 | memcpy(arp_ptr, &sip, 4); | |
430 | ||
431 | netpoll_send_skb(np, send_skb); | |
432 | } | |
433 | ||
434 | int __netpoll_rx(struct sk_buff *skb) | |
435 | { | |
436 | int proto, len, ulen; | |
437 | struct iphdr *iph; | |
438 | struct udphdr *uh; | |
068c6e98 NH |
439 | struct netpoll_info *npi = skb->dev->npinfo; |
440 | struct netpoll *np = npi->rx_np; | |
441 | ||
1da177e4 | 442 | |
fbeec2e1 | 443 | if (!np) |
1da177e4 LT |
444 | goto out; |
445 | if (skb->dev->type != ARPHRD_ETHER) | |
446 | goto out; | |
447 | ||
448 | /* check if netpoll clients need ARP */ | |
449 | if (skb->protocol == __constant_htons(ETH_P_ARP) && | |
450 | atomic_read(&trapped)) { | |
068c6e98 | 451 | skb_queue_tail(&npi->arp_tx, skb); |
1da177e4 LT |
452 | return 1; |
453 | } | |
454 | ||
455 | proto = ntohs(eth_hdr(skb)->h_proto); | |
456 | if (proto != ETH_P_IP) | |
457 | goto out; | |
458 | if (skb->pkt_type == PACKET_OTHERHOST) | |
459 | goto out; | |
460 | if (skb_shared(skb)) | |
461 | goto out; | |
462 | ||
463 | iph = (struct iphdr *)skb->data; | |
464 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) | |
465 | goto out; | |
466 | if (iph->ihl < 5 || iph->version != 4) | |
467 | goto out; | |
468 | if (!pskb_may_pull(skb, iph->ihl*4)) | |
469 | goto out; | |
470 | if (ip_fast_csum((u8 *)iph, iph->ihl) != 0) | |
471 | goto out; | |
472 | ||
473 | len = ntohs(iph->tot_len); | |
474 | if (skb->len < len || len < iph->ihl*4) | |
475 | goto out; | |
476 | ||
477 | if (iph->protocol != IPPROTO_UDP) | |
478 | goto out; | |
479 | ||
480 | len -= iph->ihl*4; | |
481 | uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); | |
482 | ulen = ntohs(uh->len); | |
483 | ||
484 | if (ulen != len) | |
485 | goto out; | |
fb286bb2 | 486 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr)) |
1da177e4 LT |
487 | goto out; |
488 | if (np->local_ip && np->local_ip != ntohl(iph->daddr)) | |
489 | goto out; | |
490 | if (np->remote_ip && np->remote_ip != ntohl(iph->saddr)) | |
491 | goto out; | |
492 | if (np->local_port && np->local_port != ntohs(uh->dest)) | |
493 | goto out; | |
494 | ||
495 | np->rx_hook(np, ntohs(uh->source), | |
496 | (char *)(uh+1), | |
497 | ulen - sizeof(struct udphdr)); | |
498 | ||
499 | kfree_skb(skb); | |
500 | return 1; | |
501 | ||
502 | out: | |
503 | if (atomic_read(&trapped)) { | |
504 | kfree_skb(skb); | |
505 | return 1; | |
506 | } | |
507 | ||
508 | return 0; | |
509 | } | |
510 | ||
511 | int netpoll_parse_options(struct netpoll *np, char *opt) | |
512 | { | |
513 | char *cur=opt, *delim; | |
514 | ||
515 | if(*cur != '@') { | |
516 | if ((delim = strchr(cur, '@')) == NULL) | |
517 | goto parse_failed; | |
518 | *delim=0; | |
519 | np->local_port=simple_strtol(cur, NULL, 10); | |
520 | cur=delim; | |
521 | } | |
522 | cur++; | |
523 | printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port); | |
524 | ||
525 | if(*cur != '/') { | |
526 | if ((delim = strchr(cur, '/')) == NULL) | |
527 | goto parse_failed; | |
528 | *delim=0; | |
529 | np->local_ip=ntohl(in_aton(cur)); | |
530 | cur=delim; | |
531 | ||
532 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", | |
533 | np->name, HIPQUAD(np->local_ip)); | |
534 | } | |
535 | cur++; | |
536 | ||
537 | if ( *cur != ',') { | |
538 | /* parse out dev name */ | |
539 | if ((delim = strchr(cur, ',')) == NULL) | |
540 | goto parse_failed; | |
541 | *delim=0; | |
542 | strlcpy(np->dev_name, cur, sizeof(np->dev_name)); | |
543 | cur=delim; | |
544 | } | |
545 | cur++; | |
546 | ||
547 | printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name); | |
548 | ||
549 | if ( *cur != '@' ) { | |
550 | /* dst port */ | |
551 | if ((delim = strchr(cur, '@')) == NULL) | |
552 | goto parse_failed; | |
553 | *delim=0; | |
554 | np->remote_port=simple_strtol(cur, NULL, 10); | |
555 | cur=delim; | |
556 | } | |
557 | cur++; | |
558 | printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port); | |
559 | ||
560 | /* dst ip */ | |
561 | if ((delim = strchr(cur, '/')) == NULL) | |
562 | goto parse_failed; | |
563 | *delim=0; | |
564 | np->remote_ip=ntohl(in_aton(cur)); | |
565 | cur=delim+1; | |
566 | ||
567 | printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n", | |
568 | np->name, HIPQUAD(np->remote_ip)); | |
569 | ||
570 | if( *cur != 0 ) | |
571 | { | |
572 | /* MAC address */ | |
573 | if ((delim = strchr(cur, ':')) == NULL) | |
574 | goto parse_failed; | |
575 | *delim=0; | |
576 | np->remote_mac[0]=simple_strtol(cur, NULL, 16); | |
577 | cur=delim+1; | |
578 | if ((delim = strchr(cur, ':')) == NULL) | |
579 | goto parse_failed; | |
580 | *delim=0; | |
581 | np->remote_mac[1]=simple_strtol(cur, NULL, 16); | |
582 | cur=delim+1; | |
583 | if ((delim = strchr(cur, ':')) == NULL) | |
584 | goto parse_failed; | |
585 | *delim=0; | |
586 | np->remote_mac[2]=simple_strtol(cur, NULL, 16); | |
587 | cur=delim+1; | |
588 | if ((delim = strchr(cur, ':')) == NULL) | |
589 | goto parse_failed; | |
590 | *delim=0; | |
591 | np->remote_mac[3]=simple_strtol(cur, NULL, 16); | |
592 | cur=delim+1; | |
593 | if ((delim = strchr(cur, ':')) == NULL) | |
594 | goto parse_failed; | |
595 | *delim=0; | |
596 | np->remote_mac[4]=simple_strtol(cur, NULL, 16); | |
597 | cur=delim+1; | |
598 | np->remote_mac[5]=simple_strtol(cur, NULL, 16); | |
599 | } | |
600 | ||
601 | printk(KERN_INFO "%s: remote ethernet address " | |
602 | "%02x:%02x:%02x:%02x:%02x:%02x\n", | |
603 | np->name, | |
604 | np->remote_mac[0], | |
605 | np->remote_mac[1], | |
606 | np->remote_mac[2], | |
607 | np->remote_mac[3], | |
608 | np->remote_mac[4], | |
609 | np->remote_mac[5]); | |
610 | ||
611 | return 0; | |
612 | ||
613 | parse_failed: | |
614 | printk(KERN_INFO "%s: couldn't parse config at %s!\n", | |
615 | np->name, cur); | |
616 | return -1; | |
617 | } | |
618 | ||
619 | int netpoll_setup(struct netpoll *np) | |
620 | { | |
621 | struct net_device *ndev = NULL; | |
622 | struct in_device *in_dev; | |
115c1d6e | 623 | struct netpoll_info *npinfo; |
fbeec2e1 | 624 | unsigned long flags; |
b41848b6 | 625 | int err; |
1da177e4 LT |
626 | |
627 | if (np->dev_name) | |
628 | ndev = dev_get_by_name(np->dev_name); | |
629 | if (!ndev) { | |
630 | printk(KERN_ERR "%s: %s doesn't exist, aborting.\n", | |
631 | np->name, np->dev_name); | |
b41848b6 | 632 | return -ENODEV; |
1da177e4 LT |
633 | } |
634 | ||
635 | np->dev = ndev; | |
115c1d6e JM |
636 | if (!ndev->npinfo) { |
637 | npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); | |
b41848b6 SH |
638 | if (!npinfo) { |
639 | err = -ENOMEM; | |
115c1d6e | 640 | goto release; |
b41848b6 | 641 | } |
115c1d6e | 642 | |
11513128 | 643 | npinfo->rx_flags = 0; |
fbeec2e1 | 644 | npinfo->rx_np = NULL; |
a9f6a0dd | 645 | spin_lock_init(&npinfo->poll_lock); |
115c1d6e | 646 | npinfo->poll_owner = -1; |
2bdfe0ba | 647 | |
a9f6a0dd | 648 | spin_lock_init(&npinfo->rx_lock); |
068c6e98 | 649 | skb_queue_head_init(&npinfo->arp_tx); |
b6cd27ed SH |
650 | skb_queue_head_init(&npinfo->txq); |
651 | INIT_WORK(&npinfo->tx_work, queue_process, npinfo); | |
652 | ||
93ec2c72 SH |
653 | atomic_set(&npinfo->refcnt, 1); |
654 | } else { | |
115c1d6e | 655 | npinfo = ndev->npinfo; |
93ec2c72 SH |
656 | atomic_inc(&npinfo->refcnt); |
657 | } | |
1da177e4 LT |
658 | |
659 | if (!ndev->poll_controller) { | |
660 | printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n", | |
661 | np->name, np->dev_name); | |
b41848b6 | 662 | err = -ENOTSUPP; |
1da177e4 LT |
663 | goto release; |
664 | } | |
665 | ||
666 | if (!netif_running(ndev)) { | |
667 | unsigned long atmost, atleast; | |
668 | ||
669 | printk(KERN_INFO "%s: device %s not up yet, forcing it\n", | |
670 | np->name, np->dev_name); | |
671 | ||
6756ae4b | 672 | rtnl_lock(); |
b41848b6 SH |
673 | err = dev_open(ndev); |
674 | rtnl_unlock(); | |
675 | ||
676 | if (err) { | |
1da177e4 | 677 | printk(KERN_ERR "%s: failed to open %s\n", |
b41848b6 | 678 | np->name, ndev->name); |
1da177e4 LT |
679 | goto release; |
680 | } | |
1da177e4 LT |
681 | |
682 | atleast = jiffies + HZ/10; | |
683 | atmost = jiffies + 4*HZ; | |
684 | while (!netif_carrier_ok(ndev)) { | |
685 | if (time_after(jiffies, atmost)) { | |
686 | printk(KERN_NOTICE | |
687 | "%s: timeout waiting for carrier\n", | |
688 | np->name); | |
689 | break; | |
690 | } | |
691 | cond_resched(); | |
692 | } | |
693 | ||
694 | /* If carrier appears to come up instantly, we don't | |
695 | * trust it and pause so that we don't pump all our | |
696 | * queued console messages into the bitbucket. | |
697 | */ | |
698 | ||
699 | if (time_before(jiffies, atleast)) { | |
700 | printk(KERN_NOTICE "%s: carrier detect appears" | |
701 | " untrustworthy, waiting 4 seconds\n", | |
702 | np->name); | |
703 | msleep(4000); | |
704 | } | |
705 | } | |
706 | ||
3860288e | 707 | if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr) |
1da177e4 LT |
708 | memcpy(np->local_mac, ndev->dev_addr, 6); |
709 | ||
710 | if (!np->local_ip) { | |
711 | rcu_read_lock(); | |
e5ed6399 | 712 | in_dev = __in_dev_get_rcu(ndev); |
1da177e4 LT |
713 | |
714 | if (!in_dev || !in_dev->ifa_list) { | |
715 | rcu_read_unlock(); | |
716 | printk(KERN_ERR "%s: no IP address for %s, aborting\n", | |
717 | np->name, np->dev_name); | |
b41848b6 | 718 | err = -EDESTADDRREQ; |
1da177e4 LT |
719 | goto release; |
720 | } | |
721 | ||
722 | np->local_ip = ntohl(in_dev->ifa_list->ifa_local); | |
723 | rcu_read_unlock(); | |
724 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", | |
725 | np->name, HIPQUAD(np->local_ip)); | |
726 | } | |
727 | ||
fbeec2e1 JM |
728 | if (np->rx_hook) { |
729 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
730 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; | |
731 | npinfo->rx_np = np; | |
732 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
733 | } | |
26520765 IM |
734 | |
735 | /* fill up the skb queue */ | |
736 | refill_skbs(); | |
737 | ||
fbeec2e1 | 738 | /* last thing to do is link it to the net device structure */ |
115c1d6e | 739 | ndev->npinfo = npinfo; |
1da177e4 | 740 | |
53fb95d3 MM |
741 | /* avoid racing with NAPI reading npinfo */ |
742 | synchronize_rcu(); | |
743 | ||
1da177e4 LT |
744 | return 0; |
745 | ||
746 | release: | |
115c1d6e JM |
747 | if (!ndev->npinfo) |
748 | kfree(npinfo); | |
1da177e4 LT |
749 | np->dev = NULL; |
750 | dev_put(ndev); | |
b41848b6 | 751 | return err; |
1da177e4 LT |
752 | } |
753 | ||
a1bcfacd SH |
754 | static int __init netpoll_init(void) { |
755 | skb_queue_head_init(&skb_pool); | |
756 | return 0; | |
757 | } | |
758 | core_initcall(netpoll_init); | |
759 | ||
1da177e4 LT |
760 | void netpoll_cleanup(struct netpoll *np) |
761 | { | |
fbeec2e1 JM |
762 | struct netpoll_info *npinfo; |
763 | unsigned long flags; | |
764 | ||
115c1d6e | 765 | if (np->dev) { |
fbeec2e1 | 766 | npinfo = np->dev->npinfo; |
93ec2c72 SH |
767 | if (npinfo) { |
768 | if (npinfo->rx_np == np) { | |
769 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
770 | npinfo->rx_np = NULL; | |
771 | npinfo->rx_flags &= ~NETPOLL_RX_ENABLED; | |
772 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
773 | } | |
774 | ||
775 | np->dev->npinfo = NULL; | |
776 | if (atomic_dec_and_test(&npinfo->refcnt)) { | |
777 | skb_queue_purge(&npinfo->arp_tx); | |
b6cd27ed | 778 | skb_queue_purge(&npinfo->txq); |
6c43ff18 | 779 | cancel_rearming_delayed_work(&npinfo->tx_work); |
b6cd27ed | 780 | flush_scheduled_work(); |
93ec2c72 SH |
781 | |
782 | kfree(npinfo); | |
783 | } | |
fbeec2e1 | 784 | } |
93ec2c72 | 785 | |
115c1d6e JM |
786 | dev_put(np->dev); |
787 | } | |
fbeec2e1 | 788 | |
1da177e4 LT |
789 | np->dev = NULL; |
790 | } | |
791 | ||
792 | int netpoll_trap(void) | |
793 | { | |
794 | return atomic_read(&trapped); | |
795 | } | |
796 | ||
797 | void netpoll_set_trap(int trap) | |
798 | { | |
799 | if (trap) | |
800 | atomic_inc(&trapped); | |
801 | else | |
802 | atomic_dec(&trapped); | |
803 | } | |
804 | ||
805 | EXPORT_SYMBOL(netpoll_set_trap); | |
806 | EXPORT_SYMBOL(netpoll_trap); | |
807 | EXPORT_SYMBOL(netpoll_parse_options); | |
808 | EXPORT_SYMBOL(netpoll_setup); | |
809 | EXPORT_SYMBOL(netpoll_cleanup); | |
810 | EXPORT_SYMBOL(netpoll_send_udp); | |
811 | EXPORT_SYMBOL(netpoll_poll); | |
812 | EXPORT_SYMBOL(netpoll_queue); |