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