1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Common framework for low-level network console, dump, and debugger code 4 * 5 * Sep 8 2003 Matt Mackall <mpm@selenic.com> 6 * 7 * based on the netconsole code from: 8 * 9 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com> 10 * Copyright (C) 2002 Red Hat, Inc. 11 */ 12 13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15 #include <linux/moduleparam.h> 16 #include <linux/kernel.h> 17 #include <linux/netdevice.h> 18 #include <linux/etherdevice.h> 19 #include <linux/string.h> 20 #include <linux/if_arp.h> 21 #include <linux/inetdevice.h> 22 #include <linux/inet.h> 23 #include <linux/interrupt.h> 24 #include <linux/netpoll.h> 25 #include <linux/sched.h> 26 #include <linux/delay.h> 27 #include <linux/rcupdate.h> 28 #include <linux/workqueue.h> 29 #include <linux/slab.h> 30 #include <linux/export.h> 31 #include <linux/if_vlan.h> 32 #include <net/tcp.h> 33 #include <net/udp.h> 34 #include <net/addrconf.h> 35 #include <net/ndisc.h> 36 #include <net/ip6_checksum.h> 37 #include <linux/unaligned.h> 38 #include <trace/events/napi.h> 39 #include <linux/kconfig.h> 40 41 /* 42 * We maintain a small pool of fully-sized skbs, to make sure the 43 * message gets out even in extreme OOM situations. 44 */ 45 46 #define MAX_UDP_CHUNK 1460 47 #define MAX_SKBS 32 48 #define USEC_PER_POLL 50 49 50 #define MAX_SKB_SIZE \ 51 (sizeof(struct ethhdr) + \ 52 sizeof(struct iphdr) + \ 53 sizeof(struct udphdr) + \ 54 MAX_UDP_CHUNK) 55 56 static void zap_completion_queue(void); 57 58 static unsigned int carrier_timeout = 4; 59 module_param(carrier_timeout, uint, 0644); 60 61 static netdev_tx_t netpoll_start_xmit(struct sk_buff *skb, 62 struct net_device *dev, 63 struct netdev_queue *txq) 64 { 65 netdev_tx_t status = NETDEV_TX_OK; 66 netdev_features_t features; 67 68 features = netif_skb_features(skb); 69 70 if (skb_vlan_tag_present(skb) && 71 !vlan_hw_offload_capable(features, skb->vlan_proto)) { 72 skb = __vlan_hwaccel_push_inside(skb); 73 if (unlikely(!skb)) { 74 /* This is actually a packet drop, but we 75 * don't want the code that calls this 76 * function to try and operate on a NULL skb. 77 */ 78 goto out; 79 } 80 } 81 82 status = netdev_start_xmit(skb, dev, txq, false); 83 84 out: 85 return status; 86 } 87 88 static void queue_process(struct work_struct *work) 89 { 90 struct netpoll_info *npinfo = 91 container_of(work, struct netpoll_info, tx_work.work); 92 struct sk_buff *skb; 93 unsigned long flags; 94 95 while ((skb = skb_dequeue(&npinfo->txq))) { 96 struct net_device *dev = skb->dev; 97 struct netdev_queue *txq; 98 unsigned int q_index; 99 100 if (!netif_device_present(dev) || !netif_running(dev)) { 101 kfree_skb(skb); 102 continue; 103 } 104 105 local_irq_save(flags); 106 /* check if skb->queue_mapping is still valid */ 107 q_index = skb_get_queue_mapping(skb); 108 if (unlikely(q_index >= dev->real_num_tx_queues)) { 109 q_index = q_index % dev->real_num_tx_queues; 110 skb_set_queue_mapping(skb, q_index); 111 } 112 txq = netdev_get_tx_queue(dev, q_index); 113 HARD_TX_LOCK(dev, txq, smp_processor_id()); 114 if (netif_xmit_frozen_or_stopped(txq) || 115 !dev_xmit_complete(netpoll_start_xmit(skb, dev, txq))) { 116 skb_queue_head(&npinfo->txq, skb); 117 HARD_TX_UNLOCK(dev, txq); 118 local_irq_restore(flags); 119 120 schedule_delayed_work(&npinfo->tx_work, HZ/10); 121 return; 122 } 123 HARD_TX_UNLOCK(dev, txq); 124 local_irq_restore(flags); 125 } 126 } 127 128 static int netif_local_xmit_active(struct net_device *dev) 129 { 130 int i; 131 132 for (i = 0; i < dev->num_tx_queues; i++) { 133 struct netdev_queue *txq = netdev_get_tx_queue(dev, i); 134 135 if (READ_ONCE(txq->xmit_lock_owner) == smp_processor_id()) 136 return 1; 137 } 138 139 return 0; 140 } 141 142 static void poll_one_napi(struct napi_struct *napi) 143 { 144 int work; 145 146 /* If we set this bit but see that it has already been set, 147 * that indicates that napi has been disabled and we need 148 * to abort this operation 149 */ 150 if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state)) 151 return; 152 153 /* We explicitly pass the polling call a budget of 0 to 154 * indicate that we are clearing the Tx path only. 155 */ 156 work = napi->poll(napi, 0); 157 WARN_ONCE(work, "%pS exceeded budget in poll\n", napi->poll); 158 trace_napi_poll(napi, work, 0); 159 160 clear_bit(NAPI_STATE_NPSVC, &napi->state); 161 } 162 163 static void poll_napi(struct net_device *dev) 164 { 165 struct napi_struct *napi; 166 int cpu = smp_processor_id(); 167 168 list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) { 169 if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) { 170 poll_one_napi(napi); 171 smp_store_release(&napi->poll_owner, -1); 172 } 173 } 174 } 175 176 void netpoll_poll_dev(struct net_device *dev) 177 { 178 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo); 179 const struct net_device_ops *ops; 180 181 /* Don't do any rx activity if the dev_lock mutex is held 182 * the dev_open/close paths use this to block netpoll activity 183 * while changing device state 184 */ 185 if (!ni || down_trylock(&ni->dev_lock)) 186 return; 187 188 /* Some drivers will take the same locks in poll and xmit, 189 * we can't poll if local CPU is already in xmit. 190 */ 191 if (!netif_running(dev) || netif_local_xmit_active(dev)) { 192 up(&ni->dev_lock); 193 return; 194 } 195 196 ops = dev->netdev_ops; 197 if (ops->ndo_poll_controller) 198 ops->ndo_poll_controller(dev); 199 200 poll_napi(dev); 201 202 up(&ni->dev_lock); 203 204 zap_completion_queue(); 205 } 206 EXPORT_SYMBOL(netpoll_poll_dev); 207 208 void netpoll_poll_disable(struct net_device *dev) 209 { 210 struct netpoll_info *ni; 211 212 might_sleep(); 213 ni = rtnl_dereference(dev->npinfo); 214 if (ni) 215 down(&ni->dev_lock); 216 } 217 218 void netpoll_poll_enable(struct net_device *dev) 219 { 220 struct netpoll_info *ni; 221 222 ni = rtnl_dereference(dev->npinfo); 223 if (ni) 224 up(&ni->dev_lock); 225 } 226 227 static void refill_skbs(struct netpoll *np) 228 { 229 struct sk_buff_head *skb_pool; 230 struct sk_buff *skb; 231 unsigned long flags; 232 233 skb_pool = &np->skb_pool; 234 235 spin_lock_irqsave(&skb_pool->lock, flags); 236 while (skb_pool->qlen < MAX_SKBS) { 237 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); 238 if (!skb) 239 break; 240 241 __skb_queue_tail(skb_pool, skb); 242 } 243 spin_unlock_irqrestore(&skb_pool->lock, flags); 244 } 245 246 static void zap_completion_queue(void) 247 { 248 unsigned long flags; 249 struct softnet_data *sd = &get_cpu_var(softnet_data); 250 251 if (sd->completion_queue) { 252 struct sk_buff *clist; 253 254 local_irq_save(flags); 255 clist = sd->completion_queue; 256 sd->completion_queue = NULL; 257 local_irq_restore(flags); 258 259 while (clist != NULL) { 260 struct sk_buff *skb = clist; 261 clist = clist->next; 262 if (!skb_irq_freeable(skb)) { 263 refcount_set(&skb->users, 1); 264 dev_kfree_skb_any(skb); /* put this one back */ 265 } else { 266 __kfree_skb(skb); 267 } 268 } 269 } 270 271 put_cpu_var(softnet_data); 272 } 273 274 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve) 275 { 276 int count = 0; 277 struct sk_buff *skb; 278 279 zap_completion_queue(); 280 repeat: 281 282 skb = alloc_skb(len, GFP_ATOMIC); 283 if (!skb) { 284 skb = skb_dequeue(&np->skb_pool); 285 schedule_work(&np->refill_wq); 286 } 287 288 if (!skb) { 289 if (++count < 10) { 290 netpoll_poll_dev(np->dev); 291 goto repeat; 292 } 293 return NULL; 294 } 295 296 refcount_set(&skb->users, 1); 297 skb_reserve(skb, reserve); 298 return skb; 299 } 300 301 static int netpoll_owner_active(struct net_device *dev) 302 { 303 struct napi_struct *napi; 304 305 list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) { 306 if (READ_ONCE(napi->poll_owner) == smp_processor_id()) 307 return 1; 308 } 309 return 0; 310 } 311 312 /* call with IRQ disabled */ 313 static netdev_tx_t __netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) 314 { 315 netdev_tx_t status = NETDEV_TX_BUSY; 316 netdev_tx_t ret = NET_XMIT_DROP; 317 struct net_device *dev; 318 unsigned long tries; 319 /* It is up to the caller to keep npinfo alive. */ 320 struct netpoll_info *npinfo; 321 322 lockdep_assert_irqs_disabled(); 323 324 dev = np->dev; 325 rcu_read_lock(); 326 npinfo = rcu_dereference_bh(dev->npinfo); 327 328 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) { 329 dev_kfree_skb_irq(skb); 330 goto out; 331 } 332 333 /* don't get messages out of order, and no recursion */ 334 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) { 335 struct netdev_queue *txq; 336 337 txq = netdev_core_pick_tx(dev, skb, NULL); 338 339 /* try until next clock tick */ 340 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; 341 tries > 0; --tries) { 342 if (HARD_TX_TRYLOCK(dev, txq)) { 343 if (!netif_xmit_stopped(txq)) 344 status = netpoll_start_xmit(skb, dev, txq); 345 346 HARD_TX_UNLOCK(dev, txq); 347 348 if (dev_xmit_complete(status)) 349 break; 350 351 } 352 353 /* tickle device maybe there is some cleanup */ 354 netpoll_poll_dev(np->dev); 355 356 udelay(USEC_PER_POLL); 357 } 358 359 WARN_ONCE(!irqs_disabled(), 360 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n", 361 dev->name, dev->netdev_ops->ndo_start_xmit); 362 363 } 364 365 if (!dev_xmit_complete(status)) { 366 skb_queue_tail(&npinfo->txq, skb); 367 schedule_delayed_work(&npinfo->tx_work,0); 368 } 369 ret = NETDEV_TX_OK; 370 out: 371 rcu_read_unlock(); 372 return ret; 373 } 374 375 static void netpoll_udp_checksum(struct netpoll *np, struct sk_buff *skb, 376 int len) 377 { 378 struct udphdr *udph; 379 int udp_len; 380 381 udp_len = len + sizeof(struct udphdr); 382 udph = udp_hdr(skb); 383 384 /* check needs to be set, since it will be consumed in csum_partial */ 385 udph->check = 0; 386 if (np->ipv6) 387 udph->check = csum_ipv6_magic(&np->local_ip.in6, 388 &np->remote_ip.in6, 389 udp_len, IPPROTO_UDP, 390 csum_partial(udph, udp_len, 0)); 391 else 392 udph->check = csum_tcpudp_magic(np->local_ip.ip, 393 np->remote_ip.ip, 394 udp_len, IPPROTO_UDP, 395 csum_partial(udph, udp_len, 0)); 396 if (udph->check == 0) 397 udph->check = CSUM_MANGLED_0; 398 } 399 400 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) 401 { 402 unsigned long flags; 403 netdev_tx_t ret; 404 405 if (unlikely(!np)) { 406 dev_kfree_skb_irq(skb); 407 ret = NET_XMIT_DROP; 408 } else { 409 local_irq_save(flags); 410 ret = __netpoll_send_skb(np, skb); 411 local_irq_restore(flags); 412 } 413 return ret; 414 } 415 EXPORT_SYMBOL(netpoll_send_skb); 416 417 static void push_ipv6(struct netpoll *np, struct sk_buff *skb, int len) 418 { 419 struct ipv6hdr *ip6h; 420 421 skb_push(skb, sizeof(struct ipv6hdr)); 422 skb_reset_network_header(skb); 423 ip6h = ipv6_hdr(skb); 424 425 /* ip6h->version = 6; ip6h->priority = 0; */ 426 *(unsigned char *)ip6h = 0x60; 427 ip6h->flow_lbl[0] = 0; 428 ip6h->flow_lbl[1] = 0; 429 ip6h->flow_lbl[2] = 0; 430 431 ip6h->payload_len = htons(sizeof(struct udphdr) + len); 432 ip6h->nexthdr = IPPROTO_UDP; 433 ip6h->hop_limit = 32; 434 ip6h->saddr = np->local_ip.in6; 435 ip6h->daddr = np->remote_ip.in6; 436 437 skb->protocol = htons(ETH_P_IPV6); 438 } 439 440 static void push_ipv4(struct netpoll *np, struct sk_buff *skb, int len) 441 { 442 static atomic_t ip_ident; 443 struct iphdr *iph; 444 int ip_len; 445 446 ip_len = len + sizeof(struct udphdr) + sizeof(struct iphdr); 447 448 skb_push(skb, sizeof(struct iphdr)); 449 skb_reset_network_header(skb); 450 iph = ip_hdr(skb); 451 452 /* iph->version = 4; iph->ihl = 5; */ 453 *(unsigned char *)iph = 0x45; 454 iph->tos = 0; 455 put_unaligned(htons(ip_len), &iph->tot_len); 456 iph->id = htons(atomic_inc_return(&ip_ident)); 457 iph->frag_off = 0; 458 iph->ttl = 64; 459 iph->protocol = IPPROTO_UDP; 460 iph->check = 0; 461 put_unaligned(np->local_ip.ip, &iph->saddr); 462 put_unaligned(np->remote_ip.ip, &iph->daddr); 463 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 464 skb->protocol = htons(ETH_P_IP); 465 } 466 467 static void push_udp(struct netpoll *np, struct sk_buff *skb, int len) 468 { 469 struct udphdr *udph; 470 int udp_len; 471 472 udp_len = len + sizeof(struct udphdr); 473 474 skb_push(skb, sizeof(struct udphdr)); 475 skb_reset_transport_header(skb); 476 477 udph = udp_hdr(skb); 478 udph->source = htons(np->local_port); 479 udph->dest = htons(np->remote_port); 480 udph->len = htons(udp_len); 481 482 netpoll_udp_checksum(np, skb, len); 483 } 484 485 static void push_eth(struct netpoll *np, struct sk_buff *skb) 486 { 487 struct ethhdr *eth; 488 489 eth = skb_push(skb, ETH_HLEN); 490 skb_reset_mac_header(skb); 491 ether_addr_copy(eth->h_source, np->dev->dev_addr); 492 ether_addr_copy(eth->h_dest, np->remote_mac); 493 if (np->ipv6) 494 eth->h_proto = htons(ETH_P_IPV6); 495 else 496 eth->h_proto = htons(ETH_P_IP); 497 } 498 499 int netpoll_send_udp(struct netpoll *np, const char *msg, int len) 500 { 501 int total_len, ip_len, udp_len; 502 struct sk_buff *skb; 503 504 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 505 WARN_ON_ONCE(!irqs_disabled()); 506 507 udp_len = len + sizeof(struct udphdr); 508 if (np->ipv6) 509 ip_len = udp_len + sizeof(struct ipv6hdr); 510 else 511 ip_len = udp_len + sizeof(struct iphdr); 512 513 total_len = ip_len + LL_RESERVED_SPACE(np->dev); 514 515 skb = find_skb(np, total_len + np->dev->needed_tailroom, 516 total_len - len); 517 if (!skb) 518 return -ENOMEM; 519 520 skb_copy_to_linear_data(skb, msg, len); 521 skb_put(skb, len); 522 523 push_udp(np, skb, len); 524 if (np->ipv6) 525 push_ipv6(np, skb, len); 526 else 527 push_ipv4(np, skb, len); 528 push_eth(np, skb); 529 skb->dev = np->dev; 530 531 return (int)netpoll_send_skb(np, skb); 532 } 533 EXPORT_SYMBOL(netpoll_send_udp); 534 535 536 static void skb_pool_flush(struct netpoll *np) 537 { 538 struct sk_buff_head *skb_pool; 539 540 cancel_work_sync(&np->refill_wq); 541 skb_pool = &np->skb_pool; 542 skb_queue_purge_reason(skb_pool, SKB_CONSUMED); 543 } 544 545 static void refill_skbs_work_handler(struct work_struct *work) 546 { 547 struct netpoll *np = 548 container_of(work, struct netpoll, refill_wq); 549 550 refill_skbs(np); 551 } 552 553 int __netpoll_setup(struct netpoll *np, struct net_device *ndev) 554 { 555 struct netpoll_info *npinfo; 556 const struct net_device_ops *ops; 557 int err; 558 559 skb_queue_head_init(&np->skb_pool); 560 561 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) { 562 np_err(np, "%s doesn't support polling, aborting\n", 563 ndev->name); 564 err = -ENOTSUPP; 565 goto out; 566 } 567 568 npinfo = rtnl_dereference(ndev->npinfo); 569 if (!npinfo) { 570 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); 571 if (!npinfo) { 572 err = -ENOMEM; 573 goto out; 574 } 575 576 sema_init(&npinfo->dev_lock, 1); 577 skb_queue_head_init(&npinfo->txq); 578 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process); 579 580 refcount_set(&npinfo->refcnt, 1); 581 582 ops = ndev->netdev_ops; 583 if (ops->ndo_netpoll_setup) { 584 err = ops->ndo_netpoll_setup(ndev); 585 if (err) 586 goto free_npinfo; 587 } 588 } else { 589 refcount_inc(&npinfo->refcnt); 590 } 591 592 np->dev = ndev; 593 strscpy(np->dev_name, ndev->name, IFNAMSIZ); 594 npinfo->netpoll = np; 595 596 /* fill up the skb queue */ 597 refill_skbs(np); 598 INIT_WORK(&np->refill_wq, refill_skbs_work_handler); 599 600 /* last thing to do is link it to the net device structure */ 601 rcu_assign_pointer(ndev->npinfo, npinfo); 602 603 return 0; 604 605 free_npinfo: 606 kfree(npinfo); 607 out: 608 return err; 609 } 610 EXPORT_SYMBOL_GPL(__netpoll_setup); 611 612 /* 613 * Returns a pointer to a string representation of the identifier used 614 * to select the egress interface for the given netpoll instance. buf 615 * must be a buffer of length at least MAC_ADDR_STR_LEN + 1. 616 */ 617 static char *egress_dev(struct netpoll *np, char *buf) 618 { 619 if (np->dev_name[0]) 620 return np->dev_name; 621 622 snprintf(buf, MAC_ADDR_STR_LEN, "%pM", np->dev_mac); 623 return buf; 624 } 625 626 static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev, 627 unsigned int timeout) 628 { 629 unsigned long atmost; 630 631 atmost = jiffies + timeout * HZ; 632 while (!netif_carrier_ok(ndev)) { 633 if (time_after(jiffies, atmost)) { 634 np_notice(np, "timeout waiting for carrier\n"); 635 break; 636 } 637 msleep(1); 638 } 639 } 640 641 /* 642 * Take the IPv6 from ndev and populate local_ip structure in netpoll 643 */ 644 static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev) 645 { 646 char buf[MAC_ADDR_STR_LEN + 1]; 647 int err = -EDESTADDRREQ; 648 struct inet6_dev *idev; 649 650 if (!IS_ENABLED(CONFIG_IPV6)) { 651 np_err(np, "IPv6 is not supported %s, aborting\n", 652 egress_dev(np, buf)); 653 return -EINVAL; 654 } 655 656 idev = __in6_dev_get(ndev); 657 if (idev) { 658 struct inet6_ifaddr *ifp; 659 660 read_lock_bh(&idev->lock); 661 list_for_each_entry(ifp, &idev->addr_list, if_list) { 662 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) != 663 !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL)) 664 continue; 665 /* Got the IP, let's return */ 666 np->local_ip.in6 = ifp->addr; 667 err = 0; 668 break; 669 } 670 read_unlock_bh(&idev->lock); 671 } 672 if (err) { 673 np_err(np, "no IPv6 address for %s, aborting\n", 674 egress_dev(np, buf)); 675 return err; 676 } 677 678 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6); 679 return 0; 680 } 681 682 /* 683 * Take the IPv4 from ndev and populate local_ip structure in netpoll 684 */ 685 static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev) 686 { 687 char buf[MAC_ADDR_STR_LEN + 1]; 688 const struct in_ifaddr *ifa; 689 struct in_device *in_dev; 690 691 in_dev = __in_dev_get_rtnl(ndev); 692 if (!in_dev) { 693 np_err(np, "no IP address for %s, aborting\n", 694 egress_dev(np, buf)); 695 return -EDESTADDRREQ; 696 } 697 698 ifa = rtnl_dereference(in_dev->ifa_list); 699 if (!ifa) { 700 np_err(np, "no IP address for %s, aborting\n", 701 egress_dev(np, buf)); 702 return -EDESTADDRREQ; 703 } 704 705 np->local_ip.ip = ifa->ifa_local; 706 np_info(np, "local IP %pI4\n", &np->local_ip.ip); 707 708 return 0; 709 } 710 711 int netpoll_setup(struct netpoll *np) 712 { 713 struct net *net = current->nsproxy->net_ns; 714 char buf[MAC_ADDR_STR_LEN + 1]; 715 struct net_device *ndev = NULL; 716 bool ip_overwritten = false; 717 int err; 718 719 rtnl_lock(); 720 if (np->dev_name[0]) 721 ndev = __dev_get_by_name(net, np->dev_name); 722 else if (is_valid_ether_addr(np->dev_mac)) 723 ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac); 724 725 if (!ndev) { 726 np_err(np, "%s doesn't exist, aborting\n", egress_dev(np, buf)); 727 err = -ENODEV; 728 goto unlock; 729 } 730 netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL); 731 732 if (netdev_master_upper_dev_get(ndev)) { 733 np_err(np, "%s is a slave device, aborting\n", 734 egress_dev(np, buf)); 735 err = -EBUSY; 736 goto put; 737 } 738 739 if (!netif_running(ndev)) { 740 np_info(np, "device %s not up yet, forcing it\n", 741 egress_dev(np, buf)); 742 743 err = dev_open(ndev, NULL); 744 if (err) { 745 np_err(np, "failed to open %s\n", ndev->name); 746 goto put; 747 } 748 749 rtnl_unlock(); 750 netpoll_wait_carrier(np, ndev, carrier_timeout); 751 rtnl_lock(); 752 } 753 754 if (!np->local_ip.ip) { 755 if (!np->ipv6) { 756 err = netpoll_take_ipv4(np, ndev); 757 if (err) 758 goto put; 759 } else { 760 err = netpoll_take_ipv6(np, ndev); 761 if (err) 762 goto put; 763 } 764 ip_overwritten = true; 765 } 766 767 err = __netpoll_setup(np, ndev); 768 if (err) 769 goto flush; 770 rtnl_unlock(); 771 772 /* Make sure all NAPI polls which started before dev->npinfo 773 * was visible have exited before we start calling NAPI poll. 774 * NAPI skips locking if dev->npinfo is NULL. 775 */ 776 synchronize_rcu(); 777 778 return 0; 779 780 flush: 781 skb_pool_flush(np); 782 put: 783 DEBUG_NET_WARN_ON_ONCE(np->dev); 784 if (ip_overwritten) 785 memset(&np->local_ip, 0, sizeof(np->local_ip)); 786 netdev_put(ndev, &np->dev_tracker); 787 unlock: 788 rtnl_unlock(); 789 return err; 790 } 791 EXPORT_SYMBOL(netpoll_setup); 792 793 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head) 794 { 795 struct netpoll_info *npinfo = 796 container_of(rcu_head, struct netpoll_info, rcu); 797 798 skb_queue_purge(&npinfo->txq); 799 800 /* we can't call cancel_delayed_work_sync here, as we are in softirq */ 801 cancel_delayed_work(&npinfo->tx_work); 802 803 /* clean after last, unfinished work */ 804 __skb_queue_purge(&npinfo->txq); 805 /* now cancel it again */ 806 cancel_delayed_work(&npinfo->tx_work); 807 kfree(npinfo); 808 } 809 810 static void __netpoll_cleanup(struct netpoll *np) 811 { 812 struct netpoll_info *npinfo; 813 814 npinfo = rtnl_dereference(np->dev->npinfo); 815 if (!npinfo) 816 return; 817 818 if (refcount_dec_and_test(&npinfo->refcnt)) { 819 const struct net_device_ops *ops; 820 821 ops = np->dev->netdev_ops; 822 if (ops->ndo_netpoll_cleanup) 823 ops->ndo_netpoll_cleanup(np->dev); 824 825 RCU_INIT_POINTER(np->dev->npinfo, NULL); 826 call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info); 827 } else 828 RCU_INIT_POINTER(np->dev->npinfo, NULL); 829 830 skb_pool_flush(np); 831 } 832 833 void __netpoll_free(struct netpoll *np) 834 { 835 ASSERT_RTNL(); 836 837 /* Wait for transmitting packets to finish before freeing. */ 838 synchronize_rcu(); 839 __netpoll_cleanup(np); 840 kfree(np); 841 } 842 EXPORT_SYMBOL_GPL(__netpoll_free); 843 844 void do_netpoll_cleanup(struct netpoll *np) 845 { 846 __netpoll_cleanup(np); 847 netdev_put(np->dev, &np->dev_tracker); 848 np->dev = NULL; 849 } 850 EXPORT_SYMBOL(do_netpoll_cleanup); 851 852 void netpoll_cleanup(struct netpoll *np) 853 { 854 rtnl_lock(); 855 if (!np->dev) 856 goto out; 857 do_netpoll_cleanup(np); 858 out: 859 rtnl_unlock(); 860 } 861 EXPORT_SYMBOL(netpoll_cleanup); 862