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