]>
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 | ||
bff38771 | 12 | #include <linux/moduleparam.h> |
1da177e4 LT |
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> | |
5a0e3ad6 | 25 | #include <linux/slab.h> |
1da177e4 LT |
26 | #include <net/tcp.h> |
27 | #include <net/udp.h> | |
28 | #include <asm/unaligned.h> | |
9cbc1cb8 | 29 | #include <trace/events/napi.h> |
1da177e4 LT |
30 | |
31 | /* | |
32 | * We maintain a small pool of fully-sized skbs, to make sure the | |
33 | * message gets out even in extreme OOM situations. | |
34 | */ | |
35 | ||
36 | #define MAX_UDP_CHUNK 1460 | |
37 | #define MAX_SKBS 32 | |
1da177e4 | 38 | |
a1bcfacd | 39 | static struct sk_buff_head skb_pool; |
1da177e4 LT |
40 | |
41 | static atomic_t trapped; | |
42 | ||
2bdfe0ba | 43 | #define USEC_PER_POLL 50 |
d9452e9f DM |
44 | #define NETPOLL_RX_ENABLED 1 |
45 | #define NETPOLL_RX_DROP 2 | |
1da177e4 LT |
46 | |
47 | #define MAX_SKB_SIZE \ | |
48 | (MAX_UDP_CHUNK + sizeof(struct udphdr) + \ | |
49 | sizeof(struct iphdr) + sizeof(struct ethhdr)) | |
50 | ||
3578b0c8 | 51 | static void zap_completion_queue(void); |
068c6e98 | 52 | static void arp_reply(struct sk_buff *skb); |
1da177e4 | 53 | |
bff38771 AV |
54 | static unsigned int carrier_timeout = 4; |
55 | module_param(carrier_timeout, uint, 0644); | |
56 | ||
c4028958 | 57 | static void queue_process(struct work_struct *work) |
1da177e4 | 58 | { |
4c1ac1b4 DH |
59 | struct netpoll_info *npinfo = |
60 | container_of(work, struct netpoll_info, tx_work.work); | |
1da177e4 | 61 | struct sk_buff *skb; |
3640543d | 62 | unsigned long flags; |
1da177e4 | 63 | |
6c43ff18 SH |
64 | while ((skb = skb_dequeue(&npinfo->txq))) { |
65 | struct net_device *dev = skb->dev; | |
00829823 | 66 | const struct net_device_ops *ops = dev->netdev_ops; |
fd2ea0a7 | 67 | struct netdev_queue *txq; |
1da177e4 | 68 | |
6c43ff18 SH |
69 | if (!netif_device_present(dev) || !netif_running(dev)) { |
70 | __kfree_skb(skb); | |
71 | continue; | |
72 | } | |
1da177e4 | 73 | |
fd2ea0a7 DM |
74 | txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb)); |
75 | ||
3640543d | 76 | local_irq_save(flags); |
fd2ea0a7 | 77 | __netif_tx_lock(txq, smp_processor_id()); |
5a0d2268 | 78 | if (netif_tx_queue_frozen_or_stopped(txq) || |
00829823 | 79 | ops->ndo_start_xmit(skb, dev) != NETDEV_TX_OK) { |
6c43ff18 | 80 | skb_queue_head(&npinfo->txq, skb); |
fd2ea0a7 | 81 | __netif_tx_unlock(txq); |
3640543d | 82 | local_irq_restore(flags); |
1da177e4 | 83 | |
25442caf | 84 | schedule_delayed_work(&npinfo->tx_work, HZ/10); |
6c43ff18 SH |
85 | return; |
86 | } | |
fd2ea0a7 | 87 | __netif_tx_unlock(txq); |
3640543d | 88 | local_irq_restore(flags); |
1da177e4 | 89 | } |
1da177e4 LT |
90 | } |
91 | ||
b51655b9 AV |
92 | static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh, |
93 | unsigned short ulen, __be32 saddr, __be32 daddr) | |
1da177e4 | 94 | { |
d6f5493c | 95 | __wsum psum; |
fb286bb2 | 96 | |
60476372 | 97 | if (uh->check == 0 || skb_csum_unnecessary(skb)) |
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 && |
d3bc23e7 | 103 | !csum_fold(csum_add(psum, skb->csum))) |
fb286bb2 | 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 | */ | |
0a7606c1 DM |
127 | static int poll_one_napi(struct netpoll_info *npinfo, |
128 | struct napi_struct *napi, int budget) | |
129 | { | |
130 | int work; | |
131 | ||
132 | /* net_rx_action's ->poll() invocations and our's are | |
133 | * synchronized by this test which is only made while | |
134 | * holding the napi->poll_lock. | |
135 | */ | |
136 | if (!test_bit(NAPI_STATE_SCHED, &napi->state)) | |
137 | return budget; | |
138 | ||
d9452e9f | 139 | npinfo->rx_flags |= NETPOLL_RX_DROP; |
0a7606c1 | 140 | atomic_inc(&trapped); |
7b363e44 | 141 | set_bit(NAPI_STATE_NPSVC, &napi->state); |
0a7606c1 DM |
142 | |
143 | work = napi->poll(napi, budget); | |
7d18f114 | 144 | trace_napi_poll(napi); |
0a7606c1 | 145 | |
7b363e44 | 146 | clear_bit(NAPI_STATE_NPSVC, &napi->state); |
0a7606c1 | 147 | atomic_dec(&trapped); |
d9452e9f | 148 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
0a7606c1 DM |
149 | |
150 | return budget - work; | |
151 | } | |
152 | ||
5106930b | 153 | static void poll_napi(struct net_device *dev) |
1da177e4 | 154 | { |
bea3348e | 155 | struct napi_struct *napi; |
1da177e4 LT |
156 | int budget = 16; |
157 | ||
f13d493d | 158 | list_for_each_entry(napi, &dev->napi_list, dev_list) { |
0a7606c1 | 159 | if (napi->poll_owner != smp_processor_id() && |
bea3348e | 160 | spin_trylock(&napi->poll_lock)) { |
5106930b | 161 | budget = poll_one_napi(dev->npinfo, napi, budget); |
bea3348e | 162 | spin_unlock(&napi->poll_lock); |
0a7606c1 DM |
163 | |
164 | if (!budget) | |
165 | break; | |
bea3348e | 166 | } |
1da177e4 LT |
167 | } |
168 | } | |
169 | ||
068c6e98 NH |
170 | static void service_arp_queue(struct netpoll_info *npi) |
171 | { | |
5106930b SH |
172 | if (npi) { |
173 | struct sk_buff *skb; | |
068c6e98 | 174 | |
5106930b SH |
175 | while ((skb = skb_dequeue(&npi->arp_tx))) |
176 | arp_reply(skb); | |
068c6e98 | 177 | } |
068c6e98 NH |
178 | } |
179 | ||
0e34e931 | 180 | void netpoll_poll_dev(struct net_device *dev) |
1da177e4 | 181 | { |
5e392739 | 182 | const struct net_device_ops *ops; |
5106930b | 183 | |
5e392739 PE |
184 | if (!dev || !netif_running(dev)) |
185 | return; | |
186 | ||
187 | ops = dev->netdev_ops; | |
188 | if (!ops->ndo_poll_controller) | |
1da177e4 LT |
189 | return; |
190 | ||
191 | /* Process pending work on NIC */ | |
d314774c | 192 | ops->ndo_poll_controller(dev); |
5106930b SH |
193 | |
194 | poll_napi(dev); | |
1da177e4 | 195 | |
5a698af5 AW |
196 | if (dev->priv_flags & IFF_SLAVE) { |
197 | if (dev->npinfo) { | |
198 | struct net_device *bond_dev = dev->master; | |
199 | struct sk_buff *skb; | |
200 | while ((skb = skb_dequeue(&dev->npinfo->arp_tx))) { | |
201 | skb->dev = bond_dev; | |
202 | skb_queue_tail(&bond_dev->npinfo->arp_tx, skb); | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
5106930b | 207 | service_arp_queue(dev->npinfo); |
068c6e98 | 208 | |
3578b0c8 | 209 | zap_completion_queue(); |
1da177e4 | 210 | } |
9e34a5b5 | 211 | EXPORT_SYMBOL(netpoll_poll_dev); |
1da177e4 | 212 | |
0e34e931 WC |
213 | void netpoll_poll(struct netpoll *np) |
214 | { | |
215 | netpoll_poll_dev(np->dev); | |
216 | } | |
9e34a5b5 | 217 | EXPORT_SYMBOL(netpoll_poll); |
0e34e931 | 218 | |
1da177e4 LT |
219 | static void refill_skbs(void) |
220 | { | |
221 | struct sk_buff *skb; | |
222 | unsigned long flags; | |
223 | ||
a1bcfacd SH |
224 | spin_lock_irqsave(&skb_pool.lock, flags); |
225 | while (skb_pool.qlen < MAX_SKBS) { | |
1da177e4 LT |
226 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); |
227 | if (!skb) | |
228 | break; | |
229 | ||
a1bcfacd | 230 | __skb_queue_tail(&skb_pool, skb); |
1da177e4 | 231 | } |
a1bcfacd | 232 | spin_unlock_irqrestore(&skb_pool.lock, flags); |
1da177e4 LT |
233 | } |
234 | ||
3578b0c8 DM |
235 | static void zap_completion_queue(void) |
236 | { | |
237 | unsigned long flags; | |
238 | struct softnet_data *sd = &get_cpu_var(softnet_data); | |
239 | ||
240 | if (sd->completion_queue) { | |
241 | struct sk_buff *clist; | |
242 | ||
243 | local_irq_save(flags); | |
244 | clist = sd->completion_queue; | |
245 | sd->completion_queue = NULL; | |
246 | local_irq_restore(flags); | |
247 | ||
248 | while (clist != NULL) { | |
249 | struct sk_buff *skb = clist; | |
250 | clist = clist->next; | |
251 | if (skb->destructor) { | |
252 | atomic_inc(&skb->users); | |
253 | dev_kfree_skb_any(skb); /* put this one back */ | |
254 | } else { | |
255 | __kfree_skb(skb); | |
256 | } | |
257 | } | |
258 | } | |
259 | ||
260 | put_cpu_var(softnet_data); | |
261 | } | |
262 | ||
a1bcfacd | 263 | static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve) |
1da177e4 | 264 | { |
a1bcfacd SH |
265 | int count = 0; |
266 | struct sk_buff *skb; | |
1da177e4 | 267 | |
3578b0c8 | 268 | zap_completion_queue(); |
a1bcfacd | 269 | refill_skbs(); |
1da177e4 | 270 | repeat: |
1da177e4 LT |
271 | |
272 | skb = alloc_skb(len, GFP_ATOMIC); | |
a1bcfacd SH |
273 | if (!skb) |
274 | skb = skb_dequeue(&skb_pool); | |
1da177e4 LT |
275 | |
276 | if (!skb) { | |
a1bcfacd SH |
277 | if (++count < 10) { |
278 | netpoll_poll(np); | |
279 | goto repeat; | |
1da177e4 | 280 | } |
a1bcfacd | 281 | return NULL; |
1da177e4 LT |
282 | } |
283 | ||
284 | atomic_set(&skb->users, 1); | |
285 | skb_reserve(skb, reserve); | |
286 | return skb; | |
287 | } | |
288 | ||
bea3348e SH |
289 | static int netpoll_owner_active(struct net_device *dev) |
290 | { | |
291 | struct napi_struct *napi; | |
292 | ||
293 | list_for_each_entry(napi, &dev->napi_list, dev_list) { | |
294 | if (napi->poll_owner == smp_processor_id()) | |
295 | return 1; | |
296 | } | |
297 | return 0; | |
298 | } | |
299 | ||
c2355e1a NH |
300 | void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb, |
301 | struct net_device *dev) | |
1da177e4 | 302 | { |
2bdfe0ba SH |
303 | int status = NETDEV_TX_BUSY; |
304 | unsigned long tries; | |
00829823 | 305 | const struct net_device_ops *ops = dev->netdev_ops; |
de85d99e | 306 | /* It is up to the caller to keep npinfo alive. */ |
4ec93edb | 307 | struct netpoll_info *npinfo = np->dev->npinfo; |
2bdfe0ba | 308 | |
4ec93edb YH |
309 | if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) { |
310 | __kfree_skb(skb); | |
311 | return; | |
312 | } | |
2bdfe0ba SH |
313 | |
314 | /* don't get messages out of order, and no recursion */ | |
bea3348e | 315 | if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) { |
fd2ea0a7 | 316 | struct netdev_queue *txq; |
a49f99ff AM |
317 | unsigned long flags; |
318 | ||
fd2ea0a7 DM |
319 | txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb)); |
320 | ||
a49f99ff | 321 | local_irq_save(flags); |
0db3dc73 SH |
322 | /* try until next clock tick */ |
323 | for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; | |
324 | tries > 0; --tries) { | |
fd2ea0a7 | 325 | if (__netif_tx_trylock(txq)) { |
08baf561 | 326 | if (!netif_tx_queue_stopped(txq)) { |
00829823 | 327 | status = ops->ndo_start_xmit(skb, dev); |
08baf561 ED |
328 | if (status == NETDEV_TX_OK) |
329 | txq_trans_update(txq); | |
330 | } | |
fd2ea0a7 | 331 | __netif_tx_unlock(txq); |
e37b8d93 AM |
332 | |
333 | if (status == NETDEV_TX_OK) | |
334 | break; | |
335 | ||
e37b8d93 | 336 | } |
0db3dc73 SH |
337 | |
338 | /* tickle device maybe there is some cleanup */ | |
339 | netpoll_poll(np); | |
340 | ||
341 | udelay(USEC_PER_POLL); | |
0db1d6fc | 342 | } |
79b1bee8 DD |
343 | |
344 | WARN_ONCE(!irqs_disabled(), | |
345 | "netpoll_send_skb(): %s enabled interrupts in poll (%pF)\n", | |
346 | dev->name, ops->ndo_start_xmit); | |
347 | ||
a49f99ff | 348 | local_irq_restore(flags); |
1da177e4 | 349 | } |
1da177e4 | 350 | |
2bdfe0ba | 351 | if (status != NETDEV_TX_OK) { |
5de4a473 | 352 | skb_queue_tail(&npinfo->txq, skb); |
4c1ac1b4 | 353 | schedule_delayed_work(&npinfo->tx_work,0); |
1da177e4 | 354 | } |
1da177e4 | 355 | } |
c2355e1a | 356 | EXPORT_SYMBOL(netpoll_send_skb_on_dev); |
1da177e4 LT |
357 | |
358 | void netpoll_send_udp(struct netpoll *np, const char *msg, int len) | |
359 | { | |
360 | int total_len, eth_len, ip_len, udp_len; | |
361 | struct sk_buff *skb; | |
362 | struct udphdr *udph; | |
363 | struct iphdr *iph; | |
364 | struct ethhdr *eth; | |
365 | ||
366 | udp_len = len + sizeof(*udph); | |
367 | ip_len = eth_len = udp_len + sizeof(*iph); | |
368 | total_len = eth_len + ETH_HLEN + NET_IP_ALIGN; | |
369 | ||
370 | skb = find_skb(np, total_len, total_len - len); | |
371 | if (!skb) | |
372 | return; | |
373 | ||
27d7ff46 | 374 | skb_copy_to_linear_data(skb, msg, len); |
1da177e4 LT |
375 | skb->len += len; |
376 | ||
4bedb452 ACM |
377 | skb_push(skb, sizeof(*udph)); |
378 | skb_reset_transport_header(skb); | |
379 | udph = udp_hdr(skb); | |
1da177e4 LT |
380 | udph->source = htons(np->local_port); |
381 | udph->dest = htons(np->remote_port); | |
382 | udph->len = htons(udp_len); | |
383 | udph->check = 0; | |
e7557af5 HH |
384 | udph->check = csum_tcpudp_magic(np->local_ip, |
385 | np->remote_ip, | |
8e365eec | 386 | udp_len, IPPROTO_UDP, |
07f0757a | 387 | csum_partial(udph, udp_len, 0)); |
8e365eec | 388 | if (udph->check == 0) |
5e57dff2 | 389 | udph->check = CSUM_MANGLED_0; |
1da177e4 | 390 | |
e2d1bca7 ACM |
391 | skb_push(skb, sizeof(*iph)); |
392 | skb_reset_network_header(skb); | |
eddc9ec5 | 393 | iph = ip_hdr(skb); |
1da177e4 LT |
394 | |
395 | /* iph->version = 4; iph->ihl = 5; */ | |
396 | put_unaligned(0x45, (unsigned char *)iph); | |
397 | iph->tos = 0; | |
398 | put_unaligned(htons(ip_len), &(iph->tot_len)); | |
399 | iph->id = 0; | |
400 | iph->frag_off = 0; | |
401 | iph->ttl = 64; | |
402 | iph->protocol = IPPROTO_UDP; | |
403 | iph->check = 0; | |
e7557af5 HH |
404 | put_unaligned(np->local_ip, &(iph->saddr)); |
405 | put_unaligned(np->remote_ip, &(iph->daddr)); | |
1da177e4 LT |
406 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
407 | ||
408 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); | |
459a98ed | 409 | skb_reset_mac_header(skb); |
206daaf7 | 410 | skb->protocol = eth->h_proto = htons(ETH_P_IP); |
09538641 SH |
411 | memcpy(eth->h_source, np->dev->dev_addr, ETH_ALEN); |
412 | memcpy(eth->h_dest, np->remote_mac, ETH_ALEN); | |
1da177e4 LT |
413 | |
414 | skb->dev = np->dev; | |
415 | ||
416 | netpoll_send_skb(np, skb); | |
417 | } | |
9e34a5b5 | 418 | EXPORT_SYMBOL(netpoll_send_udp); |
1da177e4 LT |
419 | |
420 | static void arp_reply(struct sk_buff *skb) | |
421 | { | |
115c1d6e | 422 | struct netpoll_info *npinfo = skb->dev->npinfo; |
1da177e4 LT |
423 | struct arphdr *arp; |
424 | unsigned char *arp_ptr; | |
425 | int size, type = ARPOP_REPLY, ptype = ETH_P_ARP; | |
252e3346 | 426 | __be32 sip, tip; |
47bbec02 | 427 | unsigned char *sha; |
1da177e4 | 428 | struct sk_buff *send_skb; |
508e14b4 DB |
429 | struct netpoll *np, *tmp; |
430 | unsigned long flags; | |
431 | int hits = 0; | |
432 | ||
433 | if (list_empty(&npinfo->rx_np)) | |
434 | return; | |
435 | ||
436 | /* Before checking the packet, we do some early | |
437 | inspection whether this is interesting at all */ | |
438 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
439 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { | |
440 | if (np->dev == skb->dev) | |
441 | hits++; | |
442 | } | |
443 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
1da177e4 | 444 | |
508e14b4 DB |
445 | /* No netpoll struct is using this dev */ |
446 | if (!hits) | |
115c1d6e | 447 | return; |
1da177e4 LT |
448 | |
449 | /* No arp on this interface */ | |
450 | if (skb->dev->flags & IFF_NOARP) | |
451 | return; | |
452 | ||
988b7050 | 453 | if (!pskb_may_pull(skb, arp_hdr_len(skb->dev))) |
1da177e4 LT |
454 | return; |
455 | ||
c1d2bbe1 | 456 | skb_reset_network_header(skb); |
badff6d0 | 457 | skb_reset_transport_header(skb); |
d0a92be0 | 458 | arp = arp_hdr(skb); |
1da177e4 LT |
459 | |
460 | if ((arp->ar_hrd != htons(ARPHRD_ETHER) && | |
461 | arp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
462 | arp->ar_pro != htons(ETH_P_IP) || | |
463 | arp->ar_op != htons(ARPOP_REQUEST)) | |
464 | return; | |
465 | ||
47bbec02 NH |
466 | arp_ptr = (unsigned char *)(arp+1); |
467 | /* save the location of the src hw addr */ | |
468 | sha = arp_ptr; | |
469 | arp_ptr += skb->dev->addr_len; | |
1da177e4 | 470 | memcpy(&sip, arp_ptr, 4); |
47bbec02 | 471 | arp_ptr += 4; |
508e14b4 DB |
472 | /* If we actually cared about dst hw addr, |
473 | it would get copied here */ | |
47bbec02 | 474 | arp_ptr += skb->dev->addr_len; |
1da177e4 LT |
475 | memcpy(&tip, arp_ptr, 4); |
476 | ||
477 | /* Should we ignore arp? */ | |
508e14b4 | 478 | if (ipv4_is_loopback(tip) || ipv4_is_multicast(tip)) |
1da177e4 LT |
479 | return; |
480 | ||
988b7050 | 481 | size = arp_hdr_len(skb->dev); |
1da177e4 | 482 | |
508e14b4 DB |
483 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
484 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { | |
485 | if (tip != np->local_ip) | |
486 | continue; | |
1da177e4 | 487 | |
508e14b4 DB |
488 | send_skb = find_skb(np, size + LL_ALLOCATED_SPACE(np->dev), |
489 | LL_RESERVED_SPACE(np->dev)); | |
490 | if (!send_skb) | |
491 | continue; | |
1da177e4 | 492 | |
508e14b4 DB |
493 | skb_reset_network_header(send_skb); |
494 | arp = (struct arphdr *) skb_put(send_skb, size); | |
495 | send_skb->dev = skb->dev; | |
496 | send_skb->protocol = htons(ETH_P_ARP); | |
1da177e4 | 497 | |
508e14b4 DB |
498 | /* Fill the device header for the ARP frame */ |
499 | if (dev_hard_header(send_skb, skb->dev, ptype, | |
500 | sha, np->dev->dev_addr, | |
501 | send_skb->len) < 0) { | |
502 | kfree_skb(send_skb); | |
503 | continue; | |
504 | } | |
1da177e4 | 505 | |
508e14b4 DB |
506 | /* |
507 | * Fill out the arp protocol part. | |
508 | * | |
509 | * we only support ethernet device type, | |
510 | * which (according to RFC 1390) should | |
511 | * always equal 1 (Ethernet). | |
512 | */ | |
1da177e4 | 513 | |
508e14b4 DB |
514 | arp->ar_hrd = htons(np->dev->type); |
515 | arp->ar_pro = htons(ETH_P_IP); | |
516 | arp->ar_hln = np->dev->addr_len; | |
517 | arp->ar_pln = 4; | |
518 | arp->ar_op = htons(type); | |
519 | ||
520 | arp_ptr = (unsigned char *)(arp + 1); | |
521 | memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len); | |
522 | arp_ptr += np->dev->addr_len; | |
523 | memcpy(arp_ptr, &tip, 4); | |
524 | arp_ptr += 4; | |
525 | memcpy(arp_ptr, sha, np->dev->addr_len); | |
526 | arp_ptr += np->dev->addr_len; | |
527 | memcpy(arp_ptr, &sip, 4); | |
528 | ||
529 | netpoll_send_skb(np, send_skb); | |
530 | ||
531 | /* If there are several rx_hooks for the same address, | |
532 | we're fine by sending a single reply */ | |
533 | break; | |
534 | } | |
535 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
1da177e4 LT |
536 | } |
537 | ||
538 | int __netpoll_rx(struct sk_buff *skb) | |
539 | { | |
540 | int proto, len, ulen; | |
508e14b4 | 541 | int hits = 0; |
b71d1d42 | 542 | const struct iphdr *iph; |
1da177e4 | 543 | struct udphdr *uh; |
508e14b4 DB |
544 | struct netpoll_info *npinfo = skb->dev->npinfo; |
545 | struct netpoll *np, *tmp; | |
068c6e98 | 546 | |
508e14b4 | 547 | if (list_empty(&npinfo->rx_np)) |
1da177e4 | 548 | goto out; |
508e14b4 | 549 | |
1da177e4 LT |
550 | if (skb->dev->type != ARPHRD_ETHER) |
551 | goto out; | |
552 | ||
d9452e9f | 553 | /* check if netpoll clients need ARP */ |
724800d6 | 554 | if (skb->protocol == htons(ETH_P_ARP) && |
1da177e4 | 555 | atomic_read(&trapped)) { |
508e14b4 | 556 | skb_queue_tail(&npinfo->arp_tx, skb); |
1da177e4 LT |
557 | return 1; |
558 | } | |
559 | ||
560 | proto = ntohs(eth_hdr(skb)->h_proto); | |
561 | if (proto != ETH_P_IP) | |
562 | goto out; | |
563 | if (skb->pkt_type == PACKET_OTHERHOST) | |
564 | goto out; | |
565 | if (skb_shared(skb)) | |
566 | goto out; | |
567 | ||
568 | iph = (struct iphdr *)skb->data; | |
569 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) | |
570 | goto out; | |
571 | if (iph->ihl < 5 || iph->version != 4) | |
572 | goto out; | |
573 | if (!pskb_may_pull(skb, iph->ihl*4)) | |
574 | goto out; | |
575 | if (ip_fast_csum((u8 *)iph, iph->ihl) != 0) | |
576 | goto out; | |
577 | ||
578 | len = ntohs(iph->tot_len); | |
579 | if (skb->len < len || len < iph->ihl*4) | |
580 | goto out; | |
581 | ||
5e7d7fa5 AL |
582 | /* |
583 | * Our transport medium may have padded the buffer out. | |
584 | * Now We trim to the true length of the frame. | |
585 | */ | |
586 | if (pskb_trim_rcsum(skb, len)) | |
587 | goto out; | |
588 | ||
1da177e4 LT |
589 | if (iph->protocol != IPPROTO_UDP) |
590 | goto out; | |
591 | ||
592 | len -= iph->ihl*4; | |
593 | uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); | |
594 | ulen = ntohs(uh->len); | |
595 | ||
596 | if (ulen != len) | |
597 | goto out; | |
fb286bb2 | 598 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr)) |
1da177e4 | 599 | goto out; |
1da177e4 | 600 | |
508e14b4 DB |
601 | list_for_each_entry_safe(np, tmp, &npinfo->rx_np, rx) { |
602 | if (np->local_ip && np->local_ip != iph->daddr) | |
603 | continue; | |
604 | if (np->remote_ip && np->remote_ip != iph->saddr) | |
605 | continue; | |
606 | if (np->local_port && np->local_port != ntohs(uh->dest)) | |
607 | continue; | |
608 | ||
609 | np->rx_hook(np, ntohs(uh->source), | |
610 | (char *)(uh+1), | |
611 | ulen - sizeof(struct udphdr)); | |
612 | hits++; | |
613 | } | |
614 | ||
615 | if (!hits) | |
616 | goto out; | |
1da177e4 LT |
617 | |
618 | kfree_skb(skb); | |
619 | return 1; | |
620 | ||
621 | out: | |
622 | if (atomic_read(&trapped)) { | |
623 | kfree_skb(skb); | |
624 | return 1; | |
625 | } | |
626 | ||
627 | return 0; | |
628 | } | |
629 | ||
0bcc1816 SS |
630 | void netpoll_print_options(struct netpoll *np) |
631 | { | |
632 | printk(KERN_INFO "%s: local port %d\n", | |
633 | np->name, np->local_port); | |
e7557af5 HH |
634 | printk(KERN_INFO "%s: local IP %pI4\n", |
635 | np->name, &np->local_ip); | |
5fc05f87 | 636 | printk(KERN_INFO "%s: interface '%s'\n", |
0bcc1816 SS |
637 | np->name, np->dev_name); |
638 | printk(KERN_INFO "%s: remote port %d\n", | |
639 | np->name, np->remote_port); | |
e7557af5 HH |
640 | printk(KERN_INFO "%s: remote IP %pI4\n", |
641 | np->name, &np->remote_ip); | |
e174961c JB |
642 | printk(KERN_INFO "%s: remote ethernet address %pM\n", |
643 | np->name, np->remote_mac); | |
0bcc1816 | 644 | } |
9e34a5b5 | 645 | EXPORT_SYMBOL(netpoll_print_options); |
0bcc1816 | 646 | |
1da177e4 LT |
647 | int netpoll_parse_options(struct netpoll *np, char *opt) |
648 | { | |
649 | char *cur=opt, *delim; | |
650 | ||
c68b9070 | 651 | if (*cur != '@') { |
1da177e4 LT |
652 | if ((delim = strchr(cur, '@')) == NULL) |
653 | goto parse_failed; | |
c68b9070 DM |
654 | *delim = 0; |
655 | np->local_port = simple_strtol(cur, NULL, 10); | |
656 | cur = delim; | |
1da177e4 LT |
657 | } |
658 | cur++; | |
1da177e4 | 659 | |
c68b9070 | 660 | if (*cur != '/') { |
1da177e4 LT |
661 | if ((delim = strchr(cur, '/')) == NULL) |
662 | goto parse_failed; | |
c68b9070 | 663 | *delim = 0; |
e7557af5 | 664 | np->local_ip = in_aton(cur); |
c68b9070 | 665 | cur = delim; |
1da177e4 LT |
666 | } |
667 | cur++; | |
668 | ||
c68b9070 | 669 | if (*cur != ',') { |
1da177e4 LT |
670 | /* parse out dev name */ |
671 | if ((delim = strchr(cur, ',')) == NULL) | |
672 | goto parse_failed; | |
c68b9070 | 673 | *delim = 0; |
1da177e4 | 674 | strlcpy(np->dev_name, cur, sizeof(np->dev_name)); |
c68b9070 | 675 | cur = delim; |
1da177e4 LT |
676 | } |
677 | cur++; | |
678 | ||
c68b9070 | 679 | if (*cur != '@') { |
1da177e4 LT |
680 | /* dst port */ |
681 | if ((delim = strchr(cur, '@')) == NULL) | |
682 | goto parse_failed; | |
c68b9070 | 683 | *delim = 0; |
5fc05f87 AW |
684 | if (*cur == ' ' || *cur == '\t') |
685 | printk(KERN_INFO "%s: warning: whitespace" | |
686 | "is not allowed\n", np->name); | |
c68b9070 DM |
687 | np->remote_port = simple_strtol(cur, NULL, 10); |
688 | cur = delim; | |
1da177e4 LT |
689 | } |
690 | cur++; | |
1da177e4 LT |
691 | |
692 | /* dst ip */ | |
693 | if ((delim = strchr(cur, '/')) == NULL) | |
694 | goto parse_failed; | |
c68b9070 | 695 | *delim = 0; |
e7557af5 | 696 | np->remote_ip = in_aton(cur); |
c68b9070 | 697 | cur = delim + 1; |
1da177e4 | 698 | |
c68b9070 | 699 | if (*cur != 0) { |
1da177e4 | 700 | /* MAC address */ |
4940fc88 | 701 | if (!mac_pton(cur, np->remote_mac)) |
1da177e4 | 702 | goto parse_failed; |
1da177e4 LT |
703 | } |
704 | ||
0bcc1816 | 705 | netpoll_print_options(np); |
1da177e4 LT |
706 | |
707 | return 0; | |
708 | ||
709 | parse_failed: | |
5fc05f87 | 710 | printk(KERN_INFO "%s: couldn't parse config at '%s'!\n", |
1da177e4 LT |
711 | np->name, cur); |
712 | return -1; | |
713 | } | |
9e34a5b5 | 714 | EXPORT_SYMBOL(netpoll_parse_options); |
1da177e4 | 715 | |
8fdd95ec | 716 | int __netpoll_setup(struct netpoll *np) |
1da177e4 | 717 | { |
8fdd95ec | 718 | struct net_device *ndev = np->dev; |
115c1d6e | 719 | struct netpoll_info *npinfo; |
4247e161 | 720 | const struct net_device_ops *ops; |
fbeec2e1 | 721 | unsigned long flags; |
b41848b6 | 722 | int err; |
1da177e4 | 723 | |
8fdd95ec HX |
724 | if ((ndev->priv_flags & IFF_DISABLE_NETPOLL) || |
725 | !ndev->netdev_ops->ndo_poll_controller) { | |
726 | printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n", | |
727 | np->name, np->dev_name); | |
728 | err = -ENOTSUPP; | |
729 | goto out; | |
730 | } | |
731 | ||
732 | if (!ndev->npinfo) { | |
733 | npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); | |
734 | if (!npinfo) { | |
735 | err = -ENOMEM; | |
736 | goto out; | |
737 | } | |
738 | ||
739 | npinfo->rx_flags = 0; | |
740 | INIT_LIST_HEAD(&npinfo->rx_np); | |
741 | ||
742 | spin_lock_init(&npinfo->rx_lock); | |
743 | skb_queue_head_init(&npinfo->arp_tx); | |
744 | skb_queue_head_init(&npinfo->txq); | |
745 | INIT_DELAYED_WORK(&npinfo->tx_work, queue_process); | |
746 | ||
747 | atomic_set(&npinfo->refcnt, 1); | |
748 | ||
749 | ops = np->dev->netdev_ops; | |
750 | if (ops->ndo_netpoll_setup) { | |
751 | err = ops->ndo_netpoll_setup(ndev, npinfo); | |
752 | if (err) | |
753 | goto free_npinfo; | |
754 | } | |
755 | } else { | |
756 | npinfo = ndev->npinfo; | |
757 | atomic_inc(&npinfo->refcnt); | |
758 | } | |
759 | ||
760 | npinfo->netpoll = np; | |
761 | ||
762 | if (np->rx_hook) { | |
763 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
764 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; | |
765 | list_add_tail(&np->rx, &npinfo->rx_np); | |
766 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
767 | } | |
768 | ||
769 | /* last thing to do is link it to the net device structure */ | |
770 | rcu_assign_pointer(ndev->npinfo, npinfo); | |
8fdd95ec HX |
771 | |
772 | return 0; | |
773 | ||
774 | free_npinfo: | |
775 | kfree(npinfo); | |
776 | out: | |
777 | return err; | |
778 | } | |
779 | EXPORT_SYMBOL_GPL(__netpoll_setup); | |
780 | ||
781 | int netpoll_setup(struct netpoll *np) | |
782 | { | |
783 | struct net_device *ndev = NULL; | |
784 | struct in_device *in_dev; | |
785 | int err; | |
786 | ||
1da177e4 | 787 | if (np->dev_name) |
881d966b | 788 | ndev = dev_get_by_name(&init_net, np->dev_name); |
1da177e4 LT |
789 | if (!ndev) { |
790 | printk(KERN_ERR "%s: %s doesn't exist, aborting.\n", | |
791 | np->name, np->dev_name); | |
b41848b6 | 792 | return -ENODEV; |
1da177e4 LT |
793 | } |
794 | ||
0c1ad04a WC |
795 | if (ndev->master) { |
796 | printk(KERN_ERR "%s: %s is a slave device, aborting.\n", | |
797 | np->name, np->dev_name); | |
798 | return -EBUSY; | |
799 | } | |
800 | ||
1da177e4 LT |
801 | if (!netif_running(ndev)) { |
802 | unsigned long atmost, atleast; | |
803 | ||
804 | printk(KERN_INFO "%s: device %s not up yet, forcing it\n", | |
805 | np->name, np->dev_name); | |
806 | ||
6756ae4b | 807 | rtnl_lock(); |
b41848b6 SH |
808 | err = dev_open(ndev); |
809 | rtnl_unlock(); | |
810 | ||
811 | if (err) { | |
1da177e4 | 812 | printk(KERN_ERR "%s: failed to open %s\n", |
b41848b6 | 813 | np->name, ndev->name); |
dbaa1541 | 814 | goto put; |
1da177e4 | 815 | } |
1da177e4 LT |
816 | |
817 | atleast = jiffies + HZ/10; | |
bff38771 | 818 | atmost = jiffies + carrier_timeout * HZ; |
1da177e4 LT |
819 | while (!netif_carrier_ok(ndev)) { |
820 | if (time_after(jiffies, atmost)) { | |
821 | printk(KERN_NOTICE | |
822 | "%s: timeout waiting for carrier\n", | |
823 | np->name); | |
824 | break; | |
825 | } | |
1b614fb9 | 826 | msleep(1); |
1da177e4 LT |
827 | } |
828 | ||
829 | /* If carrier appears to come up instantly, we don't | |
830 | * trust it and pause so that we don't pump all our | |
831 | * queued console messages into the bitbucket. | |
832 | */ | |
833 | ||
834 | if (time_before(jiffies, atleast)) { | |
835 | printk(KERN_NOTICE "%s: carrier detect appears" | |
836 | " untrustworthy, waiting 4 seconds\n", | |
837 | np->name); | |
838 | msleep(4000); | |
839 | } | |
840 | } | |
841 | ||
1da177e4 LT |
842 | if (!np->local_ip) { |
843 | rcu_read_lock(); | |
e5ed6399 | 844 | in_dev = __in_dev_get_rcu(ndev); |
1da177e4 LT |
845 | |
846 | if (!in_dev || !in_dev->ifa_list) { | |
847 | rcu_read_unlock(); | |
848 | printk(KERN_ERR "%s: no IP address for %s, aborting\n", | |
849 | np->name, np->dev_name); | |
b41848b6 | 850 | err = -EDESTADDRREQ; |
dbaa1541 | 851 | goto put; |
1da177e4 LT |
852 | } |
853 | ||
e7557af5 | 854 | np->local_ip = in_dev->ifa_list->ifa_local; |
1da177e4 | 855 | rcu_read_unlock(); |
e7557af5 | 856 | printk(KERN_INFO "%s: local IP %pI4\n", np->name, &np->local_ip); |
1da177e4 LT |
857 | } |
858 | ||
dbaa1541 HX |
859 | np->dev = ndev; |
860 | ||
861 | /* fill up the skb queue */ | |
862 | refill_skbs(); | |
863 | ||
864 | rtnl_lock(); | |
8fdd95ec | 865 | err = __netpoll_setup(np); |
dbaa1541 | 866 | rtnl_unlock(); |
53fb95d3 | 867 | |
8fdd95ec HX |
868 | if (err) |
869 | goto put; | |
870 | ||
1da177e4 LT |
871 | return 0; |
872 | ||
21edbb22 | 873 | put: |
1da177e4 | 874 | dev_put(ndev); |
b41848b6 | 875 | return err; |
1da177e4 | 876 | } |
9e34a5b5 | 877 | EXPORT_SYMBOL(netpoll_setup); |
1da177e4 | 878 | |
c68b9070 DM |
879 | static int __init netpoll_init(void) |
880 | { | |
a1bcfacd SH |
881 | skb_queue_head_init(&skb_pool); |
882 | return 0; | |
883 | } | |
884 | core_initcall(netpoll_init); | |
885 | ||
8fdd95ec | 886 | void __netpoll_cleanup(struct netpoll *np) |
1da177e4 | 887 | { |
fbeec2e1 JM |
888 | struct netpoll_info *npinfo; |
889 | unsigned long flags; | |
890 | ||
8fdd95ec HX |
891 | npinfo = np->dev->npinfo; |
892 | if (!npinfo) | |
dbaa1541 | 893 | return; |
93ec2c72 | 894 | |
8fdd95ec HX |
895 | if (!list_empty(&npinfo->rx_np)) { |
896 | spin_lock_irqsave(&npinfo->rx_lock, flags); | |
897 | list_del(&np->rx); | |
898 | if (list_empty(&npinfo->rx_np)) | |
899 | npinfo->rx_flags &= ~NETPOLL_RX_ENABLED; | |
900 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); | |
901 | } | |
de85d99e | 902 | |
8fdd95ec HX |
903 | if (atomic_dec_and_test(&npinfo->refcnt)) { |
904 | const struct net_device_ops *ops; | |
de85d99e | 905 | |
8fdd95ec HX |
906 | ops = np->dev->netdev_ops; |
907 | if (ops->ndo_netpoll_cleanup) | |
908 | ops->ndo_netpoll_cleanup(np->dev); | |
de85d99e | 909 | |
8fdd95ec | 910 | rcu_assign_pointer(np->dev->npinfo, NULL); |
de85d99e | 911 | |
dbaa1541 HX |
912 | /* avoid racing with NAPI reading npinfo */ |
913 | synchronize_rcu_bh(); | |
93ec2c72 | 914 | |
dbaa1541 HX |
915 | skb_queue_purge(&npinfo->arp_tx); |
916 | skb_queue_purge(&npinfo->txq); | |
afe2c511 | 917 | cancel_delayed_work_sync(&npinfo->tx_work); |
93ec2c72 | 918 | |
dbaa1541 HX |
919 | /* clean after last, unfinished work */ |
920 | __skb_queue_purge(&npinfo->txq); | |
921 | kfree(npinfo); | |
115c1d6e | 922 | } |
8fdd95ec HX |
923 | } |
924 | EXPORT_SYMBOL_GPL(__netpoll_cleanup); | |
fbeec2e1 | 925 | |
8fdd95ec HX |
926 | void netpoll_cleanup(struct netpoll *np) |
927 | { | |
928 | if (!np->dev) | |
929 | return; | |
dbaa1541 | 930 | |
8fdd95ec HX |
931 | rtnl_lock(); |
932 | __netpoll_cleanup(np); | |
933 | rtnl_unlock(); | |
934 | ||
935 | dev_put(np->dev); | |
1da177e4 LT |
936 | np->dev = NULL; |
937 | } | |
9e34a5b5 | 938 | EXPORT_SYMBOL(netpoll_cleanup); |
1da177e4 LT |
939 | |
940 | int netpoll_trap(void) | |
941 | { | |
942 | return atomic_read(&trapped); | |
943 | } | |
9e34a5b5 | 944 | EXPORT_SYMBOL(netpoll_trap); |
1da177e4 LT |
945 | |
946 | void netpoll_set_trap(int trap) | |
947 | { | |
948 | if (trap) | |
949 | atomic_inc(&trapped); | |
950 | else | |
951 | atomic_dec(&trapped); | |
952 | } | |
1da177e4 | 953 | EXPORT_SYMBOL(netpoll_set_trap); |