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