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 <linux/udp.h> 33 #include <net/tcp.h> 34 #include <net/addrconf.h> 35 #include <net/ndisc.h> 36 #include <trace/events/napi.h> 37 #include <linux/kconfig.h> 38 39 /* 40 * We maintain a small pool of fully-sized skbs, to make sure the 41 * message gets out even in extreme OOM situations. 42 */ 43 44 #define MAX_UDP_CHUNK 1460 45 #define MAX_SKBS 32 46 #define USEC_PER_POLL 50 47 48 #define MAX_SKB_SIZE \ 49 (sizeof(struct ethhdr) + \ 50 sizeof(struct iphdr) + \ 51 sizeof(struct udphdr) + \ 52 MAX_UDP_CHUNK) 53 54 static unsigned int carrier_timeout = 4; 55 module_param(carrier_timeout, uint, 0644); 56 57 static netdev_tx_t netpoll_start_xmit(struct sk_buff *skb, 58 struct net_device *dev, 59 struct netdev_queue *txq) 60 { 61 netdev_tx_t status = NETDEV_TX_OK; 62 netdev_features_t features; 63 64 features = netif_skb_features(skb); 65 66 if (skb_vlan_tag_present(skb) && 67 !vlan_hw_offload_capable(features, skb->vlan_proto)) { 68 skb = __vlan_hwaccel_push_inside(skb); 69 if (unlikely(!skb)) { 70 /* This is actually a packet drop, but we 71 * don't want the code that calls this 72 * function to try and operate on a NULL skb. 73 */ 74 goto out; 75 } 76 } 77 78 status = netdev_start_xmit(skb, dev, txq, false); 79 80 out: 81 return status; 82 } 83 84 static void queue_process(struct work_struct *work) 85 { 86 struct netpoll_info *npinfo = 87 container_of(work, struct netpoll_info, tx_work.work); 88 struct sk_buff *skb; 89 unsigned long flags; 90 91 while ((skb = skb_dequeue(&npinfo->txq))) { 92 struct net_device *dev = skb->dev; 93 struct netdev_queue *txq; 94 unsigned int q_index; 95 96 if (!netif_device_present(dev) || !netif_running(dev)) { 97 kfree_skb(skb); 98 continue; 99 } 100 101 local_irq_save(flags); 102 /* check if skb->queue_mapping is still valid */ 103 q_index = skb_get_queue_mapping(skb); 104 if (unlikely(q_index >= dev->real_num_tx_queues)) { 105 q_index = q_index % dev->real_num_tx_queues; 106 skb_set_queue_mapping(skb, q_index); 107 } 108 txq = netdev_get_tx_queue(dev, q_index); 109 HARD_TX_LOCK(dev, txq, smp_processor_id()); 110 if (netif_xmit_frozen_or_stopped(txq) || 111 !dev_xmit_complete(netpoll_start_xmit(skb, dev, txq))) { 112 skb_queue_head(&npinfo->txq, skb); 113 HARD_TX_UNLOCK(dev, txq); 114 local_irq_restore(flags); 115 116 schedule_delayed_work(&npinfo->tx_work, HZ/10); 117 return; 118 } 119 HARD_TX_UNLOCK(dev, txq); 120 local_irq_restore(flags); 121 } 122 } 123 124 static int netif_local_xmit_active(struct net_device *dev) 125 { 126 int i; 127 128 for (i = 0; i < dev->num_tx_queues; i++) { 129 struct netdev_queue *txq = netdev_get_tx_queue(dev, i); 130 131 if (netif_tx_owned(txq, smp_processor_id())) 132 return 1; 133 } 134 135 return 0; 136 } 137 138 static void poll_one_napi(struct napi_struct *napi) 139 { 140 int work; 141 142 /* If we set this bit but see that it has already been set, 143 * that indicates that napi has been disabled and we need 144 * to abort this operation 145 */ 146 if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state)) 147 return; 148 149 /* We explicitly pass the polling call a budget of 0 to 150 * indicate that we are clearing the Tx path only. 151 */ 152 work = napi->poll(napi, 0); 153 WARN_ONCE(work, "%pS exceeded budget in poll\n", napi->poll); 154 trace_napi_poll(napi, work, 0); 155 156 clear_bit(NAPI_STATE_NPSVC, &napi->state); 157 } 158 159 static void poll_napi(struct net_device *dev) 160 { 161 struct napi_struct *napi; 162 int cpu = smp_processor_id(); 163 164 list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) { 165 if (cmpxchg(&napi->poll_owner, -1, cpu) == -1) { 166 poll_one_napi(napi); 167 smp_store_release(&napi->poll_owner, -1); 168 } 169 } 170 } 171 172 void netpoll_poll_dev(struct net_device *dev) 173 { 174 struct netpoll_info *ni = rcu_dereference_bh(dev->npinfo); 175 const struct net_device_ops *ops; 176 177 /* Don't do any rx activity if the dev_lock mutex is held 178 * the dev_open/close paths use this to block netpoll activity 179 * while changing device state 180 */ 181 if (!ni || down_trylock(&ni->dev_lock)) 182 return; 183 184 /* Some drivers will take the same locks in poll and xmit, 185 * we can't poll if local CPU is already in xmit. 186 */ 187 if (!netif_running(dev) || netif_local_xmit_active(dev)) { 188 up(&ni->dev_lock); 189 return; 190 } 191 192 ops = dev->netdev_ops; 193 if (ops->ndo_poll_controller) 194 ops->ndo_poll_controller(dev); 195 196 poll_napi(dev); 197 198 up(&ni->dev_lock); 199 200 netpoll_zap_completion_queue(); 201 } 202 EXPORT_SYMBOL(netpoll_poll_dev); 203 204 void netpoll_poll_disable(struct net_device *dev) 205 { 206 struct netpoll_info *ni; 207 208 might_sleep(); 209 ni = rtnl_dereference(dev->npinfo); 210 if (ni) 211 down(&ni->dev_lock); 212 } 213 214 void netpoll_poll_enable(struct net_device *dev) 215 { 216 struct netpoll_info *ni; 217 218 ni = rtnl_dereference(dev->npinfo); 219 if (ni) 220 up(&ni->dev_lock); 221 } 222 223 static void refill_skbs(struct netpoll *np) 224 { 225 struct sk_buff_head *skb_pool; 226 struct sk_buff *skb; 227 228 skb_pool = &np->skb_pool; 229 230 while (READ_ONCE(skb_pool->qlen) < MAX_SKBS) { 231 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC); 232 if (!skb) 233 break; 234 235 skb_queue_tail(skb_pool, skb); 236 } 237 } 238 239 void netpoll_zap_completion_queue(void) 240 { 241 unsigned long flags; 242 struct softnet_data *sd = &get_cpu_var(softnet_data); 243 244 if (sd->completion_queue) { 245 struct sk_buff *clist; 246 247 local_irq_save(flags); 248 clist = sd->completion_queue; 249 sd->completion_queue = NULL; 250 local_irq_restore(flags); 251 252 while (clist != NULL) { 253 struct sk_buff *skb = clist; 254 clist = clist->next; 255 if (!skb_irq_freeable(skb)) { 256 refcount_set(&skb->users, 1); 257 dev_kfree_skb_any(skb); /* put this one back */ 258 } else { 259 __kfree_skb(skb); 260 } 261 } 262 } 263 264 put_cpu_var(softnet_data); 265 } 266 EXPORT_SYMBOL_NS_GPL(netpoll_zap_completion_queue, "NETDEV_INTERNAL"); 267 268 static int netpoll_owner_active(struct net_device *dev) 269 { 270 struct napi_struct *napi; 271 272 list_for_each_entry_rcu(napi, &dev->napi_list, dev_list) { 273 if (READ_ONCE(napi->poll_owner) == smp_processor_id()) 274 return 1; 275 } 276 return 0; 277 } 278 279 /* call with IRQ disabled */ 280 static netdev_tx_t __netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) 281 { 282 netdev_tx_t status = NETDEV_TX_BUSY; 283 netdev_tx_t ret = NET_XMIT_DROP; 284 struct net_device *dev; 285 unsigned long tries; 286 /* It is up to the caller to keep npinfo alive. */ 287 struct netpoll_info *npinfo; 288 289 lockdep_assert_irqs_disabled(); 290 291 dev = np->dev; 292 /* npinfo->txq belongs to np->dev, so retries must stay bound to it. */ 293 skb->dev = dev; 294 rcu_read_lock(); 295 npinfo = rcu_dereference_bh(dev->npinfo); 296 297 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) { 298 dev_kfree_skb_irq(skb); 299 goto out; 300 } 301 302 /* don't get messages out of order, and no recursion */ 303 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) { 304 struct netdev_queue *txq; 305 306 txq = netdev_core_pick_tx(dev, skb, NULL); 307 308 /* try until next clock tick */ 309 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; 310 tries > 0; --tries) { 311 if (HARD_TX_TRYLOCK(dev, txq)) { 312 if (!netif_xmit_stopped(txq)) 313 status = netpoll_start_xmit(skb, dev, txq); 314 315 HARD_TX_UNLOCK(dev, txq); 316 317 if (dev_xmit_complete(status)) 318 break; 319 320 } 321 322 /* tickle device maybe there is some cleanup */ 323 netpoll_poll_dev(np->dev); 324 325 udelay(USEC_PER_POLL); 326 } 327 328 WARN_ONCE(!irqs_disabled(), 329 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n", 330 dev->name, dev->netdev_ops->ndo_start_xmit); 331 332 } 333 334 if (!dev_xmit_complete(status)) { 335 skb_queue_tail(&npinfo->txq, skb); 336 schedule_delayed_work(&npinfo->tx_work,0); 337 } 338 ret = NETDEV_TX_OK; 339 out: 340 rcu_read_unlock(); 341 return ret; 342 } 343 344 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) 345 { 346 unsigned long flags; 347 netdev_tx_t ret; 348 349 if (unlikely(!np)) { 350 dev_kfree_skb_irq(skb); 351 ret = NET_XMIT_DROP; 352 } else { 353 local_irq_save(flags); 354 ret = __netpoll_send_skb(np, skb); 355 local_irq_restore(flags); 356 } 357 return ret; 358 } 359 EXPORT_SYMBOL(netpoll_send_skb); 360 361 static void skb_pool_flush(struct netpoll *np) 362 { 363 struct sk_buff_head *skb_pool; 364 365 cancel_work_sync(&np->refill_wq); 366 skb_pool = &np->skb_pool; 367 skb_queue_purge_reason(skb_pool, SKB_CONSUMED); 368 } 369 370 static void refill_skbs_work_handler(struct work_struct *work) 371 { 372 struct netpoll *np = 373 container_of(work, struct netpoll, refill_wq); 374 375 refill_skbs(np); 376 } 377 378 int __netpoll_setup(struct netpoll *np, struct net_device *ndev) 379 { 380 struct netpoll_info *npinfo; 381 const struct net_device_ops *ops; 382 int err; 383 384 skb_queue_head_init(&np->skb_pool); 385 INIT_WORK(&np->refill_wq, refill_skbs_work_handler); 386 387 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) { 388 np_err(np, "%s doesn't support polling, aborting\n", 389 ndev->name); 390 err = -ENOTSUPP; 391 goto out; 392 } 393 394 npinfo = rtnl_dereference(ndev->npinfo); 395 if (!npinfo) { 396 npinfo = kmalloc_obj(*npinfo); 397 if (!npinfo) { 398 err = -ENOMEM; 399 goto out; 400 } 401 402 sema_init(&npinfo->dev_lock, 1); 403 skb_queue_head_init(&npinfo->txq); 404 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process); 405 406 refcount_set(&npinfo->refcnt, 1); 407 408 ops = ndev->netdev_ops; 409 if (ops->ndo_netpoll_setup) { 410 err = ops->ndo_netpoll_setup(ndev); 411 if (err) 412 goto free_npinfo; 413 } 414 } else { 415 refcount_inc(&npinfo->refcnt); 416 } 417 418 np->dev = ndev; 419 strscpy(np->dev_name, ndev->name, IFNAMSIZ); 420 421 /* fill up the skb queue */ 422 refill_skbs(np); 423 424 /* last thing to do is link it to the net device structure */ 425 rcu_assign_pointer(ndev->npinfo, npinfo); 426 427 return 0; 428 429 free_npinfo: 430 kfree(npinfo); 431 out: 432 return err; 433 } 434 EXPORT_SYMBOL_GPL(__netpoll_setup); 435 436 /* 437 * Returns a pointer to a string representation of the identifier used 438 * to select the egress interface for the given netpoll instance. buf 439 * is used to format np->dev_mac when np->dev_name is empty; bufsz must 440 * be at least MAC_ADDR_STR_LEN + 1 to fit the formatted MAC address 441 * and its NUL terminator. 442 */ 443 static char *egress_dev(struct netpoll *np, char *buf, size_t bufsz) 444 { 445 if (np->dev_name[0]) 446 return np->dev_name; 447 448 snprintf(buf, bufsz, "%pM", np->dev_mac); 449 return buf; 450 } 451 452 static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev, 453 unsigned int timeout) 454 { 455 unsigned long atmost; 456 457 atmost = jiffies + timeout * HZ; 458 while (!netif_carrier_ok(ndev)) { 459 if (time_after(jiffies, atmost)) { 460 np_notice(np, "timeout waiting for carrier\n"); 461 break; 462 } 463 msleep(1); 464 } 465 } 466 467 /* 468 * Take the IPv6 from ndev and populate local_ip structure in netpoll 469 */ 470 static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev) 471 { 472 char buf[MAC_ADDR_STR_LEN + 1]; 473 int err = -EDESTADDRREQ; 474 struct inet6_dev *idev; 475 476 if (!IS_ENABLED(CONFIG_IPV6)) { 477 np_err(np, "IPv6 is not supported %s, aborting\n", 478 egress_dev(np, buf, sizeof(buf))); 479 return -EINVAL; 480 } 481 482 idev = __in6_dev_get(ndev); 483 if (idev) { 484 struct inet6_ifaddr *ifp; 485 486 read_lock_bh(&idev->lock); 487 list_for_each_entry(ifp, &idev->addr_list, if_list) { 488 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) != 489 !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL)) 490 continue; 491 /* Got the IP, let's return */ 492 np->local_ip.in6 = ifp->addr; 493 err = 0; 494 break; 495 } 496 read_unlock_bh(&idev->lock); 497 } 498 if (err) { 499 np_err(np, "no IPv6 address for %s, aborting\n", 500 egress_dev(np, buf, sizeof(buf))); 501 return err; 502 } 503 504 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6); 505 return 0; 506 } 507 508 /* 509 * Take the IPv4 from ndev and populate local_ip structure in netpoll 510 */ 511 static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev) 512 { 513 char buf[MAC_ADDR_STR_LEN + 1]; 514 const struct in_ifaddr *ifa; 515 struct in_device *in_dev; 516 517 in_dev = __in_dev_get_rtnl(ndev); 518 if (!in_dev) { 519 np_err(np, "no IP address for %s, aborting\n", 520 egress_dev(np, buf, sizeof(buf))); 521 return -EDESTADDRREQ; 522 } 523 524 ifa = rtnl_dereference(in_dev->ifa_list); 525 if (!ifa) { 526 np_err(np, "no IP address for %s, aborting\n", 527 egress_dev(np, buf, sizeof(buf))); 528 return -EDESTADDRREQ; 529 } 530 531 np->local_ip.ip = ifa->ifa_local; 532 np_info(np, "local IP %pI4\n", &np->local_ip.ip); 533 534 return 0; 535 } 536 537 /* 538 * Test whether the caller left np->local_ip unset, so that 539 * netpoll_setup() should auto-populate it from the egress device. 540 * 541 * np->local_ip is a union of __be32 (IPv4) and struct in6_addr (IPv6), 542 * so an IPv6 address whose first 4 bytes are zero (e.g. ::1, ::2, 543 * IPv4-mapped ::ffff:a.b.c.d) must not be tested via the IPv4 arm — 544 * doing so would misclassify a caller-supplied address as unset and 545 * silently overwrite it with whatever address the device exposes. 546 */ 547 static bool netpoll_local_ip_unset(const struct netpoll *np) 548 { 549 if (np->ipv6) 550 return ipv6_addr_any(&np->local_ip.in6); 551 return !np->local_ip.ip; 552 } 553 554 int netpoll_setup(struct netpoll *np) 555 { 556 struct net *net = current->nsproxy->net_ns; 557 char buf[MAC_ADDR_STR_LEN + 1]; 558 struct net_device *ndev = NULL; 559 bool ip_overwritten = false; 560 int err; 561 562 rtnl_lock(); 563 if (np->dev_name[0]) 564 ndev = __dev_get_by_name(net, np->dev_name); 565 else if (is_valid_ether_addr(np->dev_mac)) 566 ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac); 567 568 if (!ndev) { 569 np_err(np, "%s doesn't exist, aborting\n", 570 egress_dev(np, buf, sizeof(buf))); 571 err = -ENODEV; 572 goto unlock; 573 } 574 netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL); 575 576 if (netdev_master_upper_dev_get(ndev)) { 577 np_err(np, "%s is a slave device, aborting\n", 578 egress_dev(np, buf, sizeof(buf))); 579 err = -EBUSY; 580 goto put; 581 } 582 583 if (!netif_running(ndev)) { 584 np_info(np, "device %s not up yet, forcing it\n", 585 egress_dev(np, buf, sizeof(buf))); 586 587 err = dev_open(ndev, NULL); 588 if (err) { 589 np_err(np, "failed to open %s\n", ndev->name); 590 goto put; 591 } 592 593 rtnl_unlock(); 594 netpoll_wait_carrier(np, ndev, carrier_timeout); 595 rtnl_lock(); 596 } 597 598 if (netpoll_local_ip_unset(np)) { 599 if (!np->ipv6) { 600 err = netpoll_take_ipv4(np, ndev); 601 if (err) 602 goto put; 603 } else { 604 err = netpoll_take_ipv6(np, ndev); 605 if (err) 606 goto put; 607 } 608 ip_overwritten = true; 609 } 610 611 err = __netpoll_setup(np, ndev); 612 if (err) 613 goto flush; 614 rtnl_unlock(); 615 616 /* Make sure all NAPI polls which started before dev->npinfo 617 * was visible have exited before we start calling NAPI poll. 618 * NAPI skips locking if dev->npinfo is NULL. 619 */ 620 synchronize_rcu(); 621 622 return 0; 623 624 flush: 625 skb_pool_flush(np); 626 put: 627 DEBUG_NET_WARN_ON_ONCE(np->dev); 628 if (ip_overwritten) 629 memset(&np->local_ip, 0, sizeof(np->local_ip)); 630 netdev_put(ndev, &np->dev_tracker); 631 unlock: 632 rtnl_unlock(); 633 return err; 634 } 635 EXPORT_SYMBOL(netpoll_setup); 636 637 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head) 638 { 639 struct netpoll_info *npinfo = 640 container_of(rcu_head, struct netpoll_info, rcu); 641 642 skb_queue_purge(&npinfo->txq); 643 644 /* we can't call cancel_delayed_work_sync here, as we are in softirq */ 645 cancel_delayed_work(&npinfo->tx_work); 646 647 /* clean after last, unfinished work */ 648 __skb_queue_purge(&npinfo->txq); 649 /* now cancel it again */ 650 cancel_delayed_work(&npinfo->tx_work); 651 kfree(npinfo); 652 } 653 654 static void __netpoll_cleanup(struct netpoll *np) 655 { 656 struct netpoll_info *npinfo; 657 658 npinfo = rtnl_dereference(np->dev->npinfo); 659 if (!npinfo) 660 return; 661 662 /* At this point, there is a single npinfo instance per netdevice, and 663 * its refcnt tracks how many netpoll structures are linked to it. We 664 * only perform npinfo cleanup when the refcnt decrements to zero. 665 */ 666 if (refcount_dec_and_test(&npinfo->refcnt)) { 667 const struct net_device_ops *ops; 668 669 ops = np->dev->netdev_ops; 670 if (ops->ndo_netpoll_cleanup) 671 ops->ndo_netpoll_cleanup(np->dev); 672 673 RCU_INIT_POINTER(np->dev->npinfo, NULL); 674 call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info); 675 } 676 677 skb_pool_flush(np); 678 } 679 680 void __netpoll_free(struct netpoll *np) 681 { 682 ASSERT_RTNL(); 683 684 /* Wait for transmitting packets to finish before freeing. */ 685 synchronize_net(); 686 __netpoll_cleanup(np); 687 kfree(np); 688 } 689 EXPORT_SYMBOL_GPL(__netpoll_free); 690 691 void do_netpoll_cleanup(struct netpoll *np) 692 { 693 __netpoll_cleanup(np); 694 netdev_put(np->dev, &np->dev_tracker); 695 np->dev = NULL; 696 } 697 EXPORT_SYMBOL(do_netpoll_cleanup); 698 699 void netpoll_cleanup(struct netpoll *np) 700 { 701 rtnl_lock(); 702 if (!np->dev) 703 goto out; 704 do_netpoll_cleanup(np); 705 out: 706 rtnl_unlock(); 707 } 708 EXPORT_SYMBOL(netpoll_cleanup); 709