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