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