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