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