Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 16 | #include <linux/inetdevice.h> |
| 17 | #include <linux/inet.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/netpoll.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/rcupdate.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <net/tcp.h> |
| 25 | #include <net/udp.h> |
| 26 | #include <asm/unaligned.h> |
| 27 | |
| 28 | /* |
| 29 | * We maintain a small pool of fully-sized skbs, to make sure the |
| 30 | * message gets out even in extreme OOM situations. |
| 31 | */ |
| 32 | |
| 33 | #define MAX_UDP_CHUNK 1460 |
| 34 | #define MAX_SKBS 32 |
| 35 | #define MAX_QUEUE_DEPTH (MAX_SKBS / 2) |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 36 | #define MAX_RETRIES 20000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | static DEFINE_SPINLOCK(skb_list_lock); |
| 39 | static int nr_skbs; |
| 40 | static struct sk_buff *skbs; |
| 41 | |
| 42 | static DEFINE_SPINLOCK(queue_lock); |
| 43 | static int queue_depth; |
| 44 | static struct sk_buff *queue_head, *queue_tail; |
| 45 | |
| 46 | static atomic_t trapped; |
| 47 | |
| 48 | #define NETPOLL_RX_ENABLED 1 |
| 49 | #define NETPOLL_RX_DROP 2 |
| 50 | |
| 51 | #define MAX_SKB_SIZE \ |
| 52 | (MAX_UDP_CHUNK + sizeof(struct udphdr) + \ |
| 53 | sizeof(struct iphdr) + sizeof(struct ethhdr)) |
| 54 | |
| 55 | static void zap_completion_queue(void); |
| 56 | |
| 57 | static void queue_process(void *p) |
| 58 | { |
| 59 | unsigned long flags; |
| 60 | struct sk_buff *skb; |
| 61 | |
| 62 | while (queue_head) { |
| 63 | spin_lock_irqsave(&queue_lock, flags); |
| 64 | |
| 65 | skb = queue_head; |
| 66 | queue_head = skb->next; |
| 67 | if (skb == queue_tail) |
| 68 | queue_head = NULL; |
| 69 | |
| 70 | queue_depth--; |
| 71 | |
| 72 | spin_unlock_irqrestore(&queue_lock, flags); |
| 73 | |
| 74 | dev_queue_xmit(skb); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static DECLARE_WORK(send_queue, queue_process, NULL); |
| 79 | |
| 80 | void netpoll_queue(struct sk_buff *skb) |
| 81 | { |
| 82 | unsigned long flags; |
| 83 | |
| 84 | if (queue_depth == MAX_QUEUE_DEPTH) { |
| 85 | __kfree_skb(skb); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | spin_lock_irqsave(&queue_lock, flags); |
| 90 | if (!queue_head) |
| 91 | queue_head = skb; |
| 92 | else |
| 93 | queue_tail->next = skb; |
| 94 | queue_tail = skb; |
| 95 | queue_depth++; |
| 96 | spin_unlock_irqrestore(&queue_lock, flags); |
| 97 | |
| 98 | schedule_work(&send_queue); |
| 99 | } |
| 100 | |
| 101 | static int checksum_udp(struct sk_buff *skb, struct udphdr *uh, |
| 102 | unsigned short ulen, u32 saddr, u32 daddr) |
| 103 | { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 104 | unsigned int psum; |
| 105 | |
| 106 | if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 109 | psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 111 | if (skb->ip_summed == CHECKSUM_HW && |
| 112 | !(u16)csum_fold(csum_add(psum, skb->csum))) |
| 113 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 115 | skb->csum = psum; |
| 116 | |
| 117 | return __skb_checksum_complete(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Check whether delayed processing was scheduled for our NIC. If so, |
| 122 | * we attempt to grab the poll lock and use ->poll() to pump the card. |
| 123 | * If this fails, either we've recursed in ->poll() or it's already |
| 124 | * running on another CPU. |
| 125 | * |
| 126 | * Note: we don't mask interrupts with this lock because we're using |
| 127 | * trylock here and interrupts are already disabled in the softirq |
| 128 | * case. Further, we test the poll_owner to avoid recursion on UP |
| 129 | * systems where the lock doesn't exist. |
| 130 | * |
| 131 | * In cases where there is bi-directional communications, reading only |
| 132 | * one message at a time can lead to packets being dropped by the |
| 133 | * network adapter, forcing superfluous retries and possibly timeouts. |
| 134 | * Thus, we set our budget to greater than 1. |
| 135 | */ |
| 136 | static void poll_napi(struct netpoll *np) |
| 137 | { |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 138 | struct netpoll_info *npinfo = np->dev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | int budget = 16; |
| 140 | |
| 141 | if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) && |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 142 | npinfo->poll_owner != smp_processor_id() && |
| 143 | spin_trylock(&npinfo->poll_lock)) { |
| 144 | npinfo->rx_flags |= NETPOLL_RX_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | atomic_inc(&trapped); |
| 146 | |
| 147 | np->dev->poll(np->dev, &budget); |
| 148 | |
| 149 | atomic_dec(&trapped); |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 150 | npinfo->rx_flags &= ~NETPOLL_RX_DROP; |
| 151 | spin_unlock(&npinfo->poll_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | void netpoll_poll(struct netpoll *np) |
| 156 | { |
| 157 | if(!np->dev || !netif_running(np->dev) || !np->dev->poll_controller) |
| 158 | return; |
| 159 | |
| 160 | /* Process pending work on NIC */ |
| 161 | np->dev->poll_controller(np->dev); |
| 162 | if (np->dev->poll) |
| 163 | poll_napi(np); |
| 164 | |
| 165 | zap_completion_queue(); |
| 166 | } |
| 167 | |
| 168 | static void refill_skbs(void) |
| 169 | { |
| 170 | struct sk_buff *skb; |
| 171 | unsigned long flags; |
| 172 | |
| 173 | spin_lock_irqsave(&skb_list_lock, flags); |
| 174 | while (nr_skbs < MAX_SKBS) { |
| 175 | skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); |
| 176 | if (!skb) |
| 177 | break; |
| 178 | |
| 179 | skb->next = skbs; |
| 180 | skbs = skb; |
| 181 | nr_skbs++; |
| 182 | } |
| 183 | spin_unlock_irqrestore(&skb_list_lock, flags); |
| 184 | } |
| 185 | |
| 186 | static void zap_completion_queue(void) |
| 187 | { |
| 188 | unsigned long flags; |
| 189 | struct softnet_data *sd = &get_cpu_var(softnet_data); |
| 190 | |
| 191 | if (sd->completion_queue) { |
| 192 | struct sk_buff *clist; |
| 193 | |
| 194 | local_irq_save(flags); |
| 195 | clist = sd->completion_queue; |
| 196 | sd->completion_queue = NULL; |
| 197 | local_irq_restore(flags); |
| 198 | |
| 199 | while (clist != NULL) { |
| 200 | struct sk_buff *skb = clist; |
| 201 | clist = clist->next; |
| 202 | if(skb->destructor) |
| 203 | dev_kfree_skb_any(skb); /* put this one back */ |
| 204 | else |
| 205 | __kfree_skb(skb); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | put_cpu_var(softnet_data); |
| 210 | } |
| 211 | |
| 212 | static struct sk_buff * find_skb(struct netpoll *np, int len, int reserve) |
| 213 | { |
| 214 | int once = 1, count = 0; |
| 215 | unsigned long flags; |
| 216 | struct sk_buff *skb = NULL; |
| 217 | |
| 218 | zap_completion_queue(); |
| 219 | repeat: |
| 220 | if (nr_skbs < MAX_SKBS) |
| 221 | refill_skbs(); |
| 222 | |
| 223 | skb = alloc_skb(len, GFP_ATOMIC); |
| 224 | |
| 225 | if (!skb) { |
| 226 | spin_lock_irqsave(&skb_list_lock, flags); |
| 227 | skb = skbs; |
| 228 | if (skb) { |
| 229 | skbs = skb->next; |
| 230 | skb->next = NULL; |
| 231 | nr_skbs--; |
| 232 | } |
| 233 | spin_unlock_irqrestore(&skb_list_lock, flags); |
| 234 | } |
| 235 | |
| 236 | if(!skb) { |
| 237 | count++; |
| 238 | if (once && (count == 1000000)) { |
| 239 | printk("out of netpoll skbs!\n"); |
| 240 | once = 0; |
| 241 | } |
| 242 | netpoll_poll(np); |
| 243 | goto repeat; |
| 244 | } |
| 245 | |
| 246 | atomic_set(&skb->users, 1); |
| 247 | skb_reserve(skb, reserve); |
| 248 | return skb; |
| 249 | } |
| 250 | |
| 251 | static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) |
| 252 | { |
| 253 | int status; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 254 | struct netpoll_info *npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 256 | if (!np || !np->dev || !netif_running(np->dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | __kfree_skb(skb); |
| 258 | return; |
| 259 | } |
| 260 | |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 261 | npinfo = np->dev->npinfo; |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 262 | |
| 263 | /* avoid recursion */ |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 264 | if (npinfo->poll_owner == smp_processor_id() || |
| 265 | np->dev->xmit_lock_owner == smp_processor_id()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | if (np->drop) |
| 267 | np->drop(skb); |
| 268 | else |
| 269 | __kfree_skb(skb); |
| 270 | return; |
| 271 | } |
| 272 | |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 273 | do { |
| 274 | npinfo->tries--; |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 275 | spin_lock(&np->dev->xmit_lock); |
| 276 | np->dev->xmit_lock_owner = smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 278 | /* |
| 279 | * network drivers do not expect to be called if the queue is |
| 280 | * stopped. |
| 281 | */ |
| 282 | if (netif_queue_stopped(np->dev)) { |
| 283 | np->dev->xmit_lock_owner = -1; |
| 284 | spin_unlock(&np->dev->xmit_lock); |
| 285 | netpoll_poll(np); |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 286 | udelay(50); |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | |
| 290 | status = np->dev->hard_start_xmit(skb, np->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | np->dev->xmit_lock_owner = -1; |
| 292 | spin_unlock(&np->dev->xmit_lock); |
| 293 | |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 294 | /* success */ |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 295 | if(!status) { |
| 296 | npinfo->tries = MAX_RETRIES; /* reset */ |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 297 | return; |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Matt Mackall | f0d3459 | 2005-08-11 19:25:11 -0700 | [diff] [blame] | 300 | /* transmit busy */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | netpoll_poll(np); |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 302 | udelay(50); |
| 303 | } while (npinfo->tries > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void netpoll_send_udp(struct netpoll *np, const char *msg, int len) |
| 307 | { |
| 308 | int total_len, eth_len, ip_len, udp_len; |
| 309 | struct sk_buff *skb; |
| 310 | struct udphdr *udph; |
| 311 | struct iphdr *iph; |
| 312 | struct ethhdr *eth; |
| 313 | |
| 314 | udp_len = len + sizeof(*udph); |
| 315 | ip_len = eth_len = udp_len + sizeof(*iph); |
| 316 | total_len = eth_len + ETH_HLEN + NET_IP_ALIGN; |
| 317 | |
| 318 | skb = find_skb(np, total_len, total_len - len); |
| 319 | if (!skb) |
| 320 | return; |
| 321 | |
| 322 | memcpy(skb->data, msg, len); |
| 323 | skb->len += len; |
| 324 | |
| 325 | udph = (struct udphdr *) skb_push(skb, sizeof(*udph)); |
| 326 | udph->source = htons(np->local_port); |
| 327 | udph->dest = htons(np->remote_port); |
| 328 | udph->len = htons(udp_len); |
| 329 | udph->check = 0; |
| 330 | |
| 331 | iph = (struct iphdr *)skb_push(skb, sizeof(*iph)); |
| 332 | |
| 333 | /* iph->version = 4; iph->ihl = 5; */ |
| 334 | put_unaligned(0x45, (unsigned char *)iph); |
| 335 | iph->tos = 0; |
| 336 | put_unaligned(htons(ip_len), &(iph->tot_len)); |
| 337 | iph->id = 0; |
| 338 | iph->frag_off = 0; |
| 339 | iph->ttl = 64; |
| 340 | iph->protocol = IPPROTO_UDP; |
| 341 | iph->check = 0; |
| 342 | put_unaligned(htonl(np->local_ip), &(iph->saddr)); |
| 343 | put_unaligned(htonl(np->remote_ip), &(iph->daddr)); |
| 344 | iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); |
| 345 | |
| 346 | eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); |
| 347 | |
| 348 | eth->h_proto = htons(ETH_P_IP); |
| 349 | memcpy(eth->h_source, np->local_mac, 6); |
| 350 | memcpy(eth->h_dest, np->remote_mac, 6); |
| 351 | |
| 352 | skb->dev = np->dev; |
| 353 | |
| 354 | netpoll_send_skb(np, skb); |
| 355 | } |
| 356 | |
| 357 | static void arp_reply(struct sk_buff *skb) |
| 358 | { |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 359 | struct netpoll_info *npinfo = skb->dev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | struct arphdr *arp; |
| 361 | unsigned char *arp_ptr; |
| 362 | int size, type = ARPOP_REPLY, ptype = ETH_P_ARP; |
| 363 | u32 sip, tip; |
| 364 | struct sk_buff *send_skb; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 365 | struct netpoll *np = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 367 | if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev) |
| 368 | np = npinfo->rx_np; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 369 | if (!np) |
| 370 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | /* No arp on this interface */ |
| 373 | if (skb->dev->flags & IFF_NOARP) |
| 374 | return; |
| 375 | |
| 376 | if (!pskb_may_pull(skb, (sizeof(struct arphdr) + |
| 377 | (2 * skb->dev->addr_len) + |
| 378 | (2 * sizeof(u32))))) |
| 379 | return; |
| 380 | |
| 381 | skb->h.raw = skb->nh.raw = skb->data; |
| 382 | arp = skb->nh.arph; |
| 383 | |
| 384 | if ((arp->ar_hrd != htons(ARPHRD_ETHER) && |
| 385 | arp->ar_hrd != htons(ARPHRD_IEEE802)) || |
| 386 | arp->ar_pro != htons(ETH_P_IP) || |
| 387 | arp->ar_op != htons(ARPOP_REQUEST)) |
| 388 | return; |
| 389 | |
| 390 | arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len; |
| 391 | memcpy(&sip, arp_ptr, 4); |
| 392 | arp_ptr += 4 + skb->dev->addr_len; |
| 393 | memcpy(&tip, arp_ptr, 4); |
| 394 | |
| 395 | /* Should we ignore arp? */ |
| 396 | if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip)) |
| 397 | return; |
| 398 | |
| 399 | size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4); |
| 400 | send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev), |
| 401 | LL_RESERVED_SPACE(np->dev)); |
| 402 | |
| 403 | if (!send_skb) |
| 404 | return; |
| 405 | |
| 406 | send_skb->nh.raw = send_skb->data; |
| 407 | arp = (struct arphdr *) skb_put(send_skb, size); |
| 408 | send_skb->dev = skb->dev; |
| 409 | send_skb->protocol = htons(ETH_P_ARP); |
| 410 | |
| 411 | /* Fill the device header for the ARP frame */ |
| 412 | |
| 413 | if (np->dev->hard_header && |
| 414 | np->dev->hard_header(send_skb, skb->dev, ptype, |
| 415 | np->remote_mac, np->local_mac, |
| 416 | send_skb->len) < 0) { |
| 417 | kfree_skb(send_skb); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Fill out the arp protocol part. |
| 423 | * |
| 424 | * we only support ethernet device type, |
| 425 | * which (according to RFC 1390) should always equal 1 (Ethernet). |
| 426 | */ |
| 427 | |
| 428 | arp->ar_hrd = htons(np->dev->type); |
| 429 | arp->ar_pro = htons(ETH_P_IP); |
| 430 | arp->ar_hln = np->dev->addr_len; |
| 431 | arp->ar_pln = 4; |
| 432 | arp->ar_op = htons(type); |
| 433 | |
| 434 | arp_ptr=(unsigned char *)(arp + 1); |
| 435 | memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len); |
| 436 | arp_ptr += np->dev->addr_len; |
| 437 | memcpy(arp_ptr, &tip, 4); |
| 438 | arp_ptr += 4; |
| 439 | memcpy(arp_ptr, np->remote_mac, np->dev->addr_len); |
| 440 | arp_ptr += np->dev->addr_len; |
| 441 | memcpy(arp_ptr, &sip, 4); |
| 442 | |
| 443 | netpoll_send_skb(np, send_skb); |
| 444 | } |
| 445 | |
| 446 | int __netpoll_rx(struct sk_buff *skb) |
| 447 | { |
| 448 | int proto, len, ulen; |
| 449 | struct iphdr *iph; |
| 450 | struct udphdr *uh; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 451 | struct netpoll *np = skb->dev->npinfo->rx_np; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 453 | if (!np) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | goto out; |
| 455 | if (skb->dev->type != ARPHRD_ETHER) |
| 456 | goto out; |
| 457 | |
| 458 | /* check if netpoll clients need ARP */ |
| 459 | if (skb->protocol == __constant_htons(ETH_P_ARP) && |
| 460 | atomic_read(&trapped)) { |
| 461 | arp_reply(skb); |
| 462 | return 1; |
| 463 | } |
| 464 | |
| 465 | proto = ntohs(eth_hdr(skb)->h_proto); |
| 466 | if (proto != ETH_P_IP) |
| 467 | goto out; |
| 468 | if (skb->pkt_type == PACKET_OTHERHOST) |
| 469 | goto out; |
| 470 | if (skb_shared(skb)) |
| 471 | goto out; |
| 472 | |
| 473 | iph = (struct iphdr *)skb->data; |
| 474 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 475 | goto out; |
| 476 | if (iph->ihl < 5 || iph->version != 4) |
| 477 | goto out; |
| 478 | if (!pskb_may_pull(skb, iph->ihl*4)) |
| 479 | goto out; |
| 480 | if (ip_fast_csum((u8 *)iph, iph->ihl) != 0) |
| 481 | goto out; |
| 482 | |
| 483 | len = ntohs(iph->tot_len); |
| 484 | if (skb->len < len || len < iph->ihl*4) |
| 485 | goto out; |
| 486 | |
| 487 | if (iph->protocol != IPPROTO_UDP) |
| 488 | goto out; |
| 489 | |
| 490 | len -= iph->ihl*4; |
| 491 | uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); |
| 492 | ulen = ntohs(uh->len); |
| 493 | |
| 494 | if (ulen != len) |
| 495 | goto out; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 496 | if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | goto out; |
| 498 | if (np->local_ip && np->local_ip != ntohl(iph->daddr)) |
| 499 | goto out; |
| 500 | if (np->remote_ip && np->remote_ip != ntohl(iph->saddr)) |
| 501 | goto out; |
| 502 | if (np->local_port && np->local_port != ntohs(uh->dest)) |
| 503 | goto out; |
| 504 | |
| 505 | np->rx_hook(np, ntohs(uh->source), |
| 506 | (char *)(uh+1), |
| 507 | ulen - sizeof(struct udphdr)); |
| 508 | |
| 509 | kfree_skb(skb); |
| 510 | return 1; |
| 511 | |
| 512 | out: |
| 513 | if (atomic_read(&trapped)) { |
| 514 | kfree_skb(skb); |
| 515 | return 1; |
| 516 | } |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | int netpoll_parse_options(struct netpoll *np, char *opt) |
| 522 | { |
| 523 | char *cur=opt, *delim; |
| 524 | |
| 525 | if(*cur != '@') { |
| 526 | if ((delim = strchr(cur, '@')) == NULL) |
| 527 | goto parse_failed; |
| 528 | *delim=0; |
| 529 | np->local_port=simple_strtol(cur, NULL, 10); |
| 530 | cur=delim; |
| 531 | } |
| 532 | cur++; |
| 533 | printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port); |
| 534 | |
| 535 | if(*cur != '/') { |
| 536 | if ((delim = strchr(cur, '/')) == NULL) |
| 537 | goto parse_failed; |
| 538 | *delim=0; |
| 539 | np->local_ip=ntohl(in_aton(cur)); |
| 540 | cur=delim; |
| 541 | |
| 542 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", |
| 543 | np->name, HIPQUAD(np->local_ip)); |
| 544 | } |
| 545 | cur++; |
| 546 | |
| 547 | if ( *cur != ',') { |
| 548 | /* parse out dev name */ |
| 549 | if ((delim = strchr(cur, ',')) == NULL) |
| 550 | goto parse_failed; |
| 551 | *delim=0; |
| 552 | strlcpy(np->dev_name, cur, sizeof(np->dev_name)); |
| 553 | cur=delim; |
| 554 | } |
| 555 | cur++; |
| 556 | |
| 557 | printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name); |
| 558 | |
| 559 | if ( *cur != '@' ) { |
| 560 | /* dst port */ |
| 561 | if ((delim = strchr(cur, '@')) == NULL) |
| 562 | goto parse_failed; |
| 563 | *delim=0; |
| 564 | np->remote_port=simple_strtol(cur, NULL, 10); |
| 565 | cur=delim; |
| 566 | } |
| 567 | cur++; |
| 568 | printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port); |
| 569 | |
| 570 | /* dst ip */ |
| 571 | if ((delim = strchr(cur, '/')) == NULL) |
| 572 | goto parse_failed; |
| 573 | *delim=0; |
| 574 | np->remote_ip=ntohl(in_aton(cur)); |
| 575 | cur=delim+1; |
| 576 | |
| 577 | printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n", |
| 578 | np->name, HIPQUAD(np->remote_ip)); |
| 579 | |
| 580 | if( *cur != 0 ) |
| 581 | { |
| 582 | /* MAC address */ |
| 583 | if ((delim = strchr(cur, ':')) == NULL) |
| 584 | goto parse_failed; |
| 585 | *delim=0; |
| 586 | np->remote_mac[0]=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[1]=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[2]=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[3]=simple_strtol(cur, NULL, 16); |
| 602 | cur=delim+1; |
| 603 | if ((delim = strchr(cur, ':')) == NULL) |
| 604 | goto parse_failed; |
| 605 | *delim=0; |
| 606 | np->remote_mac[4]=simple_strtol(cur, NULL, 16); |
| 607 | cur=delim+1; |
| 608 | np->remote_mac[5]=simple_strtol(cur, NULL, 16); |
| 609 | } |
| 610 | |
| 611 | printk(KERN_INFO "%s: remote ethernet address " |
| 612 | "%02x:%02x:%02x:%02x:%02x:%02x\n", |
| 613 | np->name, |
| 614 | np->remote_mac[0], |
| 615 | np->remote_mac[1], |
| 616 | np->remote_mac[2], |
| 617 | np->remote_mac[3], |
| 618 | np->remote_mac[4], |
| 619 | np->remote_mac[5]); |
| 620 | |
| 621 | return 0; |
| 622 | |
| 623 | parse_failed: |
| 624 | printk(KERN_INFO "%s: couldn't parse config at %s!\n", |
| 625 | np->name, cur); |
| 626 | return -1; |
| 627 | } |
| 628 | |
| 629 | int netpoll_setup(struct netpoll *np) |
| 630 | { |
| 631 | struct net_device *ndev = NULL; |
| 632 | struct in_device *in_dev; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 633 | struct netpoll_info *npinfo; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 634 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | if (np->dev_name) |
| 637 | ndev = dev_get_by_name(np->dev_name); |
| 638 | if (!ndev) { |
| 639 | printk(KERN_ERR "%s: %s doesn't exist, aborting.\n", |
| 640 | np->name, np->dev_name); |
| 641 | return -1; |
| 642 | } |
| 643 | |
| 644 | np->dev = ndev; |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 645 | if (!ndev->npinfo) { |
| 646 | npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); |
| 647 | if (!npinfo) |
| 648 | goto release; |
| 649 | |
Jeff Moyer | 1151312 | 2005-08-11 19:23:04 -0700 | [diff] [blame] | 650 | npinfo->rx_flags = 0; |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 651 | npinfo->rx_np = NULL; |
Ingo Molnar | a9f6a0d | 2005-09-09 13:10:41 -0700 | [diff] [blame] | 652 | spin_lock_init(&npinfo->poll_lock); |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 653 | npinfo->poll_owner = -1; |
Matt Mackall | 0db1d6f | 2005-08-11 19:25:54 -0700 | [diff] [blame] | 654 | npinfo->tries = MAX_RETRIES; |
Ingo Molnar | a9f6a0d | 2005-09-09 13:10:41 -0700 | [diff] [blame] | 655 | spin_lock_init(&npinfo->rx_lock); |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 656 | } else |
| 657 | npinfo = ndev->npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | if (!ndev->poll_controller) { |
| 660 | printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n", |
| 661 | np->name, np->dev_name); |
| 662 | goto release; |
| 663 | } |
| 664 | |
| 665 | if (!netif_running(ndev)) { |
| 666 | unsigned long atmost, atleast; |
| 667 | |
| 668 | printk(KERN_INFO "%s: device %s not up yet, forcing it\n", |
| 669 | np->name, np->dev_name); |
| 670 | |
| 671 | rtnl_shlock(); |
| 672 | if (dev_change_flags(ndev, ndev->flags | IFF_UP) < 0) { |
| 673 | printk(KERN_ERR "%s: failed to open %s\n", |
| 674 | np->name, np->dev_name); |
| 675 | rtnl_shunlock(); |
| 676 | goto release; |
| 677 | } |
| 678 | rtnl_shunlock(); |
| 679 | |
| 680 | atleast = jiffies + HZ/10; |
| 681 | atmost = jiffies + 4*HZ; |
| 682 | while (!netif_carrier_ok(ndev)) { |
| 683 | if (time_after(jiffies, atmost)) { |
| 684 | printk(KERN_NOTICE |
| 685 | "%s: timeout waiting for carrier\n", |
| 686 | np->name); |
| 687 | break; |
| 688 | } |
| 689 | cond_resched(); |
| 690 | } |
| 691 | |
| 692 | /* If carrier appears to come up instantly, we don't |
| 693 | * trust it and pause so that we don't pump all our |
| 694 | * queued console messages into the bitbucket. |
| 695 | */ |
| 696 | |
| 697 | if (time_before(jiffies, atleast)) { |
| 698 | printk(KERN_NOTICE "%s: carrier detect appears" |
| 699 | " untrustworthy, waiting 4 seconds\n", |
| 700 | np->name); |
| 701 | msleep(4000); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | if (!memcmp(np->local_mac, "\0\0\0\0\0\0", 6) && ndev->dev_addr) |
| 706 | memcpy(np->local_mac, ndev->dev_addr, 6); |
| 707 | |
| 708 | if (!np->local_ip) { |
| 709 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 710 | in_dev = __in_dev_get_rcu(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | if (!in_dev || !in_dev->ifa_list) { |
| 713 | rcu_read_unlock(); |
| 714 | printk(KERN_ERR "%s: no IP address for %s, aborting\n", |
| 715 | np->name, np->dev_name); |
| 716 | goto release; |
| 717 | } |
| 718 | |
| 719 | np->local_ip = ntohl(in_dev->ifa_list->ifa_local); |
| 720 | rcu_read_unlock(); |
| 721 | printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n", |
| 722 | np->name, HIPQUAD(np->local_ip)); |
| 723 | } |
| 724 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 725 | if (np->rx_hook) { |
| 726 | spin_lock_irqsave(&npinfo->rx_lock, flags); |
| 727 | npinfo->rx_flags |= NETPOLL_RX_ENABLED; |
| 728 | npinfo->rx_np = np; |
| 729 | spin_unlock_irqrestore(&npinfo->rx_lock, flags); |
| 730 | } |
Ingo Molnar | 2652076 | 2005-08-11 19:26:42 -0700 | [diff] [blame] | 731 | |
| 732 | /* fill up the skb queue */ |
| 733 | refill_skbs(); |
| 734 | |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 735 | /* last thing to do is link it to the net device structure */ |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 736 | ndev->npinfo = npinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
Matt Mackall | 53fb95d | 2005-08-11 19:27:43 -0700 | [diff] [blame] | 738 | /* avoid racing with NAPI reading npinfo */ |
| 739 | synchronize_rcu(); |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | return 0; |
| 742 | |
| 743 | release: |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 744 | if (!ndev->npinfo) |
| 745 | kfree(npinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | np->dev = NULL; |
| 747 | dev_put(ndev); |
| 748 | return -1; |
| 749 | } |
| 750 | |
| 751 | void netpoll_cleanup(struct netpoll *np) |
| 752 | { |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 753 | struct netpoll_info *npinfo; |
| 754 | unsigned long flags; |
| 755 | |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 756 | if (np->dev) { |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 757 | npinfo = np->dev->npinfo; |
| 758 | if (npinfo && 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 | } |
Jeff Moyer | 115c1d6 | 2005-06-22 22:05:31 -0700 | [diff] [blame] | 764 | dev_put(np->dev); |
| 765 | } |
Jeff Moyer | fbeec2e | 2005-06-22 22:05:59 -0700 | [diff] [blame] | 766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | np->dev = NULL; |
| 768 | } |
| 769 | |
| 770 | int netpoll_trap(void) |
| 771 | { |
| 772 | return atomic_read(&trapped); |
| 773 | } |
| 774 | |
| 775 | void netpoll_set_trap(int trap) |
| 776 | { |
| 777 | if (trap) |
| 778 | atomic_inc(&trapped); |
| 779 | else |
| 780 | atomic_dec(&trapped); |
| 781 | } |
| 782 | |
| 783 | EXPORT_SYMBOL(netpoll_set_trap); |
| 784 | EXPORT_SYMBOL(netpoll_trap); |
| 785 | EXPORT_SYMBOL(netpoll_parse_options); |
| 786 | EXPORT_SYMBOL(netpoll_setup); |
| 787 | EXPORT_SYMBOL(netpoll_cleanup); |
| 788 | EXPORT_SYMBOL(netpoll_send_udp); |
| 789 | EXPORT_SYMBOL(netpoll_poll); |
| 790 | EXPORT_SYMBOL(netpoll_queue); |