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