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