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