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