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