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