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 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) 376 { 377 unsigned long flags; 378 netdev_tx_t ret; 379 380 if (unlikely(!np)) { 381 dev_kfree_skb_irq(skb); 382 ret = NET_XMIT_DROP; 383 } else { 384 local_irq_save(flags); 385 ret = __netpoll_send_skb(np, skb); 386 local_irq_restore(flags); 387 } 388 return ret; 389 } 390 EXPORT_SYMBOL(netpoll_send_skb); 391 392 int netpoll_send_udp(struct netpoll *np, const char *msg, int len) 393 { 394 int total_len, ip_len, udp_len; 395 struct sk_buff *skb; 396 struct udphdr *udph; 397 struct iphdr *iph; 398 struct ethhdr *eth; 399 static atomic_t ip_ident; 400 struct ipv6hdr *ip6h; 401 402 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 403 WARN_ON_ONCE(!irqs_disabled()); 404 405 udp_len = len + sizeof(*udph); 406 if (np->ipv6) 407 ip_len = udp_len + sizeof(*ip6h); 408 else 409 ip_len = udp_len + sizeof(*iph); 410 411 total_len = ip_len + LL_RESERVED_SPACE(np->dev); 412 413 skb = find_skb(np, total_len + np->dev->needed_tailroom, 414 total_len - len); 415 if (!skb) 416 return -ENOMEM; 417 418 skb_copy_to_linear_data(skb, msg, len); 419 skb_put(skb, len); 420 421 skb_push(skb, sizeof(*udph)); 422 skb_reset_transport_header(skb); 423 udph = udp_hdr(skb); 424 udph->source = htons(np->local_port); 425 udph->dest = htons(np->remote_port); 426 udph->len = htons(udp_len); 427 428 udph->check = 0; 429 if (np->ipv6) { 430 udph->check = csum_ipv6_magic(&np->local_ip.in6, 431 &np->remote_ip.in6, 432 udp_len, IPPROTO_UDP, 433 csum_partial(udph, udp_len, 0)); 434 if (udph->check == 0) 435 udph->check = CSUM_MANGLED_0; 436 437 skb_push(skb, sizeof(*ip6h)); 438 skb_reset_network_header(skb); 439 ip6h = ipv6_hdr(skb); 440 441 /* ip6h->version = 6; ip6h->priority = 0; */ 442 *(unsigned char *)ip6h = 0x60; 443 ip6h->flow_lbl[0] = 0; 444 ip6h->flow_lbl[1] = 0; 445 ip6h->flow_lbl[2] = 0; 446 447 ip6h->payload_len = htons(sizeof(struct udphdr) + len); 448 ip6h->nexthdr = IPPROTO_UDP; 449 ip6h->hop_limit = 32; 450 ip6h->saddr = np->local_ip.in6; 451 ip6h->daddr = np->remote_ip.in6; 452 453 eth = skb_push(skb, ETH_HLEN); 454 skb_reset_mac_header(skb); 455 skb->protocol = eth->h_proto = htons(ETH_P_IPV6); 456 } else { 457 udph->check = csum_tcpudp_magic(np->local_ip.ip, 458 np->remote_ip.ip, 459 udp_len, IPPROTO_UDP, 460 csum_partial(udph, udp_len, 0)); 461 if (udph->check == 0) 462 udph->check = CSUM_MANGLED_0; 463 464 skb_push(skb, sizeof(*iph)); 465 skb_reset_network_header(skb); 466 iph = ip_hdr(skb); 467 468 /* iph->version = 4; iph->ihl = 5; */ 469 *(unsigned char *)iph = 0x45; 470 iph->tos = 0; 471 put_unaligned(htons(ip_len), &(iph->tot_len)); 472 iph->id = htons(atomic_inc_return(&ip_ident)); 473 iph->frag_off = 0; 474 iph->ttl = 64; 475 iph->protocol = IPPROTO_UDP; 476 iph->check = 0; 477 put_unaligned(np->local_ip.ip, &(iph->saddr)); 478 put_unaligned(np->remote_ip.ip, &(iph->daddr)); 479 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 480 481 eth = skb_push(skb, ETH_HLEN); 482 skb_reset_mac_header(skb); 483 skb->protocol = eth->h_proto = htons(ETH_P_IP); 484 } 485 486 ether_addr_copy(eth->h_source, np->dev->dev_addr); 487 ether_addr_copy(eth->h_dest, np->remote_mac); 488 489 skb->dev = np->dev; 490 491 return (int)netpoll_send_skb(np, skb); 492 } 493 EXPORT_SYMBOL(netpoll_send_udp); 494 495 496 static void skb_pool_flush(struct netpoll *np) 497 { 498 struct sk_buff_head *skb_pool; 499 500 cancel_work_sync(&np->refill_wq); 501 skb_pool = &np->skb_pool; 502 skb_queue_purge_reason(skb_pool, SKB_CONSUMED); 503 } 504 505 static void refill_skbs_work_handler(struct work_struct *work) 506 { 507 struct netpoll *np = 508 container_of(work, struct netpoll, refill_wq); 509 510 refill_skbs(np); 511 } 512 513 int __netpoll_setup(struct netpoll *np, struct net_device *ndev) 514 { 515 struct netpoll_info *npinfo; 516 const struct net_device_ops *ops; 517 int err; 518 519 skb_queue_head_init(&np->skb_pool); 520 521 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) { 522 np_err(np, "%s doesn't support polling, aborting\n", 523 ndev->name); 524 err = -ENOTSUPP; 525 goto out; 526 } 527 528 npinfo = rtnl_dereference(ndev->npinfo); 529 if (!npinfo) { 530 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL); 531 if (!npinfo) { 532 err = -ENOMEM; 533 goto out; 534 } 535 536 sema_init(&npinfo->dev_lock, 1); 537 skb_queue_head_init(&npinfo->txq); 538 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process); 539 540 refcount_set(&npinfo->refcnt, 1); 541 542 ops = ndev->netdev_ops; 543 if (ops->ndo_netpoll_setup) { 544 err = ops->ndo_netpoll_setup(ndev); 545 if (err) 546 goto free_npinfo; 547 } 548 } else { 549 refcount_inc(&npinfo->refcnt); 550 } 551 552 np->dev = ndev; 553 strscpy(np->dev_name, ndev->name, IFNAMSIZ); 554 npinfo->netpoll = np; 555 556 /* fill up the skb queue */ 557 refill_skbs(np); 558 INIT_WORK(&np->refill_wq, refill_skbs_work_handler); 559 560 /* last thing to do is link it to the net device structure */ 561 rcu_assign_pointer(ndev->npinfo, npinfo); 562 563 return 0; 564 565 free_npinfo: 566 kfree(npinfo); 567 out: 568 return err; 569 } 570 EXPORT_SYMBOL_GPL(__netpoll_setup); 571 572 /* 573 * Returns a pointer to a string representation of the identifier used 574 * to select the egress interface for the given netpoll instance. buf 575 * must be a buffer of length at least MAC_ADDR_STR_LEN + 1. 576 */ 577 static char *egress_dev(struct netpoll *np, char *buf) 578 { 579 if (np->dev_name[0]) 580 return np->dev_name; 581 582 snprintf(buf, MAC_ADDR_STR_LEN, "%pM", np->dev_mac); 583 return buf; 584 } 585 586 static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev, 587 unsigned int timeout) 588 { 589 unsigned long atmost; 590 591 atmost = jiffies + timeout * HZ; 592 while (!netif_carrier_ok(ndev)) { 593 if (time_after(jiffies, atmost)) { 594 np_notice(np, "timeout waiting for carrier\n"); 595 break; 596 } 597 msleep(1); 598 } 599 } 600 601 /* 602 * Take the IPv6 from ndev and populate local_ip structure in netpoll 603 */ 604 static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev) 605 { 606 char buf[MAC_ADDR_STR_LEN + 1]; 607 int err = -EDESTADDRREQ; 608 struct inet6_dev *idev; 609 610 if (!IS_ENABLED(CONFIG_IPV6)) { 611 np_err(np, "IPv6 is not supported %s, aborting\n", 612 egress_dev(np, buf)); 613 return -EINVAL; 614 } 615 616 idev = __in6_dev_get(ndev); 617 if (idev) { 618 struct inet6_ifaddr *ifp; 619 620 read_lock_bh(&idev->lock); 621 list_for_each_entry(ifp, &idev->addr_list, if_list) { 622 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) != 623 !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL)) 624 continue; 625 /* Got the IP, let's return */ 626 np->local_ip.in6 = ifp->addr; 627 err = 0; 628 break; 629 } 630 read_unlock_bh(&idev->lock); 631 } 632 if (err) { 633 np_err(np, "no IPv6 address for %s, aborting\n", 634 egress_dev(np, buf)); 635 return err; 636 } 637 638 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6); 639 return 0; 640 } 641 642 /* 643 * Take the IPv4 from ndev and populate local_ip structure in netpoll 644 */ 645 static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev) 646 { 647 char buf[MAC_ADDR_STR_LEN + 1]; 648 const struct in_ifaddr *ifa; 649 struct in_device *in_dev; 650 651 in_dev = __in_dev_get_rtnl(ndev); 652 if (!in_dev) { 653 np_err(np, "no IP address for %s, aborting\n", 654 egress_dev(np, buf)); 655 return -EDESTADDRREQ; 656 } 657 658 ifa = rtnl_dereference(in_dev->ifa_list); 659 if (!ifa) { 660 np_err(np, "no IP address for %s, aborting\n", 661 egress_dev(np, buf)); 662 return -EDESTADDRREQ; 663 } 664 665 np->local_ip.ip = ifa->ifa_local; 666 np_info(np, "local IP %pI4\n", &np->local_ip.ip); 667 668 return 0; 669 } 670 671 int netpoll_setup(struct netpoll *np) 672 { 673 struct net *net = current->nsproxy->net_ns; 674 char buf[MAC_ADDR_STR_LEN + 1]; 675 struct net_device *ndev = NULL; 676 bool ip_overwritten = false; 677 int err; 678 679 rtnl_lock(); 680 if (np->dev_name[0]) 681 ndev = __dev_get_by_name(net, np->dev_name); 682 else if (is_valid_ether_addr(np->dev_mac)) 683 ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac); 684 685 if (!ndev) { 686 np_err(np, "%s doesn't exist, aborting\n", egress_dev(np, buf)); 687 err = -ENODEV; 688 goto unlock; 689 } 690 netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL); 691 692 if (netdev_master_upper_dev_get(ndev)) { 693 np_err(np, "%s is a slave device, aborting\n", 694 egress_dev(np, buf)); 695 err = -EBUSY; 696 goto put; 697 } 698 699 if (!netif_running(ndev)) { 700 np_info(np, "device %s not up yet, forcing it\n", 701 egress_dev(np, buf)); 702 703 err = dev_open(ndev, NULL); 704 if (err) { 705 np_err(np, "failed to open %s\n", ndev->name); 706 goto put; 707 } 708 709 rtnl_unlock(); 710 netpoll_wait_carrier(np, ndev, carrier_timeout); 711 rtnl_lock(); 712 } 713 714 if (!np->local_ip.ip) { 715 if (!np->ipv6) { 716 err = netpoll_take_ipv4(np, ndev); 717 if (err) 718 goto put; 719 } else { 720 err = netpoll_take_ipv6(np, ndev); 721 if (err) 722 goto put; 723 } 724 ip_overwritten = true; 725 } 726 727 err = __netpoll_setup(np, ndev); 728 if (err) 729 goto flush; 730 rtnl_unlock(); 731 return 0; 732 733 flush: 734 skb_pool_flush(np); 735 put: 736 DEBUG_NET_WARN_ON_ONCE(np->dev); 737 if (ip_overwritten) 738 memset(&np->local_ip, 0, sizeof(np->local_ip)); 739 netdev_put(ndev, &np->dev_tracker); 740 unlock: 741 rtnl_unlock(); 742 return err; 743 } 744 EXPORT_SYMBOL(netpoll_setup); 745 746 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head) 747 { 748 struct netpoll_info *npinfo = 749 container_of(rcu_head, struct netpoll_info, rcu); 750 751 skb_queue_purge(&npinfo->txq); 752 753 /* we can't call cancel_delayed_work_sync here, as we are in softirq */ 754 cancel_delayed_work(&npinfo->tx_work); 755 756 /* clean after last, unfinished work */ 757 __skb_queue_purge(&npinfo->txq); 758 /* now cancel it again */ 759 cancel_delayed_work(&npinfo->tx_work); 760 kfree(npinfo); 761 } 762 763 static void __netpoll_cleanup(struct netpoll *np) 764 { 765 struct netpoll_info *npinfo; 766 767 npinfo = rtnl_dereference(np->dev->npinfo); 768 if (!npinfo) 769 return; 770 771 if (refcount_dec_and_test(&npinfo->refcnt)) { 772 const struct net_device_ops *ops; 773 774 ops = np->dev->netdev_ops; 775 if (ops->ndo_netpoll_cleanup) 776 ops->ndo_netpoll_cleanup(np->dev); 777 778 RCU_INIT_POINTER(np->dev->npinfo, NULL); 779 call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info); 780 } else 781 RCU_INIT_POINTER(np->dev->npinfo, NULL); 782 783 skb_pool_flush(np); 784 } 785 786 void __netpoll_free(struct netpoll *np) 787 { 788 ASSERT_RTNL(); 789 790 /* Wait for transmitting packets to finish before freeing. */ 791 synchronize_rcu(); 792 __netpoll_cleanup(np); 793 kfree(np); 794 } 795 EXPORT_SYMBOL_GPL(__netpoll_free); 796 797 void do_netpoll_cleanup(struct netpoll *np) 798 { 799 __netpoll_cleanup(np); 800 netdev_put(np->dev, &np->dev_tracker); 801 np->dev = NULL; 802 } 803 EXPORT_SYMBOL(do_netpoll_cleanup); 804 805 void netpoll_cleanup(struct netpoll *np) 806 { 807 rtnl_lock(); 808 if (!np->dev) 809 goto out; 810 do_netpoll_cleanup(np); 811 out: 812 rtnl_unlock(); 813 } 814 EXPORT_SYMBOL(netpoll_cleanup); 815