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