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