1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NET3 Protocol independent device support routines. 4 * 5 * Derived from the non IP parts of dev.c 1.0.19 6 * Authors: Ross Biro 7 * Fred N. van Kempen, <[email protected]> 8 * Mark Evans, <[email protected]> 9 * 10 * Additional Authors: 11 * Florian la Roche <[email protected]> 12 * Alan Cox <[email protected]> 13 * David Hinds <[email protected]> 14 * Alexey Kuznetsov <[email protected]> 15 * Adam Sulmicki <[email protected]> 16 * Pekka Riikonen <[email protected]> 17 * 18 * Changes: 19 * D.J. Barrow : Fixed bug where dev->refcnt gets set 20 * to 2 if register_netdev gets called 21 * before net_dev_init & also removed a 22 * few lines of code in the process. 23 * Alan Cox : device private ioctl copies fields back. 24 * Alan Cox : Transmit queue code does relevant 25 * stunts to keep the queue safe. 26 * Alan Cox : Fixed double lock. 27 * Alan Cox : Fixed promisc NULL pointer trap 28 * ???????? : Support the full private ioctl range 29 * Alan Cox : Moved ioctl permission check into 30 * drivers 31 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI 32 * Alan Cox : 100 backlog just doesn't cut it when 33 * you start doing multicast video 8) 34 * Alan Cox : Rewrote net_bh and list manager. 35 * Alan Cox : Fix ETH_P_ALL echoback lengths. 36 * Alan Cox : Took out transmit every packet pass 37 * Saved a few bytes in the ioctl handler 38 * Alan Cox : Network driver sets packet type before 39 * calling netif_rx. Saves a function 40 * call a packet. 41 * Alan Cox : Hashed net_bh() 42 * Richard Kooijman: Timestamp fixes. 43 * Alan Cox : Wrong field in SIOCGIFDSTADDR 44 * Alan Cox : Device lock protection. 45 * Alan Cox : Fixed nasty side effect of device close 46 * changes. 47 * Rudi Cilibrasi : Pass the right thing to 48 * set_mac_address() 49 * Dave Miller : 32bit quantity for the device lock to 50 * make it work out on a Sparc. 51 * Bjorn Ekwall : Added KERNELD hack. 52 * Alan Cox : Cleaned up the backlog initialise. 53 * Craig Metz : SIOCGIFCONF fix if space for under 54 * 1 device. 55 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there 56 * is no device open function. 57 * Andi Kleen : Fix error reporting for SIOCGIFCONF 58 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF 59 * Cyrus Durgin : Cleaned for KMOD 60 * Adam Sulmicki : Bug Fix : Network Device Unload 61 * A network device unload needs to purge 62 * the backlog queue. 63 * Paul Rusty Russell : SIOCSIFNAME 64 * Pekka Riikonen : Netdev boot-time settings code 65 * Andrew Morton : Make unregister_netdevice wait 66 * indefinitely on dev->refcnt 67 * J Hadi Salim : - Backlog queue sampling 68 * - netif_rx() feedback 69 */ 70 71 #include <linux/uaccess.h> 72 #include <linux/bitmap.h> 73 #include <linux/capability.h> 74 #include <linux/cpu.h> 75 #include <linux/types.h> 76 #include <linux/kernel.h> 77 #include <linux/hash.h> 78 #include <linux/slab.h> 79 #include <linux/sched.h> 80 #include <linux/sched/isolation.h> 81 #include <linux/sched/mm.h> 82 #include <linux/smpboot.h> 83 #include <linux/mutex.h> 84 #include <linux/rwsem.h> 85 #include <linux/string.h> 86 #include <linux/mm.h> 87 #include <linux/socket.h> 88 #include <linux/sockios.h> 89 #include <linux/errno.h> 90 #include <linux/interrupt.h> 91 #include <linux/if_ether.h> 92 #include <linux/netdevice.h> 93 #include <linux/etherdevice.h> 94 #include <linux/ethtool.h> 95 #include <linux/ethtool_netlink.h> 96 #include <linux/skbuff.h> 97 #include <linux/kthread.h> 98 #include <linux/bpf.h> 99 #include <linux/bpf_trace.h> 100 #include <net/net_namespace.h> 101 #include <net/sock.h> 102 #include <net/busy_poll.h> 103 #include <linux/rtnetlink.h> 104 #include <linux/stat.h> 105 #include <net/dsa.h> 106 #include <net/dst.h> 107 #include <net/dst_metadata.h> 108 #include <net/gro.h> 109 #include <net/netdev_queues.h> 110 #include <net/pkt_sched.h> 111 #include <net/pkt_cls.h> 112 #include <net/checksum.h> 113 #include <net/xfrm.h> 114 #include <net/tcx.h> 115 #include <linux/highmem.h> 116 #include <linux/init.h> 117 #include <linux/module.h> 118 #include <linux/netpoll.h> 119 #include <linux/rcupdate.h> 120 #include <linux/delay.h> 121 #include <net/iw_handler.h> 122 #include <asm/current.h> 123 #include <linux/audit.h> 124 #include <linux/dmaengine.h> 125 #include <linux/err.h> 126 #include <linux/ctype.h> 127 #include <linux/if_arp.h> 128 #include <linux/if_vlan.h> 129 #include <linux/ip.h> 130 #include <net/ip.h> 131 #include <net/mpls.h> 132 #include <linux/ipv6.h> 133 #include <linux/in.h> 134 #include <linux/jhash.h> 135 #include <linux/random.h> 136 #include <trace/events/napi.h> 137 #include <trace/events/net.h> 138 #include <trace/events/skb.h> 139 #include <trace/events/qdisc.h> 140 #include <trace/events/xdp.h> 141 #include <linux/inetdevice.h> 142 #include <linux/cpu_rmap.h> 143 #include <linux/static_key.h> 144 #include <linux/hashtable.h> 145 #include <linux/vmalloc.h> 146 #include <linux/if_macvlan.h> 147 #include <linux/errqueue.h> 148 #include <linux/hrtimer.h> 149 #include <linux/netfilter_netdev.h> 150 #include <linux/crash_dump.h> 151 #include <linux/sctp.h> 152 #include <net/udp_tunnel.h> 153 #include <linux/net_namespace.h> 154 #include <linux/indirect_call_wrapper.h> 155 #include <net/devlink.h> 156 #include <linux/pm_runtime.h> 157 #include <linux/prandom.h> 158 #include <linux/once_lite.h> 159 #include <net/netdev_lock.h> 160 #include <net/netdev_rx_queue.h> 161 #include <net/page_pool/types.h> 162 #include <net/page_pool/helpers.h> 163 #include <net/page_pool/memory_provider.h> 164 #include <net/rps.h> 165 #include <linux/phy_link_topology.h> 166 167 #include "dev.h" 168 #include "devmem.h" 169 #include "net-sysfs.h" 170 171 static DEFINE_SPINLOCK(ptype_lock); 172 struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; 173 174 static int netif_rx_internal(struct sk_buff *skb); 175 static int call_netdevice_notifiers_extack(unsigned long val, 176 struct net_device *dev, 177 struct netlink_ext_ack *extack); 178 179 static DEFINE_MUTEX(ifalias_mutex); 180 181 /* protects napi_hash addition/deletion and napi_gen_id */ 182 static DEFINE_SPINLOCK(napi_hash_lock); 183 184 static unsigned int napi_gen_id = NR_CPUS; 185 static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8); 186 187 static inline void dev_base_seq_inc(struct net *net) 188 { 189 unsigned int val = net->dev_base_seq + 1; 190 191 WRITE_ONCE(net->dev_base_seq, val ?: 1); 192 } 193 194 static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) 195 { 196 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ)); 197 198 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; 199 } 200 201 static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) 202 { 203 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; 204 } 205 206 #ifndef CONFIG_PREEMPT_RT 207 208 static DEFINE_STATIC_KEY_FALSE(use_backlog_threads_key); 209 210 static int __init setup_backlog_napi_threads(char *arg) 211 { 212 static_branch_enable(&use_backlog_threads_key); 213 return 0; 214 } 215 early_param("thread_backlog_napi", setup_backlog_napi_threads); 216 217 static bool use_backlog_threads(void) 218 { 219 return static_branch_unlikely(&use_backlog_threads_key); 220 } 221 222 #else 223 224 static bool use_backlog_threads(void) 225 { 226 return true; 227 } 228 229 #endif 230 231 static inline void backlog_lock_irq_save(struct softnet_data *sd, 232 unsigned long *flags) 233 { 234 if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads()) 235 spin_lock_irqsave(&sd->input_pkt_queue.lock, *flags); 236 else 237 local_irq_save(*flags); 238 } 239 240 static inline void backlog_lock_irq_disable(struct softnet_data *sd) 241 { 242 if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads()) 243 spin_lock_irq(&sd->input_pkt_queue.lock); 244 else 245 local_irq_disable(); 246 } 247 248 static inline void backlog_unlock_irq_restore(struct softnet_data *sd, 249 unsigned long *flags) 250 { 251 if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads()) 252 spin_unlock_irqrestore(&sd->input_pkt_queue.lock, *flags); 253 else 254 local_irq_restore(*flags); 255 } 256 257 static inline void backlog_unlock_irq_enable(struct softnet_data *sd) 258 { 259 if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads()) 260 spin_unlock_irq(&sd->input_pkt_queue.lock); 261 else 262 local_irq_enable(); 263 } 264 265 static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev, 266 const char *name) 267 { 268 struct netdev_name_node *name_node; 269 270 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL); 271 if (!name_node) 272 return NULL; 273 INIT_HLIST_NODE(&name_node->hlist); 274 name_node->dev = dev; 275 name_node->name = name; 276 return name_node; 277 } 278 279 static struct netdev_name_node * 280 netdev_name_node_head_alloc(struct net_device *dev) 281 { 282 struct netdev_name_node *name_node; 283 284 name_node = netdev_name_node_alloc(dev, dev->name); 285 if (!name_node) 286 return NULL; 287 INIT_LIST_HEAD(&name_node->list); 288 return name_node; 289 } 290 291 static void netdev_name_node_free(struct netdev_name_node *name_node) 292 { 293 kfree(name_node); 294 } 295 296 static void netdev_name_node_add(struct net *net, 297 struct netdev_name_node *name_node) 298 { 299 hlist_add_head_rcu(&name_node->hlist, 300 dev_name_hash(net, name_node->name)); 301 } 302 303 static void netdev_name_node_del(struct netdev_name_node *name_node) 304 { 305 hlist_del_rcu(&name_node->hlist); 306 } 307 308 static struct netdev_name_node *netdev_name_node_lookup(struct net *net, 309 const char *name) 310 { 311 struct hlist_head *head = dev_name_hash(net, name); 312 struct netdev_name_node *name_node; 313 314 hlist_for_each_entry(name_node, head, hlist) 315 if (!strcmp(name_node->name, name)) 316 return name_node; 317 return NULL; 318 } 319 320 static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net, 321 const char *name) 322 { 323 struct hlist_head *head = dev_name_hash(net, name); 324 struct netdev_name_node *name_node; 325 326 hlist_for_each_entry_rcu(name_node, head, hlist) 327 if (!strcmp(name_node->name, name)) 328 return name_node; 329 return NULL; 330 } 331 332 bool netdev_name_in_use(struct net *net, const char *name) 333 { 334 return netdev_name_node_lookup(net, name); 335 } 336 EXPORT_SYMBOL(netdev_name_in_use); 337 338 int netdev_name_node_alt_create(struct net_device *dev, const char *name) 339 { 340 struct netdev_name_node *name_node; 341 struct net *net = dev_net(dev); 342 343 name_node = netdev_name_node_lookup(net, name); 344 if (name_node) 345 return -EEXIST; 346 name_node = netdev_name_node_alloc(dev, name); 347 if (!name_node) 348 return -ENOMEM; 349 netdev_name_node_add(net, name_node); 350 /* The node that holds dev->name acts as a head of per-device list. */ 351 list_add_tail_rcu(&name_node->list, &dev->name_node->list); 352 353 return 0; 354 } 355 356 static void netdev_name_node_alt_free(struct rcu_head *head) 357 { 358 struct netdev_name_node *name_node = 359 container_of(head, struct netdev_name_node, rcu); 360 361 kfree(name_node->name); 362 netdev_name_node_free(name_node); 363 } 364 365 static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node) 366 { 367 netdev_name_node_del(name_node); 368 list_del(&name_node->list); 369 call_rcu(&name_node->rcu, netdev_name_node_alt_free); 370 } 371 372 int netdev_name_node_alt_destroy(struct net_device *dev, const char *name) 373 { 374 struct netdev_name_node *name_node; 375 struct net *net = dev_net(dev); 376 377 name_node = netdev_name_node_lookup(net, name); 378 if (!name_node) 379 return -ENOENT; 380 /* lookup might have found our primary name or a name belonging 381 * to another device. 382 */ 383 if (name_node == dev->name_node || name_node->dev != dev) 384 return -EINVAL; 385 386 __netdev_name_node_alt_destroy(name_node); 387 return 0; 388 } 389 390 static void netdev_name_node_alt_flush(struct net_device *dev) 391 { 392 struct netdev_name_node *name_node, *tmp; 393 394 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list) { 395 list_del(&name_node->list); 396 netdev_name_node_alt_free(&name_node->rcu); 397 } 398 } 399 400 /* Device list insertion */ 401 static void list_netdevice(struct net_device *dev) 402 { 403 struct netdev_name_node *name_node; 404 struct net *net = dev_net(dev); 405 406 ASSERT_RTNL(); 407 408 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); 409 netdev_name_node_add(net, dev->name_node); 410 hlist_add_head_rcu(&dev->index_hlist, 411 dev_index_hash(net, dev->ifindex)); 412 413 netdev_for_each_altname(dev, name_node) 414 netdev_name_node_add(net, name_node); 415 416 /* We reserved the ifindex, this can't fail */ 417 WARN_ON(xa_store(&net->dev_by_index, dev->ifindex, dev, GFP_KERNEL)); 418 419 dev_base_seq_inc(net); 420 } 421 422 /* Device list removal 423 * caller must respect a RCU grace period before freeing/reusing dev 424 */ 425 static void unlist_netdevice(struct net_device *dev) 426 { 427 struct netdev_name_node *name_node; 428 struct net *net = dev_net(dev); 429 430 ASSERT_RTNL(); 431 432 xa_erase(&net->dev_by_index, dev->ifindex); 433 434 netdev_for_each_altname(dev, name_node) 435 netdev_name_node_del(name_node); 436 437 /* Unlink dev from the device chain */ 438 list_del_rcu(&dev->dev_list); 439 netdev_name_node_del(dev->name_node); 440 hlist_del_rcu(&dev->index_hlist); 441 442 dev_base_seq_inc(dev_net(dev)); 443 } 444 445 /* 446 * Our notifier list 447 */ 448 449 static RAW_NOTIFIER_HEAD(netdev_chain); 450 451 /* 452 * Device drivers call our routines to queue packets here. We empty the 453 * queue in the local softnet handler. 454 */ 455 456 DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data) = { 457 .process_queue_bh_lock = INIT_LOCAL_LOCK(process_queue_bh_lock), 458 }; 459 EXPORT_PER_CPU_SYMBOL(softnet_data); 460 461 /* Page_pool has a lockless array/stack to alloc/recycle pages. 462 * PP consumers must pay attention to run APIs in the appropriate context 463 * (e.g. NAPI context). 464 */ 465 DEFINE_PER_CPU(struct page_pool *, system_page_pool); 466 467 #ifdef CONFIG_LOCKDEP 468 /* 469 * register_netdevice() inits txq->_xmit_lock and sets lockdep class 470 * according to dev->type 471 */ 472 static const unsigned short netdev_lock_type[] = { 473 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, 474 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, 475 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, 476 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, 477 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, 478 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, 479 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, 480 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, 481 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, 482 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, 483 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, 484 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, 485 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM, 486 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE, 487 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE}; 488 489 static const char *const netdev_lock_name[] = { 490 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", 491 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", 492 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", 493 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", 494 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", 495 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", 496 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", 497 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", 498 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", 499 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", 500 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", 501 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", 502 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM", 503 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE", 504 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"}; 505 506 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; 507 static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)]; 508 509 static inline unsigned short netdev_lock_pos(unsigned short dev_type) 510 { 511 int i; 512 513 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) 514 if (netdev_lock_type[i] == dev_type) 515 return i; 516 /* the last key is used by default */ 517 return ARRAY_SIZE(netdev_lock_type) - 1; 518 } 519 520 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, 521 unsigned short dev_type) 522 { 523 int i; 524 525 i = netdev_lock_pos(dev_type); 526 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], 527 netdev_lock_name[i]); 528 } 529 530 static inline void netdev_set_addr_lockdep_class(struct net_device *dev) 531 { 532 int i; 533 534 i = netdev_lock_pos(dev->type); 535 lockdep_set_class_and_name(&dev->addr_list_lock, 536 &netdev_addr_lock_key[i], 537 netdev_lock_name[i]); 538 } 539 #else 540 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, 541 unsigned short dev_type) 542 { 543 } 544 545 static inline void netdev_set_addr_lockdep_class(struct net_device *dev) 546 { 547 } 548 #endif 549 550 /******************************************************************************* 551 * 552 * Protocol management and registration routines 553 * 554 *******************************************************************************/ 555 556 557 /* 558 * Add a protocol ID to the list. Now that the input handler is 559 * smarter we can dispense with all the messy stuff that used to be 560 * here. 561 * 562 * BEWARE!!! Protocol handlers, mangling input packets, 563 * MUST BE last in hash buckets and checking protocol handlers 564 * MUST start from promiscuous ptype_all chain in net_bh. 565 * It is true now, do not change it. 566 * Explanation follows: if protocol handler, mangling packet, will 567 * be the first on list, it is not able to sense, that packet 568 * is cloned and should be copied-on-write, so that it will 569 * change it and subsequent readers will get broken packet. 570 * --ANK (980803) 571 */ 572 573 static inline struct list_head *ptype_head(const struct packet_type *pt) 574 { 575 if (pt->type == htons(ETH_P_ALL)) { 576 if (!pt->af_packet_net && !pt->dev) 577 return NULL; 578 579 return pt->dev ? &pt->dev->ptype_all : 580 &pt->af_packet_net->ptype_all; 581 } 582 583 if (pt->dev) 584 return &pt->dev->ptype_specific; 585 586 return pt->af_packet_net ? &pt->af_packet_net->ptype_specific : 587 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; 588 } 589 590 /** 591 * dev_add_pack - add packet handler 592 * @pt: packet type declaration 593 * 594 * Add a protocol handler to the networking stack. The passed &packet_type 595 * is linked into kernel lists and may not be freed until it has been 596 * removed from the kernel lists. 597 * 598 * This call does not sleep therefore it can not 599 * guarantee all CPU's that are in middle of receiving packets 600 * will see the new packet type (until the next received packet). 601 */ 602 603 void dev_add_pack(struct packet_type *pt) 604 { 605 struct list_head *head = ptype_head(pt); 606 607 if (WARN_ON_ONCE(!head)) 608 return; 609 610 spin_lock(&ptype_lock); 611 list_add_rcu(&pt->list, head); 612 spin_unlock(&ptype_lock); 613 } 614 EXPORT_SYMBOL(dev_add_pack); 615 616 /** 617 * __dev_remove_pack - remove packet handler 618 * @pt: packet type declaration 619 * 620 * Remove a protocol handler that was previously added to the kernel 621 * protocol handlers by dev_add_pack(). The passed &packet_type is removed 622 * from the kernel lists and can be freed or reused once this function 623 * returns. 624 * 625 * The packet type might still be in use by receivers 626 * and must not be freed until after all the CPU's have gone 627 * through a quiescent state. 628 */ 629 void __dev_remove_pack(struct packet_type *pt) 630 { 631 struct list_head *head = ptype_head(pt); 632 struct packet_type *pt1; 633 634 if (!head) 635 return; 636 637 spin_lock(&ptype_lock); 638 639 list_for_each_entry(pt1, head, list) { 640 if (pt == pt1) { 641 list_del_rcu(&pt->list); 642 goto out; 643 } 644 } 645 646 pr_warn("dev_remove_pack: %p not found\n", pt); 647 out: 648 spin_unlock(&ptype_lock); 649 } 650 EXPORT_SYMBOL(__dev_remove_pack); 651 652 /** 653 * dev_remove_pack - remove packet handler 654 * @pt: packet type declaration 655 * 656 * Remove a protocol handler that was previously added to the kernel 657 * protocol handlers by dev_add_pack(). The passed &packet_type is removed 658 * from the kernel lists and can be freed or reused once this function 659 * returns. 660 * 661 * This call sleeps to guarantee that no CPU is looking at the packet 662 * type after return. 663 */ 664 void dev_remove_pack(struct packet_type *pt) 665 { 666 __dev_remove_pack(pt); 667 668 synchronize_net(); 669 } 670 EXPORT_SYMBOL(dev_remove_pack); 671 672 673 /******************************************************************************* 674 * 675 * Device Interface Subroutines 676 * 677 *******************************************************************************/ 678 679 /** 680 * dev_get_iflink - get 'iflink' value of a interface 681 * @dev: targeted interface 682 * 683 * Indicates the ifindex the interface is linked to. 684 * Physical interfaces have the same 'ifindex' and 'iflink' values. 685 */ 686 687 int dev_get_iflink(const struct net_device *dev) 688 { 689 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink) 690 return dev->netdev_ops->ndo_get_iflink(dev); 691 692 return READ_ONCE(dev->ifindex); 693 } 694 EXPORT_SYMBOL(dev_get_iflink); 695 696 /** 697 * dev_fill_metadata_dst - Retrieve tunnel egress information. 698 * @dev: targeted interface 699 * @skb: The packet. 700 * 701 * For better visibility of tunnel traffic OVS needs to retrieve 702 * egress tunnel information for a packet. Following API allows 703 * user to get this info. 704 */ 705 int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) 706 { 707 struct ip_tunnel_info *info; 708 709 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst) 710 return -EINVAL; 711 712 info = skb_tunnel_info_unclone(skb); 713 if (!info) 714 return -ENOMEM; 715 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX))) 716 return -EINVAL; 717 718 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb); 719 } 720 EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); 721 722 static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack) 723 { 724 int k = stack->num_paths++; 725 726 if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX)) 727 return NULL; 728 729 return &stack->path[k]; 730 } 731 732 int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr, 733 struct net_device_path_stack *stack) 734 { 735 const struct net_device *last_dev; 736 struct net_device_path_ctx ctx = { 737 .dev = dev, 738 }; 739 struct net_device_path *path; 740 int ret = 0; 741 742 memcpy(ctx.daddr, daddr, sizeof(ctx.daddr)); 743 stack->num_paths = 0; 744 while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) { 745 last_dev = ctx.dev; 746 path = dev_fwd_path(stack); 747 if (!path) 748 return -1; 749 750 memset(path, 0, sizeof(struct net_device_path)); 751 ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path); 752 if (ret < 0) 753 return -1; 754 755 if (WARN_ON_ONCE(last_dev == ctx.dev)) 756 return -1; 757 } 758 759 if (!ctx.dev) 760 return ret; 761 762 path = dev_fwd_path(stack); 763 if (!path) 764 return -1; 765 path->type = DEV_PATH_ETHERNET; 766 path->dev = ctx.dev; 767 768 return ret; 769 } 770 EXPORT_SYMBOL_GPL(dev_fill_forward_path); 771 772 /* must be called under rcu_read_lock(), as we dont take a reference */ 773 static struct napi_struct *napi_by_id(unsigned int napi_id) 774 { 775 unsigned int hash = napi_id % HASH_SIZE(napi_hash); 776 struct napi_struct *napi; 777 778 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) 779 if (napi->napi_id == napi_id) 780 return napi; 781 782 return NULL; 783 } 784 785 /* must be called under rcu_read_lock(), as we dont take a reference */ 786 static struct napi_struct * 787 netdev_napi_by_id(struct net *net, unsigned int napi_id) 788 { 789 struct napi_struct *napi; 790 791 napi = napi_by_id(napi_id); 792 if (!napi) 793 return NULL; 794 795 if (WARN_ON_ONCE(!napi->dev)) 796 return NULL; 797 if (!net_eq(net, dev_net(napi->dev))) 798 return NULL; 799 800 return napi; 801 } 802 803 /** 804 * netdev_napi_by_id_lock() - find a device by NAPI ID and lock it 805 * @net: the applicable net namespace 806 * @napi_id: ID of a NAPI of a target device 807 * 808 * Find a NAPI instance with @napi_id. Lock its device. 809 * The device must be in %NETREG_REGISTERED state for lookup to succeed. 810 * netdev_unlock() must be called to release it. 811 * 812 * Return: pointer to NAPI, its device with lock held, NULL if not found. 813 */ 814 struct napi_struct * 815 netdev_napi_by_id_lock(struct net *net, unsigned int napi_id) 816 { 817 struct napi_struct *napi; 818 struct net_device *dev; 819 820 rcu_read_lock(); 821 napi = netdev_napi_by_id(net, napi_id); 822 if (!napi || READ_ONCE(napi->dev->reg_state) != NETREG_REGISTERED) { 823 rcu_read_unlock(); 824 return NULL; 825 } 826 827 dev = napi->dev; 828 dev_hold(dev); 829 rcu_read_unlock(); 830 831 dev = __netdev_put_lock(dev); 832 if (!dev) 833 return NULL; 834 835 rcu_read_lock(); 836 napi = netdev_napi_by_id(net, napi_id); 837 if (napi && napi->dev != dev) 838 napi = NULL; 839 rcu_read_unlock(); 840 841 if (!napi) 842 netdev_unlock(dev); 843 return napi; 844 } 845 846 /** 847 * __dev_get_by_name - find a device by its name 848 * @net: the applicable net namespace 849 * @name: name to find 850 * 851 * Find an interface by name. Must be called under RTNL semaphore. 852 * If the name is found a pointer to the device is returned. 853 * If the name is not found then %NULL is returned. The 854 * reference counters are not incremented so the caller must be 855 * careful with locks. 856 */ 857 858 struct net_device *__dev_get_by_name(struct net *net, const char *name) 859 { 860 struct netdev_name_node *node_name; 861 862 node_name = netdev_name_node_lookup(net, name); 863 return node_name ? node_name->dev : NULL; 864 } 865 EXPORT_SYMBOL(__dev_get_by_name); 866 867 /** 868 * dev_get_by_name_rcu - find a device by its name 869 * @net: the applicable net namespace 870 * @name: name to find 871 * 872 * Find an interface by name. 873 * If the name is found a pointer to the device is returned. 874 * If the name is not found then %NULL is returned. 875 * The reference counters are not incremented so the caller must be 876 * careful with locks. The caller must hold RCU lock. 877 */ 878 879 struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) 880 { 881 struct netdev_name_node *node_name; 882 883 node_name = netdev_name_node_lookup_rcu(net, name); 884 return node_name ? node_name->dev : NULL; 885 } 886 EXPORT_SYMBOL(dev_get_by_name_rcu); 887 888 /* Deprecated for new users, call netdev_get_by_name() instead */ 889 struct net_device *dev_get_by_name(struct net *net, const char *name) 890 { 891 struct net_device *dev; 892 893 rcu_read_lock(); 894 dev = dev_get_by_name_rcu(net, name); 895 dev_hold(dev); 896 rcu_read_unlock(); 897 return dev; 898 } 899 EXPORT_SYMBOL(dev_get_by_name); 900 901 /** 902 * netdev_get_by_name() - find a device by its name 903 * @net: the applicable net namespace 904 * @name: name to find 905 * @tracker: tracking object for the acquired reference 906 * @gfp: allocation flags for the tracker 907 * 908 * Find an interface by name. This can be called from any 909 * context and does its own locking. The returned handle has 910 * the usage count incremented and the caller must use netdev_put() to 911 * release it when it is no longer needed. %NULL is returned if no 912 * matching device is found. 913 */ 914 struct net_device *netdev_get_by_name(struct net *net, const char *name, 915 netdevice_tracker *tracker, gfp_t gfp) 916 { 917 struct net_device *dev; 918 919 dev = dev_get_by_name(net, name); 920 if (dev) 921 netdev_tracker_alloc(dev, tracker, gfp); 922 return dev; 923 } 924 EXPORT_SYMBOL(netdev_get_by_name); 925 926 /** 927 * __dev_get_by_index - find a device by its ifindex 928 * @net: the applicable net namespace 929 * @ifindex: index of device 930 * 931 * Search for an interface by index. Returns %NULL if the device 932 * is not found or a pointer to the device. The device has not 933 * had its reference counter increased so the caller must be careful 934 * about locking. The caller must hold the RTNL semaphore. 935 */ 936 937 struct net_device *__dev_get_by_index(struct net *net, int ifindex) 938 { 939 struct net_device *dev; 940 struct hlist_head *head = dev_index_hash(net, ifindex); 941 942 hlist_for_each_entry(dev, head, index_hlist) 943 if (dev->ifindex == ifindex) 944 return dev; 945 946 return NULL; 947 } 948 EXPORT_SYMBOL(__dev_get_by_index); 949 950 /** 951 * dev_get_by_index_rcu - find a device by its ifindex 952 * @net: the applicable net namespace 953 * @ifindex: index of device 954 * 955 * Search for an interface by index. Returns %NULL if the device 956 * is not found or a pointer to the device. The device has not 957 * had its reference counter increased so the caller must be careful 958 * about locking. The caller must hold RCU lock. 959 */ 960 961 struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) 962 { 963 struct net_device *dev; 964 struct hlist_head *head = dev_index_hash(net, ifindex); 965 966 hlist_for_each_entry_rcu(dev, head, index_hlist) 967 if (dev->ifindex == ifindex) 968 return dev; 969 970 return NULL; 971 } 972 EXPORT_SYMBOL(dev_get_by_index_rcu); 973 974 /* Deprecated for new users, call netdev_get_by_index() instead */ 975 struct net_device *dev_get_by_index(struct net *net, int ifindex) 976 { 977 struct net_device *dev; 978 979 rcu_read_lock(); 980 dev = dev_get_by_index_rcu(net, ifindex); 981 dev_hold(dev); 982 rcu_read_unlock(); 983 return dev; 984 } 985 EXPORT_SYMBOL(dev_get_by_index); 986 987 /** 988 * netdev_get_by_index() - find a device by its ifindex 989 * @net: the applicable net namespace 990 * @ifindex: index of device 991 * @tracker: tracking object for the acquired reference 992 * @gfp: allocation flags for the tracker 993 * 994 * Search for an interface by index. Returns NULL if the device 995 * is not found or a pointer to the device. The device returned has 996 * had a reference added and the pointer is safe until the user calls 997 * netdev_put() to indicate they have finished with it. 998 */ 999 struct net_device *netdev_get_by_index(struct net *net, int ifindex, 1000 netdevice_tracker *tracker, gfp_t gfp) 1001 { 1002 struct net_device *dev; 1003 1004 dev = dev_get_by_index(net, ifindex); 1005 if (dev) 1006 netdev_tracker_alloc(dev, tracker, gfp); 1007 return dev; 1008 } 1009 EXPORT_SYMBOL(netdev_get_by_index); 1010 1011 /** 1012 * dev_get_by_napi_id - find a device by napi_id 1013 * @napi_id: ID of the NAPI struct 1014 * 1015 * Search for an interface by NAPI ID. Returns %NULL if the device 1016 * is not found or a pointer to the device. The device has not had 1017 * its reference counter increased so the caller must be careful 1018 * about locking. The caller must hold RCU lock. 1019 */ 1020 struct net_device *dev_get_by_napi_id(unsigned int napi_id) 1021 { 1022 struct napi_struct *napi; 1023 1024 WARN_ON_ONCE(!rcu_read_lock_held()); 1025 1026 if (!napi_id_valid(napi_id)) 1027 return NULL; 1028 1029 napi = napi_by_id(napi_id); 1030 1031 return napi ? napi->dev : NULL; 1032 } 1033 1034 /* Release the held reference on the net_device, and if the net_device 1035 * is still registered try to lock the instance lock. If device is being 1036 * unregistered NULL will be returned (but the reference has been released, 1037 * either way!) 1038 * 1039 * This helper is intended for locking net_device after it has been looked up 1040 * using a lockless lookup helper. Lock prevents the instance from going away. 1041 */ 1042 struct net_device *__netdev_put_lock(struct net_device *dev) 1043 { 1044 netdev_lock(dev); 1045 if (dev->reg_state > NETREG_REGISTERED) { 1046 netdev_unlock(dev); 1047 dev_put(dev); 1048 return NULL; 1049 } 1050 dev_put(dev); 1051 return dev; 1052 } 1053 1054 /** 1055 * netdev_get_by_index_lock() - find a device by its ifindex 1056 * @net: the applicable net namespace 1057 * @ifindex: index of device 1058 * 1059 * Search for an interface by index. If a valid device 1060 * with @ifindex is found it will be returned with netdev->lock held. 1061 * netdev_unlock() must be called to release it. 1062 * 1063 * Return: pointer to a device with lock held, NULL if not found. 1064 */ 1065 struct net_device *netdev_get_by_index_lock(struct net *net, int ifindex) 1066 { 1067 struct net_device *dev; 1068 1069 dev = dev_get_by_index(net, ifindex); 1070 if (!dev) 1071 return NULL; 1072 1073 return __netdev_put_lock(dev); 1074 } 1075 1076 struct net_device * 1077 netdev_xa_find_lock(struct net *net, struct net_device *dev, 1078 unsigned long *index) 1079 { 1080 if (dev) 1081 netdev_unlock(dev); 1082 1083 do { 1084 rcu_read_lock(); 1085 dev = xa_find(&net->dev_by_index, index, ULONG_MAX, XA_PRESENT); 1086 if (!dev) { 1087 rcu_read_unlock(); 1088 return NULL; 1089 } 1090 dev_hold(dev); 1091 rcu_read_unlock(); 1092 1093 dev = __netdev_put_lock(dev); 1094 if (dev) 1095 return dev; 1096 1097 (*index)++; 1098 } while (true); 1099 } 1100 1101 static DEFINE_SEQLOCK(netdev_rename_lock); 1102 1103 void netdev_copy_name(struct net_device *dev, char *name) 1104 { 1105 unsigned int seq; 1106 1107 do { 1108 seq = read_seqbegin(&netdev_rename_lock); 1109 strscpy(name, dev->name, IFNAMSIZ); 1110 } while (read_seqretry(&netdev_rename_lock, seq)); 1111 } 1112 1113 /** 1114 * netdev_get_name - get a netdevice name, knowing its ifindex. 1115 * @net: network namespace 1116 * @name: a pointer to the buffer where the name will be stored. 1117 * @ifindex: the ifindex of the interface to get the name from. 1118 */ 1119 int netdev_get_name(struct net *net, char *name, int ifindex) 1120 { 1121 struct net_device *dev; 1122 int ret; 1123 1124 rcu_read_lock(); 1125 1126 dev = dev_get_by_index_rcu(net, ifindex); 1127 if (!dev) { 1128 ret = -ENODEV; 1129 goto out; 1130 } 1131 1132 netdev_copy_name(dev, name); 1133 1134 ret = 0; 1135 out: 1136 rcu_read_unlock(); 1137 return ret; 1138 } 1139 1140 static bool dev_addr_cmp(struct net_device *dev, unsigned short type, 1141 const char *ha) 1142 { 1143 return dev->type == type && !memcmp(dev->dev_addr, ha, dev->addr_len); 1144 } 1145 1146 /** 1147 * dev_getbyhwaddr_rcu - find a device by its hardware address 1148 * @net: the applicable net namespace 1149 * @type: media type of device 1150 * @ha: hardware address 1151 * 1152 * Search for an interface by MAC address. Returns NULL if the device 1153 * is not found or a pointer to the device. 1154 * The caller must hold RCU. 1155 * The returned device has not had its ref count increased 1156 * and the caller must therefore be careful about locking 1157 * 1158 */ 1159 1160 struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, 1161 const char *ha) 1162 { 1163 struct net_device *dev; 1164 1165 for_each_netdev_rcu(net, dev) 1166 if (dev_addr_cmp(dev, type, ha)) 1167 return dev; 1168 1169 return NULL; 1170 } 1171 EXPORT_SYMBOL(dev_getbyhwaddr_rcu); 1172 1173 /** 1174 * dev_getbyhwaddr() - find a device by its hardware address 1175 * @net: the applicable net namespace 1176 * @type: media type of device 1177 * @ha: hardware address 1178 * 1179 * Similar to dev_getbyhwaddr_rcu(), but the owner needs to hold 1180 * rtnl_lock. 1181 * 1182 * Context: rtnl_lock() must be held. 1183 * Return: pointer to the net_device, or NULL if not found 1184 */ 1185 struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type, 1186 const char *ha) 1187 { 1188 struct net_device *dev; 1189 1190 ASSERT_RTNL(); 1191 for_each_netdev(net, dev) 1192 if (dev_addr_cmp(dev, type, ha)) 1193 return dev; 1194 1195 return NULL; 1196 } 1197 EXPORT_SYMBOL(dev_getbyhwaddr); 1198 1199 struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) 1200 { 1201 struct net_device *dev, *ret = NULL; 1202 1203 rcu_read_lock(); 1204 for_each_netdev_rcu(net, dev) 1205 if (dev->type == type) { 1206 dev_hold(dev); 1207 ret = dev; 1208 break; 1209 } 1210 rcu_read_unlock(); 1211 return ret; 1212 } 1213 EXPORT_SYMBOL(dev_getfirstbyhwtype); 1214 1215 /** 1216 * __dev_get_by_flags - find any device with given flags 1217 * @net: the applicable net namespace 1218 * @if_flags: IFF_* values 1219 * @mask: bitmask of bits in if_flags to check 1220 * 1221 * Search for any interface with the given flags. Returns NULL if a device 1222 * is not found or a pointer to the device. Must be called inside 1223 * rtnl_lock(), and result refcount is unchanged. 1224 */ 1225 1226 struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags, 1227 unsigned short mask) 1228 { 1229 struct net_device *dev, *ret; 1230 1231 ASSERT_RTNL(); 1232 1233 ret = NULL; 1234 for_each_netdev(net, dev) { 1235 if (((dev->flags ^ if_flags) & mask) == 0) { 1236 ret = dev; 1237 break; 1238 } 1239 } 1240 return ret; 1241 } 1242 EXPORT_SYMBOL(__dev_get_by_flags); 1243 1244 /** 1245 * dev_valid_name - check if name is okay for network device 1246 * @name: name string 1247 * 1248 * Network device names need to be valid file names to 1249 * allow sysfs to work. We also disallow any kind of 1250 * whitespace. 1251 */ 1252 bool dev_valid_name(const char *name) 1253 { 1254 if (*name == '\0') 1255 return false; 1256 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) 1257 return false; 1258 if (!strcmp(name, ".") || !strcmp(name, "..")) 1259 return false; 1260 1261 while (*name) { 1262 if (*name == '/' || *name == ':' || isspace(*name)) 1263 return false; 1264 name++; 1265 } 1266 return true; 1267 } 1268 EXPORT_SYMBOL(dev_valid_name); 1269 1270 /** 1271 * __dev_alloc_name - allocate a name for a device 1272 * @net: network namespace to allocate the device name in 1273 * @name: name format string 1274 * @res: result name string 1275 * 1276 * Passed a format string - eg "lt%d" it will try and find a suitable 1277 * id. It scans list of devices to build up a free map, then chooses 1278 * the first empty slot. The caller must hold the dev_base or rtnl lock 1279 * while allocating the name and adding the device in order to avoid 1280 * duplicates. 1281 * Limited to bits_per_byte * page size devices (ie 32K on most platforms). 1282 * Returns the number of the unit assigned or a negative errno code. 1283 */ 1284 1285 static int __dev_alloc_name(struct net *net, const char *name, char *res) 1286 { 1287 int i = 0; 1288 const char *p; 1289 const int max_netdevices = 8*PAGE_SIZE; 1290 unsigned long *inuse; 1291 struct net_device *d; 1292 char buf[IFNAMSIZ]; 1293 1294 /* Verify the string as this thing may have come from the user. 1295 * There must be one "%d" and no other "%" characters. 1296 */ 1297 p = strchr(name, '%'); 1298 if (!p || p[1] != 'd' || strchr(p + 2, '%')) 1299 return -EINVAL; 1300 1301 /* Use one page as a bit array of possible slots */ 1302 inuse = bitmap_zalloc(max_netdevices, GFP_ATOMIC); 1303 if (!inuse) 1304 return -ENOMEM; 1305 1306 for_each_netdev(net, d) { 1307 struct netdev_name_node *name_node; 1308 1309 netdev_for_each_altname(d, name_node) { 1310 if (!sscanf(name_node->name, name, &i)) 1311 continue; 1312 if (i < 0 || i >= max_netdevices) 1313 continue; 1314 1315 /* avoid cases where sscanf is not exact inverse of printf */ 1316 snprintf(buf, IFNAMSIZ, name, i); 1317 if (!strncmp(buf, name_node->name, IFNAMSIZ)) 1318 __set_bit(i, inuse); 1319 } 1320 if (!sscanf(d->name, name, &i)) 1321 continue; 1322 if (i < 0 || i >= max_netdevices) 1323 continue; 1324 1325 /* avoid cases where sscanf is not exact inverse of printf */ 1326 snprintf(buf, IFNAMSIZ, name, i); 1327 if (!strncmp(buf, d->name, IFNAMSIZ)) 1328 __set_bit(i, inuse); 1329 } 1330 1331 i = find_first_zero_bit(inuse, max_netdevices); 1332 bitmap_free(inuse); 1333 if (i == max_netdevices) 1334 return -ENFILE; 1335 1336 /* 'res' and 'name' could overlap, use 'buf' as an intermediate buffer */ 1337 strscpy(buf, name, IFNAMSIZ); 1338 snprintf(res, IFNAMSIZ, buf, i); 1339 return i; 1340 } 1341 1342 /* Returns negative errno or allocated unit id (see __dev_alloc_name()) */ 1343 static int dev_prep_valid_name(struct net *net, struct net_device *dev, 1344 const char *want_name, char *out_name, 1345 int dup_errno) 1346 { 1347 if (!dev_valid_name(want_name)) 1348 return -EINVAL; 1349 1350 if (strchr(want_name, '%')) 1351 return __dev_alloc_name(net, want_name, out_name); 1352 1353 if (netdev_name_in_use(net, want_name)) 1354 return -dup_errno; 1355 if (out_name != want_name) 1356 strscpy(out_name, want_name, IFNAMSIZ); 1357 return 0; 1358 } 1359 1360 /** 1361 * dev_alloc_name - allocate a name for a device 1362 * @dev: device 1363 * @name: name format string 1364 * 1365 * Passed a format string - eg "lt%d" it will try and find a suitable 1366 * id. It scans list of devices to build up a free map, then chooses 1367 * the first empty slot. The caller must hold the dev_base or rtnl lock 1368 * while allocating the name and adding the device in order to avoid 1369 * duplicates. 1370 * Limited to bits_per_byte * page size devices (ie 32K on most platforms). 1371 * Returns the number of the unit assigned or a negative errno code. 1372 */ 1373 1374 int dev_alloc_name(struct net_device *dev, const char *name) 1375 { 1376 return dev_prep_valid_name(dev_net(dev), dev, name, dev->name, ENFILE); 1377 } 1378 EXPORT_SYMBOL(dev_alloc_name); 1379 1380 static int dev_get_valid_name(struct net *net, struct net_device *dev, 1381 const char *name) 1382 { 1383 int ret; 1384 1385 ret = dev_prep_valid_name(net, dev, name, dev->name, EEXIST); 1386 return ret < 0 ? ret : 0; 1387 } 1388 1389 int netif_change_name(struct net_device *dev, const char *newname) 1390 { 1391 struct net *net = dev_net(dev); 1392 unsigned char old_assign_type; 1393 char oldname[IFNAMSIZ]; 1394 int err = 0; 1395 int ret; 1396 1397 ASSERT_RTNL_NET(net); 1398 1399 if (!strncmp(newname, dev->name, IFNAMSIZ)) 1400 return 0; 1401 1402 memcpy(oldname, dev->name, IFNAMSIZ); 1403 1404 write_seqlock_bh(&netdev_rename_lock); 1405 err = dev_get_valid_name(net, dev, newname); 1406 write_sequnlock_bh(&netdev_rename_lock); 1407 1408 if (err < 0) 1409 return err; 1410 1411 if (oldname[0] && !strchr(oldname, '%')) 1412 netdev_info(dev, "renamed from %s%s\n", oldname, 1413 dev->flags & IFF_UP ? " (while UP)" : ""); 1414 1415 old_assign_type = dev->name_assign_type; 1416 WRITE_ONCE(dev->name_assign_type, NET_NAME_RENAMED); 1417 1418 rollback: 1419 ret = device_rename(&dev->dev, dev->name); 1420 if (ret) { 1421 write_seqlock_bh(&netdev_rename_lock); 1422 memcpy(dev->name, oldname, IFNAMSIZ); 1423 write_sequnlock_bh(&netdev_rename_lock); 1424 WRITE_ONCE(dev->name_assign_type, old_assign_type); 1425 return ret; 1426 } 1427 1428 netdev_adjacent_rename_links(dev, oldname); 1429 1430 netdev_name_node_del(dev->name_node); 1431 1432 synchronize_net(); 1433 1434 netdev_name_node_add(net, dev->name_node); 1435 1436 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); 1437 ret = notifier_to_errno(ret); 1438 1439 if (ret) { 1440 /* err >= 0 after dev_alloc_name() or stores the first errno */ 1441 if (err >= 0) { 1442 err = ret; 1443 write_seqlock_bh(&netdev_rename_lock); 1444 memcpy(dev->name, oldname, IFNAMSIZ); 1445 write_sequnlock_bh(&netdev_rename_lock); 1446 memcpy(oldname, newname, IFNAMSIZ); 1447 WRITE_ONCE(dev->name_assign_type, old_assign_type); 1448 old_assign_type = NET_NAME_RENAMED; 1449 goto rollback; 1450 } else { 1451 netdev_err(dev, "name change rollback failed: %d\n", 1452 ret); 1453 } 1454 } 1455 1456 return err; 1457 } 1458 1459 int netif_set_alias(struct net_device *dev, const char *alias, size_t len) 1460 { 1461 struct dev_ifalias *new_alias = NULL; 1462 1463 if (len >= IFALIASZ) 1464 return -EINVAL; 1465 1466 if (len) { 1467 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL); 1468 if (!new_alias) 1469 return -ENOMEM; 1470 1471 memcpy(new_alias->ifalias, alias, len); 1472 new_alias->ifalias[len] = 0; 1473 } 1474 1475 mutex_lock(&ifalias_mutex); 1476 new_alias = rcu_replace_pointer(dev->ifalias, new_alias, 1477 mutex_is_locked(&ifalias_mutex)); 1478 mutex_unlock(&ifalias_mutex); 1479 1480 if (new_alias) 1481 kfree_rcu(new_alias, rcuhead); 1482 1483 return len; 1484 } 1485 1486 /** 1487 * dev_get_alias - get ifalias of a device 1488 * @dev: device 1489 * @name: buffer to store name of ifalias 1490 * @len: size of buffer 1491 * 1492 * get ifalias for a device. Caller must make sure dev cannot go 1493 * away, e.g. rcu read lock or own a reference count to device. 1494 */ 1495 int dev_get_alias(const struct net_device *dev, char *name, size_t len) 1496 { 1497 const struct dev_ifalias *alias; 1498 int ret = 0; 1499 1500 rcu_read_lock(); 1501 alias = rcu_dereference(dev->ifalias); 1502 if (alias) 1503 ret = snprintf(name, len, "%s", alias->ifalias); 1504 rcu_read_unlock(); 1505 1506 return ret; 1507 } 1508 1509 /** 1510 * netdev_features_change - device changes features 1511 * @dev: device to cause notification 1512 * 1513 * Called to indicate a device has changed features. 1514 */ 1515 void netdev_features_change(struct net_device *dev) 1516 { 1517 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); 1518 } 1519 EXPORT_SYMBOL(netdev_features_change); 1520 1521 /** 1522 * netdev_state_change - device changes state 1523 * @dev: device to cause notification 1524 * 1525 * Called to indicate a device has changed state. This function calls 1526 * the notifier chains for netdev_chain and sends a NEWLINK message 1527 * to the routing socket. 1528 */ 1529 void netdev_state_change(struct net_device *dev) 1530 { 1531 if (dev->flags & IFF_UP) { 1532 struct netdev_notifier_change_info change_info = { 1533 .info.dev = dev, 1534 }; 1535 1536 call_netdevice_notifiers_info(NETDEV_CHANGE, 1537 &change_info.info); 1538 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL, 0, NULL); 1539 } 1540 } 1541 EXPORT_SYMBOL(netdev_state_change); 1542 1543 /** 1544 * __netdev_notify_peers - notify network peers about existence of @dev, 1545 * to be called when rtnl lock is already held. 1546 * @dev: network device 1547 * 1548 * Generate traffic such that interested network peers are aware of 1549 * @dev, such as by generating a gratuitous ARP. This may be used when 1550 * a device wants to inform the rest of the network about some sort of 1551 * reconfiguration such as a failover event or virtual machine 1552 * migration. 1553 */ 1554 void __netdev_notify_peers(struct net_device *dev) 1555 { 1556 ASSERT_RTNL(); 1557 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); 1558 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev); 1559 } 1560 EXPORT_SYMBOL(__netdev_notify_peers); 1561 1562 /** 1563 * netdev_notify_peers - notify network peers about existence of @dev 1564 * @dev: network device 1565 * 1566 * Generate traffic such that interested network peers are aware of 1567 * @dev, such as by generating a gratuitous ARP. This may be used when 1568 * a device wants to inform the rest of the network about some sort of 1569 * reconfiguration such as a failover event or virtual machine 1570 * migration. 1571 */ 1572 void netdev_notify_peers(struct net_device *dev) 1573 { 1574 rtnl_lock(); 1575 __netdev_notify_peers(dev); 1576 rtnl_unlock(); 1577 } 1578 EXPORT_SYMBOL(netdev_notify_peers); 1579 1580 static int napi_threaded_poll(void *data); 1581 1582 static int napi_kthread_create(struct napi_struct *n) 1583 { 1584 int err = 0; 1585 1586 /* Create and wake up the kthread once to put it in 1587 * TASK_INTERRUPTIBLE mode to avoid the blocked task 1588 * warning and work with loadavg. 1589 */ 1590 n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d", 1591 n->dev->name, n->napi_id); 1592 if (IS_ERR(n->thread)) { 1593 err = PTR_ERR(n->thread); 1594 pr_err("kthread_run failed with err %d\n", err); 1595 n->thread = NULL; 1596 } 1597 1598 return err; 1599 } 1600 1601 static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) 1602 { 1603 const struct net_device_ops *ops = dev->netdev_ops; 1604 int ret; 1605 1606 ASSERT_RTNL(); 1607 dev_addr_check(dev); 1608 1609 if (!netif_device_present(dev)) { 1610 /* may be detached because parent is runtime-suspended */ 1611 if (dev->dev.parent) 1612 pm_runtime_resume(dev->dev.parent); 1613 if (!netif_device_present(dev)) 1614 return -ENODEV; 1615 } 1616 1617 /* Block netpoll from trying to do any rx path servicing. 1618 * If we don't do this there is a chance ndo_poll_controller 1619 * or ndo_poll may be running while we open the device 1620 */ 1621 netpoll_poll_disable(dev); 1622 1623 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack); 1624 ret = notifier_to_errno(ret); 1625 if (ret) 1626 return ret; 1627 1628 set_bit(__LINK_STATE_START, &dev->state); 1629 1630 netdev_ops_assert_locked(dev); 1631 1632 if (ops->ndo_validate_addr) 1633 ret = ops->ndo_validate_addr(dev); 1634 1635 if (!ret && ops->ndo_open) 1636 ret = ops->ndo_open(dev); 1637 1638 netpoll_poll_enable(dev); 1639 1640 if (ret) 1641 clear_bit(__LINK_STATE_START, &dev->state); 1642 else { 1643 netif_set_up(dev, true); 1644 dev_set_rx_mode(dev); 1645 dev_activate(dev); 1646 add_device_randomness(dev->dev_addr, dev->addr_len); 1647 } 1648 1649 return ret; 1650 } 1651 1652 int netif_open(struct net_device *dev, struct netlink_ext_ack *extack) 1653 { 1654 int ret; 1655 1656 if (dev->flags & IFF_UP) 1657 return 0; 1658 1659 ret = __dev_open(dev, extack); 1660 if (ret < 0) 1661 return ret; 1662 1663 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP | IFF_RUNNING, GFP_KERNEL, 0, NULL); 1664 call_netdevice_notifiers(NETDEV_UP, dev); 1665 1666 return ret; 1667 } 1668 1669 static void __dev_close_many(struct list_head *head) 1670 { 1671 struct net_device *dev; 1672 1673 ASSERT_RTNL(); 1674 might_sleep(); 1675 1676 list_for_each_entry(dev, head, close_list) { 1677 /* Temporarily disable netpoll until the interface is down */ 1678 netpoll_poll_disable(dev); 1679 1680 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); 1681 1682 clear_bit(__LINK_STATE_START, &dev->state); 1683 1684 /* Synchronize to scheduled poll. We cannot touch poll list, it 1685 * can be even on different cpu. So just clear netif_running(). 1686 * 1687 * dev->stop() will invoke napi_disable() on all of it's 1688 * napi_struct instances on this device. 1689 */ 1690 smp_mb__after_atomic(); /* Commit netif_running(). */ 1691 } 1692 1693 dev_deactivate_many(head); 1694 1695 list_for_each_entry(dev, head, close_list) { 1696 const struct net_device_ops *ops = dev->netdev_ops; 1697 1698 /* 1699 * Call the device specific close. This cannot fail. 1700 * Only if device is UP 1701 * 1702 * We allow it to be called even after a DETACH hot-plug 1703 * event. 1704 */ 1705 1706 netdev_ops_assert_locked(dev); 1707 1708 if (ops->ndo_stop) 1709 ops->ndo_stop(dev); 1710 1711 netif_set_up(dev, false); 1712 netpoll_poll_enable(dev); 1713 } 1714 } 1715 1716 static void __dev_close(struct net_device *dev) 1717 { 1718 LIST_HEAD(single); 1719 1720 list_add(&dev->close_list, &single); 1721 __dev_close_many(&single); 1722 list_del(&single); 1723 } 1724 1725 void dev_close_many(struct list_head *head, bool unlink) 1726 { 1727 struct net_device *dev, *tmp; 1728 1729 /* Remove the devices that don't need to be closed */ 1730 list_for_each_entry_safe(dev, tmp, head, close_list) 1731 if (!(dev->flags & IFF_UP)) 1732 list_del_init(&dev->close_list); 1733 1734 __dev_close_many(head); 1735 1736 list_for_each_entry_safe(dev, tmp, head, close_list) { 1737 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP | IFF_RUNNING, GFP_KERNEL, 0, NULL); 1738 call_netdevice_notifiers(NETDEV_DOWN, dev); 1739 if (unlink) 1740 list_del_init(&dev->close_list); 1741 } 1742 } 1743 EXPORT_SYMBOL(dev_close_many); 1744 1745 void netif_close(struct net_device *dev) 1746 { 1747 if (dev->flags & IFF_UP) { 1748 LIST_HEAD(single); 1749 1750 list_add(&dev->close_list, &single); 1751 dev_close_many(&single, true); 1752 list_del(&single); 1753 } 1754 } 1755 EXPORT_SYMBOL(netif_close); 1756 1757 void netif_disable_lro(struct net_device *dev) 1758 { 1759 struct net_device *lower_dev; 1760 struct list_head *iter; 1761 1762 dev->wanted_features &= ~NETIF_F_LRO; 1763 netdev_update_features(dev); 1764 1765 if (unlikely(dev->features & NETIF_F_LRO)) 1766 netdev_WARN(dev, "failed to disable LRO!\n"); 1767 1768 netdev_for_each_lower_dev(dev, lower_dev, iter) { 1769 netdev_lock_ops(lower_dev); 1770 netif_disable_lro(lower_dev); 1771 netdev_unlock_ops(lower_dev); 1772 } 1773 } 1774 1775 /** 1776 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device 1777 * @dev: device 1778 * 1779 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be 1780 * called under RTNL. This is needed if Generic XDP is installed on 1781 * the device. 1782 */ 1783 static void dev_disable_gro_hw(struct net_device *dev) 1784 { 1785 dev->wanted_features &= ~NETIF_F_GRO_HW; 1786 netdev_update_features(dev); 1787 1788 if (unlikely(dev->features & NETIF_F_GRO_HW)) 1789 netdev_WARN(dev, "failed to disable GRO_HW!\n"); 1790 } 1791 1792 const char *netdev_cmd_to_name(enum netdev_cmd cmd) 1793 { 1794 #define N(val) \ 1795 case NETDEV_##val: \ 1796 return "NETDEV_" __stringify(val); 1797 switch (cmd) { 1798 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER) 1799 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE) 1800 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE) 1801 N(POST_INIT) N(PRE_UNINIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) 1802 N(CHANGEUPPER) N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) 1803 N(BONDING_INFO) N(PRECHANGEUPPER) N(CHANGELOWERSTATE) 1804 N(UDP_TUNNEL_PUSH_INFO) N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN) 1805 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO) 1806 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) 1807 N(PRE_CHANGEADDR) N(OFFLOAD_XSTATS_ENABLE) N(OFFLOAD_XSTATS_DISABLE) 1808 N(OFFLOAD_XSTATS_REPORT_USED) N(OFFLOAD_XSTATS_REPORT_DELTA) 1809 N(XDP_FEAT_CHANGE) 1810 } 1811 #undef N 1812 return "UNKNOWN_NETDEV_EVENT"; 1813 } 1814 EXPORT_SYMBOL_GPL(netdev_cmd_to_name); 1815 1816 static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, 1817 struct net_device *dev) 1818 { 1819 struct netdev_notifier_info info = { 1820 .dev = dev, 1821 }; 1822 1823 return nb->notifier_call(nb, val, &info); 1824 } 1825 1826 static int call_netdevice_register_notifiers(struct notifier_block *nb, 1827 struct net_device *dev) 1828 { 1829 int err; 1830 1831 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); 1832 err = notifier_to_errno(err); 1833 if (err) 1834 return err; 1835 1836 if (!(dev->flags & IFF_UP)) 1837 return 0; 1838 1839 call_netdevice_notifier(nb, NETDEV_UP, dev); 1840 return 0; 1841 } 1842 1843 static void call_netdevice_unregister_notifiers(struct notifier_block *nb, 1844 struct net_device *dev) 1845 { 1846 if (dev->flags & IFF_UP) { 1847 call_netdevice_notifier(nb, NETDEV_GOING_DOWN, 1848 dev); 1849 call_netdevice_notifier(nb, NETDEV_DOWN, dev); 1850 } 1851 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); 1852 } 1853 1854 static int call_netdevice_register_net_notifiers(struct notifier_block *nb, 1855 struct net *net) 1856 { 1857 struct net_device *dev; 1858 int err; 1859 1860 for_each_netdev(net, dev) { 1861 err = call_netdevice_register_notifiers(nb, dev); 1862 if (err) 1863 goto rollback; 1864 } 1865 return 0; 1866 1867 rollback: 1868 for_each_netdev_continue_reverse(net, dev) 1869 call_netdevice_unregister_notifiers(nb, dev); 1870 return err; 1871 } 1872 1873 static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb, 1874 struct net *net) 1875 { 1876 struct net_device *dev; 1877 1878 for_each_netdev(net, dev) 1879 call_netdevice_unregister_notifiers(nb, dev); 1880 } 1881 1882 static int dev_boot_phase = 1; 1883 1884 /** 1885 * register_netdevice_notifier - register a network notifier block 1886 * @nb: notifier 1887 * 1888 * Register a notifier to be called when network device events occur. 1889 * The notifier passed is linked into the kernel structures and must 1890 * not be reused until it has been unregistered. A negative errno code 1891 * is returned on a failure. 1892 * 1893 * When registered all registration and up events are replayed 1894 * to the new notifier to allow device to have a race free 1895 * view of the network device list. 1896 */ 1897 1898 int register_netdevice_notifier(struct notifier_block *nb) 1899 { 1900 struct net *net; 1901 int err; 1902 1903 /* Close race with setup_net() and cleanup_net() */ 1904 down_write(&pernet_ops_rwsem); 1905 1906 /* When RTNL is removed, we need protection for netdev_chain. */ 1907 rtnl_lock(); 1908 1909 err = raw_notifier_chain_register(&netdev_chain, nb); 1910 if (err) 1911 goto unlock; 1912 if (dev_boot_phase) 1913 goto unlock; 1914 for_each_net(net) { 1915 __rtnl_net_lock(net); 1916 err = call_netdevice_register_net_notifiers(nb, net); 1917 __rtnl_net_unlock(net); 1918 if (err) 1919 goto rollback; 1920 } 1921 1922 unlock: 1923 rtnl_unlock(); 1924 up_write(&pernet_ops_rwsem); 1925 return err; 1926 1927 rollback: 1928 for_each_net_continue_reverse(net) { 1929 __rtnl_net_lock(net); 1930 call_netdevice_unregister_net_notifiers(nb, net); 1931 __rtnl_net_unlock(net); 1932 } 1933 1934 raw_notifier_chain_unregister(&netdev_chain, nb); 1935 goto unlock; 1936 } 1937 EXPORT_SYMBOL(register_netdevice_notifier); 1938 1939 /** 1940 * unregister_netdevice_notifier - unregister a network notifier block 1941 * @nb: notifier 1942 * 1943 * Unregister a notifier previously registered by 1944 * register_netdevice_notifier(). The notifier is unlinked into the 1945 * kernel structures and may then be reused. A negative errno code 1946 * is returned on a failure. 1947 * 1948 * After unregistering unregister and down device events are synthesized 1949 * for all devices on the device list to the removed notifier to remove 1950 * the need for special case cleanup code. 1951 */ 1952 1953 int unregister_netdevice_notifier(struct notifier_block *nb) 1954 { 1955 struct net *net; 1956 int err; 1957 1958 /* Close race with setup_net() and cleanup_net() */ 1959 down_write(&pernet_ops_rwsem); 1960 rtnl_lock(); 1961 err = raw_notifier_chain_unregister(&netdev_chain, nb); 1962 if (err) 1963 goto unlock; 1964 1965 for_each_net(net) { 1966 __rtnl_net_lock(net); 1967 call_netdevice_unregister_net_notifiers(nb, net); 1968 __rtnl_net_unlock(net); 1969 } 1970 1971 unlock: 1972 rtnl_unlock(); 1973 up_write(&pernet_ops_rwsem); 1974 return err; 1975 } 1976 EXPORT_SYMBOL(unregister_netdevice_notifier); 1977 1978 static int __register_netdevice_notifier_net(struct net *net, 1979 struct notifier_block *nb, 1980 bool ignore_call_fail) 1981 { 1982 int err; 1983 1984 err = raw_notifier_chain_register(&net->netdev_chain, nb); 1985 if (err) 1986 return err; 1987 if (dev_boot_phase) 1988 return 0; 1989 1990 err = call_netdevice_register_net_notifiers(nb, net); 1991 if (err && !ignore_call_fail) 1992 goto chain_unregister; 1993 1994 return 0; 1995 1996 chain_unregister: 1997 raw_notifier_chain_unregister(&net->netdev_chain, nb); 1998 return err; 1999 } 2000 2001 static int __unregister_netdevice_notifier_net(struct net *net, 2002 struct notifier_block *nb) 2003 { 2004 int err; 2005 2006 err = raw_notifier_chain_unregister(&net->netdev_chain, nb); 2007 if (err) 2008 return err; 2009 2010 call_netdevice_unregister_net_notifiers(nb, net); 2011 return 0; 2012 } 2013 2014 /** 2015 * register_netdevice_notifier_net - register a per-netns network notifier block 2016 * @net: network namespace 2017 * @nb: notifier 2018 * 2019 * Register a notifier to be called when network device events occur. 2020 * The notifier passed is linked into the kernel structures and must 2021 * not be reused until it has been unregistered. A negative errno code 2022 * is returned on a failure. 2023 * 2024 * When registered all registration and up events are replayed 2025 * to the new notifier to allow device to have a race free 2026 * view of the network device list. 2027 */ 2028 2029 int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb) 2030 { 2031 int err; 2032 2033 rtnl_net_lock(net); 2034 err = __register_netdevice_notifier_net(net, nb, false); 2035 rtnl_net_unlock(net); 2036 2037 return err; 2038 } 2039 EXPORT_SYMBOL(register_netdevice_notifier_net); 2040 2041 /** 2042 * unregister_netdevice_notifier_net - unregister a per-netns 2043 * network notifier block 2044 * @net: network namespace 2045 * @nb: notifier 2046 * 2047 * Unregister a notifier previously registered by 2048 * register_netdevice_notifier_net(). The notifier is unlinked from the 2049 * kernel structures and may then be reused. A negative errno code 2050 * is returned on a failure. 2051 * 2052 * After unregistering unregister and down device events are synthesized 2053 * for all devices on the device list to the removed notifier to remove 2054 * the need for special case cleanup code. 2055 */ 2056 2057 int unregister_netdevice_notifier_net(struct net *net, 2058 struct notifier_block *nb) 2059 { 2060 int err; 2061 2062 rtnl_net_lock(net); 2063 err = __unregister_netdevice_notifier_net(net, nb); 2064 rtnl_net_unlock(net); 2065 2066 return err; 2067 } 2068 EXPORT_SYMBOL(unregister_netdevice_notifier_net); 2069 2070 static void __move_netdevice_notifier_net(struct net *src_net, 2071 struct net *dst_net, 2072 struct notifier_block *nb) 2073 { 2074 __unregister_netdevice_notifier_net(src_net, nb); 2075 __register_netdevice_notifier_net(dst_net, nb, true); 2076 } 2077 2078 static void rtnl_net_dev_lock(struct net_device *dev) 2079 { 2080 bool again; 2081 2082 do { 2083 struct net *net; 2084 2085 again = false; 2086 2087 /* netns might be being dismantled. */ 2088 rcu_read_lock(); 2089 net = dev_net_rcu(dev); 2090 net_passive_inc(net); 2091 rcu_read_unlock(); 2092 2093 rtnl_net_lock(net); 2094 2095 #ifdef CONFIG_NET_NS 2096 /* dev might have been moved to another netns. */ 2097 if (!net_eq(net, rcu_access_pointer(dev->nd_net.net))) { 2098 rtnl_net_unlock(net); 2099 net_passive_dec(net); 2100 again = true; 2101 } 2102 #endif 2103 } while (again); 2104 } 2105 2106 static void rtnl_net_dev_unlock(struct net_device *dev) 2107 { 2108 struct net *net = dev_net(dev); 2109 2110 rtnl_net_unlock(net); 2111 net_passive_dec(net); 2112 } 2113 2114 int register_netdevice_notifier_dev_net(struct net_device *dev, 2115 struct notifier_block *nb, 2116 struct netdev_net_notifier *nn) 2117 { 2118 int err; 2119 2120 rtnl_net_dev_lock(dev); 2121 err = __register_netdevice_notifier_net(dev_net(dev), nb, false); 2122 if (!err) { 2123 nn->nb = nb; 2124 list_add(&nn->list, &dev->net_notifier_list); 2125 } 2126 rtnl_net_dev_unlock(dev); 2127 2128 return err; 2129 } 2130 EXPORT_SYMBOL(register_netdevice_notifier_dev_net); 2131 2132 int unregister_netdevice_notifier_dev_net(struct net_device *dev, 2133 struct notifier_block *nb, 2134 struct netdev_net_notifier *nn) 2135 { 2136 int err; 2137 2138 rtnl_net_dev_lock(dev); 2139 list_del(&nn->list); 2140 err = __unregister_netdevice_notifier_net(dev_net(dev), nb); 2141 rtnl_net_dev_unlock(dev); 2142 2143 return err; 2144 } 2145 EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net); 2146 2147 static void move_netdevice_notifiers_dev_net(struct net_device *dev, 2148 struct net *net) 2149 { 2150 struct netdev_net_notifier *nn; 2151 2152 list_for_each_entry(nn, &dev->net_notifier_list, list) 2153 __move_netdevice_notifier_net(dev_net(dev), net, nn->nb); 2154 } 2155 2156 /** 2157 * call_netdevice_notifiers_info - call all network notifier blocks 2158 * @val: value passed unmodified to notifier function 2159 * @info: notifier information data 2160 * 2161 * Call all network notifier blocks. Parameters and return value 2162 * are as for raw_notifier_call_chain(). 2163 */ 2164 2165 int call_netdevice_notifiers_info(unsigned long val, 2166 struct netdev_notifier_info *info) 2167 { 2168 struct net *net = dev_net(info->dev); 2169 int ret; 2170 2171 ASSERT_RTNL(); 2172 2173 /* Run per-netns notifier block chain first, then run the global one. 2174 * Hopefully, one day, the global one is going to be removed after 2175 * all notifier block registrators get converted to be per-netns. 2176 */ 2177 ret = raw_notifier_call_chain(&net->netdev_chain, val, info); 2178 if (ret & NOTIFY_STOP_MASK) 2179 return ret; 2180 return raw_notifier_call_chain(&netdev_chain, val, info); 2181 } 2182 2183 /** 2184 * call_netdevice_notifiers_info_robust - call per-netns notifier blocks 2185 * for and rollback on error 2186 * @val_up: value passed unmodified to notifier function 2187 * @val_down: value passed unmodified to the notifier function when 2188 * recovering from an error on @val_up 2189 * @info: notifier information data 2190 * 2191 * Call all per-netns network notifier blocks, but not notifier blocks on 2192 * the global notifier chain. Parameters and return value are as for 2193 * raw_notifier_call_chain_robust(). 2194 */ 2195 2196 static int 2197 call_netdevice_notifiers_info_robust(unsigned long val_up, 2198 unsigned long val_down, 2199 struct netdev_notifier_info *info) 2200 { 2201 struct net *net = dev_net(info->dev); 2202 2203 ASSERT_RTNL(); 2204 2205 return raw_notifier_call_chain_robust(&net->netdev_chain, 2206 val_up, val_down, info); 2207 } 2208 2209 static int call_netdevice_notifiers_extack(unsigned long val, 2210 struct net_device *dev, 2211 struct netlink_ext_ack *extack) 2212 { 2213 struct netdev_notifier_info info = { 2214 .dev = dev, 2215 .extack = extack, 2216 }; 2217 2218 return call_netdevice_notifiers_info(val, &info); 2219 } 2220 2221 /** 2222 * call_netdevice_notifiers - call all network notifier blocks 2223 * @val: value passed unmodified to notifier function 2224 * @dev: net_device pointer passed unmodified to notifier function 2225 * 2226 * Call all network notifier blocks. Parameters and return value 2227 * are as for raw_notifier_call_chain(). 2228 */ 2229 2230 int call_netdevice_notifiers(unsigned long val, struct net_device *dev) 2231 { 2232 return call_netdevice_notifiers_extack(val, dev, NULL); 2233 } 2234 EXPORT_SYMBOL(call_netdevice_notifiers); 2235 2236 /** 2237 * call_netdevice_notifiers_mtu - call all network notifier blocks 2238 * @val: value passed unmodified to notifier function 2239 * @dev: net_device pointer passed unmodified to notifier function 2240 * @arg: additional u32 argument passed to the notifier function 2241 * 2242 * Call all network notifier blocks. Parameters and return value 2243 * are as for raw_notifier_call_chain(). 2244 */ 2245 static int call_netdevice_notifiers_mtu(unsigned long val, 2246 struct net_device *dev, u32 arg) 2247 { 2248 struct netdev_notifier_info_ext info = { 2249 .info.dev = dev, 2250 .ext.mtu = arg, 2251 }; 2252 2253 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0); 2254 2255 return call_netdevice_notifiers_info(val, &info.info); 2256 } 2257 2258 #ifdef CONFIG_NET_INGRESS 2259 static DEFINE_STATIC_KEY_FALSE(ingress_needed_key); 2260 2261 void net_inc_ingress_queue(void) 2262 { 2263 static_branch_inc(&ingress_needed_key); 2264 } 2265 EXPORT_SYMBOL_GPL(net_inc_ingress_queue); 2266 2267 void net_dec_ingress_queue(void) 2268 { 2269 static_branch_dec(&ingress_needed_key); 2270 } 2271 EXPORT_SYMBOL_GPL(net_dec_ingress_queue); 2272 #endif 2273 2274 #ifdef CONFIG_NET_EGRESS 2275 static DEFINE_STATIC_KEY_FALSE(egress_needed_key); 2276 2277 void net_inc_egress_queue(void) 2278 { 2279 static_branch_inc(&egress_needed_key); 2280 } 2281 EXPORT_SYMBOL_GPL(net_inc_egress_queue); 2282 2283 void net_dec_egress_queue(void) 2284 { 2285 static_branch_dec(&egress_needed_key); 2286 } 2287 EXPORT_SYMBOL_GPL(net_dec_egress_queue); 2288 #endif 2289 2290 #ifdef CONFIG_NET_CLS_ACT 2291 DEFINE_STATIC_KEY_FALSE(tcf_sw_enabled_key); 2292 EXPORT_SYMBOL(tcf_sw_enabled_key); 2293 #endif 2294 2295 DEFINE_STATIC_KEY_FALSE(netstamp_needed_key); 2296 EXPORT_SYMBOL(netstamp_needed_key); 2297 #ifdef CONFIG_JUMP_LABEL 2298 static atomic_t netstamp_needed_deferred; 2299 static atomic_t netstamp_wanted; 2300 static void netstamp_clear(struct work_struct *work) 2301 { 2302 int deferred = atomic_xchg(&netstamp_needed_deferred, 0); 2303 int wanted; 2304 2305 wanted = atomic_add_return(deferred, &netstamp_wanted); 2306 if (wanted > 0) 2307 static_branch_enable(&netstamp_needed_key); 2308 else 2309 static_branch_disable(&netstamp_needed_key); 2310 } 2311 static DECLARE_WORK(netstamp_work, netstamp_clear); 2312 #endif 2313 2314 void net_enable_timestamp(void) 2315 { 2316 #ifdef CONFIG_JUMP_LABEL 2317 int wanted = atomic_read(&netstamp_wanted); 2318 2319 while (wanted > 0) { 2320 if (atomic_try_cmpxchg(&netstamp_wanted, &wanted, wanted + 1)) 2321 return; 2322 } 2323 atomic_inc(&netstamp_needed_deferred); 2324 schedule_work(&netstamp_work); 2325 #else 2326 static_branch_inc(&netstamp_needed_key); 2327 #endif 2328 } 2329 EXPORT_SYMBOL(net_enable_timestamp); 2330 2331 void net_disable_timestamp(void) 2332 { 2333 #ifdef CONFIG_JUMP_LABEL 2334 int wanted = atomic_read(&netstamp_wanted); 2335 2336 while (wanted > 1) { 2337 if (atomic_try_cmpxchg(&netstamp_wanted, &wanted, wanted - 1)) 2338 return; 2339 } 2340 atomic_dec(&netstamp_needed_deferred); 2341 schedule_work(&netstamp_work); 2342 #else 2343 static_branch_dec(&netstamp_needed_key); 2344 #endif 2345 } 2346 EXPORT_SYMBOL(net_disable_timestamp); 2347 2348 static inline void net_timestamp_set(struct sk_buff *skb) 2349 { 2350 skb->tstamp = 0; 2351 skb->tstamp_type = SKB_CLOCK_REALTIME; 2352 if (static_branch_unlikely(&netstamp_needed_key)) 2353 skb->tstamp = ktime_get_real(); 2354 } 2355 2356 #define net_timestamp_check(COND, SKB) \ 2357 if (static_branch_unlikely(&netstamp_needed_key)) { \ 2358 if ((COND) && !(SKB)->tstamp) \ 2359 (SKB)->tstamp = ktime_get_real(); \ 2360 } \ 2361 2362 bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb) 2363 { 2364 return __is_skb_forwardable(dev, skb, true); 2365 } 2366 EXPORT_SYMBOL_GPL(is_skb_forwardable); 2367 2368 static int __dev_forward_skb2(struct net_device *dev, struct sk_buff *skb, 2369 bool check_mtu) 2370 { 2371 int ret = ____dev_forward_skb(dev, skb, check_mtu); 2372 2373 if (likely(!ret)) { 2374 skb->protocol = eth_type_trans(skb, dev); 2375 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 2376 } 2377 2378 return ret; 2379 } 2380 2381 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb) 2382 { 2383 return __dev_forward_skb2(dev, skb, true); 2384 } 2385 EXPORT_SYMBOL_GPL(__dev_forward_skb); 2386 2387 /** 2388 * dev_forward_skb - loopback an skb to another netif 2389 * 2390 * @dev: destination network device 2391 * @skb: buffer to forward 2392 * 2393 * return values: 2394 * NET_RX_SUCCESS (no congestion) 2395 * NET_RX_DROP (packet was dropped, but freed) 2396 * 2397 * dev_forward_skb can be used for injecting an skb from the 2398 * start_xmit function of one device into the receive queue 2399 * of another device. 2400 * 2401 * The receiving device may be in another namespace, so 2402 * we have to clear all information in the skb that could 2403 * impact namespace isolation. 2404 */ 2405 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) 2406 { 2407 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb); 2408 } 2409 EXPORT_SYMBOL_GPL(dev_forward_skb); 2410 2411 int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb) 2412 { 2413 return __dev_forward_skb2(dev, skb, false) ?: netif_rx_internal(skb); 2414 } 2415 2416 static inline int deliver_skb(struct sk_buff *skb, 2417 struct packet_type *pt_prev, 2418 struct net_device *orig_dev) 2419 { 2420 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) 2421 return -ENOMEM; 2422 refcount_inc(&skb->users); 2423 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); 2424 } 2425 2426 static inline void deliver_ptype_list_skb(struct sk_buff *skb, 2427 struct packet_type **pt, 2428 struct net_device *orig_dev, 2429 __be16 type, 2430 struct list_head *ptype_list) 2431 { 2432 struct packet_type *ptype, *pt_prev = *pt; 2433 2434 list_for_each_entry_rcu(ptype, ptype_list, list) { 2435 if (ptype->type != type) 2436 continue; 2437 if (pt_prev) 2438 deliver_skb(skb, pt_prev, orig_dev); 2439 pt_prev = ptype; 2440 } 2441 *pt = pt_prev; 2442 } 2443 2444 static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) 2445 { 2446 if (!ptype->af_packet_priv || !skb->sk) 2447 return false; 2448 2449 if (ptype->id_match) 2450 return ptype->id_match(ptype, skb->sk); 2451 else if ((struct sock *)ptype->af_packet_priv == skb->sk) 2452 return true; 2453 2454 return false; 2455 } 2456 2457 /** 2458 * dev_nit_active_rcu - return true if any network interface taps are in use 2459 * 2460 * The caller must hold the RCU lock 2461 * 2462 * @dev: network device to check for the presence of taps 2463 */ 2464 bool dev_nit_active_rcu(const struct net_device *dev) 2465 { 2466 /* Callers may hold either RCU or RCU BH lock */ 2467 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); 2468 2469 return !list_empty(&dev_net(dev)->ptype_all) || 2470 !list_empty(&dev->ptype_all); 2471 } 2472 EXPORT_SYMBOL_GPL(dev_nit_active_rcu); 2473 2474 /* 2475 * Support routine. Sends outgoing frames to any network 2476 * taps currently in use. 2477 */ 2478 2479 void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) 2480 { 2481 struct packet_type *ptype, *pt_prev = NULL; 2482 struct list_head *ptype_list; 2483 struct sk_buff *skb2 = NULL; 2484 2485 rcu_read_lock(); 2486 ptype_list = &dev_net_rcu(dev)->ptype_all; 2487 again: 2488 list_for_each_entry_rcu(ptype, ptype_list, list) { 2489 if (READ_ONCE(ptype->ignore_outgoing)) 2490 continue; 2491 2492 /* Never send packets back to the socket 2493 * they originated from - MvS ([email protected]) 2494 */ 2495 if (skb_loop_sk(ptype, skb)) 2496 continue; 2497 2498 if (pt_prev) { 2499 deliver_skb(skb2, pt_prev, skb->dev); 2500 pt_prev = ptype; 2501 continue; 2502 } 2503 2504 /* need to clone skb, done only once */ 2505 skb2 = skb_clone(skb, GFP_ATOMIC); 2506 if (!skb2) 2507 goto out_unlock; 2508 2509 net_timestamp_set(skb2); 2510 2511 /* skb->nh should be correctly 2512 * set by sender, so that the second statement is 2513 * just protection against buggy protocols. 2514 */ 2515 skb_reset_mac_header(skb2); 2516 2517 if (skb_network_header(skb2) < skb2->data || 2518 skb_network_header(skb2) > skb_tail_pointer(skb2)) { 2519 net_crit_ratelimited("protocol %04x is buggy, dev %s\n", 2520 ntohs(skb2->protocol), 2521 dev->name); 2522 skb_reset_network_header(skb2); 2523 } 2524 2525 skb2->transport_header = skb2->network_header; 2526 skb2->pkt_type = PACKET_OUTGOING; 2527 pt_prev = ptype; 2528 } 2529 2530 if (ptype_list != &dev->ptype_all) { 2531 ptype_list = &dev->ptype_all; 2532 goto again; 2533 } 2534 out_unlock: 2535 if (pt_prev) { 2536 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC)) 2537 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); 2538 else 2539 kfree_skb(skb2); 2540 } 2541 rcu_read_unlock(); 2542 } 2543 EXPORT_SYMBOL_GPL(dev_queue_xmit_nit); 2544 2545 /** 2546 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change 2547 * @dev: Network device 2548 * @txq: number of queues available 2549 * 2550 * If real_num_tx_queues is changed the tc mappings may no longer be 2551 * valid. To resolve this verify the tc mapping remains valid and if 2552 * not NULL the mapping. With no priorities mapping to this 2553 * offset/count pair it will no longer be used. In the worst case TC0 2554 * is invalid nothing can be done so disable priority mappings. If is 2555 * expected that drivers will fix this mapping if they can before 2556 * calling netif_set_real_num_tx_queues. 2557 */ 2558 static void netif_setup_tc(struct net_device *dev, unsigned int txq) 2559 { 2560 int i; 2561 struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; 2562 2563 /* If TC0 is invalidated disable TC mapping */ 2564 if (tc->offset + tc->count > txq) { 2565 netdev_warn(dev, "Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); 2566 dev->num_tc = 0; 2567 return; 2568 } 2569 2570 /* Invalidated prio to tc mappings set to TC0 */ 2571 for (i = 1; i < TC_BITMASK + 1; i++) { 2572 int q = netdev_get_prio_tc_map(dev, i); 2573 2574 tc = &dev->tc_to_txq[q]; 2575 if (tc->offset + tc->count > txq) { 2576 netdev_warn(dev, "Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", 2577 i, q); 2578 netdev_set_prio_tc_map(dev, i, 0); 2579 } 2580 } 2581 } 2582 2583 int netdev_txq_to_tc(struct net_device *dev, unsigned int txq) 2584 { 2585 if (dev->num_tc) { 2586 struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; 2587 int i; 2588 2589 /* walk through the TCs and see if it falls into any of them */ 2590 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) { 2591 if ((txq - tc->offset) < tc->count) 2592 return i; 2593 } 2594 2595 /* didn't find it, just return -1 to indicate no match */ 2596 return -1; 2597 } 2598 2599 return 0; 2600 } 2601 EXPORT_SYMBOL(netdev_txq_to_tc); 2602 2603 #ifdef CONFIG_XPS 2604 static struct static_key xps_needed __read_mostly; 2605 static struct static_key xps_rxqs_needed __read_mostly; 2606 static DEFINE_MUTEX(xps_map_mutex); 2607 #define xmap_dereference(P) \ 2608 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) 2609 2610 static bool remove_xps_queue(struct xps_dev_maps *dev_maps, 2611 struct xps_dev_maps *old_maps, int tci, u16 index) 2612 { 2613 struct xps_map *map = NULL; 2614 int pos; 2615 2616 map = xmap_dereference(dev_maps->attr_map[tci]); 2617 if (!map) 2618 return false; 2619 2620 for (pos = map->len; pos--;) { 2621 if (map->queues[pos] != index) 2622 continue; 2623 2624 if (map->len > 1) { 2625 map->queues[pos] = map->queues[--map->len]; 2626 break; 2627 } 2628 2629 if (old_maps) 2630 RCU_INIT_POINTER(old_maps->attr_map[tci], NULL); 2631 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); 2632 kfree_rcu(map, rcu); 2633 return false; 2634 } 2635 2636 return true; 2637 } 2638 2639 static bool remove_xps_queue_cpu(struct net_device *dev, 2640 struct xps_dev_maps *dev_maps, 2641 int cpu, u16 offset, u16 count) 2642 { 2643 int num_tc = dev_maps->num_tc; 2644 bool active = false; 2645 int tci; 2646 2647 for (tci = cpu * num_tc; num_tc--; tci++) { 2648 int i, j; 2649 2650 for (i = count, j = offset; i--; j++) { 2651 if (!remove_xps_queue(dev_maps, NULL, tci, j)) 2652 break; 2653 } 2654 2655 active |= i < 0; 2656 } 2657 2658 return active; 2659 } 2660 2661 static void reset_xps_maps(struct net_device *dev, 2662 struct xps_dev_maps *dev_maps, 2663 enum xps_map_type type) 2664 { 2665 static_key_slow_dec_cpuslocked(&xps_needed); 2666 if (type == XPS_RXQS) 2667 static_key_slow_dec_cpuslocked(&xps_rxqs_needed); 2668 2669 RCU_INIT_POINTER(dev->xps_maps[type], NULL); 2670 2671 kfree_rcu(dev_maps, rcu); 2672 } 2673 2674 static void clean_xps_maps(struct net_device *dev, enum xps_map_type type, 2675 u16 offset, u16 count) 2676 { 2677 struct xps_dev_maps *dev_maps; 2678 bool active = false; 2679 int i, j; 2680 2681 dev_maps = xmap_dereference(dev->xps_maps[type]); 2682 if (!dev_maps) 2683 return; 2684 2685 for (j = 0; j < dev_maps->nr_ids; j++) 2686 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, count); 2687 if (!active) 2688 reset_xps_maps(dev, dev_maps, type); 2689 2690 if (type == XPS_CPUS) { 2691 for (i = offset + (count - 1); count--; i--) 2692 netdev_queue_numa_node_write( 2693 netdev_get_tx_queue(dev, i), NUMA_NO_NODE); 2694 } 2695 } 2696 2697 static void netif_reset_xps_queues(struct net_device *dev, u16 offset, 2698 u16 count) 2699 { 2700 if (!static_key_false(&xps_needed)) 2701 return; 2702 2703 cpus_read_lock(); 2704 mutex_lock(&xps_map_mutex); 2705 2706 if (static_key_false(&xps_rxqs_needed)) 2707 clean_xps_maps(dev, XPS_RXQS, offset, count); 2708 2709 clean_xps_maps(dev, XPS_CPUS, offset, count); 2710 2711 mutex_unlock(&xps_map_mutex); 2712 cpus_read_unlock(); 2713 } 2714 2715 static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) 2716 { 2717 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index); 2718 } 2719 2720 static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index, 2721 u16 index, bool is_rxqs_map) 2722 { 2723 struct xps_map *new_map; 2724 int alloc_len = XPS_MIN_MAP_ALLOC; 2725 int i, pos; 2726 2727 for (pos = 0; map && pos < map->len; pos++) { 2728 if (map->queues[pos] != index) 2729 continue; 2730 return map; 2731 } 2732 2733 /* Need to add tx-queue to this CPU's/rx-queue's existing map */ 2734 if (map) { 2735 if (pos < map->alloc_len) 2736 return map; 2737 2738 alloc_len = map->alloc_len * 2; 2739 } 2740 2741 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's 2742 * map 2743 */ 2744 if (is_rxqs_map) 2745 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL); 2746 else 2747 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, 2748 cpu_to_node(attr_index)); 2749 if (!new_map) 2750 return NULL; 2751 2752 for (i = 0; i < pos; i++) 2753 new_map->queues[i] = map->queues[i]; 2754 new_map->alloc_len = alloc_len; 2755 new_map->len = pos; 2756 2757 return new_map; 2758 } 2759 2760 /* Copy xps maps at a given index */ 2761 static void xps_copy_dev_maps(struct xps_dev_maps *dev_maps, 2762 struct xps_dev_maps *new_dev_maps, int index, 2763 int tc, bool skip_tc) 2764 { 2765 int i, tci = index * dev_maps->num_tc; 2766 struct xps_map *map; 2767 2768 /* copy maps belonging to foreign traffic classes */ 2769 for (i = 0; i < dev_maps->num_tc; i++, tci++) { 2770 if (i == tc && skip_tc) 2771 continue; 2772 2773 /* fill in the new device map from the old device map */ 2774 map = xmap_dereference(dev_maps->attr_map[tci]); 2775 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); 2776 } 2777 } 2778 2779 /* Must be called under cpus_read_lock */ 2780 int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, 2781 u16 index, enum xps_map_type type) 2782 { 2783 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL; 2784 const unsigned long *online_mask = NULL; 2785 bool active = false, copy = false; 2786 int i, j, tci, numa_node_id = -2; 2787 int maps_sz, num_tc = 1, tc = 0; 2788 struct xps_map *map, *new_map; 2789 unsigned int nr_ids; 2790 2791 WARN_ON_ONCE(index >= dev->num_tx_queues); 2792 2793 if (dev->num_tc) { 2794 /* Do not allow XPS on subordinate device directly */ 2795 num_tc = dev->num_tc; 2796 if (num_tc < 0) 2797 return -EINVAL; 2798 2799 /* If queue belongs to subordinate dev use its map */ 2800 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; 2801 2802 tc = netdev_txq_to_tc(dev, index); 2803 if (tc < 0) 2804 return -EINVAL; 2805 } 2806 2807 mutex_lock(&xps_map_mutex); 2808 2809 dev_maps = xmap_dereference(dev->xps_maps[type]); 2810 if (type == XPS_RXQS) { 2811 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues); 2812 nr_ids = dev->num_rx_queues; 2813 } else { 2814 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc); 2815 if (num_possible_cpus() > 1) 2816 online_mask = cpumask_bits(cpu_online_mask); 2817 nr_ids = nr_cpu_ids; 2818 } 2819 2820 if (maps_sz < L1_CACHE_BYTES) 2821 maps_sz = L1_CACHE_BYTES; 2822 2823 /* The old dev_maps could be larger or smaller than the one we're 2824 * setting up now, as dev->num_tc or nr_ids could have been updated in 2825 * between. We could try to be smart, but let's be safe instead and only 2826 * copy foreign traffic classes if the two map sizes match. 2827 */ 2828 if (dev_maps && 2829 dev_maps->num_tc == num_tc && dev_maps->nr_ids == nr_ids) 2830 copy = true; 2831 2832 /* allocate memory for queue storage */ 2833 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids), 2834 j < nr_ids;) { 2835 if (!new_dev_maps) { 2836 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); 2837 if (!new_dev_maps) { 2838 mutex_unlock(&xps_map_mutex); 2839 return -ENOMEM; 2840 } 2841 2842 new_dev_maps->nr_ids = nr_ids; 2843 new_dev_maps->num_tc = num_tc; 2844 } 2845 2846 tci = j * num_tc + tc; 2847 map = copy ? xmap_dereference(dev_maps->attr_map[tci]) : NULL; 2848 2849 map = expand_xps_map(map, j, index, type == XPS_RXQS); 2850 if (!map) 2851 goto error; 2852 2853 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); 2854 } 2855 2856 if (!new_dev_maps) 2857 goto out_no_new_maps; 2858 2859 if (!dev_maps) { 2860 /* Increment static keys at most once per type */ 2861 static_key_slow_inc_cpuslocked(&xps_needed); 2862 if (type == XPS_RXQS) 2863 static_key_slow_inc_cpuslocked(&xps_rxqs_needed); 2864 } 2865 2866 for (j = 0; j < nr_ids; j++) { 2867 bool skip_tc = false; 2868 2869 tci = j * num_tc + tc; 2870 if (netif_attr_test_mask(j, mask, nr_ids) && 2871 netif_attr_test_online(j, online_mask, nr_ids)) { 2872 /* add tx-queue to CPU/rx-queue maps */ 2873 int pos = 0; 2874 2875 skip_tc = true; 2876 2877 map = xmap_dereference(new_dev_maps->attr_map[tci]); 2878 while ((pos < map->len) && (map->queues[pos] != index)) 2879 pos++; 2880 2881 if (pos == map->len) 2882 map->queues[map->len++] = index; 2883 #ifdef CONFIG_NUMA 2884 if (type == XPS_CPUS) { 2885 if (numa_node_id == -2) 2886 numa_node_id = cpu_to_node(j); 2887 else if (numa_node_id != cpu_to_node(j)) 2888 numa_node_id = -1; 2889 } 2890 #endif 2891 } 2892 2893 if (copy) 2894 xps_copy_dev_maps(dev_maps, new_dev_maps, j, tc, 2895 skip_tc); 2896 } 2897 2898 rcu_assign_pointer(dev->xps_maps[type], new_dev_maps); 2899 2900 /* Cleanup old maps */ 2901 if (!dev_maps) 2902 goto out_no_old_maps; 2903 2904 for (j = 0; j < dev_maps->nr_ids; j++) { 2905 for (i = num_tc, tci = j * dev_maps->num_tc; i--; tci++) { 2906 map = xmap_dereference(dev_maps->attr_map[tci]); 2907 if (!map) 2908 continue; 2909 2910 if (copy) { 2911 new_map = xmap_dereference(new_dev_maps->attr_map[tci]); 2912 if (map == new_map) 2913 continue; 2914 } 2915 2916 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); 2917 kfree_rcu(map, rcu); 2918 } 2919 } 2920 2921 old_dev_maps = dev_maps; 2922 2923 out_no_old_maps: 2924 dev_maps = new_dev_maps; 2925 active = true; 2926 2927 out_no_new_maps: 2928 if (type == XPS_CPUS) 2929 /* update Tx queue numa node */ 2930 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), 2931 (numa_node_id >= 0) ? 2932 numa_node_id : NUMA_NO_NODE); 2933 2934 if (!dev_maps) 2935 goto out_no_maps; 2936 2937 /* removes tx-queue from unused CPUs/rx-queues */ 2938 for (j = 0; j < dev_maps->nr_ids; j++) { 2939 tci = j * dev_maps->num_tc; 2940 2941 for (i = 0; i < dev_maps->num_tc; i++, tci++) { 2942 if (i == tc && 2943 netif_attr_test_mask(j, mask, dev_maps->nr_ids) && 2944 netif_attr_test_online(j, online_mask, dev_maps->nr_ids)) 2945 continue; 2946 2947 active |= remove_xps_queue(dev_maps, 2948 copy ? old_dev_maps : NULL, 2949 tci, index); 2950 } 2951 } 2952 2953 if (old_dev_maps) 2954 kfree_rcu(old_dev_maps, rcu); 2955 2956 /* free map if not active */ 2957 if (!active) 2958 reset_xps_maps(dev, dev_maps, type); 2959 2960 out_no_maps: 2961 mutex_unlock(&xps_map_mutex); 2962 2963 return 0; 2964 error: 2965 /* remove any maps that we added */ 2966 for (j = 0; j < nr_ids; j++) { 2967 for (i = num_tc, tci = j * num_tc; i--; tci++) { 2968 new_map = xmap_dereference(new_dev_maps->attr_map[tci]); 2969 map = copy ? 2970 xmap_dereference(dev_maps->attr_map[tci]) : 2971 NULL; 2972 if (new_map && new_map != map) 2973 kfree(new_map); 2974 } 2975 } 2976 2977 mutex_unlock(&xps_map_mutex); 2978 2979 kfree(new_dev_maps); 2980 return -ENOMEM; 2981 } 2982 EXPORT_SYMBOL_GPL(__netif_set_xps_queue); 2983 2984 int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask, 2985 u16 index) 2986 { 2987 int ret; 2988 2989 cpus_read_lock(); 2990 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, XPS_CPUS); 2991 cpus_read_unlock(); 2992 2993 return ret; 2994 } 2995 EXPORT_SYMBOL(netif_set_xps_queue); 2996 2997 #endif 2998 static void netdev_unbind_all_sb_channels(struct net_device *dev) 2999 { 3000 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; 3001 3002 /* Unbind any subordinate channels */ 3003 while (txq-- != &dev->_tx[0]) { 3004 if (txq->sb_dev) 3005 netdev_unbind_sb_channel(dev, txq->sb_dev); 3006 } 3007 } 3008 3009 void netdev_reset_tc(struct net_device *dev) 3010 { 3011 #ifdef CONFIG_XPS 3012 netif_reset_xps_queues_gt(dev, 0); 3013 #endif 3014 netdev_unbind_all_sb_channels(dev); 3015 3016 /* Reset TC configuration of device */ 3017 dev->num_tc = 0; 3018 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq)); 3019 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map)); 3020 } 3021 EXPORT_SYMBOL(netdev_reset_tc); 3022 3023 int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset) 3024 { 3025 if (tc >= dev->num_tc) 3026 return -EINVAL; 3027 3028 #ifdef CONFIG_XPS 3029 netif_reset_xps_queues(dev, offset, count); 3030 #endif 3031 dev->tc_to_txq[tc].count = count; 3032 dev->tc_to_txq[tc].offset = offset; 3033 return 0; 3034 } 3035 EXPORT_SYMBOL(netdev_set_tc_queue); 3036 3037 int netdev_set_num_tc(struct net_device *dev, u8 num_tc) 3038 { 3039 if (num_tc > TC_MAX_QUEUE) 3040 return -EINVAL; 3041 3042 #ifdef CONFIG_XPS 3043 netif_reset_xps_queues_gt(dev, 0); 3044 #endif 3045 netdev_unbind_all_sb_channels(dev); 3046 3047 dev->num_tc = num_tc; 3048 return 0; 3049 } 3050 EXPORT_SYMBOL(netdev_set_num_tc); 3051 3052 void netdev_unbind_sb_channel(struct net_device *dev, 3053 struct net_device *sb_dev) 3054 { 3055 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; 3056 3057 #ifdef CONFIG_XPS 3058 netif_reset_xps_queues_gt(sb_dev, 0); 3059 #endif 3060 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq)); 3061 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map)); 3062 3063 while (txq-- != &dev->_tx[0]) { 3064 if (txq->sb_dev == sb_dev) 3065 txq->sb_dev = NULL; 3066 } 3067 } 3068 EXPORT_SYMBOL(netdev_unbind_sb_channel); 3069 3070 int netdev_bind_sb_channel_queue(struct net_device *dev, 3071 struct net_device *sb_dev, 3072 u8 tc, u16 count, u16 offset) 3073 { 3074 /* Make certain the sb_dev and dev are already configured */ 3075 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc) 3076 return -EINVAL; 3077 3078 /* We cannot hand out queues we don't have */ 3079 if ((offset + count) > dev->real_num_tx_queues) 3080 return -EINVAL; 3081 3082 /* Record the mapping */ 3083 sb_dev->tc_to_txq[tc].count = count; 3084 sb_dev->tc_to_txq[tc].offset = offset; 3085 3086 /* Provide a way for Tx queue to find the tc_to_txq map or 3087 * XPS map for itself. 3088 */ 3089 while (count--) 3090 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev; 3091 3092 return 0; 3093 } 3094 EXPORT_SYMBOL(netdev_bind_sb_channel_queue); 3095 3096 int netdev_set_sb_channel(struct net_device *dev, u16 channel) 3097 { 3098 /* Do not use a multiqueue device to represent a subordinate channel */ 3099 if (netif_is_multiqueue(dev)) 3100 return -ENODEV; 3101 3102 /* We allow channels 1 - 32767 to be used for subordinate channels. 3103 * Channel 0 is meant to be "native" mode and used only to represent 3104 * the main root device. We allow writing 0 to reset the device back 3105 * to normal mode after being used as a subordinate channel. 3106 */ 3107 if (channel > S16_MAX) 3108 return -EINVAL; 3109 3110 dev->num_tc = -channel; 3111 3112 return 0; 3113 } 3114 EXPORT_SYMBOL(netdev_set_sb_channel); 3115 3116 /* 3117 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues 3118 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed. 3119 */ 3120 int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) 3121 { 3122 bool disabling; 3123 int rc; 3124 3125 disabling = txq < dev->real_num_tx_queues; 3126 3127 if (txq < 1 || txq > dev->num_tx_queues) 3128 return -EINVAL; 3129 3130 if (dev->reg_state == NETREG_REGISTERED || 3131 dev->reg_state == NETREG_UNREGISTERING) { 3132 ASSERT_RTNL(); 3133 3134 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, 3135 txq); 3136 if (rc) 3137 return rc; 3138 3139 if (dev->num_tc) 3140 netif_setup_tc(dev, txq); 3141 3142 net_shaper_set_real_num_tx_queues(dev, txq); 3143 3144 dev_qdisc_change_real_num_tx(dev, txq); 3145 3146 dev->real_num_tx_queues = txq; 3147 3148 if (disabling) { 3149 synchronize_net(); 3150 qdisc_reset_all_tx_gt(dev, txq); 3151 #ifdef CONFIG_XPS 3152 netif_reset_xps_queues_gt(dev, txq); 3153 #endif 3154 } 3155 } else { 3156 dev->real_num_tx_queues = txq; 3157 } 3158 3159 return 0; 3160 } 3161 EXPORT_SYMBOL(netif_set_real_num_tx_queues); 3162 3163 #ifdef CONFIG_SYSFS 3164 /** 3165 * netif_set_real_num_rx_queues - set actual number of RX queues used 3166 * @dev: Network device 3167 * @rxq: Actual number of RX queues 3168 * 3169 * This must be called either with the rtnl_lock held or before 3170 * registration of the net device. Returns 0 on success, or a 3171 * negative error code. If called before registration, it always 3172 * succeeds. 3173 */ 3174 int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) 3175 { 3176 int rc; 3177 3178 if (rxq < 1 || rxq > dev->num_rx_queues) 3179 return -EINVAL; 3180 3181 if (dev->reg_state == NETREG_REGISTERED) { 3182 ASSERT_RTNL(); 3183 3184 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, 3185 rxq); 3186 if (rc) 3187 return rc; 3188 } 3189 3190 dev->real_num_rx_queues = rxq; 3191 return 0; 3192 } 3193 EXPORT_SYMBOL(netif_set_real_num_rx_queues); 3194 #endif 3195 3196 /** 3197 * netif_set_real_num_queues - set actual number of RX and TX queues used 3198 * @dev: Network device 3199 * @txq: Actual number of TX queues 3200 * @rxq: Actual number of RX queues 3201 * 3202 * Set the real number of both TX and RX queues. 3203 * Does nothing if the number of queues is already correct. 3204 */ 3205 int netif_set_real_num_queues(struct net_device *dev, 3206 unsigned int txq, unsigned int rxq) 3207 { 3208 unsigned int old_rxq = dev->real_num_rx_queues; 3209 int err; 3210 3211 if (txq < 1 || txq > dev->num_tx_queues || 3212 rxq < 1 || rxq > dev->num_rx_queues) 3213 return -EINVAL; 3214 3215 /* Start from increases, so the error path only does decreases - 3216 * decreases can't fail. 3217 */ 3218 if (rxq > dev->real_num_rx_queues) { 3219 err = netif_set_real_num_rx_queues(dev, rxq); 3220 if (err) 3221 return err; 3222 } 3223 if (txq > dev->real_num_tx_queues) { 3224 err = netif_set_real_num_tx_queues(dev, txq); 3225 if (err) 3226 goto undo_rx; 3227 } 3228 if (rxq < dev->real_num_rx_queues) 3229 WARN_ON(netif_set_real_num_rx_queues(dev, rxq)); 3230 if (txq < dev->real_num_tx_queues) 3231 WARN_ON(netif_set_real_num_tx_queues(dev, txq)); 3232 3233 return 0; 3234 undo_rx: 3235 WARN_ON(netif_set_real_num_rx_queues(dev, old_rxq)); 3236 return err; 3237 } 3238 EXPORT_SYMBOL(netif_set_real_num_queues); 3239 3240 /** 3241 * netif_set_tso_max_size() - set the max size of TSO frames supported 3242 * @dev: netdev to update 3243 * @size: max skb->len of a TSO frame 3244 * 3245 * Set the limit on the size of TSO super-frames the device can handle. 3246 * Unless explicitly set the stack will assume the value of 3247 * %GSO_LEGACY_MAX_SIZE. 3248 */ 3249 void netif_set_tso_max_size(struct net_device *dev, unsigned int size) 3250 { 3251 dev->tso_max_size = min(GSO_MAX_SIZE, size); 3252 if (size < READ_ONCE(dev->gso_max_size)) 3253 netif_set_gso_max_size(dev, size); 3254 if (size < READ_ONCE(dev->gso_ipv4_max_size)) 3255 netif_set_gso_ipv4_max_size(dev, size); 3256 } 3257 EXPORT_SYMBOL(netif_set_tso_max_size); 3258 3259 /** 3260 * netif_set_tso_max_segs() - set the max number of segs supported for TSO 3261 * @dev: netdev to update 3262 * @segs: max number of TCP segments 3263 * 3264 * Set the limit on the number of TCP segments the device can generate from 3265 * a single TSO super-frame. 3266 * Unless explicitly set the stack will assume the value of %GSO_MAX_SEGS. 3267 */ 3268 void netif_set_tso_max_segs(struct net_device *dev, unsigned int segs) 3269 { 3270 dev->tso_max_segs = segs; 3271 if (segs < READ_ONCE(dev->gso_max_segs)) 3272 netif_set_gso_max_segs(dev, segs); 3273 } 3274 EXPORT_SYMBOL(netif_set_tso_max_segs); 3275 3276 /** 3277 * netif_inherit_tso_max() - copy all TSO limits from a lower device to an upper 3278 * @to: netdev to update 3279 * @from: netdev from which to copy the limits 3280 */ 3281 void netif_inherit_tso_max(struct net_device *to, const struct net_device *from) 3282 { 3283 netif_set_tso_max_size(to, from->tso_max_size); 3284 netif_set_tso_max_segs(to, from->tso_max_segs); 3285 } 3286 EXPORT_SYMBOL(netif_inherit_tso_max); 3287 3288 /** 3289 * netif_get_num_default_rss_queues - default number of RSS queues 3290 * 3291 * Default value is the number of physical cores if there are only 1 or 2, or 3292 * divided by 2 if there are more. 3293 */ 3294 int netif_get_num_default_rss_queues(void) 3295 { 3296 cpumask_var_t cpus; 3297 int cpu, count = 0; 3298 3299 if (unlikely(is_kdump_kernel() || !zalloc_cpumask_var(&cpus, GFP_KERNEL))) 3300 return 1; 3301 3302 cpumask_copy(cpus, cpu_online_mask); 3303 for_each_cpu(cpu, cpus) { 3304 ++count; 3305 cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu)); 3306 } 3307 free_cpumask_var(cpus); 3308 3309 return count > 2 ? DIV_ROUND_UP(count, 2) : count; 3310 } 3311 EXPORT_SYMBOL(netif_get_num_default_rss_queues); 3312 3313 static void __netif_reschedule(struct Qdisc *q) 3314 { 3315 struct softnet_data *sd; 3316 unsigned long flags; 3317 3318 local_irq_save(flags); 3319 sd = this_cpu_ptr(&softnet_data); 3320 q->next_sched = NULL; 3321 *sd->output_queue_tailp = q; 3322 sd->output_queue_tailp = &q->next_sched; 3323 raise_softirq_irqoff(NET_TX_SOFTIRQ); 3324 local_irq_restore(flags); 3325 } 3326 3327 void __netif_schedule(struct Qdisc *q) 3328 { 3329 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) 3330 __netif_reschedule(q); 3331 } 3332 EXPORT_SYMBOL(__netif_schedule); 3333 3334 struct dev_kfree_skb_cb { 3335 enum skb_drop_reason reason; 3336 }; 3337 3338 static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb) 3339 { 3340 return (struct dev_kfree_skb_cb *)skb->cb; 3341 } 3342 3343 void netif_schedule_queue(struct netdev_queue *txq) 3344 { 3345 rcu_read_lock(); 3346 if (!netif_xmit_stopped(txq)) { 3347 struct Qdisc *q = rcu_dereference(txq->qdisc); 3348 3349 __netif_schedule(q); 3350 } 3351 rcu_read_unlock(); 3352 } 3353 EXPORT_SYMBOL(netif_schedule_queue); 3354 3355 void netif_tx_wake_queue(struct netdev_queue *dev_queue) 3356 { 3357 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { 3358 struct Qdisc *q; 3359 3360 rcu_read_lock(); 3361 q = rcu_dereference(dev_queue->qdisc); 3362 __netif_schedule(q); 3363 rcu_read_unlock(); 3364 } 3365 } 3366 EXPORT_SYMBOL(netif_tx_wake_queue); 3367 3368 void dev_kfree_skb_irq_reason(struct sk_buff *skb, enum skb_drop_reason reason) 3369 { 3370 unsigned long flags; 3371 3372 if (unlikely(!skb)) 3373 return; 3374 3375 if (likely(refcount_read(&skb->users) == 1)) { 3376 smp_rmb(); 3377 refcount_set(&skb->users, 0); 3378 } else if (likely(!refcount_dec_and_test(&skb->users))) { 3379 return; 3380 } 3381 get_kfree_skb_cb(skb)->reason = reason; 3382 local_irq_save(flags); 3383 skb->next = __this_cpu_read(softnet_data.completion_queue); 3384 __this_cpu_write(softnet_data.completion_queue, skb); 3385 raise_softirq_irqoff(NET_TX_SOFTIRQ); 3386 local_irq_restore(flags); 3387 } 3388 EXPORT_SYMBOL(dev_kfree_skb_irq_reason); 3389 3390 void dev_kfree_skb_any_reason(struct sk_buff *skb, enum skb_drop_reason reason) 3391 { 3392 if (in_hardirq() || irqs_disabled()) 3393 dev_kfree_skb_irq_reason(skb, reason); 3394 else 3395 kfree_skb_reason(skb, reason); 3396 } 3397 EXPORT_SYMBOL(dev_kfree_skb_any_reason); 3398 3399 3400 /** 3401 * netif_device_detach - mark device as removed 3402 * @dev: network device 3403 * 3404 * Mark device as removed from system and therefore no longer available. 3405 */ 3406 void netif_device_detach(struct net_device *dev) 3407 { 3408 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && 3409 netif_running(dev)) { 3410 netif_tx_stop_all_queues(dev); 3411 } 3412 } 3413 EXPORT_SYMBOL(netif_device_detach); 3414 3415 /** 3416 * netif_device_attach - mark device as attached 3417 * @dev: network device 3418 * 3419 * Mark device as attached from system and restart if needed. 3420 */ 3421 void netif_device_attach(struct net_device *dev) 3422 { 3423 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && 3424 netif_running(dev)) { 3425 netif_tx_wake_all_queues(dev); 3426 netdev_watchdog_up(dev); 3427 } 3428 } 3429 EXPORT_SYMBOL(netif_device_attach); 3430 3431 /* 3432 * Returns a Tx hash based on the given packet descriptor a Tx queues' number 3433 * to be used as a distribution range. 3434 */ 3435 static u16 skb_tx_hash(const struct net_device *dev, 3436 const struct net_device *sb_dev, 3437 struct sk_buff *skb) 3438 { 3439 u32 hash; 3440 u16 qoffset = 0; 3441 u16 qcount = dev->real_num_tx_queues; 3442 3443 if (dev->num_tc) { 3444 u8 tc = netdev_get_prio_tc_map(dev, skb->priority); 3445 3446 qoffset = sb_dev->tc_to_txq[tc].offset; 3447 qcount = sb_dev->tc_to_txq[tc].count; 3448 if (unlikely(!qcount)) { 3449 net_warn_ratelimited("%s: invalid qcount, qoffset %u for tc %u\n", 3450 sb_dev->name, qoffset, tc); 3451 qoffset = 0; 3452 qcount = dev->real_num_tx_queues; 3453 } 3454 } 3455 3456 if (skb_rx_queue_recorded(skb)) { 3457 DEBUG_NET_WARN_ON_ONCE(qcount == 0); 3458 hash = skb_get_rx_queue(skb); 3459 if (hash >= qoffset) 3460 hash -= qoffset; 3461 while (unlikely(hash >= qcount)) 3462 hash -= qcount; 3463 return hash + qoffset; 3464 } 3465 3466 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset; 3467 } 3468 3469 void skb_warn_bad_offload(const struct sk_buff *skb) 3470 { 3471 static const netdev_features_t null_features; 3472 struct net_device *dev = skb->dev; 3473 const char *name = ""; 3474 3475 if (!net_ratelimit()) 3476 return; 3477 3478 if (dev) { 3479 if (dev->dev.parent) 3480 name = dev_driver_string(dev->dev.parent); 3481 else 3482 name = netdev_name(dev); 3483 } 3484 skb_dump(KERN_WARNING, skb, false); 3485 WARN(1, "%s: caps=(%pNF, %pNF)\n", 3486 name, dev ? &dev->features : &null_features, 3487 skb->sk ? &skb->sk->sk_route_caps : &null_features); 3488 } 3489 3490 /* 3491 * Invalidate hardware checksum when packet is to be mangled, and 3492 * complete checksum manually on outgoing path. 3493 */ 3494 int skb_checksum_help(struct sk_buff *skb) 3495 { 3496 __wsum csum; 3497 int ret = 0, offset; 3498 3499 if (skb->ip_summed == CHECKSUM_COMPLETE) 3500 goto out_set_summed; 3501 3502 if (unlikely(skb_is_gso(skb))) { 3503 skb_warn_bad_offload(skb); 3504 return -EINVAL; 3505 } 3506 3507 if (!skb_frags_readable(skb)) { 3508 return -EFAULT; 3509 } 3510 3511 /* Before computing a checksum, we should make sure no frag could 3512 * be modified by an external entity : checksum could be wrong. 3513 */ 3514 if (skb_has_shared_frag(skb)) { 3515 ret = __skb_linearize(skb); 3516 if (ret) 3517 goto out; 3518 } 3519 3520 offset = skb_checksum_start_offset(skb); 3521 ret = -EINVAL; 3522 if (unlikely(offset >= skb_headlen(skb))) { 3523 DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false); 3524 WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n", 3525 offset, skb_headlen(skb)); 3526 goto out; 3527 } 3528 csum = skb_checksum(skb, offset, skb->len - offset, 0); 3529 3530 offset += skb->csum_offset; 3531 if (unlikely(offset + sizeof(__sum16) > skb_headlen(skb))) { 3532 DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false); 3533 WARN_ONCE(true, "offset+2 (%zu) > skb_headlen() (%u)\n", 3534 offset + sizeof(__sum16), skb_headlen(skb)); 3535 goto out; 3536 } 3537 ret = skb_ensure_writable(skb, offset + sizeof(__sum16)); 3538 if (ret) 3539 goto out; 3540 3541 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0; 3542 out_set_summed: 3543 skb->ip_summed = CHECKSUM_NONE; 3544 out: 3545 return ret; 3546 } 3547 EXPORT_SYMBOL(skb_checksum_help); 3548 3549 int skb_crc32c_csum_help(struct sk_buff *skb) 3550 { 3551 __le32 crc32c_csum; 3552 int ret = 0, offset, start; 3553 3554 if (skb->ip_summed != CHECKSUM_PARTIAL) 3555 goto out; 3556 3557 if (unlikely(skb_is_gso(skb))) 3558 goto out; 3559 3560 /* Before computing a checksum, we should make sure no frag could 3561 * be modified by an external entity : checksum could be wrong. 3562 */ 3563 if (unlikely(skb_has_shared_frag(skb))) { 3564 ret = __skb_linearize(skb); 3565 if (ret) 3566 goto out; 3567 } 3568 start = skb_checksum_start_offset(skb); 3569 offset = start + offsetof(struct sctphdr, checksum); 3570 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) { 3571 ret = -EINVAL; 3572 goto out; 3573 } 3574 3575 ret = skb_ensure_writable(skb, offset + sizeof(__le32)); 3576 if (ret) 3577 goto out; 3578 3579 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start, 3580 skb->len - start, ~(__u32)0, 3581 crc32c_csum_stub)); 3582 *(__le32 *)(skb->data + offset) = crc32c_csum; 3583 skb_reset_csum_not_inet(skb); 3584 out: 3585 return ret; 3586 } 3587 EXPORT_SYMBOL(skb_crc32c_csum_help); 3588 3589 __be16 skb_network_protocol(struct sk_buff *skb, int *depth) 3590 { 3591 __be16 type = skb->protocol; 3592 3593 /* Tunnel gso handlers can set protocol to ethernet. */ 3594 if (type == htons(ETH_P_TEB)) { 3595 struct ethhdr *eth; 3596 3597 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) 3598 return 0; 3599 3600 eth = (struct ethhdr *)skb->data; 3601 type = eth->h_proto; 3602 } 3603 3604 return vlan_get_protocol_and_depth(skb, type, depth); 3605 } 3606 3607 3608 /* Take action when hardware reception checksum errors are detected. */ 3609 #ifdef CONFIG_BUG 3610 static void do_netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) 3611 { 3612 netdev_err(dev, "hw csum failure\n"); 3613 skb_dump(KERN_ERR, skb, true); 3614 dump_stack(); 3615 } 3616 3617 void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) 3618 { 3619 DO_ONCE_LITE(do_netdev_rx_csum_fault, dev, skb); 3620 } 3621 EXPORT_SYMBOL(netdev_rx_csum_fault); 3622 #endif 3623 3624 /* XXX: check that highmem exists at all on the given machine. */ 3625 static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) 3626 { 3627 #ifdef CONFIG_HIGHMEM 3628 int i; 3629 3630 if (!(dev->features & NETIF_F_HIGHDMA)) { 3631 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3632 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3633 struct page *page = skb_frag_page(frag); 3634 3635 if (page && PageHighMem(page)) 3636 return 1; 3637 } 3638 } 3639 #endif 3640 return 0; 3641 } 3642 3643 /* If MPLS offload request, verify we are testing hardware MPLS features 3644 * instead of standard features for the netdev. 3645 */ 3646 #if IS_ENABLED(CONFIG_NET_MPLS_GSO) 3647 static netdev_features_t net_mpls_features(struct sk_buff *skb, 3648 netdev_features_t features, 3649 __be16 type) 3650 { 3651 if (eth_p_mpls(type)) 3652 features &= skb->dev->mpls_features; 3653 3654 return features; 3655 } 3656 #else 3657 static netdev_features_t net_mpls_features(struct sk_buff *skb, 3658 netdev_features_t features, 3659 __be16 type) 3660 { 3661 return features; 3662 } 3663 #endif 3664 3665 static netdev_features_t harmonize_features(struct sk_buff *skb, 3666 netdev_features_t features) 3667 { 3668 __be16 type; 3669 3670 type = skb_network_protocol(skb, NULL); 3671 features = net_mpls_features(skb, features, type); 3672 3673 if (skb->ip_summed != CHECKSUM_NONE && 3674 !can_checksum_protocol(features, type)) { 3675 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 3676 } 3677 if (illegal_highdma(skb->dev, skb)) 3678 features &= ~NETIF_F_SG; 3679 3680 return features; 3681 } 3682 3683 netdev_features_t passthru_features_check(struct sk_buff *skb, 3684 struct net_device *dev, 3685 netdev_features_t features) 3686 { 3687 return features; 3688 } 3689 EXPORT_SYMBOL(passthru_features_check); 3690 3691 static netdev_features_t dflt_features_check(struct sk_buff *skb, 3692 struct net_device *dev, 3693 netdev_features_t features) 3694 { 3695 return vlan_features_check(skb, features); 3696 } 3697 3698 static netdev_features_t gso_features_check(const struct sk_buff *skb, 3699 struct net_device *dev, 3700 netdev_features_t features) 3701 { 3702 u16 gso_segs = skb_shinfo(skb)->gso_segs; 3703 3704 if (gso_segs > READ_ONCE(dev->gso_max_segs)) 3705 return features & ~NETIF_F_GSO_MASK; 3706 3707 if (unlikely(skb->len >= netif_get_gso_max_size(dev, skb))) 3708 return features & ~NETIF_F_GSO_MASK; 3709 3710 if (!skb_shinfo(skb)->gso_type) { 3711 skb_warn_bad_offload(skb); 3712 return features & ~NETIF_F_GSO_MASK; 3713 } 3714 3715 /* Support for GSO partial features requires software 3716 * intervention before we can actually process the packets 3717 * so we need to strip support for any partial features now 3718 * and we can pull them back in after we have partially 3719 * segmented the frame. 3720 */ 3721 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)) 3722 features &= ~dev->gso_partial_features; 3723 3724 /* Make sure to clear the IPv4 ID mangling feature if the 3725 * IPv4 header has the potential to be fragmented. 3726 */ 3727 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { 3728 struct iphdr *iph = skb->encapsulation ? 3729 inner_ip_hdr(skb) : ip_hdr(skb); 3730 3731 if (!(iph->frag_off & htons(IP_DF))) 3732 features &= ~NETIF_F_TSO_MANGLEID; 3733 } 3734 3735 return features; 3736 } 3737 3738 netdev_features_t netif_skb_features(struct sk_buff *skb) 3739 { 3740 struct net_device *dev = skb->dev; 3741 netdev_features_t features = dev->features; 3742 3743 if (skb_is_gso(skb)) 3744 features = gso_features_check(skb, dev, features); 3745 3746 /* If encapsulation offload request, verify we are testing 3747 * hardware encapsulation features instead of standard 3748 * features for the netdev 3749 */ 3750 if (skb->encapsulation) 3751 features &= dev->hw_enc_features; 3752 3753 if (skb_vlan_tagged(skb)) 3754 features = netdev_intersect_features(features, 3755 dev->vlan_features | 3756 NETIF_F_HW_VLAN_CTAG_TX | 3757 NETIF_F_HW_VLAN_STAG_TX); 3758 3759 if (dev->netdev_ops->ndo_features_check) 3760 features &= dev->netdev_ops->ndo_features_check(skb, dev, 3761 features); 3762 else 3763 features &= dflt_features_check(skb, dev, features); 3764 3765 return harmonize_features(skb, features); 3766 } 3767 EXPORT_SYMBOL(netif_skb_features); 3768 3769 static int xmit_one(struct sk_buff *skb, struct net_device *dev, 3770 struct netdev_queue *txq, bool more) 3771 { 3772 unsigned int len; 3773 int rc; 3774 3775 if (dev_nit_active_rcu(dev)) 3776 dev_queue_xmit_nit(skb, dev); 3777 3778 len = skb->len; 3779 trace_net_dev_start_xmit(skb, dev); 3780 rc = netdev_start_xmit(skb, dev, txq, more); 3781 trace_net_dev_xmit(skb, rc, dev, len); 3782 3783 return rc; 3784 } 3785 3786 struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev, 3787 struct netdev_queue *txq, int *ret) 3788 { 3789 struct sk_buff *skb = first; 3790 int rc = NETDEV_TX_OK; 3791 3792 while (skb) { 3793 struct sk_buff *next = skb->next; 3794 3795 skb_mark_not_on_list(skb); 3796 rc = xmit_one(skb, dev, txq, next != NULL); 3797 if (unlikely(!dev_xmit_complete(rc))) { 3798 skb->next = next; 3799 goto out; 3800 } 3801 3802 skb = next; 3803 if (netif_tx_queue_stopped(txq) && skb) { 3804 rc = NETDEV_TX_BUSY; 3805 break; 3806 } 3807 } 3808 3809 out: 3810 *ret = rc; 3811 return skb; 3812 } 3813 3814 static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb, 3815 netdev_features_t features) 3816 { 3817 if (skb_vlan_tag_present(skb) && 3818 !vlan_hw_offload_capable(features, skb->vlan_proto)) 3819 skb = __vlan_hwaccel_push_inside(skb); 3820 return skb; 3821 } 3822 3823 int skb_csum_hwoffload_help(struct sk_buff *skb, 3824 const netdev_features_t features) 3825 { 3826 if (unlikely(skb_csum_is_sctp(skb))) 3827 return !!(features & NETIF_F_SCTP_CRC) ? 0 : 3828 skb_crc32c_csum_help(skb); 3829 3830 if (features & NETIF_F_HW_CSUM) 3831 return 0; 3832 3833 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) { 3834 if (vlan_get_protocol(skb) == htons(ETH_P_IPV6) && 3835 skb_network_header_len(skb) != sizeof(struct ipv6hdr) && 3836 !ipv6_has_hopopt_jumbo(skb)) 3837 goto sw_checksum; 3838 3839 switch (skb->csum_offset) { 3840 case offsetof(struct tcphdr, check): 3841 case offsetof(struct udphdr, check): 3842 return 0; 3843 } 3844 } 3845 3846 sw_checksum: 3847 return skb_checksum_help(skb); 3848 } 3849 EXPORT_SYMBOL(skb_csum_hwoffload_help); 3850 3851 static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again) 3852 { 3853 netdev_features_t features; 3854 3855 if (!skb_frags_readable(skb)) 3856 goto out_kfree_skb; 3857 3858 features = netif_skb_features(skb); 3859 skb = validate_xmit_vlan(skb, features); 3860 if (unlikely(!skb)) 3861 goto out_null; 3862 3863 skb = sk_validate_xmit_skb(skb, dev); 3864 if (unlikely(!skb)) 3865 goto out_null; 3866 3867 if (netif_needs_gso(skb, features)) { 3868 struct sk_buff *segs; 3869 3870 segs = skb_gso_segment(skb, features); 3871 if (IS_ERR(segs)) { 3872 goto out_kfree_skb; 3873 } else if (segs) { 3874 consume_skb(skb); 3875 skb = segs; 3876 } 3877 } else { 3878 if (skb_needs_linearize(skb, features) && 3879 __skb_linearize(skb)) 3880 goto out_kfree_skb; 3881 3882 /* If packet is not checksummed and device does not 3883 * support checksumming for this protocol, complete 3884 * checksumming here. 3885 */ 3886 if (skb->ip_summed == CHECKSUM_PARTIAL) { 3887 if (skb->encapsulation) 3888 skb_set_inner_transport_header(skb, 3889 skb_checksum_start_offset(skb)); 3890 else 3891 skb_set_transport_header(skb, 3892 skb_checksum_start_offset(skb)); 3893 if (skb_csum_hwoffload_help(skb, features)) 3894 goto out_kfree_skb; 3895 } 3896 } 3897 3898 skb = validate_xmit_xfrm(skb, features, again); 3899 3900 return skb; 3901 3902 out_kfree_skb: 3903 kfree_skb(skb); 3904 out_null: 3905 dev_core_stats_tx_dropped_inc(dev); 3906 return NULL; 3907 } 3908 3909 struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again) 3910 { 3911 struct sk_buff *next, *head = NULL, *tail; 3912 3913 for (; skb != NULL; skb = next) { 3914 next = skb->next; 3915 skb_mark_not_on_list(skb); 3916 3917 /* in case skb won't be segmented, point to itself */ 3918 skb->prev = skb; 3919 3920 skb = validate_xmit_skb(skb, dev, again); 3921 if (!skb) 3922 continue; 3923 3924 if (!head) 3925 head = skb; 3926 else 3927 tail->next = skb; 3928 /* If skb was segmented, skb->prev points to 3929 * the last segment. If not, it still contains skb. 3930 */ 3931 tail = skb->prev; 3932 } 3933 return head; 3934 } 3935 EXPORT_SYMBOL_GPL(validate_xmit_skb_list); 3936 3937 static void qdisc_pkt_len_init(struct sk_buff *skb) 3938 { 3939 const struct skb_shared_info *shinfo = skb_shinfo(skb); 3940 3941 qdisc_skb_cb(skb)->pkt_len = skb->len; 3942 3943 /* To get more precise estimation of bytes sent on wire, 3944 * we add to pkt_len the headers size of all segments 3945 */ 3946 if (shinfo->gso_size && skb_transport_header_was_set(skb)) { 3947 u16 gso_segs = shinfo->gso_segs; 3948 unsigned int hdr_len; 3949 3950 /* mac layer + network layer */ 3951 hdr_len = skb_transport_offset(skb); 3952 3953 /* + transport layer */ 3954 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 3955 const struct tcphdr *th; 3956 struct tcphdr _tcphdr; 3957 3958 th = skb_header_pointer(skb, hdr_len, 3959 sizeof(_tcphdr), &_tcphdr); 3960 if (likely(th)) 3961 hdr_len += __tcp_hdrlen(th); 3962 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 3963 struct udphdr _udphdr; 3964 3965 if (skb_header_pointer(skb, hdr_len, 3966 sizeof(_udphdr), &_udphdr)) 3967 hdr_len += sizeof(struct udphdr); 3968 } 3969 3970 if (unlikely(shinfo->gso_type & SKB_GSO_DODGY)) { 3971 int payload = skb->len - hdr_len; 3972 3973 /* Malicious packet. */ 3974 if (payload <= 0) 3975 return; 3976 gso_segs = DIV_ROUND_UP(payload, shinfo->gso_size); 3977 } 3978 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; 3979 } 3980 } 3981 3982 static int dev_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *q, 3983 struct sk_buff **to_free, 3984 struct netdev_queue *txq) 3985 { 3986 int rc; 3987 3988 rc = q->enqueue(skb, q, to_free) & NET_XMIT_MASK; 3989 if (rc == NET_XMIT_SUCCESS) 3990 trace_qdisc_enqueue(q, txq, skb); 3991 return rc; 3992 } 3993 3994 static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, 3995 struct net_device *dev, 3996 struct netdev_queue *txq) 3997 { 3998 spinlock_t *root_lock = qdisc_lock(q); 3999 struct sk_buff *to_free = NULL; 4000 bool contended; 4001 int rc; 4002 4003 qdisc_calculate_pkt_len(skb, q); 4004 4005 tcf_set_drop_reason(skb, SKB_DROP_REASON_QDISC_DROP); 4006 4007 if (q->flags & TCQ_F_NOLOCK) { 4008 if (q->flags & TCQ_F_CAN_BYPASS && nolock_qdisc_is_empty(q) && 4009 qdisc_run_begin(q)) { 4010 /* Retest nolock_qdisc_is_empty() within the protection 4011 * of q->seqlock to protect from racing with requeuing. 4012 */ 4013 if (unlikely(!nolock_qdisc_is_empty(q))) { 4014 rc = dev_qdisc_enqueue(skb, q, &to_free, txq); 4015 __qdisc_run(q); 4016 qdisc_run_end(q); 4017 4018 goto no_lock_out; 4019 } 4020 4021 qdisc_bstats_cpu_update(q, skb); 4022 if (sch_direct_xmit(skb, q, dev, txq, NULL, true) && 4023 !nolock_qdisc_is_empty(q)) 4024 __qdisc_run(q); 4025 4026 qdisc_run_end(q); 4027 return NET_XMIT_SUCCESS; 4028 } 4029 4030 rc = dev_qdisc_enqueue(skb, q, &to_free, txq); 4031 qdisc_run(q); 4032 4033 no_lock_out: 4034 if (unlikely(to_free)) 4035 kfree_skb_list_reason(to_free, 4036 tcf_get_drop_reason(to_free)); 4037 return rc; 4038 } 4039 4040 if (unlikely(READ_ONCE(q->owner) == smp_processor_id())) { 4041 kfree_skb_reason(skb, SKB_DROP_REASON_TC_RECLASSIFY_LOOP); 4042 return NET_XMIT_DROP; 4043 } 4044 /* 4045 * Heuristic to force contended enqueues to serialize on a 4046 * separate lock before trying to get qdisc main lock. 4047 * This permits qdisc->running owner to get the lock more 4048 * often and dequeue packets faster. 4049 * On PREEMPT_RT it is possible to preempt the qdisc owner during xmit 4050 * and then other tasks will only enqueue packets. The packets will be 4051 * sent after the qdisc owner is scheduled again. To prevent this 4052 * scenario the task always serialize on the lock. 4053 */ 4054 contended = qdisc_is_running(q) || IS_ENABLED(CONFIG_PREEMPT_RT); 4055 if (unlikely(contended)) 4056 spin_lock(&q->busylock); 4057 4058 spin_lock(root_lock); 4059 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { 4060 __qdisc_drop(skb, &to_free); 4061 rc = NET_XMIT_DROP; 4062 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && 4063 qdisc_run_begin(q)) { 4064 /* 4065 * This is a work-conserving queue; there are no old skbs 4066 * waiting to be sent out; and the qdisc is not running - 4067 * xmit the skb directly. 4068 */ 4069 4070 qdisc_bstats_update(q, skb); 4071 4072 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) { 4073 if (unlikely(contended)) { 4074 spin_unlock(&q->busylock); 4075 contended = false; 4076 } 4077 __qdisc_run(q); 4078 } 4079 4080 qdisc_run_end(q); 4081 rc = NET_XMIT_SUCCESS; 4082 } else { 4083 WRITE_ONCE(q->owner, smp_processor_id()); 4084 rc = dev_qdisc_enqueue(skb, q, &to_free, txq); 4085 WRITE_ONCE(q->owner, -1); 4086 if (qdisc_run_begin(q)) { 4087 if (unlikely(contended)) { 4088 spin_unlock(&q->busylock); 4089 contended = false; 4090 } 4091 __qdisc_run(q); 4092 qdisc_run_end(q); 4093 } 4094 } 4095 spin_unlock(root_lock); 4096 if (unlikely(to_free)) 4097 kfree_skb_list_reason(to_free, 4098 tcf_get_drop_reason(to_free)); 4099 if (unlikely(contended)) 4100 spin_unlock(&q->busylock); 4101 return rc; 4102 } 4103 4104 #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) 4105 static void skb_update_prio(struct sk_buff *skb) 4106 { 4107 const struct netprio_map *map; 4108 const struct sock *sk; 4109 unsigned int prioidx; 4110 4111 if (skb->priority) 4112 return; 4113 map = rcu_dereference_bh(skb->dev->priomap); 4114 if (!map) 4115 return; 4116 sk = skb_to_full_sk(skb); 4117 if (!sk) 4118 return; 4119 4120 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data); 4121 4122 if (prioidx < map->priomap_len) 4123 skb->priority = map->priomap[prioidx]; 4124 } 4125 #else 4126 #define skb_update_prio(skb) 4127 #endif 4128 4129 /** 4130 * dev_loopback_xmit - loop back @skb 4131 * @net: network namespace this loopback is happening in 4132 * @sk: sk needed to be a netfilter okfn 4133 * @skb: buffer to transmit 4134 */ 4135 int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) 4136 { 4137 skb_reset_mac_header(skb); 4138 __skb_pull(skb, skb_network_offset(skb)); 4139 skb->pkt_type = PACKET_LOOPBACK; 4140 if (skb->ip_summed == CHECKSUM_NONE) 4141 skb->ip_summed = CHECKSUM_UNNECESSARY; 4142 DEBUG_NET_WARN_ON_ONCE(!skb_dst(skb)); 4143 skb_dst_force(skb); 4144 netif_rx(skb); 4145 return 0; 4146 } 4147 EXPORT_SYMBOL(dev_loopback_xmit); 4148 4149 #ifdef CONFIG_NET_EGRESS 4150 static struct netdev_queue * 4151 netdev_tx_queue_mapping(struct net_device *dev, struct sk_buff *skb) 4152 { 4153 int qm = skb_get_queue_mapping(skb); 4154 4155 return netdev_get_tx_queue(dev, netdev_cap_txqueue(dev, qm)); 4156 } 4157 4158 #ifndef CONFIG_PREEMPT_RT 4159 static bool netdev_xmit_txqueue_skipped(void) 4160 { 4161 return __this_cpu_read(softnet_data.xmit.skip_txqueue); 4162 } 4163 4164 void netdev_xmit_skip_txqueue(bool skip) 4165 { 4166 __this_cpu_write(softnet_data.xmit.skip_txqueue, skip); 4167 } 4168 EXPORT_SYMBOL_GPL(netdev_xmit_skip_txqueue); 4169 4170 #else 4171 static bool netdev_xmit_txqueue_skipped(void) 4172 { 4173 return current->net_xmit.skip_txqueue; 4174 } 4175 4176 void netdev_xmit_skip_txqueue(bool skip) 4177 { 4178 current->net_xmit.skip_txqueue = skip; 4179 } 4180 EXPORT_SYMBOL_GPL(netdev_xmit_skip_txqueue); 4181 #endif 4182 #endif /* CONFIG_NET_EGRESS */ 4183 4184 #ifdef CONFIG_NET_XGRESS 4185 static int tc_run(struct tcx_entry *entry, struct sk_buff *skb, 4186 enum skb_drop_reason *drop_reason) 4187 { 4188 int ret = TC_ACT_UNSPEC; 4189 #ifdef CONFIG_NET_CLS_ACT 4190 struct mini_Qdisc *miniq = rcu_dereference_bh(entry->miniq); 4191 struct tcf_result res; 4192 4193 if (!miniq) 4194 return ret; 4195 4196 /* Global bypass */ 4197 if (!static_branch_likely(&tcf_sw_enabled_key)) 4198 return ret; 4199 4200 /* Block-wise bypass */ 4201 if (tcf_block_bypass_sw(miniq->block)) 4202 return ret; 4203 4204 tc_skb_cb(skb)->mru = 0; 4205 tc_skb_cb(skb)->post_ct = false; 4206 tcf_set_drop_reason(skb, *drop_reason); 4207 4208 mini_qdisc_bstats_cpu_update(miniq, skb); 4209 ret = tcf_classify(skb, miniq->block, miniq->filter_list, &res, false); 4210 /* Only tcf related quirks below. */ 4211 switch (ret) { 4212 case TC_ACT_SHOT: 4213 *drop_reason = tcf_get_drop_reason(skb); 4214 mini_qdisc_qstats_cpu_drop(miniq); 4215 break; 4216 case TC_ACT_OK: 4217 case TC_ACT_RECLASSIFY: 4218 skb->tc_index = TC_H_MIN(res.classid); 4219 break; 4220 } 4221 #endif /* CONFIG_NET_CLS_ACT */ 4222 return ret; 4223 } 4224 4225 static DEFINE_STATIC_KEY_FALSE(tcx_needed_key); 4226 4227 void tcx_inc(void) 4228 { 4229 static_branch_inc(&tcx_needed_key); 4230 } 4231 4232 void tcx_dec(void) 4233 { 4234 static_branch_dec(&tcx_needed_key); 4235 } 4236 4237 static __always_inline enum tcx_action_base 4238 tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb, 4239 const bool needs_mac) 4240 { 4241 const struct bpf_mprog_fp *fp; 4242 const struct bpf_prog *prog; 4243 int ret = TCX_NEXT; 4244 4245 if (needs_mac) 4246 __skb_push(skb, skb->mac_len); 4247 bpf_mprog_foreach_prog(entry, fp, prog) { 4248 bpf_compute_data_pointers(skb); 4249 ret = bpf_prog_run(prog, skb); 4250 if (ret != TCX_NEXT) 4251 break; 4252 } 4253 if (needs_mac) 4254 __skb_pull(skb, skb->mac_len); 4255 return tcx_action_code(skb, ret); 4256 } 4257 4258 static __always_inline struct sk_buff * 4259 sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, 4260 struct net_device *orig_dev, bool *another) 4261 { 4262 struct bpf_mprog_entry *entry = rcu_dereference_bh(skb->dev->tcx_ingress); 4263 enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_INGRESS; 4264 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 4265 int sch_ret; 4266 4267 if (!entry) 4268 return skb; 4269 4270 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 4271 if (*pt_prev) { 4272 *ret = deliver_skb(skb, *pt_prev, orig_dev); 4273 *pt_prev = NULL; 4274 } 4275 4276 qdisc_skb_cb(skb)->pkt_len = skb->len; 4277 tcx_set_ingress(skb, true); 4278 4279 if (static_branch_unlikely(&tcx_needed_key)) { 4280 sch_ret = tcx_run(entry, skb, true); 4281 if (sch_ret != TC_ACT_UNSPEC) 4282 goto ingress_verdict; 4283 } 4284 sch_ret = tc_run(tcx_entry(entry), skb, &drop_reason); 4285 ingress_verdict: 4286 switch (sch_ret) { 4287 case TC_ACT_REDIRECT: 4288 /* skb_mac_header check was done by BPF, so we can safely 4289 * push the L2 header back before redirecting to another 4290 * netdev. 4291 */ 4292 __skb_push(skb, skb->mac_len); 4293 if (skb_do_redirect(skb) == -EAGAIN) { 4294 __skb_pull(skb, skb->mac_len); 4295 *another = true; 4296 break; 4297 } 4298 *ret = NET_RX_SUCCESS; 4299 bpf_net_ctx_clear(bpf_net_ctx); 4300 return NULL; 4301 case TC_ACT_SHOT: 4302 kfree_skb_reason(skb, drop_reason); 4303 *ret = NET_RX_DROP; 4304 bpf_net_ctx_clear(bpf_net_ctx); 4305 return NULL; 4306 /* used by tc_run */ 4307 case TC_ACT_STOLEN: 4308 case TC_ACT_QUEUED: 4309 case TC_ACT_TRAP: 4310 consume_skb(skb); 4311 fallthrough; 4312 case TC_ACT_CONSUMED: 4313 *ret = NET_RX_SUCCESS; 4314 bpf_net_ctx_clear(bpf_net_ctx); 4315 return NULL; 4316 } 4317 bpf_net_ctx_clear(bpf_net_ctx); 4318 4319 return skb; 4320 } 4321 4322 static __always_inline struct sk_buff * 4323 sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) 4324 { 4325 struct bpf_mprog_entry *entry = rcu_dereference_bh(dev->tcx_egress); 4326 enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_EGRESS; 4327 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 4328 int sch_ret; 4329 4330 if (!entry) 4331 return skb; 4332 4333 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 4334 4335 /* qdisc_skb_cb(skb)->pkt_len & tcx_set_ingress() was 4336 * already set by the caller. 4337 */ 4338 if (static_branch_unlikely(&tcx_needed_key)) { 4339 sch_ret = tcx_run(entry, skb, false); 4340 if (sch_ret != TC_ACT_UNSPEC) 4341 goto egress_verdict; 4342 } 4343 sch_ret = tc_run(tcx_entry(entry), skb, &drop_reason); 4344 egress_verdict: 4345 switch (sch_ret) { 4346 case TC_ACT_REDIRECT: 4347 /* No need to push/pop skb's mac_header here on egress! */ 4348 skb_do_redirect(skb); 4349 *ret = NET_XMIT_SUCCESS; 4350 bpf_net_ctx_clear(bpf_net_ctx); 4351 return NULL; 4352 case TC_ACT_SHOT: 4353 kfree_skb_reason(skb, drop_reason); 4354 *ret = NET_XMIT_DROP; 4355 bpf_net_ctx_clear(bpf_net_ctx); 4356 return NULL; 4357 /* used by tc_run */ 4358 case TC_ACT_STOLEN: 4359 case TC_ACT_QUEUED: 4360 case TC_ACT_TRAP: 4361 consume_skb(skb); 4362 fallthrough; 4363 case TC_ACT_CONSUMED: 4364 *ret = NET_XMIT_SUCCESS; 4365 bpf_net_ctx_clear(bpf_net_ctx); 4366 return NULL; 4367 } 4368 bpf_net_ctx_clear(bpf_net_ctx); 4369 4370 return skb; 4371 } 4372 #else 4373 static __always_inline struct sk_buff * 4374 sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, 4375 struct net_device *orig_dev, bool *another) 4376 { 4377 return skb; 4378 } 4379 4380 static __always_inline struct sk_buff * 4381 sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) 4382 { 4383 return skb; 4384 } 4385 #endif /* CONFIG_NET_XGRESS */ 4386 4387 #ifdef CONFIG_XPS 4388 static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb, 4389 struct xps_dev_maps *dev_maps, unsigned int tci) 4390 { 4391 int tc = netdev_get_prio_tc_map(dev, skb->priority); 4392 struct xps_map *map; 4393 int queue_index = -1; 4394 4395 if (tc >= dev_maps->num_tc || tci >= dev_maps->nr_ids) 4396 return queue_index; 4397 4398 tci *= dev_maps->num_tc; 4399 tci += tc; 4400 4401 map = rcu_dereference(dev_maps->attr_map[tci]); 4402 if (map) { 4403 if (map->len == 1) 4404 queue_index = map->queues[0]; 4405 else 4406 queue_index = map->queues[reciprocal_scale( 4407 skb_get_hash(skb), map->len)]; 4408 if (unlikely(queue_index >= dev->real_num_tx_queues)) 4409 queue_index = -1; 4410 } 4411 return queue_index; 4412 } 4413 #endif 4414 4415 static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev, 4416 struct sk_buff *skb) 4417 { 4418 #ifdef CONFIG_XPS 4419 struct xps_dev_maps *dev_maps; 4420 struct sock *sk = skb->sk; 4421 int queue_index = -1; 4422 4423 if (!static_key_false(&xps_needed)) 4424 return -1; 4425 4426 rcu_read_lock(); 4427 if (!static_key_false(&xps_rxqs_needed)) 4428 goto get_cpus_map; 4429 4430 dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_RXQS]); 4431 if (dev_maps) { 4432 int tci = sk_rx_queue_get(sk); 4433 4434 if (tci >= 0) 4435 queue_index = __get_xps_queue_idx(dev, skb, dev_maps, 4436 tci); 4437 } 4438 4439 get_cpus_map: 4440 if (queue_index < 0) { 4441 dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_CPUS]); 4442 if (dev_maps) { 4443 unsigned int tci = skb->sender_cpu - 1; 4444 4445 queue_index = __get_xps_queue_idx(dev, skb, dev_maps, 4446 tci); 4447 } 4448 } 4449 rcu_read_unlock(); 4450 4451 return queue_index; 4452 #else 4453 return -1; 4454 #endif 4455 } 4456 4457 u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb, 4458 struct net_device *sb_dev) 4459 { 4460 return 0; 4461 } 4462 EXPORT_SYMBOL(dev_pick_tx_zero); 4463 4464 u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb, 4465 struct net_device *sb_dev) 4466 { 4467 struct sock *sk = skb->sk; 4468 int queue_index = sk_tx_queue_get(sk); 4469 4470 sb_dev = sb_dev ? : dev; 4471 4472 if (queue_index < 0 || skb->ooo_okay || 4473 queue_index >= dev->real_num_tx_queues) { 4474 int new_index = get_xps_queue(dev, sb_dev, skb); 4475 4476 if (new_index < 0) 4477 new_index = skb_tx_hash(dev, sb_dev, skb); 4478 4479 if (queue_index != new_index && sk && 4480 sk_fullsock(sk) && 4481 rcu_access_pointer(sk->sk_dst_cache)) 4482 sk_tx_queue_set(sk, new_index); 4483 4484 queue_index = new_index; 4485 } 4486 4487 return queue_index; 4488 } 4489 EXPORT_SYMBOL(netdev_pick_tx); 4490 4491 struct netdev_queue *netdev_core_pick_tx(struct net_device *dev, 4492 struct sk_buff *skb, 4493 struct net_device *sb_dev) 4494 { 4495 int queue_index = 0; 4496 4497 #ifdef CONFIG_XPS 4498 u32 sender_cpu = skb->sender_cpu - 1; 4499 4500 if (sender_cpu >= (u32)NR_CPUS) 4501 skb->sender_cpu = raw_smp_processor_id() + 1; 4502 #endif 4503 4504 if (dev->real_num_tx_queues != 1) { 4505 const struct net_device_ops *ops = dev->netdev_ops; 4506 4507 if (ops->ndo_select_queue) 4508 queue_index = ops->ndo_select_queue(dev, skb, sb_dev); 4509 else 4510 queue_index = netdev_pick_tx(dev, skb, sb_dev); 4511 4512 queue_index = netdev_cap_txqueue(dev, queue_index); 4513 } 4514 4515 skb_set_queue_mapping(skb, queue_index); 4516 return netdev_get_tx_queue(dev, queue_index); 4517 } 4518 4519 /** 4520 * __dev_queue_xmit() - transmit a buffer 4521 * @skb: buffer to transmit 4522 * @sb_dev: suboordinate device used for L2 forwarding offload 4523 * 4524 * Queue a buffer for transmission to a network device. The caller must 4525 * have set the device and priority and built the buffer before calling 4526 * this function. The function can be called from an interrupt. 4527 * 4528 * When calling this method, interrupts MUST be enabled. This is because 4529 * the BH enable code must have IRQs enabled so that it will not deadlock. 4530 * 4531 * Regardless of the return value, the skb is consumed, so it is currently 4532 * difficult to retry a send to this method. (You can bump the ref count 4533 * before sending to hold a reference for retry if you are careful.) 4534 * 4535 * Return: 4536 * * 0 - buffer successfully transmitted 4537 * * positive qdisc return code - NET_XMIT_DROP etc. 4538 * * negative errno - other errors 4539 */ 4540 int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) 4541 { 4542 struct net_device *dev = skb->dev; 4543 struct netdev_queue *txq = NULL; 4544 struct Qdisc *q; 4545 int rc = -ENOMEM; 4546 bool again = false; 4547 4548 skb_reset_mac_header(skb); 4549 skb_assert_len(skb); 4550 4551 if (unlikely(skb_shinfo(skb)->tx_flags & 4552 (SKBTX_SCHED_TSTAMP | SKBTX_BPF))) 4553 __skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED); 4554 4555 /* Disable soft irqs for various locks below. Also 4556 * stops preemption for RCU. 4557 */ 4558 rcu_read_lock_bh(); 4559 4560 skb_update_prio(skb); 4561 4562 qdisc_pkt_len_init(skb); 4563 tcx_set_ingress(skb, false); 4564 #ifdef CONFIG_NET_EGRESS 4565 if (static_branch_unlikely(&egress_needed_key)) { 4566 if (nf_hook_egress_active()) { 4567 skb = nf_hook_egress(skb, &rc, dev); 4568 if (!skb) 4569 goto out; 4570 } 4571 4572 netdev_xmit_skip_txqueue(false); 4573 4574 nf_skip_egress(skb, true); 4575 skb = sch_handle_egress(skb, &rc, dev); 4576 if (!skb) 4577 goto out; 4578 nf_skip_egress(skb, false); 4579 4580 if (netdev_xmit_txqueue_skipped()) 4581 txq = netdev_tx_queue_mapping(dev, skb); 4582 } 4583 #endif 4584 /* If device/qdisc don't need skb->dst, release it right now while 4585 * its hot in this cpu cache. 4586 */ 4587 if (dev->priv_flags & IFF_XMIT_DST_RELEASE) 4588 skb_dst_drop(skb); 4589 else 4590 skb_dst_force(skb); 4591 4592 if (!txq) 4593 txq = netdev_core_pick_tx(dev, skb, sb_dev); 4594 4595 q = rcu_dereference_bh(txq->qdisc); 4596 4597 trace_net_dev_queue(skb); 4598 if (q->enqueue) { 4599 rc = __dev_xmit_skb(skb, q, dev, txq); 4600 goto out; 4601 } 4602 4603 /* The device has no queue. Common case for software devices: 4604 * loopback, all the sorts of tunnels... 4605 4606 * Really, it is unlikely that netif_tx_lock protection is necessary 4607 * here. (f.e. loopback and IP tunnels are clean ignoring statistics 4608 * counters.) 4609 * However, it is possible, that they rely on protection 4610 * made by us here. 4611 4612 * Check this and shot the lock. It is not prone from deadlocks. 4613 *Either shot noqueue qdisc, it is even simpler 8) 4614 */ 4615 if (dev->flags & IFF_UP) { 4616 int cpu = smp_processor_id(); /* ok because BHs are off */ 4617 4618 /* Other cpus might concurrently change txq->xmit_lock_owner 4619 * to -1 or to their cpu id, but not to our id. 4620 */ 4621 if (READ_ONCE(txq->xmit_lock_owner) != cpu) { 4622 if (dev_xmit_recursion()) 4623 goto recursion_alert; 4624 4625 skb = validate_xmit_skb(skb, dev, &again); 4626 if (!skb) 4627 goto out; 4628 4629 HARD_TX_LOCK(dev, txq, cpu); 4630 4631 if (!netif_xmit_stopped(txq)) { 4632 dev_xmit_recursion_inc(); 4633 skb = dev_hard_start_xmit(skb, dev, txq, &rc); 4634 dev_xmit_recursion_dec(); 4635 if (dev_xmit_complete(rc)) { 4636 HARD_TX_UNLOCK(dev, txq); 4637 goto out; 4638 } 4639 } 4640 HARD_TX_UNLOCK(dev, txq); 4641 net_crit_ratelimited("Virtual device %s asks to queue packet!\n", 4642 dev->name); 4643 } else { 4644 /* Recursion is detected! It is possible, 4645 * unfortunately 4646 */ 4647 recursion_alert: 4648 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", 4649 dev->name); 4650 } 4651 } 4652 4653 rc = -ENETDOWN; 4654 rcu_read_unlock_bh(); 4655 4656 dev_core_stats_tx_dropped_inc(dev); 4657 kfree_skb_list(skb); 4658 return rc; 4659 out: 4660 rcu_read_unlock_bh(); 4661 return rc; 4662 } 4663 EXPORT_SYMBOL(__dev_queue_xmit); 4664 4665 int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id) 4666 { 4667 struct net_device *dev = skb->dev; 4668 struct sk_buff *orig_skb = skb; 4669 struct netdev_queue *txq; 4670 int ret = NETDEV_TX_BUSY; 4671 bool again = false; 4672 4673 if (unlikely(!netif_running(dev) || 4674 !netif_carrier_ok(dev))) 4675 goto drop; 4676 4677 skb = validate_xmit_skb_list(skb, dev, &again); 4678 if (skb != orig_skb) 4679 goto drop; 4680 4681 skb_set_queue_mapping(skb, queue_id); 4682 txq = skb_get_tx_queue(dev, skb); 4683 4684 local_bh_disable(); 4685 4686 dev_xmit_recursion_inc(); 4687 HARD_TX_LOCK(dev, txq, smp_processor_id()); 4688 if (!netif_xmit_frozen_or_drv_stopped(txq)) 4689 ret = netdev_start_xmit(skb, dev, txq, false); 4690 HARD_TX_UNLOCK(dev, txq); 4691 dev_xmit_recursion_dec(); 4692 4693 local_bh_enable(); 4694 return ret; 4695 drop: 4696 dev_core_stats_tx_dropped_inc(dev); 4697 kfree_skb_list(skb); 4698 return NET_XMIT_DROP; 4699 } 4700 EXPORT_SYMBOL(__dev_direct_xmit); 4701 4702 /************************************************************************* 4703 * Receiver routines 4704 *************************************************************************/ 4705 static DEFINE_PER_CPU(struct task_struct *, backlog_napi); 4706 4707 int weight_p __read_mostly = 64; /* old backlog weight */ 4708 int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */ 4709 int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */ 4710 4711 /* Called with irq disabled */ 4712 static inline void ____napi_schedule(struct softnet_data *sd, 4713 struct napi_struct *napi) 4714 { 4715 struct task_struct *thread; 4716 4717 lockdep_assert_irqs_disabled(); 4718 4719 if (test_bit(NAPI_STATE_THREADED, &napi->state)) { 4720 /* Paired with smp_mb__before_atomic() in 4721 * napi_enable()/dev_set_threaded(). 4722 * Use READ_ONCE() to guarantee a complete 4723 * read on napi->thread. Only call 4724 * wake_up_process() when it's not NULL. 4725 */ 4726 thread = READ_ONCE(napi->thread); 4727 if (thread) { 4728 if (use_backlog_threads() && thread == raw_cpu_read(backlog_napi)) 4729 goto use_local_napi; 4730 4731 set_bit(NAPI_STATE_SCHED_THREADED, &napi->state); 4732 wake_up_process(thread); 4733 return; 4734 } 4735 } 4736 4737 use_local_napi: 4738 list_add_tail(&napi->poll_list, &sd->poll_list); 4739 WRITE_ONCE(napi->list_owner, smp_processor_id()); 4740 /* If not called from net_rx_action() 4741 * we have to raise NET_RX_SOFTIRQ. 4742 */ 4743 if (!sd->in_net_rx_action) 4744 raise_softirq_irqoff(NET_RX_SOFTIRQ); 4745 } 4746 4747 #ifdef CONFIG_RPS 4748 4749 struct static_key_false rps_needed __read_mostly; 4750 EXPORT_SYMBOL(rps_needed); 4751 struct static_key_false rfs_needed __read_mostly; 4752 EXPORT_SYMBOL(rfs_needed); 4753 4754 static u32 rfs_slot(u32 hash, const struct rps_dev_flow_table *flow_table) 4755 { 4756 return hash_32(hash, flow_table->log); 4757 } 4758 4759 static struct rps_dev_flow * 4760 set_rps_cpu(struct net_device *dev, struct sk_buff *skb, 4761 struct rps_dev_flow *rflow, u16 next_cpu) 4762 { 4763 if (next_cpu < nr_cpu_ids) { 4764 u32 head; 4765 #ifdef CONFIG_RFS_ACCEL 4766 struct netdev_rx_queue *rxqueue; 4767 struct rps_dev_flow_table *flow_table; 4768 struct rps_dev_flow *old_rflow; 4769 u16 rxq_index; 4770 u32 flow_id; 4771 int rc; 4772 4773 /* Should we steer this flow to a different hardware queue? */ 4774 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || 4775 !(dev->features & NETIF_F_NTUPLE)) 4776 goto out; 4777 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); 4778 if (rxq_index == skb_get_rx_queue(skb)) 4779 goto out; 4780 4781 rxqueue = dev->_rx + rxq_index; 4782 flow_table = rcu_dereference(rxqueue->rps_flow_table); 4783 if (!flow_table) 4784 goto out; 4785 flow_id = rfs_slot(skb_get_hash(skb), flow_table); 4786 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, 4787 rxq_index, flow_id); 4788 if (rc < 0) 4789 goto out; 4790 old_rflow = rflow; 4791 rflow = &flow_table->flows[flow_id]; 4792 WRITE_ONCE(rflow->filter, rc); 4793 if (old_rflow->filter == rc) 4794 WRITE_ONCE(old_rflow->filter, RPS_NO_FILTER); 4795 out: 4796 #endif 4797 head = READ_ONCE(per_cpu(softnet_data, next_cpu).input_queue_head); 4798 rps_input_queue_tail_save(&rflow->last_qtail, head); 4799 } 4800 4801 WRITE_ONCE(rflow->cpu, next_cpu); 4802 return rflow; 4803 } 4804 4805 /* 4806 * get_rps_cpu is called from netif_receive_skb and returns the target 4807 * CPU from the RPS map of the receiving queue for a given skb. 4808 * rcu_read_lock must be held on entry. 4809 */ 4810 static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, 4811 struct rps_dev_flow **rflowp) 4812 { 4813 const struct rps_sock_flow_table *sock_flow_table; 4814 struct netdev_rx_queue *rxqueue = dev->_rx; 4815 struct rps_dev_flow_table *flow_table; 4816 struct rps_map *map; 4817 int cpu = -1; 4818 u32 tcpu; 4819 u32 hash; 4820 4821 if (skb_rx_queue_recorded(skb)) { 4822 u16 index = skb_get_rx_queue(skb); 4823 4824 if (unlikely(index >= dev->real_num_rx_queues)) { 4825 WARN_ONCE(dev->real_num_rx_queues > 1, 4826 "%s received packet on queue %u, but number " 4827 "of RX queues is %u\n", 4828 dev->name, index, dev->real_num_rx_queues); 4829 goto done; 4830 } 4831 rxqueue += index; 4832 } 4833 4834 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */ 4835 4836 flow_table = rcu_dereference(rxqueue->rps_flow_table); 4837 map = rcu_dereference(rxqueue->rps_map); 4838 if (!flow_table && !map) 4839 goto done; 4840 4841 skb_reset_network_header(skb); 4842 hash = skb_get_hash(skb); 4843 if (!hash) 4844 goto done; 4845 4846 sock_flow_table = rcu_dereference(net_hotdata.rps_sock_flow_table); 4847 if (flow_table && sock_flow_table) { 4848 struct rps_dev_flow *rflow; 4849 u32 next_cpu; 4850 u32 ident; 4851 4852 /* First check into global flow table if there is a match. 4853 * This READ_ONCE() pairs with WRITE_ONCE() from rps_record_sock_flow(). 4854 */ 4855 ident = READ_ONCE(sock_flow_table->ents[hash & sock_flow_table->mask]); 4856 if ((ident ^ hash) & ~net_hotdata.rps_cpu_mask) 4857 goto try_rps; 4858 4859 next_cpu = ident & net_hotdata.rps_cpu_mask; 4860 4861 /* OK, now we know there is a match, 4862 * we can look at the local (per receive queue) flow table 4863 */ 4864 rflow = &flow_table->flows[rfs_slot(hash, flow_table)]; 4865 tcpu = rflow->cpu; 4866 4867 /* 4868 * If the desired CPU (where last recvmsg was done) is 4869 * different from current CPU (one in the rx-queue flow 4870 * table entry), switch if one of the following holds: 4871 * - Current CPU is unset (>= nr_cpu_ids). 4872 * - Current CPU is offline. 4873 * - The current CPU's queue tail has advanced beyond the 4874 * last packet that was enqueued using this table entry. 4875 * This guarantees that all previous packets for the flow 4876 * have been dequeued, thus preserving in order delivery. 4877 */ 4878 if (unlikely(tcpu != next_cpu) && 4879 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) || 4880 ((int)(READ_ONCE(per_cpu(softnet_data, tcpu).input_queue_head) - 4881 rflow->last_qtail)) >= 0)) { 4882 tcpu = next_cpu; 4883 rflow = set_rps_cpu(dev, skb, rflow, next_cpu); 4884 } 4885 4886 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) { 4887 *rflowp = rflow; 4888 cpu = tcpu; 4889 goto done; 4890 } 4891 } 4892 4893 try_rps: 4894 4895 if (map) { 4896 tcpu = map->cpus[reciprocal_scale(hash, map->len)]; 4897 if (cpu_online(tcpu)) { 4898 cpu = tcpu; 4899 goto done; 4900 } 4901 } 4902 4903 done: 4904 return cpu; 4905 } 4906 4907 #ifdef CONFIG_RFS_ACCEL 4908 4909 /** 4910 * rps_may_expire_flow - check whether an RFS hardware filter may be removed 4911 * @dev: Device on which the filter was set 4912 * @rxq_index: RX queue index 4913 * @flow_id: Flow ID passed to ndo_rx_flow_steer() 4914 * @filter_id: Filter ID returned by ndo_rx_flow_steer() 4915 * 4916 * Drivers that implement ndo_rx_flow_steer() should periodically call 4917 * this function for each installed filter and remove the filters for 4918 * which it returns %true. 4919 */ 4920 bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, 4921 u32 flow_id, u16 filter_id) 4922 { 4923 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; 4924 struct rps_dev_flow_table *flow_table; 4925 struct rps_dev_flow *rflow; 4926 bool expire = true; 4927 unsigned int cpu; 4928 4929 rcu_read_lock(); 4930 flow_table = rcu_dereference(rxqueue->rps_flow_table); 4931 if (flow_table && flow_id < (1UL << flow_table->log)) { 4932 rflow = &flow_table->flows[flow_id]; 4933 cpu = READ_ONCE(rflow->cpu); 4934 if (READ_ONCE(rflow->filter) == filter_id && cpu < nr_cpu_ids && 4935 ((int)(READ_ONCE(per_cpu(softnet_data, cpu).input_queue_head) - 4936 READ_ONCE(rflow->last_qtail)) < 4937 (int)(10 << flow_table->log))) 4938 expire = false; 4939 } 4940 rcu_read_unlock(); 4941 return expire; 4942 } 4943 EXPORT_SYMBOL(rps_may_expire_flow); 4944 4945 #endif /* CONFIG_RFS_ACCEL */ 4946 4947 /* Called from hardirq (IPI) context */ 4948 static void rps_trigger_softirq(void *data) 4949 { 4950 struct softnet_data *sd = data; 4951 4952 ____napi_schedule(sd, &sd->backlog); 4953 sd->received_rps++; 4954 } 4955 4956 #endif /* CONFIG_RPS */ 4957 4958 /* Called from hardirq (IPI) context */ 4959 static void trigger_rx_softirq(void *data) 4960 { 4961 struct softnet_data *sd = data; 4962 4963 __raise_softirq_irqoff(NET_RX_SOFTIRQ); 4964 smp_store_release(&sd->defer_ipi_scheduled, 0); 4965 } 4966 4967 /* 4968 * After we queued a packet into sd->input_pkt_queue, 4969 * we need to make sure this queue is serviced soon. 4970 * 4971 * - If this is another cpu queue, link it to our rps_ipi_list, 4972 * and make sure we will process rps_ipi_list from net_rx_action(). 4973 * 4974 * - If this is our own queue, NAPI schedule our backlog. 4975 * Note that this also raises NET_RX_SOFTIRQ. 4976 */ 4977 static void napi_schedule_rps(struct softnet_data *sd) 4978 { 4979 struct softnet_data *mysd = this_cpu_ptr(&softnet_data); 4980 4981 #ifdef CONFIG_RPS 4982 if (sd != mysd) { 4983 if (use_backlog_threads()) { 4984 __napi_schedule_irqoff(&sd->backlog); 4985 return; 4986 } 4987 4988 sd->rps_ipi_next = mysd->rps_ipi_list; 4989 mysd->rps_ipi_list = sd; 4990 4991 /* If not called from net_rx_action() or napi_threaded_poll() 4992 * we have to raise NET_RX_SOFTIRQ. 4993 */ 4994 if (!mysd->in_net_rx_action && !mysd->in_napi_threaded_poll) 4995 __raise_softirq_irqoff(NET_RX_SOFTIRQ); 4996 return; 4997 } 4998 #endif /* CONFIG_RPS */ 4999 __napi_schedule_irqoff(&mysd->backlog); 5000 } 5001 5002 void kick_defer_list_purge(struct softnet_data *sd, unsigned int cpu) 5003 { 5004 unsigned long flags; 5005 5006 if (use_backlog_threads()) { 5007 backlog_lock_irq_save(sd, &flags); 5008 5009 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) 5010 __napi_schedule_irqoff(&sd->backlog); 5011 5012 backlog_unlock_irq_restore(sd, &flags); 5013 5014 } else if (!cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) { 5015 smp_call_function_single_async(cpu, &sd->defer_csd); 5016 } 5017 } 5018 5019 #ifdef CONFIG_NET_FLOW_LIMIT 5020 int netdev_flow_limit_table_len __read_mostly = (1 << 12); 5021 #endif 5022 5023 static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) 5024 { 5025 #ifdef CONFIG_NET_FLOW_LIMIT 5026 struct sd_flow_limit *fl; 5027 struct softnet_data *sd; 5028 unsigned int old_flow, new_flow; 5029 5030 if (qlen < (READ_ONCE(net_hotdata.max_backlog) >> 1)) 5031 return false; 5032 5033 sd = this_cpu_ptr(&softnet_data); 5034 5035 rcu_read_lock(); 5036 fl = rcu_dereference(sd->flow_limit); 5037 if (fl) { 5038 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1); 5039 old_flow = fl->history[fl->history_head]; 5040 fl->history[fl->history_head] = new_flow; 5041 5042 fl->history_head++; 5043 fl->history_head &= FLOW_LIMIT_HISTORY - 1; 5044 5045 if (likely(fl->buckets[old_flow])) 5046 fl->buckets[old_flow]--; 5047 5048 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { 5049 fl->count++; 5050 rcu_read_unlock(); 5051 return true; 5052 } 5053 } 5054 rcu_read_unlock(); 5055 #endif 5056 return false; 5057 } 5058 5059 /* 5060 * enqueue_to_backlog is called to queue an skb to a per CPU backlog 5061 * queue (may be a remote CPU queue). 5062 */ 5063 static int enqueue_to_backlog(struct sk_buff *skb, int cpu, 5064 unsigned int *qtail) 5065 { 5066 enum skb_drop_reason reason; 5067 struct softnet_data *sd; 5068 unsigned long flags; 5069 unsigned int qlen; 5070 int max_backlog; 5071 u32 tail; 5072 5073 reason = SKB_DROP_REASON_DEV_READY; 5074 if (!netif_running(skb->dev)) 5075 goto bad_dev; 5076 5077 reason = SKB_DROP_REASON_CPU_BACKLOG; 5078 sd = &per_cpu(softnet_data, cpu); 5079 5080 qlen = skb_queue_len_lockless(&sd->input_pkt_queue); 5081 max_backlog = READ_ONCE(net_hotdata.max_backlog); 5082 if (unlikely(qlen > max_backlog)) 5083 goto cpu_backlog_drop; 5084 backlog_lock_irq_save(sd, &flags); 5085 qlen = skb_queue_len(&sd->input_pkt_queue); 5086 if (qlen <= max_backlog && !skb_flow_limit(skb, qlen)) { 5087 if (!qlen) { 5088 /* Schedule NAPI for backlog device. We can use 5089 * non atomic operation as we own the queue lock. 5090 */ 5091 if (!__test_and_set_bit(NAPI_STATE_SCHED, 5092 &sd->backlog.state)) 5093 napi_schedule_rps(sd); 5094 } 5095 __skb_queue_tail(&sd->input_pkt_queue, skb); 5096 tail = rps_input_queue_tail_incr(sd); 5097 backlog_unlock_irq_restore(sd, &flags); 5098 5099 /* save the tail outside of the critical section */ 5100 rps_input_queue_tail_save(qtail, tail); 5101 return NET_RX_SUCCESS; 5102 } 5103 5104 backlog_unlock_irq_restore(sd, &flags); 5105 5106 cpu_backlog_drop: 5107 atomic_inc(&sd->dropped); 5108 bad_dev: 5109 dev_core_stats_rx_dropped_inc(skb->dev); 5110 kfree_skb_reason(skb, reason); 5111 return NET_RX_DROP; 5112 } 5113 5114 static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) 5115 { 5116 struct net_device *dev = skb->dev; 5117 struct netdev_rx_queue *rxqueue; 5118 5119 rxqueue = dev->_rx; 5120 5121 if (skb_rx_queue_recorded(skb)) { 5122 u16 index = skb_get_rx_queue(skb); 5123 5124 if (unlikely(index >= dev->real_num_rx_queues)) { 5125 WARN_ONCE(dev->real_num_rx_queues > 1, 5126 "%s received packet on queue %u, but number " 5127 "of RX queues is %u\n", 5128 dev->name, index, dev->real_num_rx_queues); 5129 5130 return rxqueue; /* Return first rxqueue */ 5131 } 5132 rxqueue += index; 5133 } 5134 return rxqueue; 5135 } 5136 5137 u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp, 5138 const struct bpf_prog *xdp_prog) 5139 { 5140 void *orig_data, *orig_data_end, *hard_start; 5141 struct netdev_rx_queue *rxqueue; 5142 bool orig_bcast, orig_host; 5143 u32 mac_len, frame_sz; 5144 __be16 orig_eth_type; 5145 struct ethhdr *eth; 5146 u32 metalen, act; 5147 int off; 5148 5149 /* The XDP program wants to see the packet starting at the MAC 5150 * header. 5151 */ 5152 mac_len = skb->data - skb_mac_header(skb); 5153 hard_start = skb->data - skb_headroom(skb); 5154 5155 /* SKB "head" area always have tailroom for skb_shared_info */ 5156 frame_sz = (void *)skb_end_pointer(skb) - hard_start; 5157 frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5158 5159 rxqueue = netif_get_rxqueue(skb); 5160 xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq); 5161 xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len, 5162 skb_headlen(skb) + mac_len, true); 5163 if (skb_is_nonlinear(skb)) { 5164 skb_shinfo(skb)->xdp_frags_size = skb->data_len; 5165 xdp_buff_set_frags_flag(xdp); 5166 } else { 5167 xdp_buff_clear_frags_flag(xdp); 5168 } 5169 5170 orig_data_end = xdp->data_end; 5171 orig_data = xdp->data; 5172 eth = (struct ethhdr *)xdp->data; 5173 orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr); 5174 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); 5175 orig_eth_type = eth->h_proto; 5176 5177 act = bpf_prog_run_xdp(xdp_prog, xdp); 5178 5179 /* check if bpf_xdp_adjust_head was used */ 5180 off = xdp->data - orig_data; 5181 if (off) { 5182 if (off > 0) 5183 __skb_pull(skb, off); 5184 else if (off < 0) 5185 __skb_push(skb, -off); 5186 5187 skb->mac_header += off; 5188 skb_reset_network_header(skb); 5189 } 5190 5191 /* check if bpf_xdp_adjust_tail was used */ 5192 off = xdp->data_end - orig_data_end; 5193 if (off != 0) { 5194 skb_set_tail_pointer(skb, xdp->data_end - xdp->data); 5195 skb->len += off; /* positive on grow, negative on shrink */ 5196 } 5197 5198 /* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers 5199 * (e.g. bpf_xdp_adjust_tail), we need to update data_len here. 5200 */ 5201 if (xdp_buff_has_frags(xdp)) 5202 skb->data_len = skb_shinfo(skb)->xdp_frags_size; 5203 else 5204 skb->data_len = 0; 5205 5206 /* check if XDP changed eth hdr such SKB needs update */ 5207 eth = (struct ethhdr *)xdp->data; 5208 if ((orig_eth_type != eth->h_proto) || 5209 (orig_host != ether_addr_equal_64bits(eth->h_dest, 5210 skb->dev->dev_addr)) || 5211 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { 5212 __skb_push(skb, ETH_HLEN); 5213 skb->pkt_type = PACKET_HOST; 5214 skb->protocol = eth_type_trans(skb, skb->dev); 5215 } 5216 5217 /* Redirect/Tx gives L2 packet, code that will reuse skb must __skb_pull 5218 * before calling us again on redirect path. We do not call do_redirect 5219 * as we leave that up to the caller. 5220 * 5221 * Caller is responsible for managing lifetime of skb (i.e. calling 5222 * kfree_skb in response to actions it cannot handle/XDP_DROP). 5223 */ 5224 switch (act) { 5225 case XDP_REDIRECT: 5226 case XDP_TX: 5227 __skb_push(skb, mac_len); 5228 break; 5229 case XDP_PASS: 5230 metalen = xdp->data - xdp->data_meta; 5231 if (metalen) 5232 skb_metadata_set(skb, metalen); 5233 break; 5234 } 5235 5236 return act; 5237 } 5238 5239 static int 5240 netif_skb_check_for_xdp(struct sk_buff **pskb, const struct bpf_prog *prog) 5241 { 5242 struct sk_buff *skb = *pskb; 5243 int err, hroom, troom; 5244 5245 if (!skb_cow_data_for_xdp(this_cpu_read(system_page_pool), pskb, prog)) 5246 return 0; 5247 5248 /* In case we have to go down the path and also linearize, 5249 * then lets do the pskb_expand_head() work just once here. 5250 */ 5251 hroom = XDP_PACKET_HEADROOM - skb_headroom(skb); 5252 troom = skb->tail + skb->data_len - skb->end; 5253 err = pskb_expand_head(skb, 5254 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0, 5255 troom > 0 ? troom + 128 : 0, GFP_ATOMIC); 5256 if (err) 5257 return err; 5258 5259 return skb_linearize(skb); 5260 } 5261 5262 static u32 netif_receive_generic_xdp(struct sk_buff **pskb, 5263 struct xdp_buff *xdp, 5264 const struct bpf_prog *xdp_prog) 5265 { 5266 struct sk_buff *skb = *pskb; 5267 u32 mac_len, act = XDP_DROP; 5268 5269 /* Reinjected packets coming from act_mirred or similar should 5270 * not get XDP generic processing. 5271 */ 5272 if (skb_is_redirected(skb)) 5273 return XDP_PASS; 5274 5275 /* XDP packets must have sufficient headroom of XDP_PACKET_HEADROOM 5276 * bytes. This is the guarantee that also native XDP provides, 5277 * thus we need to do it here as well. 5278 */ 5279 mac_len = skb->data - skb_mac_header(skb); 5280 __skb_push(skb, mac_len); 5281 5282 if (skb_cloned(skb) || skb_is_nonlinear(skb) || 5283 skb_headroom(skb) < XDP_PACKET_HEADROOM) { 5284 if (netif_skb_check_for_xdp(pskb, xdp_prog)) 5285 goto do_drop; 5286 } 5287 5288 __skb_pull(*pskb, mac_len); 5289 5290 act = bpf_prog_run_generic_xdp(*pskb, xdp, xdp_prog); 5291 switch (act) { 5292 case XDP_REDIRECT: 5293 case XDP_TX: 5294 case XDP_PASS: 5295 break; 5296 default: 5297 bpf_warn_invalid_xdp_action((*pskb)->dev, xdp_prog, act); 5298 fallthrough; 5299 case XDP_ABORTED: 5300 trace_xdp_exception((*pskb)->dev, xdp_prog, act); 5301 fallthrough; 5302 case XDP_DROP: 5303 do_drop: 5304 kfree_skb(*pskb); 5305 break; 5306 } 5307 5308 return act; 5309 } 5310 5311 /* When doing generic XDP we have to bypass the qdisc layer and the 5312 * network taps in order to match in-driver-XDP behavior. This also means 5313 * that XDP packets are able to starve other packets going through a qdisc, 5314 * and DDOS attacks will be more effective. In-driver-XDP use dedicated TX 5315 * queues, so they do not have this starvation issue. 5316 */ 5317 void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog) 5318 { 5319 struct net_device *dev = skb->dev; 5320 struct netdev_queue *txq; 5321 bool free_skb = true; 5322 int cpu, rc; 5323 5324 txq = netdev_core_pick_tx(dev, skb, NULL); 5325 cpu = smp_processor_id(); 5326 HARD_TX_LOCK(dev, txq, cpu); 5327 if (!netif_xmit_frozen_or_drv_stopped(txq)) { 5328 rc = netdev_start_xmit(skb, dev, txq, 0); 5329 if (dev_xmit_complete(rc)) 5330 free_skb = false; 5331 } 5332 HARD_TX_UNLOCK(dev, txq); 5333 if (free_skb) { 5334 trace_xdp_exception(dev, xdp_prog, XDP_TX); 5335 dev_core_stats_tx_dropped_inc(dev); 5336 kfree_skb(skb); 5337 } 5338 } 5339 5340 static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); 5341 5342 int do_xdp_generic(const struct bpf_prog *xdp_prog, struct sk_buff **pskb) 5343 { 5344 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 5345 5346 if (xdp_prog) { 5347 struct xdp_buff xdp; 5348 u32 act; 5349 int err; 5350 5351 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 5352 act = netif_receive_generic_xdp(pskb, &xdp, xdp_prog); 5353 if (act != XDP_PASS) { 5354 switch (act) { 5355 case XDP_REDIRECT: 5356 err = xdp_do_generic_redirect((*pskb)->dev, *pskb, 5357 &xdp, xdp_prog); 5358 if (err) 5359 goto out_redir; 5360 break; 5361 case XDP_TX: 5362 generic_xdp_tx(*pskb, xdp_prog); 5363 break; 5364 } 5365 bpf_net_ctx_clear(bpf_net_ctx); 5366 return XDP_DROP; 5367 } 5368 bpf_net_ctx_clear(bpf_net_ctx); 5369 } 5370 return XDP_PASS; 5371 out_redir: 5372 bpf_net_ctx_clear(bpf_net_ctx); 5373 kfree_skb_reason(*pskb, SKB_DROP_REASON_XDP); 5374 return XDP_DROP; 5375 } 5376 EXPORT_SYMBOL_GPL(do_xdp_generic); 5377 5378 static int netif_rx_internal(struct sk_buff *skb) 5379 { 5380 int ret; 5381 5382 net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb); 5383 5384 trace_netif_rx(skb); 5385 5386 #ifdef CONFIG_RPS 5387 if (static_branch_unlikely(&rps_needed)) { 5388 struct rps_dev_flow voidflow, *rflow = &voidflow; 5389 int cpu; 5390 5391 rcu_read_lock(); 5392 5393 cpu = get_rps_cpu(skb->dev, skb, &rflow); 5394 if (cpu < 0) 5395 cpu = smp_processor_id(); 5396 5397 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); 5398 5399 rcu_read_unlock(); 5400 } else 5401 #endif 5402 { 5403 unsigned int qtail; 5404 5405 ret = enqueue_to_backlog(skb, smp_processor_id(), &qtail); 5406 } 5407 return ret; 5408 } 5409 5410 /** 5411 * __netif_rx - Slightly optimized version of netif_rx 5412 * @skb: buffer to post 5413 * 5414 * This behaves as netif_rx except that it does not disable bottom halves. 5415 * As a result this function may only be invoked from the interrupt context 5416 * (either hard or soft interrupt). 5417 */ 5418 int __netif_rx(struct sk_buff *skb) 5419 { 5420 int ret; 5421 5422 lockdep_assert_once(hardirq_count() | softirq_count()); 5423 5424 trace_netif_rx_entry(skb); 5425 ret = netif_rx_internal(skb); 5426 trace_netif_rx_exit(ret); 5427 return ret; 5428 } 5429 EXPORT_SYMBOL(__netif_rx); 5430 5431 /** 5432 * netif_rx - post buffer to the network code 5433 * @skb: buffer to post 5434 * 5435 * This function receives a packet from a device driver and queues it for 5436 * the upper (protocol) levels to process via the backlog NAPI device. It 5437 * always succeeds. The buffer may be dropped during processing for 5438 * congestion control or by the protocol layers. 5439 * The network buffer is passed via the backlog NAPI device. Modern NIC 5440 * driver should use NAPI and GRO. 5441 * This function can used from interrupt and from process context. The 5442 * caller from process context must not disable interrupts before invoking 5443 * this function. 5444 * 5445 * return values: 5446 * NET_RX_SUCCESS (no congestion) 5447 * NET_RX_DROP (packet was dropped) 5448 * 5449 */ 5450 int netif_rx(struct sk_buff *skb) 5451 { 5452 bool need_bh_off = !(hardirq_count() | softirq_count()); 5453 int ret; 5454 5455 if (need_bh_off) 5456 local_bh_disable(); 5457 trace_netif_rx_entry(skb); 5458 ret = netif_rx_internal(skb); 5459 trace_netif_rx_exit(ret); 5460 if (need_bh_off) 5461 local_bh_enable(); 5462 return ret; 5463 } 5464 EXPORT_SYMBOL(netif_rx); 5465 5466 static __latent_entropy void net_tx_action(void) 5467 { 5468 struct softnet_data *sd = this_cpu_ptr(&softnet_data); 5469 5470 if (sd->completion_queue) { 5471 struct sk_buff *clist; 5472 5473 local_irq_disable(); 5474 clist = sd->completion_queue; 5475 sd->completion_queue = NULL; 5476 local_irq_enable(); 5477 5478 while (clist) { 5479 struct sk_buff *skb = clist; 5480 5481 clist = clist->next; 5482 5483 WARN_ON(refcount_read(&skb->users)); 5484 if (likely(get_kfree_skb_cb(skb)->reason == SKB_CONSUMED)) 5485 trace_consume_skb(skb, net_tx_action); 5486 else 5487 trace_kfree_skb(skb, net_tx_action, 5488 get_kfree_skb_cb(skb)->reason, NULL); 5489 5490 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) 5491 __kfree_skb(skb); 5492 else 5493 __napi_kfree_skb(skb, 5494 get_kfree_skb_cb(skb)->reason); 5495 } 5496 } 5497 5498 if (sd->output_queue) { 5499 struct Qdisc *head; 5500 5501 local_irq_disable(); 5502 head = sd->output_queue; 5503 sd->output_queue = NULL; 5504 sd->output_queue_tailp = &sd->output_queue; 5505 local_irq_enable(); 5506 5507 rcu_read_lock(); 5508 5509 while (head) { 5510 struct Qdisc *q = head; 5511 spinlock_t *root_lock = NULL; 5512 5513 head = head->next_sched; 5514 5515 /* We need to make sure head->next_sched is read 5516 * before clearing __QDISC_STATE_SCHED 5517 */ 5518 smp_mb__before_atomic(); 5519 5520 if (!(q->flags & TCQ_F_NOLOCK)) { 5521 root_lock = qdisc_lock(q); 5522 spin_lock(root_lock); 5523 } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, 5524 &q->state))) { 5525 /* There is a synchronize_net() between 5526 * STATE_DEACTIVATED flag being set and 5527 * qdisc_reset()/some_qdisc_is_busy() in 5528 * dev_deactivate(), so we can safely bail out 5529 * early here to avoid data race between 5530 * qdisc_deactivate() and some_qdisc_is_busy() 5531 * for lockless qdisc. 5532 */ 5533 clear_bit(__QDISC_STATE_SCHED, &q->state); 5534 continue; 5535 } 5536 5537 clear_bit(__QDISC_STATE_SCHED, &q->state); 5538 qdisc_run(q); 5539 if (root_lock) 5540 spin_unlock(root_lock); 5541 } 5542 5543 rcu_read_unlock(); 5544 } 5545 5546 xfrm_dev_backlog(sd); 5547 } 5548 5549 #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE) 5550 /* This hook is defined here for ATM LANE */ 5551 int (*br_fdb_test_addr_hook)(struct net_device *dev, 5552 unsigned char *addr) __read_mostly; 5553 EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); 5554 #endif 5555 5556 /** 5557 * netdev_is_rx_handler_busy - check if receive handler is registered 5558 * @dev: device to check 5559 * 5560 * Check if a receive handler is already registered for a given device. 5561 * Return true if there one. 5562 * 5563 * The caller must hold the rtnl_mutex. 5564 */ 5565 bool netdev_is_rx_handler_busy(struct net_device *dev) 5566 { 5567 ASSERT_RTNL(); 5568 return dev && rtnl_dereference(dev->rx_handler); 5569 } 5570 EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy); 5571 5572 /** 5573 * netdev_rx_handler_register - register receive handler 5574 * @dev: device to register a handler for 5575 * @rx_handler: receive handler to register 5576 * @rx_handler_data: data pointer that is used by rx handler 5577 * 5578 * Register a receive handler for a device. This handler will then be 5579 * called from __netif_receive_skb. A negative errno code is returned 5580 * on a failure. 5581 * 5582 * The caller must hold the rtnl_mutex. 5583 * 5584 * For a general description of rx_handler, see enum rx_handler_result. 5585 */ 5586 int netdev_rx_handler_register(struct net_device *dev, 5587 rx_handler_func_t *rx_handler, 5588 void *rx_handler_data) 5589 { 5590 if (netdev_is_rx_handler_busy(dev)) 5591 return -EBUSY; 5592 5593 if (dev->priv_flags & IFF_NO_RX_HANDLER) 5594 return -EINVAL; 5595 5596 /* Note: rx_handler_data must be set before rx_handler */ 5597 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); 5598 rcu_assign_pointer(dev->rx_handler, rx_handler); 5599 5600 return 0; 5601 } 5602 EXPORT_SYMBOL_GPL(netdev_rx_handler_register); 5603 5604 /** 5605 * netdev_rx_handler_unregister - unregister receive handler 5606 * @dev: device to unregister a handler from 5607 * 5608 * Unregister a receive handler from a device. 5609 * 5610 * The caller must hold the rtnl_mutex. 5611 */ 5612 void netdev_rx_handler_unregister(struct net_device *dev) 5613 { 5614 5615 ASSERT_RTNL(); 5616 RCU_INIT_POINTER(dev->rx_handler, NULL); 5617 /* a reader seeing a non NULL rx_handler in a rcu_read_lock() 5618 * section has a guarantee to see a non NULL rx_handler_data 5619 * as well. 5620 */ 5621 synchronize_net(); 5622 RCU_INIT_POINTER(dev->rx_handler_data, NULL); 5623 } 5624 EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); 5625 5626 /* 5627 * Limit the use of PFMEMALLOC reserves to those protocols that implement 5628 * the special handling of PFMEMALLOC skbs. 5629 */ 5630 static bool skb_pfmemalloc_protocol(struct sk_buff *skb) 5631 { 5632 switch (skb->protocol) { 5633 case htons(ETH_P_ARP): 5634 case htons(ETH_P_IP): 5635 case htons(ETH_P_IPV6): 5636 case htons(ETH_P_8021Q): 5637 case htons(ETH_P_8021AD): 5638 return true; 5639 default: 5640 return false; 5641 } 5642 } 5643 5644 static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev, 5645 int *ret, struct net_device *orig_dev) 5646 { 5647 if (nf_hook_ingress_active(skb)) { 5648 int ingress_retval; 5649 5650 if (*pt_prev) { 5651 *ret = deliver_skb(skb, *pt_prev, orig_dev); 5652 *pt_prev = NULL; 5653 } 5654 5655 rcu_read_lock(); 5656 ingress_retval = nf_hook_ingress(skb); 5657 rcu_read_unlock(); 5658 return ingress_retval; 5659 } 5660 return 0; 5661 } 5662 5663 static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc, 5664 struct packet_type **ppt_prev) 5665 { 5666 struct packet_type *ptype, *pt_prev; 5667 rx_handler_func_t *rx_handler; 5668 struct sk_buff *skb = *pskb; 5669 struct net_device *orig_dev; 5670 bool deliver_exact = false; 5671 int ret = NET_RX_DROP; 5672 __be16 type; 5673 5674 net_timestamp_check(!READ_ONCE(net_hotdata.tstamp_prequeue), skb); 5675 5676 trace_netif_receive_skb(skb); 5677 5678 orig_dev = skb->dev; 5679 5680 skb_reset_network_header(skb); 5681 #if !defined(CONFIG_DEBUG_NET) 5682 /* We plan to no longer reset the transport header here. 5683 * Give some time to fuzzers and dev build to catch bugs 5684 * in network stacks. 5685 */ 5686 if (!skb_transport_header_was_set(skb)) 5687 skb_reset_transport_header(skb); 5688 #endif 5689 skb_reset_mac_len(skb); 5690 5691 pt_prev = NULL; 5692 5693 another_round: 5694 skb->skb_iif = skb->dev->ifindex; 5695 5696 __this_cpu_inc(softnet_data.processed); 5697 5698 if (static_branch_unlikely(&generic_xdp_needed_key)) { 5699 int ret2; 5700 5701 migrate_disable(); 5702 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), 5703 &skb); 5704 migrate_enable(); 5705 5706 if (ret2 != XDP_PASS) { 5707 ret = NET_RX_DROP; 5708 goto out; 5709 } 5710 } 5711 5712 if (eth_type_vlan(skb->protocol)) { 5713 skb = skb_vlan_untag(skb); 5714 if (unlikely(!skb)) 5715 goto out; 5716 } 5717 5718 if (skb_skip_tc_classify(skb)) 5719 goto skip_classify; 5720 5721 if (pfmemalloc) 5722 goto skip_taps; 5723 5724 list_for_each_entry_rcu(ptype, &dev_net_rcu(skb->dev)->ptype_all, 5725 list) { 5726 if (pt_prev) 5727 ret = deliver_skb(skb, pt_prev, orig_dev); 5728 pt_prev = ptype; 5729 } 5730 5731 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { 5732 if (pt_prev) 5733 ret = deliver_skb(skb, pt_prev, orig_dev); 5734 pt_prev = ptype; 5735 } 5736 5737 skip_taps: 5738 #ifdef CONFIG_NET_INGRESS 5739 if (static_branch_unlikely(&ingress_needed_key)) { 5740 bool another = false; 5741 5742 nf_skip_egress(skb, true); 5743 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev, 5744 &another); 5745 if (another) 5746 goto another_round; 5747 if (!skb) 5748 goto out; 5749 5750 nf_skip_egress(skb, false); 5751 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0) 5752 goto out; 5753 } 5754 #endif 5755 skb_reset_redirect(skb); 5756 skip_classify: 5757 if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) 5758 goto drop; 5759 5760 if (skb_vlan_tag_present(skb)) { 5761 if (pt_prev) { 5762 ret = deliver_skb(skb, pt_prev, orig_dev); 5763 pt_prev = NULL; 5764 } 5765 if (vlan_do_receive(&skb)) 5766 goto another_round; 5767 else if (unlikely(!skb)) 5768 goto out; 5769 } 5770 5771 rx_handler = rcu_dereference(skb->dev->rx_handler); 5772 if (rx_handler) { 5773 if (pt_prev) { 5774 ret = deliver_skb(skb, pt_prev, orig_dev); 5775 pt_prev = NULL; 5776 } 5777 switch (rx_handler(&skb)) { 5778 case RX_HANDLER_CONSUMED: 5779 ret = NET_RX_SUCCESS; 5780 goto out; 5781 case RX_HANDLER_ANOTHER: 5782 goto another_round; 5783 case RX_HANDLER_EXACT: 5784 deliver_exact = true; 5785 break; 5786 case RX_HANDLER_PASS: 5787 break; 5788 default: 5789 BUG(); 5790 } 5791 } 5792 5793 if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) { 5794 check_vlan_id: 5795 if (skb_vlan_tag_get_id(skb)) { 5796 /* Vlan id is non 0 and vlan_do_receive() above couldn't 5797 * find vlan device. 5798 */ 5799 skb->pkt_type = PACKET_OTHERHOST; 5800 } else if (eth_type_vlan(skb->protocol)) { 5801 /* Outer header is 802.1P with vlan 0, inner header is 5802 * 802.1Q or 802.1AD and vlan_do_receive() above could 5803 * not find vlan dev for vlan id 0. 5804 */ 5805 __vlan_hwaccel_clear_tag(skb); 5806 skb = skb_vlan_untag(skb); 5807 if (unlikely(!skb)) 5808 goto out; 5809 if (vlan_do_receive(&skb)) 5810 /* After stripping off 802.1P header with vlan 0 5811 * vlan dev is found for inner header. 5812 */ 5813 goto another_round; 5814 else if (unlikely(!skb)) 5815 goto out; 5816 else 5817 /* We have stripped outer 802.1P vlan 0 header. 5818 * But could not find vlan dev. 5819 * check again for vlan id to set OTHERHOST. 5820 */ 5821 goto check_vlan_id; 5822 } 5823 /* Note: we might in the future use prio bits 5824 * and set skb->priority like in vlan_do_receive() 5825 * For the time being, just ignore Priority Code Point 5826 */ 5827 __vlan_hwaccel_clear_tag(skb); 5828 } 5829 5830 type = skb->protocol; 5831 5832 /* deliver only exact match when indicated */ 5833 if (likely(!deliver_exact)) { 5834 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, 5835 &ptype_base[ntohs(type) & 5836 PTYPE_HASH_MASK]); 5837 5838 /* orig_dev and skb->dev could belong to different netns; 5839 * Even in such case we need to traverse only the list 5840 * coming from skb->dev, as the ptype owner (packet socket) 5841 * will use dev_net(skb->dev) to do namespace filtering. 5842 */ 5843 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, 5844 &dev_net_rcu(skb->dev)->ptype_specific); 5845 } 5846 5847 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, 5848 &orig_dev->ptype_specific); 5849 5850 if (unlikely(skb->dev != orig_dev)) { 5851 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, 5852 &skb->dev->ptype_specific); 5853 } 5854 5855 if (pt_prev) { 5856 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) 5857 goto drop; 5858 *ppt_prev = pt_prev; 5859 } else { 5860 drop: 5861 if (!deliver_exact) 5862 dev_core_stats_rx_dropped_inc(skb->dev); 5863 else 5864 dev_core_stats_rx_nohandler_inc(skb->dev); 5865 kfree_skb_reason(skb, SKB_DROP_REASON_UNHANDLED_PROTO); 5866 /* Jamal, now you will not able to escape explaining 5867 * me how you were going to use this. :-) 5868 */ 5869 ret = NET_RX_DROP; 5870 } 5871 5872 out: 5873 /* The invariant here is that if *ppt_prev is not NULL 5874 * then skb should also be non-NULL. 5875 * 5876 * Apparently *ppt_prev assignment above holds this invariant due to 5877 * skb dereferencing near it. 5878 */ 5879 *pskb = skb; 5880 return ret; 5881 } 5882 5883 static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc) 5884 { 5885 struct net_device *orig_dev = skb->dev; 5886 struct packet_type *pt_prev = NULL; 5887 int ret; 5888 5889 ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); 5890 if (pt_prev) 5891 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb, 5892 skb->dev, pt_prev, orig_dev); 5893 return ret; 5894 } 5895 5896 /** 5897 * netif_receive_skb_core - special purpose version of netif_receive_skb 5898 * @skb: buffer to process 5899 * 5900 * More direct receive version of netif_receive_skb(). It should 5901 * only be used by callers that have a need to skip RPS and Generic XDP. 5902 * Caller must also take care of handling if ``(page_is_)pfmemalloc``. 5903 * 5904 * This function may only be called from softirq context and interrupts 5905 * should be enabled. 5906 * 5907 * Return values (usually ignored): 5908 * NET_RX_SUCCESS: no congestion 5909 * NET_RX_DROP: packet was dropped 5910 */ 5911 int netif_receive_skb_core(struct sk_buff *skb) 5912 { 5913 int ret; 5914 5915 rcu_read_lock(); 5916 ret = __netif_receive_skb_one_core(skb, false); 5917 rcu_read_unlock(); 5918 5919 return ret; 5920 } 5921 EXPORT_SYMBOL(netif_receive_skb_core); 5922 5923 static inline void __netif_receive_skb_list_ptype(struct list_head *head, 5924 struct packet_type *pt_prev, 5925 struct net_device *orig_dev) 5926 { 5927 struct sk_buff *skb, *next; 5928 5929 if (!pt_prev) 5930 return; 5931 if (list_empty(head)) 5932 return; 5933 if (pt_prev->list_func != NULL) 5934 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv, 5935 ip_list_rcv, head, pt_prev, orig_dev); 5936 else 5937 list_for_each_entry_safe(skb, next, head, list) { 5938 skb_list_del_init(skb); 5939 pt_prev->func(skb, skb->dev, pt_prev, orig_dev); 5940 } 5941 } 5942 5943 static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc) 5944 { 5945 /* Fast-path assumptions: 5946 * - There is no RX handler. 5947 * - Only one packet_type matches. 5948 * If either of these fails, we will end up doing some per-packet 5949 * processing in-line, then handling the 'last ptype' for the whole 5950 * sublist. This can't cause out-of-order delivery to any single ptype, 5951 * because the 'last ptype' must be constant across the sublist, and all 5952 * other ptypes are handled per-packet. 5953 */ 5954 /* Current (common) ptype of sublist */ 5955 struct packet_type *pt_curr = NULL; 5956 /* Current (common) orig_dev of sublist */ 5957 struct net_device *od_curr = NULL; 5958 struct sk_buff *skb, *next; 5959 LIST_HEAD(sublist); 5960 5961 list_for_each_entry_safe(skb, next, head, list) { 5962 struct net_device *orig_dev = skb->dev; 5963 struct packet_type *pt_prev = NULL; 5964 5965 skb_list_del_init(skb); 5966 __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); 5967 if (!pt_prev) 5968 continue; 5969 if (pt_curr != pt_prev || od_curr != orig_dev) { 5970 /* dispatch old sublist */ 5971 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); 5972 /* start new sublist */ 5973 INIT_LIST_HEAD(&sublist); 5974 pt_curr = pt_prev; 5975 od_curr = orig_dev; 5976 } 5977 list_add_tail(&skb->list, &sublist); 5978 } 5979 5980 /* dispatch final sublist */ 5981 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); 5982 } 5983 5984 static int __netif_receive_skb(struct sk_buff *skb) 5985 { 5986 int ret; 5987 5988 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { 5989 unsigned int noreclaim_flag; 5990 5991 /* 5992 * PFMEMALLOC skbs are special, they should 5993 * - be delivered to SOCK_MEMALLOC sockets only 5994 * - stay away from userspace 5995 * - have bounded memory usage 5996 * 5997 * Use PF_MEMALLOC as this saves us from propagating the allocation 5998 * context down to all allocation sites. 5999 */ 6000 noreclaim_flag = memalloc_noreclaim_save(); 6001 ret = __netif_receive_skb_one_core(skb, true); 6002 memalloc_noreclaim_restore(noreclaim_flag); 6003 } else 6004 ret = __netif_receive_skb_one_core(skb, false); 6005 6006 return ret; 6007 } 6008 6009 static void __netif_receive_skb_list(struct list_head *head) 6010 { 6011 unsigned long noreclaim_flag = 0; 6012 struct sk_buff *skb, *next; 6013 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */ 6014 6015 list_for_each_entry_safe(skb, next, head, list) { 6016 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) { 6017 struct list_head sublist; 6018 6019 /* Handle the previous sublist */ 6020 list_cut_before(&sublist, head, &skb->list); 6021 if (!list_empty(&sublist)) 6022 __netif_receive_skb_list_core(&sublist, pfmemalloc); 6023 pfmemalloc = !pfmemalloc; 6024 /* See comments in __netif_receive_skb */ 6025 if (pfmemalloc) 6026 noreclaim_flag = memalloc_noreclaim_save(); 6027 else 6028 memalloc_noreclaim_restore(noreclaim_flag); 6029 } 6030 } 6031 /* Handle the remaining sublist */ 6032 if (!list_empty(head)) 6033 __netif_receive_skb_list_core(head, pfmemalloc); 6034 /* Restore pflags */ 6035 if (pfmemalloc) 6036 memalloc_noreclaim_restore(noreclaim_flag); 6037 } 6038 6039 static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) 6040 { 6041 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog); 6042 struct bpf_prog *new = xdp->prog; 6043 int ret = 0; 6044 6045 switch (xdp->command) { 6046 case XDP_SETUP_PROG: 6047 rcu_assign_pointer(dev->xdp_prog, new); 6048 if (old) 6049 bpf_prog_put(old); 6050 6051 if (old && !new) { 6052 static_branch_dec(&generic_xdp_needed_key); 6053 } else if (new && !old) { 6054 static_branch_inc(&generic_xdp_needed_key); 6055 netif_disable_lro(dev); 6056 dev_disable_gro_hw(dev); 6057 } 6058 break; 6059 6060 default: 6061 ret = -EINVAL; 6062 break; 6063 } 6064 6065 return ret; 6066 } 6067 6068 static int netif_receive_skb_internal(struct sk_buff *skb) 6069 { 6070 int ret; 6071 6072 net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb); 6073 6074 if (skb_defer_rx_timestamp(skb)) 6075 return NET_RX_SUCCESS; 6076 6077 rcu_read_lock(); 6078 #ifdef CONFIG_RPS 6079 if (static_branch_unlikely(&rps_needed)) { 6080 struct rps_dev_flow voidflow, *rflow = &voidflow; 6081 int cpu = get_rps_cpu(skb->dev, skb, &rflow); 6082 6083 if (cpu >= 0) { 6084 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); 6085 rcu_read_unlock(); 6086 return ret; 6087 } 6088 } 6089 #endif 6090 ret = __netif_receive_skb(skb); 6091 rcu_read_unlock(); 6092 return ret; 6093 } 6094 6095 void netif_receive_skb_list_internal(struct list_head *head) 6096 { 6097 struct sk_buff *skb, *next; 6098 LIST_HEAD(sublist); 6099 6100 list_for_each_entry_safe(skb, next, head, list) { 6101 net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), 6102 skb); 6103 skb_list_del_init(skb); 6104 if (!skb_defer_rx_timestamp(skb)) 6105 list_add_tail(&skb->list, &sublist); 6106 } 6107 list_splice_init(&sublist, head); 6108 6109 rcu_read_lock(); 6110 #ifdef CONFIG_RPS 6111 if (static_branch_unlikely(&rps_needed)) { 6112 list_for_each_entry_safe(skb, next, head, list) { 6113 struct rps_dev_flow voidflow, *rflow = &voidflow; 6114 int cpu = get_rps_cpu(skb->dev, skb, &rflow); 6115 6116 if (cpu >= 0) { 6117 /* Will be handled, remove from list */ 6118 skb_list_del_init(skb); 6119 enqueue_to_backlog(skb, cpu, &rflow->last_qtail); 6120 } 6121 } 6122 } 6123 #endif 6124 __netif_receive_skb_list(head); 6125 rcu_read_unlock(); 6126 } 6127 6128 /** 6129 * netif_receive_skb - process receive buffer from network 6130 * @skb: buffer to process 6131 * 6132 * netif_receive_skb() is the main receive data processing function. 6133 * It always succeeds. The buffer may be dropped during processing 6134 * for congestion control or by the protocol layers. 6135 * 6136 * This function may only be called from softirq context and interrupts 6137 * should be enabled. 6138 * 6139 * Return values (usually ignored): 6140 * NET_RX_SUCCESS: no congestion 6141 * NET_RX_DROP: packet was dropped 6142 */ 6143 int netif_receive_skb(struct sk_buff *skb) 6144 { 6145 int ret; 6146 6147 trace_netif_receive_skb_entry(skb); 6148 6149 ret = netif_receive_skb_internal(skb); 6150 trace_netif_receive_skb_exit(ret); 6151 6152 return ret; 6153 } 6154 EXPORT_SYMBOL(netif_receive_skb); 6155 6156 /** 6157 * netif_receive_skb_list - process many receive buffers from network 6158 * @head: list of skbs to process. 6159 * 6160 * Since return value of netif_receive_skb() is normally ignored, and 6161 * wouldn't be meaningful for a list, this function returns void. 6162 * 6163 * This function may only be called from softirq context and interrupts 6164 * should be enabled. 6165 */ 6166 void netif_receive_skb_list(struct list_head *head) 6167 { 6168 struct sk_buff *skb; 6169 6170 if (list_empty(head)) 6171 return; 6172 if (trace_netif_receive_skb_list_entry_enabled()) { 6173 list_for_each_entry(skb, head, list) 6174 trace_netif_receive_skb_list_entry(skb); 6175 } 6176 netif_receive_skb_list_internal(head); 6177 trace_netif_receive_skb_list_exit(0); 6178 } 6179 EXPORT_SYMBOL(netif_receive_skb_list); 6180 6181 /* Network device is going away, flush any packets still pending */ 6182 static void flush_backlog(struct work_struct *work) 6183 { 6184 struct sk_buff *skb, *tmp; 6185 struct sk_buff_head list; 6186 struct softnet_data *sd; 6187 6188 __skb_queue_head_init(&list); 6189 local_bh_disable(); 6190 sd = this_cpu_ptr(&softnet_data); 6191 6192 backlog_lock_irq_disable(sd); 6193 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { 6194 if (READ_ONCE(skb->dev->reg_state) == NETREG_UNREGISTERING) { 6195 __skb_unlink(skb, &sd->input_pkt_queue); 6196 __skb_queue_tail(&list, skb); 6197 rps_input_queue_head_incr(sd); 6198 } 6199 } 6200 backlog_unlock_irq_enable(sd); 6201 6202 local_lock_nested_bh(&softnet_data.process_queue_bh_lock); 6203 skb_queue_walk_safe(&sd->process_queue, skb, tmp) { 6204 if (READ_ONCE(skb->dev->reg_state) == NETREG_UNREGISTERING) { 6205 __skb_unlink(skb, &sd->process_queue); 6206 __skb_queue_tail(&list, skb); 6207 rps_input_queue_head_incr(sd); 6208 } 6209 } 6210 local_unlock_nested_bh(&softnet_data.process_queue_bh_lock); 6211 local_bh_enable(); 6212 6213 __skb_queue_purge_reason(&list, SKB_DROP_REASON_DEV_READY); 6214 } 6215 6216 static bool flush_required(int cpu) 6217 { 6218 #if IS_ENABLED(CONFIG_RPS) 6219 struct softnet_data *sd = &per_cpu(softnet_data, cpu); 6220 bool do_flush; 6221 6222 backlog_lock_irq_disable(sd); 6223 6224 /* as insertion into process_queue happens with the rps lock held, 6225 * process_queue access may race only with dequeue 6226 */ 6227 do_flush = !skb_queue_empty(&sd->input_pkt_queue) || 6228 !skb_queue_empty_lockless(&sd->process_queue); 6229 backlog_unlock_irq_enable(sd); 6230 6231 return do_flush; 6232 #endif 6233 /* without RPS we can't safely check input_pkt_queue: during a 6234 * concurrent remote skb_queue_splice() we can detect as empty both 6235 * input_pkt_queue and process_queue even if the latter could end-up 6236 * containing a lot of packets. 6237 */ 6238 return true; 6239 } 6240 6241 struct flush_backlogs { 6242 cpumask_t flush_cpus; 6243 struct work_struct w[]; 6244 }; 6245 6246 static struct flush_backlogs *flush_backlogs_alloc(void) 6247 { 6248 return kmalloc(struct_size_t(struct flush_backlogs, w, nr_cpu_ids), 6249 GFP_KERNEL); 6250 } 6251 6252 static struct flush_backlogs *flush_backlogs_fallback; 6253 static DEFINE_MUTEX(flush_backlogs_mutex); 6254 6255 static void flush_all_backlogs(void) 6256 { 6257 struct flush_backlogs *ptr = flush_backlogs_alloc(); 6258 unsigned int cpu; 6259 6260 if (!ptr) { 6261 mutex_lock(&flush_backlogs_mutex); 6262 ptr = flush_backlogs_fallback; 6263 } 6264 cpumask_clear(&ptr->flush_cpus); 6265 6266 cpus_read_lock(); 6267 6268 for_each_online_cpu(cpu) { 6269 if (flush_required(cpu)) { 6270 INIT_WORK(&ptr->w[cpu], flush_backlog); 6271 queue_work_on(cpu, system_highpri_wq, &ptr->w[cpu]); 6272 __cpumask_set_cpu(cpu, &ptr->flush_cpus); 6273 } 6274 } 6275 6276 /* we can have in flight packet[s] on the cpus we are not flushing, 6277 * synchronize_net() in unregister_netdevice_many() will take care of 6278 * them. 6279 */ 6280 for_each_cpu(cpu, &ptr->flush_cpus) 6281 flush_work(&ptr->w[cpu]); 6282 6283 cpus_read_unlock(); 6284 6285 if (ptr != flush_backlogs_fallback) 6286 kfree(ptr); 6287 else 6288 mutex_unlock(&flush_backlogs_mutex); 6289 } 6290 6291 static void net_rps_send_ipi(struct softnet_data *remsd) 6292 { 6293 #ifdef CONFIG_RPS 6294 while (remsd) { 6295 struct softnet_data *next = remsd->rps_ipi_next; 6296 6297 if (cpu_online(remsd->cpu)) 6298 smp_call_function_single_async(remsd->cpu, &remsd->csd); 6299 remsd = next; 6300 } 6301 #endif 6302 } 6303 6304 /* 6305 * net_rps_action_and_irq_enable sends any pending IPI's for rps. 6306 * Note: called with local irq disabled, but exits with local irq enabled. 6307 */ 6308 static void net_rps_action_and_irq_enable(struct softnet_data *sd) 6309 { 6310 #ifdef CONFIG_RPS 6311 struct softnet_data *remsd = sd->rps_ipi_list; 6312 6313 if (!use_backlog_threads() && remsd) { 6314 sd->rps_ipi_list = NULL; 6315 6316 local_irq_enable(); 6317 6318 /* Send pending IPI's to kick RPS processing on remote cpus. */ 6319 net_rps_send_ipi(remsd); 6320 } else 6321 #endif 6322 local_irq_enable(); 6323 } 6324 6325 static bool sd_has_rps_ipi_waiting(struct softnet_data *sd) 6326 { 6327 #ifdef CONFIG_RPS 6328 return !use_backlog_threads() && sd->rps_ipi_list; 6329 #else 6330 return false; 6331 #endif 6332 } 6333 6334 static int process_backlog(struct napi_struct *napi, int quota) 6335 { 6336 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); 6337 bool again = true; 6338 int work = 0; 6339 6340 /* Check if we have pending ipi, its better to send them now, 6341 * not waiting net_rx_action() end. 6342 */ 6343 if (sd_has_rps_ipi_waiting(sd)) { 6344 local_irq_disable(); 6345 net_rps_action_and_irq_enable(sd); 6346 } 6347 6348 napi->weight = READ_ONCE(net_hotdata.dev_rx_weight); 6349 while (again) { 6350 struct sk_buff *skb; 6351 6352 local_lock_nested_bh(&softnet_data.process_queue_bh_lock); 6353 while ((skb = __skb_dequeue(&sd->process_queue))) { 6354 local_unlock_nested_bh(&softnet_data.process_queue_bh_lock); 6355 rcu_read_lock(); 6356 __netif_receive_skb(skb); 6357 rcu_read_unlock(); 6358 if (++work >= quota) { 6359 rps_input_queue_head_add(sd, work); 6360 return work; 6361 } 6362 6363 local_lock_nested_bh(&softnet_data.process_queue_bh_lock); 6364 } 6365 local_unlock_nested_bh(&softnet_data.process_queue_bh_lock); 6366 6367 backlog_lock_irq_disable(sd); 6368 if (skb_queue_empty(&sd->input_pkt_queue)) { 6369 /* 6370 * Inline a custom version of __napi_complete(). 6371 * only current cpu owns and manipulates this napi, 6372 * and NAPI_STATE_SCHED is the only possible flag set 6373 * on backlog. 6374 * We can use a plain write instead of clear_bit(), 6375 * and we dont need an smp_mb() memory barrier. 6376 */ 6377 napi->state &= NAPIF_STATE_THREADED; 6378 again = false; 6379 } else { 6380 local_lock_nested_bh(&softnet_data.process_queue_bh_lock); 6381 skb_queue_splice_tail_init(&sd->input_pkt_queue, 6382 &sd->process_queue); 6383 local_unlock_nested_bh(&softnet_data.process_queue_bh_lock); 6384 } 6385 backlog_unlock_irq_enable(sd); 6386 } 6387 6388 if (work) 6389 rps_input_queue_head_add(sd, work); 6390 return work; 6391 } 6392 6393 /** 6394 * __napi_schedule - schedule for receive 6395 * @n: entry to schedule 6396 * 6397 * The entry's receive function will be scheduled to run. 6398 * Consider using __napi_schedule_irqoff() if hard irqs are masked. 6399 */ 6400 void __napi_schedule(struct napi_struct *n) 6401 { 6402 unsigned long flags; 6403 6404 local_irq_save(flags); 6405 ____napi_schedule(this_cpu_ptr(&softnet_data), n); 6406 local_irq_restore(flags); 6407 } 6408 EXPORT_SYMBOL(__napi_schedule); 6409 6410 /** 6411 * napi_schedule_prep - check if napi can be scheduled 6412 * @n: napi context 6413 * 6414 * Test if NAPI routine is already running, and if not mark 6415 * it as running. This is used as a condition variable to 6416 * insure only one NAPI poll instance runs. We also make 6417 * sure there is no pending NAPI disable. 6418 */ 6419 bool napi_schedule_prep(struct napi_struct *n) 6420 { 6421 unsigned long new, val = READ_ONCE(n->state); 6422 6423 do { 6424 if (unlikely(val & NAPIF_STATE_DISABLE)) 6425 return false; 6426 new = val | NAPIF_STATE_SCHED; 6427 6428 /* Sets STATE_MISSED bit if STATE_SCHED was already set 6429 * This was suggested by Alexander Duyck, as compiler 6430 * emits better code than : 6431 * if (val & NAPIF_STATE_SCHED) 6432 * new |= NAPIF_STATE_MISSED; 6433 */ 6434 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED * 6435 NAPIF_STATE_MISSED; 6436 } while (!try_cmpxchg(&n->state, &val, new)); 6437 6438 return !(val & NAPIF_STATE_SCHED); 6439 } 6440 EXPORT_SYMBOL(napi_schedule_prep); 6441 6442 /** 6443 * __napi_schedule_irqoff - schedule for receive 6444 * @n: entry to schedule 6445 * 6446 * Variant of __napi_schedule() assuming hard irqs are masked. 6447 * 6448 * On PREEMPT_RT enabled kernels this maps to __napi_schedule() 6449 * because the interrupt disabled assumption might not be true 6450 * due to force-threaded interrupts and spinlock substitution. 6451 */ 6452 void __napi_schedule_irqoff(struct napi_struct *n) 6453 { 6454 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 6455 ____napi_schedule(this_cpu_ptr(&softnet_data), n); 6456 else 6457 __napi_schedule(n); 6458 } 6459 EXPORT_SYMBOL(__napi_schedule_irqoff); 6460 6461 bool napi_complete_done(struct napi_struct *n, int work_done) 6462 { 6463 unsigned long flags, val, new, timeout = 0; 6464 bool ret = true; 6465 6466 /* 6467 * 1) Don't let napi dequeue from the cpu poll list 6468 * just in case its running on a different cpu. 6469 * 2) If we are busy polling, do nothing here, we have 6470 * the guarantee we will be called later. 6471 */ 6472 if (unlikely(n->state & (NAPIF_STATE_NPSVC | 6473 NAPIF_STATE_IN_BUSY_POLL))) 6474 return false; 6475 6476 if (work_done) { 6477 if (n->gro.bitmask) 6478 timeout = napi_get_gro_flush_timeout(n); 6479 n->defer_hard_irqs_count = napi_get_defer_hard_irqs(n); 6480 } 6481 if (n->defer_hard_irqs_count > 0) { 6482 n->defer_hard_irqs_count--; 6483 timeout = napi_get_gro_flush_timeout(n); 6484 if (timeout) 6485 ret = false; 6486 } 6487 6488 /* 6489 * When the NAPI instance uses a timeout and keeps postponing 6490 * it, we need to bound somehow the time packets are kept in 6491 * the GRO layer. 6492 */ 6493 gro_flush(&n->gro, !!timeout); 6494 gro_normal_list(&n->gro); 6495 6496 if (unlikely(!list_empty(&n->poll_list))) { 6497 /* If n->poll_list is not empty, we need to mask irqs */ 6498 local_irq_save(flags); 6499 list_del_init(&n->poll_list); 6500 local_irq_restore(flags); 6501 } 6502 WRITE_ONCE(n->list_owner, -1); 6503 6504 val = READ_ONCE(n->state); 6505 do { 6506 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED)); 6507 6508 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED | 6509 NAPIF_STATE_SCHED_THREADED | 6510 NAPIF_STATE_PREFER_BUSY_POLL); 6511 6512 /* If STATE_MISSED was set, leave STATE_SCHED set, 6513 * because we will call napi->poll() one more time. 6514 * This C code was suggested by Alexander Duyck to help gcc. 6515 */ 6516 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED * 6517 NAPIF_STATE_SCHED; 6518 } while (!try_cmpxchg(&n->state, &val, new)); 6519 6520 if (unlikely(val & NAPIF_STATE_MISSED)) { 6521 __napi_schedule(n); 6522 return false; 6523 } 6524 6525 if (timeout) 6526 hrtimer_start(&n->timer, ns_to_ktime(timeout), 6527 HRTIMER_MODE_REL_PINNED); 6528 return ret; 6529 } 6530 EXPORT_SYMBOL(napi_complete_done); 6531 6532 static void skb_defer_free_flush(struct softnet_data *sd) 6533 { 6534 struct sk_buff *skb, *next; 6535 6536 /* Paired with WRITE_ONCE() in skb_attempt_defer_free() */ 6537 if (!READ_ONCE(sd->defer_list)) 6538 return; 6539 6540 spin_lock(&sd->defer_lock); 6541 skb = sd->defer_list; 6542 sd->defer_list = NULL; 6543 sd->defer_count = 0; 6544 spin_unlock(&sd->defer_lock); 6545 6546 while (skb != NULL) { 6547 next = skb->next; 6548 napi_consume_skb(skb, 1); 6549 skb = next; 6550 } 6551 } 6552 6553 #if defined(CONFIG_NET_RX_BUSY_POLL) 6554 6555 static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule) 6556 { 6557 if (!skip_schedule) { 6558 gro_normal_list(&napi->gro); 6559 __napi_schedule(napi); 6560 return; 6561 } 6562 6563 /* Flush too old packets. If HZ < 1000, flush all packets */ 6564 gro_flush(&napi->gro, HZ >= 1000); 6565 gro_normal_list(&napi->gro); 6566 6567 clear_bit(NAPI_STATE_SCHED, &napi->state); 6568 } 6569 6570 enum { 6571 NAPI_F_PREFER_BUSY_POLL = 1, 6572 NAPI_F_END_ON_RESCHED = 2, 6573 }; 6574 6575 static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock, 6576 unsigned flags, u16 budget) 6577 { 6578 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 6579 bool skip_schedule = false; 6580 unsigned long timeout; 6581 int rc; 6582 6583 /* Busy polling means there is a high chance device driver hard irq 6584 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was 6585 * set in napi_schedule_prep(). 6586 * Since we are about to call napi->poll() once more, we can safely 6587 * clear NAPI_STATE_MISSED. 6588 * 6589 * Note: x86 could use a single "lock and ..." instruction 6590 * to perform these two clear_bit() 6591 */ 6592 clear_bit(NAPI_STATE_MISSED, &napi->state); 6593 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state); 6594 6595 local_bh_disable(); 6596 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 6597 6598 if (flags & NAPI_F_PREFER_BUSY_POLL) { 6599 napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi); 6600 timeout = napi_get_gro_flush_timeout(napi); 6601 if (napi->defer_hard_irqs_count && timeout) { 6602 hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED); 6603 skip_schedule = true; 6604 } 6605 } 6606 6607 /* All we really want here is to re-enable device interrupts. 6608 * Ideally, a new ndo_busy_poll_stop() could avoid another round. 6609 */ 6610 rc = napi->poll(napi, budget); 6611 /* We can't gro_normal_list() here, because napi->poll() might have 6612 * rearmed the napi (napi_complete_done()) in which case it could 6613 * already be running on another CPU. 6614 */ 6615 trace_napi_poll(napi, rc, budget); 6616 netpoll_poll_unlock(have_poll_lock); 6617 if (rc == budget) 6618 __busy_poll_stop(napi, skip_schedule); 6619 bpf_net_ctx_clear(bpf_net_ctx); 6620 local_bh_enable(); 6621 } 6622 6623 static void __napi_busy_loop(unsigned int napi_id, 6624 bool (*loop_end)(void *, unsigned long), 6625 void *loop_end_arg, unsigned flags, u16 budget) 6626 { 6627 unsigned long start_time = loop_end ? busy_loop_current_time() : 0; 6628 int (*napi_poll)(struct napi_struct *napi, int budget); 6629 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 6630 void *have_poll_lock = NULL; 6631 struct napi_struct *napi; 6632 6633 WARN_ON_ONCE(!rcu_read_lock_held()); 6634 6635 restart: 6636 napi_poll = NULL; 6637 6638 napi = napi_by_id(napi_id); 6639 if (!napi) 6640 return; 6641 6642 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 6643 preempt_disable(); 6644 for (;;) { 6645 int work = 0; 6646 6647 local_bh_disable(); 6648 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 6649 if (!napi_poll) { 6650 unsigned long val = READ_ONCE(napi->state); 6651 6652 /* If multiple threads are competing for this napi, 6653 * we avoid dirtying napi->state as much as we can. 6654 */ 6655 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED | 6656 NAPIF_STATE_IN_BUSY_POLL)) { 6657 if (flags & NAPI_F_PREFER_BUSY_POLL) 6658 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); 6659 goto count; 6660 } 6661 if (cmpxchg(&napi->state, val, 6662 val | NAPIF_STATE_IN_BUSY_POLL | 6663 NAPIF_STATE_SCHED) != val) { 6664 if (flags & NAPI_F_PREFER_BUSY_POLL) 6665 set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); 6666 goto count; 6667 } 6668 have_poll_lock = netpoll_poll_lock(napi); 6669 napi_poll = napi->poll; 6670 } 6671 work = napi_poll(napi, budget); 6672 trace_napi_poll(napi, work, budget); 6673 gro_normal_list(&napi->gro); 6674 count: 6675 if (work > 0) 6676 __NET_ADD_STATS(dev_net(napi->dev), 6677 LINUX_MIB_BUSYPOLLRXPACKETS, work); 6678 skb_defer_free_flush(this_cpu_ptr(&softnet_data)); 6679 bpf_net_ctx_clear(bpf_net_ctx); 6680 local_bh_enable(); 6681 6682 if (!loop_end || loop_end(loop_end_arg, start_time)) 6683 break; 6684 6685 if (unlikely(need_resched())) { 6686 if (flags & NAPI_F_END_ON_RESCHED) 6687 break; 6688 if (napi_poll) 6689 busy_poll_stop(napi, have_poll_lock, flags, budget); 6690 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 6691 preempt_enable(); 6692 rcu_read_unlock(); 6693 cond_resched(); 6694 rcu_read_lock(); 6695 if (loop_end(loop_end_arg, start_time)) 6696 return; 6697 goto restart; 6698 } 6699 cpu_relax(); 6700 } 6701 if (napi_poll) 6702 busy_poll_stop(napi, have_poll_lock, flags, budget); 6703 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 6704 preempt_enable(); 6705 } 6706 6707 void napi_busy_loop_rcu(unsigned int napi_id, 6708 bool (*loop_end)(void *, unsigned long), 6709 void *loop_end_arg, bool prefer_busy_poll, u16 budget) 6710 { 6711 unsigned flags = NAPI_F_END_ON_RESCHED; 6712 6713 if (prefer_busy_poll) 6714 flags |= NAPI_F_PREFER_BUSY_POLL; 6715 6716 __napi_busy_loop(napi_id, loop_end, loop_end_arg, flags, budget); 6717 } 6718 6719 void napi_busy_loop(unsigned int napi_id, 6720 bool (*loop_end)(void *, unsigned long), 6721 void *loop_end_arg, bool prefer_busy_poll, u16 budget) 6722 { 6723 unsigned flags = prefer_busy_poll ? NAPI_F_PREFER_BUSY_POLL : 0; 6724 6725 rcu_read_lock(); 6726 __napi_busy_loop(napi_id, loop_end, loop_end_arg, flags, budget); 6727 rcu_read_unlock(); 6728 } 6729 EXPORT_SYMBOL(napi_busy_loop); 6730 6731 void napi_suspend_irqs(unsigned int napi_id) 6732 { 6733 struct napi_struct *napi; 6734 6735 rcu_read_lock(); 6736 napi = napi_by_id(napi_id); 6737 if (napi) { 6738 unsigned long timeout = napi_get_irq_suspend_timeout(napi); 6739 6740 if (timeout) 6741 hrtimer_start(&napi->timer, ns_to_ktime(timeout), 6742 HRTIMER_MODE_REL_PINNED); 6743 } 6744 rcu_read_unlock(); 6745 } 6746 6747 void napi_resume_irqs(unsigned int napi_id) 6748 { 6749 struct napi_struct *napi; 6750 6751 rcu_read_lock(); 6752 napi = napi_by_id(napi_id); 6753 if (napi) { 6754 /* If irq_suspend_timeout is set to 0 between the call to 6755 * napi_suspend_irqs and now, the original value still 6756 * determines the safety timeout as intended and napi_watchdog 6757 * will resume irq processing. 6758 */ 6759 if (napi_get_irq_suspend_timeout(napi)) { 6760 local_bh_disable(); 6761 napi_schedule(napi); 6762 local_bh_enable(); 6763 } 6764 } 6765 rcu_read_unlock(); 6766 } 6767 6768 #endif /* CONFIG_NET_RX_BUSY_POLL */ 6769 6770 static void __napi_hash_add_with_id(struct napi_struct *napi, 6771 unsigned int napi_id) 6772 { 6773 napi->gro.cached_napi_id = napi_id; 6774 6775 WRITE_ONCE(napi->napi_id, napi_id); 6776 hlist_add_head_rcu(&napi->napi_hash_node, 6777 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); 6778 } 6779 6780 static void napi_hash_add_with_id(struct napi_struct *napi, 6781 unsigned int napi_id) 6782 { 6783 unsigned long flags; 6784 6785 spin_lock_irqsave(&napi_hash_lock, flags); 6786 WARN_ON_ONCE(napi_by_id(napi_id)); 6787 __napi_hash_add_with_id(napi, napi_id); 6788 spin_unlock_irqrestore(&napi_hash_lock, flags); 6789 } 6790 6791 static void napi_hash_add(struct napi_struct *napi) 6792 { 6793 unsigned long flags; 6794 6795 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state)) 6796 return; 6797 6798 spin_lock_irqsave(&napi_hash_lock, flags); 6799 6800 /* 0..NR_CPUS range is reserved for sender_cpu use */ 6801 do { 6802 if (unlikely(!napi_id_valid(++napi_gen_id))) 6803 napi_gen_id = MIN_NAPI_ID; 6804 } while (napi_by_id(napi_gen_id)); 6805 6806 __napi_hash_add_with_id(napi, napi_gen_id); 6807 6808 spin_unlock_irqrestore(&napi_hash_lock, flags); 6809 } 6810 6811 /* Warning : caller is responsible to make sure rcu grace period 6812 * is respected before freeing memory containing @napi 6813 */ 6814 static void napi_hash_del(struct napi_struct *napi) 6815 { 6816 unsigned long flags; 6817 6818 spin_lock_irqsave(&napi_hash_lock, flags); 6819 6820 hlist_del_init_rcu(&napi->napi_hash_node); 6821 6822 spin_unlock_irqrestore(&napi_hash_lock, flags); 6823 } 6824 6825 static enum hrtimer_restart napi_watchdog(struct hrtimer *timer) 6826 { 6827 struct napi_struct *napi; 6828 6829 napi = container_of(timer, struct napi_struct, timer); 6830 6831 /* Note : we use a relaxed variant of napi_schedule_prep() not setting 6832 * NAPI_STATE_MISSED, since we do not react to a device IRQ. 6833 */ 6834 if (!napi_disable_pending(napi) && 6835 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) { 6836 clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state); 6837 __napi_schedule_irqoff(napi); 6838 } 6839 6840 return HRTIMER_NORESTART; 6841 } 6842 6843 int dev_set_threaded(struct net_device *dev, bool threaded) 6844 { 6845 struct napi_struct *napi; 6846 int err = 0; 6847 6848 netdev_assert_locked_or_invisible(dev); 6849 6850 if (dev->threaded == threaded) 6851 return 0; 6852 6853 if (threaded) { 6854 list_for_each_entry(napi, &dev->napi_list, dev_list) { 6855 if (!napi->thread) { 6856 err = napi_kthread_create(napi); 6857 if (err) { 6858 threaded = false; 6859 break; 6860 } 6861 } 6862 } 6863 } 6864 6865 WRITE_ONCE(dev->threaded, threaded); 6866 6867 /* Make sure kthread is created before THREADED bit 6868 * is set. 6869 */ 6870 smp_mb__before_atomic(); 6871 6872 /* Setting/unsetting threaded mode on a napi might not immediately 6873 * take effect, if the current napi instance is actively being 6874 * polled. In this case, the switch between threaded mode and 6875 * softirq mode will happen in the next round of napi_schedule(). 6876 * This should not cause hiccups/stalls to the live traffic. 6877 */ 6878 list_for_each_entry(napi, &dev->napi_list, dev_list) 6879 assign_bit(NAPI_STATE_THREADED, &napi->state, threaded); 6880 6881 return err; 6882 } 6883 EXPORT_SYMBOL(dev_set_threaded); 6884 6885 /** 6886 * netif_queue_set_napi - Associate queue with the napi 6887 * @dev: device to which NAPI and queue belong 6888 * @queue_index: Index of queue 6889 * @type: queue type as RX or TX 6890 * @napi: NAPI context, pass NULL to clear previously set NAPI 6891 * 6892 * Set queue with its corresponding napi context. This should be done after 6893 * registering the NAPI handler for the queue-vector and the queues have been 6894 * mapped to the corresponding interrupt vector. 6895 */ 6896 void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index, 6897 enum netdev_queue_type type, struct napi_struct *napi) 6898 { 6899 struct netdev_rx_queue *rxq; 6900 struct netdev_queue *txq; 6901 6902 if (WARN_ON_ONCE(napi && !napi->dev)) 6903 return; 6904 if (dev->reg_state >= NETREG_REGISTERED) 6905 ASSERT_RTNL(); 6906 6907 switch (type) { 6908 case NETDEV_QUEUE_TYPE_RX: 6909 rxq = __netif_get_rx_queue(dev, queue_index); 6910 rxq->napi = napi; 6911 return; 6912 case NETDEV_QUEUE_TYPE_TX: 6913 txq = netdev_get_tx_queue(dev, queue_index); 6914 txq->napi = napi; 6915 return; 6916 default: 6917 return; 6918 } 6919 } 6920 EXPORT_SYMBOL(netif_queue_set_napi); 6921 6922 static void 6923 netif_napi_irq_notify(struct irq_affinity_notify *notify, 6924 const cpumask_t *mask) 6925 { 6926 struct napi_struct *napi = 6927 container_of(notify, struct napi_struct, notify); 6928 #ifdef CONFIG_RFS_ACCEL 6929 struct cpu_rmap *rmap = napi->dev->rx_cpu_rmap; 6930 int err; 6931 #endif 6932 6933 if (napi->config && napi->dev->irq_affinity_auto) 6934 cpumask_copy(&napi->config->affinity_mask, mask); 6935 6936 #ifdef CONFIG_RFS_ACCEL 6937 if (napi->dev->rx_cpu_rmap_auto) { 6938 err = cpu_rmap_update(rmap, napi->napi_rmap_idx, mask); 6939 if (err) 6940 netdev_warn(napi->dev, "RMAP update failed (%d)\n", 6941 err); 6942 } 6943 #endif 6944 } 6945 6946 #ifdef CONFIG_RFS_ACCEL 6947 static void netif_napi_affinity_release(struct kref *ref) 6948 { 6949 struct napi_struct *napi = 6950 container_of(ref, struct napi_struct, notify.kref); 6951 struct cpu_rmap *rmap = napi->dev->rx_cpu_rmap; 6952 6953 netdev_assert_locked(napi->dev); 6954 WARN_ON(test_and_clear_bit(NAPI_STATE_HAS_NOTIFIER, 6955 &napi->state)); 6956 6957 if (!napi->dev->rx_cpu_rmap_auto) 6958 return; 6959 rmap->obj[napi->napi_rmap_idx] = NULL; 6960 napi->napi_rmap_idx = -1; 6961 cpu_rmap_put(rmap); 6962 } 6963 6964 int netif_enable_cpu_rmap(struct net_device *dev, unsigned int num_irqs) 6965 { 6966 if (dev->rx_cpu_rmap_auto) 6967 return 0; 6968 6969 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(num_irqs); 6970 if (!dev->rx_cpu_rmap) 6971 return -ENOMEM; 6972 6973 dev->rx_cpu_rmap_auto = true; 6974 return 0; 6975 } 6976 EXPORT_SYMBOL(netif_enable_cpu_rmap); 6977 6978 static void netif_del_cpu_rmap(struct net_device *dev) 6979 { 6980 struct cpu_rmap *rmap = dev->rx_cpu_rmap; 6981 6982 if (!dev->rx_cpu_rmap_auto) 6983 return; 6984 6985 /* Free the rmap */ 6986 cpu_rmap_put(rmap); 6987 dev->rx_cpu_rmap = NULL; 6988 dev->rx_cpu_rmap_auto = false; 6989 } 6990 6991 #else 6992 static void netif_napi_affinity_release(struct kref *ref) 6993 { 6994 } 6995 6996 int netif_enable_cpu_rmap(struct net_device *dev, unsigned int num_irqs) 6997 { 6998 return 0; 6999 } 7000 EXPORT_SYMBOL(netif_enable_cpu_rmap); 7001 7002 static void netif_del_cpu_rmap(struct net_device *dev) 7003 { 7004 } 7005 #endif 7006 7007 void netif_set_affinity_auto(struct net_device *dev) 7008 { 7009 unsigned int i, maxqs, numa; 7010 7011 maxqs = max(dev->num_tx_queues, dev->num_rx_queues); 7012 numa = dev_to_node(&dev->dev); 7013 7014 for (i = 0; i < maxqs; i++) 7015 cpumask_set_cpu(cpumask_local_spread(i, numa), 7016 &dev->napi_config[i].affinity_mask); 7017 7018 dev->irq_affinity_auto = true; 7019 } 7020 EXPORT_SYMBOL(netif_set_affinity_auto); 7021 7022 void netif_napi_set_irq_locked(struct napi_struct *napi, int irq) 7023 { 7024 int rc; 7025 7026 netdev_assert_locked_or_invisible(napi->dev); 7027 7028 if (napi->irq == irq) 7029 return; 7030 7031 /* Remove existing resources */ 7032 if (test_and_clear_bit(NAPI_STATE_HAS_NOTIFIER, &napi->state)) 7033 irq_set_affinity_notifier(napi->irq, NULL); 7034 7035 napi->irq = irq; 7036 if (irq < 0 || 7037 (!napi->dev->rx_cpu_rmap_auto && !napi->dev->irq_affinity_auto)) 7038 return; 7039 7040 /* Abort for buggy drivers */ 7041 if (napi->dev->irq_affinity_auto && WARN_ON_ONCE(!napi->config)) 7042 return; 7043 7044 #ifdef CONFIG_RFS_ACCEL 7045 if (napi->dev->rx_cpu_rmap_auto) { 7046 rc = cpu_rmap_add(napi->dev->rx_cpu_rmap, napi); 7047 if (rc < 0) 7048 return; 7049 7050 cpu_rmap_get(napi->dev->rx_cpu_rmap); 7051 napi->napi_rmap_idx = rc; 7052 } 7053 #endif 7054 7055 /* Use core IRQ notifier */ 7056 napi->notify.notify = netif_napi_irq_notify; 7057 napi->notify.release = netif_napi_affinity_release; 7058 rc = irq_set_affinity_notifier(irq, &napi->notify); 7059 if (rc) { 7060 netdev_warn(napi->dev, "Unable to set IRQ notifier (%d)\n", 7061 rc); 7062 goto put_rmap; 7063 } 7064 7065 set_bit(NAPI_STATE_HAS_NOTIFIER, &napi->state); 7066 return; 7067 7068 put_rmap: 7069 #ifdef CONFIG_RFS_ACCEL 7070 if (napi->dev->rx_cpu_rmap_auto) { 7071 napi->dev->rx_cpu_rmap->obj[napi->napi_rmap_idx] = NULL; 7072 cpu_rmap_put(napi->dev->rx_cpu_rmap); 7073 napi->napi_rmap_idx = -1; 7074 } 7075 #endif 7076 napi->notify.notify = NULL; 7077 napi->notify.release = NULL; 7078 } 7079 EXPORT_SYMBOL(netif_napi_set_irq_locked); 7080 7081 static void napi_restore_config(struct napi_struct *n) 7082 { 7083 n->defer_hard_irqs = n->config->defer_hard_irqs; 7084 n->gro_flush_timeout = n->config->gro_flush_timeout; 7085 n->irq_suspend_timeout = n->config->irq_suspend_timeout; 7086 7087 if (n->dev->irq_affinity_auto && 7088 test_bit(NAPI_STATE_HAS_NOTIFIER, &n->state)) 7089 irq_set_affinity(n->irq, &n->config->affinity_mask); 7090 7091 /* a NAPI ID might be stored in the config, if so use it. if not, use 7092 * napi_hash_add to generate one for us. 7093 */ 7094 if (n->config->napi_id) { 7095 napi_hash_add_with_id(n, n->config->napi_id); 7096 } else { 7097 napi_hash_add(n); 7098 n->config->napi_id = n->napi_id; 7099 } 7100 } 7101 7102 static void napi_save_config(struct napi_struct *n) 7103 { 7104 n->config->defer_hard_irqs = n->defer_hard_irqs; 7105 n->config->gro_flush_timeout = n->gro_flush_timeout; 7106 n->config->irq_suspend_timeout = n->irq_suspend_timeout; 7107 napi_hash_del(n); 7108 } 7109 7110 /* Netlink wants the NAPI list to be sorted by ID, if adding a NAPI which will 7111 * inherit an existing ID try to insert it at the right position. 7112 */ 7113 static void 7114 netif_napi_dev_list_add(struct net_device *dev, struct napi_struct *napi) 7115 { 7116 unsigned int new_id, pos_id; 7117 struct list_head *higher; 7118 struct napi_struct *pos; 7119 7120 new_id = UINT_MAX; 7121 if (napi->config && napi->config->napi_id) 7122 new_id = napi->config->napi_id; 7123 7124 higher = &dev->napi_list; 7125 list_for_each_entry(pos, &dev->napi_list, dev_list) { 7126 if (napi_id_valid(pos->napi_id)) 7127 pos_id = pos->napi_id; 7128 else if (pos->config) 7129 pos_id = pos->config->napi_id; 7130 else 7131 pos_id = UINT_MAX; 7132 7133 if (pos_id <= new_id) 7134 break; 7135 higher = &pos->dev_list; 7136 } 7137 list_add_rcu(&napi->dev_list, higher); /* adds after higher */ 7138 } 7139 7140 /* Double check that napi_get_frags() allocates skbs with 7141 * skb->head being backed by slab, not a page fragment. 7142 * This is to make sure bug fixed in 3226b158e67c 7143 * ("net: avoid 32 x truesize under-estimation for tiny skbs") 7144 * does not accidentally come back. 7145 */ 7146 static void napi_get_frags_check(struct napi_struct *napi) 7147 { 7148 struct sk_buff *skb; 7149 7150 local_bh_disable(); 7151 skb = napi_get_frags(napi); 7152 WARN_ON_ONCE(skb && skb->head_frag); 7153 napi_free_frags(napi); 7154 local_bh_enable(); 7155 } 7156 7157 void netif_napi_add_weight_locked(struct net_device *dev, 7158 struct napi_struct *napi, 7159 int (*poll)(struct napi_struct *, int), 7160 int weight) 7161 { 7162 netdev_assert_locked(dev); 7163 if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state))) 7164 return; 7165 7166 INIT_LIST_HEAD(&napi->poll_list); 7167 INIT_HLIST_NODE(&napi->napi_hash_node); 7168 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); 7169 napi->timer.function = napi_watchdog; 7170 gro_init(&napi->gro); 7171 napi->skb = NULL; 7172 napi->poll = poll; 7173 if (weight > NAPI_POLL_WEIGHT) 7174 netdev_err_once(dev, "%s() called with weight %d\n", __func__, 7175 weight); 7176 napi->weight = weight; 7177 napi->dev = dev; 7178 #ifdef CONFIG_NETPOLL 7179 napi->poll_owner = -1; 7180 #endif 7181 napi->list_owner = -1; 7182 set_bit(NAPI_STATE_SCHED, &napi->state); 7183 set_bit(NAPI_STATE_NPSVC, &napi->state); 7184 netif_napi_dev_list_add(dev, napi); 7185 7186 /* default settings from sysfs are applied to all NAPIs. any per-NAPI 7187 * configuration will be loaded in napi_enable 7188 */ 7189 napi_set_defer_hard_irqs(napi, READ_ONCE(dev->napi_defer_hard_irqs)); 7190 napi_set_gro_flush_timeout(napi, READ_ONCE(dev->gro_flush_timeout)); 7191 7192 napi_get_frags_check(napi); 7193 /* Create kthread for this napi if dev->threaded is set. 7194 * Clear dev->threaded if kthread creation failed so that 7195 * threaded mode will not be enabled in napi_enable(). 7196 */ 7197 if (dev->threaded && napi_kthread_create(napi)) 7198 dev->threaded = false; 7199 netif_napi_set_irq_locked(napi, -1); 7200 } 7201 EXPORT_SYMBOL(netif_napi_add_weight_locked); 7202 7203 void napi_disable_locked(struct napi_struct *n) 7204 { 7205 unsigned long val, new; 7206 7207 might_sleep(); 7208 netdev_assert_locked(n->dev); 7209 7210 set_bit(NAPI_STATE_DISABLE, &n->state); 7211 7212 val = READ_ONCE(n->state); 7213 do { 7214 while (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) { 7215 usleep_range(20, 200); 7216 val = READ_ONCE(n->state); 7217 } 7218 7219 new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC; 7220 new &= ~(NAPIF_STATE_THREADED | NAPIF_STATE_PREFER_BUSY_POLL); 7221 } while (!try_cmpxchg(&n->state, &val, new)); 7222 7223 hrtimer_cancel(&n->timer); 7224 7225 if (n->config) 7226 napi_save_config(n); 7227 else 7228 napi_hash_del(n); 7229 7230 clear_bit(NAPI_STATE_DISABLE, &n->state); 7231 } 7232 EXPORT_SYMBOL(napi_disable_locked); 7233 7234 /** 7235 * napi_disable() - prevent NAPI from scheduling 7236 * @n: NAPI context 7237 * 7238 * Stop NAPI from being scheduled on this context. 7239 * Waits till any outstanding processing completes. 7240 * Takes netdev_lock() for associated net_device. 7241 */ 7242 void napi_disable(struct napi_struct *n) 7243 { 7244 netdev_lock(n->dev); 7245 napi_disable_locked(n); 7246 netdev_unlock(n->dev); 7247 } 7248 EXPORT_SYMBOL(napi_disable); 7249 7250 void napi_enable_locked(struct napi_struct *n) 7251 { 7252 unsigned long new, val = READ_ONCE(n->state); 7253 7254 if (n->config) 7255 napi_restore_config(n); 7256 else 7257 napi_hash_add(n); 7258 7259 do { 7260 BUG_ON(!test_bit(NAPI_STATE_SCHED, &val)); 7261 7262 new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC); 7263 if (n->dev->threaded && n->thread) 7264 new |= NAPIF_STATE_THREADED; 7265 } while (!try_cmpxchg(&n->state, &val, new)); 7266 } 7267 EXPORT_SYMBOL(napi_enable_locked); 7268 7269 /** 7270 * napi_enable() - enable NAPI scheduling 7271 * @n: NAPI context 7272 * 7273 * Enable scheduling of a NAPI instance. 7274 * Must be paired with napi_disable(). 7275 * Takes netdev_lock() for associated net_device. 7276 */ 7277 void napi_enable(struct napi_struct *n) 7278 { 7279 netdev_lock(n->dev); 7280 napi_enable_locked(n); 7281 netdev_unlock(n->dev); 7282 } 7283 EXPORT_SYMBOL(napi_enable); 7284 7285 /* Must be called in process context */ 7286 void __netif_napi_del_locked(struct napi_struct *napi) 7287 { 7288 netdev_assert_locked(napi->dev); 7289 7290 if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state)) 7291 return; 7292 7293 /* Make sure NAPI is disabled (or was never enabled). */ 7294 WARN_ON(!test_bit(NAPI_STATE_SCHED, &napi->state)); 7295 7296 if (test_and_clear_bit(NAPI_STATE_HAS_NOTIFIER, &napi->state)) 7297 irq_set_affinity_notifier(napi->irq, NULL); 7298 7299 if (napi->config) { 7300 napi->index = -1; 7301 napi->config = NULL; 7302 } 7303 7304 list_del_rcu(&napi->dev_list); 7305 napi_free_frags(napi); 7306 7307 gro_cleanup(&napi->gro); 7308 7309 if (napi->thread) { 7310 kthread_stop(napi->thread); 7311 napi->thread = NULL; 7312 } 7313 } 7314 EXPORT_SYMBOL(__netif_napi_del_locked); 7315 7316 static int __napi_poll(struct napi_struct *n, bool *repoll) 7317 { 7318 int work, weight; 7319 7320 weight = n->weight; 7321 7322 /* This NAPI_STATE_SCHED test is for avoiding a race 7323 * with netpoll's poll_napi(). Only the entity which 7324 * obtains the lock and sees NAPI_STATE_SCHED set will 7325 * actually make the ->poll() call. Therefore we avoid 7326 * accidentally calling ->poll() when NAPI is not scheduled. 7327 */ 7328 work = 0; 7329 if (napi_is_scheduled(n)) { 7330 work = n->poll(n, weight); 7331 trace_napi_poll(n, work, weight); 7332 7333 xdp_do_check_flushed(n); 7334 } 7335 7336 if (unlikely(work > weight)) 7337 netdev_err_once(n->dev, "NAPI poll function %pS returned %d, exceeding its budget of %d.\n", 7338 n->poll, work, weight); 7339 7340 if (likely(work < weight)) 7341 return work; 7342 7343 /* Drivers must not modify the NAPI state if they 7344 * consume the entire weight. In such cases this code 7345 * still "owns" the NAPI instance and therefore can 7346 * move the instance around on the list at-will. 7347 */ 7348 if (unlikely(napi_disable_pending(n))) { 7349 napi_complete(n); 7350 return work; 7351 } 7352 7353 /* The NAPI context has more processing work, but busy-polling 7354 * is preferred. Exit early. 7355 */ 7356 if (napi_prefer_busy_poll(n)) { 7357 if (napi_complete_done(n, work)) { 7358 /* If timeout is not set, we need to make sure 7359 * that the NAPI is re-scheduled. 7360 */ 7361 napi_schedule(n); 7362 } 7363 return work; 7364 } 7365 7366 /* Flush too old packets. If HZ < 1000, flush all packets */ 7367 gro_flush(&n->gro, HZ >= 1000); 7368 gro_normal_list(&n->gro); 7369 7370 /* Some drivers may have called napi_schedule 7371 * prior to exhausting their budget. 7372 */ 7373 if (unlikely(!list_empty(&n->poll_list))) { 7374 pr_warn_once("%s: Budget exhausted after napi rescheduled\n", 7375 n->dev ? n->dev->name : "backlog"); 7376 return work; 7377 } 7378 7379 *repoll = true; 7380 7381 return work; 7382 } 7383 7384 static int napi_poll(struct napi_struct *n, struct list_head *repoll) 7385 { 7386 bool do_repoll = false; 7387 void *have; 7388 int work; 7389 7390 list_del_init(&n->poll_list); 7391 7392 have = netpoll_poll_lock(n); 7393 7394 work = __napi_poll(n, &do_repoll); 7395 7396 if (do_repoll) 7397 list_add_tail(&n->poll_list, repoll); 7398 7399 netpoll_poll_unlock(have); 7400 7401 return work; 7402 } 7403 7404 static int napi_thread_wait(struct napi_struct *napi) 7405 { 7406 set_current_state(TASK_INTERRUPTIBLE); 7407 7408 while (!kthread_should_stop()) { 7409 /* Testing SCHED_THREADED bit here to make sure the current 7410 * kthread owns this napi and could poll on this napi. 7411 * Testing SCHED bit is not enough because SCHED bit might be 7412 * set by some other busy poll thread or by napi_disable(). 7413 */ 7414 if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state)) { 7415 WARN_ON(!list_empty(&napi->poll_list)); 7416 __set_current_state(TASK_RUNNING); 7417 return 0; 7418 } 7419 7420 schedule(); 7421 set_current_state(TASK_INTERRUPTIBLE); 7422 } 7423 __set_current_state(TASK_RUNNING); 7424 7425 return -1; 7426 } 7427 7428 static void napi_threaded_poll_loop(struct napi_struct *napi) 7429 { 7430 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 7431 struct softnet_data *sd; 7432 unsigned long last_qs = jiffies; 7433 7434 for (;;) { 7435 bool repoll = false; 7436 void *have; 7437 7438 local_bh_disable(); 7439 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 7440 7441 sd = this_cpu_ptr(&softnet_data); 7442 sd->in_napi_threaded_poll = true; 7443 7444 have = netpoll_poll_lock(napi); 7445 __napi_poll(napi, &repoll); 7446 netpoll_poll_unlock(have); 7447 7448 sd->in_napi_threaded_poll = false; 7449 barrier(); 7450 7451 if (sd_has_rps_ipi_waiting(sd)) { 7452 local_irq_disable(); 7453 net_rps_action_and_irq_enable(sd); 7454 } 7455 skb_defer_free_flush(sd); 7456 bpf_net_ctx_clear(bpf_net_ctx); 7457 local_bh_enable(); 7458 7459 if (!repoll) 7460 break; 7461 7462 rcu_softirq_qs_periodic(last_qs); 7463 cond_resched(); 7464 } 7465 } 7466 7467 static int napi_threaded_poll(void *data) 7468 { 7469 struct napi_struct *napi = data; 7470 7471 while (!napi_thread_wait(napi)) 7472 napi_threaded_poll_loop(napi); 7473 7474 return 0; 7475 } 7476 7477 static __latent_entropy void net_rx_action(void) 7478 { 7479 struct softnet_data *sd = this_cpu_ptr(&softnet_data); 7480 unsigned long time_limit = jiffies + 7481 usecs_to_jiffies(READ_ONCE(net_hotdata.netdev_budget_usecs)); 7482 struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx; 7483 int budget = READ_ONCE(net_hotdata.netdev_budget); 7484 LIST_HEAD(list); 7485 LIST_HEAD(repoll); 7486 7487 bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx); 7488 start: 7489 sd->in_net_rx_action = true; 7490 local_irq_disable(); 7491 list_splice_init(&sd->poll_list, &list); 7492 local_irq_enable(); 7493 7494 for (;;) { 7495 struct napi_struct *n; 7496 7497 skb_defer_free_flush(sd); 7498 7499 if (list_empty(&list)) { 7500 if (list_empty(&repoll)) { 7501 sd->in_net_rx_action = false; 7502 barrier(); 7503 /* We need to check if ____napi_schedule() 7504 * had refilled poll_list while 7505 * sd->in_net_rx_action was true. 7506 */ 7507 if (!list_empty(&sd->poll_list)) 7508 goto start; 7509 if (!sd_has_rps_ipi_waiting(sd)) 7510 goto end; 7511 } 7512 break; 7513 } 7514 7515 n = list_first_entry(&list, struct napi_struct, poll_list); 7516 budget -= napi_poll(n, &repoll); 7517 7518 /* If softirq window is exhausted then punt. 7519 * Allow this to run for 2 jiffies since which will allow 7520 * an average latency of 1.5/HZ. 7521 */ 7522 if (unlikely(budget <= 0 || 7523 time_after_eq(jiffies, time_limit))) { 7524 sd->time_squeeze++; 7525 break; 7526 } 7527 } 7528 7529 local_irq_disable(); 7530 7531 list_splice_tail_init(&sd->poll_list, &list); 7532 list_splice_tail(&repoll, &list); 7533 list_splice(&list, &sd->poll_list); 7534 if (!list_empty(&sd->poll_list)) 7535 __raise_softirq_irqoff(NET_RX_SOFTIRQ); 7536 else 7537 sd->in_net_rx_action = false; 7538 7539 net_rps_action_and_irq_enable(sd); 7540 end: 7541 bpf_net_ctx_clear(bpf_net_ctx); 7542 } 7543 7544 struct netdev_adjacent { 7545 struct net_device *dev; 7546 netdevice_tracker dev_tracker; 7547 7548 /* upper master flag, there can only be one master device per list */ 7549 bool master; 7550 7551 /* lookup ignore flag */ 7552 bool ignore; 7553 7554 /* counter for the number of times this device was added to us */ 7555 u16 ref_nr; 7556 7557 /* private field for the users */ 7558 void *private; 7559 7560 struct list_head list; 7561 struct rcu_head rcu; 7562 }; 7563 7564 static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev, 7565 struct list_head *adj_list) 7566 { 7567 struct netdev_adjacent *adj; 7568 7569 list_for_each_entry(adj, adj_list, list) { 7570 if (adj->dev == adj_dev) 7571 return adj; 7572 } 7573 return NULL; 7574 } 7575 7576 static int ____netdev_has_upper_dev(struct net_device *upper_dev, 7577 struct netdev_nested_priv *priv) 7578 { 7579 struct net_device *dev = (struct net_device *)priv->data; 7580 7581 return upper_dev == dev; 7582 } 7583 7584 /** 7585 * netdev_has_upper_dev - Check if device is linked to an upper device 7586 * @dev: device 7587 * @upper_dev: upper device to check 7588 * 7589 * Find out if a device is linked to specified upper device and return true 7590 * in case it is. Note that this checks only immediate upper device, 7591 * not through a complete stack of devices. The caller must hold the RTNL lock. 7592 */ 7593 bool netdev_has_upper_dev(struct net_device *dev, 7594 struct net_device *upper_dev) 7595 { 7596 struct netdev_nested_priv priv = { 7597 .data = (void *)upper_dev, 7598 }; 7599 7600 ASSERT_RTNL(); 7601 7602 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, 7603 &priv); 7604 } 7605 EXPORT_SYMBOL(netdev_has_upper_dev); 7606 7607 /** 7608 * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device 7609 * @dev: device 7610 * @upper_dev: upper device to check 7611 * 7612 * Find out if a device is linked to specified upper device and return true 7613 * in case it is. Note that this checks the entire upper device chain. 7614 * The caller must hold rcu lock. 7615 */ 7616 7617 bool netdev_has_upper_dev_all_rcu(struct net_device *dev, 7618 struct net_device *upper_dev) 7619 { 7620 struct netdev_nested_priv priv = { 7621 .data = (void *)upper_dev, 7622 }; 7623 7624 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, 7625 &priv); 7626 } 7627 EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu); 7628 7629 /** 7630 * netdev_has_any_upper_dev - Check if device is linked to some device 7631 * @dev: device 7632 * 7633 * Find out if a device is linked to an upper device and return true in case 7634 * it is. The caller must hold the RTNL lock. 7635 */ 7636 bool netdev_has_any_upper_dev(struct net_device *dev) 7637 { 7638 ASSERT_RTNL(); 7639 7640 return !list_empty(&dev->adj_list.upper); 7641 } 7642 EXPORT_SYMBOL(netdev_has_any_upper_dev); 7643 7644 /** 7645 * netdev_master_upper_dev_get - Get master upper device 7646 * @dev: device 7647 * 7648 * Find a master upper device and return pointer to it or NULL in case 7649 * it's not there. The caller must hold the RTNL lock. 7650 */ 7651 struct net_device *netdev_master_upper_dev_get(struct net_device *dev) 7652 { 7653 struct netdev_adjacent *upper; 7654 7655 ASSERT_RTNL(); 7656 7657 if (list_empty(&dev->adj_list.upper)) 7658 return NULL; 7659 7660 upper = list_first_entry(&dev->adj_list.upper, 7661 struct netdev_adjacent, list); 7662 if (likely(upper->master)) 7663 return upper->dev; 7664 return NULL; 7665 } 7666 EXPORT_SYMBOL(netdev_master_upper_dev_get); 7667 7668 static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev) 7669 { 7670 struct netdev_adjacent *upper; 7671 7672 ASSERT_RTNL(); 7673 7674 if (list_empty(&dev->adj_list.upper)) 7675 return NULL; 7676 7677 upper = list_first_entry(&dev->adj_list.upper, 7678 struct netdev_adjacent, list); 7679 if (likely(upper->master) && !upper->ignore) 7680 return upper->dev; 7681 return NULL; 7682 } 7683 7684 /** 7685 * netdev_has_any_lower_dev - Check if device is linked to some device 7686 * @dev: device 7687 * 7688 * Find out if a device is linked to a lower device and return true in case 7689 * it is. The caller must hold the RTNL lock. 7690 */ 7691 static bool netdev_has_any_lower_dev(struct net_device *dev) 7692 { 7693 ASSERT_RTNL(); 7694 7695 return !list_empty(&dev->adj_list.lower); 7696 } 7697 7698 void *netdev_adjacent_get_private(struct list_head *adj_list) 7699 { 7700 struct netdev_adjacent *adj; 7701 7702 adj = list_entry(adj_list, struct netdev_adjacent, list); 7703 7704 return adj->private; 7705 } 7706 EXPORT_SYMBOL(netdev_adjacent_get_private); 7707 7708 /** 7709 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list 7710 * @dev: device 7711 * @iter: list_head ** of the current position 7712 * 7713 * Gets the next device from the dev's upper list, starting from iter 7714 * position. The caller must hold RCU read lock. 7715 */ 7716 struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, 7717 struct list_head **iter) 7718 { 7719 struct netdev_adjacent *upper; 7720 7721 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); 7722 7723 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); 7724 7725 if (&upper->list == &dev->adj_list.upper) 7726 return NULL; 7727 7728 *iter = &upper->list; 7729 7730 return upper->dev; 7731 } 7732 EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); 7733 7734 static struct net_device *__netdev_next_upper_dev(struct net_device *dev, 7735 struct list_head **iter, 7736 bool *ignore) 7737 { 7738 struct netdev_adjacent *upper; 7739 7740 upper = list_entry((*iter)->next, struct netdev_adjacent, list); 7741 7742 if (&upper->list == &dev->adj_list.upper) 7743 return NULL; 7744 7745 *iter = &upper->list; 7746 *ignore = upper->ignore; 7747 7748 return upper->dev; 7749 } 7750 7751 static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev, 7752 struct list_head **iter) 7753 { 7754 struct netdev_adjacent *upper; 7755 7756 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); 7757 7758 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); 7759 7760 if (&upper->list == &dev->adj_list.upper) 7761 return NULL; 7762 7763 *iter = &upper->list; 7764 7765 return upper->dev; 7766 } 7767 7768 static int __netdev_walk_all_upper_dev(struct net_device *dev, 7769 int (*fn)(struct net_device *dev, 7770 struct netdev_nested_priv *priv), 7771 struct netdev_nested_priv *priv) 7772 { 7773 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; 7774 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; 7775 int ret, cur = 0; 7776 bool ignore; 7777 7778 now = dev; 7779 iter = &dev->adj_list.upper; 7780 7781 while (1) { 7782 if (now != dev) { 7783 ret = fn(now, priv); 7784 if (ret) 7785 return ret; 7786 } 7787 7788 next = NULL; 7789 while (1) { 7790 udev = __netdev_next_upper_dev(now, &iter, &ignore); 7791 if (!udev) 7792 break; 7793 if (ignore) 7794 continue; 7795 7796 next = udev; 7797 niter = &udev->adj_list.upper; 7798 dev_stack[cur] = now; 7799 iter_stack[cur++] = iter; 7800 break; 7801 } 7802 7803 if (!next) { 7804 if (!cur) 7805 return 0; 7806 next = dev_stack[--cur]; 7807 niter = iter_stack[cur]; 7808 } 7809 7810 now = next; 7811 iter = niter; 7812 } 7813 7814 return 0; 7815 } 7816 7817 int netdev_walk_all_upper_dev_rcu(struct net_device *dev, 7818 int (*fn)(struct net_device *dev, 7819 struct netdev_nested_priv *priv), 7820 struct netdev_nested_priv *priv) 7821 { 7822 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; 7823 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; 7824 int ret, cur = 0; 7825 7826 now = dev; 7827 iter = &dev->adj_list.upper; 7828 7829 while (1) { 7830 if (now != dev) { 7831 ret = fn(now, priv); 7832 if (ret) 7833 return ret; 7834 } 7835 7836 next = NULL; 7837 while (1) { 7838 udev = netdev_next_upper_dev_rcu(now, &iter); 7839 if (!udev) 7840 break; 7841 7842 next = udev; 7843 niter = &udev->adj_list.upper; 7844 dev_stack[cur] = now; 7845 iter_stack[cur++] = iter; 7846 break; 7847 } 7848 7849 if (!next) { 7850 if (!cur) 7851 return 0; 7852 next = dev_stack[--cur]; 7853 niter = iter_stack[cur]; 7854 } 7855 7856 now = next; 7857 iter = niter; 7858 } 7859 7860 return 0; 7861 } 7862 EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu); 7863 7864 static bool __netdev_has_upper_dev(struct net_device *dev, 7865 struct net_device *upper_dev) 7866 { 7867 struct netdev_nested_priv priv = { 7868 .flags = 0, 7869 .data = (void *)upper_dev, 7870 }; 7871 7872 ASSERT_RTNL(); 7873 7874 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev, 7875 &priv); 7876 } 7877 7878 /** 7879 * netdev_lower_get_next_private - Get the next ->private from the 7880 * lower neighbour list 7881 * @dev: device 7882 * @iter: list_head ** of the current position 7883 * 7884 * Gets the next netdev_adjacent->private from the dev's lower neighbour 7885 * list, starting from iter position. The caller must hold either hold the 7886 * RTNL lock or its own locking that guarantees that the neighbour lower 7887 * list will remain unchanged. 7888 */ 7889 void *netdev_lower_get_next_private(struct net_device *dev, 7890 struct list_head **iter) 7891 { 7892 struct netdev_adjacent *lower; 7893 7894 lower = list_entry(*iter, struct netdev_adjacent, list); 7895 7896 if (&lower->list == &dev->adj_list.lower) 7897 return NULL; 7898 7899 *iter = lower->list.next; 7900 7901 return lower->private; 7902 } 7903 EXPORT_SYMBOL(netdev_lower_get_next_private); 7904 7905 /** 7906 * netdev_lower_get_next_private_rcu - Get the next ->private from the 7907 * lower neighbour list, RCU 7908 * variant 7909 * @dev: device 7910 * @iter: list_head ** of the current position 7911 * 7912 * Gets the next netdev_adjacent->private from the dev's lower neighbour 7913 * list, starting from iter position. The caller must hold RCU read lock. 7914 */ 7915 void *netdev_lower_get_next_private_rcu(struct net_device *dev, 7916 struct list_head **iter) 7917 { 7918 struct netdev_adjacent *lower; 7919 7920 WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held()); 7921 7922 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); 7923 7924 if (&lower->list == &dev->adj_list.lower) 7925 return NULL; 7926 7927 *iter = &lower->list; 7928 7929 return lower->private; 7930 } 7931 EXPORT_SYMBOL(netdev_lower_get_next_private_rcu); 7932 7933 /** 7934 * netdev_lower_get_next - Get the next device from the lower neighbour 7935 * list 7936 * @dev: device 7937 * @iter: list_head ** of the current position 7938 * 7939 * Gets the next netdev_adjacent from the dev's lower neighbour 7940 * list, starting from iter position. The caller must hold RTNL lock or 7941 * its own locking that guarantees that the neighbour lower 7942 * list will remain unchanged. 7943 */ 7944 void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter) 7945 { 7946 struct netdev_adjacent *lower; 7947 7948 lower = list_entry(*iter, struct netdev_adjacent, list); 7949 7950 if (&lower->list == &dev->adj_list.lower) 7951 return NULL; 7952 7953 *iter = lower->list.next; 7954 7955 return lower->dev; 7956 } 7957 EXPORT_SYMBOL(netdev_lower_get_next); 7958 7959 static struct net_device *netdev_next_lower_dev(struct net_device *dev, 7960 struct list_head **iter) 7961 { 7962 struct netdev_adjacent *lower; 7963 7964 lower = list_entry((*iter)->next, struct netdev_adjacent, list); 7965 7966 if (&lower->list == &dev->adj_list.lower) 7967 return NULL; 7968 7969 *iter = &lower->list; 7970 7971 return lower->dev; 7972 } 7973 7974 static struct net_device *__netdev_next_lower_dev(struct net_device *dev, 7975 struct list_head **iter, 7976 bool *ignore) 7977 { 7978 struct netdev_adjacent *lower; 7979 7980 lower = list_entry((*iter)->next, struct netdev_adjacent, list); 7981 7982 if (&lower->list == &dev->adj_list.lower) 7983 return NULL; 7984 7985 *iter = &lower->list; 7986 *ignore = lower->ignore; 7987 7988 return lower->dev; 7989 } 7990 7991 int netdev_walk_all_lower_dev(struct net_device *dev, 7992 int (*fn)(struct net_device *dev, 7993 struct netdev_nested_priv *priv), 7994 struct netdev_nested_priv *priv) 7995 { 7996 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; 7997 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; 7998 int ret, cur = 0; 7999 8000 now = dev; 8001 iter = &dev->adj_list.lower; 8002 8003 while (1) { 8004 if (now != dev) { 8005 ret = fn(now, priv); 8006 if (ret) 8007 return ret; 8008 } 8009 8010 next = NULL; 8011 while (1) { 8012 ldev = netdev_next_lower_dev(now, &iter); 8013 if (!ldev) 8014 break; 8015 8016 next = ldev; 8017 niter = &ldev->adj_list.lower; 8018 dev_stack[cur] = now; 8019 iter_stack[cur++] = iter; 8020 break; 8021 } 8022 8023 if (!next) { 8024 if (!cur) 8025 return 0; 8026 next = dev_stack[--cur]; 8027 niter = iter_stack[cur]; 8028 } 8029 8030 now = next; 8031 iter = niter; 8032 } 8033 8034 return 0; 8035 } 8036 EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev); 8037 8038 static int __netdev_walk_all_lower_dev(struct net_device *dev, 8039 int (*fn)(struct net_device *dev, 8040 struct netdev_nested_priv *priv), 8041 struct netdev_nested_priv *priv) 8042 { 8043 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; 8044 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; 8045 int ret, cur = 0; 8046 bool ignore; 8047 8048 now = dev; 8049 iter = &dev->adj_list.lower; 8050 8051 while (1) { 8052 if (now != dev) { 8053 ret = fn(now, priv); 8054 if (ret) 8055 return ret; 8056 } 8057 8058 next = NULL; 8059 while (1) { 8060 ldev = __netdev_next_lower_dev(now, &iter, &ignore); 8061 if (!ldev) 8062 break; 8063 if (ignore) 8064 continue; 8065 8066 next = ldev; 8067 niter = &ldev->adj_list.lower; 8068 dev_stack[cur] = now; 8069 iter_stack[cur++] = iter; 8070 break; 8071 } 8072 8073 if (!next) { 8074 if (!cur) 8075 return 0; 8076 next = dev_stack[--cur]; 8077 niter = iter_stack[cur]; 8078 } 8079 8080 now = next; 8081 iter = niter; 8082 } 8083 8084 return 0; 8085 } 8086 8087 struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev, 8088 struct list_head **iter) 8089 { 8090 struct netdev_adjacent *lower; 8091 8092 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); 8093 if (&lower->list == &dev->adj_list.lower) 8094 return NULL; 8095 8096 *iter = &lower->list; 8097 8098 return lower->dev; 8099 } 8100 EXPORT_SYMBOL(netdev_next_lower_dev_rcu); 8101 8102 static u8 __netdev_upper_depth(struct net_device *dev) 8103 { 8104 struct net_device *udev; 8105 struct list_head *iter; 8106 u8 max_depth = 0; 8107 bool ignore; 8108 8109 for (iter = &dev->adj_list.upper, 8110 udev = __netdev_next_upper_dev(dev, &iter, &ignore); 8111 udev; 8112 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) { 8113 if (ignore) 8114 continue; 8115 if (max_depth < udev->upper_level) 8116 max_depth = udev->upper_level; 8117 } 8118 8119 return max_depth; 8120 } 8121 8122 static u8 __netdev_lower_depth(struct net_device *dev) 8123 { 8124 struct net_device *ldev; 8125 struct list_head *iter; 8126 u8 max_depth = 0; 8127 bool ignore; 8128 8129 for (iter = &dev->adj_list.lower, 8130 ldev = __netdev_next_lower_dev(dev, &iter, &ignore); 8131 ldev; 8132 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) { 8133 if (ignore) 8134 continue; 8135 if (max_depth < ldev->lower_level) 8136 max_depth = ldev->lower_level; 8137 } 8138 8139 return max_depth; 8140 } 8141 8142 static int __netdev_update_upper_level(struct net_device *dev, 8143 struct netdev_nested_priv *__unused) 8144 { 8145 dev->upper_level = __netdev_upper_depth(dev) + 1; 8146 return 0; 8147 } 8148 8149 #ifdef CONFIG_LOCKDEP 8150 static LIST_HEAD(net_unlink_list); 8151 8152 static void net_unlink_todo(struct net_device *dev) 8153 { 8154 if (list_empty(&dev->unlink_list)) 8155 list_add_tail(&dev->unlink_list, &net_unlink_list); 8156 } 8157 #endif 8158 8159 static int __netdev_update_lower_level(struct net_device *dev, 8160 struct netdev_nested_priv *priv) 8161 { 8162 dev->lower_level = __netdev_lower_depth(dev) + 1; 8163 8164 #ifdef CONFIG_LOCKDEP 8165 if (!priv) 8166 return 0; 8167 8168 if (priv->flags & NESTED_SYNC_IMM) 8169 dev->nested_level = dev->lower_level - 1; 8170 if (priv->flags & NESTED_SYNC_TODO) 8171 net_unlink_todo(dev); 8172 #endif 8173 return 0; 8174 } 8175 8176 int netdev_walk_all_lower_dev_rcu(struct net_device *dev, 8177 int (*fn)(struct net_device *dev, 8178 struct netdev_nested_priv *priv), 8179 struct netdev_nested_priv *priv) 8180 { 8181 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; 8182 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; 8183 int ret, cur = 0; 8184 8185 now = dev; 8186 iter = &dev->adj_list.lower; 8187 8188 while (1) { 8189 if (now != dev) { 8190 ret = fn(now, priv); 8191 if (ret) 8192 return ret; 8193 } 8194 8195 next = NULL; 8196 while (1) { 8197 ldev = netdev_next_lower_dev_rcu(now, &iter); 8198 if (!ldev) 8199 break; 8200 8201 next = ldev; 8202 niter = &ldev->adj_list.lower; 8203 dev_stack[cur] = now; 8204 iter_stack[cur++] = iter; 8205 break; 8206 } 8207 8208 if (!next) { 8209 if (!cur) 8210 return 0; 8211 next = dev_stack[--cur]; 8212 niter = iter_stack[cur]; 8213 } 8214 8215 now = next; 8216 iter = niter; 8217 } 8218 8219 return 0; 8220 } 8221 EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu); 8222 8223 /** 8224 * netdev_lower_get_first_private_rcu - Get the first ->private from the 8225 * lower neighbour list, RCU 8226 * variant 8227 * @dev: device 8228 * 8229 * Gets the first netdev_adjacent->private from the dev's lower neighbour 8230 * list. The caller must hold RCU read lock. 8231 */ 8232 void *netdev_lower_get_first_private_rcu(struct net_device *dev) 8233 { 8234 struct netdev_adjacent *lower; 8235 8236 lower = list_first_or_null_rcu(&dev->adj_list.lower, 8237 struct netdev_adjacent, list); 8238 if (lower) 8239 return lower->private; 8240 return NULL; 8241 } 8242 EXPORT_SYMBOL(netdev_lower_get_first_private_rcu); 8243 8244 /** 8245 * netdev_master_upper_dev_get_rcu - Get master upper device 8246 * @dev: device 8247 * 8248 * Find a master upper device and return pointer to it or NULL in case 8249 * it's not there. The caller must hold the RCU read lock. 8250 */ 8251 struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) 8252 { 8253 struct netdev_adjacent *upper; 8254 8255 upper = list_first_or_null_rcu(&dev->adj_list.upper, 8256 struct netdev_adjacent, list); 8257 if (upper && likely(upper->master)) 8258 return upper->dev; 8259 return NULL; 8260 } 8261 EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); 8262 8263 static int netdev_adjacent_sysfs_add(struct net_device *dev, 8264 struct net_device *adj_dev, 8265 struct list_head *dev_list) 8266 { 8267 char linkname[IFNAMSIZ+7]; 8268 8269 sprintf(linkname, dev_list == &dev->adj_list.upper ? 8270 "upper_%s" : "lower_%s", adj_dev->name); 8271 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj), 8272 linkname); 8273 } 8274 static void netdev_adjacent_sysfs_del(struct net_device *dev, 8275 char *name, 8276 struct list_head *dev_list) 8277 { 8278 char linkname[IFNAMSIZ+7]; 8279 8280 sprintf(linkname, dev_list == &dev->adj_list.upper ? 8281 "upper_%s" : "lower_%s", name); 8282 sysfs_remove_link(&(dev->dev.kobj), linkname); 8283 } 8284 8285 static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev, 8286 struct net_device *adj_dev, 8287 struct list_head *dev_list) 8288 { 8289 return (dev_list == &dev->adj_list.upper || 8290 dev_list == &dev->adj_list.lower) && 8291 net_eq(dev_net(dev), dev_net(adj_dev)); 8292 } 8293 8294 static int __netdev_adjacent_dev_insert(struct net_device *dev, 8295 struct net_device *adj_dev, 8296 struct list_head *dev_list, 8297 void *private, bool master) 8298 { 8299 struct netdev_adjacent *adj; 8300 int ret; 8301 8302 adj = __netdev_find_adj(adj_dev, dev_list); 8303 8304 if (adj) { 8305 adj->ref_nr += 1; 8306 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n", 8307 dev->name, adj_dev->name, adj->ref_nr); 8308 8309 return 0; 8310 } 8311 8312 adj = kmalloc(sizeof(*adj), GFP_KERNEL); 8313 if (!adj) 8314 return -ENOMEM; 8315 8316 adj->dev = adj_dev; 8317 adj->master = master; 8318 adj->ref_nr = 1; 8319 adj->private = private; 8320 adj->ignore = false; 8321 netdev_hold(adj_dev, &adj->dev_tracker, GFP_KERNEL); 8322 8323 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n", 8324 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name); 8325 8326 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) { 8327 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list); 8328 if (ret) 8329 goto free_adj; 8330 } 8331 8332 /* Ensure that master link is always the first item in list. */ 8333 if (master) { 8334 ret = sysfs_create_link(&(dev->dev.kobj), 8335 &(adj_dev->dev.kobj), "master"); 8336 if (ret) 8337 goto remove_symlinks; 8338 8339 list_add_rcu(&adj->list, dev_list); 8340 } else { 8341 list_add_tail_rcu(&adj->list, dev_list); 8342 } 8343 8344 return 0; 8345 8346 remove_symlinks: 8347 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) 8348 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); 8349 free_adj: 8350 netdev_put(adj_dev, &adj->dev_tracker); 8351 kfree(adj); 8352 8353 return ret; 8354 } 8355 8356 static void __netdev_adjacent_dev_remove(struct net_device *dev, 8357 struct net_device *adj_dev, 8358 u16 ref_nr, 8359 struct list_head *dev_list) 8360 { 8361 struct netdev_adjacent *adj; 8362 8363 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n", 8364 dev->name, adj_dev->name, ref_nr); 8365 8366 adj = __netdev_find_adj(adj_dev, dev_list); 8367 8368 if (!adj) { 8369 pr_err("Adjacency does not exist for device %s from %s\n", 8370 dev->name, adj_dev->name); 8371 WARN_ON(1); 8372 return; 8373 } 8374 8375 if (adj->ref_nr > ref_nr) { 8376 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n", 8377 dev->name, adj_dev->name, ref_nr, 8378 adj->ref_nr - ref_nr); 8379 adj->ref_nr -= ref_nr; 8380 return; 8381 } 8382 8383 if (adj->master) 8384 sysfs_remove_link(&(dev->dev.kobj), "master"); 8385 8386 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) 8387 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); 8388 8389 list_del_rcu(&adj->list); 8390 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n", 8391 adj_dev->name, dev->name, adj_dev->name); 8392 netdev_put(adj_dev, &adj->dev_tracker); 8393 kfree_rcu(adj, rcu); 8394 } 8395 8396 static int __netdev_adjacent_dev_link_lists(struct net_device *dev, 8397 struct net_device *upper_dev, 8398 struct list_head *up_list, 8399 struct list_head *down_list, 8400 void *private, bool master) 8401 { 8402 int ret; 8403 8404 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, 8405 private, master); 8406 if (ret) 8407 return ret; 8408 8409 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, 8410 private, false); 8411 if (ret) { 8412 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list); 8413 return ret; 8414 } 8415 8416 return 0; 8417 } 8418 8419 static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev, 8420 struct net_device *upper_dev, 8421 u16 ref_nr, 8422 struct list_head *up_list, 8423 struct list_head *down_list) 8424 { 8425 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list); 8426 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list); 8427 } 8428 8429 static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, 8430 struct net_device *upper_dev, 8431 void *private, bool master) 8432 { 8433 return __netdev_adjacent_dev_link_lists(dev, upper_dev, 8434 &dev->adj_list.upper, 8435 &upper_dev->adj_list.lower, 8436 private, master); 8437 } 8438 8439 static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev, 8440 struct net_device *upper_dev) 8441 { 8442 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1, 8443 &dev->adj_list.upper, 8444 &upper_dev->adj_list.lower); 8445 } 8446 8447 static int __netdev_upper_dev_link(struct net_device *dev, 8448 struct net_device *upper_dev, bool master, 8449 void *upper_priv, void *upper_info, 8450 struct netdev_nested_priv *priv, 8451 struct netlink_ext_ack *extack) 8452 { 8453 struct netdev_notifier_changeupper_info changeupper_info = { 8454 .info = { 8455 .dev = dev, 8456 .extack = extack, 8457 }, 8458 .upper_dev = upper_dev, 8459 .master = master, 8460 .linking = true, 8461 .upper_info = upper_info, 8462 }; 8463 struct net_device *master_dev; 8464 int ret = 0; 8465 8466 ASSERT_RTNL(); 8467 8468 if (dev == upper_dev) 8469 return -EBUSY; 8470 8471 /* To prevent loops, check if dev is not upper device to upper_dev. */ 8472 if (__netdev_has_upper_dev(upper_dev, dev)) 8473 return -EBUSY; 8474 8475 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV) 8476 return -EMLINK; 8477 8478 if (!master) { 8479 if (__netdev_has_upper_dev(dev, upper_dev)) 8480 return -EEXIST; 8481 } else { 8482 master_dev = __netdev_master_upper_dev_get(dev); 8483 if (master_dev) 8484 return master_dev == upper_dev ? -EEXIST : -EBUSY; 8485 } 8486 8487 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, 8488 &changeupper_info.info); 8489 ret = notifier_to_errno(ret); 8490 if (ret) 8491 return ret; 8492 8493 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv, 8494 master); 8495 if (ret) 8496 return ret; 8497 8498 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, 8499 &changeupper_info.info); 8500 ret = notifier_to_errno(ret); 8501 if (ret) 8502 goto rollback; 8503 8504 __netdev_update_upper_level(dev, NULL); 8505 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); 8506 8507 __netdev_update_lower_level(upper_dev, priv); 8508 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, 8509 priv); 8510 8511 return 0; 8512 8513 rollback: 8514 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); 8515 8516 return ret; 8517 } 8518 8519 /** 8520 * netdev_upper_dev_link - Add a link to the upper device 8521 * @dev: device 8522 * @upper_dev: new upper device 8523 * @extack: netlink extended ack 8524 * 8525 * Adds a link to device which is upper to this one. The caller must hold 8526 * the RTNL lock. On a failure a negative errno code is returned. 8527 * On success the reference counts are adjusted and the function 8528 * returns zero. 8529 */ 8530 int netdev_upper_dev_link(struct net_device *dev, 8531 struct net_device *upper_dev, 8532 struct netlink_ext_ack *extack) 8533 { 8534 struct netdev_nested_priv priv = { 8535 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, 8536 .data = NULL, 8537 }; 8538 8539 return __netdev_upper_dev_link(dev, upper_dev, false, 8540 NULL, NULL, &priv, extack); 8541 } 8542 EXPORT_SYMBOL(netdev_upper_dev_link); 8543 8544 /** 8545 * netdev_master_upper_dev_link - Add a master link to the upper device 8546 * @dev: device 8547 * @upper_dev: new upper device 8548 * @upper_priv: upper device private 8549 * @upper_info: upper info to be passed down via notifier 8550 * @extack: netlink extended ack 8551 * 8552 * Adds a link to device which is upper to this one. In this case, only 8553 * one master upper device can be linked, although other non-master devices 8554 * might be linked as well. The caller must hold the RTNL lock. 8555 * On a failure a negative errno code is returned. On success the reference 8556 * counts are adjusted and the function returns zero. 8557 */ 8558 int netdev_master_upper_dev_link(struct net_device *dev, 8559 struct net_device *upper_dev, 8560 void *upper_priv, void *upper_info, 8561 struct netlink_ext_ack *extack) 8562 { 8563 struct netdev_nested_priv priv = { 8564 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, 8565 .data = NULL, 8566 }; 8567 8568 return __netdev_upper_dev_link(dev, upper_dev, true, 8569 upper_priv, upper_info, &priv, extack); 8570 } 8571 EXPORT_SYMBOL(netdev_master_upper_dev_link); 8572 8573 static void __netdev_upper_dev_unlink(struct net_device *dev, 8574 struct net_device *upper_dev, 8575 struct netdev_nested_priv *priv) 8576 { 8577 struct netdev_notifier_changeupper_info changeupper_info = { 8578 .info = { 8579 .dev = dev, 8580 }, 8581 .upper_dev = upper_dev, 8582 .linking = false, 8583 }; 8584 8585 ASSERT_RTNL(); 8586 8587 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev; 8588 8589 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, 8590 &changeupper_info.info); 8591 8592 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); 8593 8594 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, 8595 &changeupper_info.info); 8596 8597 __netdev_update_upper_level(dev, NULL); 8598 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); 8599 8600 __netdev_update_lower_level(upper_dev, priv); 8601 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, 8602 priv); 8603 } 8604 8605 /** 8606 * netdev_upper_dev_unlink - Removes a link to upper device 8607 * @dev: device 8608 * @upper_dev: new upper device 8609 * 8610 * Removes a link to device which is upper to this one. The caller must hold 8611 * the RTNL lock. 8612 */ 8613 void netdev_upper_dev_unlink(struct net_device *dev, 8614 struct net_device *upper_dev) 8615 { 8616 struct netdev_nested_priv priv = { 8617 .flags = NESTED_SYNC_TODO, 8618 .data = NULL, 8619 }; 8620 8621 __netdev_upper_dev_unlink(dev, upper_dev, &priv); 8622 } 8623 EXPORT_SYMBOL(netdev_upper_dev_unlink); 8624 8625 static void __netdev_adjacent_dev_set(struct net_device *upper_dev, 8626 struct net_device *lower_dev, 8627 bool val) 8628 { 8629 struct netdev_adjacent *adj; 8630 8631 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower); 8632 if (adj) 8633 adj->ignore = val; 8634 8635 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper); 8636 if (adj) 8637 adj->ignore = val; 8638 } 8639 8640 static void netdev_adjacent_dev_disable(struct net_device *upper_dev, 8641 struct net_device *lower_dev) 8642 { 8643 __netdev_adjacent_dev_set(upper_dev, lower_dev, true); 8644 } 8645 8646 static void netdev_adjacent_dev_enable(struct net_device *upper_dev, 8647 struct net_device *lower_dev) 8648 { 8649 __netdev_adjacent_dev_set(upper_dev, lower_dev, false); 8650 } 8651 8652 int netdev_adjacent_change_prepare(struct net_device *old_dev, 8653 struct net_device *new_dev, 8654 struct net_device *dev, 8655 struct netlink_ext_ack *extack) 8656 { 8657 struct netdev_nested_priv priv = { 8658 .flags = 0, 8659 .data = NULL, 8660 }; 8661 int err; 8662 8663 if (!new_dev) 8664 return 0; 8665 8666 if (old_dev && new_dev != old_dev) 8667 netdev_adjacent_dev_disable(dev, old_dev); 8668 err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv, 8669 extack); 8670 if (err) { 8671 if (old_dev && new_dev != old_dev) 8672 netdev_adjacent_dev_enable(dev, old_dev); 8673 return err; 8674 } 8675 8676 return 0; 8677 } 8678 EXPORT_SYMBOL(netdev_adjacent_change_prepare); 8679 8680 void netdev_adjacent_change_commit(struct net_device *old_dev, 8681 struct net_device *new_dev, 8682 struct net_device *dev) 8683 { 8684 struct netdev_nested_priv priv = { 8685 .flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO, 8686 .data = NULL, 8687 }; 8688 8689 if (!new_dev || !old_dev) 8690 return; 8691 8692 if (new_dev == old_dev) 8693 return; 8694 8695 netdev_adjacent_dev_enable(dev, old_dev); 8696 __netdev_upper_dev_unlink(old_dev, dev, &priv); 8697 } 8698 EXPORT_SYMBOL(netdev_adjacent_change_commit); 8699 8700 void netdev_adjacent_change_abort(struct net_device *old_dev, 8701 struct net_device *new_dev, 8702 struct net_device *dev) 8703 { 8704 struct netdev_nested_priv priv = { 8705 .flags = 0, 8706 .data = NULL, 8707 }; 8708 8709 if (!new_dev) 8710 return; 8711 8712 if (old_dev && new_dev != old_dev) 8713 netdev_adjacent_dev_enable(dev, old_dev); 8714 8715 __netdev_upper_dev_unlink(new_dev, dev, &priv); 8716 } 8717 EXPORT_SYMBOL(netdev_adjacent_change_abort); 8718 8719 /** 8720 * netdev_bonding_info_change - Dispatch event about slave change 8721 * @dev: device 8722 * @bonding_info: info to dispatch 8723 * 8724 * Send NETDEV_BONDING_INFO to netdev notifiers with info. 8725 * The caller must hold the RTNL lock. 8726 */ 8727 void netdev_bonding_info_change(struct net_device *dev, 8728 struct netdev_bonding_info *bonding_info) 8729 { 8730 struct netdev_notifier_bonding_info info = { 8731 .info.dev = dev, 8732 }; 8733 8734 memcpy(&info.bonding_info, bonding_info, 8735 sizeof(struct netdev_bonding_info)); 8736 call_netdevice_notifiers_info(NETDEV_BONDING_INFO, 8737 &info.info); 8738 } 8739 EXPORT_SYMBOL(netdev_bonding_info_change); 8740 8741 static int netdev_offload_xstats_enable_l3(struct net_device *dev, 8742 struct netlink_ext_ack *extack) 8743 { 8744 struct netdev_notifier_offload_xstats_info info = { 8745 .info.dev = dev, 8746 .info.extack = extack, 8747 .type = NETDEV_OFFLOAD_XSTATS_TYPE_L3, 8748 }; 8749 int err; 8750 int rc; 8751 8752 dev->offload_xstats_l3 = kzalloc(sizeof(*dev->offload_xstats_l3), 8753 GFP_KERNEL); 8754 if (!dev->offload_xstats_l3) 8755 return -ENOMEM; 8756 8757 rc = call_netdevice_notifiers_info_robust(NETDEV_OFFLOAD_XSTATS_ENABLE, 8758 NETDEV_OFFLOAD_XSTATS_DISABLE, 8759 &info.info); 8760 err = notifier_to_errno(rc); 8761 if (err) 8762 goto free_stats; 8763 8764 return 0; 8765 8766 free_stats: 8767 kfree(dev->offload_xstats_l3); 8768 dev->offload_xstats_l3 = NULL; 8769 return err; 8770 } 8771 8772 int netdev_offload_xstats_enable(struct net_device *dev, 8773 enum netdev_offload_xstats_type type, 8774 struct netlink_ext_ack *extack) 8775 { 8776 ASSERT_RTNL(); 8777 8778 if (netdev_offload_xstats_enabled(dev, type)) 8779 return -EALREADY; 8780 8781 switch (type) { 8782 case NETDEV_OFFLOAD_XSTATS_TYPE_L3: 8783 return netdev_offload_xstats_enable_l3(dev, extack); 8784 } 8785 8786 WARN_ON(1); 8787 return -EINVAL; 8788 } 8789 EXPORT_SYMBOL(netdev_offload_xstats_enable); 8790 8791 static void netdev_offload_xstats_disable_l3(struct net_device *dev) 8792 { 8793 struct netdev_notifier_offload_xstats_info info = { 8794 .info.dev = dev, 8795 .type = NETDEV_OFFLOAD_XSTATS_TYPE_L3, 8796 }; 8797 8798 call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_DISABLE, 8799 &info.info); 8800 kfree(dev->offload_xstats_l3); 8801 dev->offload_xstats_l3 = NULL; 8802 } 8803 8804 int netdev_offload_xstats_disable(struct net_device *dev, 8805 enum netdev_offload_xstats_type type) 8806 { 8807 ASSERT_RTNL(); 8808 8809 if (!netdev_offload_xstats_enabled(dev, type)) 8810 return -EALREADY; 8811 8812 switch (type) { 8813 case NETDEV_OFFLOAD_XSTATS_TYPE_L3: 8814 netdev_offload_xstats_disable_l3(dev); 8815 return 0; 8816 } 8817 8818 WARN_ON(1); 8819 return -EINVAL; 8820 } 8821 EXPORT_SYMBOL(netdev_offload_xstats_disable); 8822 8823 static void netdev_offload_xstats_disable_all(struct net_device *dev) 8824 { 8825 netdev_offload_xstats_disable(dev, NETDEV_OFFLOAD_XSTATS_TYPE_L3); 8826 } 8827 8828 static struct rtnl_hw_stats64 * 8829 netdev_offload_xstats_get_ptr(const struct net_device *dev, 8830 enum netdev_offload_xstats_type type) 8831 { 8832 switch (type) { 8833 case NETDEV_OFFLOAD_XSTATS_TYPE_L3: 8834 return dev->offload_xstats_l3; 8835 } 8836 8837 WARN_ON(1); 8838 return NULL; 8839 } 8840 8841 bool netdev_offload_xstats_enabled(const struct net_device *dev, 8842 enum netdev_offload_xstats_type type) 8843 { 8844 ASSERT_RTNL(); 8845 8846 return netdev_offload_xstats_get_ptr(dev, type); 8847 } 8848 EXPORT_SYMBOL(netdev_offload_xstats_enabled); 8849 8850 struct netdev_notifier_offload_xstats_ru { 8851 bool used; 8852 }; 8853 8854 struct netdev_notifier_offload_xstats_rd { 8855 struct rtnl_hw_stats64 stats; 8856 bool used; 8857 }; 8858 8859 static void netdev_hw_stats64_add(struct rtnl_hw_stats64 *dest, 8860 const struct rtnl_hw_stats64 *src) 8861 { 8862 dest->rx_packets += src->rx_packets; 8863 dest->tx_packets += src->tx_packets; 8864 dest->rx_bytes += src->rx_bytes; 8865 dest->tx_bytes += src->tx_bytes; 8866 dest->rx_errors += src->rx_errors; 8867 dest->tx_errors += src->tx_errors; 8868 dest->rx_dropped += src->rx_dropped; 8869 dest->tx_dropped += src->tx_dropped; 8870 dest->multicast += src->multicast; 8871 } 8872 8873 static int netdev_offload_xstats_get_used(struct net_device *dev, 8874 enum netdev_offload_xstats_type type, 8875 bool *p_used, 8876 struct netlink_ext_ack *extack) 8877 { 8878 struct netdev_notifier_offload_xstats_ru report_used = {}; 8879 struct netdev_notifier_offload_xstats_info info = { 8880 .info.dev = dev, 8881 .info.extack = extack, 8882 .type = type, 8883 .report_used = &report_used, 8884 }; 8885 int rc; 8886 8887 WARN_ON(!netdev_offload_xstats_enabled(dev, type)); 8888 rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_USED, 8889 &info.info); 8890 *p_used = report_used.used; 8891 return notifier_to_errno(rc); 8892 } 8893 8894 static int netdev_offload_xstats_get_stats(struct net_device *dev, 8895 enum netdev_offload_xstats_type type, 8896 struct rtnl_hw_stats64 *p_stats, 8897 bool *p_used, 8898 struct netlink_ext_ack *extack) 8899 { 8900 struct netdev_notifier_offload_xstats_rd report_delta = {}; 8901 struct netdev_notifier_offload_xstats_info info = { 8902 .info.dev = dev, 8903 .info.extack = extack, 8904 .type = type, 8905 .report_delta = &report_delta, 8906 }; 8907 struct rtnl_hw_stats64 *stats; 8908 int rc; 8909 8910 stats = netdev_offload_xstats_get_ptr(dev, type); 8911 if (WARN_ON(!stats)) 8912 return -EINVAL; 8913 8914 rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_DELTA, 8915 &info.info); 8916 8917 /* Cache whatever we got, even if there was an error, otherwise the 8918 * successful stats retrievals would get lost. 8919 */ 8920 netdev_hw_stats64_add(stats, &report_delta.stats); 8921 8922 if (p_stats) 8923 *p_stats = *stats; 8924 *p_used = report_delta.used; 8925 8926 return notifier_to_errno(rc); 8927 } 8928 8929 int netdev_offload_xstats_get(struct net_device *dev, 8930 enum netdev_offload_xstats_type type, 8931 struct rtnl_hw_stats64 *p_stats, bool *p_used, 8932 struct netlink_ext_ack *extack) 8933 { 8934 ASSERT_RTNL(); 8935 8936 if (p_stats) 8937 return netdev_offload_xstats_get_stats(dev, type, p_stats, 8938 p_used, extack); 8939 else 8940 return netdev_offload_xstats_get_used(dev, type, p_used, 8941 extack); 8942 } 8943 EXPORT_SYMBOL(netdev_offload_xstats_get); 8944 8945 void 8946 netdev_offload_xstats_report_delta(struct netdev_notifier_offload_xstats_rd *report_delta, 8947 const struct rtnl_hw_stats64 *stats) 8948 { 8949 report_delta->used = true; 8950 netdev_hw_stats64_add(&report_delta->stats, stats); 8951 } 8952 EXPORT_SYMBOL(netdev_offload_xstats_report_delta); 8953 8954 void 8955 netdev_offload_xstats_report_used(struct netdev_notifier_offload_xstats_ru *report_used) 8956 { 8957 report_used->used = true; 8958 } 8959 EXPORT_SYMBOL(netdev_offload_xstats_report_used); 8960 8961 void netdev_offload_xstats_push_delta(struct net_device *dev, 8962 enum netdev_offload_xstats_type type, 8963 const struct rtnl_hw_stats64 *p_stats) 8964 { 8965 struct rtnl_hw_stats64 *stats; 8966 8967 ASSERT_RTNL(); 8968 8969 stats = netdev_offload_xstats_get_ptr(dev, type); 8970 if (WARN_ON(!stats)) 8971 return; 8972 8973 netdev_hw_stats64_add(stats, p_stats); 8974 } 8975 EXPORT_SYMBOL(netdev_offload_xstats_push_delta); 8976 8977 /** 8978 * netdev_get_xmit_slave - Get the xmit slave of master device 8979 * @dev: device 8980 * @skb: The packet 8981 * @all_slaves: assume all the slaves are active 8982 * 8983 * The reference counters are not incremented so the caller must be 8984 * careful with locks. The caller must hold RCU lock. 8985 * %NULL is returned if no slave is found. 8986 */ 8987 8988 struct net_device *netdev_get_xmit_slave(struct net_device *dev, 8989 struct sk_buff *skb, 8990 bool all_slaves) 8991 { 8992 const struct net_device_ops *ops = dev->netdev_ops; 8993 8994 if (!ops->ndo_get_xmit_slave) 8995 return NULL; 8996 return ops->ndo_get_xmit_slave(dev, skb, all_slaves); 8997 } 8998 EXPORT_SYMBOL(netdev_get_xmit_slave); 8999 9000 static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev, 9001 struct sock *sk) 9002 { 9003 const struct net_device_ops *ops = dev->netdev_ops; 9004 9005 if (!ops->ndo_sk_get_lower_dev) 9006 return NULL; 9007 return ops->ndo_sk_get_lower_dev(dev, sk); 9008 } 9009 9010 /** 9011 * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket 9012 * @dev: device 9013 * @sk: the socket 9014 * 9015 * %NULL is returned if no lower device is found. 9016 */ 9017 9018 struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev, 9019 struct sock *sk) 9020 { 9021 struct net_device *lower; 9022 9023 lower = netdev_sk_get_lower_dev(dev, sk); 9024 while (lower) { 9025 dev = lower; 9026 lower = netdev_sk_get_lower_dev(dev, sk); 9027 } 9028 9029 return dev; 9030 } 9031 EXPORT_SYMBOL(netdev_sk_get_lowest_dev); 9032 9033 static void netdev_adjacent_add_links(struct net_device *dev) 9034 { 9035 struct netdev_adjacent *iter; 9036 9037 struct net *net = dev_net(dev); 9038 9039 list_for_each_entry(iter, &dev->adj_list.upper, list) { 9040 if (!net_eq(net, dev_net(iter->dev))) 9041 continue; 9042 netdev_adjacent_sysfs_add(iter->dev, dev, 9043 &iter->dev->adj_list.lower); 9044 netdev_adjacent_sysfs_add(dev, iter->dev, 9045 &dev->adj_list.upper); 9046 } 9047 9048 list_for_each_entry(iter, &dev->adj_list.lower, list) { 9049 if (!net_eq(net, dev_net(iter->dev))) 9050 continue; 9051 netdev_adjacent_sysfs_add(iter->dev, dev, 9052 &iter->dev->adj_list.upper); 9053 netdev_adjacent_sysfs_add(dev, iter->dev, 9054 &dev->adj_list.lower); 9055 } 9056 } 9057 9058 static void netdev_adjacent_del_links(struct net_device *dev) 9059 { 9060 struct netdev_adjacent *iter; 9061 9062 struct net *net = dev_net(dev); 9063 9064 list_for_each_entry(iter, &dev->adj_list.upper, list) { 9065 if (!net_eq(net, dev_net(iter->dev))) 9066 continue; 9067 netdev_adjacent_sysfs_del(iter->dev, dev->name, 9068 &iter->dev->adj_list.lower); 9069 netdev_adjacent_sysfs_del(dev, iter->dev->name, 9070 &dev->adj_list.upper); 9071 } 9072 9073 list_for_each_entry(iter, &dev->adj_list.lower, list) { 9074 if (!net_eq(net, dev_net(iter->dev))) 9075 continue; 9076 netdev_adjacent_sysfs_del(iter->dev, dev->name, 9077 &iter->dev->adj_list.upper); 9078 netdev_adjacent_sysfs_del(dev, iter->dev->name, 9079 &dev->adj_list.lower); 9080 } 9081 } 9082 9083 void netdev_adjacent_rename_links(struct net_device *dev, char *oldname) 9084 { 9085 struct netdev_adjacent *iter; 9086 9087 struct net *net = dev_net(dev); 9088 9089 list_for_each_entry(iter, &dev->adj_list.upper, list) { 9090 if (!net_eq(net, dev_net(iter->dev))) 9091 continue; 9092 netdev_adjacent_sysfs_del(iter->dev, oldname, 9093 &iter->dev->adj_list.lower); 9094 netdev_adjacent_sysfs_add(iter->dev, dev, 9095 &iter->dev->adj_list.lower); 9096 } 9097 9098 list_for_each_entry(iter, &dev->adj_list.lower, list) { 9099 if (!net_eq(net, dev_net(iter->dev))) 9100 continue; 9101 netdev_adjacent_sysfs_del(iter->dev, oldname, 9102 &iter->dev->adj_list.upper); 9103 netdev_adjacent_sysfs_add(iter->dev, dev, 9104 &iter->dev->adj_list.upper); 9105 } 9106 } 9107 9108 void *netdev_lower_dev_get_private(struct net_device *dev, 9109 struct net_device *lower_dev) 9110 { 9111 struct netdev_adjacent *lower; 9112 9113 if (!lower_dev) 9114 return NULL; 9115 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower); 9116 if (!lower) 9117 return NULL; 9118 9119 return lower->private; 9120 } 9121 EXPORT_SYMBOL(netdev_lower_dev_get_private); 9122 9123 9124 /** 9125 * netdev_lower_state_changed - Dispatch event about lower device state change 9126 * @lower_dev: device 9127 * @lower_state_info: state to dispatch 9128 * 9129 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info. 9130 * The caller must hold the RTNL lock. 9131 */ 9132 void netdev_lower_state_changed(struct net_device *lower_dev, 9133 void *lower_state_info) 9134 { 9135 struct netdev_notifier_changelowerstate_info changelowerstate_info = { 9136 .info.dev = lower_dev, 9137 }; 9138 9139 ASSERT_RTNL(); 9140 changelowerstate_info.lower_state_info = lower_state_info; 9141 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, 9142 &changelowerstate_info.info); 9143 } 9144 EXPORT_SYMBOL(netdev_lower_state_changed); 9145 9146 static void dev_change_rx_flags(struct net_device *dev, int flags) 9147 { 9148 const struct net_device_ops *ops = dev->netdev_ops; 9149 9150 if (ops->ndo_change_rx_flags) 9151 ops->ndo_change_rx_flags(dev, flags); 9152 } 9153 9154 static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify) 9155 { 9156 unsigned int old_flags = dev->flags; 9157 unsigned int promiscuity, flags; 9158 kuid_t uid; 9159 kgid_t gid; 9160 9161 ASSERT_RTNL(); 9162 9163 promiscuity = dev->promiscuity + inc; 9164 if (promiscuity == 0) { 9165 /* 9166 * Avoid overflow. 9167 * If inc causes overflow, untouch promisc and return error. 9168 */ 9169 if (unlikely(inc > 0)) { 9170 netdev_warn(dev, "promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n"); 9171 return -EOVERFLOW; 9172 } 9173 flags = old_flags & ~IFF_PROMISC; 9174 } else { 9175 flags = old_flags | IFF_PROMISC; 9176 } 9177 WRITE_ONCE(dev->promiscuity, promiscuity); 9178 if (flags != old_flags) { 9179 WRITE_ONCE(dev->flags, flags); 9180 netdev_info(dev, "%s promiscuous mode\n", 9181 dev->flags & IFF_PROMISC ? "entered" : "left"); 9182 if (audit_enabled) { 9183 current_uid_gid(&uid, &gid); 9184 audit_log(audit_context(), GFP_ATOMIC, 9185 AUDIT_ANOM_PROMISCUOUS, 9186 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", 9187 dev->name, (dev->flags & IFF_PROMISC), 9188 (old_flags & IFF_PROMISC), 9189 from_kuid(&init_user_ns, audit_get_loginuid(current)), 9190 from_kuid(&init_user_ns, uid), 9191 from_kgid(&init_user_ns, gid), 9192 audit_get_sessionid(current)); 9193 } 9194 9195 dev_change_rx_flags(dev, IFF_PROMISC); 9196 } 9197 if (notify) 9198 __dev_notify_flags(dev, old_flags, IFF_PROMISC, 0, NULL); 9199 return 0; 9200 } 9201 9202 /** 9203 * dev_set_promiscuity - update promiscuity count on a device 9204 * @dev: device 9205 * @inc: modifier 9206 * 9207 * Add or remove promiscuity from a device. While the count in the device 9208 * remains above zero the interface remains promiscuous. Once it hits zero 9209 * the device reverts back to normal filtering operation. A negative inc 9210 * value is used to drop promiscuity on the device. 9211 * Return 0 if successful or a negative errno code on error. 9212 */ 9213 int dev_set_promiscuity(struct net_device *dev, int inc) 9214 { 9215 unsigned int old_flags = dev->flags; 9216 int err; 9217 9218 err = __dev_set_promiscuity(dev, inc, true); 9219 if (err < 0) 9220 return err; 9221 if (dev->flags != old_flags) 9222 dev_set_rx_mode(dev); 9223 return err; 9224 } 9225 EXPORT_SYMBOL(dev_set_promiscuity); 9226 9227 int netif_set_allmulti(struct net_device *dev, int inc, bool notify) 9228 { 9229 unsigned int old_flags = dev->flags, old_gflags = dev->gflags; 9230 unsigned int allmulti, flags; 9231 9232 ASSERT_RTNL(); 9233 9234 allmulti = dev->allmulti + inc; 9235 if (allmulti == 0) { 9236 /* 9237 * Avoid overflow. 9238 * If inc causes overflow, untouch allmulti and return error. 9239 */ 9240 if (unlikely(inc > 0)) { 9241 netdev_warn(dev, "allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n"); 9242 return -EOVERFLOW; 9243 } 9244 flags = old_flags & ~IFF_ALLMULTI; 9245 } else { 9246 flags = old_flags | IFF_ALLMULTI; 9247 } 9248 WRITE_ONCE(dev->allmulti, allmulti); 9249 if (flags != old_flags) { 9250 WRITE_ONCE(dev->flags, flags); 9251 netdev_info(dev, "%s allmulticast mode\n", 9252 dev->flags & IFF_ALLMULTI ? "entered" : "left"); 9253 dev_change_rx_flags(dev, IFF_ALLMULTI); 9254 dev_set_rx_mode(dev); 9255 if (notify) 9256 __dev_notify_flags(dev, old_flags, 9257 dev->gflags ^ old_gflags, 0, NULL); 9258 } 9259 return 0; 9260 } 9261 9262 /* 9263 * Upload unicast and multicast address lists to device and 9264 * configure RX filtering. When the device doesn't support unicast 9265 * filtering it is put in promiscuous mode while unicast addresses 9266 * are present. 9267 */ 9268 void __dev_set_rx_mode(struct net_device *dev) 9269 { 9270 const struct net_device_ops *ops = dev->netdev_ops; 9271 9272 /* dev_open will call this function so the list will stay sane. */ 9273 if (!(dev->flags&IFF_UP)) 9274 return; 9275 9276 if (!netif_device_present(dev)) 9277 return; 9278 9279 if (!(dev->priv_flags & IFF_UNICAST_FLT)) { 9280 /* Unicast addresses changes may only happen under the rtnl, 9281 * therefore calling __dev_set_promiscuity here is safe. 9282 */ 9283 if (!netdev_uc_empty(dev) && !dev->uc_promisc) { 9284 __dev_set_promiscuity(dev, 1, false); 9285 dev->uc_promisc = true; 9286 } else if (netdev_uc_empty(dev) && dev->uc_promisc) { 9287 __dev_set_promiscuity(dev, -1, false); 9288 dev->uc_promisc = false; 9289 } 9290 } 9291 9292 if (ops->ndo_set_rx_mode) 9293 ops->ndo_set_rx_mode(dev); 9294 } 9295 9296 void dev_set_rx_mode(struct net_device *dev) 9297 { 9298 netif_addr_lock_bh(dev); 9299 __dev_set_rx_mode(dev); 9300 netif_addr_unlock_bh(dev); 9301 } 9302 9303 /** 9304 * dev_get_flags - get flags reported to userspace 9305 * @dev: device 9306 * 9307 * Get the combination of flag bits exported through APIs to userspace. 9308 */ 9309 unsigned int dev_get_flags(const struct net_device *dev) 9310 { 9311 unsigned int flags; 9312 9313 flags = (READ_ONCE(dev->flags) & ~(IFF_PROMISC | 9314 IFF_ALLMULTI | 9315 IFF_RUNNING | 9316 IFF_LOWER_UP | 9317 IFF_DORMANT)) | 9318 (READ_ONCE(dev->gflags) & (IFF_PROMISC | 9319 IFF_ALLMULTI)); 9320 9321 if (netif_running(dev)) { 9322 if (netif_oper_up(dev)) 9323 flags |= IFF_RUNNING; 9324 if (netif_carrier_ok(dev)) 9325 flags |= IFF_LOWER_UP; 9326 if (netif_dormant(dev)) 9327 flags |= IFF_DORMANT; 9328 } 9329 9330 return flags; 9331 } 9332 EXPORT_SYMBOL(dev_get_flags); 9333 9334 int __dev_change_flags(struct net_device *dev, unsigned int flags, 9335 struct netlink_ext_ack *extack) 9336 { 9337 unsigned int old_flags = dev->flags; 9338 int ret; 9339 9340 ASSERT_RTNL(); 9341 9342 /* 9343 * Set the flags on our device. 9344 */ 9345 9346 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | 9347 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | 9348 IFF_AUTOMEDIA)) | 9349 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | 9350 IFF_ALLMULTI)); 9351 9352 /* 9353 * Load in the correct multicast list now the flags have changed. 9354 */ 9355 9356 if ((old_flags ^ flags) & IFF_MULTICAST) 9357 dev_change_rx_flags(dev, IFF_MULTICAST); 9358 9359 dev_set_rx_mode(dev); 9360 9361 /* 9362 * Have we downed the interface. We handle IFF_UP ourselves 9363 * according to user attempts to set it, rather than blindly 9364 * setting it. 9365 */ 9366 9367 ret = 0; 9368 if ((old_flags ^ flags) & IFF_UP) { 9369 if (old_flags & IFF_UP) 9370 __dev_close(dev); 9371 else 9372 ret = __dev_open(dev, extack); 9373 } 9374 9375 if ((flags ^ dev->gflags) & IFF_PROMISC) { 9376 int inc = (flags & IFF_PROMISC) ? 1 : -1; 9377 old_flags = dev->flags; 9378 9379 dev->gflags ^= IFF_PROMISC; 9380 9381 if (__dev_set_promiscuity(dev, inc, false) >= 0) 9382 if (dev->flags != old_flags) 9383 dev_set_rx_mode(dev); 9384 } 9385 9386 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI 9387 * is important. Some (broken) drivers set IFF_PROMISC, when 9388 * IFF_ALLMULTI is requested not asking us and not reporting. 9389 */ 9390 if ((flags ^ dev->gflags) & IFF_ALLMULTI) { 9391 int inc = (flags & IFF_ALLMULTI) ? 1 : -1; 9392 9393 dev->gflags ^= IFF_ALLMULTI; 9394 netif_set_allmulti(dev, inc, false); 9395 } 9396 9397 return ret; 9398 } 9399 9400 void __dev_notify_flags(struct net_device *dev, unsigned int old_flags, 9401 unsigned int gchanges, u32 portid, 9402 const struct nlmsghdr *nlh) 9403 { 9404 unsigned int changes = dev->flags ^ old_flags; 9405 9406 if (gchanges) 9407 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC, portid, nlh); 9408 9409 if (changes & IFF_UP) { 9410 if (dev->flags & IFF_UP) 9411 call_netdevice_notifiers(NETDEV_UP, dev); 9412 else 9413 call_netdevice_notifiers(NETDEV_DOWN, dev); 9414 } 9415 9416 if (dev->flags & IFF_UP && 9417 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { 9418 struct netdev_notifier_change_info change_info = { 9419 .info = { 9420 .dev = dev, 9421 }, 9422 .flags_changed = changes, 9423 }; 9424 9425 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info); 9426 } 9427 } 9428 9429 int netif_change_flags(struct net_device *dev, unsigned int flags, 9430 struct netlink_ext_ack *extack) 9431 { 9432 int ret; 9433 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags; 9434 9435 ret = __dev_change_flags(dev, flags, extack); 9436 if (ret < 0) 9437 return ret; 9438 9439 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags); 9440 __dev_notify_flags(dev, old_flags, changes, 0, NULL); 9441 return ret; 9442 } 9443 9444 int __dev_set_mtu(struct net_device *dev, int new_mtu) 9445 { 9446 const struct net_device_ops *ops = dev->netdev_ops; 9447 9448 if (ops->ndo_change_mtu) 9449 return ops->ndo_change_mtu(dev, new_mtu); 9450 9451 /* Pairs with all the lockless reads of dev->mtu in the stack */ 9452 WRITE_ONCE(dev->mtu, new_mtu); 9453 return 0; 9454 } 9455 EXPORT_SYMBOL(__dev_set_mtu); 9456 9457 int dev_validate_mtu(struct net_device *dev, int new_mtu, 9458 struct netlink_ext_ack *extack) 9459 { 9460 /* MTU must be positive, and in range */ 9461 if (new_mtu < 0 || new_mtu < dev->min_mtu) { 9462 NL_SET_ERR_MSG(extack, "mtu less than device minimum"); 9463 return -EINVAL; 9464 } 9465 9466 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) { 9467 NL_SET_ERR_MSG(extack, "mtu greater than device maximum"); 9468 return -EINVAL; 9469 } 9470 return 0; 9471 } 9472 9473 /** 9474 * netif_set_mtu_ext - Change maximum transfer unit 9475 * @dev: device 9476 * @new_mtu: new transfer unit 9477 * @extack: netlink extended ack 9478 * 9479 * Change the maximum transfer size of the network device. 9480 */ 9481 int netif_set_mtu_ext(struct net_device *dev, int new_mtu, 9482 struct netlink_ext_ack *extack) 9483 { 9484 int err, orig_mtu; 9485 9486 if (new_mtu == dev->mtu) 9487 return 0; 9488 9489 err = dev_validate_mtu(dev, new_mtu, extack); 9490 if (err) 9491 return err; 9492 9493 if (!netif_device_present(dev)) 9494 return -ENODEV; 9495 9496 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev); 9497 err = notifier_to_errno(err); 9498 if (err) 9499 return err; 9500 9501 orig_mtu = dev->mtu; 9502 err = __dev_set_mtu(dev, new_mtu); 9503 9504 if (!err) { 9505 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, 9506 orig_mtu); 9507 err = notifier_to_errno(err); 9508 if (err) { 9509 /* setting mtu back and notifying everyone again, 9510 * so that they have a chance to revert changes. 9511 */ 9512 __dev_set_mtu(dev, orig_mtu); 9513 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, 9514 new_mtu); 9515 } 9516 } 9517 return err; 9518 } 9519 9520 int netif_set_mtu(struct net_device *dev, int new_mtu) 9521 { 9522 struct netlink_ext_ack extack; 9523 int err; 9524 9525 memset(&extack, 0, sizeof(extack)); 9526 err = netif_set_mtu_ext(dev, new_mtu, &extack); 9527 if (err && extack._msg) 9528 net_err_ratelimited("%s: %s\n", dev->name, extack._msg); 9529 return err; 9530 } 9531 EXPORT_SYMBOL(netif_set_mtu); 9532 9533 int netif_change_tx_queue_len(struct net_device *dev, unsigned long new_len) 9534 { 9535 unsigned int orig_len = dev->tx_queue_len; 9536 int res; 9537 9538 if (new_len != (unsigned int)new_len) 9539 return -ERANGE; 9540 9541 if (new_len != orig_len) { 9542 WRITE_ONCE(dev->tx_queue_len, new_len); 9543 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); 9544 res = notifier_to_errno(res); 9545 if (res) 9546 goto err_rollback; 9547 res = dev_qdisc_change_tx_queue_len(dev); 9548 if (res) 9549 goto err_rollback; 9550 } 9551 9552 return 0; 9553 9554 err_rollback: 9555 netdev_err(dev, "refused to change device tx_queue_len\n"); 9556 WRITE_ONCE(dev->tx_queue_len, orig_len); 9557 return res; 9558 } 9559 9560 void netif_set_group(struct net_device *dev, int new_group) 9561 { 9562 dev->group = new_group; 9563 } 9564 9565 /** 9566 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR. 9567 * @dev: device 9568 * @addr: new address 9569 * @extack: netlink extended ack 9570 */ 9571 int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr, 9572 struct netlink_ext_ack *extack) 9573 { 9574 struct netdev_notifier_pre_changeaddr_info info = { 9575 .info.dev = dev, 9576 .info.extack = extack, 9577 .dev_addr = addr, 9578 }; 9579 int rc; 9580 9581 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info); 9582 return notifier_to_errno(rc); 9583 } 9584 EXPORT_SYMBOL(dev_pre_changeaddr_notify); 9585 9586 int netif_set_mac_address(struct net_device *dev, struct sockaddr *sa, 9587 struct netlink_ext_ack *extack) 9588 { 9589 const struct net_device_ops *ops = dev->netdev_ops; 9590 int err; 9591 9592 if (!ops->ndo_set_mac_address) 9593 return -EOPNOTSUPP; 9594 if (sa->sa_family != dev->type) 9595 return -EINVAL; 9596 if (!netif_device_present(dev)) 9597 return -ENODEV; 9598 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack); 9599 if (err) 9600 return err; 9601 if (memcmp(dev->dev_addr, sa->sa_data, dev->addr_len)) { 9602 err = ops->ndo_set_mac_address(dev, sa); 9603 if (err) 9604 return err; 9605 } 9606 dev->addr_assign_type = NET_ADDR_SET; 9607 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 9608 add_device_randomness(dev->dev_addr, dev->addr_len); 9609 return 0; 9610 } 9611 9612 DECLARE_RWSEM(dev_addr_sem); 9613 9614 int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name) 9615 { 9616 size_t size = sizeof(sa->sa_data_min); 9617 struct net_device *dev; 9618 int ret = 0; 9619 9620 down_read(&dev_addr_sem); 9621 rcu_read_lock(); 9622 9623 dev = dev_get_by_name_rcu(net, dev_name); 9624 if (!dev) { 9625 ret = -ENODEV; 9626 goto unlock; 9627 } 9628 if (!dev->addr_len) 9629 memset(sa->sa_data, 0, size); 9630 else 9631 memcpy(sa->sa_data, dev->dev_addr, 9632 min_t(size_t, size, dev->addr_len)); 9633 sa->sa_family = dev->type; 9634 9635 unlock: 9636 rcu_read_unlock(); 9637 up_read(&dev_addr_sem); 9638 return ret; 9639 } 9640 EXPORT_SYMBOL(dev_get_mac_address); 9641 9642 int netif_change_carrier(struct net_device *dev, bool new_carrier) 9643 { 9644 const struct net_device_ops *ops = dev->netdev_ops; 9645 9646 if (!ops->ndo_change_carrier) 9647 return -EOPNOTSUPP; 9648 if (!netif_device_present(dev)) 9649 return -ENODEV; 9650 return ops->ndo_change_carrier(dev, new_carrier); 9651 } 9652 9653 /** 9654 * dev_get_phys_port_id - Get device physical port ID 9655 * @dev: device 9656 * @ppid: port ID 9657 * 9658 * Get device physical port ID 9659 */ 9660 int dev_get_phys_port_id(struct net_device *dev, 9661 struct netdev_phys_item_id *ppid) 9662 { 9663 const struct net_device_ops *ops = dev->netdev_ops; 9664 9665 if (!ops->ndo_get_phys_port_id) 9666 return -EOPNOTSUPP; 9667 return ops->ndo_get_phys_port_id(dev, ppid); 9668 } 9669 9670 /** 9671 * dev_get_phys_port_name - Get device physical port name 9672 * @dev: device 9673 * @name: port name 9674 * @len: limit of bytes to copy to name 9675 * 9676 * Get device physical port name 9677 */ 9678 int dev_get_phys_port_name(struct net_device *dev, 9679 char *name, size_t len) 9680 { 9681 const struct net_device_ops *ops = dev->netdev_ops; 9682 int err; 9683 9684 if (ops->ndo_get_phys_port_name) { 9685 err = ops->ndo_get_phys_port_name(dev, name, len); 9686 if (err != -EOPNOTSUPP) 9687 return err; 9688 } 9689 return devlink_compat_phys_port_name_get(dev, name, len); 9690 } 9691 9692 /** 9693 * dev_get_port_parent_id - Get the device's port parent identifier 9694 * @dev: network device 9695 * @ppid: pointer to a storage for the port's parent identifier 9696 * @recurse: allow/disallow recursion to lower devices 9697 * 9698 * Get the devices's port parent identifier 9699 */ 9700 int dev_get_port_parent_id(struct net_device *dev, 9701 struct netdev_phys_item_id *ppid, 9702 bool recurse) 9703 { 9704 const struct net_device_ops *ops = dev->netdev_ops; 9705 struct netdev_phys_item_id first = { }; 9706 struct net_device *lower_dev; 9707 struct list_head *iter; 9708 int err; 9709 9710 if (ops->ndo_get_port_parent_id) { 9711 err = ops->ndo_get_port_parent_id(dev, ppid); 9712 if (err != -EOPNOTSUPP) 9713 return err; 9714 } 9715 9716 err = devlink_compat_switch_id_get(dev, ppid); 9717 if (!recurse || err != -EOPNOTSUPP) 9718 return err; 9719 9720 netdev_for_each_lower_dev(dev, lower_dev, iter) { 9721 err = dev_get_port_parent_id(lower_dev, ppid, true); 9722 if (err) 9723 break; 9724 if (!first.id_len) 9725 first = *ppid; 9726 else if (memcmp(&first, ppid, sizeof(*ppid))) 9727 return -EOPNOTSUPP; 9728 } 9729 9730 return err; 9731 } 9732 EXPORT_SYMBOL(dev_get_port_parent_id); 9733 9734 /** 9735 * netdev_port_same_parent_id - Indicate if two network devices have 9736 * the same port parent identifier 9737 * @a: first network device 9738 * @b: second network device 9739 */ 9740 bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b) 9741 { 9742 struct netdev_phys_item_id a_id = { }; 9743 struct netdev_phys_item_id b_id = { }; 9744 9745 if (dev_get_port_parent_id(a, &a_id, true) || 9746 dev_get_port_parent_id(b, &b_id, true)) 9747 return false; 9748 9749 return netdev_phys_item_id_same(&a_id, &b_id); 9750 } 9751 EXPORT_SYMBOL(netdev_port_same_parent_id); 9752 9753 int netif_change_proto_down(struct net_device *dev, bool proto_down) 9754 { 9755 if (!dev->change_proto_down) 9756 return -EOPNOTSUPP; 9757 if (!netif_device_present(dev)) 9758 return -ENODEV; 9759 if (proto_down) 9760 netif_carrier_off(dev); 9761 else 9762 netif_carrier_on(dev); 9763 WRITE_ONCE(dev->proto_down, proto_down); 9764 return 0; 9765 } 9766 9767 /** 9768 * netdev_change_proto_down_reason_locked - proto down reason 9769 * 9770 * @dev: device 9771 * @mask: proto down mask 9772 * @value: proto down value 9773 */ 9774 void netdev_change_proto_down_reason_locked(struct net_device *dev, 9775 unsigned long mask, u32 value) 9776 { 9777 u32 proto_down_reason; 9778 int b; 9779 9780 if (!mask) { 9781 proto_down_reason = value; 9782 } else { 9783 proto_down_reason = dev->proto_down_reason; 9784 for_each_set_bit(b, &mask, 32) { 9785 if (value & (1 << b)) 9786 proto_down_reason |= BIT(b); 9787 else 9788 proto_down_reason &= ~BIT(b); 9789 } 9790 } 9791 WRITE_ONCE(dev->proto_down_reason, proto_down_reason); 9792 } 9793 9794 struct bpf_xdp_link { 9795 struct bpf_link link; 9796 struct net_device *dev; /* protected by rtnl_lock, no refcnt held */ 9797 int flags; 9798 }; 9799 9800 static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags) 9801 { 9802 if (flags & XDP_FLAGS_HW_MODE) 9803 return XDP_MODE_HW; 9804 if (flags & XDP_FLAGS_DRV_MODE) 9805 return XDP_MODE_DRV; 9806 if (flags & XDP_FLAGS_SKB_MODE) 9807 return XDP_MODE_SKB; 9808 return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB; 9809 } 9810 9811 static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode) 9812 { 9813 switch (mode) { 9814 case XDP_MODE_SKB: 9815 return generic_xdp_install; 9816 case XDP_MODE_DRV: 9817 case XDP_MODE_HW: 9818 return dev->netdev_ops->ndo_bpf; 9819 default: 9820 return NULL; 9821 } 9822 } 9823 9824 static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev, 9825 enum bpf_xdp_mode mode) 9826 { 9827 return dev->xdp_state[mode].link; 9828 } 9829 9830 static struct bpf_prog *dev_xdp_prog(struct net_device *dev, 9831 enum bpf_xdp_mode mode) 9832 { 9833 struct bpf_xdp_link *link = dev_xdp_link(dev, mode); 9834 9835 if (link) 9836 return link->link.prog; 9837 return dev->xdp_state[mode].prog; 9838 } 9839 9840 u8 dev_xdp_prog_count(struct net_device *dev) 9841 { 9842 u8 count = 0; 9843 int i; 9844 9845 for (i = 0; i < __MAX_XDP_MODE; i++) 9846 if (dev->xdp_state[i].prog || dev->xdp_state[i].link) 9847 count++; 9848 return count; 9849 } 9850 EXPORT_SYMBOL_GPL(dev_xdp_prog_count); 9851 9852 u8 dev_xdp_sb_prog_count(struct net_device *dev) 9853 { 9854 u8 count = 0; 9855 int i; 9856 9857 for (i = 0; i < __MAX_XDP_MODE; i++) 9858 if (dev->xdp_state[i].prog && 9859 !dev->xdp_state[i].prog->aux->xdp_has_frags) 9860 count++; 9861 return count; 9862 } 9863 9864 int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf) 9865 { 9866 if (!dev->netdev_ops->ndo_bpf) 9867 return -EOPNOTSUPP; 9868 9869 if (dev->cfg->hds_config == ETHTOOL_TCP_DATA_SPLIT_ENABLED && 9870 bpf->command == XDP_SETUP_PROG && 9871 bpf->prog && !bpf->prog->aux->xdp_has_frags) { 9872 NL_SET_ERR_MSG(bpf->extack, 9873 "unable to propagate XDP to device using tcp-data-split"); 9874 return -EBUSY; 9875 } 9876 9877 if (dev_get_min_mp_channel_count(dev)) { 9878 NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider"); 9879 return -EBUSY; 9880 } 9881 9882 return dev->netdev_ops->ndo_bpf(dev, bpf); 9883 } 9884 9885 u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode) 9886 { 9887 struct bpf_prog *prog = dev_xdp_prog(dev, mode); 9888 9889 return prog ? prog->aux->id : 0; 9890 } 9891 9892 static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode, 9893 struct bpf_xdp_link *link) 9894 { 9895 dev->xdp_state[mode].link = link; 9896 dev->xdp_state[mode].prog = NULL; 9897 } 9898 9899 static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode, 9900 struct bpf_prog *prog) 9901 { 9902 dev->xdp_state[mode].link = NULL; 9903 dev->xdp_state[mode].prog = prog; 9904 } 9905 9906 static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode, 9907 bpf_op_t bpf_op, struct netlink_ext_ack *extack, 9908 u32 flags, struct bpf_prog *prog) 9909 { 9910 struct netdev_bpf xdp; 9911 int err; 9912 9913 netdev_ops_assert_locked(dev); 9914 9915 if (dev->cfg->hds_config == ETHTOOL_TCP_DATA_SPLIT_ENABLED && 9916 prog && !prog->aux->xdp_has_frags) { 9917 NL_SET_ERR_MSG(extack, "unable to install XDP to device using tcp-data-split"); 9918 return -EBUSY; 9919 } 9920 9921 if (dev_get_min_mp_channel_count(dev)) { 9922 NL_SET_ERR_MSG(extack, "unable to install XDP to device using memory provider"); 9923 return -EBUSY; 9924 } 9925 9926 memset(&xdp, 0, sizeof(xdp)); 9927 xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG; 9928 xdp.extack = extack; 9929 xdp.flags = flags; 9930 xdp.prog = prog; 9931 9932 /* Drivers assume refcnt is already incremented (i.e, prog pointer is 9933 * "moved" into driver), so they don't increment it on their own, but 9934 * they do decrement refcnt when program is detached or replaced. 9935 * Given net_device also owns link/prog, we need to bump refcnt here 9936 * to prevent drivers from underflowing it. 9937 */ 9938 if (prog) 9939 bpf_prog_inc(prog); 9940 err = bpf_op(dev, &xdp); 9941 if (err) { 9942 if (prog) 9943 bpf_prog_put(prog); 9944 return err; 9945 } 9946 9947 if (mode != XDP_MODE_HW) 9948 bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog); 9949 9950 return 0; 9951 } 9952 9953 static void dev_xdp_uninstall(struct net_device *dev) 9954 { 9955 struct bpf_xdp_link *link; 9956 struct bpf_prog *prog; 9957 enum bpf_xdp_mode mode; 9958 bpf_op_t bpf_op; 9959 9960 ASSERT_RTNL(); 9961 9962 for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) { 9963 prog = dev_xdp_prog(dev, mode); 9964 if (!prog) 9965 continue; 9966 9967 bpf_op = dev_xdp_bpf_op(dev, mode); 9968 if (!bpf_op) 9969 continue; 9970 9971 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); 9972 9973 /* auto-detach link from net device */ 9974 link = dev_xdp_link(dev, mode); 9975 if (link) 9976 link->dev = NULL; 9977 else 9978 bpf_prog_put(prog); 9979 9980 dev_xdp_set_link(dev, mode, NULL); 9981 } 9982 } 9983 9984 static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack, 9985 struct bpf_xdp_link *link, struct bpf_prog *new_prog, 9986 struct bpf_prog *old_prog, u32 flags) 9987 { 9988 unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES); 9989 struct bpf_prog *cur_prog; 9990 struct net_device *upper; 9991 struct list_head *iter; 9992 enum bpf_xdp_mode mode; 9993 bpf_op_t bpf_op; 9994 int err; 9995 9996 ASSERT_RTNL(); 9997 9998 /* either link or prog attachment, never both */ 9999 if (link && (new_prog || old_prog)) 10000 return -EINVAL; 10001 /* link supports only XDP mode flags */ 10002 if (link && (flags & ~XDP_FLAGS_MODES)) { 10003 NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment"); 10004 return -EINVAL; 10005 } 10006 /* just one XDP mode bit should be set, zero defaults to drv/skb mode */ 10007 if (num_modes > 1) { 10008 NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set"); 10009 return -EINVAL; 10010 } 10011 /* avoid ambiguity if offload + drv/skb mode progs are both loaded */ 10012 if (!num_modes && dev_xdp_prog_count(dev) > 1) { 10013 NL_SET_ERR_MSG(extack, 10014 "More than one program loaded, unset mode is ambiguous"); 10015 return -EINVAL; 10016 } 10017 /* old_prog != NULL implies XDP_FLAGS_REPLACE is set */ 10018 if (old_prog && !(flags & XDP_FLAGS_REPLACE)) { 10019 NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified"); 10020 return -EINVAL; 10021 } 10022 10023 mode = dev_xdp_mode(dev, flags); 10024 /* can't replace attached link */ 10025 if (dev_xdp_link(dev, mode)) { 10026 NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link"); 10027 return -EBUSY; 10028 } 10029 10030 /* don't allow if an upper device already has a program */ 10031 netdev_for_each_upper_dev_rcu(dev, upper, iter) { 10032 if (dev_xdp_prog_count(upper) > 0) { 10033 NL_SET_ERR_MSG(extack, "Cannot attach when an upper device already has a program"); 10034 return -EEXIST; 10035 } 10036 } 10037 10038 cur_prog = dev_xdp_prog(dev, mode); 10039 /* can't replace attached prog with link */ 10040 if (link && cur_prog) { 10041 NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link"); 10042 return -EBUSY; 10043 } 10044 if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) { 10045 NL_SET_ERR_MSG(extack, "Active program does not match expected"); 10046 return -EEXIST; 10047 } 10048 10049 /* put effective new program into new_prog */ 10050 if (link) 10051 new_prog = link->link.prog; 10052 10053 if (new_prog) { 10054 bool offload = mode == XDP_MODE_HW; 10055 enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB 10056 ? XDP_MODE_DRV : XDP_MODE_SKB; 10057 10058 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) { 10059 NL_SET_ERR_MSG(extack, "XDP program already attached"); 10060 return -EBUSY; 10061 } 10062 if (!offload && dev_xdp_prog(dev, other_mode)) { 10063 NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time"); 10064 return -EEXIST; 10065 } 10066 if (!offload && bpf_prog_is_offloaded(new_prog->aux)) { 10067 NL_SET_ERR_MSG(extack, "Using offloaded program without HW_MODE flag is not supported"); 10068 return -EINVAL; 10069 } 10070 if (bpf_prog_is_dev_bound(new_prog->aux) && !bpf_offload_dev_match(new_prog, dev)) { 10071 NL_SET_ERR_MSG(extack, "Program bound to different device"); 10072 return -EINVAL; 10073 } 10074 if (bpf_prog_is_dev_bound(new_prog->aux) && mode == XDP_MODE_SKB) { 10075 NL_SET_ERR_MSG(extack, "Can't attach device-bound programs in generic mode"); 10076 return -EINVAL; 10077 } 10078 if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) { 10079 NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device"); 10080 return -EINVAL; 10081 } 10082 if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) { 10083 NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device"); 10084 return -EINVAL; 10085 } 10086 } 10087 10088 /* don't call drivers if the effective program didn't change */ 10089 if (new_prog != cur_prog) { 10090 bpf_op = dev_xdp_bpf_op(dev, mode); 10091 if (!bpf_op) { 10092 NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode"); 10093 return -EOPNOTSUPP; 10094 } 10095 10096 err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog); 10097 if (err) 10098 return err; 10099 } 10100 10101 if (link) 10102 dev_xdp_set_link(dev, mode, link); 10103 else 10104 dev_xdp_set_prog(dev, mode, new_prog); 10105 if (cur_prog) 10106 bpf_prog_put(cur_prog); 10107 10108 return 0; 10109 } 10110 10111 static int dev_xdp_attach_link(struct net_device *dev, 10112 struct netlink_ext_ack *extack, 10113 struct bpf_xdp_link *link) 10114 { 10115 return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags); 10116 } 10117 10118 static int dev_xdp_detach_link(struct net_device *dev, 10119 struct netlink_ext_ack *extack, 10120 struct bpf_xdp_link *link) 10121 { 10122 enum bpf_xdp_mode mode; 10123 bpf_op_t bpf_op; 10124 10125 ASSERT_RTNL(); 10126 10127 mode = dev_xdp_mode(dev, link->flags); 10128 if (dev_xdp_link(dev, mode) != link) 10129 return -EINVAL; 10130 10131 bpf_op = dev_xdp_bpf_op(dev, mode); 10132 WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL)); 10133 dev_xdp_set_link(dev, mode, NULL); 10134 return 0; 10135 } 10136 10137 static void bpf_xdp_link_release(struct bpf_link *link) 10138 { 10139 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); 10140 10141 rtnl_lock(); 10142 10143 /* if racing with net_device's tear down, xdp_link->dev might be 10144 * already NULL, in which case link was already auto-detached 10145 */ 10146 if (xdp_link->dev) { 10147 netdev_lock_ops(xdp_link->dev); 10148 WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link)); 10149 netdev_unlock_ops(xdp_link->dev); 10150 xdp_link->dev = NULL; 10151 } 10152 10153 rtnl_unlock(); 10154 } 10155 10156 static int bpf_xdp_link_detach(struct bpf_link *link) 10157 { 10158 bpf_xdp_link_release(link); 10159 return 0; 10160 } 10161 10162 static void bpf_xdp_link_dealloc(struct bpf_link *link) 10163 { 10164 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); 10165 10166 kfree(xdp_link); 10167 } 10168 10169 static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link, 10170 struct seq_file *seq) 10171 { 10172 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); 10173 u32 ifindex = 0; 10174 10175 rtnl_lock(); 10176 if (xdp_link->dev) 10177 ifindex = xdp_link->dev->ifindex; 10178 rtnl_unlock(); 10179 10180 seq_printf(seq, "ifindex:\t%u\n", ifindex); 10181 } 10182 10183 static int bpf_xdp_link_fill_link_info(const struct bpf_link *link, 10184 struct bpf_link_info *info) 10185 { 10186 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); 10187 u32 ifindex = 0; 10188 10189 rtnl_lock(); 10190 if (xdp_link->dev) 10191 ifindex = xdp_link->dev->ifindex; 10192 rtnl_unlock(); 10193 10194 info->xdp.ifindex = ifindex; 10195 return 0; 10196 } 10197 10198 static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog, 10199 struct bpf_prog *old_prog) 10200 { 10201 struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); 10202 enum bpf_xdp_mode mode; 10203 bpf_op_t bpf_op; 10204 int err = 0; 10205 10206 rtnl_lock(); 10207 10208 /* link might have been auto-released already, so fail */ 10209 if (!xdp_link->dev) { 10210 err = -ENOLINK; 10211 goto out_unlock; 10212 } 10213 10214 if (old_prog && link->prog != old_prog) { 10215 err = -EPERM; 10216 goto out_unlock; 10217 } 10218 old_prog = link->prog; 10219 if (old_prog->type != new_prog->type || 10220 old_prog->expected_attach_type != new_prog->expected_attach_type) { 10221 err = -EINVAL; 10222 goto out_unlock; 10223 } 10224 10225 if (old_prog == new_prog) { 10226 /* no-op, don't disturb drivers */ 10227 bpf_prog_put(new_prog); 10228 goto out_unlock; 10229 } 10230 10231 netdev_lock_ops(xdp_link->dev); 10232 mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags); 10233 bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode); 10234 err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL, 10235 xdp_link->flags, new_prog); 10236 netdev_unlock_ops(xdp_link->dev); 10237 if (err) 10238 goto out_unlock; 10239 10240 old_prog = xchg(&link->prog, new_prog); 10241 bpf_prog_put(old_prog); 10242 10243 out_unlock: 10244 rtnl_unlock(); 10245 return err; 10246 } 10247 10248 static const struct bpf_link_ops bpf_xdp_link_lops = { 10249 .release = bpf_xdp_link_release, 10250 .dealloc = bpf_xdp_link_dealloc, 10251 .detach = bpf_xdp_link_detach, 10252 .show_fdinfo = bpf_xdp_link_show_fdinfo, 10253 .fill_link_info = bpf_xdp_link_fill_link_info, 10254 .update_prog = bpf_xdp_link_update, 10255 }; 10256 10257 int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) 10258 { 10259 struct net *net = current->nsproxy->net_ns; 10260 struct bpf_link_primer link_primer; 10261 struct netlink_ext_ack extack = {}; 10262 struct bpf_xdp_link *link; 10263 struct net_device *dev; 10264 int err, fd; 10265 10266 rtnl_lock(); 10267 dev = dev_get_by_index(net, attr->link_create.target_ifindex); 10268 if (!dev) { 10269 rtnl_unlock(); 10270 return -EINVAL; 10271 } 10272 10273 link = kzalloc(sizeof(*link), GFP_USER); 10274 if (!link) { 10275 err = -ENOMEM; 10276 goto unlock; 10277 } 10278 10279 bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog); 10280 link->dev = dev; 10281 link->flags = attr->link_create.flags; 10282 10283 err = bpf_link_prime(&link->link, &link_primer); 10284 if (err) { 10285 kfree(link); 10286 goto unlock; 10287 } 10288 10289 err = dev_xdp_attach_link(dev, &extack, link); 10290 rtnl_unlock(); 10291 10292 if (err) { 10293 link->dev = NULL; 10294 bpf_link_cleanup(&link_primer); 10295 trace_bpf_xdp_link_attach_failed(extack._msg); 10296 goto out_put_dev; 10297 } 10298 10299 fd = bpf_link_settle(&link_primer); 10300 /* link itself doesn't hold dev's refcnt to not complicate shutdown */ 10301 dev_put(dev); 10302 return fd; 10303 10304 unlock: 10305 rtnl_unlock(); 10306 10307 out_put_dev: 10308 dev_put(dev); 10309 return err; 10310 } 10311 10312 /** 10313 * dev_change_xdp_fd - set or clear a bpf program for a device rx path 10314 * @dev: device 10315 * @extack: netlink extended ack 10316 * @fd: new program fd or negative value to clear 10317 * @expected_fd: old program fd that userspace expects to replace or clear 10318 * @flags: xdp-related flags 10319 * 10320 * Set or clear a bpf program for a device 10321 */ 10322 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, 10323 int fd, int expected_fd, u32 flags) 10324 { 10325 enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags); 10326 struct bpf_prog *new_prog = NULL, *old_prog = NULL; 10327 int err; 10328 10329 ASSERT_RTNL(); 10330 10331 if (fd >= 0) { 10332 new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, 10333 mode != XDP_MODE_SKB); 10334 if (IS_ERR(new_prog)) 10335 return PTR_ERR(new_prog); 10336 } 10337 10338 if (expected_fd >= 0) { 10339 old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP, 10340 mode != XDP_MODE_SKB); 10341 if (IS_ERR(old_prog)) { 10342 err = PTR_ERR(old_prog); 10343 old_prog = NULL; 10344 goto err_out; 10345 } 10346 } 10347 10348 err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags); 10349 10350 err_out: 10351 if (err && new_prog) 10352 bpf_prog_put(new_prog); 10353 if (old_prog) 10354 bpf_prog_put(old_prog); 10355 return err; 10356 } 10357 10358 u32 dev_get_min_mp_channel_count(const struct net_device *dev) 10359 { 10360 int i; 10361 10362 ASSERT_RTNL(); 10363 10364 for (i = dev->real_num_rx_queues - 1; i >= 0; i--) 10365 if (dev->_rx[i].mp_params.mp_priv) 10366 /* The channel count is the idx plus 1. */ 10367 return i + 1; 10368 10369 return 0; 10370 } 10371 10372 /** 10373 * dev_index_reserve() - allocate an ifindex in a namespace 10374 * @net: the applicable net namespace 10375 * @ifindex: requested ifindex, pass %0 to get one allocated 10376 * 10377 * Allocate a ifindex for a new device. Caller must either use the ifindex 10378 * to store the device (via list_netdevice()) or call dev_index_release() 10379 * to give the index up. 10380 * 10381 * Return: a suitable unique value for a new device interface number or -errno. 10382 */ 10383 static int dev_index_reserve(struct net *net, u32 ifindex) 10384 { 10385 int err; 10386 10387 if (ifindex > INT_MAX) { 10388 DEBUG_NET_WARN_ON_ONCE(1); 10389 return -EINVAL; 10390 } 10391 10392 if (!ifindex) 10393 err = xa_alloc_cyclic(&net->dev_by_index, &ifindex, NULL, 10394 xa_limit_31b, &net->ifindex, GFP_KERNEL); 10395 else 10396 err = xa_insert(&net->dev_by_index, ifindex, NULL, GFP_KERNEL); 10397 if (err < 0) 10398 return err; 10399 10400 return ifindex; 10401 } 10402 10403 static void dev_index_release(struct net *net, int ifindex) 10404 { 10405 /* Expect only unused indexes, unlist_netdevice() removes the used */ 10406 WARN_ON(xa_erase(&net->dev_by_index, ifindex)); 10407 } 10408 10409 static bool from_cleanup_net(void) 10410 { 10411 #ifdef CONFIG_NET_NS 10412 return current == cleanup_net_task; 10413 #else 10414 return false; 10415 #endif 10416 } 10417 10418 /* Delayed registration/unregisteration */ 10419 LIST_HEAD(net_todo_list); 10420 DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq); 10421 atomic_t dev_unreg_count = ATOMIC_INIT(0); 10422 10423 static void net_set_todo(struct net_device *dev) 10424 { 10425 list_add_tail(&dev->todo_list, &net_todo_list); 10426 } 10427 10428 static netdev_features_t netdev_sync_upper_features(struct net_device *lower, 10429 struct net_device *upper, netdev_features_t features) 10430 { 10431 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; 10432 netdev_features_t feature; 10433 int feature_bit; 10434 10435 for_each_netdev_feature(upper_disables, feature_bit) { 10436 feature = __NETIF_F_BIT(feature_bit); 10437 if (!(upper->wanted_features & feature) 10438 && (features & feature)) { 10439 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n", 10440 &feature, upper->name); 10441 features &= ~feature; 10442 } 10443 } 10444 10445 return features; 10446 } 10447 10448 static void netdev_sync_lower_features(struct net_device *upper, 10449 struct net_device *lower, netdev_features_t features) 10450 { 10451 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; 10452 netdev_features_t feature; 10453 int feature_bit; 10454 10455 for_each_netdev_feature(upper_disables, feature_bit) { 10456 feature = __NETIF_F_BIT(feature_bit); 10457 if (!(features & feature) && (lower->features & feature)) { 10458 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n", 10459 &feature, lower->name); 10460 lower->wanted_features &= ~feature; 10461 __netdev_update_features(lower); 10462 10463 if (unlikely(lower->features & feature)) 10464 netdev_WARN(upper, "failed to disable %pNF on %s!\n", 10465 &feature, lower->name); 10466 else 10467 netdev_features_change(lower); 10468 } 10469 } 10470 } 10471 10472 static bool netdev_has_ip_or_hw_csum(netdev_features_t features) 10473 { 10474 netdev_features_t ip_csum_mask = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; 10475 bool ip_csum = (features & ip_csum_mask) == ip_csum_mask; 10476 bool hw_csum = features & NETIF_F_HW_CSUM; 10477 10478 return ip_csum || hw_csum; 10479 } 10480 10481 static netdev_features_t netdev_fix_features(struct net_device *dev, 10482 netdev_features_t features) 10483 { 10484 /* Fix illegal checksum combinations */ 10485 if ((features & NETIF_F_HW_CSUM) && 10486 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { 10487 netdev_warn(dev, "mixed HW and IP checksum settings.\n"); 10488 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); 10489 } 10490 10491 /* TSO requires that SG is present as well. */ 10492 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { 10493 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); 10494 features &= ~NETIF_F_ALL_TSO; 10495 } 10496 10497 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && 10498 !(features & NETIF_F_IP_CSUM)) { 10499 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); 10500 features &= ~NETIF_F_TSO; 10501 features &= ~NETIF_F_TSO_ECN; 10502 } 10503 10504 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && 10505 !(features & NETIF_F_IPV6_CSUM)) { 10506 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); 10507 features &= ~NETIF_F_TSO6; 10508 } 10509 10510 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */ 10511 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO)) 10512 features &= ~NETIF_F_TSO_MANGLEID; 10513 10514 /* TSO ECN requires that TSO is present as well. */ 10515 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) 10516 features &= ~NETIF_F_TSO_ECN; 10517 10518 /* Software GSO depends on SG. */ 10519 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { 10520 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); 10521 features &= ~NETIF_F_GSO; 10522 } 10523 10524 /* GSO partial features require GSO partial be set */ 10525 if ((features & dev->gso_partial_features) && 10526 !(features & NETIF_F_GSO_PARTIAL)) { 10527 netdev_dbg(dev, 10528 "Dropping partially supported GSO features since no GSO partial.\n"); 10529 features &= ~dev->gso_partial_features; 10530 } 10531 10532 if (!(features & NETIF_F_RXCSUM)) { 10533 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet 10534 * successfully merged by hardware must also have the 10535 * checksum verified by hardware. If the user does not 10536 * want to enable RXCSUM, logically, we should disable GRO_HW. 10537 */ 10538 if (features & NETIF_F_GRO_HW) { 10539 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n"); 10540 features &= ~NETIF_F_GRO_HW; 10541 } 10542 } 10543 10544 /* LRO/HW-GRO features cannot be combined with RX-FCS */ 10545 if (features & NETIF_F_RXFCS) { 10546 if (features & NETIF_F_LRO) { 10547 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n"); 10548 features &= ~NETIF_F_LRO; 10549 } 10550 10551 if (features & NETIF_F_GRO_HW) { 10552 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n"); 10553 features &= ~NETIF_F_GRO_HW; 10554 } 10555 } 10556 10557 if ((features & NETIF_F_GRO_HW) && (features & NETIF_F_LRO)) { 10558 netdev_dbg(dev, "Dropping LRO feature since HW-GRO is requested.\n"); 10559 features &= ~NETIF_F_LRO; 10560 } 10561 10562 if ((features & NETIF_F_HW_TLS_TX) && !netdev_has_ip_or_hw_csum(features)) { 10563 netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n"); 10564 features &= ~NETIF_F_HW_TLS_TX; 10565 } 10566 10567 if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) { 10568 netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n"); 10569 features &= ~NETIF_F_HW_TLS_RX; 10570 } 10571 10572 if ((features & NETIF_F_GSO_UDP_L4) && !netdev_has_ip_or_hw_csum(features)) { 10573 netdev_dbg(dev, "Dropping USO feature since no CSUM feature.\n"); 10574 features &= ~NETIF_F_GSO_UDP_L4; 10575 } 10576 10577 return features; 10578 } 10579 10580 int __netdev_update_features(struct net_device *dev) 10581 { 10582 struct net_device *upper, *lower; 10583 netdev_features_t features; 10584 struct list_head *iter; 10585 int err = -1; 10586 10587 ASSERT_RTNL(); 10588 netdev_ops_assert_locked(dev); 10589 10590 features = netdev_get_wanted_features(dev); 10591 10592 if (dev->netdev_ops->ndo_fix_features) 10593 features = dev->netdev_ops->ndo_fix_features(dev, features); 10594 10595 /* driver might be less strict about feature dependencies */ 10596 features = netdev_fix_features(dev, features); 10597 10598 /* some features can't be enabled if they're off on an upper device */ 10599 netdev_for_each_upper_dev_rcu(dev, upper, iter) 10600 features = netdev_sync_upper_features(dev, upper, features); 10601 10602 if (dev->features == features) 10603 goto sync_lower; 10604 10605 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", 10606 &dev->features, &features); 10607 10608 if (dev->netdev_ops->ndo_set_features) 10609 err = dev->netdev_ops->ndo_set_features(dev, features); 10610 else 10611 err = 0; 10612 10613 if (unlikely(err < 0)) { 10614 netdev_err(dev, 10615 "set_features() failed (%d); wanted %pNF, left %pNF\n", 10616 err, &features, &dev->features); 10617 /* return non-0 since some features might have changed and 10618 * it's better to fire a spurious notification than miss it 10619 */ 10620 return -1; 10621 } 10622 10623 sync_lower: 10624 /* some features must be disabled on lower devices when disabled 10625 * on an upper device (think: bonding master or bridge) 10626 */ 10627 netdev_for_each_lower_dev(dev, lower, iter) 10628 netdev_sync_lower_features(dev, lower, features); 10629 10630 if (!err) { 10631 netdev_features_t diff = features ^ dev->features; 10632 10633 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) { 10634 /* udp_tunnel_{get,drop}_rx_info both need 10635 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the 10636 * device, or they won't do anything. 10637 * Thus we need to update dev->features 10638 * *before* calling udp_tunnel_get_rx_info, 10639 * but *after* calling udp_tunnel_drop_rx_info. 10640 */ 10641 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) { 10642 dev->features = features; 10643 udp_tunnel_get_rx_info(dev); 10644 } else { 10645 udp_tunnel_drop_rx_info(dev); 10646 } 10647 } 10648 10649 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) { 10650 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) { 10651 dev->features = features; 10652 err |= vlan_get_rx_ctag_filter_info(dev); 10653 } else { 10654 vlan_drop_rx_ctag_filter_info(dev); 10655 } 10656 } 10657 10658 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) { 10659 if (features & NETIF_F_HW_VLAN_STAG_FILTER) { 10660 dev->features = features; 10661 err |= vlan_get_rx_stag_filter_info(dev); 10662 } else { 10663 vlan_drop_rx_stag_filter_info(dev); 10664 } 10665 } 10666 10667 dev->features = features; 10668 } 10669 10670 return err < 0 ? 0 : 1; 10671 } 10672 10673 /** 10674 * netdev_update_features - recalculate device features 10675 * @dev: the device to check 10676 * 10677 * Recalculate dev->features set and send notifications if it 10678 * has changed. Should be called after driver or hardware dependent 10679 * conditions might have changed that influence the features. 10680 */ 10681 void netdev_update_features(struct net_device *dev) 10682 { 10683 if (__netdev_update_features(dev)) 10684 netdev_features_change(dev); 10685 } 10686 EXPORT_SYMBOL(netdev_update_features); 10687 10688 /** 10689 * netdev_change_features - recalculate device features 10690 * @dev: the device to check 10691 * 10692 * Recalculate dev->features set and send notifications even 10693 * if they have not changed. Should be called instead of 10694 * netdev_update_features() if also dev->vlan_features might 10695 * have changed to allow the changes to be propagated to stacked 10696 * VLAN devices. 10697 */ 10698 void netdev_change_features(struct net_device *dev) 10699 { 10700 __netdev_update_features(dev); 10701 netdev_features_change(dev); 10702 } 10703 EXPORT_SYMBOL(netdev_change_features); 10704 10705 /** 10706 * netif_stacked_transfer_operstate - transfer operstate 10707 * @rootdev: the root or lower level device to transfer state from 10708 * @dev: the device to transfer operstate to 10709 * 10710 * Transfer operational state from root to device. This is normally 10711 * called when a stacking relationship exists between the root 10712 * device and the device(a leaf device). 10713 */ 10714 void netif_stacked_transfer_operstate(const struct net_device *rootdev, 10715 struct net_device *dev) 10716 { 10717 if (rootdev->operstate == IF_OPER_DORMANT) 10718 netif_dormant_on(dev); 10719 else 10720 netif_dormant_off(dev); 10721 10722 if (rootdev->operstate == IF_OPER_TESTING) 10723 netif_testing_on(dev); 10724 else 10725 netif_testing_off(dev); 10726 10727 if (netif_carrier_ok(rootdev)) 10728 netif_carrier_on(dev); 10729 else 10730 netif_carrier_off(dev); 10731 } 10732 EXPORT_SYMBOL(netif_stacked_transfer_operstate); 10733 10734 static int netif_alloc_rx_queues(struct net_device *dev) 10735 { 10736 unsigned int i, count = dev->num_rx_queues; 10737 struct netdev_rx_queue *rx; 10738 size_t sz = count * sizeof(*rx); 10739 int err = 0; 10740 10741 BUG_ON(count < 1); 10742 10743 rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); 10744 if (!rx) 10745 return -ENOMEM; 10746 10747 dev->_rx = rx; 10748 10749 for (i = 0; i < count; i++) { 10750 rx[i].dev = dev; 10751 10752 /* XDP RX-queue setup */ 10753 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0); 10754 if (err < 0) 10755 goto err_rxq_info; 10756 } 10757 return 0; 10758 10759 err_rxq_info: 10760 /* Rollback successful reg's and free other resources */ 10761 while (i--) 10762 xdp_rxq_info_unreg(&rx[i].xdp_rxq); 10763 kvfree(dev->_rx); 10764 dev->_rx = NULL; 10765 return err; 10766 } 10767 10768 static void netif_free_rx_queues(struct net_device *dev) 10769 { 10770 unsigned int i, count = dev->num_rx_queues; 10771 10772 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */ 10773 if (!dev->_rx) 10774 return; 10775 10776 for (i = 0; i < count; i++) 10777 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq); 10778 10779 kvfree(dev->_rx); 10780 } 10781 10782 static void netdev_init_one_queue(struct net_device *dev, 10783 struct netdev_queue *queue, void *_unused) 10784 { 10785 /* Initialize queue lock */ 10786 spin_lock_init(&queue->_xmit_lock); 10787 netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type); 10788 queue->xmit_lock_owner = -1; 10789 netdev_queue_numa_node_write(queue, NUMA_NO_NODE); 10790 queue->dev = dev; 10791 #ifdef CONFIG_BQL 10792 dql_init(&queue->dql, HZ); 10793 #endif 10794 } 10795 10796 static void netif_free_tx_queues(struct net_device *dev) 10797 { 10798 kvfree(dev->_tx); 10799 } 10800 10801 static int netif_alloc_netdev_queues(struct net_device *dev) 10802 { 10803 unsigned int count = dev->num_tx_queues; 10804 struct netdev_queue *tx; 10805 size_t sz = count * sizeof(*tx); 10806 10807 if (count < 1 || count > 0xffff) 10808 return -EINVAL; 10809 10810 tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); 10811 if (!tx) 10812 return -ENOMEM; 10813 10814 dev->_tx = tx; 10815 10816 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); 10817 spin_lock_init(&dev->tx_global_lock); 10818 10819 return 0; 10820 } 10821 10822 void netif_tx_stop_all_queues(struct net_device *dev) 10823 { 10824 unsigned int i; 10825 10826 for (i = 0; i < dev->num_tx_queues; i++) { 10827 struct netdev_queue *txq = netdev_get_tx_queue(dev, i); 10828 10829 netif_tx_stop_queue(txq); 10830 } 10831 } 10832 EXPORT_SYMBOL(netif_tx_stop_all_queues); 10833 10834 static int netdev_do_alloc_pcpu_stats(struct net_device *dev) 10835 { 10836 void __percpu *v; 10837 10838 /* Drivers implementing ndo_get_peer_dev must support tstat 10839 * accounting, so that skb_do_redirect() can bump the dev's 10840 * RX stats upon network namespace switch. 10841 */ 10842 if (dev->netdev_ops->ndo_get_peer_dev && 10843 dev->pcpu_stat_type != NETDEV_PCPU_STAT_TSTATS) 10844 return -EOPNOTSUPP; 10845 10846 switch (dev->pcpu_stat_type) { 10847 case NETDEV_PCPU_STAT_NONE: 10848 return 0; 10849 case NETDEV_PCPU_STAT_LSTATS: 10850 v = dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats); 10851 break; 10852 case NETDEV_PCPU_STAT_TSTATS: 10853 v = dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 10854 break; 10855 case NETDEV_PCPU_STAT_DSTATS: 10856 v = dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); 10857 break; 10858 default: 10859 return -EINVAL; 10860 } 10861 10862 return v ? 0 : -ENOMEM; 10863 } 10864 10865 static void netdev_do_free_pcpu_stats(struct net_device *dev) 10866 { 10867 switch (dev->pcpu_stat_type) { 10868 case NETDEV_PCPU_STAT_NONE: 10869 return; 10870 case NETDEV_PCPU_STAT_LSTATS: 10871 free_percpu(dev->lstats); 10872 break; 10873 case NETDEV_PCPU_STAT_TSTATS: 10874 free_percpu(dev->tstats); 10875 break; 10876 case NETDEV_PCPU_STAT_DSTATS: 10877 free_percpu(dev->dstats); 10878 break; 10879 } 10880 } 10881 10882 static void netdev_free_phy_link_topology(struct net_device *dev) 10883 { 10884 struct phy_link_topology *topo = dev->link_topo; 10885 10886 if (IS_ENABLED(CONFIG_PHYLIB) && topo) { 10887 xa_destroy(&topo->phys); 10888 kfree(topo); 10889 dev->link_topo = NULL; 10890 } 10891 } 10892 10893 /** 10894 * register_netdevice() - register a network device 10895 * @dev: device to register 10896 * 10897 * Take a prepared network device structure and make it externally accessible. 10898 * A %NETDEV_REGISTER message is sent to the netdev notifier chain. 10899 * Callers must hold the rtnl lock - you may want register_netdev() 10900 * instead of this. 10901 */ 10902 int register_netdevice(struct net_device *dev) 10903 { 10904 int ret; 10905 struct net *net = dev_net(dev); 10906 10907 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE < 10908 NETDEV_FEATURE_COUNT); 10909 BUG_ON(dev_boot_phase); 10910 ASSERT_RTNL(); 10911 10912 might_sleep(); 10913 10914 /* When net_device's are persistent, this will be fatal. */ 10915 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); 10916 BUG_ON(!net); 10917 10918 ret = ethtool_check_ops(dev->ethtool_ops); 10919 if (ret) 10920 return ret; 10921 10922 /* rss ctx ID 0 is reserved for the default context, start from 1 */ 10923 xa_init_flags(&dev->ethtool->rss_ctx, XA_FLAGS_ALLOC1); 10924 mutex_init(&dev->ethtool->rss_lock); 10925 10926 spin_lock_init(&dev->addr_list_lock); 10927 netdev_set_addr_lockdep_class(dev); 10928 10929 ret = dev_get_valid_name(net, dev, dev->name); 10930 if (ret < 0) 10931 goto out; 10932 10933 ret = -ENOMEM; 10934 dev->name_node = netdev_name_node_head_alloc(dev); 10935 if (!dev->name_node) 10936 goto out; 10937 10938 /* Init, if this function is available */ 10939 if (dev->netdev_ops->ndo_init) { 10940 ret = dev->netdev_ops->ndo_init(dev); 10941 if (ret) { 10942 if (ret > 0) 10943 ret = -EIO; 10944 goto err_free_name; 10945 } 10946 } 10947 10948 if (((dev->hw_features | dev->features) & 10949 NETIF_F_HW_VLAN_CTAG_FILTER) && 10950 (!dev->netdev_ops->ndo_vlan_rx_add_vid || 10951 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { 10952 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); 10953 ret = -EINVAL; 10954 goto err_uninit; 10955 } 10956 10957 ret = netdev_do_alloc_pcpu_stats(dev); 10958 if (ret) 10959 goto err_uninit; 10960 10961 ret = dev_index_reserve(net, dev->ifindex); 10962 if (ret < 0) 10963 goto err_free_pcpu; 10964 dev->ifindex = ret; 10965 10966 /* Transfer changeable features to wanted_features and enable 10967 * software offloads (GSO and GRO). 10968 */ 10969 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF); 10970 dev->features |= NETIF_F_SOFT_FEATURES; 10971 10972 if (dev->udp_tunnel_nic_info) { 10973 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT; 10974 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT; 10975 } 10976 10977 dev->wanted_features = dev->features & dev->hw_features; 10978 10979 if (!(dev->flags & IFF_LOOPBACK)) 10980 dev->hw_features |= NETIF_F_NOCACHE_COPY; 10981 10982 /* If IPv4 TCP segmentation offload is supported we should also 10983 * allow the device to enable segmenting the frame with the option 10984 * of ignoring a static IP ID value. This doesn't enable the 10985 * feature itself but allows the user to enable it later. 10986 */ 10987 if (dev->hw_features & NETIF_F_TSO) 10988 dev->hw_features |= NETIF_F_TSO_MANGLEID; 10989 if (dev->vlan_features & NETIF_F_TSO) 10990 dev->vlan_features |= NETIF_F_TSO_MANGLEID; 10991 if (dev->mpls_features & NETIF_F_TSO) 10992 dev->mpls_features |= NETIF_F_TSO_MANGLEID; 10993 if (dev->hw_enc_features & NETIF_F_TSO) 10994 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID; 10995 10996 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. 10997 */ 10998 dev->vlan_features |= NETIF_F_HIGHDMA; 10999 11000 /* Make NETIF_F_SG inheritable to tunnel devices. 11001 */ 11002 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL; 11003 11004 /* Make NETIF_F_SG inheritable to MPLS. 11005 */ 11006 dev->mpls_features |= NETIF_F_SG; 11007 11008 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); 11009 ret = notifier_to_errno(ret); 11010 if (ret) 11011 goto err_ifindex_release; 11012 11013 ret = netdev_register_kobject(dev); 11014 11015 netdev_lock(dev); 11016 WRITE_ONCE(dev->reg_state, ret ? NETREG_UNREGISTERED : NETREG_REGISTERED); 11017 netdev_unlock(dev); 11018 11019 if (ret) 11020 goto err_uninit_notify; 11021 11022 netdev_lock_ops(dev); 11023 __netdev_update_features(dev); 11024 netdev_unlock_ops(dev); 11025 11026 /* 11027 * Default initial state at registry is that the 11028 * device is present. 11029 */ 11030 11031 set_bit(__LINK_STATE_PRESENT, &dev->state); 11032 11033 linkwatch_init_dev(dev); 11034 11035 dev_init_scheduler(dev); 11036 11037 netdev_hold(dev, &dev->dev_registered_tracker, GFP_KERNEL); 11038 list_netdevice(dev); 11039 11040 add_device_randomness(dev->dev_addr, dev->addr_len); 11041 11042 /* If the device has permanent device address, driver should 11043 * set dev_addr and also addr_assign_type should be set to 11044 * NET_ADDR_PERM (default value). 11045 */ 11046 if (dev->addr_assign_type == NET_ADDR_PERM) 11047 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); 11048 11049 /* Notify protocols, that a new device appeared. */ 11050 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); 11051 ret = notifier_to_errno(ret); 11052 if (ret) { 11053 /* Expect explicit free_netdev() on failure */ 11054 dev->needs_free_netdev = false; 11055 unregister_netdevice_queue(dev, NULL); 11056 goto out; 11057 } 11058 /* 11059 * Prevent userspace races by waiting until the network 11060 * device is fully setup before sending notifications. 11061 */ 11062 if (!dev->rtnl_link_ops || 11063 dev->rtnl_link_state == RTNL_LINK_INITIALIZED) 11064 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL, 0, NULL); 11065 11066 out: 11067 return ret; 11068 11069 err_uninit_notify: 11070 call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev); 11071 err_ifindex_release: 11072 dev_index_release(net, dev->ifindex); 11073 err_free_pcpu: 11074 netdev_do_free_pcpu_stats(dev); 11075 err_uninit: 11076 if (dev->netdev_ops->ndo_uninit) 11077 dev->netdev_ops->ndo_uninit(dev); 11078 if (dev->priv_destructor) 11079 dev->priv_destructor(dev); 11080 err_free_name: 11081 netdev_name_node_free(dev->name_node); 11082 goto out; 11083 } 11084 EXPORT_SYMBOL(register_netdevice); 11085 11086 /* Initialize the core of a dummy net device. 11087 * The setup steps dummy netdevs need which normal netdevs get by going 11088 * through register_netdevice(). 11089 */ 11090 static void init_dummy_netdev(struct net_device *dev) 11091 { 11092 /* make sure we BUG if trying to hit standard 11093 * register/unregister code path 11094 */ 11095 dev->reg_state = NETREG_DUMMY; 11096 11097 /* a dummy interface is started by default */ 11098 set_bit(__LINK_STATE_PRESENT, &dev->state); 11099 set_bit(__LINK_STATE_START, &dev->state); 11100 11101 /* Note : We dont allocate pcpu_refcnt for dummy devices, 11102 * because users of this 'device' dont need to change 11103 * its refcount. 11104 */ 11105 } 11106 11107 /** 11108 * register_netdev - register a network device 11109 * @dev: device to register 11110 * 11111 * Take a completed network device structure and add it to the kernel 11112 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier 11113 * chain. 0 is returned on success. A negative errno code is returned 11114 * on a failure to set up the device, or if the name is a duplicate. 11115 * 11116 * This is a wrapper around register_netdevice that takes the rtnl semaphore 11117 * and expands the device name if you passed a format string to 11118 * alloc_netdev. 11119 */ 11120 int register_netdev(struct net_device *dev) 11121 { 11122 struct net *net = dev_net(dev); 11123 int err; 11124 11125 if (rtnl_net_lock_killable(net)) 11126 return -EINTR; 11127 11128 err = register_netdevice(dev); 11129 11130 rtnl_net_unlock(net); 11131 11132 return err; 11133 } 11134 EXPORT_SYMBOL(register_netdev); 11135 11136 int netdev_refcnt_read(const struct net_device *dev) 11137 { 11138 #ifdef CONFIG_PCPU_DEV_REFCNT 11139 int i, refcnt = 0; 11140 11141 for_each_possible_cpu(i) 11142 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); 11143 return refcnt; 11144 #else 11145 return refcount_read(&dev->dev_refcnt); 11146 #endif 11147 } 11148 EXPORT_SYMBOL(netdev_refcnt_read); 11149 11150 int netdev_unregister_timeout_secs __read_mostly = 10; 11151 11152 #define WAIT_REFS_MIN_MSECS 1 11153 #define WAIT_REFS_MAX_MSECS 250 11154 /** 11155 * netdev_wait_allrefs_any - wait until all references are gone. 11156 * @list: list of net_devices to wait on 11157 * 11158 * This is called when unregistering network devices. 11159 * 11160 * Any protocol or device that holds a reference should register 11161 * for netdevice notification, and cleanup and put back the 11162 * reference if they receive an UNREGISTER event. 11163 * We can get stuck here if buggy protocols don't correctly 11164 * call dev_put. 11165 */ 11166 static struct net_device *netdev_wait_allrefs_any(struct list_head *list) 11167 { 11168 unsigned long rebroadcast_time, warning_time; 11169 struct net_device *dev; 11170 int wait = 0; 11171 11172 rebroadcast_time = warning_time = jiffies; 11173 11174 list_for_each_entry(dev, list, todo_list) 11175 if (netdev_refcnt_read(dev) == 1) 11176 return dev; 11177 11178 while (true) { 11179 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { 11180 rtnl_lock(); 11181 11182 /* Rebroadcast unregister notification */ 11183 list_for_each_entry(dev, list, todo_list) 11184 call_netdevice_notifiers(NETDEV_UNREGISTER, dev); 11185 11186 __rtnl_unlock(); 11187 rcu_barrier(); 11188 rtnl_lock(); 11189 11190 list_for_each_entry(dev, list, todo_list) 11191 if (test_bit(__LINK_STATE_LINKWATCH_PENDING, 11192 &dev->state)) { 11193 /* We must not have linkwatch events 11194 * pending on unregister. If this 11195 * happens, we simply run the queue 11196 * unscheduled, resulting in a noop 11197 * for this device. 11198 */ 11199 linkwatch_run_queue(); 11200 break; 11201 } 11202 11203 __rtnl_unlock(); 11204 11205 rebroadcast_time = jiffies; 11206 } 11207 11208 rcu_barrier(); 11209 11210 if (!wait) { 11211 wait = WAIT_REFS_MIN_MSECS; 11212 } else { 11213 msleep(wait); 11214 wait = min(wait << 1, WAIT_REFS_MAX_MSECS); 11215 } 11216 11217 list_for_each_entry(dev, list, todo_list) 11218 if (netdev_refcnt_read(dev) == 1) 11219 return dev; 11220 11221 if (time_after(jiffies, warning_time + 11222 READ_ONCE(netdev_unregister_timeout_secs) * HZ)) { 11223 list_for_each_entry(dev, list, todo_list) { 11224 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", 11225 dev->name, netdev_refcnt_read(dev)); 11226 ref_tracker_dir_print(&dev->refcnt_tracker, 10); 11227 } 11228 11229 warning_time = jiffies; 11230 } 11231 } 11232 } 11233 11234 /* The sequence is: 11235 * 11236 * rtnl_lock(); 11237 * ... 11238 * register_netdevice(x1); 11239 * register_netdevice(x2); 11240 * ... 11241 * unregister_netdevice(y1); 11242 * unregister_netdevice(y2); 11243 * ... 11244 * rtnl_unlock(); 11245 * free_netdev(y1); 11246 * free_netdev(y2); 11247 * 11248 * We are invoked by rtnl_unlock(). 11249 * This allows us to deal with problems: 11250 * 1) We can delete sysfs objects which invoke hotplug 11251 * without deadlocking with linkwatch via keventd. 11252 * 2) Since we run with the RTNL semaphore not held, we can sleep 11253 * safely in order to wait for the netdev refcnt to drop to zero. 11254 * 11255 * We must not return until all unregister events added during 11256 * the interval the lock was held have been completed. 11257 */ 11258 void netdev_run_todo(void) 11259 { 11260 struct net_device *dev, *tmp; 11261 struct list_head list; 11262 int cnt; 11263 #ifdef CONFIG_LOCKDEP 11264 struct list_head unlink_list; 11265 11266 list_replace_init(&net_unlink_list, &unlink_list); 11267 11268 while (!list_empty(&unlink_list)) { 11269 dev = list_first_entry(&unlink_list, struct net_device, 11270 unlink_list); 11271 list_del_init(&dev->unlink_list); 11272 dev->nested_level = dev->lower_level - 1; 11273 } 11274 #endif 11275 11276 /* Snapshot list, allow later requests */ 11277 list_replace_init(&net_todo_list, &list); 11278 11279 __rtnl_unlock(); 11280 11281 /* Wait for rcu callbacks to finish before next phase */ 11282 if (!list_empty(&list)) 11283 rcu_barrier(); 11284 11285 list_for_each_entry_safe(dev, tmp, &list, todo_list) { 11286 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { 11287 netdev_WARN(dev, "run_todo but not unregistering\n"); 11288 list_del(&dev->todo_list); 11289 continue; 11290 } 11291 11292 netdev_lock(dev); 11293 WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERED); 11294 netdev_unlock(dev); 11295 linkwatch_sync_dev(dev); 11296 } 11297 11298 cnt = 0; 11299 while (!list_empty(&list)) { 11300 dev = netdev_wait_allrefs_any(&list); 11301 list_del(&dev->todo_list); 11302 11303 /* paranoia */ 11304 BUG_ON(netdev_refcnt_read(dev) != 1); 11305 BUG_ON(!list_empty(&dev->ptype_all)); 11306 BUG_ON(!list_empty(&dev->ptype_specific)); 11307 WARN_ON(rcu_access_pointer(dev->ip_ptr)); 11308 WARN_ON(rcu_access_pointer(dev->ip6_ptr)); 11309 11310 netdev_do_free_pcpu_stats(dev); 11311 if (dev->priv_destructor) 11312 dev->priv_destructor(dev); 11313 if (dev->needs_free_netdev) 11314 free_netdev(dev); 11315 11316 cnt++; 11317 11318 /* Free network device */ 11319 kobject_put(&dev->dev.kobj); 11320 } 11321 if (cnt && atomic_sub_and_test(cnt, &dev_unreg_count)) 11322 wake_up(&netdev_unregistering_wq); 11323 } 11324 11325 /* Collate per-cpu network dstats statistics 11326 * 11327 * Read per-cpu network statistics from dev->dstats and populate the related 11328 * fields in @s. 11329 */ 11330 static void dev_fetch_dstats(struct rtnl_link_stats64 *s, 11331 const struct pcpu_dstats __percpu *dstats) 11332 { 11333 int cpu; 11334 11335 for_each_possible_cpu(cpu) { 11336 u64 rx_packets, rx_bytes, rx_drops; 11337 u64 tx_packets, tx_bytes, tx_drops; 11338 const struct pcpu_dstats *stats; 11339 unsigned int start; 11340 11341 stats = per_cpu_ptr(dstats, cpu); 11342 do { 11343 start = u64_stats_fetch_begin(&stats->syncp); 11344 rx_packets = u64_stats_read(&stats->rx_packets); 11345 rx_bytes = u64_stats_read(&stats->rx_bytes); 11346 rx_drops = u64_stats_read(&stats->rx_drops); 11347 tx_packets = u64_stats_read(&stats->tx_packets); 11348 tx_bytes = u64_stats_read(&stats->tx_bytes); 11349 tx_drops = u64_stats_read(&stats->tx_drops); 11350 } while (u64_stats_fetch_retry(&stats->syncp, start)); 11351 11352 s->rx_packets += rx_packets; 11353 s->rx_bytes += rx_bytes; 11354 s->rx_dropped += rx_drops; 11355 s->tx_packets += tx_packets; 11356 s->tx_bytes += tx_bytes; 11357 s->tx_dropped += tx_drops; 11358 } 11359 } 11360 11361 /* ndo_get_stats64 implementation for dtstats-based accounting. 11362 * 11363 * Populate @s from dev->stats and dev->dstats. This is used internally by the 11364 * core for NETDEV_PCPU_STAT_DSTAT-type stats collection. 11365 */ 11366 static void dev_get_dstats64(const struct net_device *dev, 11367 struct rtnl_link_stats64 *s) 11368 { 11369 netdev_stats_to_stats64(s, &dev->stats); 11370 dev_fetch_dstats(s, dev->dstats); 11371 } 11372 11373 /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has 11374 * all the same fields in the same order as net_device_stats, with only 11375 * the type differing, but rtnl_link_stats64 may have additional fields 11376 * at the end for newer counters. 11377 */ 11378 void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, 11379 const struct net_device_stats *netdev_stats) 11380 { 11381 size_t i, n = sizeof(*netdev_stats) / sizeof(atomic_long_t); 11382 const atomic_long_t *src = (atomic_long_t *)netdev_stats; 11383 u64 *dst = (u64 *)stats64; 11384 11385 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64)); 11386 for (i = 0; i < n; i++) 11387 dst[i] = (unsigned long)atomic_long_read(&src[i]); 11388 /* zero out counters that only exist in rtnl_link_stats64 */ 11389 memset((char *)stats64 + n * sizeof(u64), 0, 11390 sizeof(*stats64) - n * sizeof(u64)); 11391 } 11392 EXPORT_SYMBOL(netdev_stats_to_stats64); 11393 11394 static __cold struct net_device_core_stats __percpu *netdev_core_stats_alloc( 11395 struct net_device *dev) 11396 { 11397 struct net_device_core_stats __percpu *p; 11398 11399 p = alloc_percpu_gfp(struct net_device_core_stats, 11400 GFP_ATOMIC | __GFP_NOWARN); 11401 11402 if (p && cmpxchg(&dev->core_stats, NULL, p)) 11403 free_percpu(p); 11404 11405 /* This READ_ONCE() pairs with the cmpxchg() above */ 11406 return READ_ONCE(dev->core_stats); 11407 } 11408 11409 noinline void netdev_core_stats_inc(struct net_device *dev, u32 offset) 11410 { 11411 /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */ 11412 struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats); 11413 unsigned long __percpu *field; 11414 11415 if (unlikely(!p)) { 11416 p = netdev_core_stats_alloc(dev); 11417 if (!p) 11418 return; 11419 } 11420 11421 field = (unsigned long __percpu *)((void __percpu *)p + offset); 11422 this_cpu_inc(*field); 11423 } 11424 EXPORT_SYMBOL_GPL(netdev_core_stats_inc); 11425 11426 /** 11427 * dev_get_stats - get network device statistics 11428 * @dev: device to get statistics from 11429 * @storage: place to store stats 11430 * 11431 * Get network statistics from device. Return @storage. 11432 * The device driver may provide its own method by setting 11433 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; 11434 * otherwise the internal statistics structure is used. 11435 */ 11436 struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, 11437 struct rtnl_link_stats64 *storage) 11438 { 11439 const struct net_device_ops *ops = dev->netdev_ops; 11440 const struct net_device_core_stats __percpu *p; 11441 11442 /* 11443 * IPv{4,6} and udp tunnels share common stat helpers and use 11444 * different stat type (NETDEV_PCPU_STAT_TSTATS vs 11445 * NETDEV_PCPU_STAT_DSTATS). Ensure the accounting is consistent. 11446 */ 11447 BUILD_BUG_ON(offsetof(struct pcpu_sw_netstats, rx_bytes) != 11448 offsetof(struct pcpu_dstats, rx_bytes)); 11449 BUILD_BUG_ON(offsetof(struct pcpu_sw_netstats, rx_packets) != 11450 offsetof(struct pcpu_dstats, rx_packets)); 11451 BUILD_BUG_ON(offsetof(struct pcpu_sw_netstats, tx_bytes) != 11452 offsetof(struct pcpu_dstats, tx_bytes)); 11453 BUILD_BUG_ON(offsetof(struct pcpu_sw_netstats, tx_packets) != 11454 offsetof(struct pcpu_dstats, tx_packets)); 11455 11456 if (ops->ndo_get_stats64) { 11457 memset(storage, 0, sizeof(*storage)); 11458 ops->ndo_get_stats64(dev, storage); 11459 } else if (ops->ndo_get_stats) { 11460 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); 11461 } else if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_TSTATS) { 11462 dev_get_tstats64(dev, storage); 11463 } else if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_DSTATS) { 11464 dev_get_dstats64(dev, storage); 11465 } else { 11466 netdev_stats_to_stats64(storage, &dev->stats); 11467 } 11468 11469 /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */ 11470 p = READ_ONCE(dev->core_stats); 11471 if (p) { 11472 const struct net_device_core_stats *core_stats; 11473 int i; 11474 11475 for_each_possible_cpu(i) { 11476 core_stats = per_cpu_ptr(p, i); 11477 storage->rx_dropped += READ_ONCE(core_stats->rx_dropped); 11478 storage->tx_dropped += READ_ONCE(core_stats->tx_dropped); 11479 storage->rx_nohandler += READ_ONCE(core_stats->rx_nohandler); 11480 storage->rx_otherhost_dropped += READ_ONCE(core_stats->rx_otherhost_dropped); 11481 } 11482 } 11483 return storage; 11484 } 11485 EXPORT_SYMBOL(dev_get_stats); 11486 11487 /** 11488 * dev_fetch_sw_netstats - get per-cpu network device statistics 11489 * @s: place to store stats 11490 * @netstats: per-cpu network stats to read from 11491 * 11492 * Read per-cpu network statistics and populate the related fields in @s. 11493 */ 11494 void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s, 11495 const struct pcpu_sw_netstats __percpu *netstats) 11496 { 11497 int cpu; 11498 11499 for_each_possible_cpu(cpu) { 11500 u64 rx_packets, rx_bytes, tx_packets, tx_bytes; 11501 const struct pcpu_sw_netstats *stats; 11502 unsigned int start; 11503 11504 stats = per_cpu_ptr(netstats, cpu); 11505 do { 11506 start = u64_stats_fetch_begin(&stats->syncp); 11507 rx_packets = u64_stats_read(&stats->rx_packets); 11508 rx_bytes = u64_stats_read(&stats->rx_bytes); 11509 tx_packets = u64_stats_read(&stats->tx_packets); 11510 tx_bytes = u64_stats_read(&stats->tx_bytes); 11511 } while (u64_stats_fetch_retry(&stats->syncp, start)); 11512 11513 s->rx_packets += rx_packets; 11514 s->rx_bytes += rx_bytes; 11515 s->tx_packets += tx_packets; 11516 s->tx_bytes += tx_bytes; 11517 } 11518 } 11519 EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats); 11520 11521 /** 11522 * dev_get_tstats64 - ndo_get_stats64 implementation 11523 * @dev: device to get statistics from 11524 * @s: place to store stats 11525 * 11526 * Populate @s from dev->stats and dev->tstats. Can be used as 11527 * ndo_get_stats64() callback. 11528 */ 11529 void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s) 11530 { 11531 netdev_stats_to_stats64(s, &dev->stats); 11532 dev_fetch_sw_netstats(s, dev->tstats); 11533 } 11534 EXPORT_SYMBOL_GPL(dev_get_tstats64); 11535 11536 struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) 11537 { 11538 struct netdev_queue *queue = dev_ingress_queue(dev); 11539 11540 #ifdef CONFIG_NET_CLS_ACT 11541 if (queue) 11542 return queue; 11543 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 11544 if (!queue) 11545 return NULL; 11546 netdev_init_one_queue(dev, queue, NULL); 11547 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc); 11548 RCU_INIT_POINTER(queue->qdisc_sleeping, &noop_qdisc); 11549 rcu_assign_pointer(dev->ingress_queue, queue); 11550 #endif 11551 return queue; 11552 } 11553 11554 static const struct ethtool_ops default_ethtool_ops; 11555 11556 void netdev_set_default_ethtool_ops(struct net_device *dev, 11557 const struct ethtool_ops *ops) 11558 { 11559 if (dev->ethtool_ops == &default_ethtool_ops) 11560 dev->ethtool_ops = ops; 11561 } 11562 EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); 11563 11564 /** 11565 * netdev_sw_irq_coalesce_default_on() - enable SW IRQ coalescing by default 11566 * @dev: netdev to enable the IRQ coalescing on 11567 * 11568 * Sets a conservative default for SW IRQ coalescing. Users can use 11569 * sysfs attributes to override the default values. 11570 */ 11571 void netdev_sw_irq_coalesce_default_on(struct net_device *dev) 11572 { 11573 WARN_ON(dev->reg_state == NETREG_REGISTERED); 11574 11575 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) { 11576 netdev_set_gro_flush_timeout(dev, 20000); 11577 netdev_set_defer_hard_irqs(dev, 1); 11578 } 11579 } 11580 EXPORT_SYMBOL_GPL(netdev_sw_irq_coalesce_default_on); 11581 11582 /** 11583 * alloc_netdev_mqs - allocate network device 11584 * @sizeof_priv: size of private data to allocate space for 11585 * @name: device name format string 11586 * @name_assign_type: origin of device name 11587 * @setup: callback to initialize device 11588 * @txqs: the number of TX subqueues to allocate 11589 * @rxqs: the number of RX subqueues to allocate 11590 * 11591 * Allocates a struct net_device with private data area for driver use 11592 * and performs basic initialization. Also allocates subqueue structs 11593 * for each queue on the device. 11594 */ 11595 struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, 11596 unsigned char name_assign_type, 11597 void (*setup)(struct net_device *), 11598 unsigned int txqs, unsigned int rxqs) 11599 { 11600 struct net_device *dev; 11601 size_t napi_config_sz; 11602 unsigned int maxqs; 11603 11604 BUG_ON(strlen(name) >= sizeof(dev->name)); 11605 11606 if (txqs < 1) { 11607 pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); 11608 return NULL; 11609 } 11610 11611 if (rxqs < 1) { 11612 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); 11613 return NULL; 11614 } 11615 11616 maxqs = max(txqs, rxqs); 11617 11618 dev = kvzalloc(struct_size(dev, priv, sizeof_priv), 11619 GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); 11620 if (!dev) 11621 return NULL; 11622 11623 dev->priv_len = sizeof_priv; 11624 11625 ref_tracker_dir_init(&dev->refcnt_tracker, 128, name); 11626 #ifdef CONFIG_PCPU_DEV_REFCNT 11627 dev->pcpu_refcnt = alloc_percpu(int); 11628 if (!dev->pcpu_refcnt) 11629 goto free_dev; 11630 __dev_hold(dev); 11631 #else 11632 refcount_set(&dev->dev_refcnt, 1); 11633 #endif 11634 11635 if (dev_addr_init(dev)) 11636 goto free_pcpu; 11637 11638 dev_mc_init(dev); 11639 dev_uc_init(dev); 11640 11641 dev_net_set(dev, &init_net); 11642 11643 dev->gso_max_size = GSO_LEGACY_MAX_SIZE; 11644 dev->xdp_zc_max_segs = 1; 11645 dev->gso_max_segs = GSO_MAX_SEGS; 11646 dev->gro_max_size = GRO_LEGACY_MAX_SIZE; 11647 dev->gso_ipv4_max_size = GSO_LEGACY_MAX_SIZE; 11648 dev->gro_ipv4_max_size = GRO_LEGACY_MAX_SIZE; 11649 dev->tso_max_size = TSO_LEGACY_MAX_SIZE; 11650 dev->tso_max_segs = TSO_MAX_SEGS; 11651 dev->upper_level = 1; 11652 dev->lower_level = 1; 11653 #ifdef CONFIG_LOCKDEP 11654 dev->nested_level = 0; 11655 INIT_LIST_HEAD(&dev->unlink_list); 11656 #endif 11657 11658 INIT_LIST_HEAD(&dev->napi_list); 11659 INIT_LIST_HEAD(&dev->unreg_list); 11660 INIT_LIST_HEAD(&dev->close_list); 11661 INIT_LIST_HEAD(&dev->link_watch_list); 11662 INIT_LIST_HEAD(&dev->adj_list.upper); 11663 INIT_LIST_HEAD(&dev->adj_list.lower); 11664 INIT_LIST_HEAD(&dev->ptype_all); 11665 INIT_LIST_HEAD(&dev->ptype_specific); 11666 INIT_LIST_HEAD(&dev->net_notifier_list); 11667 #ifdef CONFIG_NET_SCHED 11668 hash_init(dev->qdisc_hash); 11669 #endif 11670 11671 mutex_init(&dev->lock); 11672 11673 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; 11674 setup(dev); 11675 11676 if (!dev->tx_queue_len) { 11677 dev->priv_flags |= IFF_NO_QUEUE; 11678 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; 11679 } 11680 11681 dev->num_tx_queues = txqs; 11682 dev->real_num_tx_queues = txqs; 11683 if (netif_alloc_netdev_queues(dev)) 11684 goto free_all; 11685 11686 dev->num_rx_queues = rxqs; 11687 dev->real_num_rx_queues = rxqs; 11688 if (netif_alloc_rx_queues(dev)) 11689 goto free_all; 11690 dev->ethtool = kzalloc(sizeof(*dev->ethtool), GFP_KERNEL_ACCOUNT); 11691 if (!dev->ethtool) 11692 goto free_all; 11693 11694 dev->cfg = kzalloc(sizeof(*dev->cfg), GFP_KERNEL_ACCOUNT); 11695 if (!dev->cfg) 11696 goto free_all; 11697 dev->cfg_pending = dev->cfg; 11698 11699 napi_config_sz = array_size(maxqs, sizeof(*dev->napi_config)); 11700 dev->napi_config = kvzalloc(napi_config_sz, GFP_KERNEL_ACCOUNT); 11701 if (!dev->napi_config) 11702 goto free_all; 11703 11704 strscpy(dev->name, name); 11705 dev->name_assign_type = name_assign_type; 11706 dev->group = INIT_NETDEV_GROUP; 11707 if (!dev->ethtool_ops) 11708 dev->ethtool_ops = &default_ethtool_ops; 11709 11710 nf_hook_netdev_init(dev); 11711 11712 return dev; 11713 11714 free_all: 11715 free_netdev(dev); 11716 return NULL; 11717 11718 free_pcpu: 11719 #ifdef CONFIG_PCPU_DEV_REFCNT 11720 free_percpu(dev->pcpu_refcnt); 11721 free_dev: 11722 #endif 11723 kvfree(dev); 11724 return NULL; 11725 } 11726 EXPORT_SYMBOL(alloc_netdev_mqs); 11727 11728 static void netdev_napi_exit(struct net_device *dev) 11729 { 11730 if (!list_empty(&dev->napi_list)) { 11731 struct napi_struct *p, *n; 11732 11733 netdev_lock(dev); 11734 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) 11735 __netif_napi_del_locked(p); 11736 netdev_unlock(dev); 11737 11738 synchronize_net(); 11739 } 11740 11741 kvfree(dev->napi_config); 11742 } 11743 11744 /** 11745 * free_netdev - free network device 11746 * @dev: device 11747 * 11748 * This function does the last stage of destroying an allocated device 11749 * interface. The reference to the device object is released. If this 11750 * is the last reference then it will be freed.Must be called in process 11751 * context. 11752 */ 11753 void free_netdev(struct net_device *dev) 11754 { 11755 might_sleep(); 11756 11757 /* When called immediately after register_netdevice() failed the unwind 11758 * handling may still be dismantling the device. Handle that case by 11759 * deferring the free. 11760 */ 11761 if (dev->reg_state == NETREG_UNREGISTERING) { 11762 ASSERT_RTNL(); 11763 dev->needs_free_netdev = true; 11764 return; 11765 } 11766 11767 WARN_ON(dev->cfg != dev->cfg_pending); 11768 kfree(dev->cfg); 11769 kfree(dev->ethtool); 11770 netif_free_tx_queues(dev); 11771 netif_free_rx_queues(dev); 11772 11773 kfree(rcu_dereference_protected(dev->ingress_queue, 1)); 11774 11775 /* Flush device addresses */ 11776 dev_addr_flush(dev); 11777 11778 netdev_napi_exit(dev); 11779 11780 netif_del_cpu_rmap(dev); 11781 11782 ref_tracker_dir_exit(&dev->refcnt_tracker); 11783 #ifdef CONFIG_PCPU_DEV_REFCNT 11784 free_percpu(dev->pcpu_refcnt); 11785 dev->pcpu_refcnt = NULL; 11786 #endif 11787 free_percpu(dev->core_stats); 11788 dev->core_stats = NULL; 11789 free_percpu(dev->xdp_bulkq); 11790 dev->xdp_bulkq = NULL; 11791 11792 netdev_free_phy_link_topology(dev); 11793 11794 mutex_destroy(&dev->lock); 11795 11796 /* Compatibility with error handling in drivers */ 11797 if (dev->reg_state == NETREG_UNINITIALIZED || 11798 dev->reg_state == NETREG_DUMMY) { 11799 kvfree(dev); 11800 return; 11801 } 11802 11803 BUG_ON(dev->reg_state != NETREG_UNREGISTERED); 11804 WRITE_ONCE(dev->reg_state, NETREG_RELEASED); 11805 11806 /* will free via device release */ 11807 put_device(&dev->dev); 11808 } 11809 EXPORT_SYMBOL(free_netdev); 11810 11811 /** 11812 * alloc_netdev_dummy - Allocate and initialize a dummy net device. 11813 * @sizeof_priv: size of private data to allocate space for 11814 * 11815 * Return: the allocated net_device on success, NULL otherwise 11816 */ 11817 struct net_device *alloc_netdev_dummy(int sizeof_priv) 11818 { 11819 return alloc_netdev(sizeof_priv, "dummy#", NET_NAME_UNKNOWN, 11820 init_dummy_netdev); 11821 } 11822 EXPORT_SYMBOL_GPL(alloc_netdev_dummy); 11823 11824 /** 11825 * synchronize_net - Synchronize with packet receive processing 11826 * 11827 * Wait for packets currently being received to be done. 11828 * Does not block later packets from starting. 11829 */ 11830 void synchronize_net(void) 11831 { 11832 might_sleep(); 11833 if (from_cleanup_net() || rtnl_is_locked()) 11834 synchronize_rcu_expedited(); 11835 else 11836 synchronize_rcu(); 11837 } 11838 EXPORT_SYMBOL(synchronize_net); 11839 11840 static void netdev_rss_contexts_free(struct net_device *dev) 11841 { 11842 struct ethtool_rxfh_context *ctx; 11843 unsigned long context; 11844 11845 mutex_lock(&dev->ethtool->rss_lock); 11846 xa_for_each(&dev->ethtool->rss_ctx, context, ctx) { 11847 struct ethtool_rxfh_param rxfh; 11848 11849 rxfh.indir = ethtool_rxfh_context_indir(ctx); 11850 rxfh.key = ethtool_rxfh_context_key(ctx); 11851 rxfh.hfunc = ctx->hfunc; 11852 rxfh.input_xfrm = ctx->input_xfrm; 11853 rxfh.rss_context = context; 11854 rxfh.rss_delete = true; 11855 11856 xa_erase(&dev->ethtool->rss_ctx, context); 11857 if (dev->ethtool_ops->create_rxfh_context) 11858 dev->ethtool_ops->remove_rxfh_context(dev, ctx, 11859 context, NULL); 11860 else 11861 dev->ethtool_ops->set_rxfh(dev, &rxfh, NULL); 11862 kfree(ctx); 11863 } 11864 xa_destroy(&dev->ethtool->rss_ctx); 11865 mutex_unlock(&dev->ethtool->rss_lock); 11866 } 11867 11868 /** 11869 * unregister_netdevice_queue - remove device from the kernel 11870 * @dev: device 11871 * @head: list 11872 * 11873 * This function shuts down a device interface and removes it 11874 * from the kernel tables. 11875 * If head not NULL, device is queued to be unregistered later. 11876 * 11877 * Callers must hold the rtnl semaphore. You may want 11878 * unregister_netdev() instead of this. 11879 */ 11880 11881 void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) 11882 { 11883 ASSERT_RTNL(); 11884 11885 if (head) { 11886 list_move_tail(&dev->unreg_list, head); 11887 } else { 11888 LIST_HEAD(single); 11889 11890 list_add(&dev->unreg_list, &single); 11891 unregister_netdevice_many(&single); 11892 } 11893 } 11894 EXPORT_SYMBOL(unregister_netdevice_queue); 11895 11896 static void dev_memory_provider_uninstall(struct net_device *dev) 11897 { 11898 unsigned int i; 11899 11900 for (i = 0; i < dev->real_num_rx_queues; i++) { 11901 struct netdev_rx_queue *rxq = &dev->_rx[i]; 11902 struct pp_memory_provider_params *p = &rxq->mp_params; 11903 11904 if (p->mp_ops && p->mp_ops->uninstall) 11905 p->mp_ops->uninstall(rxq->mp_params.mp_priv, rxq); 11906 } 11907 } 11908 11909 void unregister_netdevice_many_notify(struct list_head *head, 11910 u32 portid, const struct nlmsghdr *nlh) 11911 { 11912 struct net_device *dev, *tmp; 11913 LIST_HEAD(close_head); 11914 int cnt = 0; 11915 11916 BUG_ON(dev_boot_phase); 11917 ASSERT_RTNL(); 11918 11919 if (list_empty(head)) 11920 return; 11921 11922 list_for_each_entry_safe(dev, tmp, head, unreg_list) { 11923 /* Some devices call without registering 11924 * for initialization unwind. Remove those 11925 * devices and proceed with the remaining. 11926 */ 11927 if (dev->reg_state == NETREG_UNINITIALIZED) { 11928 pr_debug("unregister_netdevice: device %s/%p never was registered\n", 11929 dev->name, dev); 11930 11931 WARN_ON(1); 11932 list_del(&dev->unreg_list); 11933 continue; 11934 } 11935 dev->dismantle = true; 11936 BUG_ON(dev->reg_state != NETREG_REGISTERED); 11937 } 11938 11939 /* If device is running, close it first. */ 11940 list_for_each_entry(dev, head, unreg_list) { 11941 list_add_tail(&dev->close_list, &close_head); 11942 netdev_lock_ops(dev); 11943 } 11944 dev_close_many(&close_head, true); 11945 11946 list_for_each_entry(dev, head, unreg_list) { 11947 netdev_unlock_ops(dev); 11948 /* And unlink it from device chain. */ 11949 unlist_netdevice(dev); 11950 netdev_lock(dev); 11951 WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERING); 11952 netdev_unlock(dev); 11953 } 11954 flush_all_backlogs(); 11955 11956 synchronize_net(); 11957 11958 list_for_each_entry(dev, head, unreg_list) { 11959 struct sk_buff *skb = NULL; 11960 11961 /* Shutdown queueing discipline. */ 11962 dev_shutdown(dev); 11963 dev_tcx_uninstall(dev); 11964 netdev_lock_ops(dev); 11965 dev_xdp_uninstall(dev); 11966 netdev_unlock_ops(dev); 11967 bpf_dev_bound_netdev_unregister(dev); 11968 dev_memory_provider_uninstall(dev); 11969 11970 netdev_offload_xstats_disable_all(dev); 11971 11972 /* Notify protocols, that we are about to destroy 11973 * this device. They should clean all the things. 11974 */ 11975 call_netdevice_notifiers(NETDEV_UNREGISTER, dev); 11976 11977 if (!dev->rtnl_link_ops || 11978 dev->rtnl_link_state == RTNL_LINK_INITIALIZED) 11979 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0, 11980 GFP_KERNEL, NULL, 0, 11981 portid, nlh); 11982 11983 /* 11984 * Flush the unicast and multicast chains 11985 */ 11986 dev_uc_flush(dev); 11987 dev_mc_flush(dev); 11988 11989 netdev_name_node_alt_flush(dev); 11990 netdev_name_node_free(dev->name_node); 11991 11992 netdev_rss_contexts_free(dev); 11993 11994 call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev); 11995 11996 if (dev->netdev_ops->ndo_uninit) 11997 dev->netdev_ops->ndo_uninit(dev); 11998 11999 mutex_destroy(&dev->ethtool->rss_lock); 12000 12001 net_shaper_flush_netdev(dev); 12002 12003 if (skb) 12004 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL, portid, nlh); 12005 12006 /* Notifier chain MUST detach us all upper devices. */ 12007 WARN_ON(netdev_has_any_upper_dev(dev)); 12008 WARN_ON(netdev_has_any_lower_dev(dev)); 12009 12010 /* Remove entries from kobject tree */ 12011 netdev_unregister_kobject(dev); 12012 #ifdef CONFIG_XPS 12013 /* Remove XPS queueing entries */ 12014 netif_reset_xps_queues_gt(dev, 0); 12015 #endif 12016 } 12017 12018 synchronize_net(); 12019 12020 list_for_each_entry(dev, head, unreg_list) { 12021 netdev_put(dev, &dev->dev_registered_tracker); 12022 net_set_todo(dev); 12023 cnt++; 12024 } 12025 atomic_add(cnt, &dev_unreg_count); 12026 12027 list_del(head); 12028 } 12029 12030 /** 12031 * unregister_netdevice_many - unregister many devices 12032 * @head: list of devices 12033 * 12034 * Note: As most callers use a stack allocated list_head, 12035 * we force a list_del() to make sure stack won't be corrupted later. 12036 */ 12037 void unregister_netdevice_many(struct list_head *head) 12038 { 12039 unregister_netdevice_many_notify(head, 0, NULL); 12040 } 12041 EXPORT_SYMBOL(unregister_netdevice_many); 12042 12043 /** 12044 * unregister_netdev - remove device from the kernel 12045 * @dev: device 12046 * 12047 * This function shuts down a device interface and removes it 12048 * from the kernel tables. 12049 * 12050 * This is just a wrapper for unregister_netdevice that takes 12051 * the rtnl semaphore. In general you want to use this and not 12052 * unregister_netdevice. 12053 */ 12054 void unregister_netdev(struct net_device *dev) 12055 { 12056 rtnl_net_dev_lock(dev); 12057 unregister_netdevice(dev); 12058 rtnl_net_dev_unlock(dev); 12059 } 12060 EXPORT_SYMBOL(unregister_netdev); 12061 12062 int netif_change_net_namespace(struct net_device *dev, struct net *net, 12063 const char *pat, int new_ifindex, 12064 struct netlink_ext_ack *extack) 12065 { 12066 struct netdev_name_node *name_node; 12067 struct net *net_old = dev_net(dev); 12068 char new_name[IFNAMSIZ] = {}; 12069 int err, new_nsid; 12070 12071 ASSERT_RTNL(); 12072 12073 /* Don't allow namespace local devices to be moved. */ 12074 err = -EINVAL; 12075 if (dev->netns_immutable) { 12076 NL_SET_ERR_MSG(extack, "The interface netns is immutable"); 12077 goto out; 12078 } 12079 12080 /* Ensure the device has been registered */ 12081 if (dev->reg_state != NETREG_REGISTERED) { 12082 NL_SET_ERR_MSG(extack, "The interface isn't registered"); 12083 goto out; 12084 } 12085 12086 /* Get out if there is nothing todo */ 12087 err = 0; 12088 if (net_eq(net_old, net)) 12089 goto out; 12090 12091 /* Pick the destination device name, and ensure 12092 * we can use it in the destination network namespace. 12093 */ 12094 err = -EEXIST; 12095 if (netdev_name_in_use(net, dev->name)) { 12096 /* We get here if we can't use the current device name */ 12097 if (!pat) { 12098 NL_SET_ERR_MSG(extack, 12099 "An interface with the same name exists in the target netns"); 12100 goto out; 12101 } 12102 err = dev_prep_valid_name(net, dev, pat, new_name, EEXIST); 12103 if (err < 0) { 12104 NL_SET_ERR_MSG_FMT(extack, 12105 "Unable to use '%s' for the new interface name in the target netns", 12106 pat); 12107 goto out; 12108 } 12109 } 12110 /* Check that none of the altnames conflicts. */ 12111 err = -EEXIST; 12112 netdev_for_each_altname(dev, name_node) { 12113 if (netdev_name_in_use(net, name_node->name)) { 12114 NL_SET_ERR_MSG_FMT(extack, 12115 "An interface with the altname %s exists in the target netns", 12116 name_node->name); 12117 goto out; 12118 } 12119 } 12120 12121 /* Check that new_ifindex isn't used yet. */ 12122 if (new_ifindex) { 12123 err = dev_index_reserve(net, new_ifindex); 12124 if (err < 0) { 12125 NL_SET_ERR_MSG_FMT(extack, 12126 "The ifindex %d is not available in the target netns", 12127 new_ifindex); 12128 goto out; 12129 } 12130 } else { 12131 /* If there is an ifindex conflict assign a new one */ 12132 err = dev_index_reserve(net, dev->ifindex); 12133 if (err == -EBUSY) 12134 err = dev_index_reserve(net, 0); 12135 if (err < 0) { 12136 NL_SET_ERR_MSG(extack, 12137 "Unable to allocate a new ifindex in the target netns"); 12138 goto out; 12139 } 12140 new_ifindex = err; 12141 } 12142 12143 /* 12144 * And now a mini version of register_netdevice unregister_netdevice. 12145 */ 12146 12147 /* If device is running close it first. */ 12148 netif_close(dev); 12149 12150 /* And unlink it from device chain */ 12151 unlist_netdevice(dev); 12152 12153 synchronize_net(); 12154 12155 /* Shutdown queueing discipline. */ 12156 dev_shutdown(dev); 12157 12158 /* Notify protocols, that we are about to destroy 12159 * this device. They should clean all the things. 12160 * 12161 * Note that dev->reg_state stays at NETREG_REGISTERED. 12162 * This is wanted because this way 8021q and macvlan know 12163 * the device is just moving and can keep their slaves up. 12164 */ 12165 call_netdevice_notifiers(NETDEV_UNREGISTER, dev); 12166 rcu_barrier(); 12167 12168 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL); 12169 12170 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid, 12171 new_ifindex); 12172 12173 /* 12174 * Flush the unicast and multicast chains 12175 */ 12176 dev_uc_flush(dev); 12177 dev_mc_flush(dev); 12178 12179 /* Send a netdev-removed uevent to the old namespace */ 12180 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); 12181 netdev_adjacent_del_links(dev); 12182 12183 /* Move per-net netdevice notifiers that are following the netdevice */ 12184 move_netdevice_notifiers_dev_net(dev, net); 12185 12186 /* Actually switch the network namespace */ 12187 dev_net_set(dev, net); 12188 dev->ifindex = new_ifindex; 12189 12190 if (new_name[0]) { 12191 /* Rename the netdev to prepared name */ 12192 write_seqlock_bh(&netdev_rename_lock); 12193 strscpy(dev->name, new_name, IFNAMSIZ); 12194 write_sequnlock_bh(&netdev_rename_lock); 12195 } 12196 12197 /* Fixup kobjects */ 12198 dev_set_uevent_suppress(&dev->dev, 1); 12199 err = device_rename(&dev->dev, dev->name); 12200 dev_set_uevent_suppress(&dev->dev, 0); 12201 WARN_ON(err); 12202 12203 /* Send a netdev-add uevent to the new namespace */ 12204 kobject_uevent(&dev->dev.kobj, KOBJ_ADD); 12205 netdev_adjacent_add_links(dev); 12206 12207 /* Adapt owner in case owning user namespace of target network 12208 * namespace is different from the original one. 12209 */ 12210 err = netdev_change_owner(dev, net_old, net); 12211 WARN_ON(err); 12212 12213 /* Add the device back in the hashes */ 12214 list_netdevice(dev); 12215 12216 /* Notify protocols, that a new device appeared. */ 12217 call_netdevice_notifiers(NETDEV_REGISTER, dev); 12218 12219 /* 12220 * Prevent userspace races by waiting until the network 12221 * device is fully setup before sending notifications. 12222 */ 12223 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL, 0, NULL); 12224 12225 synchronize_net(); 12226 err = 0; 12227 out: 12228 return err; 12229 } 12230 12231 static int dev_cpu_dead(unsigned int oldcpu) 12232 { 12233 struct sk_buff **list_skb; 12234 struct sk_buff *skb; 12235 unsigned int cpu; 12236 struct softnet_data *sd, *oldsd, *remsd = NULL; 12237 12238 local_irq_disable(); 12239 cpu = smp_processor_id(); 12240 sd = &per_cpu(softnet_data, cpu); 12241 oldsd = &per_cpu(softnet_data, oldcpu); 12242 12243 /* Find end of our completion_queue. */ 12244 list_skb = &sd->completion_queue; 12245 while (*list_skb) 12246 list_skb = &(*list_skb)->next; 12247 /* Append completion queue from offline CPU. */ 12248 *list_skb = oldsd->completion_queue; 12249 oldsd->completion_queue = NULL; 12250 12251 /* Append output queue from offline CPU. */ 12252 if (oldsd->output_queue) { 12253 *sd->output_queue_tailp = oldsd->output_queue; 12254 sd->output_queue_tailp = oldsd->output_queue_tailp; 12255 oldsd->output_queue = NULL; 12256 oldsd->output_queue_tailp = &oldsd->output_queue; 12257 } 12258 /* Append NAPI poll list from offline CPU, with one exception : 12259 * process_backlog() must be called by cpu owning percpu backlog. 12260 * We properly handle process_queue & input_pkt_queue later. 12261 */ 12262 while (!list_empty(&oldsd->poll_list)) { 12263 struct napi_struct *napi = list_first_entry(&oldsd->poll_list, 12264 struct napi_struct, 12265 poll_list); 12266 12267 list_del_init(&napi->poll_list); 12268 if (napi->poll == process_backlog) 12269 napi->state &= NAPIF_STATE_THREADED; 12270 else 12271 ____napi_schedule(sd, napi); 12272 } 12273 12274 raise_softirq_irqoff(NET_TX_SOFTIRQ); 12275 local_irq_enable(); 12276 12277 if (!use_backlog_threads()) { 12278 #ifdef CONFIG_RPS 12279 remsd = oldsd->rps_ipi_list; 12280 oldsd->rps_ipi_list = NULL; 12281 #endif 12282 /* send out pending IPI's on offline CPU */ 12283 net_rps_send_ipi(remsd); 12284 } 12285 12286 /* Process offline CPU's input_pkt_queue */ 12287 while ((skb = __skb_dequeue(&oldsd->process_queue))) { 12288 netif_rx(skb); 12289 rps_input_queue_head_incr(oldsd); 12290 } 12291 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) { 12292 netif_rx(skb); 12293 rps_input_queue_head_incr(oldsd); 12294 } 12295 12296 return 0; 12297 } 12298 12299 /** 12300 * netdev_increment_features - increment feature set by one 12301 * @all: current feature set 12302 * @one: new feature set 12303 * @mask: mask feature set 12304 * 12305 * Computes a new feature set after adding a device with feature set 12306 * @one to the master device with current feature set @all. Will not 12307 * enable anything that is off in @mask. Returns the new feature set. 12308 */ 12309 netdev_features_t netdev_increment_features(netdev_features_t all, 12310 netdev_features_t one, netdev_features_t mask) 12311 { 12312 if (mask & NETIF_F_HW_CSUM) 12313 mask |= NETIF_F_CSUM_MASK; 12314 mask |= NETIF_F_VLAN_CHALLENGED; 12315 12316 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask; 12317 all &= one | ~NETIF_F_ALL_FOR_ALL; 12318 12319 /* If one device supports hw checksumming, set for all. */ 12320 if (all & NETIF_F_HW_CSUM) 12321 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM); 12322 12323 return all; 12324 } 12325 EXPORT_SYMBOL(netdev_increment_features); 12326 12327 static struct hlist_head * __net_init netdev_create_hash(void) 12328 { 12329 int i; 12330 struct hlist_head *hash; 12331 12332 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL); 12333 if (hash != NULL) 12334 for (i = 0; i < NETDEV_HASHENTRIES; i++) 12335 INIT_HLIST_HEAD(&hash[i]); 12336 12337 return hash; 12338 } 12339 12340 /* Initialize per network namespace state */ 12341 static int __net_init netdev_init(struct net *net) 12342 { 12343 BUILD_BUG_ON(GRO_HASH_BUCKETS > 12344 BITS_PER_BYTE * sizeof_field(struct gro_node, bitmask)); 12345 12346 INIT_LIST_HEAD(&net->dev_base_head); 12347 12348 net->dev_name_head = netdev_create_hash(); 12349 if (net->dev_name_head == NULL) 12350 goto err_name; 12351 12352 net->dev_index_head = netdev_create_hash(); 12353 if (net->dev_index_head == NULL) 12354 goto err_idx; 12355 12356 xa_init_flags(&net->dev_by_index, XA_FLAGS_ALLOC1); 12357 12358 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain); 12359 12360 return 0; 12361 12362 err_idx: 12363 kfree(net->dev_name_head); 12364 err_name: 12365 return -ENOMEM; 12366 } 12367 12368 /** 12369 * netdev_drivername - network driver for the device 12370 * @dev: network device 12371 * 12372 * Determine network driver for device. 12373 */ 12374 const char *netdev_drivername(const struct net_device *dev) 12375 { 12376 const struct device_driver *driver; 12377 const struct device *parent; 12378 const char *empty = ""; 12379 12380 parent = dev->dev.parent; 12381 if (!parent) 12382 return empty; 12383 12384 driver = parent->driver; 12385 if (driver && driver->name) 12386 return driver->name; 12387 return empty; 12388 } 12389 12390 static void __netdev_printk(const char *level, const struct net_device *dev, 12391 struct va_format *vaf) 12392 { 12393 if (dev && dev->dev.parent) { 12394 dev_printk_emit(level[1] - '0', 12395 dev->dev.parent, 12396 "%s %s %s%s: %pV", 12397 dev_driver_string(dev->dev.parent), 12398 dev_name(dev->dev.parent), 12399 netdev_name(dev), netdev_reg_state(dev), 12400 vaf); 12401 } else if (dev) { 12402 printk("%s%s%s: %pV", 12403 level, netdev_name(dev), netdev_reg_state(dev), vaf); 12404 } else { 12405 printk("%s(NULL net_device): %pV", level, vaf); 12406 } 12407 } 12408 12409 void netdev_printk(const char *level, const struct net_device *dev, 12410 const char *format, ...) 12411 { 12412 struct va_format vaf; 12413 va_list args; 12414 12415 va_start(args, format); 12416 12417 vaf.fmt = format; 12418 vaf.va = &args; 12419 12420 __netdev_printk(level, dev, &vaf); 12421 12422 va_end(args); 12423 } 12424 EXPORT_SYMBOL(netdev_printk); 12425 12426 #define define_netdev_printk_level(func, level) \ 12427 void func(const struct net_device *dev, const char *fmt, ...) \ 12428 { \ 12429 struct va_format vaf; \ 12430 va_list args; \ 12431 \ 12432 va_start(args, fmt); \ 12433 \ 12434 vaf.fmt = fmt; \ 12435 vaf.va = &args; \ 12436 \ 12437 __netdev_printk(level, dev, &vaf); \ 12438 \ 12439 va_end(args); \ 12440 } \ 12441 EXPORT_SYMBOL(func); 12442 12443 define_netdev_printk_level(netdev_emerg, KERN_EMERG); 12444 define_netdev_printk_level(netdev_alert, KERN_ALERT); 12445 define_netdev_printk_level(netdev_crit, KERN_CRIT); 12446 define_netdev_printk_level(netdev_err, KERN_ERR); 12447 define_netdev_printk_level(netdev_warn, KERN_WARNING); 12448 define_netdev_printk_level(netdev_notice, KERN_NOTICE); 12449 define_netdev_printk_level(netdev_info, KERN_INFO); 12450 12451 static void __net_exit netdev_exit(struct net *net) 12452 { 12453 kfree(net->dev_name_head); 12454 kfree(net->dev_index_head); 12455 xa_destroy(&net->dev_by_index); 12456 if (net != &init_net) 12457 WARN_ON_ONCE(!list_empty(&net->dev_base_head)); 12458 } 12459 12460 static struct pernet_operations __net_initdata netdev_net_ops = { 12461 .init = netdev_init, 12462 .exit = netdev_exit, 12463 }; 12464 12465 static void __net_exit default_device_exit_net(struct net *net) 12466 { 12467 struct netdev_name_node *name_node, *tmp; 12468 struct net_device *dev, *aux; 12469 /* 12470 * Push all migratable network devices back to the 12471 * initial network namespace 12472 */ 12473 ASSERT_RTNL(); 12474 for_each_netdev_safe(net, dev, aux) { 12475 int err; 12476 char fb_name[IFNAMSIZ]; 12477 12478 /* Ignore unmoveable devices (i.e. loopback) */ 12479 if (dev->netns_immutable) 12480 continue; 12481 12482 /* Leave virtual devices for the generic cleanup */ 12483 if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund) 12484 continue; 12485 12486 /* Push remaining network devices to init_net */ 12487 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); 12488 if (netdev_name_in_use(&init_net, fb_name)) 12489 snprintf(fb_name, IFNAMSIZ, "dev%%d"); 12490 12491 netdev_for_each_altname_safe(dev, name_node, tmp) 12492 if (netdev_name_in_use(&init_net, name_node->name)) 12493 __netdev_name_node_alt_destroy(name_node); 12494 12495 err = dev_change_net_namespace(dev, &init_net, fb_name); 12496 if (err) { 12497 pr_emerg("%s: failed to move %s to init_net: %d\n", 12498 __func__, dev->name, err); 12499 BUG(); 12500 } 12501 } 12502 } 12503 12504 static void __net_exit default_device_exit_batch(struct list_head *net_list) 12505 { 12506 /* At exit all network devices most be removed from a network 12507 * namespace. Do this in the reverse order of registration. 12508 * Do this across as many network namespaces as possible to 12509 * improve batching efficiency. 12510 */ 12511 struct net_device *dev; 12512 struct net *net; 12513 LIST_HEAD(dev_kill_list); 12514 12515 rtnl_lock(); 12516 list_for_each_entry(net, net_list, exit_list) { 12517 default_device_exit_net(net); 12518 cond_resched(); 12519 } 12520 12521 list_for_each_entry(net, net_list, exit_list) { 12522 for_each_netdev_reverse(net, dev) { 12523 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink) 12524 dev->rtnl_link_ops->dellink(dev, &dev_kill_list); 12525 else 12526 unregister_netdevice_queue(dev, &dev_kill_list); 12527 } 12528 } 12529 unregister_netdevice_many(&dev_kill_list); 12530 rtnl_unlock(); 12531 } 12532 12533 static struct pernet_operations __net_initdata default_device_ops = { 12534 .exit_batch = default_device_exit_batch, 12535 }; 12536 12537 static void __init net_dev_struct_check(void) 12538 { 12539 /* TX read-mostly hotpath */ 12540 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, priv_flags_fast); 12541 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, netdev_ops); 12542 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, header_ops); 12543 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, _tx); 12544 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, real_num_tx_queues); 12545 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_max_size); 12546 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_ipv4_max_size); 12547 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_max_segs); 12548 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_partial_features); 12549 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, num_tc); 12550 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, mtu); 12551 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, needed_headroom); 12552 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tc_to_txq); 12553 #ifdef CONFIG_XPS 12554 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, xps_maps); 12555 #endif 12556 #ifdef CONFIG_NETFILTER_EGRESS 12557 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, nf_hooks_egress); 12558 #endif 12559 #ifdef CONFIG_NET_XGRESS 12560 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tcx_egress); 12561 #endif 12562 CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_tx, 160); 12563 12564 /* TXRX read-mostly hotpath */ 12565 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, lstats); 12566 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, state); 12567 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, flags); 12568 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, hard_header_len); 12569 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, features); 12570 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, ip6_ptr); 12571 CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_txrx, 46); 12572 12573 /* RX read-mostly hotpath */ 12574 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, ptype_specific); 12575 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, ifindex); 12576 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, real_num_rx_queues); 12577 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, _rx); 12578 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_max_size); 12579 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_ipv4_max_size); 12580 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, rx_handler); 12581 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, rx_handler_data); 12582 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, nd_net); 12583 #ifdef CONFIG_NETPOLL 12584 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, npinfo); 12585 #endif 12586 #ifdef CONFIG_NET_XGRESS 12587 CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, tcx_ingress); 12588 #endif 12589 CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_rx, 92); 12590 } 12591 12592 /* 12593 * Initialize the DEV module. At boot time this walks the device list and 12594 * unhooks any devices that fail to initialise (normally hardware not 12595 * present) and leaves us with a valid list of present and active devices. 12596 * 12597 */ 12598 12599 /* We allocate 256 pages for each CPU if PAGE_SHIFT is 12 */ 12600 #define SYSTEM_PERCPU_PAGE_POOL_SIZE ((1 << 20) / PAGE_SIZE) 12601 12602 static int net_page_pool_create(int cpuid) 12603 { 12604 #if IS_ENABLED(CONFIG_PAGE_POOL) 12605 struct page_pool_params page_pool_params = { 12606 .pool_size = SYSTEM_PERCPU_PAGE_POOL_SIZE, 12607 .flags = PP_FLAG_SYSTEM_POOL, 12608 .nid = cpu_to_mem(cpuid), 12609 }; 12610 struct page_pool *pp_ptr; 12611 int err; 12612 12613 pp_ptr = page_pool_create_percpu(&page_pool_params, cpuid); 12614 if (IS_ERR(pp_ptr)) 12615 return -ENOMEM; 12616 12617 err = xdp_reg_page_pool(pp_ptr); 12618 if (err) { 12619 page_pool_destroy(pp_ptr); 12620 return err; 12621 } 12622 12623 per_cpu(system_page_pool, cpuid) = pp_ptr; 12624 #endif 12625 return 0; 12626 } 12627 12628 static int backlog_napi_should_run(unsigned int cpu) 12629 { 12630 struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu); 12631 struct napi_struct *napi = &sd->backlog; 12632 12633 return test_bit(NAPI_STATE_SCHED_THREADED, &napi->state); 12634 } 12635 12636 static void run_backlog_napi(unsigned int cpu) 12637 { 12638 struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu); 12639 12640 napi_threaded_poll_loop(&sd->backlog); 12641 } 12642 12643 static void backlog_napi_setup(unsigned int cpu) 12644 { 12645 struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu); 12646 struct napi_struct *napi = &sd->backlog; 12647 12648 napi->thread = this_cpu_read(backlog_napi); 12649 set_bit(NAPI_STATE_THREADED, &napi->state); 12650 } 12651 12652 static struct smp_hotplug_thread backlog_threads = { 12653 .store = &backlog_napi, 12654 .thread_should_run = backlog_napi_should_run, 12655 .thread_fn = run_backlog_napi, 12656 .thread_comm = "backlog_napi/%u", 12657 .setup = backlog_napi_setup, 12658 }; 12659 12660 /* 12661 * This is called single threaded during boot, so no need 12662 * to take the rtnl semaphore. 12663 */ 12664 static int __init net_dev_init(void) 12665 { 12666 int i, rc = -ENOMEM; 12667 12668 BUG_ON(!dev_boot_phase); 12669 12670 net_dev_struct_check(); 12671 12672 if (dev_proc_init()) 12673 goto out; 12674 12675 if (netdev_kobject_init()) 12676 goto out; 12677 12678 for (i = 0; i < PTYPE_HASH_SIZE; i++) 12679 INIT_LIST_HEAD(&ptype_base[i]); 12680 12681 if (register_pernet_subsys(&netdev_net_ops)) 12682 goto out; 12683 12684 /* 12685 * Initialise the packet receive queues. 12686 */ 12687 12688 flush_backlogs_fallback = flush_backlogs_alloc(); 12689 if (!flush_backlogs_fallback) 12690 goto out; 12691 12692 for_each_possible_cpu(i) { 12693 struct softnet_data *sd = &per_cpu(softnet_data, i); 12694 12695 skb_queue_head_init(&sd->input_pkt_queue); 12696 skb_queue_head_init(&sd->process_queue); 12697 #ifdef CONFIG_XFRM_OFFLOAD 12698 skb_queue_head_init(&sd->xfrm_backlog); 12699 #endif 12700 INIT_LIST_HEAD(&sd->poll_list); 12701 sd->output_queue_tailp = &sd->output_queue; 12702 #ifdef CONFIG_RPS 12703 INIT_CSD(&sd->csd, rps_trigger_softirq, sd); 12704 sd->cpu = i; 12705 #endif 12706 INIT_CSD(&sd->defer_csd, trigger_rx_softirq, sd); 12707 spin_lock_init(&sd->defer_lock); 12708 12709 gro_init(&sd->backlog.gro); 12710 sd->backlog.poll = process_backlog; 12711 sd->backlog.weight = weight_p; 12712 INIT_LIST_HEAD(&sd->backlog.poll_list); 12713 12714 if (net_page_pool_create(i)) 12715 goto out; 12716 } 12717 if (use_backlog_threads()) 12718 smpboot_register_percpu_thread(&backlog_threads); 12719 12720 dev_boot_phase = 0; 12721 12722 /* The loopback device is special if any other network devices 12723 * is present in a network namespace the loopback device must 12724 * be present. Since we now dynamically allocate and free the 12725 * loopback device ensure this invariant is maintained by 12726 * keeping the loopback device as the first device on the 12727 * list of network devices. Ensuring the loopback devices 12728 * is the first device that appears and the last network device 12729 * that disappears. 12730 */ 12731 if (register_pernet_device(&loopback_net_ops)) 12732 goto out; 12733 12734 if (register_pernet_device(&default_device_ops)) 12735 goto out; 12736 12737 open_softirq(NET_TX_SOFTIRQ, net_tx_action); 12738 open_softirq(NET_RX_SOFTIRQ, net_rx_action); 12739 12740 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", 12741 NULL, dev_cpu_dead); 12742 WARN_ON(rc < 0); 12743 rc = 0; 12744 12745 /* avoid static key IPIs to isolated CPUs */ 12746 if (housekeeping_enabled(HK_TYPE_MISC)) 12747 net_enable_timestamp(); 12748 out: 12749 if (rc < 0) { 12750 for_each_possible_cpu(i) { 12751 struct page_pool *pp_ptr; 12752 12753 pp_ptr = per_cpu(system_page_pool, i); 12754 if (!pp_ptr) 12755 continue; 12756 12757 xdp_unreg_page_pool(pp_ptr); 12758 page_pool_destroy(pp_ptr); 12759 per_cpu(system_page_pool, i) = NULL; 12760 } 12761 } 12762 12763 return rc; 12764 } 12765 12766 subsys_initcall(net_dev_init); 12767