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 rcu_read_lock(); 293 npinfo = rcu_dereference_bh(dev->npinfo); 294 295 if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) { 296 dev_kfree_skb_irq(skb); 297 goto out; 298 } 299 300 /* don't get messages out of order, and no recursion */ 301 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) { 302 struct netdev_queue *txq; 303 304 txq = netdev_core_pick_tx(dev, skb, NULL); 305 306 /* try until next clock tick */ 307 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; 308 tries > 0; --tries) { 309 if (HARD_TX_TRYLOCK(dev, txq)) { 310 if (!netif_xmit_stopped(txq)) 311 status = netpoll_start_xmit(skb, dev, txq); 312 313 HARD_TX_UNLOCK(dev, txq); 314 315 if (dev_xmit_complete(status)) 316 break; 317 318 } 319 320 /* tickle device maybe there is some cleanup */ 321 netpoll_poll_dev(np->dev); 322 323 udelay(USEC_PER_POLL); 324 } 325 326 WARN_ONCE(!irqs_disabled(), 327 "netpoll_send_skb_on_dev(): %s enabled interrupts in poll (%pS)\n", 328 dev->name, dev->netdev_ops->ndo_start_xmit); 329 330 } 331 332 if (!dev_xmit_complete(status)) { 333 skb_queue_tail(&npinfo->txq, skb); 334 schedule_delayed_work(&npinfo->tx_work,0); 335 } 336 ret = NETDEV_TX_OK; 337 out: 338 rcu_read_unlock(); 339 return ret; 340 } 341 342 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb) 343 { 344 unsigned long flags; 345 netdev_tx_t ret; 346 347 if (unlikely(!np)) { 348 dev_kfree_skb_irq(skb); 349 ret = NET_XMIT_DROP; 350 } else { 351 local_irq_save(flags); 352 ret = __netpoll_send_skb(np, skb); 353 local_irq_restore(flags); 354 } 355 return ret; 356 } 357 EXPORT_SYMBOL(netpoll_send_skb); 358 359 static void skb_pool_flush(struct netpoll *np) 360 { 361 struct sk_buff_head *skb_pool; 362 363 cancel_work_sync(&np->refill_wq); 364 skb_pool = &np->skb_pool; 365 skb_queue_purge_reason(skb_pool, SKB_CONSUMED); 366 } 367 368 static void refill_skbs_work_handler(struct work_struct *work) 369 { 370 struct netpoll *np = 371 container_of(work, struct netpoll, refill_wq); 372 373 refill_skbs(np); 374 } 375 376 int __netpoll_setup(struct netpoll *np, struct net_device *ndev) 377 { 378 struct netpoll_info *npinfo; 379 const struct net_device_ops *ops; 380 int err; 381 382 skb_queue_head_init(&np->skb_pool); 383 INIT_WORK(&np->refill_wq, refill_skbs_work_handler); 384 385 if (ndev->priv_flags & IFF_DISABLE_NETPOLL) { 386 np_err(np, "%s doesn't support polling, aborting\n", 387 ndev->name); 388 err = -ENOTSUPP; 389 goto out; 390 } 391 392 npinfo = rtnl_dereference(ndev->npinfo); 393 if (!npinfo) { 394 npinfo = kmalloc_obj(*npinfo); 395 if (!npinfo) { 396 err = -ENOMEM; 397 goto out; 398 } 399 400 sema_init(&npinfo->dev_lock, 1); 401 skb_queue_head_init(&npinfo->txq); 402 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process); 403 404 refcount_set(&npinfo->refcnt, 1); 405 406 ops = ndev->netdev_ops; 407 if (ops->ndo_netpoll_setup) { 408 err = ops->ndo_netpoll_setup(ndev); 409 if (err) 410 goto free_npinfo; 411 } 412 } else { 413 refcount_inc(&npinfo->refcnt); 414 } 415 416 np->dev = ndev; 417 strscpy(np->dev_name, ndev->name, IFNAMSIZ); 418 419 /* fill up the skb queue */ 420 refill_skbs(np); 421 422 /* last thing to do is link it to the net device structure */ 423 rcu_assign_pointer(ndev->npinfo, npinfo); 424 425 return 0; 426 427 free_npinfo: 428 kfree(npinfo); 429 out: 430 return err; 431 } 432 EXPORT_SYMBOL_GPL(__netpoll_setup); 433 434 /* 435 * Returns a pointer to a string representation of the identifier used 436 * to select the egress interface for the given netpoll instance. buf 437 * is used to format np->dev_mac when np->dev_name is empty; bufsz must 438 * be at least MAC_ADDR_STR_LEN + 1 to fit the formatted MAC address 439 * and its NUL terminator. 440 */ 441 static char *egress_dev(struct netpoll *np, char *buf, size_t bufsz) 442 { 443 if (np->dev_name[0]) 444 return np->dev_name; 445 446 snprintf(buf, bufsz, "%pM", np->dev_mac); 447 return buf; 448 } 449 450 static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev, 451 unsigned int timeout) 452 { 453 unsigned long atmost; 454 455 atmost = jiffies + timeout * HZ; 456 while (!netif_carrier_ok(ndev)) { 457 if (time_after(jiffies, atmost)) { 458 np_notice(np, "timeout waiting for carrier\n"); 459 break; 460 } 461 msleep(1); 462 } 463 } 464 465 /* 466 * Take the IPv6 from ndev and populate local_ip structure in netpoll 467 */ 468 static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev) 469 { 470 char buf[MAC_ADDR_STR_LEN + 1]; 471 int err = -EDESTADDRREQ; 472 struct inet6_dev *idev; 473 474 if (!IS_ENABLED(CONFIG_IPV6)) { 475 np_err(np, "IPv6 is not supported %s, aborting\n", 476 egress_dev(np, buf, sizeof(buf))); 477 return -EINVAL; 478 } 479 480 idev = __in6_dev_get(ndev); 481 if (idev) { 482 struct inet6_ifaddr *ifp; 483 484 read_lock_bh(&idev->lock); 485 list_for_each_entry(ifp, &idev->addr_list, if_list) { 486 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) != 487 !!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL)) 488 continue; 489 /* Got the IP, let's return */ 490 np->local_ip.in6 = ifp->addr; 491 err = 0; 492 break; 493 } 494 read_unlock_bh(&idev->lock); 495 } 496 if (err) { 497 np_err(np, "no IPv6 address for %s, aborting\n", 498 egress_dev(np, buf, sizeof(buf))); 499 return err; 500 } 501 502 np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6); 503 return 0; 504 } 505 506 /* 507 * Take the IPv4 from ndev and populate local_ip structure in netpoll 508 */ 509 static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev) 510 { 511 char buf[MAC_ADDR_STR_LEN + 1]; 512 const struct in_ifaddr *ifa; 513 struct in_device *in_dev; 514 515 in_dev = __in_dev_get_rtnl(ndev); 516 if (!in_dev) { 517 np_err(np, "no IP address for %s, aborting\n", 518 egress_dev(np, buf, sizeof(buf))); 519 return -EDESTADDRREQ; 520 } 521 522 ifa = rtnl_dereference(in_dev->ifa_list); 523 if (!ifa) { 524 np_err(np, "no IP address for %s, aborting\n", 525 egress_dev(np, buf, sizeof(buf))); 526 return -EDESTADDRREQ; 527 } 528 529 np->local_ip.ip = ifa->ifa_local; 530 np_info(np, "local IP %pI4\n", &np->local_ip.ip); 531 532 return 0; 533 } 534 535 /* 536 * Test whether the caller left np->local_ip unset, so that 537 * netpoll_setup() should auto-populate it from the egress device. 538 * 539 * np->local_ip is a union of __be32 (IPv4) and struct in6_addr (IPv6), 540 * so an IPv6 address whose first 4 bytes are zero (e.g. ::1, ::2, 541 * IPv4-mapped ::ffff:a.b.c.d) must not be tested via the IPv4 arm — 542 * doing so would misclassify a caller-supplied address as unset and 543 * silently overwrite it with whatever address the device exposes. 544 */ 545 static bool netpoll_local_ip_unset(const struct netpoll *np) 546 { 547 if (np->ipv6) 548 return ipv6_addr_any(&np->local_ip.in6); 549 return !np->local_ip.ip; 550 } 551 552 int netpoll_setup(struct netpoll *np) 553 { 554 struct net *net = current->nsproxy->net_ns; 555 char buf[MAC_ADDR_STR_LEN + 1]; 556 struct net_device *ndev = NULL; 557 bool ip_overwritten = false; 558 int err; 559 560 rtnl_lock(); 561 if (np->dev_name[0]) 562 ndev = __dev_get_by_name(net, np->dev_name); 563 else if (is_valid_ether_addr(np->dev_mac)) 564 ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac); 565 566 if (!ndev) { 567 np_err(np, "%s doesn't exist, aborting\n", 568 egress_dev(np, buf, sizeof(buf))); 569 err = -ENODEV; 570 goto unlock; 571 } 572 netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL); 573 574 if (netdev_master_upper_dev_get(ndev)) { 575 np_err(np, "%s is a slave device, aborting\n", 576 egress_dev(np, buf, sizeof(buf))); 577 err = -EBUSY; 578 goto put; 579 } 580 581 if (!netif_running(ndev)) { 582 np_info(np, "device %s not up yet, forcing it\n", 583 egress_dev(np, buf, sizeof(buf))); 584 585 err = dev_open(ndev, NULL); 586 if (err) { 587 np_err(np, "failed to open %s\n", ndev->name); 588 goto put; 589 } 590 591 rtnl_unlock(); 592 netpoll_wait_carrier(np, ndev, carrier_timeout); 593 rtnl_lock(); 594 } 595 596 if (netpoll_local_ip_unset(np)) { 597 if (!np->ipv6) { 598 err = netpoll_take_ipv4(np, ndev); 599 if (err) 600 goto put; 601 } else { 602 err = netpoll_take_ipv6(np, ndev); 603 if (err) 604 goto put; 605 } 606 ip_overwritten = true; 607 } 608 609 err = __netpoll_setup(np, ndev); 610 if (err) 611 goto flush; 612 rtnl_unlock(); 613 614 /* Make sure all NAPI polls which started before dev->npinfo 615 * was visible have exited before we start calling NAPI poll. 616 * NAPI skips locking if dev->npinfo is NULL. 617 */ 618 synchronize_rcu(); 619 620 return 0; 621 622 flush: 623 skb_pool_flush(np); 624 put: 625 DEBUG_NET_WARN_ON_ONCE(np->dev); 626 if (ip_overwritten) 627 memset(&np->local_ip, 0, sizeof(np->local_ip)); 628 netdev_put(ndev, &np->dev_tracker); 629 unlock: 630 rtnl_unlock(); 631 return err; 632 } 633 EXPORT_SYMBOL(netpoll_setup); 634 635 static void rcu_cleanup_netpoll_info(struct rcu_head *rcu_head) 636 { 637 struct netpoll_info *npinfo = 638 container_of(rcu_head, struct netpoll_info, rcu); 639 640 skb_queue_purge(&npinfo->txq); 641 642 /* we can't call cancel_delayed_work_sync here, as we are in softirq */ 643 cancel_delayed_work(&npinfo->tx_work); 644 645 /* clean after last, unfinished work */ 646 __skb_queue_purge(&npinfo->txq); 647 /* now cancel it again */ 648 cancel_delayed_work(&npinfo->tx_work); 649 kfree(npinfo); 650 } 651 652 static void __netpoll_cleanup(struct netpoll *np) 653 { 654 struct netpoll_info *npinfo; 655 656 npinfo = rtnl_dereference(np->dev->npinfo); 657 if (!npinfo) 658 return; 659 660 /* At this point, there is a single npinfo instance per netdevice, and 661 * its refcnt tracks how many netpoll structures are linked to it. We 662 * only perform npinfo cleanup when the refcnt decrements to zero. 663 */ 664 if (refcount_dec_and_test(&npinfo->refcnt)) { 665 const struct net_device_ops *ops; 666 667 ops = np->dev->netdev_ops; 668 if (ops->ndo_netpoll_cleanup) 669 ops->ndo_netpoll_cleanup(np->dev); 670 671 RCU_INIT_POINTER(np->dev->npinfo, NULL); 672 call_rcu(&npinfo->rcu, rcu_cleanup_netpoll_info); 673 } 674 675 skb_pool_flush(np); 676 } 677 678 void __netpoll_free(struct netpoll *np) 679 { 680 ASSERT_RTNL(); 681 682 /* Wait for transmitting packets to finish before freeing. */ 683 synchronize_net(); 684 __netpoll_cleanup(np); 685 kfree(np); 686 } 687 EXPORT_SYMBOL_GPL(__netpoll_free); 688 689 void do_netpoll_cleanup(struct netpoll *np) 690 { 691 __netpoll_cleanup(np); 692 netdev_put(np->dev, &np->dev_tracker); 693 np->dev = NULL; 694 } 695 EXPORT_SYMBOL(do_netpoll_cleanup); 696 697 void netpoll_cleanup(struct netpoll *np) 698 { 699 rtnl_lock(); 700 if (!np->dev) 701 goto out; 702 do_netpoll_cleanup(np); 703 out: 704 rtnl_unlock(); 705 } 706 EXPORT_SYMBOL(netpoll_cleanup); 707