blob: 156d7fd81fec1829edd5bbe178c3179db79c1005 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020016#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -080038static struct sk_buff_head skb_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static atomic_t trapped;
41
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -070042#define USEC_PER_POLL 50
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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
50static void zap_completion_queue(void);
Neil Horman068c6e92006-06-26 00:04:27 -070051static void arp_reply(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
David Howellsc4028952006-11-22 14:57:56 +000053static void queue_process(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
David Howells4c1ac1b2006-12-05 14:37:56 +000055 struct netpoll_info *npinfo =
56 container_of(work, struct netpoll_info, tx_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct sk_buff *skb;
58
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070059 while ((skb = skb_dequeue(&npinfo->txq))) {
60 struct net_device *dev = skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070062 if (!netif_device_present(dev) || !netif_running(dev)) {
63 __kfree_skb(skb);
64 continue;
65 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070067 netif_tx_lock_bh(dev);
68 if (netif_queue_stopped(dev) ||
69 dev->hard_start_xmit(skb, dev) != NETDEV_TX_OK) {
70 skb_queue_head(&npinfo->txq, skb);
71 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Stephen Hemminger6c43ff12006-10-26 15:46:53 -070073 schedule_delayed_work(&npinfo->tx_work, HZ/10);
74 return;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77}
78
Al Virob51655b2006-11-14 21:40:42 -080079static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
80 unsigned short ulen, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Al Virod6f5493c2006-11-14 21:26:08 -080082 __wsum psum;
Herbert Xufb286bb2005-11-10 13:01:24 -080083
84 if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86
Herbert Xufb286bb2005-11-10 13:01:24 -080087 psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Patrick McHardy84fa7932006-08-29 16:44:56 -070089 if (skb->ip_summed == CHECKSUM_COMPLETE &&
Al Virod3bc23e2006-11-14 21:24:49 -080090 !csum_fold(csum_add(psum, skb->csum)))
Herbert Xufb286bb2005-11-10 13:01:24 -080091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Herbert Xufb286bb2005-11-10 13:01:24 -080093 skb->csum = psum;
94
95 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98/*
99 * Check whether delayed processing was scheduled for our NIC. If so,
100 * we attempt to grab the poll lock and use ->poll() to pump the card.
101 * If this fails, either we've recursed in ->poll() or it's already
102 * running on another CPU.
103 *
104 * Note: we don't mask interrupts with this lock because we're using
105 * trylock here and interrupts are already disabled in the softirq
106 * case. Further, we test the poll_owner to avoid recursion on UP
107 * systems where the lock doesn't exist.
108 *
109 * In cases where there is bi-directional communications, reading only
110 * one message at a time can lead to packets being dropped by the
111 * network adapter, forcing superfluous retries and possibly timeouts.
112 * Thus, we set our budget to greater than 1.
113 */
114static void poll_napi(struct netpoll *np)
115{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700116 struct netpoll_info *npinfo = np->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int budget = 16;
118
119 if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
Jeff Moyer115c1d62005-06-22 22:05:31 -0700120 npinfo->poll_owner != smp_processor_id() &&
121 spin_trylock(&npinfo->poll_lock)) {
122 npinfo->rx_flags |= NETPOLL_RX_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 atomic_inc(&trapped);
124
125 np->dev->poll(np->dev, &budget);
126
127 atomic_dec(&trapped);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700128 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
129 spin_unlock(&npinfo->poll_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131}
132
Neil Horman068c6e92006-06-26 00:04:27 -0700133static void service_arp_queue(struct netpoll_info *npi)
134{
135 struct sk_buff *skb;
136
137 if (unlikely(!npi))
138 return;
139
140 skb = skb_dequeue(&npi->arp_tx);
141
142 while (skb != NULL) {
143 arp_reply(skb);
144 skb = skb_dequeue(&npi->arp_tx);
145 }
Neil Horman068c6e92006-06-26 00:04:27 -0700146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148void netpoll_poll(struct netpoll *np)
149{
David S. Millerc68b9072006-11-14 20:40:49 -0800150 if (!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return;
152
153 /* Process pending work on NIC */
154 np->dev->poll_controller(np->dev);
155 if (np->dev->poll)
156 poll_napi(np);
157
Neil Horman068c6e92006-06-26 00:04:27 -0700158 service_arp_queue(np->dev->npinfo);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 zap_completion_queue();
161}
162
163static void refill_skbs(void)
164{
165 struct sk_buff *skb;
166 unsigned long flags;
167
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800168 spin_lock_irqsave(&skb_pool.lock, flags);
169 while (skb_pool.qlen < MAX_SKBS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
171 if (!skb)
172 break;
173
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800174 __skb_queue_tail(&skb_pool, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800176 spin_unlock_irqrestore(&skb_pool.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static void zap_completion_queue(void)
180{
181 unsigned long flags;
182 struct softnet_data *sd = &get_cpu_var(softnet_data);
183
184 if (sd->completion_queue) {
185 struct sk_buff *clist;
186
187 local_irq_save(flags);
188 clist = sd->completion_queue;
189 sd->completion_queue = NULL;
190 local_irq_restore(flags);
191
192 while (clist != NULL) {
193 struct sk_buff *skb = clist;
194 clist = clist->next;
David S. Millerc68b9072006-11-14 20:40:49 -0800195 if (skb->destructor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 dev_kfree_skb_any(skb); /* put this one back */
197 else
198 __kfree_skb(skb);
199 }
200 }
201
202 put_cpu_var(softnet_data);
203}
204
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800205static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800207 int count = 0;
208 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 zap_completion_queue();
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800211 refill_skbs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212repeat:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 skb = alloc_skb(len, GFP_ATOMIC);
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800215 if (!skb)
216 skb = skb_dequeue(&skb_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (!skb) {
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800219 if (++count < 10) {
220 netpoll_poll(np);
221 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800223 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 atomic_set(&skb->users, 1);
227 skb_reserve(skb, reserve);
228 return skb;
229}
230
231static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
232{
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700233 int status = NETDEV_TX_BUSY;
234 unsigned long tries;
235 struct net_device *dev = np->dev;
236 struct netpoll_info *npinfo = np->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700238 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
239 __kfree_skb(skb);
240 return;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700243 /* don't get messages out of order, and no recursion */
David S. Millerc68b9072006-11-14 20:40:49 -0800244 if (skb_queue_len(&npinfo->txq) == 0 &&
Andrew Mortone37b8d92006-12-09 14:01:49 -0800245 npinfo->poll_owner != smp_processor_id()) {
246 local_bh_disable(); /* Where's netif_tx_trylock_bh()? */
247 if (netif_tx_trylock(dev)) {
248 /* try until next clock tick */
249 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
250 tries > 0; --tries) {
251 if (!netif_queue_stopped(dev))
252 status = dev->hard_start_xmit(skb, dev);
Matt Mackallf0d34592005-08-11 19:25:11 -0700253
Andrew Mortone37b8d92006-12-09 14:01:49 -0800254 if (status == NETDEV_TX_OK)
255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Andrew Mortone37b8d92006-12-09 14:01:49 -0800257 /* tickle device maybe there is some cleanup */
258 netpoll_poll(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Andrew Mortone37b8d92006-12-09 14:01:49 -0800260 udelay(USEC_PER_POLL);
261 }
262 netif_tx_unlock(dev);
Matt Mackall0db1d6f2005-08-11 19:25:54 -0700263 }
Andrew Mortone37b8d92006-12-09 14:01:49 -0800264 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700267 if (status != NETDEV_TX_OK) {
Stephen Hemminger5de4a472006-10-26 15:46:55 -0700268 skb_queue_tail(&npinfo->txq, skb);
David Howells4c1ac1b2006-12-05 14:37:56 +0000269 schedule_delayed_work(&npinfo->tx_work,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
274{
275 int total_len, eth_len, ip_len, udp_len;
276 struct sk_buff *skb;
277 struct udphdr *udph;
278 struct iphdr *iph;
279 struct ethhdr *eth;
280
281 udp_len = len + sizeof(*udph);
282 ip_len = eth_len = udp_len + sizeof(*iph);
283 total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
284
285 skb = find_skb(np, total_len, total_len - len);
286 if (!skb)
287 return;
288
289 memcpy(skb->data, msg, len);
290 skb->len += len;
291
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700292 skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 udph->source = htons(np->local_port);
294 udph->dest = htons(np->remote_port);
295 udph->len = htons(udp_len);
296 udph->check = 0;
Chris Lalancette8e365ee2006-11-07 14:56:19 -0800297 udph->check = csum_tcpudp_magic(htonl(np->local_ip),
298 htonl(np->remote_ip),
299 udp_len, IPPROTO_UDP,
300 csum_partial((unsigned char *)udph, udp_len, 0));
301 if (udph->check == 0)
Al Viro5e57dff2006-11-20 18:08:13 -0800302 udph->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700304 skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* iph->version = 4; iph->ihl = 5; */
307 put_unaligned(0x45, (unsigned char *)iph);
308 iph->tos = 0;
309 put_unaligned(htons(ip_len), &(iph->tot_len));
310 iph->id = 0;
311 iph->frag_off = 0;
312 iph->ttl = 64;
313 iph->protocol = IPPROTO_UDP;
314 iph->check = 0;
315 put_unaligned(htonl(np->local_ip), &(iph->saddr));
316 put_unaligned(htonl(np->remote_ip), &(iph->daddr));
317 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
318
319 eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
Stephen Hemminger206daaf2006-10-19 23:58:23 -0700320 skb->mac.raw = skb->data;
321 skb->protocol = eth->h_proto = htons(ETH_P_IP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 memcpy(eth->h_source, np->local_mac, 6);
323 memcpy(eth->h_dest, np->remote_mac, 6);
324
325 skb->dev = np->dev;
326
327 netpoll_send_skb(np, skb);
328}
329
330static void arp_reply(struct sk_buff *skb)
331{
Jeff Moyer115c1d62005-06-22 22:05:31 -0700332 struct netpoll_info *npinfo = skb->dev->npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct arphdr *arp;
334 unsigned char *arp_ptr;
335 int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
Al Viro252e33462006-11-14 20:48:11 -0800336 __be32 sip, tip;
Neil Horman47bbec02006-12-08 00:05:55 -0800337 unsigned char *sha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct sk_buff *send_skb;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700339 struct netpoll *np = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700341 if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
342 np = npinfo->rx_np;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700343 if (!np)
344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* No arp on this interface */
347 if (skb->dev->flags & IFF_NOARP)
348 return;
349
350 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
351 (2 * skb->dev->addr_len) +
352 (2 * sizeof(u32)))))
353 return;
354
355 skb->h.raw = skb->nh.raw = skb->data;
356 arp = skb->nh.arph;
357
358 if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
359 arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
360 arp->ar_pro != htons(ETH_P_IP) ||
361 arp->ar_op != htons(ARPOP_REQUEST))
362 return;
363
Neil Horman47bbec02006-12-08 00:05:55 -0800364 arp_ptr = (unsigned char *)(arp+1);
365 /* save the location of the src hw addr */
366 sha = arp_ptr;
367 arp_ptr += skb->dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 memcpy(&sip, arp_ptr, 4);
Neil Horman47bbec02006-12-08 00:05:55 -0800369 arp_ptr += 4;
370 /* if we actually cared about dst hw addr, it would get copied here */
371 arp_ptr += skb->dev->addr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 memcpy(&tip, arp_ptr, 4);
373
374 /* Should we ignore arp? */
375 if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
376 return;
377
378 size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
379 send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
380 LL_RESERVED_SPACE(np->dev));
381
382 if (!send_skb)
383 return;
384
385 send_skb->nh.raw = send_skb->data;
386 arp = (struct arphdr *) skb_put(send_skb, size);
387 send_skb->dev = skb->dev;
388 send_skb->protocol = htons(ETH_P_ARP);
389
390 /* Fill the device header for the ARP frame */
391
392 if (np->dev->hard_header &&
393 np->dev->hard_header(send_skb, skb->dev, ptype,
Neil Horman47bbec02006-12-08 00:05:55 -0800394 sha, np->local_mac,
David S. Millerc68b9072006-11-14 20:40:49 -0800395 send_skb->len) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 kfree_skb(send_skb);
397 return;
398 }
399
400 /*
401 * Fill out the arp protocol part.
402 *
403 * we only support ethernet device type,
404 * which (according to RFC 1390) should always equal 1 (Ethernet).
405 */
406
407 arp->ar_hrd = htons(np->dev->type);
408 arp->ar_pro = htons(ETH_P_IP);
409 arp->ar_hln = np->dev->addr_len;
410 arp->ar_pln = 4;
411 arp->ar_op = htons(type);
412
413 arp_ptr=(unsigned char *)(arp + 1);
414 memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
415 arp_ptr += np->dev->addr_len;
416 memcpy(arp_ptr, &tip, 4);
417 arp_ptr += 4;
Neil Horman47bbec02006-12-08 00:05:55 -0800418 memcpy(arp_ptr, sha, np->dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 arp_ptr += np->dev->addr_len;
420 memcpy(arp_ptr, &sip, 4);
421
422 netpoll_send_skb(np, send_skb);
423}
424
425int __netpoll_rx(struct sk_buff *skb)
426{
427 int proto, len, ulen;
428 struct iphdr *iph;
429 struct udphdr *uh;
Neil Horman068c6e92006-06-26 00:04:27 -0700430 struct netpoll_info *npi = skb->dev->npinfo;
431 struct netpoll *np = npi->rx_np;
432
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700433 if (!np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 goto out;
435 if (skb->dev->type != ARPHRD_ETHER)
436 goto out;
437
438 /* check if netpoll clients need ARP */
439 if (skb->protocol == __constant_htons(ETH_P_ARP) &&
440 atomic_read(&trapped)) {
Neil Horman068c6e92006-06-26 00:04:27 -0700441 skb_queue_tail(&npi->arp_tx, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 1;
443 }
444
445 proto = ntohs(eth_hdr(skb)->h_proto);
446 if (proto != ETH_P_IP)
447 goto out;
448 if (skb->pkt_type == PACKET_OTHERHOST)
449 goto out;
450 if (skb_shared(skb))
451 goto out;
452
453 iph = (struct iphdr *)skb->data;
454 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
455 goto out;
456 if (iph->ihl < 5 || iph->version != 4)
457 goto out;
458 if (!pskb_may_pull(skb, iph->ihl*4))
459 goto out;
460 if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
461 goto out;
462
463 len = ntohs(iph->tot_len);
464 if (skb->len < len || len < iph->ihl*4)
465 goto out;
466
467 if (iph->protocol != IPPROTO_UDP)
468 goto out;
469
470 len -= iph->ihl*4;
471 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
472 ulen = ntohs(uh->len);
473
474 if (ulen != len)
475 goto out;
Herbert Xufb286bb2005-11-10 13:01:24 -0800476 if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto out;
478 if (np->local_ip && np->local_ip != ntohl(iph->daddr))
479 goto out;
480 if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
481 goto out;
482 if (np->local_port && np->local_port != ntohs(uh->dest))
483 goto out;
484
485 np->rx_hook(np, ntohs(uh->source),
486 (char *)(uh+1),
487 ulen - sizeof(struct udphdr));
488
489 kfree_skb(skb);
490 return 1;
491
492out:
493 if (atomic_read(&trapped)) {
494 kfree_skb(skb);
495 return 1;
496 }
497
498 return 0;
499}
500
501int netpoll_parse_options(struct netpoll *np, char *opt)
502{
503 char *cur=opt, *delim;
504
David S. Millerc68b9072006-11-14 20:40:49 -0800505 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if ((delim = strchr(cur, '@')) == NULL)
507 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800508 *delim = 0;
509 np->local_port = simple_strtol(cur, NULL, 10);
510 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 cur++;
513 printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
514
David S. Millerc68b9072006-11-14 20:40:49 -0800515 if (*cur != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if ((delim = strchr(cur, '/')) == NULL)
517 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800518 *delim = 0;
519 np->local_ip = ntohl(in_aton(cur));
520 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
523 np->name, HIPQUAD(np->local_ip));
524 }
525 cur++;
526
David S. Millerc68b9072006-11-14 20:40:49 -0800527 if (*cur != ',') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* parse out dev name */
529 if ((delim = strchr(cur, ',')) == NULL)
530 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800531 *delim = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
David S. Millerc68b9072006-11-14 20:40:49 -0800533 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 cur++;
536
537 printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
538
David S. Millerc68b9072006-11-14 20:40:49 -0800539 if (*cur != '@') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* dst port */
541 if ((delim = strchr(cur, '@')) == NULL)
542 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800543 *delim = 0;
544 np->remote_port = simple_strtol(cur, NULL, 10);
545 cur = delim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 cur++;
548 printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
549
550 /* dst ip */
551 if ((delim = strchr(cur, '/')) == NULL)
552 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800553 *delim = 0;
554 np->remote_ip = ntohl(in_aton(cur));
555 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
David S. Millerc68b9072006-11-14 20:40:49 -0800558 np->name, HIPQUAD(np->remote_ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
David S. Millerc68b9072006-11-14 20:40:49 -0800560 if (*cur != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* MAC address */
562 if ((delim = strchr(cur, ':')) == NULL)
563 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800564 *delim = 0;
565 np->remote_mac[0] = simple_strtol(cur, NULL, 16);
566 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if ((delim = strchr(cur, ':')) == NULL)
568 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800569 *delim = 0;
570 np->remote_mac[1] = simple_strtol(cur, NULL, 16);
571 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if ((delim = strchr(cur, ':')) == NULL)
573 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800574 *delim = 0;
575 np->remote_mac[2] = simple_strtol(cur, NULL, 16);
576 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if ((delim = strchr(cur, ':')) == NULL)
578 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800579 *delim = 0;
580 np->remote_mac[3] = simple_strtol(cur, NULL, 16);
581 cur = delim + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if ((delim = strchr(cur, ':')) == NULL)
583 goto parse_failed;
David S. Millerc68b9072006-11-14 20:40:49 -0800584 *delim = 0;
585 np->remote_mac[4] = simple_strtol(cur, NULL, 16);
586 cur = delim + 1;
587 np->remote_mac[5] = simple_strtol(cur, NULL, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
590 printk(KERN_INFO "%s: remote ethernet address "
591 "%02x:%02x:%02x:%02x:%02x:%02x\n",
592 np->name,
593 np->remote_mac[0],
594 np->remote_mac[1],
595 np->remote_mac[2],
596 np->remote_mac[3],
597 np->remote_mac[4],
598 np->remote_mac[5]);
599
600 return 0;
601
602 parse_failed:
603 printk(KERN_INFO "%s: couldn't parse config at %s!\n",
604 np->name, cur);
605 return -1;
606}
607
608int netpoll_setup(struct netpoll *np)
609{
610 struct net_device *ndev = NULL;
611 struct in_device *in_dev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700612 struct netpoll_info *npinfo;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700613 unsigned long flags;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700614 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (np->dev_name)
617 ndev = dev_get_by_name(np->dev_name);
618 if (!ndev) {
619 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
620 np->name, np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700621 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 np->dev = ndev;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700625 if (!ndev->npinfo) {
626 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700627 if (!npinfo) {
628 err = -ENOMEM;
Jeff Moyer115c1d62005-06-22 22:05:31 -0700629 goto release;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700630 }
Jeff Moyer115c1d62005-06-22 22:05:31 -0700631
Jeff Moyer11513122005-08-11 19:23:04 -0700632 npinfo->rx_flags = 0;
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700633 npinfo->rx_np = NULL;
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700634 spin_lock_init(&npinfo->poll_lock);
Jeff Moyer115c1d62005-06-22 22:05:31 -0700635 npinfo->poll_owner = -1;
Stephen Hemminger2bdfe0b2006-10-26 15:46:54 -0700636
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700637 spin_lock_init(&npinfo->rx_lock);
Neil Horman068c6e92006-06-26 00:04:27 -0700638 skb_queue_head_init(&npinfo->arp_tx);
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -0700639 skb_queue_head_init(&npinfo->txq);
David Howells4c1ac1b2006-12-05 14:37:56 +0000640 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -0700641
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700642 atomic_set(&npinfo->refcnt, 1);
643 } else {
Jeff Moyer115c1d62005-06-22 22:05:31 -0700644 npinfo = ndev->npinfo;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700645 atomic_inc(&npinfo->refcnt);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (!ndev->poll_controller) {
649 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
650 np->name, np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700651 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 goto release;
653 }
654
655 if (!netif_running(ndev)) {
656 unsigned long atmost, atleast;
657
658 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
659 np->name, np->dev_name);
660
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800661 rtnl_lock();
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700662 err = dev_open(ndev);
663 rtnl_unlock();
664
665 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 printk(KERN_ERR "%s: failed to open %s\n",
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700667 np->name, ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto release;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 atleast = jiffies + HZ/10;
672 atmost = jiffies + 4*HZ;
673 while (!netif_carrier_ok(ndev)) {
674 if (time_after(jiffies, atmost)) {
675 printk(KERN_NOTICE
676 "%s: timeout waiting for carrier\n",
677 np->name);
678 break;
679 }
680 cond_resched();
681 }
682
683 /* If carrier appears to come up instantly, we don't
684 * trust it and pause so that we don't pump all our
685 * queued console messages into the bitbucket.
686 */
687
688 if (time_before(jiffies, atleast)) {
689 printk(KERN_NOTICE "%s: carrier detect appears"
690 " untrustworthy, waiting 4 seconds\n",
691 np->name);
692 msleep(4000);
693 }
694 }
695
Kris Katterjohn38602882006-01-17 15:15:38 -0800696 if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 memcpy(np->local_mac, ndev->dev_addr, 6);
698
699 if (!np->local_ip) {
700 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700701 in_dev = __in_dev_get_rcu(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (!in_dev || !in_dev->ifa_list) {
704 rcu_read_unlock();
705 printk(KERN_ERR "%s: no IP address for %s, aborting\n",
706 np->name, np->dev_name);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700707 err = -EDESTADDRREQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 goto release;
709 }
710
711 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
712 rcu_read_unlock();
713 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
714 np->name, HIPQUAD(np->local_ip));
715 }
716
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700717 if (np->rx_hook) {
718 spin_lock_irqsave(&npinfo->rx_lock, flags);
719 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
720 npinfo->rx_np = np;
721 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
722 }
Ingo Molnar26520762005-08-11 19:26:42 -0700723
724 /* fill up the skb queue */
725 refill_skbs();
726
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700727 /* last thing to do is link it to the net device structure */
Jeff Moyer115c1d62005-06-22 22:05:31 -0700728 ndev->npinfo = npinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Matt Mackall53fb95d2005-08-11 19:27:43 -0700730 /* avoid racing with NAPI reading npinfo */
731 synchronize_rcu();
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734
735 release:
Jeff Moyer115c1d62005-06-22 22:05:31 -0700736 if (!ndev->npinfo)
737 kfree(npinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 np->dev = NULL;
739 dev_put(ndev);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
David S. Millerc68b9072006-11-14 20:40:49 -0800743static int __init netpoll_init(void)
744{
Stephen Hemmingera1bcfac2006-11-14 10:43:58 -0800745 skb_queue_head_init(&skb_pool);
746 return 0;
747}
748core_initcall(netpoll_init);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750void netpoll_cleanup(struct netpoll *np)
751{
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700752 struct netpoll_info *npinfo;
753 unsigned long flags;
754
Jeff Moyer115c1d62005-06-22 22:05:31 -0700755 if (np->dev) {
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700756 npinfo = np->dev->npinfo;
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700757 if (npinfo) {
758 if (npinfo->rx_np == np) {
759 spin_lock_irqsave(&npinfo->rx_lock, flags);
760 npinfo->rx_np = NULL;
761 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
762 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
763 }
764
765 np->dev->npinfo = NULL;
766 if (atomic_dec_and_test(&npinfo->refcnt)) {
767 skb_queue_purge(&npinfo->arp_tx);
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -0700768 skb_queue_purge(&npinfo->txq);
Stephen Hemminger6c43ff12006-10-26 15:46:53 -0700769 cancel_rearming_delayed_work(&npinfo->tx_work);
Stephen Hemmingerb6cd27e2006-10-26 15:46:51 -0700770 flush_scheduled_work();
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700771
772 kfree(npinfo);
773 }
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700774 }
Stephen Hemminger93ec2c72006-10-26 15:46:50 -0700775
Jeff Moyer115c1d62005-06-22 22:05:31 -0700776 dev_put(np->dev);
777 }
Jeff Moyerfbeec2e2005-06-22 22:05:59 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 np->dev = NULL;
780}
781
782int netpoll_trap(void)
783{
784 return atomic_read(&trapped);
785}
786
787void netpoll_set_trap(int trap)
788{
789 if (trap)
790 atomic_inc(&trapped);
791 else
792 atomic_dec(&trapped);
793}
794
795EXPORT_SYMBOL(netpoll_set_trap);
796EXPORT_SYMBOL(netpoll_trap);
797EXPORT_SYMBOL(netpoll_parse_options);
798EXPORT_SYMBOL(netpoll_setup);
799EXPORT_SYMBOL(netpoll_cleanup);
800EXPORT_SYMBOL(netpoll_send_udp);
801EXPORT_SYMBOL(netpoll_poll);