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