b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * NET3 Protocol independent device support routines. |
| 4 | * |
| 5 | * Derived from the non IP parts of dev.c 1.0.19 |
| 6 | * Authors: Ross Biro |
| 7 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 8 | * Mark Evans, <evansmp@uhura.aston.ac.uk> |
| 9 | * |
| 10 | * Additional Authors: |
| 11 | * Florian la Roche <rzsfl@rz.uni-sb.de> |
| 12 | * Alan Cox <gw4pts@gw4pts.ampr.org> |
| 13 | * David Hinds <dahinds@users.sourceforge.net> |
| 14 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 15 | * Adam Sulmicki <adam@cfar.umd.edu> |
| 16 | * Pekka Riikonen <priikone@poesidon.pspt.fi> |
| 17 | * |
| 18 | * Changes: |
| 19 | * D.J. Barrow : Fixed bug where dev->refcnt gets set |
| 20 | * to 2 if register_netdev gets called |
| 21 | * before net_dev_init & also removed a |
| 22 | * few lines of code in the process. |
| 23 | * Alan Cox : device private ioctl copies fields back. |
| 24 | * Alan Cox : Transmit queue code does relevant |
| 25 | * stunts to keep the queue safe. |
| 26 | * Alan Cox : Fixed double lock. |
| 27 | * Alan Cox : Fixed promisc NULL pointer trap |
| 28 | * ???????? : Support the full private ioctl range |
| 29 | * Alan Cox : Moved ioctl permission check into |
| 30 | * drivers |
| 31 | * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI |
| 32 | * Alan Cox : 100 backlog just doesn't cut it when |
| 33 | * you start doing multicast video 8) |
| 34 | * Alan Cox : Rewrote net_bh and list manager. |
| 35 | * Alan Cox : Fix ETH_P_ALL echoback lengths. |
| 36 | * Alan Cox : Took out transmit every packet pass |
| 37 | * Saved a few bytes in the ioctl handler |
| 38 | * Alan Cox : Network driver sets packet type before |
| 39 | * calling netif_rx. Saves a function |
| 40 | * call a packet. |
| 41 | * Alan Cox : Hashed net_bh() |
| 42 | * Richard Kooijman: Timestamp fixes. |
| 43 | * Alan Cox : Wrong field in SIOCGIFDSTADDR |
| 44 | * Alan Cox : Device lock protection. |
| 45 | * Alan Cox : Fixed nasty side effect of device close |
| 46 | * changes. |
| 47 | * Rudi Cilibrasi : Pass the right thing to |
| 48 | * set_mac_address() |
| 49 | * Dave Miller : 32bit quantity for the device lock to |
| 50 | * make it work out on a Sparc. |
| 51 | * Bjorn Ekwall : Added KERNELD hack. |
| 52 | * Alan Cox : Cleaned up the backlog initialise. |
| 53 | * Craig Metz : SIOCGIFCONF fix if space for under |
| 54 | * 1 device. |
| 55 | * Thomas Bogendoerfer : Return ENODEV for dev_open, if there |
| 56 | * is no device open function. |
| 57 | * Andi Kleen : Fix error reporting for SIOCGIFCONF |
| 58 | * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF |
| 59 | * Cyrus Durgin : Cleaned for KMOD |
| 60 | * Adam Sulmicki : Bug Fix : Network Device Unload |
| 61 | * A network device unload needs to purge |
| 62 | * the backlog queue. |
| 63 | * Paul Rusty Russell : SIOCSIFNAME |
| 64 | * Pekka Riikonen : Netdev boot-time settings code |
| 65 | * Andrew Morton : Make unregister_netdevice wait |
| 66 | * indefinitely on dev->refcnt |
| 67 | * J Hadi Salim : - Backlog queue sampling |
| 68 | * - netif_rx() feedback |
| 69 | */ |
| 70 | |
| 71 | #include <linux/uaccess.h> |
| 72 | #include <linux/bitops.h> |
| 73 | #include <linux/capability.h> |
| 74 | #include <linux/cpu.h> |
| 75 | #include <linux/types.h> |
| 76 | #include <linux/kernel.h> |
| 77 | #include <linux/hash.h> |
| 78 | #include <linux/slab.h> |
| 79 | #include <linux/sched.h> |
| 80 | #include <linux/sched/mm.h> |
| 81 | #include <linux/mutex.h> |
| 82 | #include <linux/rwsem.h> |
| 83 | #include <linux/string.h> |
| 84 | #include <linux/mm.h> |
| 85 | #include <linux/socket.h> |
| 86 | #include <linux/sockios.h> |
| 87 | #include <linux/errno.h> |
| 88 | #include <linux/interrupt.h> |
| 89 | #include <linux/if_ether.h> |
| 90 | #include <linux/netdevice.h> |
| 91 | #include <linux/etherdevice.h> |
| 92 | #include <linux/ethtool.h> |
| 93 | #include <linux/skbuff.h> |
| 94 | #include <linux/kthread.h> |
| 95 | #include <linux/bpf.h> |
| 96 | #include <linux/bpf_trace.h> |
| 97 | #include <net/net_namespace.h> |
| 98 | #include <net/sock.h> |
| 99 | #include <net/busy_poll.h> |
| 100 | #include <linux/rtnetlink.h> |
| 101 | #include <linux/stat.h> |
| 102 | #include <net/dst.h> |
| 103 | #include <net/dst_metadata.h> |
| 104 | #include <net/pkt_sched.h> |
| 105 | #include <net/pkt_cls.h> |
| 106 | #include <net/checksum.h> |
| 107 | #include <net/xfrm.h> |
| 108 | #include <linux/highmem.h> |
| 109 | #include <linux/init.h> |
| 110 | #include <linux/module.h> |
| 111 | #include <linux/netpoll.h> |
| 112 | #include <linux/rcupdate.h> |
| 113 | #include <linux/delay.h> |
| 114 | #include <net/iw_handler.h> |
| 115 | #include <asm/current.h> |
| 116 | #include <linux/audit.h> |
| 117 | #include <linux/dmaengine.h> |
| 118 | #include <linux/err.h> |
| 119 | #include <linux/ctype.h> |
| 120 | #include <linux/if_arp.h> |
| 121 | #include <linux/if_vlan.h> |
| 122 | #include <linux/ip.h> |
| 123 | #include <net/ip.h> |
| 124 | #include <net/mpls.h> |
| 125 | #include <linux/ipv6.h> |
| 126 | #include <linux/in.h> |
| 127 | #include <linux/jhash.h> |
| 128 | #include <linux/random.h> |
| 129 | #include <trace/events/napi.h> |
| 130 | #include <trace/events/net.h> |
| 131 | #include <trace/events/skb.h> |
| 132 | #include <linux/inetdevice.h> |
| 133 | #include <linux/cpu_rmap.h> |
| 134 | #include <linux/static_key.h> |
| 135 | #include <linux/hashtable.h> |
| 136 | #include <linux/vmalloc.h> |
| 137 | #include <linux/if_macvlan.h> |
| 138 | #include <linux/errqueue.h> |
| 139 | #include <linux/hrtimer.h> |
| 140 | #include <linux/netfilter_ingress.h> |
| 141 | #include <linux/crash_dump.h> |
| 142 | #include <linux/sctp.h> |
| 143 | #include <net/udp_tunnel.h> |
| 144 | #include <linux/net_namespace.h> |
| 145 | #include <linux/indirect_call_wrapper.h> |
| 146 | #include <net/devlink.h> |
| 147 | #include <trace/hooks/net.h> |
| 148 | |
| 149 | #include "net-sysfs.h" |
| 150 | #include <linux/skbrb.h> |
| 151 | |
| 152 | #define MAX_GRO_SKBS 8 |
| 153 | |
| 154 | /* This should be increased if a protocol with a bigger head is added. */ |
| 155 | #define GRO_MAX_HEAD (MAX_HEADER + 128) |
| 156 | |
| 157 | static DEFINE_SPINLOCK(ptype_lock); |
| 158 | static DEFINE_SPINLOCK(offload_lock); |
| 159 | struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
| 160 | struct list_head ptype_all __read_mostly; /* Taps */ |
| 161 | static struct list_head offload_base __read_mostly; |
| 162 | |
| 163 | static int netif_rx_internal(struct sk_buff *skb); |
| 164 | static int call_netdevice_notifiers_info(unsigned long val, |
| 165 | struct netdev_notifier_info *info); |
| 166 | static int call_netdevice_notifiers_extack(unsigned long val, |
| 167 | struct net_device *dev, |
| 168 | struct netlink_ext_ack *extack); |
| 169 | static struct napi_struct *napi_by_id(unsigned int napi_id); |
| 170 | |
| 171 | /* |
| 172 | * The @dev_base_head list is protected by @dev_base_lock and the rtnl |
| 173 | * semaphore. |
| 174 | * |
| 175 | * Pure readers hold dev_base_lock for reading, or rcu_read_lock() |
| 176 | * |
| 177 | * Writers must hold the rtnl semaphore while they loop through the |
| 178 | * dev_base_head list, and hold dev_base_lock for writing when they do the |
| 179 | * actual updates. This allows pure readers to access the list even |
| 180 | * while a writer is preparing to update it. |
| 181 | * |
| 182 | * To put it another way, dev_base_lock is held for writing only to |
| 183 | * protect against pure readers; the rtnl semaphore provides the |
| 184 | * protection against other writers. |
| 185 | * |
| 186 | * See, for example usages, register_netdevice() and |
| 187 | * unregister_netdevice(), which must be called with the rtnl |
| 188 | * semaphore held. |
| 189 | */ |
| 190 | DEFINE_RWLOCK(dev_base_lock); |
| 191 | EXPORT_SYMBOL(dev_base_lock); |
| 192 | |
| 193 | static DEFINE_MUTEX(ifalias_mutex); |
| 194 | |
| 195 | /* protects napi_hash addition/deletion and napi_gen_id */ |
| 196 | static DEFINE_SPINLOCK(napi_hash_lock); |
| 197 | |
| 198 | static unsigned int napi_gen_id = NR_CPUS; |
| 199 | static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8); |
| 200 | |
| 201 | static DECLARE_RWSEM(devnet_rename_sem); |
| 202 | |
| 203 | static inline void dev_base_seq_inc(struct net *net) |
| 204 | { |
| 205 | while (++net->dev_base_seq == 0) |
| 206 | ; |
| 207 | } |
| 208 | |
| 209 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
| 210 | { |
| 211 | unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ)); |
| 212 | |
| 213 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
| 214 | } |
| 215 | |
| 216 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
| 217 | { |
| 218 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
| 219 | } |
| 220 | |
| 221 | static inline void rps_lock(struct softnet_data *sd) |
| 222 | { |
| 223 | #ifdef CONFIG_RPS |
| 224 | spin_lock(&sd->input_pkt_queue.lock); |
| 225 | #endif |
| 226 | } |
| 227 | |
| 228 | static inline void rps_unlock(struct softnet_data *sd) |
| 229 | { |
| 230 | #ifdef CONFIG_RPS |
| 231 | spin_unlock(&sd->input_pkt_queue.lock); |
| 232 | #endif |
| 233 | } |
| 234 | |
| 235 | /* Device list insertion */ |
| 236 | static void list_netdevice(struct net_device *dev) |
| 237 | { |
| 238 | struct net *net = dev_net(dev); |
| 239 | |
| 240 | ASSERT_RTNL(); |
| 241 | |
| 242 | write_lock_bh(&dev_base_lock); |
| 243 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
| 244 | hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name)); |
| 245 | hlist_add_head_rcu(&dev->index_hlist, |
| 246 | dev_index_hash(net, dev->ifindex)); |
| 247 | write_unlock_bh(&dev_base_lock); |
| 248 | |
| 249 | dev_base_seq_inc(net); |
| 250 | } |
| 251 | |
| 252 | /* Device list removal |
| 253 | * caller must respect a RCU grace period before freeing/reusing dev |
| 254 | */ |
| 255 | static void unlist_netdevice(struct net_device *dev) |
| 256 | { |
| 257 | ASSERT_RTNL(); |
| 258 | |
| 259 | /* Unlink dev from the device chain */ |
| 260 | write_lock_bh(&dev_base_lock); |
| 261 | list_del_rcu(&dev->dev_list); |
| 262 | hlist_del_rcu(&dev->name_hlist); |
| 263 | hlist_del_rcu(&dev->index_hlist); |
| 264 | write_unlock_bh(&dev_base_lock); |
| 265 | |
| 266 | dev_base_seq_inc(dev_net(dev)); |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Our notifier list |
| 271 | */ |
| 272 | |
| 273 | static RAW_NOTIFIER_HEAD(netdev_chain); |
| 274 | |
| 275 | /* |
| 276 | * Device drivers call our routines to queue packets here. We empty the |
| 277 | * queue in the local softnet handler. |
| 278 | */ |
| 279 | |
| 280 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
| 281 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
| 282 | |
| 283 | /******************************************************************************* |
| 284 | * |
| 285 | * Protocol management and registration routines |
| 286 | * |
| 287 | *******************************************************************************/ |
| 288 | |
| 289 | |
| 290 | /* |
| 291 | * Add a protocol ID to the list. Now that the input handler is |
| 292 | * smarter we can dispense with all the messy stuff that used to be |
| 293 | * here. |
| 294 | * |
| 295 | * BEWARE!!! Protocol handlers, mangling input packets, |
| 296 | * MUST BE last in hash buckets and checking protocol handlers |
| 297 | * MUST start from promiscuous ptype_all chain in net_bh. |
| 298 | * It is true now, do not change it. |
| 299 | * Explanation follows: if protocol handler, mangling packet, will |
| 300 | * be the first on list, it is not able to sense, that packet |
| 301 | * is cloned and should be copied-on-write, so that it will |
| 302 | * change it and subsequent readers will get broken packet. |
| 303 | * --ANK (980803) |
| 304 | */ |
| 305 | |
| 306 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
| 307 | { |
| 308 | struct list_head vendor_pt = { .next = NULL, }; |
| 309 | |
| 310 | trace_android_vh_ptype_head(pt, &vendor_pt); |
| 311 | if (vendor_pt.next) |
| 312 | return vendor_pt.next; |
| 313 | |
| 314 | if (pt->type == htons(ETH_P_ALL)) |
| 315 | return pt->dev ? &pt->dev->ptype_all : &ptype_all; |
| 316 | else |
| 317 | return pt->dev ? &pt->dev->ptype_specific : |
| 318 | &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * dev_add_pack - add packet handler |
| 323 | * @pt: packet type declaration |
| 324 | * |
| 325 | * Add a protocol handler to the networking stack. The passed &packet_type |
| 326 | * is linked into kernel lists and may not be freed until it has been |
| 327 | * removed from the kernel lists. |
| 328 | * |
| 329 | * This call does not sleep therefore it can not |
| 330 | * guarantee all CPU's that are in middle of receiving packets |
| 331 | * will see the new packet type (until the next received packet). |
| 332 | */ |
| 333 | |
| 334 | void dev_add_pack(struct packet_type *pt) |
| 335 | { |
| 336 | struct list_head *head = ptype_head(pt); |
| 337 | |
| 338 | spin_lock(&ptype_lock); |
| 339 | list_add_rcu(&pt->list, head); |
| 340 | spin_unlock(&ptype_lock); |
| 341 | } |
| 342 | EXPORT_SYMBOL(dev_add_pack); |
| 343 | |
| 344 | /** |
| 345 | * __dev_remove_pack - remove packet handler |
| 346 | * @pt: packet type declaration |
| 347 | * |
| 348 | * Remove a protocol handler that was previously added to the kernel |
| 349 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed |
| 350 | * from the kernel lists and can be freed or reused once this function |
| 351 | * returns. |
| 352 | * |
| 353 | * The packet type might still be in use by receivers |
| 354 | * and must not be freed until after all the CPU's have gone |
| 355 | * through a quiescent state. |
| 356 | */ |
| 357 | void __dev_remove_pack(struct packet_type *pt) |
| 358 | { |
| 359 | struct list_head *head = ptype_head(pt); |
| 360 | struct packet_type *pt1; |
| 361 | |
| 362 | spin_lock(&ptype_lock); |
| 363 | |
| 364 | list_for_each_entry(pt1, head, list) { |
| 365 | if (pt == pt1) { |
| 366 | list_del_rcu(&pt->list); |
| 367 | goto out; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | pr_warn("dev_remove_pack: %p not found\n", pt); |
| 372 | out: |
| 373 | spin_unlock(&ptype_lock); |
| 374 | } |
| 375 | EXPORT_SYMBOL(__dev_remove_pack); |
| 376 | |
| 377 | /** |
| 378 | * dev_remove_pack - remove packet handler |
| 379 | * @pt: packet type declaration |
| 380 | * |
| 381 | * Remove a protocol handler that was previously added to the kernel |
| 382 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed |
| 383 | * from the kernel lists and can be freed or reused once this function |
| 384 | * returns. |
| 385 | * |
| 386 | * This call sleeps to guarantee that no CPU is looking at the packet |
| 387 | * type after return. |
| 388 | */ |
| 389 | void dev_remove_pack(struct packet_type *pt) |
| 390 | { |
| 391 | __dev_remove_pack(pt); |
| 392 | |
| 393 | synchronize_net(); |
| 394 | } |
| 395 | EXPORT_SYMBOL(dev_remove_pack); |
| 396 | |
| 397 | |
| 398 | /** |
| 399 | * dev_add_offload - register offload handlers |
| 400 | * @po: protocol offload declaration |
| 401 | * |
| 402 | * Add protocol offload handlers to the networking stack. The passed |
| 403 | * &proto_offload is linked into kernel lists and may not be freed until |
| 404 | * it has been removed from the kernel lists. |
| 405 | * |
| 406 | * This call does not sleep therefore it can not |
| 407 | * guarantee all CPU's that are in middle of receiving packets |
| 408 | * will see the new offload handlers (until the next received packet). |
| 409 | */ |
| 410 | void dev_add_offload(struct packet_offload *po) |
| 411 | { |
| 412 | struct packet_offload *elem; |
| 413 | |
| 414 | spin_lock(&offload_lock); |
| 415 | list_for_each_entry(elem, &offload_base, list) { |
| 416 | if (po->priority < elem->priority) |
| 417 | break; |
| 418 | } |
| 419 | list_add_rcu(&po->list, elem->list.prev); |
| 420 | spin_unlock(&offload_lock); |
| 421 | } |
| 422 | EXPORT_SYMBOL(dev_add_offload); |
| 423 | |
| 424 | /** |
| 425 | * __dev_remove_offload - remove offload handler |
| 426 | * @po: packet offload declaration |
| 427 | * |
| 428 | * Remove a protocol offload handler that was previously added to the |
| 429 | * kernel offload handlers by dev_add_offload(). The passed &offload_type |
| 430 | * is removed from the kernel lists and can be freed or reused once this |
| 431 | * function returns. |
| 432 | * |
| 433 | * The packet type might still be in use by receivers |
| 434 | * and must not be freed until after all the CPU's have gone |
| 435 | * through a quiescent state. |
| 436 | */ |
| 437 | static void __dev_remove_offload(struct packet_offload *po) |
| 438 | { |
| 439 | struct list_head *head = &offload_base; |
| 440 | struct packet_offload *po1; |
| 441 | |
| 442 | spin_lock(&offload_lock); |
| 443 | |
| 444 | list_for_each_entry(po1, head, list) { |
| 445 | if (po == po1) { |
| 446 | list_del_rcu(&po->list); |
| 447 | goto out; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | pr_warn("dev_remove_offload: %p not found\n", po); |
| 452 | out: |
| 453 | spin_unlock(&offload_lock); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * dev_remove_offload - remove packet offload handler |
| 458 | * @po: packet offload declaration |
| 459 | * |
| 460 | * Remove a packet offload handler that was previously added to the kernel |
| 461 | * offload handlers by dev_add_offload(). The passed &offload_type is |
| 462 | * removed from the kernel lists and can be freed or reused once this |
| 463 | * function returns. |
| 464 | * |
| 465 | * This call sleeps to guarantee that no CPU is looking at the packet |
| 466 | * type after return. |
| 467 | */ |
| 468 | void dev_remove_offload(struct packet_offload *po) |
| 469 | { |
| 470 | __dev_remove_offload(po); |
| 471 | |
| 472 | synchronize_net(); |
| 473 | } |
| 474 | EXPORT_SYMBOL(dev_remove_offload); |
| 475 | |
| 476 | /****************************************************************************** |
| 477 | * |
| 478 | * Device Boot-time Settings Routines |
| 479 | * |
| 480 | ******************************************************************************/ |
| 481 | |
| 482 | /* Boot time configuration table */ |
| 483 | static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX]; |
| 484 | |
| 485 | /** |
| 486 | * netdev_boot_setup_add - add new setup entry |
| 487 | * @name: name of the device |
| 488 | * @map: configured settings for the device |
| 489 | * |
| 490 | * Adds new setup entry to the dev_boot_setup list. The function |
| 491 | * returns 0 on error and 1 on success. This is a generic routine to |
| 492 | * all netdevices. |
| 493 | */ |
| 494 | static int netdev_boot_setup_add(char *name, struct ifmap *map) |
| 495 | { |
| 496 | struct netdev_boot_setup *s; |
| 497 | int i; |
| 498 | |
| 499 | s = dev_boot_setup; |
| 500 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { |
| 501 | if (s[i].name[0] == '\0' || s[i].name[0] == ' ') { |
| 502 | memset(s[i].name, 0, sizeof(s[i].name)); |
| 503 | strlcpy(s[i].name, name, IFNAMSIZ); |
| 504 | memcpy(&s[i].map, map, sizeof(s[i].map)); |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * netdev_boot_setup_check - check boot time settings |
| 514 | * @dev: the netdevice |
| 515 | * |
| 516 | * Check boot time settings for the device. |
| 517 | * The found settings are set for the device to be used |
| 518 | * later in the device probing. |
| 519 | * Returns 0 if no settings found, 1 if they are. |
| 520 | */ |
| 521 | int netdev_boot_setup_check(struct net_device *dev) |
| 522 | { |
| 523 | struct netdev_boot_setup *s = dev_boot_setup; |
| 524 | int i; |
| 525 | |
| 526 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { |
| 527 | if (s[i].name[0] != '\0' && s[i].name[0] != ' ' && |
| 528 | !strcmp(dev->name, s[i].name)) { |
| 529 | dev->irq = s[i].map.irq; |
| 530 | dev->base_addr = s[i].map.base_addr; |
| 531 | dev->mem_start = s[i].map.mem_start; |
| 532 | dev->mem_end = s[i].map.mem_end; |
| 533 | return 1; |
| 534 | } |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | EXPORT_SYMBOL(netdev_boot_setup_check); |
| 539 | |
| 540 | |
| 541 | /** |
| 542 | * netdev_boot_base - get address from boot time settings |
| 543 | * @prefix: prefix for network device |
| 544 | * @unit: id for network device |
| 545 | * |
| 546 | * Check boot time settings for the base address of device. |
| 547 | * The found settings are set for the device to be used |
| 548 | * later in the device probing. |
| 549 | * Returns 0 if no settings found. |
| 550 | */ |
| 551 | unsigned long netdev_boot_base(const char *prefix, int unit) |
| 552 | { |
| 553 | const struct netdev_boot_setup *s = dev_boot_setup; |
| 554 | char name[IFNAMSIZ]; |
| 555 | int i; |
| 556 | |
| 557 | sprintf(name, "%s%d", prefix, unit); |
| 558 | |
| 559 | /* |
| 560 | * If device already registered then return base of 1 |
| 561 | * to indicate not to probe for this interface |
| 562 | */ |
| 563 | if (__dev_get_by_name(&init_net, name)) |
| 564 | return 1; |
| 565 | |
| 566 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) |
| 567 | if (!strcmp(name, s[i].name)) |
| 568 | return s[i].map.base_addr; |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Saves at boot time configured settings for any netdevice. |
| 574 | */ |
| 575 | int __init netdev_boot_setup(char *str) |
| 576 | { |
| 577 | int ints[5]; |
| 578 | struct ifmap map; |
| 579 | |
| 580 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 581 | if (!str || !*str) |
| 582 | return 0; |
| 583 | |
| 584 | /* Save settings */ |
| 585 | memset(&map, 0, sizeof(map)); |
| 586 | if (ints[0] > 0) |
| 587 | map.irq = ints[1]; |
| 588 | if (ints[0] > 1) |
| 589 | map.base_addr = ints[2]; |
| 590 | if (ints[0] > 2) |
| 591 | map.mem_start = ints[3]; |
| 592 | if (ints[0] > 3) |
| 593 | map.mem_end = ints[4]; |
| 594 | |
| 595 | /* Add new entry to the list */ |
| 596 | return netdev_boot_setup_add(str, &map); |
| 597 | } |
| 598 | |
| 599 | __setup("netdev=", netdev_boot_setup); |
| 600 | |
| 601 | /******************************************************************************* |
| 602 | * |
| 603 | * Device Interface Subroutines |
| 604 | * |
| 605 | *******************************************************************************/ |
| 606 | |
| 607 | /** |
| 608 | * dev_get_iflink - get 'iflink' value of a interface |
| 609 | * @dev: targeted interface |
| 610 | * |
| 611 | * Indicates the ifindex the interface is linked to. |
| 612 | * Physical interfaces have the same 'ifindex' and 'iflink' values. |
| 613 | */ |
| 614 | |
| 615 | int dev_get_iflink(const struct net_device *dev) |
| 616 | { |
| 617 | if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink) |
| 618 | return dev->netdev_ops->ndo_get_iflink(dev); |
| 619 | |
| 620 | return dev->ifindex; |
| 621 | } |
| 622 | EXPORT_SYMBOL(dev_get_iflink); |
| 623 | |
| 624 | /** |
| 625 | * dev_fill_metadata_dst - Retrieve tunnel egress information. |
| 626 | * @dev: targeted interface |
| 627 | * @skb: The packet. |
| 628 | * |
| 629 | * For better visibility of tunnel traffic OVS needs to retrieve |
| 630 | * egress tunnel information for a packet. Following API allows |
| 631 | * user to get this info. |
| 632 | */ |
| 633 | int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
| 634 | { |
| 635 | struct ip_tunnel_info *info; |
| 636 | |
| 637 | if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst) |
| 638 | return -EINVAL; |
| 639 | |
| 640 | info = skb_tunnel_info_unclone(skb); |
| 641 | if (!info) |
| 642 | return -ENOMEM; |
| 643 | if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX))) |
| 644 | return -EINVAL; |
| 645 | |
| 646 | return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb); |
| 647 | } |
| 648 | EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); |
| 649 | |
| 650 | /** |
| 651 | * __dev_get_by_name - find a device by its name |
| 652 | * @net: the applicable net namespace |
| 653 | * @name: name to find |
| 654 | * |
| 655 | * Find an interface by name. Must be called under RTNL semaphore |
| 656 | * or @dev_base_lock. If the name is found a pointer to the device |
| 657 | * is returned. If the name is not found then %NULL is returned. The |
| 658 | * reference counters are not incremented so the caller must be |
| 659 | * careful with locks. |
| 660 | */ |
| 661 | |
| 662 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
| 663 | { |
| 664 | struct net_device *dev; |
| 665 | struct hlist_head *head = dev_name_hash(net, name); |
| 666 | |
| 667 | hlist_for_each_entry(dev, head, name_hlist) |
| 668 | if (!strncmp(dev->name, name, IFNAMSIZ)) |
| 669 | return dev; |
| 670 | |
| 671 | return NULL; |
| 672 | } |
| 673 | EXPORT_SYMBOL(__dev_get_by_name); |
| 674 | |
| 675 | /** |
| 676 | * dev_get_by_name_rcu - find a device by its name |
| 677 | * @net: the applicable net namespace |
| 678 | * @name: name to find |
| 679 | * |
| 680 | * Find an interface by name. |
| 681 | * If the name is found a pointer to the device is returned. |
| 682 | * If the name is not found then %NULL is returned. |
| 683 | * The reference counters are not incremented so the caller must be |
| 684 | * careful with locks. The caller must hold RCU lock. |
| 685 | */ |
| 686 | |
| 687 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) |
| 688 | { |
| 689 | struct net_device *dev; |
| 690 | struct hlist_head *head = dev_name_hash(net, name); |
| 691 | |
| 692 | hlist_for_each_entry_rcu(dev, head, name_hlist) |
| 693 | if (!strncmp(dev->name, name, IFNAMSIZ)) |
| 694 | return dev; |
| 695 | |
| 696 | return NULL; |
| 697 | } |
| 698 | EXPORT_SYMBOL(dev_get_by_name_rcu); |
| 699 | |
| 700 | /** |
| 701 | * dev_get_by_name - find a device by its name |
| 702 | * @net: the applicable net namespace |
| 703 | * @name: name to find |
| 704 | * |
| 705 | * Find an interface by name. This can be called from any |
| 706 | * context and does its own locking. The returned handle has |
| 707 | * the usage count incremented and the caller must use dev_put() to |
| 708 | * release it when it is no longer needed. %NULL is returned if no |
| 709 | * matching device is found. |
| 710 | */ |
| 711 | |
| 712 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
| 713 | { |
| 714 | struct net_device *dev; |
| 715 | |
| 716 | rcu_read_lock(); |
| 717 | dev = dev_get_by_name_rcu(net, name); |
| 718 | if (dev) |
| 719 | dev_hold(dev); |
| 720 | rcu_read_unlock(); |
| 721 | return dev; |
| 722 | } |
| 723 | EXPORT_SYMBOL(dev_get_by_name); |
| 724 | |
| 725 | /** |
| 726 | * __dev_get_by_index - find a device by its ifindex |
| 727 | * @net: the applicable net namespace |
| 728 | * @ifindex: index of device |
| 729 | * |
| 730 | * Search for an interface by index. Returns %NULL if the device |
| 731 | * is not found or a pointer to the device. The device has not |
| 732 | * had its reference counter increased so the caller must be careful |
| 733 | * about locking. The caller must hold either the RTNL semaphore |
| 734 | * or @dev_base_lock. |
| 735 | */ |
| 736 | |
| 737 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
| 738 | { |
| 739 | struct net_device *dev; |
| 740 | struct hlist_head *head = dev_index_hash(net, ifindex); |
| 741 | |
| 742 | hlist_for_each_entry(dev, head, index_hlist) |
| 743 | if (dev->ifindex == ifindex) |
| 744 | return dev; |
| 745 | |
| 746 | return NULL; |
| 747 | } |
| 748 | EXPORT_SYMBOL(__dev_get_by_index); |
| 749 | |
| 750 | /** |
| 751 | * dev_get_by_index_rcu - find a device by its ifindex |
| 752 | * @net: the applicable net namespace |
| 753 | * @ifindex: index of device |
| 754 | * |
| 755 | * Search for an interface by index. Returns %NULL if the device |
| 756 | * is not found or a pointer to the device. The device has not |
| 757 | * had its reference counter increased so the caller must be careful |
| 758 | * about locking. The caller must hold RCU lock. |
| 759 | */ |
| 760 | |
| 761 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) |
| 762 | { |
| 763 | struct net_device *dev; |
| 764 | struct hlist_head *head = dev_index_hash(net, ifindex); |
| 765 | |
| 766 | hlist_for_each_entry_rcu(dev, head, index_hlist) |
| 767 | if (dev->ifindex == ifindex) |
| 768 | return dev; |
| 769 | |
| 770 | return NULL; |
| 771 | } |
| 772 | EXPORT_SYMBOL(dev_get_by_index_rcu); |
| 773 | |
| 774 | |
| 775 | /** |
| 776 | * dev_get_by_index - find a device by its ifindex |
| 777 | * @net: the applicable net namespace |
| 778 | * @ifindex: index of device |
| 779 | * |
| 780 | * Search for an interface by index. Returns NULL if the device |
| 781 | * is not found or a pointer to the device. The device returned has |
| 782 | * had a reference added and the pointer is safe until the user calls |
| 783 | * dev_put to indicate they have finished with it. |
| 784 | */ |
| 785 | |
| 786 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
| 787 | { |
| 788 | struct net_device *dev; |
| 789 | |
| 790 | rcu_read_lock(); |
| 791 | dev = dev_get_by_index_rcu(net, ifindex); |
| 792 | if (dev) |
| 793 | dev_hold(dev); |
| 794 | rcu_read_unlock(); |
| 795 | return dev; |
| 796 | } |
| 797 | EXPORT_SYMBOL(dev_get_by_index); |
| 798 | |
| 799 | /** |
| 800 | * dev_get_by_napi_id - find a device by napi_id |
| 801 | * @napi_id: ID of the NAPI struct |
| 802 | * |
| 803 | * Search for an interface by NAPI ID. Returns %NULL if the device |
| 804 | * is not found or a pointer to the device. The device has not had |
| 805 | * its reference counter increased so the caller must be careful |
| 806 | * about locking. The caller must hold RCU lock. |
| 807 | */ |
| 808 | |
| 809 | struct net_device *dev_get_by_napi_id(unsigned int napi_id) |
| 810 | { |
| 811 | struct napi_struct *napi; |
| 812 | |
| 813 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 814 | |
| 815 | if (napi_id < MIN_NAPI_ID) |
| 816 | return NULL; |
| 817 | |
| 818 | napi = napi_by_id(napi_id); |
| 819 | |
| 820 | return napi ? napi->dev : NULL; |
| 821 | } |
| 822 | EXPORT_SYMBOL(dev_get_by_napi_id); |
| 823 | |
| 824 | /** |
| 825 | * netdev_get_name - get a netdevice name, knowing its ifindex. |
| 826 | * @net: network namespace |
| 827 | * @name: a pointer to the buffer where the name will be stored. |
| 828 | * @ifindex: the ifindex of the interface to get the name from. |
| 829 | */ |
| 830 | int netdev_get_name(struct net *net, char *name, int ifindex) |
| 831 | { |
| 832 | struct net_device *dev; |
| 833 | int ret; |
| 834 | |
| 835 | down_read(&devnet_rename_sem); |
| 836 | rcu_read_lock(); |
| 837 | |
| 838 | dev = dev_get_by_index_rcu(net, ifindex); |
| 839 | if (!dev) { |
| 840 | ret = -ENODEV; |
| 841 | goto out; |
| 842 | } |
| 843 | |
| 844 | strcpy(name, dev->name); |
| 845 | |
| 846 | ret = 0; |
| 847 | out: |
| 848 | rcu_read_unlock(); |
| 849 | up_read(&devnet_rename_sem); |
| 850 | return ret; |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
| 855 | * @net: the applicable net namespace |
| 856 | * @type: media type of device |
| 857 | * @ha: hardware address |
| 858 | * |
| 859 | * Search for an interface by MAC address. Returns NULL if the device |
| 860 | * is not found or a pointer to the device. |
| 861 | * The caller must hold RCU or RTNL. |
| 862 | * The returned device has not had its ref count increased |
| 863 | * and the caller must therefore be careful about locking |
| 864 | * |
| 865 | */ |
| 866 | |
| 867 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
| 868 | const char *ha) |
| 869 | { |
| 870 | struct net_device *dev; |
| 871 | |
| 872 | for_each_netdev_rcu(net, dev) |
| 873 | if (dev->type == type && |
| 874 | !memcmp(dev->dev_addr, ha, dev->addr_len)) |
| 875 | return dev; |
| 876 | |
| 877 | return NULL; |
| 878 | } |
| 879 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
| 880 | |
| 881 | struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type) |
| 882 | { |
| 883 | struct net_device *dev; |
| 884 | |
| 885 | ASSERT_RTNL(); |
| 886 | for_each_netdev(net, dev) |
| 887 | if (dev->type == type) |
| 888 | return dev; |
| 889 | |
| 890 | return NULL; |
| 891 | } |
| 892 | EXPORT_SYMBOL(__dev_getfirstbyhwtype); |
| 893 | |
| 894 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
| 895 | { |
| 896 | struct net_device *dev, *ret = NULL; |
| 897 | |
| 898 | rcu_read_lock(); |
| 899 | for_each_netdev_rcu(net, dev) |
| 900 | if (dev->type == type) { |
| 901 | dev_hold(dev); |
| 902 | ret = dev; |
| 903 | break; |
| 904 | } |
| 905 | rcu_read_unlock(); |
| 906 | return ret; |
| 907 | } |
| 908 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
| 909 | |
| 910 | /** |
| 911 | * __dev_get_by_flags - find any device with given flags |
| 912 | * @net: the applicable net namespace |
| 913 | * @if_flags: IFF_* values |
| 914 | * @mask: bitmask of bits in if_flags to check |
| 915 | * |
| 916 | * Search for any interface with the given flags. Returns NULL if a device |
| 917 | * is not found or a pointer to the device. Must be called inside |
| 918 | * rtnl_lock(), and result refcount is unchanged. |
| 919 | */ |
| 920 | |
| 921 | struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags, |
| 922 | unsigned short mask) |
| 923 | { |
| 924 | struct net_device *dev, *ret; |
| 925 | |
| 926 | ASSERT_RTNL(); |
| 927 | |
| 928 | ret = NULL; |
| 929 | for_each_netdev(net, dev) { |
| 930 | if (((dev->flags ^ if_flags) & mask) == 0) { |
| 931 | ret = dev; |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | return ret; |
| 936 | } |
| 937 | EXPORT_SYMBOL(__dev_get_by_flags); |
| 938 | |
| 939 | /** |
| 940 | * dev_valid_name - check if name is okay for network device |
| 941 | * @name: name string |
| 942 | * |
| 943 | * Network device names need to be valid file names to |
| 944 | * to allow sysfs to work. We also disallow any kind of |
| 945 | * whitespace. |
| 946 | */ |
| 947 | bool dev_valid_name(const char *name) |
| 948 | { |
| 949 | if (*name == '\0') |
| 950 | return false; |
| 951 | if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) |
| 952 | return false; |
| 953 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 954 | return false; |
| 955 | |
| 956 | while (*name) { |
| 957 | if (*name == '/' || *name == ':' || isspace(*name)) |
| 958 | return false; |
| 959 | name++; |
| 960 | } |
| 961 | return true; |
| 962 | } |
| 963 | EXPORT_SYMBOL(dev_valid_name); |
| 964 | |
| 965 | /** |
| 966 | * __dev_alloc_name - allocate a name for a device |
| 967 | * @net: network namespace to allocate the device name in |
| 968 | * @name: name format string |
| 969 | * @buf: scratch buffer and result name string |
| 970 | * |
| 971 | * Passed a format string - eg "lt%d" it will try and find a suitable |
| 972 | * id. It scans list of devices to build up a free map, then chooses |
| 973 | * the first empty slot. The caller must hold the dev_base or rtnl lock |
| 974 | * while allocating the name and adding the device in order to avoid |
| 975 | * duplicates. |
| 976 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). |
| 977 | * Returns the number of the unit assigned or a negative errno code. |
| 978 | */ |
| 979 | |
| 980 | static int __dev_alloc_name(struct net *net, const char *name, char *buf) |
| 981 | { |
| 982 | int i = 0; |
| 983 | const char *p; |
| 984 | const int max_netdevices = 8*PAGE_SIZE; |
| 985 | unsigned long *inuse; |
| 986 | struct net_device *d; |
| 987 | |
| 988 | if (!dev_valid_name(name)) |
| 989 | return -EINVAL; |
| 990 | |
| 991 | p = strchr(name, '%'); |
| 992 | if (p) { |
| 993 | /* |
| 994 | * Verify the string as this thing may have come from |
| 995 | * the user. There must be either one "%d" and no other "%" |
| 996 | * characters. |
| 997 | */ |
| 998 | if (p[1] != 'd' || strchr(p + 2, '%')) |
| 999 | return -EINVAL; |
| 1000 | |
| 1001 | /* Use one page as a bit array of possible slots */ |
| 1002 | inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); |
| 1003 | if (!inuse) |
| 1004 | return -ENOMEM; |
| 1005 | |
| 1006 | for_each_netdev(net, d) { |
| 1007 | if (!sscanf(d->name, name, &i)) |
| 1008 | continue; |
| 1009 | if (i < 0 || i >= max_netdevices) |
| 1010 | continue; |
| 1011 | |
| 1012 | /* avoid cases where sscanf is not exact inverse of printf */ |
| 1013 | snprintf(buf, IFNAMSIZ, name, i); |
| 1014 | if (!strncmp(buf, d->name, IFNAMSIZ)) |
| 1015 | set_bit(i, inuse); |
| 1016 | } |
| 1017 | |
| 1018 | i = find_first_zero_bit(inuse, max_netdevices); |
| 1019 | free_page((unsigned long) inuse); |
| 1020 | } |
| 1021 | |
| 1022 | snprintf(buf, IFNAMSIZ, name, i); |
| 1023 | if (!__dev_get_by_name(net, buf)) |
| 1024 | return i; |
| 1025 | |
| 1026 | /* It is possible to run out of possible slots |
| 1027 | * when the name is long and there isn't enough space left |
| 1028 | * for the digits, or if all bits are used. |
| 1029 | */ |
| 1030 | return -ENFILE; |
| 1031 | } |
| 1032 | |
| 1033 | static int dev_alloc_name_ns(struct net *net, |
| 1034 | struct net_device *dev, |
| 1035 | const char *name) |
| 1036 | { |
| 1037 | char buf[IFNAMSIZ]; |
| 1038 | int ret; |
| 1039 | |
| 1040 | BUG_ON(!net); |
| 1041 | ret = __dev_alloc_name(net, name, buf); |
| 1042 | if (ret >= 0) |
| 1043 | strlcpy(dev->name, buf, IFNAMSIZ); |
| 1044 | return ret; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * dev_alloc_name - allocate a name for a device |
| 1049 | * @dev: device |
| 1050 | * @name: name format string |
| 1051 | * |
| 1052 | * Passed a format string - eg "lt%d" it will try and find a suitable |
| 1053 | * id. It scans list of devices to build up a free map, then chooses |
| 1054 | * the first empty slot. The caller must hold the dev_base or rtnl lock |
| 1055 | * while allocating the name and adding the device in order to avoid |
| 1056 | * duplicates. |
| 1057 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). |
| 1058 | * Returns the number of the unit assigned or a negative errno code. |
| 1059 | */ |
| 1060 | |
| 1061 | int dev_alloc_name(struct net_device *dev, const char *name) |
| 1062 | { |
| 1063 | return dev_alloc_name_ns(dev_net(dev), dev, name); |
| 1064 | } |
| 1065 | EXPORT_SYMBOL(dev_alloc_name); |
| 1066 | |
| 1067 | int dev_get_valid_name(struct net *net, struct net_device *dev, |
| 1068 | const char *name) |
| 1069 | { |
| 1070 | BUG_ON(!net); |
| 1071 | |
| 1072 | if (!dev_valid_name(name)) |
| 1073 | return -EINVAL; |
| 1074 | |
| 1075 | if (strchr(name, '%')) |
| 1076 | return dev_alloc_name_ns(net, dev, name); |
| 1077 | else if (__dev_get_by_name(net, name)) |
| 1078 | return -EEXIST; |
| 1079 | else if (dev->name != name) |
| 1080 | strlcpy(dev->name, name, IFNAMSIZ); |
| 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | EXPORT_SYMBOL(dev_get_valid_name); |
| 1085 | |
| 1086 | /** |
| 1087 | * dev_change_name - change name of a device |
| 1088 | * @dev: device |
| 1089 | * @newname: name (or format string) must be at least IFNAMSIZ |
| 1090 | * |
| 1091 | * Change name of a device, can pass format strings "eth%d". |
| 1092 | * for wildcarding. |
| 1093 | */ |
| 1094 | int dev_change_name(struct net_device *dev, const char *newname) |
| 1095 | { |
| 1096 | unsigned char old_assign_type; |
| 1097 | char oldname[IFNAMSIZ]; |
| 1098 | int err = 0; |
| 1099 | int ret; |
| 1100 | struct net *net; |
| 1101 | |
| 1102 | ASSERT_RTNL(); |
| 1103 | BUG_ON(!dev_net(dev)); |
| 1104 | |
| 1105 | net = dev_net(dev); |
| 1106 | |
| 1107 | /* Some auto-enslaved devices e.g. failover slaves are |
| 1108 | * special, as userspace might rename the device after |
| 1109 | * the interface had been brought up and running since |
| 1110 | * the point kernel initiated auto-enslavement. Allow |
| 1111 | * live name change even when these slave devices are |
| 1112 | * up and running. |
| 1113 | * |
| 1114 | * Typically, users of these auto-enslaving devices |
| 1115 | * don't actually care about slave name change, as |
| 1116 | * they are supposed to operate on master interface |
| 1117 | * directly. |
| 1118 | */ |
| 1119 | if (dev->flags & IFF_UP && |
| 1120 | likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK))) |
| 1121 | return -EBUSY; |
| 1122 | |
| 1123 | down_write(&devnet_rename_sem); |
| 1124 | |
| 1125 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { |
| 1126 | up_write(&devnet_rename_sem); |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | memcpy(oldname, dev->name, IFNAMSIZ); |
| 1131 | |
| 1132 | err = dev_get_valid_name(net, dev, newname); |
| 1133 | if (err < 0) { |
| 1134 | up_write(&devnet_rename_sem); |
| 1135 | return err; |
| 1136 | } |
| 1137 | |
| 1138 | if (oldname[0] && !strchr(oldname, '%')) |
| 1139 | netdev_info(dev, "renamed from %s\n", oldname); |
| 1140 | |
| 1141 | old_assign_type = dev->name_assign_type; |
| 1142 | dev->name_assign_type = NET_NAME_RENAMED; |
| 1143 | |
| 1144 | rollback: |
| 1145 | ret = device_rename(&dev->dev, dev->name); |
| 1146 | if (ret) { |
| 1147 | memcpy(dev->name, oldname, IFNAMSIZ); |
| 1148 | dev->name_assign_type = old_assign_type; |
| 1149 | up_write(&devnet_rename_sem); |
| 1150 | return ret; |
| 1151 | } |
| 1152 | |
| 1153 | up_write(&devnet_rename_sem); |
| 1154 | |
| 1155 | netdev_adjacent_rename_links(dev, oldname); |
| 1156 | |
| 1157 | write_lock_bh(&dev_base_lock); |
| 1158 | hlist_del_rcu(&dev->name_hlist); |
| 1159 | write_unlock_bh(&dev_base_lock); |
| 1160 | |
| 1161 | synchronize_rcu(); |
| 1162 | |
| 1163 | write_lock_bh(&dev_base_lock); |
| 1164 | hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name)); |
| 1165 | write_unlock_bh(&dev_base_lock); |
| 1166 | |
| 1167 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
| 1168 | ret = notifier_to_errno(ret); |
| 1169 | |
| 1170 | if (ret) { |
| 1171 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
| 1172 | if (err >= 0) { |
| 1173 | err = ret; |
| 1174 | down_write(&devnet_rename_sem); |
| 1175 | memcpy(dev->name, oldname, IFNAMSIZ); |
| 1176 | memcpy(oldname, newname, IFNAMSIZ); |
| 1177 | dev->name_assign_type = old_assign_type; |
| 1178 | old_assign_type = NET_NAME_RENAMED; |
| 1179 | goto rollback; |
| 1180 | } else { |
| 1181 | pr_err("%s: name change rollback failed: %d\n", |
| 1182 | dev->name, ret); |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | return err; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * dev_set_alias - change ifalias of a device |
| 1191 | * @dev: device |
| 1192 | * @alias: name up to IFALIASZ |
| 1193 | * @len: limit of bytes to copy from info |
| 1194 | * |
| 1195 | * Set ifalias for a device, |
| 1196 | */ |
| 1197 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) |
| 1198 | { |
| 1199 | struct dev_ifalias *new_alias = NULL; |
| 1200 | |
| 1201 | if (len >= IFALIASZ) |
| 1202 | return -EINVAL; |
| 1203 | |
| 1204 | if (len) { |
| 1205 | new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL); |
| 1206 | if (!new_alias) |
| 1207 | return -ENOMEM; |
| 1208 | |
| 1209 | memcpy(new_alias->ifalias, alias, len); |
| 1210 | new_alias->ifalias[len] = 0; |
| 1211 | } |
| 1212 | |
| 1213 | mutex_lock(&ifalias_mutex); |
| 1214 | rcu_swap_protected(dev->ifalias, new_alias, |
| 1215 | mutex_is_locked(&ifalias_mutex)); |
| 1216 | mutex_unlock(&ifalias_mutex); |
| 1217 | |
| 1218 | if (new_alias) |
| 1219 | kfree_rcu(new_alias, rcuhead); |
| 1220 | |
| 1221 | return len; |
| 1222 | } |
| 1223 | EXPORT_SYMBOL(dev_set_alias); |
| 1224 | |
| 1225 | /** |
| 1226 | * dev_get_alias - get ifalias of a device |
| 1227 | * @dev: device |
| 1228 | * @name: buffer to store name of ifalias |
| 1229 | * @len: size of buffer |
| 1230 | * |
| 1231 | * get ifalias for a device. Caller must make sure dev cannot go |
| 1232 | * away, e.g. rcu read lock or own a reference count to device. |
| 1233 | */ |
| 1234 | int dev_get_alias(const struct net_device *dev, char *name, size_t len) |
| 1235 | { |
| 1236 | const struct dev_ifalias *alias; |
| 1237 | int ret = 0; |
| 1238 | |
| 1239 | rcu_read_lock(); |
| 1240 | alias = rcu_dereference(dev->ifalias); |
| 1241 | if (alias) |
| 1242 | ret = snprintf(name, len, "%s", alias->ifalias); |
| 1243 | rcu_read_unlock(); |
| 1244 | |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | /** |
| 1249 | * netdev_features_change - device changes features |
| 1250 | * @dev: device to cause notification |
| 1251 | * |
| 1252 | * Called to indicate a device has changed features. |
| 1253 | */ |
| 1254 | void netdev_features_change(struct net_device *dev) |
| 1255 | { |
| 1256 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
| 1257 | } |
| 1258 | EXPORT_SYMBOL(netdev_features_change); |
| 1259 | |
| 1260 | /** |
| 1261 | * netdev_state_change - device changes state |
| 1262 | * @dev: device to cause notification |
| 1263 | * |
| 1264 | * Called to indicate a device has changed state. This function calls |
| 1265 | * the notifier chains for netdev_chain and sends a NEWLINK message |
| 1266 | * to the routing socket. |
| 1267 | */ |
| 1268 | void netdev_state_change(struct net_device *dev) |
| 1269 | { |
| 1270 | if (dev->flags & IFF_UP) { |
| 1271 | struct netdev_notifier_change_info change_info = { |
| 1272 | .info.dev = dev, |
| 1273 | }; |
| 1274 | |
| 1275 | call_netdevice_notifiers_info(NETDEV_CHANGE, |
| 1276 | &change_info.info); |
| 1277 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
| 1278 | } |
| 1279 | } |
| 1280 | EXPORT_SYMBOL(netdev_state_change); |
| 1281 | |
| 1282 | /** |
| 1283 | * netdev_notify_peers - notify network peers about existence of @dev |
| 1284 | * @dev: network device |
| 1285 | * |
| 1286 | * Generate traffic such that interested network peers are aware of |
| 1287 | * @dev, such as by generating a gratuitous ARP. This may be used when |
| 1288 | * a device wants to inform the rest of the network about some sort of |
| 1289 | * reconfiguration such as a failover event or virtual machine |
| 1290 | * migration. |
| 1291 | */ |
| 1292 | void netdev_notify_peers(struct net_device *dev) |
| 1293 | { |
| 1294 | rtnl_lock(); |
| 1295 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); |
| 1296 | call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev); |
| 1297 | rtnl_unlock(); |
| 1298 | } |
| 1299 | EXPORT_SYMBOL(netdev_notify_peers); |
| 1300 | |
| 1301 | static int napi_threaded_poll(void *data); |
| 1302 | |
| 1303 | static int napi_kthread_create(struct napi_struct *n) |
| 1304 | { |
| 1305 | int err = 0; |
| 1306 | |
| 1307 | /* Create and wake up the kthread once to put it in |
| 1308 | * TASK_INTERRUPTIBLE mode to avoid the blocked task |
| 1309 | * warning and work with loadavg. |
| 1310 | */ |
| 1311 | n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d", |
| 1312 | n->dev->name, n->napi_id); |
| 1313 | if (IS_ERR(n->thread)) { |
| 1314 | err = PTR_ERR(n->thread); |
| 1315 | pr_err("kthread_run failed with err %d\n", err); |
| 1316 | n->thread = NULL; |
| 1317 | } |
| 1318 | |
| 1319 | return err; |
| 1320 | } |
| 1321 | |
| 1322 | static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
| 1323 | { |
| 1324 | const struct net_device_ops *ops = dev->netdev_ops; |
| 1325 | int ret; |
| 1326 | |
| 1327 | ASSERT_RTNL(); |
| 1328 | |
| 1329 | if (!netif_device_present(dev)) |
| 1330 | return -ENODEV; |
| 1331 | |
| 1332 | /* Block netpoll from trying to do any rx path servicing. |
| 1333 | * If we don't do this there is a chance ndo_poll_controller |
| 1334 | * or ndo_poll may be running while we open the device |
| 1335 | */ |
| 1336 | netpoll_poll_disable(dev); |
| 1337 | |
| 1338 | ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack); |
| 1339 | ret = notifier_to_errno(ret); |
| 1340 | if (ret) |
| 1341 | return ret; |
| 1342 | |
| 1343 | set_bit(__LINK_STATE_START, &dev->state); |
| 1344 | |
| 1345 | if (ops->ndo_validate_addr) |
| 1346 | ret = ops->ndo_validate_addr(dev); |
| 1347 | |
| 1348 | if (!ret && ops->ndo_open) |
| 1349 | ret = ops->ndo_open(dev); |
| 1350 | |
| 1351 | netpoll_poll_enable(dev); |
| 1352 | |
| 1353 | if (ret) |
| 1354 | clear_bit(__LINK_STATE_START, &dev->state); |
| 1355 | else { |
| 1356 | dev->flags |= IFF_UP; |
| 1357 | dev_set_rx_mode(dev); |
| 1358 | dev_activate(dev); |
| 1359 | add_device_randomness(dev->dev_addr, dev->addr_len); |
| 1360 | } |
| 1361 | |
| 1362 | return ret; |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * dev_open - prepare an interface for use. |
| 1367 | * @dev: device to open |
| 1368 | * @extack: netlink extended ack |
| 1369 | * |
| 1370 | * Takes a device from down to up state. The device's private open |
| 1371 | * function is invoked and then the multicast lists are loaded. Finally |
| 1372 | * the device is moved into the up state and a %NETDEV_UP message is |
| 1373 | * sent to the netdev notifier chain. |
| 1374 | * |
| 1375 | * Calling this function on an active interface is a nop. On a failure |
| 1376 | * a negative errno code is returned. |
| 1377 | */ |
| 1378 | int dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
| 1379 | { |
| 1380 | int ret; |
| 1381 | |
| 1382 | if (dev->flags & IFF_UP) |
| 1383 | return 0; |
| 1384 | |
| 1385 | ret = __dev_open(dev, extack); |
| 1386 | if (ret < 0) |
| 1387 | return ret; |
| 1388 | |
| 1389 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
| 1390 | call_netdevice_notifiers(NETDEV_UP, dev); |
| 1391 | |
| 1392 | return ret; |
| 1393 | } |
| 1394 | EXPORT_SYMBOL(dev_open); |
| 1395 | |
| 1396 | static void __dev_close_many(struct list_head *head) |
| 1397 | { |
| 1398 | struct net_device *dev; |
| 1399 | |
| 1400 | ASSERT_RTNL(); |
| 1401 | might_sleep(); |
| 1402 | |
| 1403 | list_for_each_entry(dev, head, close_list) { |
| 1404 | /* Temporarily disable netpoll until the interface is down */ |
| 1405 | netpoll_poll_disable(dev); |
| 1406 | |
| 1407 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
| 1408 | |
| 1409 | clear_bit(__LINK_STATE_START, &dev->state); |
| 1410 | |
| 1411 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
| 1412 | * can be even on different cpu. So just clear netif_running(). |
| 1413 | * |
| 1414 | * dev->stop() will invoke napi_disable() on all of it's |
| 1415 | * napi_struct instances on this device. |
| 1416 | */ |
| 1417 | smp_mb__after_atomic(); /* Commit netif_running(). */ |
| 1418 | } |
| 1419 | |
| 1420 | dev_deactivate_many(head); |
| 1421 | |
| 1422 | list_for_each_entry(dev, head, close_list) { |
| 1423 | const struct net_device_ops *ops = dev->netdev_ops; |
| 1424 | |
| 1425 | /* |
| 1426 | * Call the device specific close. This cannot fail. |
| 1427 | * Only if device is UP |
| 1428 | * |
| 1429 | * We allow it to be called even after a DETACH hot-plug |
| 1430 | * event. |
| 1431 | */ |
| 1432 | if (ops->ndo_stop) |
| 1433 | ops->ndo_stop(dev); |
| 1434 | |
| 1435 | dev->flags &= ~IFF_UP; |
| 1436 | netpoll_poll_enable(dev); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | static void __dev_close(struct net_device *dev) |
| 1441 | { |
| 1442 | LIST_HEAD(single); |
| 1443 | |
| 1444 | list_add(&dev->close_list, &single); |
| 1445 | __dev_close_many(&single); |
| 1446 | list_del(&single); |
| 1447 | } |
| 1448 | |
| 1449 | void dev_close_many(struct list_head *head, bool unlink) |
| 1450 | { |
| 1451 | struct net_device *dev, *tmp; |
| 1452 | |
| 1453 | /* Remove the devices that don't need to be closed */ |
| 1454 | list_for_each_entry_safe(dev, tmp, head, close_list) |
| 1455 | if (!(dev->flags & IFF_UP)) |
| 1456 | list_del_init(&dev->close_list); |
| 1457 | |
| 1458 | __dev_close_many(head); |
| 1459 | |
| 1460 | list_for_each_entry_safe(dev, tmp, head, close_list) { |
| 1461 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
| 1462 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
| 1463 | if (unlink) |
| 1464 | list_del_init(&dev->close_list); |
| 1465 | } |
| 1466 | } |
| 1467 | EXPORT_SYMBOL(dev_close_many); |
| 1468 | |
| 1469 | /** |
| 1470 | * dev_close - shutdown an interface. |
| 1471 | * @dev: device to shutdown |
| 1472 | * |
| 1473 | * This function moves an active device into down state. A |
| 1474 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device |
| 1475 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier |
| 1476 | * chain. |
| 1477 | */ |
| 1478 | void dev_close(struct net_device *dev) |
| 1479 | { |
| 1480 | if (dev->flags & IFF_UP) { |
| 1481 | LIST_HEAD(single); |
| 1482 | |
| 1483 | list_add(&dev->close_list, &single); |
| 1484 | dev_close_many(&single, true); |
| 1485 | list_del(&single); |
| 1486 | } |
| 1487 | } |
| 1488 | EXPORT_SYMBOL(dev_close); |
| 1489 | |
| 1490 | |
| 1491 | /** |
| 1492 | * dev_disable_lro - disable Large Receive Offload on a device |
| 1493 | * @dev: device |
| 1494 | * |
| 1495 | * Disable Large Receive Offload (LRO) on a net device. Must be |
| 1496 | * called under RTNL. This is needed if received packets may be |
| 1497 | * forwarded to another interface. |
| 1498 | */ |
| 1499 | void dev_disable_lro(struct net_device *dev) |
| 1500 | { |
| 1501 | struct net_device *lower_dev; |
| 1502 | struct list_head *iter; |
| 1503 | |
| 1504 | dev->wanted_features &= ~NETIF_F_LRO; |
| 1505 | netdev_update_features(dev); |
| 1506 | |
| 1507 | if (unlikely(dev->features & NETIF_F_LRO)) |
| 1508 | netdev_WARN(dev, "failed to disable LRO!\n"); |
| 1509 | |
| 1510 | netdev_for_each_lower_dev(dev, lower_dev, iter) |
| 1511 | dev_disable_lro(lower_dev); |
| 1512 | } |
| 1513 | EXPORT_SYMBOL(dev_disable_lro); |
| 1514 | |
| 1515 | /** |
| 1516 | * dev_disable_gro_hw - disable HW Generic Receive Offload on a device |
| 1517 | * @dev: device |
| 1518 | * |
| 1519 | * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be |
| 1520 | * called under RTNL. This is needed if Generic XDP is installed on |
| 1521 | * the device. |
| 1522 | */ |
| 1523 | static void dev_disable_gro_hw(struct net_device *dev) |
| 1524 | { |
| 1525 | dev->wanted_features &= ~NETIF_F_GRO_HW; |
| 1526 | netdev_update_features(dev); |
| 1527 | |
| 1528 | if (unlikely(dev->features & NETIF_F_GRO_HW)) |
| 1529 | netdev_WARN(dev, "failed to disable GRO_HW!\n"); |
| 1530 | } |
| 1531 | |
| 1532 | const char *netdev_cmd_to_name(enum netdev_cmd cmd) |
| 1533 | { |
| 1534 | #define N(val) \ |
| 1535 | case NETDEV_##val: \ |
| 1536 | return "NETDEV_" __stringify(val); |
| 1537 | switch (cmd) { |
| 1538 | N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER) |
| 1539 | N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE) |
| 1540 | N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE) |
| 1541 | N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER) |
| 1542 | N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO) |
| 1543 | N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO) |
| 1544 | N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN) |
| 1545 | N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO) |
| 1546 | N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) |
| 1547 | N(PRE_CHANGEADDR) |
| 1548 | } |
| 1549 | #undef N |
| 1550 | return "UNKNOWN_NETDEV_EVENT"; |
| 1551 | } |
| 1552 | EXPORT_SYMBOL_GPL(netdev_cmd_to_name); |
| 1553 | |
| 1554 | static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, |
| 1555 | struct net_device *dev) |
| 1556 | { |
| 1557 | struct netdev_notifier_info info = { |
| 1558 | .dev = dev, |
| 1559 | }; |
| 1560 | |
| 1561 | return nb->notifier_call(nb, val, &info); |
| 1562 | } |
| 1563 | |
| 1564 | static int dev_boot_phase = 1; |
| 1565 | |
| 1566 | /** |
| 1567 | * register_netdevice_notifier - register a network notifier block |
| 1568 | * @nb: notifier |
| 1569 | * |
| 1570 | * Register a notifier to be called when network device events occur. |
| 1571 | * The notifier passed is linked into the kernel structures and must |
| 1572 | * not be reused until it has been unregistered. A negative errno code |
| 1573 | * is returned on a failure. |
| 1574 | * |
| 1575 | * When registered all registration and up events are replayed |
| 1576 | * to the new notifier to allow device to have a race free |
| 1577 | * view of the network device list. |
| 1578 | */ |
| 1579 | |
| 1580 | int register_netdevice_notifier(struct notifier_block *nb) |
| 1581 | { |
| 1582 | struct net_device *dev; |
| 1583 | struct net_device *last; |
| 1584 | struct net *net; |
| 1585 | int err; |
| 1586 | |
| 1587 | /* Close race with setup_net() and cleanup_net() */ |
| 1588 | down_write(&pernet_ops_rwsem); |
| 1589 | rtnl_lock(); |
| 1590 | err = raw_notifier_chain_register(&netdev_chain, nb); |
| 1591 | if (err) |
| 1592 | goto unlock; |
| 1593 | if (dev_boot_phase) |
| 1594 | goto unlock; |
| 1595 | for_each_net(net) { |
| 1596 | for_each_netdev(net, dev) { |
| 1597 | err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); |
| 1598 | err = notifier_to_errno(err); |
| 1599 | if (err) |
| 1600 | goto rollback; |
| 1601 | |
| 1602 | if (!(dev->flags & IFF_UP)) |
| 1603 | continue; |
| 1604 | |
| 1605 | call_netdevice_notifier(nb, NETDEV_UP, dev); |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | unlock: |
| 1610 | rtnl_unlock(); |
| 1611 | up_write(&pernet_ops_rwsem); |
| 1612 | return err; |
| 1613 | |
| 1614 | rollback: |
| 1615 | last = dev; |
| 1616 | for_each_net(net) { |
| 1617 | for_each_netdev(net, dev) { |
| 1618 | if (dev == last) |
| 1619 | goto outroll; |
| 1620 | |
| 1621 | if (dev->flags & IFF_UP) { |
| 1622 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, |
| 1623 | dev); |
| 1624 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); |
| 1625 | } |
| 1626 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | outroll: |
| 1631 | raw_notifier_chain_unregister(&netdev_chain, nb); |
| 1632 | goto unlock; |
| 1633 | } |
| 1634 | EXPORT_SYMBOL(register_netdevice_notifier); |
| 1635 | |
| 1636 | /** |
| 1637 | * unregister_netdevice_notifier - unregister a network notifier block |
| 1638 | * @nb: notifier |
| 1639 | * |
| 1640 | * Unregister a notifier previously registered by |
| 1641 | * register_netdevice_notifier(). The notifier is unlinked into the |
| 1642 | * kernel structures and may then be reused. A negative errno code |
| 1643 | * is returned on a failure. |
| 1644 | * |
| 1645 | * After unregistering unregister and down device events are synthesized |
| 1646 | * for all devices on the device list to the removed notifier to remove |
| 1647 | * the need for special case cleanup code. |
| 1648 | */ |
| 1649 | |
| 1650 | int unregister_netdevice_notifier(struct notifier_block *nb) |
| 1651 | { |
| 1652 | struct net_device *dev; |
| 1653 | struct net *net; |
| 1654 | int err; |
| 1655 | |
| 1656 | /* Close race with setup_net() and cleanup_net() */ |
| 1657 | down_write(&pernet_ops_rwsem); |
| 1658 | rtnl_lock(); |
| 1659 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
| 1660 | if (err) |
| 1661 | goto unlock; |
| 1662 | |
| 1663 | for_each_net(net) { |
| 1664 | for_each_netdev(net, dev) { |
| 1665 | if (dev->flags & IFF_UP) { |
| 1666 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, |
| 1667 | dev); |
| 1668 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); |
| 1669 | } |
| 1670 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); |
| 1671 | } |
| 1672 | } |
| 1673 | unlock: |
| 1674 | rtnl_unlock(); |
| 1675 | up_write(&pernet_ops_rwsem); |
| 1676 | return err; |
| 1677 | } |
| 1678 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
| 1679 | |
| 1680 | /** |
| 1681 | * call_netdevice_notifiers_info - call all network notifier blocks |
| 1682 | * @val: value passed unmodified to notifier function |
| 1683 | * @info: notifier information data |
| 1684 | * |
| 1685 | * Call all network notifier blocks. Parameters and return value |
| 1686 | * are as for raw_notifier_call_chain(). |
| 1687 | */ |
| 1688 | |
| 1689 | static int call_netdevice_notifiers_info(unsigned long val, |
| 1690 | struct netdev_notifier_info *info) |
| 1691 | { |
| 1692 | ASSERT_RTNL(); |
| 1693 | return raw_notifier_call_chain(&netdev_chain, val, info); |
| 1694 | } |
| 1695 | |
| 1696 | static int call_netdevice_notifiers_extack(unsigned long val, |
| 1697 | struct net_device *dev, |
| 1698 | struct netlink_ext_ack *extack) |
| 1699 | { |
| 1700 | struct netdev_notifier_info info = { |
| 1701 | .dev = dev, |
| 1702 | .extack = extack, |
| 1703 | }; |
| 1704 | |
| 1705 | return call_netdevice_notifiers_info(val, &info); |
| 1706 | } |
| 1707 | |
| 1708 | /** |
| 1709 | * call_netdevice_notifiers - call all network notifier blocks |
| 1710 | * @val: value passed unmodified to notifier function |
| 1711 | * @dev: net_device pointer passed unmodified to notifier function |
| 1712 | * |
| 1713 | * Call all network notifier blocks. Parameters and return value |
| 1714 | * are as for raw_notifier_call_chain(). |
| 1715 | */ |
| 1716 | |
| 1717 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
| 1718 | { |
| 1719 | return call_netdevice_notifiers_extack(val, dev, NULL); |
| 1720 | } |
| 1721 | EXPORT_SYMBOL(call_netdevice_notifiers); |
| 1722 | |
| 1723 | /** |
| 1724 | * call_netdevice_notifiers_mtu - call all network notifier blocks |
| 1725 | * @val: value passed unmodified to notifier function |
| 1726 | * @dev: net_device pointer passed unmodified to notifier function |
| 1727 | * @arg: additional u32 argument passed to the notifier function |
| 1728 | * |
| 1729 | * Call all network notifier blocks. Parameters and return value |
| 1730 | * are as for raw_notifier_call_chain(). |
| 1731 | */ |
| 1732 | static int call_netdevice_notifiers_mtu(unsigned long val, |
| 1733 | struct net_device *dev, u32 arg) |
| 1734 | { |
| 1735 | struct netdev_notifier_info_ext info = { |
| 1736 | .info.dev = dev, |
| 1737 | .ext.mtu = arg, |
| 1738 | }; |
| 1739 | |
| 1740 | BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0); |
| 1741 | |
| 1742 | return call_netdevice_notifiers_info(val, &info.info); |
| 1743 | } |
| 1744 | |
| 1745 | #ifdef CONFIG_NET_INGRESS |
| 1746 | static DEFINE_STATIC_KEY_FALSE(ingress_needed_key); |
| 1747 | |
| 1748 | void net_inc_ingress_queue(void) |
| 1749 | { |
| 1750 | static_branch_inc(&ingress_needed_key); |
| 1751 | } |
| 1752 | EXPORT_SYMBOL_GPL(net_inc_ingress_queue); |
| 1753 | |
| 1754 | void net_dec_ingress_queue(void) |
| 1755 | { |
| 1756 | static_branch_dec(&ingress_needed_key); |
| 1757 | } |
| 1758 | EXPORT_SYMBOL_GPL(net_dec_ingress_queue); |
| 1759 | #endif |
| 1760 | |
| 1761 | #ifdef CONFIG_NET_EGRESS |
| 1762 | static DEFINE_STATIC_KEY_FALSE(egress_needed_key); |
| 1763 | |
| 1764 | void net_inc_egress_queue(void) |
| 1765 | { |
| 1766 | static_branch_inc(&egress_needed_key); |
| 1767 | } |
| 1768 | EXPORT_SYMBOL_GPL(net_inc_egress_queue); |
| 1769 | |
| 1770 | void net_dec_egress_queue(void) |
| 1771 | { |
| 1772 | static_branch_dec(&egress_needed_key); |
| 1773 | } |
| 1774 | EXPORT_SYMBOL_GPL(net_dec_egress_queue); |
| 1775 | #endif |
| 1776 | |
| 1777 | static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key); |
| 1778 | #ifdef CONFIG_JUMP_LABEL |
| 1779 | static atomic_t netstamp_needed_deferred; |
| 1780 | static atomic_t netstamp_wanted; |
| 1781 | static void netstamp_clear(struct work_struct *work) |
| 1782 | { |
| 1783 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); |
| 1784 | int wanted; |
| 1785 | |
| 1786 | wanted = atomic_add_return(deferred, &netstamp_wanted); |
| 1787 | if (wanted > 0) |
| 1788 | static_branch_enable(&netstamp_needed_key); |
| 1789 | else |
| 1790 | static_branch_disable(&netstamp_needed_key); |
| 1791 | } |
| 1792 | static DECLARE_WORK(netstamp_work, netstamp_clear); |
| 1793 | #endif |
| 1794 | |
| 1795 | void net_enable_timestamp(void) |
| 1796 | { |
| 1797 | #ifdef CONFIG_JUMP_LABEL |
| 1798 | int wanted; |
| 1799 | |
| 1800 | while (1) { |
| 1801 | wanted = atomic_read(&netstamp_wanted); |
| 1802 | if (wanted <= 0) |
| 1803 | break; |
| 1804 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted) |
| 1805 | return; |
| 1806 | } |
| 1807 | atomic_inc(&netstamp_needed_deferred); |
| 1808 | schedule_work(&netstamp_work); |
| 1809 | #else |
| 1810 | static_branch_inc(&netstamp_needed_key); |
| 1811 | #endif |
| 1812 | } |
| 1813 | EXPORT_SYMBOL(net_enable_timestamp); |
| 1814 | |
| 1815 | void net_disable_timestamp(void) |
| 1816 | { |
| 1817 | #ifdef CONFIG_JUMP_LABEL |
| 1818 | int wanted; |
| 1819 | |
| 1820 | while (1) { |
| 1821 | wanted = atomic_read(&netstamp_wanted); |
| 1822 | if (wanted <= 1) |
| 1823 | break; |
| 1824 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted) |
| 1825 | return; |
| 1826 | } |
| 1827 | atomic_dec(&netstamp_needed_deferred); |
| 1828 | schedule_work(&netstamp_work); |
| 1829 | #else |
| 1830 | static_branch_dec(&netstamp_needed_key); |
| 1831 | #endif |
| 1832 | } |
| 1833 | EXPORT_SYMBOL(net_disable_timestamp); |
| 1834 | |
| 1835 | static inline void net_timestamp_set(struct sk_buff *skb) |
| 1836 | { |
| 1837 | skb->tstamp = 0; |
| 1838 | if (static_branch_unlikely(&netstamp_needed_key)) |
| 1839 | __net_timestamp(skb); |
| 1840 | } |
| 1841 | |
| 1842 | #define net_timestamp_check(COND, SKB) \ |
| 1843 | if (static_branch_unlikely(&netstamp_needed_key)) { \ |
| 1844 | if ((COND) && !(SKB)->tstamp) \ |
| 1845 | __net_timestamp(SKB); \ |
| 1846 | } \ |
| 1847 | |
| 1848 | bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb) |
| 1849 | { |
| 1850 | unsigned int len; |
| 1851 | |
| 1852 | if (!(dev->flags & IFF_UP)) |
| 1853 | return false; |
| 1854 | |
| 1855 | len = dev->mtu + dev->hard_header_len + VLAN_HLEN; |
| 1856 | if (skb->len <= len) |
| 1857 | return true; |
| 1858 | |
| 1859 | /* if TSO is enabled, we don't care about the length as the packet |
| 1860 | * could be forwarded without being segmented before |
| 1861 | */ |
| 1862 | if (skb_is_gso(skb)) |
| 1863 | return true; |
| 1864 | |
| 1865 | return false; |
| 1866 | } |
| 1867 | EXPORT_SYMBOL_GPL(is_skb_forwardable); |
| 1868 | |
| 1869 | int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb) |
| 1870 | { |
| 1871 | int ret = ____dev_forward_skb(dev, skb); |
| 1872 | |
| 1873 | if (likely(!ret)) { |
| 1874 | skb->protocol = eth_type_trans(skb, dev); |
| 1875 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
| 1876 | } |
| 1877 | |
| 1878 | return ret; |
| 1879 | } |
| 1880 | EXPORT_SYMBOL_GPL(__dev_forward_skb); |
| 1881 | |
| 1882 | /** |
| 1883 | * dev_forward_skb - loopback an skb to another netif |
| 1884 | * |
| 1885 | * @dev: destination network device |
| 1886 | * @skb: buffer to forward |
| 1887 | * |
| 1888 | * return values: |
| 1889 | * NET_RX_SUCCESS (no congestion) |
| 1890 | * NET_RX_DROP (packet was dropped, but freed) |
| 1891 | * |
| 1892 | * dev_forward_skb can be used for injecting an skb from the |
| 1893 | * start_xmit function of one device into the receive queue |
| 1894 | * of another device. |
| 1895 | * |
| 1896 | * The receiving device may be in another namespace, so |
| 1897 | * we have to clear all information in the skb that could |
| 1898 | * impact namespace isolation. |
| 1899 | */ |
| 1900 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) |
| 1901 | { |
| 1902 | return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb); |
| 1903 | } |
| 1904 | EXPORT_SYMBOL_GPL(dev_forward_skb); |
| 1905 | |
| 1906 | static inline int deliver_skb(struct sk_buff *skb, |
| 1907 | struct packet_type *pt_prev, |
| 1908 | struct net_device *orig_dev) |
| 1909 | { |
| 1910 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
| 1911 | return -ENOMEM; |
| 1912 | refcount_inc(&skb->users); |
| 1913 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
| 1914 | } |
| 1915 | |
| 1916 | static inline void deliver_ptype_list_skb(struct sk_buff *skb, |
| 1917 | struct packet_type **pt, |
| 1918 | struct net_device *orig_dev, |
| 1919 | __be16 type, |
| 1920 | struct list_head *ptype_list) |
| 1921 | { |
| 1922 | struct packet_type *ptype, *pt_prev = *pt; |
| 1923 | |
| 1924 | list_for_each_entry_rcu(ptype, ptype_list, list) { |
| 1925 | if (ptype->type != type) |
| 1926 | continue; |
| 1927 | if (pt_prev) |
| 1928 | deliver_skb(skb, pt_prev, orig_dev); |
| 1929 | pt_prev = ptype; |
| 1930 | } |
| 1931 | *pt = pt_prev; |
| 1932 | } |
| 1933 | |
| 1934 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
| 1935 | { |
| 1936 | if (!ptype->af_packet_priv || !skb->sk) |
| 1937 | return false; |
| 1938 | |
| 1939 | if (ptype->id_match) |
| 1940 | return ptype->id_match(ptype, skb->sk); |
| 1941 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) |
| 1942 | return true; |
| 1943 | |
| 1944 | return false; |
| 1945 | } |
| 1946 | |
| 1947 | /** |
| 1948 | * dev_nit_active - return true if any network interface taps are in use |
| 1949 | * |
| 1950 | * @dev: network device to check for the presence of taps |
| 1951 | */ |
| 1952 | bool dev_nit_active(struct net_device *dev) |
| 1953 | { |
| 1954 | return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all); |
| 1955 | } |
| 1956 | EXPORT_SYMBOL_GPL(dev_nit_active); |
| 1957 | |
| 1958 | /* |
| 1959 | * Support routine. Sends outgoing frames to any network |
| 1960 | * taps currently in use. |
| 1961 | */ |
| 1962 | |
| 1963 | void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
| 1964 | { |
| 1965 | struct packet_type *ptype; |
| 1966 | struct sk_buff *skb2 = NULL; |
| 1967 | struct packet_type *pt_prev = NULL; |
| 1968 | struct list_head *ptype_list = &ptype_all; |
| 1969 | |
| 1970 | rcu_read_lock(); |
| 1971 | again: |
| 1972 | list_for_each_entry_rcu(ptype, ptype_list, list) { |
| 1973 | if (READ_ONCE(ptype->ignore_outgoing)) |
| 1974 | continue; |
| 1975 | |
| 1976 | /* Never send packets back to the socket |
| 1977 | * they originated from - MvS (miquels@drinkel.ow.org) |
| 1978 | */ |
| 1979 | if (skb_loop_sk(ptype, skb)) |
| 1980 | continue; |
| 1981 | |
| 1982 | if (pt_prev) { |
| 1983 | deliver_skb(skb2, pt_prev, skb->dev); |
| 1984 | pt_prev = ptype; |
| 1985 | continue; |
| 1986 | } |
| 1987 | |
| 1988 | /* need to clone skb, done only once */ |
| 1989 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1990 | if (!skb2) |
| 1991 | goto out_unlock; |
| 1992 | |
| 1993 | net_timestamp_set(skb2); |
| 1994 | |
| 1995 | /* skb->nh should be correctly |
| 1996 | * set by sender, so that the second statement is |
| 1997 | * just protection against buggy protocols. |
| 1998 | */ |
| 1999 | skb_reset_mac_header(skb2); |
| 2000 | |
| 2001 | if (skb_network_header(skb2) < skb2->data || |
| 2002 | skb_network_header(skb2) > skb_tail_pointer(skb2)) { |
| 2003 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", |
| 2004 | ntohs(skb2->protocol), |
| 2005 | dev->name); |
| 2006 | skb_reset_network_header(skb2); |
| 2007 | } |
| 2008 | |
| 2009 | skb2->transport_header = skb2->network_header; |
| 2010 | skb2->pkt_type = PACKET_OUTGOING; |
| 2011 | pt_prev = ptype; |
| 2012 | } |
| 2013 | |
| 2014 | if (ptype_list == &ptype_all) { |
| 2015 | ptype_list = &dev->ptype_all; |
| 2016 | goto again; |
| 2017 | } |
| 2018 | out_unlock: |
| 2019 | if (pt_prev) { |
| 2020 | if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC)) |
| 2021 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); |
| 2022 | else |
| 2023 | kfree_skb(skb2); |
| 2024 | } |
| 2025 | rcu_read_unlock(); |
| 2026 | } |
| 2027 | EXPORT_SYMBOL_GPL(dev_queue_xmit_nit); |
| 2028 | |
| 2029 | /** |
| 2030 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change |
| 2031 | * @dev: Network device |
| 2032 | * @txq: number of queues available |
| 2033 | * |
| 2034 | * If real_num_tx_queues is changed the tc mappings may no longer be |
| 2035 | * valid. To resolve this verify the tc mapping remains valid and if |
| 2036 | * not NULL the mapping. With no priorities mapping to this |
| 2037 | * offset/count pair it will no longer be used. In the worst case TC0 |
| 2038 | * is invalid nothing can be done so disable priority mappings. If is |
| 2039 | * expected that drivers will fix this mapping if they can before |
| 2040 | * calling netif_set_real_num_tx_queues. |
| 2041 | */ |
| 2042 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
| 2043 | { |
| 2044 | int i; |
| 2045 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; |
| 2046 | |
| 2047 | /* If TC0 is invalidated disable TC mapping */ |
| 2048 | if (tc->offset + tc->count > txq) { |
| 2049 | pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
| 2050 | dev->num_tc = 0; |
| 2051 | return; |
| 2052 | } |
| 2053 | |
| 2054 | /* Invalidated prio to tc mappings set to TC0 */ |
| 2055 | for (i = 1; i < TC_BITMASK + 1; i++) { |
| 2056 | int q = netdev_get_prio_tc_map(dev, i); |
| 2057 | |
| 2058 | tc = &dev->tc_to_txq[q]; |
| 2059 | if (tc->offset + tc->count > txq) { |
| 2060 | pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", |
| 2061 | i, q); |
| 2062 | netdev_set_prio_tc_map(dev, i, 0); |
| 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | int netdev_txq_to_tc(struct net_device *dev, unsigned int txq) |
| 2068 | { |
| 2069 | if (dev->num_tc) { |
| 2070 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; |
| 2071 | int i; |
| 2072 | |
| 2073 | /* walk through the TCs and see if it falls into any of them */ |
| 2074 | for (i = 0; i < TC_MAX_QUEUE; i++, tc++) { |
| 2075 | if ((txq - tc->offset) < tc->count) |
| 2076 | return i; |
| 2077 | } |
| 2078 | |
| 2079 | /* didn't find it, just return -1 to indicate no match */ |
| 2080 | return -1; |
| 2081 | } |
| 2082 | |
| 2083 | return 0; |
| 2084 | } |
| 2085 | EXPORT_SYMBOL(netdev_txq_to_tc); |
| 2086 | |
| 2087 | #ifdef CONFIG_XPS |
| 2088 | struct static_key xps_needed __read_mostly; |
| 2089 | EXPORT_SYMBOL(xps_needed); |
| 2090 | struct static_key xps_rxqs_needed __read_mostly; |
| 2091 | EXPORT_SYMBOL(xps_rxqs_needed); |
| 2092 | static DEFINE_MUTEX(xps_map_mutex); |
| 2093 | #define xmap_dereference(P) \ |
| 2094 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) |
| 2095 | |
| 2096 | static bool remove_xps_queue(struct xps_dev_maps *dev_maps, |
| 2097 | int tci, u16 index) |
| 2098 | { |
| 2099 | struct xps_map *map = NULL; |
| 2100 | int pos; |
| 2101 | |
| 2102 | if (dev_maps) |
| 2103 | map = xmap_dereference(dev_maps->attr_map[tci]); |
| 2104 | if (!map) |
| 2105 | return false; |
| 2106 | |
| 2107 | for (pos = map->len; pos--;) { |
| 2108 | if (map->queues[pos] != index) |
| 2109 | continue; |
| 2110 | |
| 2111 | if (map->len > 1) { |
| 2112 | map->queues[pos] = map->queues[--map->len]; |
| 2113 | break; |
| 2114 | } |
| 2115 | |
| 2116 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
| 2117 | kfree_rcu(map, rcu); |
| 2118 | return false; |
| 2119 | } |
| 2120 | |
| 2121 | return true; |
| 2122 | } |
| 2123 | |
| 2124 | static bool remove_xps_queue_cpu(struct net_device *dev, |
| 2125 | struct xps_dev_maps *dev_maps, |
| 2126 | int cpu, u16 offset, u16 count) |
| 2127 | { |
| 2128 | int num_tc = dev->num_tc ? : 1; |
| 2129 | bool active = false; |
| 2130 | int tci; |
| 2131 | |
| 2132 | for (tci = cpu * num_tc; num_tc--; tci++) { |
| 2133 | int i, j; |
| 2134 | |
| 2135 | for (i = count, j = offset; i--; j++) { |
| 2136 | if (!remove_xps_queue(dev_maps, tci, j)) |
| 2137 | break; |
| 2138 | } |
| 2139 | |
| 2140 | active |= i < 0; |
| 2141 | } |
| 2142 | |
| 2143 | return active; |
| 2144 | } |
| 2145 | |
| 2146 | static void reset_xps_maps(struct net_device *dev, |
| 2147 | struct xps_dev_maps *dev_maps, |
| 2148 | bool is_rxqs_map) |
| 2149 | { |
| 2150 | if (is_rxqs_map) { |
| 2151 | static_key_slow_dec_cpuslocked(&xps_rxqs_needed); |
| 2152 | RCU_INIT_POINTER(dev->xps_rxqs_map, NULL); |
| 2153 | } else { |
| 2154 | RCU_INIT_POINTER(dev->xps_cpus_map, NULL); |
| 2155 | } |
| 2156 | static_key_slow_dec_cpuslocked(&xps_needed); |
| 2157 | kfree_rcu(dev_maps, rcu); |
| 2158 | } |
| 2159 | |
| 2160 | static void clean_xps_maps(struct net_device *dev, const unsigned long *mask, |
| 2161 | struct xps_dev_maps *dev_maps, unsigned int nr_ids, |
| 2162 | u16 offset, u16 count, bool is_rxqs_map) |
| 2163 | { |
| 2164 | bool active = false; |
| 2165 | int i, j; |
| 2166 | |
| 2167 | for (j = -1; j = netif_attrmask_next(j, mask, nr_ids), |
| 2168 | j < nr_ids;) |
| 2169 | active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, |
| 2170 | count); |
| 2171 | if (!active) |
| 2172 | reset_xps_maps(dev, dev_maps, is_rxqs_map); |
| 2173 | |
| 2174 | if (!is_rxqs_map) { |
| 2175 | for (i = offset + (count - 1); count--; i--) { |
| 2176 | netdev_queue_numa_node_write( |
| 2177 | netdev_get_tx_queue(dev, i), |
| 2178 | NUMA_NO_NODE); |
| 2179 | } |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | static void netif_reset_xps_queues(struct net_device *dev, u16 offset, |
| 2184 | u16 count) |
| 2185 | { |
| 2186 | const unsigned long *possible_mask = NULL; |
| 2187 | struct xps_dev_maps *dev_maps; |
| 2188 | unsigned int nr_ids; |
| 2189 | |
| 2190 | if (!static_key_false(&xps_needed)) |
| 2191 | return; |
| 2192 | |
| 2193 | cpus_read_lock(); |
| 2194 | mutex_lock(&xps_map_mutex); |
| 2195 | |
| 2196 | if (static_key_false(&xps_rxqs_needed)) { |
| 2197 | dev_maps = xmap_dereference(dev->xps_rxqs_map); |
| 2198 | if (dev_maps) { |
| 2199 | nr_ids = dev->num_rx_queues; |
| 2200 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, |
| 2201 | offset, count, true); |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | dev_maps = xmap_dereference(dev->xps_cpus_map); |
| 2206 | if (!dev_maps) |
| 2207 | goto out_no_maps; |
| 2208 | |
| 2209 | if (num_possible_cpus() > 1) |
| 2210 | possible_mask = cpumask_bits(cpu_possible_mask); |
| 2211 | nr_ids = nr_cpu_ids; |
| 2212 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count, |
| 2213 | false); |
| 2214 | |
| 2215 | out_no_maps: |
| 2216 | mutex_unlock(&xps_map_mutex); |
| 2217 | cpus_read_unlock(); |
| 2218 | } |
| 2219 | |
| 2220 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
| 2221 | { |
| 2222 | netif_reset_xps_queues(dev, index, dev->num_tx_queues - index); |
| 2223 | } |
| 2224 | |
| 2225 | static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index, |
| 2226 | u16 index, bool is_rxqs_map) |
| 2227 | { |
| 2228 | struct xps_map *new_map; |
| 2229 | int alloc_len = XPS_MIN_MAP_ALLOC; |
| 2230 | int i, pos; |
| 2231 | |
| 2232 | for (pos = 0; map && pos < map->len; pos++) { |
| 2233 | if (map->queues[pos] != index) |
| 2234 | continue; |
| 2235 | return map; |
| 2236 | } |
| 2237 | |
| 2238 | /* Need to add tx-queue to this CPU's/rx-queue's existing map */ |
| 2239 | if (map) { |
| 2240 | if (pos < map->alloc_len) |
| 2241 | return map; |
| 2242 | |
| 2243 | alloc_len = map->alloc_len * 2; |
| 2244 | } |
| 2245 | |
| 2246 | /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's |
| 2247 | * map |
| 2248 | */ |
| 2249 | if (is_rxqs_map) |
| 2250 | new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL); |
| 2251 | else |
| 2252 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, |
| 2253 | cpu_to_node(attr_index)); |
| 2254 | if (!new_map) |
| 2255 | return NULL; |
| 2256 | |
| 2257 | for (i = 0; i < pos; i++) |
| 2258 | new_map->queues[i] = map->queues[i]; |
| 2259 | new_map->alloc_len = alloc_len; |
| 2260 | new_map->len = pos; |
| 2261 | |
| 2262 | return new_map; |
| 2263 | } |
| 2264 | |
| 2265 | /* Must be called under cpus_read_lock */ |
| 2266 | int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, |
| 2267 | u16 index, bool is_rxqs_map) |
| 2268 | { |
| 2269 | const unsigned long *online_mask = NULL, *possible_mask = NULL; |
| 2270 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL; |
| 2271 | int i, j, tci, numa_node_id = -2; |
| 2272 | int maps_sz, num_tc = 1, tc = 0; |
| 2273 | struct xps_map *map, *new_map; |
| 2274 | bool active = false; |
| 2275 | unsigned int nr_ids; |
| 2276 | |
| 2277 | WARN_ON_ONCE(index >= dev->num_tx_queues); |
| 2278 | |
| 2279 | if (dev->num_tc) { |
| 2280 | /* Do not allow XPS on subordinate device directly */ |
| 2281 | num_tc = dev->num_tc; |
| 2282 | if (num_tc < 0) |
| 2283 | return -EINVAL; |
| 2284 | |
| 2285 | /* If queue belongs to subordinate dev use its map */ |
| 2286 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; |
| 2287 | |
| 2288 | tc = netdev_txq_to_tc(dev, index); |
| 2289 | if (tc < 0) |
| 2290 | return -EINVAL; |
| 2291 | } |
| 2292 | |
| 2293 | mutex_lock(&xps_map_mutex); |
| 2294 | if (is_rxqs_map) { |
| 2295 | maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues); |
| 2296 | dev_maps = xmap_dereference(dev->xps_rxqs_map); |
| 2297 | nr_ids = dev->num_rx_queues; |
| 2298 | } else { |
| 2299 | maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc); |
| 2300 | if (num_possible_cpus() > 1) { |
| 2301 | online_mask = cpumask_bits(cpu_online_mask); |
| 2302 | possible_mask = cpumask_bits(cpu_possible_mask); |
| 2303 | } |
| 2304 | dev_maps = xmap_dereference(dev->xps_cpus_map); |
| 2305 | nr_ids = nr_cpu_ids; |
| 2306 | } |
| 2307 | |
| 2308 | if (maps_sz < L1_CACHE_BYTES) |
| 2309 | maps_sz = L1_CACHE_BYTES; |
| 2310 | |
| 2311 | /* allocate memory for queue storage */ |
| 2312 | for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids), |
| 2313 | j < nr_ids;) { |
| 2314 | if (!new_dev_maps) |
| 2315 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); |
| 2316 | if (!new_dev_maps) { |
| 2317 | mutex_unlock(&xps_map_mutex); |
| 2318 | return -ENOMEM; |
| 2319 | } |
| 2320 | |
| 2321 | tci = j * num_tc + tc; |
| 2322 | map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) : |
| 2323 | NULL; |
| 2324 | |
| 2325 | map = expand_xps_map(map, j, index, is_rxqs_map); |
| 2326 | if (!map) |
| 2327 | goto error; |
| 2328 | |
| 2329 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
| 2330 | } |
| 2331 | |
| 2332 | if (!new_dev_maps) |
| 2333 | goto out_no_new_maps; |
| 2334 | |
| 2335 | if (!dev_maps) { |
| 2336 | /* Increment static keys at most once per type */ |
| 2337 | static_key_slow_inc_cpuslocked(&xps_needed); |
| 2338 | if (is_rxqs_map) |
| 2339 | static_key_slow_inc_cpuslocked(&xps_rxqs_needed); |
| 2340 | } |
| 2341 | |
| 2342 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
| 2343 | j < nr_ids;) { |
| 2344 | /* copy maps belonging to foreign traffic classes */ |
| 2345 | for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) { |
| 2346 | /* fill in the new device map from the old device map */ |
| 2347 | map = xmap_dereference(dev_maps->attr_map[tci]); |
| 2348 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
| 2349 | } |
| 2350 | |
| 2351 | /* We need to explicitly update tci as prevous loop |
| 2352 | * could break out early if dev_maps is NULL. |
| 2353 | */ |
| 2354 | tci = j * num_tc + tc; |
| 2355 | |
| 2356 | if (netif_attr_test_mask(j, mask, nr_ids) && |
| 2357 | netif_attr_test_online(j, online_mask, nr_ids)) { |
| 2358 | /* add tx-queue to CPU/rx-queue maps */ |
| 2359 | int pos = 0; |
| 2360 | |
| 2361 | map = xmap_dereference(new_dev_maps->attr_map[tci]); |
| 2362 | while ((pos < map->len) && (map->queues[pos] != index)) |
| 2363 | pos++; |
| 2364 | |
| 2365 | if (pos == map->len) |
| 2366 | map->queues[map->len++] = index; |
| 2367 | #ifdef CONFIG_NUMA |
| 2368 | if (!is_rxqs_map) { |
| 2369 | if (numa_node_id == -2) |
| 2370 | numa_node_id = cpu_to_node(j); |
| 2371 | else if (numa_node_id != cpu_to_node(j)) |
| 2372 | numa_node_id = -1; |
| 2373 | } |
| 2374 | #endif |
| 2375 | } else if (dev_maps) { |
| 2376 | /* fill in the new device map from the old device map */ |
| 2377 | map = xmap_dereference(dev_maps->attr_map[tci]); |
| 2378 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
| 2379 | } |
| 2380 | |
| 2381 | /* copy maps belonging to foreign traffic classes */ |
| 2382 | for (i = num_tc - tc, tci++; dev_maps && --i; tci++) { |
| 2383 | /* fill in the new device map from the old device map */ |
| 2384 | map = xmap_dereference(dev_maps->attr_map[tci]); |
| 2385 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | if (is_rxqs_map) |
| 2390 | rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps); |
| 2391 | else |
| 2392 | rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps); |
| 2393 | |
| 2394 | /* Cleanup old maps */ |
| 2395 | if (!dev_maps) |
| 2396 | goto out_no_old_maps; |
| 2397 | |
| 2398 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
| 2399 | j < nr_ids;) { |
| 2400 | for (i = num_tc, tci = j * num_tc; i--; tci++) { |
| 2401 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); |
| 2402 | map = xmap_dereference(dev_maps->attr_map[tci]); |
| 2403 | if (map && map != new_map) |
| 2404 | kfree_rcu(map, rcu); |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | kfree_rcu(dev_maps, rcu); |
| 2409 | |
| 2410 | out_no_old_maps: |
| 2411 | dev_maps = new_dev_maps; |
| 2412 | active = true; |
| 2413 | |
| 2414 | out_no_new_maps: |
| 2415 | if (!is_rxqs_map) { |
| 2416 | /* update Tx queue numa node */ |
| 2417 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), |
| 2418 | (numa_node_id >= 0) ? |
| 2419 | numa_node_id : NUMA_NO_NODE); |
| 2420 | } |
| 2421 | |
| 2422 | if (!dev_maps) |
| 2423 | goto out_no_maps; |
| 2424 | |
| 2425 | /* removes tx-queue from unused CPUs/rx-queues */ |
| 2426 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
| 2427 | j < nr_ids;) { |
| 2428 | for (i = tc, tci = j * num_tc; i--; tci++) |
| 2429 | active |= remove_xps_queue(dev_maps, tci, index); |
| 2430 | if (!netif_attr_test_mask(j, mask, nr_ids) || |
| 2431 | !netif_attr_test_online(j, online_mask, nr_ids)) |
| 2432 | active |= remove_xps_queue(dev_maps, tci, index); |
| 2433 | for (i = num_tc - tc, tci++; --i; tci++) |
| 2434 | active |= remove_xps_queue(dev_maps, tci, index); |
| 2435 | } |
| 2436 | |
| 2437 | /* free map if not active */ |
| 2438 | if (!active) |
| 2439 | reset_xps_maps(dev, dev_maps, is_rxqs_map); |
| 2440 | |
| 2441 | out_no_maps: |
| 2442 | mutex_unlock(&xps_map_mutex); |
| 2443 | |
| 2444 | return 0; |
| 2445 | error: |
| 2446 | /* remove any maps that we added */ |
| 2447 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
| 2448 | j < nr_ids;) { |
| 2449 | for (i = num_tc, tci = j * num_tc; i--; tci++) { |
| 2450 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); |
| 2451 | map = dev_maps ? |
| 2452 | xmap_dereference(dev_maps->attr_map[tci]) : |
| 2453 | NULL; |
| 2454 | if (new_map && new_map != map) |
| 2455 | kfree(new_map); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | mutex_unlock(&xps_map_mutex); |
| 2460 | |
| 2461 | kfree(new_dev_maps); |
| 2462 | return -ENOMEM; |
| 2463 | } |
| 2464 | EXPORT_SYMBOL_GPL(__netif_set_xps_queue); |
| 2465 | |
| 2466 | int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask, |
| 2467 | u16 index) |
| 2468 | { |
| 2469 | int ret; |
| 2470 | |
| 2471 | cpus_read_lock(); |
| 2472 | ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false); |
| 2473 | cpus_read_unlock(); |
| 2474 | |
| 2475 | return ret; |
| 2476 | } |
| 2477 | EXPORT_SYMBOL(netif_set_xps_queue); |
| 2478 | |
| 2479 | #endif |
| 2480 | static void netdev_unbind_all_sb_channels(struct net_device *dev) |
| 2481 | { |
| 2482 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; |
| 2483 | |
| 2484 | /* Unbind any subordinate channels */ |
| 2485 | while (txq-- != &dev->_tx[0]) { |
| 2486 | if (txq->sb_dev) |
| 2487 | netdev_unbind_sb_channel(dev, txq->sb_dev); |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | void netdev_reset_tc(struct net_device *dev) |
| 2492 | { |
| 2493 | #ifdef CONFIG_XPS |
| 2494 | netif_reset_xps_queues_gt(dev, 0); |
| 2495 | #endif |
| 2496 | netdev_unbind_all_sb_channels(dev); |
| 2497 | |
| 2498 | /* Reset TC configuration of device */ |
| 2499 | dev->num_tc = 0; |
| 2500 | memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq)); |
| 2501 | memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map)); |
| 2502 | } |
| 2503 | EXPORT_SYMBOL(netdev_reset_tc); |
| 2504 | |
| 2505 | int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset) |
| 2506 | { |
| 2507 | if (tc >= dev->num_tc) |
| 2508 | return -EINVAL; |
| 2509 | |
| 2510 | #ifdef CONFIG_XPS |
| 2511 | netif_reset_xps_queues(dev, offset, count); |
| 2512 | #endif |
| 2513 | dev->tc_to_txq[tc].count = count; |
| 2514 | dev->tc_to_txq[tc].offset = offset; |
| 2515 | return 0; |
| 2516 | } |
| 2517 | EXPORT_SYMBOL(netdev_set_tc_queue); |
| 2518 | |
| 2519 | int netdev_set_num_tc(struct net_device *dev, u8 num_tc) |
| 2520 | { |
| 2521 | if (num_tc > TC_MAX_QUEUE) |
| 2522 | return -EINVAL; |
| 2523 | |
| 2524 | #ifdef CONFIG_XPS |
| 2525 | netif_reset_xps_queues_gt(dev, 0); |
| 2526 | #endif |
| 2527 | netdev_unbind_all_sb_channels(dev); |
| 2528 | |
| 2529 | dev->num_tc = num_tc; |
| 2530 | return 0; |
| 2531 | } |
| 2532 | EXPORT_SYMBOL(netdev_set_num_tc); |
| 2533 | |
| 2534 | void netdev_unbind_sb_channel(struct net_device *dev, |
| 2535 | struct net_device *sb_dev) |
| 2536 | { |
| 2537 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; |
| 2538 | |
| 2539 | #ifdef CONFIG_XPS |
| 2540 | netif_reset_xps_queues_gt(sb_dev, 0); |
| 2541 | #endif |
| 2542 | memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq)); |
| 2543 | memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map)); |
| 2544 | |
| 2545 | while (txq-- != &dev->_tx[0]) { |
| 2546 | if (txq->sb_dev == sb_dev) |
| 2547 | txq->sb_dev = NULL; |
| 2548 | } |
| 2549 | } |
| 2550 | EXPORT_SYMBOL(netdev_unbind_sb_channel); |
| 2551 | |
| 2552 | int netdev_bind_sb_channel_queue(struct net_device *dev, |
| 2553 | struct net_device *sb_dev, |
| 2554 | u8 tc, u16 count, u16 offset) |
| 2555 | { |
| 2556 | /* Make certain the sb_dev and dev are already configured */ |
| 2557 | if (sb_dev->num_tc >= 0 || tc >= dev->num_tc) |
| 2558 | return -EINVAL; |
| 2559 | |
| 2560 | /* We cannot hand out queues we don't have */ |
| 2561 | if ((offset + count) > dev->real_num_tx_queues) |
| 2562 | return -EINVAL; |
| 2563 | |
| 2564 | /* Record the mapping */ |
| 2565 | sb_dev->tc_to_txq[tc].count = count; |
| 2566 | sb_dev->tc_to_txq[tc].offset = offset; |
| 2567 | |
| 2568 | /* Provide a way for Tx queue to find the tc_to_txq map or |
| 2569 | * XPS map for itself. |
| 2570 | */ |
| 2571 | while (count--) |
| 2572 | netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev; |
| 2573 | |
| 2574 | return 0; |
| 2575 | } |
| 2576 | EXPORT_SYMBOL(netdev_bind_sb_channel_queue); |
| 2577 | |
| 2578 | int netdev_set_sb_channel(struct net_device *dev, u16 channel) |
| 2579 | { |
| 2580 | /* Do not use a multiqueue device to represent a subordinate channel */ |
| 2581 | if (netif_is_multiqueue(dev)) |
| 2582 | return -ENODEV; |
| 2583 | |
| 2584 | /* We allow channels 1 - 32767 to be used for subordinate channels. |
| 2585 | * Channel 0 is meant to be "native" mode and used only to represent |
| 2586 | * the main root device. We allow writing 0 to reset the device back |
| 2587 | * to normal mode after being used as a subordinate channel. |
| 2588 | */ |
| 2589 | if (channel > S16_MAX) |
| 2590 | return -EINVAL; |
| 2591 | |
| 2592 | dev->num_tc = -channel; |
| 2593 | |
| 2594 | return 0; |
| 2595 | } |
| 2596 | EXPORT_SYMBOL(netdev_set_sb_channel); |
| 2597 | |
| 2598 | /* |
| 2599 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues |
| 2600 | * greater than real_num_tx_queues stale skbs on the qdisc must be flushed. |
| 2601 | */ |
| 2602 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
| 2603 | { |
| 2604 | bool disabling; |
| 2605 | int rc; |
| 2606 | |
| 2607 | disabling = txq < dev->real_num_tx_queues; |
| 2608 | |
| 2609 | if (txq < 1 || txq > dev->num_tx_queues) |
| 2610 | return -EINVAL; |
| 2611 | |
| 2612 | if (dev->reg_state == NETREG_REGISTERED || |
| 2613 | dev->reg_state == NETREG_UNREGISTERING) { |
| 2614 | ASSERT_RTNL(); |
| 2615 | |
| 2616 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
| 2617 | txq); |
| 2618 | if (rc) |
| 2619 | return rc; |
| 2620 | |
| 2621 | if (dev->num_tc) |
| 2622 | netif_setup_tc(dev, txq); |
| 2623 | |
| 2624 | dev_qdisc_change_real_num_tx(dev, txq); |
| 2625 | |
| 2626 | dev->real_num_tx_queues = txq; |
| 2627 | |
| 2628 | if (disabling) { |
| 2629 | synchronize_net(); |
| 2630 | qdisc_reset_all_tx_gt(dev, txq); |
| 2631 | #ifdef CONFIG_XPS |
| 2632 | netif_reset_xps_queues_gt(dev, txq); |
| 2633 | #endif |
| 2634 | } |
| 2635 | } else { |
| 2636 | dev->real_num_tx_queues = txq; |
| 2637 | } |
| 2638 | |
| 2639 | return 0; |
| 2640 | } |
| 2641 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); |
| 2642 | |
| 2643 | #ifdef CONFIG_SYSFS |
| 2644 | /** |
| 2645 | * netif_set_real_num_rx_queues - set actual number of RX queues used |
| 2646 | * @dev: Network device |
| 2647 | * @rxq: Actual number of RX queues |
| 2648 | * |
| 2649 | * This must be called either with the rtnl_lock held or before |
| 2650 | * registration of the net device. Returns 0 on success, or a |
| 2651 | * negative error code. If called before registration, it always |
| 2652 | * succeeds. |
| 2653 | */ |
| 2654 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) |
| 2655 | { |
| 2656 | int rc; |
| 2657 | |
| 2658 | if (rxq < 1 || rxq > dev->num_rx_queues) |
| 2659 | return -EINVAL; |
| 2660 | |
| 2661 | if (dev->reg_state == NETREG_REGISTERED) { |
| 2662 | ASSERT_RTNL(); |
| 2663 | |
| 2664 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
| 2665 | rxq); |
| 2666 | if (rc) |
| 2667 | return rc; |
| 2668 | } |
| 2669 | |
| 2670 | dev->real_num_rx_queues = rxq; |
| 2671 | return 0; |
| 2672 | } |
| 2673 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); |
| 2674 | #endif |
| 2675 | |
| 2676 | /** |
| 2677 | * netif_get_num_default_rss_queues - default number of RSS queues |
| 2678 | * |
| 2679 | * This routine should set an upper limit on the number of RSS queues |
| 2680 | * used by default by multiqueue devices. |
| 2681 | */ |
| 2682 | int netif_get_num_default_rss_queues(void) |
| 2683 | { |
| 2684 | return is_kdump_kernel() ? |
| 2685 | 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus()); |
| 2686 | } |
| 2687 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); |
| 2688 | |
| 2689 | static void __netif_reschedule(struct Qdisc *q) |
| 2690 | { |
| 2691 | struct softnet_data *sd; |
| 2692 | unsigned long flags; |
| 2693 | |
| 2694 | local_irq_save(flags); |
| 2695 | sd = this_cpu_ptr(&softnet_data); |
| 2696 | q->next_sched = NULL; |
| 2697 | *sd->output_queue_tailp = q; |
| 2698 | sd->output_queue_tailp = &q->next_sched; |
| 2699 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
| 2700 | local_irq_restore(flags); |
| 2701 | } |
| 2702 | |
| 2703 | void __netif_schedule(struct Qdisc *q) |
| 2704 | { |
| 2705 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) |
| 2706 | __netif_reschedule(q); |
| 2707 | } |
| 2708 | EXPORT_SYMBOL(__netif_schedule); |
| 2709 | |
| 2710 | struct dev_kfree_skb_cb { |
| 2711 | enum skb_free_reason reason; |
| 2712 | }; |
| 2713 | |
| 2714 | static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb) |
| 2715 | { |
| 2716 | return (struct dev_kfree_skb_cb *)skb->cb; |
| 2717 | } |
| 2718 | |
| 2719 | void netif_schedule_queue(struct netdev_queue *txq) |
| 2720 | { |
| 2721 | rcu_read_lock(); |
| 2722 | if (!(txq->state & QUEUE_STATE_ANY_XOFF)) { |
| 2723 | struct Qdisc *q = rcu_dereference(txq->qdisc); |
| 2724 | |
| 2725 | __netif_schedule(q); |
| 2726 | } |
| 2727 | rcu_read_unlock(); |
| 2728 | } |
| 2729 | EXPORT_SYMBOL(netif_schedule_queue); |
| 2730 | |
| 2731 | void netif_tx_wake_queue(struct netdev_queue *dev_queue) |
| 2732 | { |
| 2733 | if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { |
| 2734 | struct Qdisc *q; |
| 2735 | |
| 2736 | rcu_read_lock(); |
| 2737 | q = rcu_dereference(dev_queue->qdisc); |
| 2738 | __netif_schedule(q); |
| 2739 | rcu_read_unlock(); |
| 2740 | } |
| 2741 | } |
| 2742 | EXPORT_SYMBOL(netif_tx_wake_queue); |
| 2743 | |
| 2744 | void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason) |
| 2745 | { |
| 2746 | unsigned long flags; |
| 2747 | |
| 2748 | if (unlikely(!skb)) |
| 2749 | return; |
| 2750 | |
| 2751 | if (likely(refcount_read(&skb->users) == 1)) { |
| 2752 | smp_rmb(); |
| 2753 | refcount_set(&skb->users, 0); |
| 2754 | } else if (likely(!refcount_dec_and_test(&skb->users))) { |
| 2755 | return; |
| 2756 | } |
| 2757 | get_kfree_skb_cb(skb)->reason = reason; |
| 2758 | local_irq_save(flags); |
| 2759 | skb->next = __this_cpu_read(softnet_data.completion_queue); |
| 2760 | __this_cpu_write(softnet_data.completion_queue, skb); |
| 2761 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
| 2762 | local_irq_restore(flags); |
| 2763 | } |
| 2764 | EXPORT_SYMBOL(__dev_kfree_skb_irq); |
| 2765 | |
| 2766 | void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason) |
| 2767 | { |
| 2768 | if (in_irq() || irqs_disabled()) |
| 2769 | __dev_kfree_skb_irq(skb, reason); |
| 2770 | else if (unlikely(reason == SKB_REASON_DROPPED)) |
| 2771 | kfree_skb(skb); |
| 2772 | else |
| 2773 | consume_skb(skb); |
| 2774 | } |
| 2775 | EXPORT_SYMBOL(__dev_kfree_skb_any); |
| 2776 | |
| 2777 | |
| 2778 | /** |
| 2779 | * netif_device_detach - mark device as removed |
| 2780 | * @dev: network device |
| 2781 | * |
| 2782 | * Mark device as removed from system and therefore no longer available. |
| 2783 | */ |
| 2784 | void netif_device_detach(struct net_device *dev) |
| 2785 | { |
| 2786 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && |
| 2787 | netif_running(dev)) { |
| 2788 | netif_tx_stop_all_queues(dev); |
| 2789 | } |
| 2790 | } |
| 2791 | EXPORT_SYMBOL(netif_device_detach); |
| 2792 | |
| 2793 | /** |
| 2794 | * netif_device_attach - mark device as attached |
| 2795 | * @dev: network device |
| 2796 | * |
| 2797 | * Mark device as attached from system and restart if needed. |
| 2798 | */ |
| 2799 | void netif_device_attach(struct net_device *dev) |
| 2800 | { |
| 2801 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && |
| 2802 | netif_running(dev)) { |
| 2803 | netif_tx_wake_all_queues(dev); |
| 2804 | __netdev_watchdog_up(dev); |
| 2805 | } |
| 2806 | } |
| 2807 | EXPORT_SYMBOL(netif_device_attach); |
| 2808 | |
| 2809 | /* |
| 2810 | * Returns a Tx hash based on the given packet descriptor a Tx queues' number |
| 2811 | * to be used as a distribution range. |
| 2812 | */ |
| 2813 | static u16 skb_tx_hash(const struct net_device *dev, |
| 2814 | const struct net_device *sb_dev, |
| 2815 | struct sk_buff *skb) |
| 2816 | { |
| 2817 | u32 hash; |
| 2818 | u16 qoffset = 0; |
| 2819 | u16 qcount = dev->real_num_tx_queues; |
| 2820 | |
| 2821 | if (dev->num_tc) { |
| 2822 | u8 tc = netdev_get_prio_tc_map(dev, skb->priority); |
| 2823 | |
| 2824 | qoffset = sb_dev->tc_to_txq[tc].offset; |
| 2825 | qcount = sb_dev->tc_to_txq[tc].count; |
| 2826 | if (unlikely(!qcount)) { |
| 2827 | net_warn_ratelimited("%s: invalid qcount, qoffset %u for tc %u\n", |
| 2828 | sb_dev->name, qoffset, tc); |
| 2829 | qoffset = 0; |
| 2830 | qcount = dev->real_num_tx_queues; |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | if (skb_rx_queue_recorded(skb)) { |
| 2835 | hash = skb_get_rx_queue(skb); |
| 2836 | if (hash >= qoffset) |
| 2837 | hash -= qoffset; |
| 2838 | while (unlikely(hash >= qcount)) |
| 2839 | hash -= qcount; |
| 2840 | return hash + qoffset; |
| 2841 | } |
| 2842 | |
| 2843 | return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset; |
| 2844 | } |
| 2845 | |
| 2846 | static void skb_warn_bad_offload(const struct sk_buff *skb) |
| 2847 | { |
| 2848 | static const netdev_features_t null_features; |
| 2849 | struct net_device *dev = skb->dev; |
| 2850 | const char *name = ""; |
| 2851 | |
| 2852 | if (!net_ratelimit()) |
| 2853 | return; |
| 2854 | |
| 2855 | if (dev) { |
| 2856 | if (dev->dev.parent) |
| 2857 | name = dev_driver_string(dev->dev.parent); |
| 2858 | else |
| 2859 | name = netdev_name(dev); |
| 2860 | } |
| 2861 | skb_dump(KERN_WARNING, skb, false); |
| 2862 | WARN(1, "%s: caps=(%pNF, %pNF)\n", |
| 2863 | name, dev ? &dev->features : &null_features, |
| 2864 | skb->sk ? &skb->sk->sk_route_caps : &null_features); |
| 2865 | } |
| 2866 | |
| 2867 | /* |
| 2868 | * Invalidate hardware checksum when packet is to be mangled, and |
| 2869 | * complete checksum manually on outgoing path. |
| 2870 | */ |
| 2871 | int skb_checksum_help(struct sk_buff *skb) |
| 2872 | { |
| 2873 | __wsum csum; |
| 2874 | int ret = 0, offset; |
| 2875 | |
| 2876 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 2877 | goto out_set_summed; |
| 2878 | |
| 2879 | if (unlikely(skb_shinfo(skb)->gso_size)) { |
| 2880 | skb_warn_bad_offload(skb); |
| 2881 | return -EINVAL; |
| 2882 | } |
| 2883 | |
| 2884 | /* Before computing a checksum, we should make sure no frag could |
| 2885 | * be modified by an external entity : checksum could be wrong. |
| 2886 | */ |
| 2887 | if (skb_has_shared_frag(skb)) { |
| 2888 | ret = __skb_linearize(skb); |
| 2889 | if (ret) |
| 2890 | goto out; |
| 2891 | } |
| 2892 | |
| 2893 | offset = skb_checksum_start_offset(skb); |
| 2894 | BUG_ON(offset >= skb_headlen(skb)); |
| 2895 | csum = skb_checksum(skb, offset, skb->len - offset, 0); |
| 2896 | |
| 2897 | offset += skb->csum_offset; |
| 2898 | BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb)); |
| 2899 | |
| 2900 | if (skb_cloned(skb) && |
| 2901 | !skb_clone_writable(skb, offset + sizeof(__sum16))) { |
| 2902 | ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 2903 | if (ret) |
| 2904 | goto out; |
| 2905 | } |
| 2906 | |
| 2907 | *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0; |
| 2908 | out_set_summed: |
| 2909 | skb->ip_summed = CHECKSUM_NONE; |
| 2910 | out: |
| 2911 | return ret; |
| 2912 | } |
| 2913 | EXPORT_SYMBOL(skb_checksum_help); |
| 2914 | |
| 2915 | int skb_crc32c_csum_help(struct sk_buff *skb) |
| 2916 | { |
| 2917 | __le32 crc32c_csum; |
| 2918 | int ret = 0, offset, start; |
| 2919 | |
| 2920 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 2921 | goto out; |
| 2922 | |
| 2923 | if (unlikely(skb_is_gso(skb))) |
| 2924 | goto out; |
| 2925 | |
| 2926 | /* Before computing a checksum, we should make sure no frag could |
| 2927 | * be modified by an external entity : checksum could be wrong. |
| 2928 | */ |
| 2929 | if (unlikely(skb_has_shared_frag(skb))) { |
| 2930 | ret = __skb_linearize(skb); |
| 2931 | if (ret) |
| 2932 | goto out; |
| 2933 | } |
| 2934 | start = skb_checksum_start_offset(skb); |
| 2935 | offset = start + offsetof(struct sctphdr, checksum); |
| 2936 | if (WARN_ON_ONCE(offset >= skb_headlen(skb))) { |
| 2937 | ret = -EINVAL; |
| 2938 | goto out; |
| 2939 | } |
| 2940 | if (skb_cloned(skb) && |
| 2941 | !skb_clone_writable(skb, offset + sizeof(__le32))) { |
| 2942 | ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 2943 | if (ret) |
| 2944 | goto out; |
| 2945 | } |
| 2946 | crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start, |
| 2947 | skb->len - start, ~(__u32)0, |
| 2948 | crc32c_csum_stub)); |
| 2949 | *(__le32 *)(skb->data + offset) = crc32c_csum; |
| 2950 | skb->ip_summed = CHECKSUM_NONE; |
| 2951 | skb->csum_not_inet = 0; |
| 2952 | out: |
| 2953 | return ret; |
| 2954 | } |
| 2955 | |
| 2956 | __be16 skb_network_protocol(struct sk_buff *skb, int *depth) |
| 2957 | { |
| 2958 | __be16 type = skb->protocol; |
| 2959 | |
| 2960 | /* Tunnel gso handlers can set protocol to ethernet. */ |
| 2961 | if (type == htons(ETH_P_TEB)) { |
| 2962 | struct ethhdr *eth; |
| 2963 | |
| 2964 | if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) |
| 2965 | return 0; |
| 2966 | |
| 2967 | eth = (struct ethhdr *)skb->data; |
| 2968 | type = eth->h_proto; |
| 2969 | } |
| 2970 | |
| 2971 | return vlan_get_protocol_and_depth(skb, type, depth); |
| 2972 | } |
| 2973 | |
| 2974 | /** |
| 2975 | * skb_mac_gso_segment - mac layer segmentation handler. |
| 2976 | * @skb: buffer to segment |
| 2977 | * @features: features for the output path (see dev->features) |
| 2978 | */ |
| 2979 | struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb, |
| 2980 | netdev_features_t features) |
| 2981 | { |
| 2982 | struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT); |
| 2983 | struct packet_offload *ptype; |
| 2984 | int vlan_depth = skb->mac_len; |
| 2985 | __be16 type = skb_network_protocol(skb, &vlan_depth); |
| 2986 | |
| 2987 | if (unlikely(!type)) |
| 2988 | return ERR_PTR(-EINVAL); |
| 2989 | |
| 2990 | __skb_pull(skb, vlan_depth); |
| 2991 | |
| 2992 | rcu_read_lock(); |
| 2993 | list_for_each_entry_rcu(ptype, &offload_base, list) { |
| 2994 | if (ptype->type == type && ptype->callbacks.gso_segment) { |
| 2995 | segs = ptype->callbacks.gso_segment(skb, features); |
| 2996 | break; |
| 2997 | } |
| 2998 | } |
| 2999 | rcu_read_unlock(); |
| 3000 | |
| 3001 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
| 3002 | |
| 3003 | return segs; |
| 3004 | } |
| 3005 | EXPORT_SYMBOL(skb_mac_gso_segment); |
| 3006 | |
| 3007 | |
| 3008 | /* openvswitch calls this on rx path, so we need a different check. |
| 3009 | */ |
| 3010 | static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path) |
| 3011 | { |
| 3012 | if (tx_path) |
| 3013 | return skb->ip_summed != CHECKSUM_PARTIAL && |
| 3014 | skb->ip_summed != CHECKSUM_UNNECESSARY; |
| 3015 | |
| 3016 | return skb->ip_summed == CHECKSUM_NONE; |
| 3017 | } |
| 3018 | |
| 3019 | /** |
| 3020 | * __skb_gso_segment - Perform segmentation on skb. |
| 3021 | * @skb: buffer to segment |
| 3022 | * @features: features for the output path (see dev->features) |
| 3023 | * @tx_path: whether it is called in TX path |
| 3024 | * |
| 3025 | * This function segments the given skb and returns a list of segments. |
| 3026 | * |
| 3027 | * It may return NULL if the skb requires no segmentation. This is |
| 3028 | * only possible when GSO is used for verifying header integrity. |
| 3029 | * |
| 3030 | * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb. |
| 3031 | */ |
| 3032 | struct sk_buff *__skb_gso_segment(struct sk_buff *skb, |
| 3033 | netdev_features_t features, bool tx_path) |
| 3034 | { |
| 3035 | struct sk_buff *segs; |
| 3036 | |
| 3037 | if (unlikely(skb_needs_check(skb, tx_path))) { |
| 3038 | int err; |
| 3039 | |
| 3040 | /* We're going to init ->check field in TCP or UDP header */ |
| 3041 | err = skb_cow_head(skb, 0); |
| 3042 | if (err < 0) |
| 3043 | return ERR_PTR(err); |
| 3044 | } |
| 3045 | |
| 3046 | /* Only report GSO partial support if it will enable us to |
| 3047 | * support segmentation on this frame without needing additional |
| 3048 | * work. |
| 3049 | */ |
| 3050 | if (features & NETIF_F_GSO_PARTIAL) { |
| 3051 | netdev_features_t partial_features = NETIF_F_GSO_ROBUST; |
| 3052 | struct net_device *dev = skb->dev; |
| 3053 | |
| 3054 | partial_features |= dev->features & dev->gso_partial_features; |
| 3055 | if (!skb_gso_ok(skb, features | partial_features)) |
| 3056 | features &= ~NETIF_F_GSO_PARTIAL; |
| 3057 | } |
| 3058 | |
| 3059 | BUILD_BUG_ON(SKB_SGO_CB_OFFSET + |
| 3060 | sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb)); |
| 3061 | |
| 3062 | SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); |
| 3063 | SKB_GSO_CB(skb)->encap_level = 0; |
| 3064 | |
| 3065 | skb_reset_mac_header(skb); |
| 3066 | skb_reset_mac_len(skb); |
| 3067 | |
| 3068 | segs = skb_mac_gso_segment(skb, features); |
| 3069 | |
| 3070 | if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs))) |
| 3071 | skb_warn_bad_offload(skb); |
| 3072 | |
| 3073 | return segs; |
| 3074 | } |
| 3075 | EXPORT_SYMBOL(__skb_gso_segment); |
| 3076 | |
| 3077 | /* Take action when hardware reception checksum errors are detected. */ |
| 3078 | #ifdef CONFIG_BUG |
| 3079 | void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
| 3080 | { |
| 3081 | if (net_ratelimit()) { |
| 3082 | pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>"); |
| 3083 | skb_dump(KERN_ERR, skb, true); |
| 3084 | dump_stack(); |
| 3085 | } |
| 3086 | } |
| 3087 | EXPORT_SYMBOL(netdev_rx_csum_fault); |
| 3088 | #endif |
| 3089 | |
| 3090 | /* XXX: check that highmem exists at all on the given machine. */ |
| 3091 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
| 3092 | { |
| 3093 | #ifdef CONFIG_HIGHMEM |
| 3094 | int i; |
| 3095 | |
| 3096 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
| 3097 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 3098 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 3099 | |
| 3100 | if (PageHighMem(skb_frag_page(frag))) |
| 3101 | return 1; |
| 3102 | } |
| 3103 | } |
| 3104 | #endif |
| 3105 | return 0; |
| 3106 | } |
| 3107 | |
| 3108 | /* If MPLS offload request, verify we are testing hardware MPLS features |
| 3109 | * instead of standard features for the netdev. |
| 3110 | */ |
| 3111 | #if IS_ENABLED(CONFIG_NET_MPLS_GSO) |
| 3112 | static netdev_features_t net_mpls_features(struct sk_buff *skb, |
| 3113 | netdev_features_t features, |
| 3114 | __be16 type) |
| 3115 | { |
| 3116 | if (eth_p_mpls(type)) |
| 3117 | features &= skb->dev->mpls_features; |
| 3118 | |
| 3119 | return features; |
| 3120 | } |
| 3121 | #else |
| 3122 | static netdev_features_t net_mpls_features(struct sk_buff *skb, |
| 3123 | netdev_features_t features, |
| 3124 | __be16 type) |
| 3125 | { |
| 3126 | return features; |
| 3127 | } |
| 3128 | #endif |
| 3129 | |
| 3130 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
| 3131 | netdev_features_t features) |
| 3132 | { |
| 3133 | int tmp; |
| 3134 | __be16 type; |
| 3135 | |
| 3136 | type = skb_network_protocol(skb, &tmp); |
| 3137 | features = net_mpls_features(skb, features, type); |
| 3138 | |
| 3139 | if (skb->ip_summed != CHECKSUM_NONE && |
| 3140 | !can_checksum_protocol(features, type)) { |
| 3141 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
| 3142 | } |
| 3143 | if (illegal_highdma(skb->dev, skb)) |
| 3144 | features &= ~NETIF_F_SG; |
| 3145 | |
| 3146 | return features; |
| 3147 | } |
| 3148 | |
| 3149 | netdev_features_t passthru_features_check(struct sk_buff *skb, |
| 3150 | struct net_device *dev, |
| 3151 | netdev_features_t features) |
| 3152 | { |
| 3153 | return features; |
| 3154 | } |
| 3155 | EXPORT_SYMBOL(passthru_features_check); |
| 3156 | |
| 3157 | static netdev_features_t dflt_features_check(struct sk_buff *skb, |
| 3158 | struct net_device *dev, |
| 3159 | netdev_features_t features) |
| 3160 | { |
| 3161 | return vlan_features_check(skb, features); |
| 3162 | } |
| 3163 | |
| 3164 | static netdev_features_t gso_features_check(const struct sk_buff *skb, |
| 3165 | struct net_device *dev, |
| 3166 | netdev_features_t features) |
| 3167 | { |
| 3168 | u16 gso_segs = skb_shinfo(skb)->gso_segs; |
| 3169 | |
| 3170 | if (gso_segs > dev->gso_max_segs) |
| 3171 | return features & ~NETIF_F_GSO_MASK; |
| 3172 | |
| 3173 | if (unlikely(skb->len >= READ_ONCE(dev->gso_max_size))) |
| 3174 | return features & ~NETIF_F_GSO_MASK; |
| 3175 | |
| 3176 | if (!skb_shinfo(skb)->gso_type) { |
| 3177 | skb_warn_bad_offload(skb); |
| 3178 | return features & ~NETIF_F_GSO_MASK; |
| 3179 | } |
| 3180 | |
| 3181 | /* Support for GSO partial features requires software |
| 3182 | * intervention before we can actually process the packets |
| 3183 | * so we need to strip support for any partial features now |
| 3184 | * and we can pull them back in after we have partially |
| 3185 | * segmented the frame. |
| 3186 | */ |
| 3187 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)) |
| 3188 | features &= ~dev->gso_partial_features; |
| 3189 | |
| 3190 | /* Make sure to clear the IPv4 ID mangling feature if the |
| 3191 | * IPv4 header has the potential to be fragmented. |
| 3192 | */ |
| 3193 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { |
| 3194 | struct iphdr *iph = skb->encapsulation ? |
| 3195 | inner_ip_hdr(skb) : ip_hdr(skb); |
| 3196 | |
| 3197 | if (!(iph->frag_off & htons(IP_DF))) |
| 3198 | features &= ~NETIF_F_TSO_MANGLEID; |
| 3199 | } |
| 3200 | |
| 3201 | return features; |
| 3202 | } |
| 3203 | |
| 3204 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
| 3205 | { |
| 3206 | struct net_device *dev = skb->dev; |
| 3207 | netdev_features_t features = dev->features; |
| 3208 | |
| 3209 | if (skb_is_gso(skb)) |
| 3210 | features = gso_features_check(skb, dev, features); |
| 3211 | |
| 3212 | /* If encapsulation offload request, verify we are testing |
| 3213 | * hardware encapsulation features instead of standard |
| 3214 | * features for the netdev |
| 3215 | */ |
| 3216 | if (skb->encapsulation) |
| 3217 | features &= dev->hw_enc_features; |
| 3218 | |
| 3219 | if (skb_vlan_tagged(skb)) |
| 3220 | features = netdev_intersect_features(features, |
| 3221 | dev->vlan_features | |
| 3222 | NETIF_F_HW_VLAN_CTAG_TX | |
| 3223 | NETIF_F_HW_VLAN_STAG_TX); |
| 3224 | |
| 3225 | if (dev->netdev_ops->ndo_features_check) |
| 3226 | features &= dev->netdev_ops->ndo_features_check(skb, dev, |
| 3227 | features); |
| 3228 | else |
| 3229 | features &= dflt_features_check(skb, dev, features); |
| 3230 | |
| 3231 | return harmonize_features(skb, features); |
| 3232 | } |
| 3233 | EXPORT_SYMBOL(netif_skb_features); |
| 3234 | |
| 3235 | int ptype_all_skb_clone __read_mostly = 1; |
| 3236 | |
| 3237 | static int xmit_one(struct sk_buff *skb, struct net_device *dev, |
| 3238 | struct netdev_queue *txq, bool more) |
| 3239 | { |
| 3240 | unsigned int len; |
| 3241 | int rc; |
| 3242 | |
| 3243 | if ((ptype_all_skb_clone == 1) && (dev_nit_active(dev))) |
| 3244 | dev_queue_xmit_nit(skb, dev); |
| 3245 | |
| 3246 | #ifdef CONFIG_ETHERNET_PACKET_MANGLE |
| 3247 | if (!dev->eth_mangle_tx || |
| 3248 | (skb = dev->eth_mangle_tx(dev, skb)) != NULL) |
| 3249 | #else |
| 3250 | if (1) |
| 3251 | #endif |
| 3252 | { |
| 3253 | len = skb->len; |
| 3254 | trace_net_dev_start_xmit(skb, dev); |
| 3255 | rc = netdev_start_xmit(skb, dev, txq, more); |
| 3256 | trace_net_dev_xmit(skb, rc, dev, len); |
| 3257 | } else { |
| 3258 | rc = NETDEV_TX_OK; |
| 3259 | } |
| 3260 | |
| 3261 | return rc; |
| 3262 | } |
| 3263 | |
| 3264 | struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev, |
| 3265 | struct netdev_queue *txq, int *ret) |
| 3266 | { |
| 3267 | struct sk_buff *skb = first; |
| 3268 | int rc = NETDEV_TX_OK; |
| 3269 | |
| 3270 | while (skb) { |
| 3271 | struct sk_buff *next = skb->next; |
| 3272 | |
| 3273 | skb_mark_not_on_list(skb); |
| 3274 | rc = xmit_one(skb, dev, txq, next != NULL); |
| 3275 | if (unlikely(!dev_xmit_complete(rc))) { |
| 3276 | skb->next = next; |
| 3277 | goto out; |
| 3278 | } |
| 3279 | |
| 3280 | skb = next; |
| 3281 | if (netif_tx_queue_stopped(txq) && skb) { |
| 3282 | rc = NETDEV_TX_BUSY; |
| 3283 | break; |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | out: |
| 3288 | *ret = rc; |
| 3289 | return skb; |
| 3290 | } |
| 3291 | |
| 3292 | static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb, |
| 3293 | netdev_features_t features) |
| 3294 | { |
| 3295 | if (skb_vlan_tag_present(skb) && |
| 3296 | !vlan_hw_offload_capable(features, skb->vlan_proto)) |
| 3297 | skb = __vlan_hwaccel_push_inside(skb); |
| 3298 | return skb; |
| 3299 | } |
| 3300 | |
| 3301 | int skb_csum_hwoffload_help(struct sk_buff *skb, |
| 3302 | const netdev_features_t features) |
| 3303 | { |
| 3304 | if (unlikely(skb->csum_not_inet)) |
| 3305 | return !!(features & NETIF_F_SCTP_CRC) ? 0 : |
| 3306 | skb_crc32c_csum_help(skb); |
| 3307 | |
| 3308 | if (features & NETIF_F_HW_CSUM) |
| 3309 | return 0; |
| 3310 | |
| 3311 | if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) { |
| 3312 | if (vlan_get_protocol(skb) == htons(ETH_P_IPV6) && |
| 3313 | skb_network_header_len(skb) != sizeof(struct ipv6hdr)) |
| 3314 | goto sw_checksum; |
| 3315 | switch (skb->csum_offset) { |
| 3316 | case offsetof(struct tcphdr, check): |
| 3317 | case offsetof(struct udphdr, check): |
| 3318 | return 0; |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | sw_checksum: |
| 3323 | return skb_checksum_help(skb); |
| 3324 | } |
| 3325 | EXPORT_SYMBOL(skb_csum_hwoffload_help); |
| 3326 | |
| 3327 | static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again) |
| 3328 | { |
| 3329 | netdev_features_t features; |
| 3330 | |
| 3331 | features = netif_skb_features(skb); |
| 3332 | skb = validate_xmit_vlan(skb, features); |
| 3333 | if (unlikely(!skb)) |
| 3334 | goto out_null; |
| 3335 | |
| 3336 | skb = sk_validate_xmit_skb(skb, dev); |
| 3337 | if (unlikely(!skb)) |
| 3338 | goto out_null; |
| 3339 | |
| 3340 | if (netif_needs_gso(skb, features)) { |
| 3341 | struct sk_buff *segs; |
| 3342 | |
| 3343 | segs = skb_gso_segment(skb, features); |
| 3344 | if (IS_ERR(segs)) { |
| 3345 | goto out_kfree_skb; |
| 3346 | } else if (segs) { |
| 3347 | consume_skb(skb); |
| 3348 | skb = segs; |
| 3349 | } |
| 3350 | } else { |
| 3351 | if (skb_needs_linearize(skb, features) && |
| 3352 | __skb_linearize(skb)) |
| 3353 | goto out_kfree_skb; |
| 3354 | |
| 3355 | /* If packet is not checksummed and device does not |
| 3356 | * support checksumming for this protocol, complete |
| 3357 | * checksumming here. |
| 3358 | */ |
| 3359 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 3360 | if (skb->encapsulation) |
| 3361 | skb_set_inner_transport_header(skb, |
| 3362 | skb_checksum_start_offset(skb)); |
| 3363 | else |
| 3364 | skb_set_transport_header(skb, |
| 3365 | skb_checksum_start_offset(skb)); |
| 3366 | if (skb_csum_hwoffload_help(skb, features)) |
| 3367 | goto out_kfree_skb; |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | skb = validate_xmit_xfrm(skb, features, again); |
| 3372 | |
| 3373 | return skb; |
| 3374 | |
| 3375 | out_kfree_skb: |
| 3376 | kfree_skb(skb); |
| 3377 | out_null: |
| 3378 | atomic_long_inc(&dev->tx_dropped); |
| 3379 | return NULL; |
| 3380 | } |
| 3381 | |
| 3382 | struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again) |
| 3383 | { |
| 3384 | struct sk_buff *next, *head = NULL, *tail; |
| 3385 | |
| 3386 | for (; skb != NULL; skb = next) { |
| 3387 | next = skb->next; |
| 3388 | skb_mark_not_on_list(skb); |
| 3389 | |
| 3390 | /* in case skb wont be segmented, point to itself */ |
| 3391 | skb->prev = skb; |
| 3392 | |
| 3393 | skb = validate_xmit_skb(skb, dev, again); |
| 3394 | if (!skb) |
| 3395 | continue; |
| 3396 | |
| 3397 | if (!head) |
| 3398 | head = skb; |
| 3399 | else |
| 3400 | tail->next = skb; |
| 3401 | /* If skb was segmented, skb->prev points to |
| 3402 | * the last segment. If not, it still contains skb. |
| 3403 | */ |
| 3404 | tail = skb->prev; |
| 3405 | } |
| 3406 | return head; |
| 3407 | } |
| 3408 | EXPORT_SYMBOL_GPL(validate_xmit_skb_list); |
| 3409 | |
| 3410 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
| 3411 | { |
| 3412 | const struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 3413 | |
| 3414 | qdisc_skb_cb(skb)->pkt_len = skb->len; |
| 3415 | |
| 3416 | /* To get more precise estimation of bytes sent on wire, |
| 3417 | * we add to pkt_len the headers size of all segments |
| 3418 | */ |
| 3419 | if (shinfo->gso_size && skb_transport_header_was_set(skb)) { |
| 3420 | unsigned int hdr_len; |
| 3421 | u16 gso_segs = shinfo->gso_segs; |
| 3422 | |
| 3423 | /* mac layer + network layer */ |
| 3424 | hdr_len = skb_transport_header(skb) - skb_mac_header(skb); |
| 3425 | |
| 3426 | /* + transport layer */ |
| 3427 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
| 3428 | const struct tcphdr *th; |
| 3429 | struct tcphdr _tcphdr; |
| 3430 | |
| 3431 | th = skb_header_pointer(skb, skb_transport_offset(skb), |
| 3432 | sizeof(_tcphdr), &_tcphdr); |
| 3433 | if (likely(th)) |
| 3434 | hdr_len += __tcp_hdrlen(th); |
| 3435 | } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { |
| 3436 | struct udphdr _udphdr; |
| 3437 | |
| 3438 | if (skb_header_pointer(skb, skb_transport_offset(skb), |
| 3439 | sizeof(_udphdr), &_udphdr)) |
| 3440 | hdr_len += sizeof(struct udphdr); |
| 3441 | } |
| 3442 | |
| 3443 | if (unlikely(shinfo->gso_type & SKB_GSO_DODGY)) { |
| 3444 | int payload = skb->len - hdr_len; |
| 3445 | |
| 3446 | /* Malicious packet. */ |
| 3447 | if (payload <= 0) |
| 3448 | return; |
| 3449 | gso_segs = DIV_ROUND_UP(payload, shinfo->gso_size); |
| 3450 | } |
| 3451 | qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
| 3456 | struct net_device *dev, |
| 3457 | struct netdev_queue *txq) |
| 3458 | { |
| 3459 | spinlock_t *root_lock = qdisc_lock(q); |
| 3460 | struct sk_buff *to_free = NULL; |
| 3461 | bool contended; |
| 3462 | int rc; |
| 3463 | |
| 3464 | qdisc_calculate_pkt_len(skb, q); |
| 3465 | |
| 3466 | if (q->flags & TCQ_F_NOLOCK) { |
| 3467 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; |
| 3468 | if (likely(!netif_xmit_frozen_or_stopped(txq))) |
| 3469 | qdisc_run(q); |
| 3470 | |
| 3471 | if (unlikely(to_free)) |
| 3472 | kfree_skb_list(to_free); |
| 3473 | return rc; |
| 3474 | } |
| 3475 | |
| 3476 | /* |
| 3477 | * Heuristic to force contended enqueues to serialize on a |
| 3478 | * separate lock before trying to get qdisc main lock. |
| 3479 | * This permits qdisc->running owner to get the lock more |
| 3480 | * often and dequeue packets faster. |
| 3481 | */ |
| 3482 | contended = qdisc_is_running(q); |
| 3483 | if (unlikely(contended)) |
| 3484 | spin_lock(&q->busylock); |
| 3485 | |
| 3486 | spin_lock(root_lock); |
| 3487 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { |
| 3488 | __qdisc_drop(skb, &to_free); |
| 3489 | rc = NET_XMIT_DROP; |
| 3490 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && |
| 3491 | qdisc_run_begin(q)) { |
| 3492 | /* |
| 3493 | * This is a work-conserving queue; there are no old skbs |
| 3494 | * waiting to be sent out; and the qdisc is not running - |
| 3495 | * xmit the skb directly. |
| 3496 | */ |
| 3497 | |
| 3498 | qdisc_bstats_update(q, skb); |
| 3499 | |
| 3500 | if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) { |
| 3501 | if (unlikely(contended)) { |
| 3502 | spin_unlock(&q->busylock); |
| 3503 | contended = false; |
| 3504 | } |
| 3505 | __qdisc_run(q); |
| 3506 | } |
| 3507 | |
| 3508 | qdisc_run_end(q); |
| 3509 | rc = NET_XMIT_SUCCESS; |
| 3510 | } else { |
| 3511 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; |
| 3512 | if (qdisc_run_begin(q)) { |
| 3513 | if (unlikely(contended)) { |
| 3514 | spin_unlock(&q->busylock); |
| 3515 | contended = false; |
| 3516 | } |
| 3517 | __qdisc_run(q); |
| 3518 | qdisc_run_end(q); |
| 3519 | } |
| 3520 | } |
| 3521 | spin_unlock(root_lock); |
| 3522 | if (unlikely(to_free)) |
| 3523 | kfree_skb_list(to_free); |
| 3524 | if (unlikely(contended)) |
| 3525 | spin_unlock(&q->busylock); |
| 3526 | return rc; |
| 3527 | } |
| 3528 | |
| 3529 | #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) |
| 3530 | static void skb_update_prio(struct sk_buff *skb) |
| 3531 | { |
| 3532 | const struct netprio_map *map; |
| 3533 | const struct sock *sk; |
| 3534 | unsigned int prioidx; |
| 3535 | |
| 3536 | if (skb->priority) |
| 3537 | return; |
| 3538 | map = rcu_dereference_bh(skb->dev->priomap); |
| 3539 | if (!map) |
| 3540 | return; |
| 3541 | sk = skb_to_full_sk(skb); |
| 3542 | if (!sk) |
| 3543 | return; |
| 3544 | |
| 3545 | prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data); |
| 3546 | |
| 3547 | if (prioidx < map->priomap_len) |
| 3548 | skb->priority = map->priomap[prioidx]; |
| 3549 | } |
| 3550 | #else |
| 3551 | #define skb_update_prio(skb) |
| 3552 | #endif |
| 3553 | |
| 3554 | /** |
| 3555 | * dev_loopback_xmit - loop back @skb |
| 3556 | * @net: network namespace this loopback is happening in |
| 3557 | * @sk: sk needed to be a netfilter okfn |
| 3558 | * @skb: buffer to transmit |
| 3559 | */ |
| 3560 | int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 3561 | { |
| 3562 | skb_reset_mac_header(skb); |
| 3563 | __skb_pull(skb, skb_network_offset(skb)); |
| 3564 | skb->pkt_type = PACKET_LOOPBACK; |
| 3565 | if (skb->ip_summed == CHECKSUM_NONE) |
| 3566 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 3567 | WARN_ON(!skb_dst(skb)); |
| 3568 | skb_dst_force(skb); |
| 3569 | netif_rx_ni(skb); |
| 3570 | return 0; |
| 3571 | } |
| 3572 | EXPORT_SYMBOL(dev_loopback_xmit); |
| 3573 | |
| 3574 | #ifdef CONFIG_NET_EGRESS |
| 3575 | static struct sk_buff * |
| 3576 | sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) |
| 3577 | { |
| 3578 | struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress); |
| 3579 | struct tcf_result cl_res; |
| 3580 | |
| 3581 | if (!miniq) |
| 3582 | return skb; |
| 3583 | |
| 3584 | /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */ |
| 3585 | mini_qdisc_bstats_cpu_update(miniq, skb); |
| 3586 | |
| 3587 | switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) { |
| 3588 | case TC_ACT_OK: |
| 3589 | case TC_ACT_RECLASSIFY: |
| 3590 | skb->tc_index = TC_H_MIN(cl_res.classid); |
| 3591 | break; |
| 3592 | case TC_ACT_SHOT: |
| 3593 | mini_qdisc_qstats_cpu_drop(miniq); |
| 3594 | *ret = NET_XMIT_DROP; |
| 3595 | kfree_skb(skb); |
| 3596 | return NULL; |
| 3597 | case TC_ACT_STOLEN: |
| 3598 | case TC_ACT_QUEUED: |
| 3599 | case TC_ACT_TRAP: |
| 3600 | *ret = NET_XMIT_SUCCESS; |
| 3601 | consume_skb(skb); |
| 3602 | return NULL; |
| 3603 | case TC_ACT_REDIRECT: |
| 3604 | /* No need to push/pop skb's mac_header here on egress! */ |
| 3605 | skb_do_redirect(skb); |
| 3606 | *ret = NET_XMIT_SUCCESS; |
| 3607 | return NULL; |
| 3608 | default: |
| 3609 | break; |
| 3610 | } |
| 3611 | |
| 3612 | return skb; |
| 3613 | } |
| 3614 | #endif /* CONFIG_NET_EGRESS */ |
| 3615 | |
| 3616 | #ifdef CONFIG_XPS |
| 3617 | static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb, |
| 3618 | struct xps_dev_maps *dev_maps, unsigned int tci) |
| 3619 | { |
| 3620 | struct xps_map *map; |
| 3621 | int queue_index = -1; |
| 3622 | |
| 3623 | if (dev->num_tc) { |
| 3624 | tci *= dev->num_tc; |
| 3625 | tci += netdev_get_prio_tc_map(dev, skb->priority); |
| 3626 | } |
| 3627 | |
| 3628 | map = rcu_dereference(dev_maps->attr_map[tci]); |
| 3629 | if (map) { |
| 3630 | if (map->len == 1) |
| 3631 | queue_index = map->queues[0]; |
| 3632 | else |
| 3633 | queue_index = map->queues[reciprocal_scale( |
| 3634 | skb_get_hash(skb), map->len)]; |
| 3635 | if (unlikely(queue_index >= dev->real_num_tx_queues)) |
| 3636 | queue_index = -1; |
| 3637 | } |
| 3638 | return queue_index; |
| 3639 | } |
| 3640 | #endif |
| 3641 | |
| 3642 | static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev, |
| 3643 | struct sk_buff *skb) |
| 3644 | { |
| 3645 | #ifdef CONFIG_XPS |
| 3646 | struct xps_dev_maps *dev_maps; |
| 3647 | struct sock *sk = skb->sk; |
| 3648 | int queue_index = -1; |
| 3649 | |
| 3650 | if (!static_key_false(&xps_needed)) |
| 3651 | return -1; |
| 3652 | |
| 3653 | rcu_read_lock(); |
| 3654 | if (!static_key_false(&xps_rxqs_needed)) |
| 3655 | goto get_cpus_map; |
| 3656 | |
| 3657 | dev_maps = rcu_dereference(sb_dev->xps_rxqs_map); |
| 3658 | if (dev_maps) { |
| 3659 | int tci = sk_rx_queue_get(sk); |
| 3660 | |
| 3661 | if (tci >= 0 && tci < dev->num_rx_queues) |
| 3662 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, |
| 3663 | tci); |
| 3664 | } |
| 3665 | |
| 3666 | get_cpus_map: |
| 3667 | if (queue_index < 0) { |
| 3668 | dev_maps = rcu_dereference(sb_dev->xps_cpus_map); |
| 3669 | if (dev_maps) { |
| 3670 | unsigned int tci = skb->sender_cpu - 1; |
| 3671 | |
| 3672 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, |
| 3673 | tci); |
| 3674 | } |
| 3675 | } |
| 3676 | rcu_read_unlock(); |
| 3677 | |
| 3678 | return queue_index; |
| 3679 | #else |
| 3680 | return -1; |
| 3681 | #endif |
| 3682 | } |
| 3683 | |
| 3684 | u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb, |
| 3685 | struct net_device *sb_dev) |
| 3686 | { |
| 3687 | return 0; |
| 3688 | } |
| 3689 | EXPORT_SYMBOL(dev_pick_tx_zero); |
| 3690 | |
| 3691 | u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb, |
| 3692 | struct net_device *sb_dev) |
| 3693 | { |
| 3694 | return (u16)raw_smp_processor_id() % dev->real_num_tx_queues; |
| 3695 | } |
| 3696 | EXPORT_SYMBOL(dev_pick_tx_cpu_id); |
| 3697 | |
| 3698 | u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb, |
| 3699 | struct net_device *sb_dev) |
| 3700 | { |
| 3701 | struct sock *sk = skb->sk; |
| 3702 | int queue_index = sk_tx_queue_get(sk); |
| 3703 | |
| 3704 | sb_dev = sb_dev ? : dev; |
| 3705 | |
| 3706 | if (queue_index < 0 || skb->ooo_okay || |
| 3707 | queue_index >= dev->real_num_tx_queues) { |
| 3708 | int new_index = get_xps_queue(dev, sb_dev, skb); |
| 3709 | |
| 3710 | if (new_index < 0) |
| 3711 | new_index = skb_tx_hash(dev, sb_dev, skb); |
| 3712 | |
| 3713 | if (queue_index != new_index && sk && |
| 3714 | sk_fullsock(sk) && |
| 3715 | rcu_access_pointer(sk->sk_dst_cache)) |
| 3716 | sk_tx_queue_set(sk, new_index); |
| 3717 | |
| 3718 | queue_index = new_index; |
| 3719 | } |
| 3720 | |
| 3721 | return queue_index; |
| 3722 | } |
| 3723 | EXPORT_SYMBOL(netdev_pick_tx); |
| 3724 | |
| 3725 | struct netdev_queue *netdev_core_pick_tx(struct net_device *dev, |
| 3726 | struct sk_buff *skb, |
| 3727 | struct net_device *sb_dev) |
| 3728 | { |
| 3729 | int queue_index = 0; |
| 3730 | |
| 3731 | #ifdef CONFIG_XPS |
| 3732 | u32 sender_cpu = skb->sender_cpu - 1; |
| 3733 | |
| 3734 | if (sender_cpu >= (u32)NR_CPUS) |
| 3735 | skb->sender_cpu = raw_smp_processor_id() + 1; |
| 3736 | #endif |
| 3737 | |
| 3738 | if (dev->real_num_tx_queues != 1) { |
| 3739 | const struct net_device_ops *ops = dev->netdev_ops; |
| 3740 | |
| 3741 | if (ops->ndo_select_queue) |
| 3742 | queue_index = ops->ndo_select_queue(dev, skb, sb_dev); |
| 3743 | else |
| 3744 | queue_index = netdev_pick_tx(dev, skb, sb_dev); |
| 3745 | |
| 3746 | queue_index = netdev_cap_txqueue(dev, queue_index); |
| 3747 | } |
| 3748 | |
| 3749 | skb_set_queue_mapping(skb, queue_index); |
| 3750 | return netdev_get_tx_queue(dev, queue_index); |
| 3751 | } |
| 3752 | EXPORT_SYMBOL(netdev_core_pick_tx); |
| 3753 | |
| 3754 | /** |
| 3755 | * __dev_queue_xmit - transmit a buffer |
| 3756 | * @skb: buffer to transmit |
| 3757 | * @sb_dev: suboordinate device used for L2 forwarding offload |
| 3758 | * |
| 3759 | * Queue a buffer for transmission to a network device. The caller must |
| 3760 | * have set the device and priority and built the buffer before calling |
| 3761 | * this function. The function can be called from an interrupt. |
| 3762 | * |
| 3763 | * A negative errno code is returned on a failure. A success does not |
| 3764 | * guarantee the frame will be transmitted as it may be dropped due |
| 3765 | * to congestion or traffic shaping. |
| 3766 | * |
| 3767 | * ----------------------------------------------------------------------------------- |
| 3768 | * I notice this method can also return errors from the queue disciplines, |
| 3769 | * including NET_XMIT_DROP, which is a positive value. So, errors can also |
| 3770 | * be positive. |
| 3771 | * |
| 3772 | * Regardless of the return value, the skb is consumed, so it is currently |
| 3773 | * difficult to retry a send to this method. (You can bump the ref count |
| 3774 | * before sending to hold a reference for retry if you are careful.) |
| 3775 | * |
| 3776 | * When calling this method, interrupts MUST be enabled. This is because |
| 3777 | * the BH enable code must have IRQs enabled so that it will not deadlock. |
| 3778 | * --BLG |
| 3779 | */ |
| 3780 | static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) |
| 3781 | { |
| 3782 | struct net_device *dev = skb->dev; |
| 3783 | struct netdev_queue *txq; |
| 3784 | struct Qdisc *q; |
| 3785 | int rc = -ENOMEM; |
| 3786 | bool again = false; |
| 3787 | |
| 3788 | skb_reset_mac_header(skb); |
| 3789 | skb_assert_len(skb); |
| 3790 | |
| 3791 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) |
| 3792 | __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED); |
| 3793 | |
| 3794 | /* Disable soft irqs for various locks below. Also |
| 3795 | * stops preemption for RCU. |
| 3796 | */ |
| 3797 | rcu_read_lock_bh(); |
| 3798 | |
| 3799 | skb_update_prio(skb); |
| 3800 | |
| 3801 | qdisc_pkt_len_init(skb); |
| 3802 | #ifdef CONFIG_NET_CLS_ACT |
| 3803 | skb->tc_at_ingress = 0; |
| 3804 | # ifdef CONFIG_NET_EGRESS |
| 3805 | if (static_branch_unlikely(&egress_needed_key)) { |
| 3806 | skb = sch_handle_egress(skb, &rc, dev); |
| 3807 | if (!skb) |
| 3808 | goto out; |
| 3809 | } |
| 3810 | # endif |
| 3811 | #endif |
| 3812 | /* If device/qdisc don't need skb->dst, release it right now while |
| 3813 | * its hot in this cpu cache. |
| 3814 | */ |
| 3815 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) |
| 3816 | skb_dst_drop(skb); |
| 3817 | else |
| 3818 | skb_dst_force(skb); |
| 3819 | |
| 3820 | txq = netdev_core_pick_tx(dev, skb, sb_dev); |
| 3821 | q = rcu_dereference_bh(txq->qdisc); |
| 3822 | |
| 3823 | trace_net_dev_queue(skb); |
| 3824 | if (q->enqueue) { |
| 3825 | rc = __dev_xmit_skb(skb, q, dev, txq); |
| 3826 | goto out; |
| 3827 | } |
| 3828 | |
| 3829 | /* The device has no queue. Common case for software devices: |
| 3830 | * loopback, all the sorts of tunnels... |
| 3831 | |
| 3832 | * Really, it is unlikely that netif_tx_lock protection is necessary |
| 3833 | * here. (f.e. loopback and IP tunnels are clean ignoring statistics |
| 3834 | * counters.) |
| 3835 | * However, it is possible, that they rely on protection |
| 3836 | * made by us here. |
| 3837 | |
| 3838 | * Check this and shot the lock. It is not prone from deadlocks. |
| 3839 | *Either shot noqueue qdisc, it is even simpler 8) |
| 3840 | */ |
| 3841 | if (dev->flags & IFF_UP) { |
| 3842 | int cpu = smp_processor_id(); /* ok because BHs are off */ |
| 3843 | |
| 3844 | /* Other cpus might concurrently change txq->xmit_lock_owner |
| 3845 | * to -1 or to their cpu id, but not to our id. |
| 3846 | */ |
| 3847 | if (READ_ONCE(txq->xmit_lock_owner) != cpu) { |
| 3848 | if (dev_xmit_recursion()) |
| 3849 | goto recursion_alert; |
| 3850 | |
| 3851 | skb = validate_xmit_skb(skb, dev, &again); |
| 3852 | if (!skb) |
| 3853 | goto out; |
| 3854 | |
| 3855 | HARD_TX_LOCK(dev, txq, cpu); |
| 3856 | |
| 3857 | if (!netif_xmit_stopped(txq)) { |
| 3858 | dev_xmit_recursion_inc(); |
| 3859 | skb = dev_hard_start_xmit(skb, dev, txq, &rc); |
| 3860 | dev_xmit_recursion_dec(); |
| 3861 | if (dev_xmit_complete(rc)) { |
| 3862 | HARD_TX_UNLOCK(dev, txq); |
| 3863 | goto out; |
| 3864 | } |
| 3865 | } |
| 3866 | HARD_TX_UNLOCK(dev, txq); |
| 3867 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
| 3868 | dev->name); |
| 3869 | } else { |
| 3870 | /* Recursion is detected! It is possible, |
| 3871 | * unfortunately |
| 3872 | */ |
| 3873 | recursion_alert: |
| 3874 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
| 3875 | dev->name); |
| 3876 | } |
| 3877 | } |
| 3878 | |
| 3879 | rc = -ENETDOWN; |
| 3880 | rcu_read_unlock_bh(); |
| 3881 | |
| 3882 | atomic_long_inc(&dev->tx_dropped); |
| 3883 | kfree_skb_list(skb); |
| 3884 | return rc; |
| 3885 | out: |
| 3886 | rcu_read_unlock_bh(); |
| 3887 | return rc; |
| 3888 | } |
| 3889 | |
| 3890 | int dev_queue_xmit(struct sk_buff *skb) |
| 3891 | { |
| 3892 | return __dev_queue_xmit(skb, NULL); |
| 3893 | } |
| 3894 | EXPORT_SYMBOL(dev_queue_xmit); |
| 3895 | |
| 3896 | int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev) |
| 3897 | { |
| 3898 | return __dev_queue_xmit(skb, sb_dev); |
| 3899 | } |
| 3900 | EXPORT_SYMBOL(dev_queue_xmit_accel); |
| 3901 | |
| 3902 | int dev_direct_xmit(struct sk_buff *skb, u16 queue_id) |
| 3903 | { |
| 3904 | struct net_device *dev = skb->dev; |
| 3905 | struct sk_buff *orig_skb = skb; |
| 3906 | struct netdev_queue *txq; |
| 3907 | int ret = NETDEV_TX_BUSY; |
| 3908 | bool again = false; |
| 3909 | |
| 3910 | if (unlikely(!netif_running(dev) || |
| 3911 | !netif_carrier_ok(dev))) |
| 3912 | goto drop; |
| 3913 | |
| 3914 | skb = validate_xmit_skb_list(skb, dev, &again); |
| 3915 | if (skb != orig_skb) |
| 3916 | goto drop; |
| 3917 | |
| 3918 | skb_set_queue_mapping(skb, queue_id); |
| 3919 | txq = skb_get_tx_queue(dev, skb); |
| 3920 | |
| 3921 | local_bh_disable(); |
| 3922 | |
| 3923 | dev_xmit_recursion_inc(); |
| 3924 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
| 3925 | if (!netif_xmit_frozen_or_drv_stopped(txq)) |
| 3926 | ret = netdev_start_xmit(skb, dev, txq, false); |
| 3927 | HARD_TX_UNLOCK(dev, txq); |
| 3928 | dev_xmit_recursion_dec(); |
| 3929 | |
| 3930 | local_bh_enable(); |
| 3931 | |
| 3932 | if (!dev_xmit_complete(ret)) |
| 3933 | kfree_skb(skb); |
| 3934 | |
| 3935 | return ret; |
| 3936 | drop: |
| 3937 | atomic_long_inc(&dev->tx_dropped); |
| 3938 | kfree_skb_list(skb); |
| 3939 | return NET_XMIT_DROP; |
| 3940 | } |
| 3941 | EXPORT_SYMBOL(dev_direct_xmit); |
| 3942 | |
| 3943 | /************************************************************************* |
| 3944 | * Receiver routines |
| 3945 | *************************************************************************/ |
| 3946 | |
| 3947 | int netdev_max_backlog __read_mostly = 1000; |
| 3948 | EXPORT_SYMBOL(netdev_max_backlog); |
| 3949 | |
| 3950 | int netdev_tstamp_prequeue __read_mostly = 1; |
| 3951 | int netdev_budget __read_mostly = 300; |
| 3952 | /* Must be at least 2 jiffes to guarantee 1 jiffy timeout */ |
| 3953 | unsigned int __read_mostly netdev_budget_usecs = 2 * USEC_PER_SEC / HZ; |
| 3954 | int weight_p __read_mostly = 64; /* old backlog weight */ |
| 3955 | int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */ |
| 3956 | int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */ |
| 3957 | int dev_rx_weight __read_mostly = 64; |
| 3958 | int dev_tx_weight __read_mostly = 64; |
| 3959 | /* Maximum number of GRO_NORMAL skbs to batch up for list-RX */ |
| 3960 | int gro_normal_batch __read_mostly = 8; |
| 3961 | |
| 3962 | /* Called with irq disabled */ |
| 3963 | static inline void ____napi_schedule(struct softnet_data *sd, |
| 3964 | struct napi_struct *napi) |
| 3965 | { |
| 3966 | struct task_struct *thread; |
| 3967 | |
| 3968 | if (test_bit(NAPI_STATE_THREADED, &napi->state)) { |
| 3969 | /* Paired with smp_mb__before_atomic() in |
| 3970 | * napi_enable()/dev_set_threaded(). |
| 3971 | * Use READ_ONCE() to guarantee a complete |
| 3972 | * read on napi->thread. Only call |
| 3973 | * wake_up_process() when it's not NULL. |
| 3974 | */ |
| 3975 | thread = READ_ONCE(napi->thread); |
| 3976 | if (thread) { |
| 3977 | if (thread->state != TASK_INTERRUPTIBLE) |
| 3978 | set_bit(NAPI_STATE_SCHED_THREADED, &napi->state); |
| 3979 | wake_up_process(thread); |
| 3980 | return; |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | list_add_tail(&napi->poll_list, &sd->poll_list); |
| 3985 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
| 3986 | } |
| 3987 | |
| 3988 | #ifdef CONFIG_RPS |
| 3989 | |
| 3990 | /* One global table that all flow-based protocols share. */ |
| 3991 | struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly; |
| 3992 | EXPORT_SYMBOL(rps_sock_flow_table); |
| 3993 | u32 rps_cpu_mask __read_mostly; |
| 3994 | EXPORT_SYMBOL(rps_cpu_mask); |
| 3995 | |
| 3996 | struct static_key_false rps_needed __read_mostly; |
| 3997 | EXPORT_SYMBOL(rps_needed); |
| 3998 | struct static_key_false rfs_needed __read_mostly; |
| 3999 | EXPORT_SYMBOL(rfs_needed); |
| 4000 | |
| 4001 | static struct rps_dev_flow * |
| 4002 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, |
| 4003 | struct rps_dev_flow *rflow, u16 next_cpu) |
| 4004 | { |
| 4005 | if (next_cpu < nr_cpu_ids) { |
| 4006 | #ifdef CONFIG_RFS_ACCEL |
| 4007 | struct netdev_rx_queue *rxqueue; |
| 4008 | struct rps_dev_flow_table *flow_table; |
| 4009 | struct rps_dev_flow *old_rflow; |
| 4010 | u32 flow_id; |
| 4011 | u16 rxq_index; |
| 4012 | int rc; |
| 4013 | |
| 4014 | /* Should we steer this flow to a different hardware queue? */ |
| 4015 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
| 4016 | !(dev->features & NETIF_F_NTUPLE)) |
| 4017 | goto out; |
| 4018 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); |
| 4019 | if (rxq_index == skb_get_rx_queue(skb)) |
| 4020 | goto out; |
| 4021 | |
| 4022 | rxqueue = dev->_rx + rxq_index; |
| 4023 | flow_table = rcu_dereference(rxqueue->rps_flow_table); |
| 4024 | if (!flow_table) |
| 4025 | goto out; |
| 4026 | flow_id = skb_get_hash(skb) & flow_table->mask; |
| 4027 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, |
| 4028 | rxq_index, flow_id); |
| 4029 | if (rc < 0) |
| 4030 | goto out; |
| 4031 | old_rflow = rflow; |
| 4032 | rflow = &flow_table->flows[flow_id]; |
| 4033 | rflow->filter = rc; |
| 4034 | if (old_rflow->filter == rflow->filter) |
| 4035 | old_rflow->filter = RPS_NO_FILTER; |
| 4036 | out: |
| 4037 | #endif |
| 4038 | rflow->last_qtail = |
| 4039 | per_cpu(softnet_data, next_cpu).input_queue_head; |
| 4040 | } |
| 4041 | |
| 4042 | rflow->cpu = next_cpu; |
| 4043 | return rflow; |
| 4044 | } |
| 4045 | |
| 4046 | /* |
| 4047 | * get_rps_cpu is called from netif_receive_skb and returns the target |
| 4048 | * CPU from the RPS map of the receiving queue for a given skb. |
| 4049 | * rcu_read_lock must be held on entry. |
| 4050 | */ |
| 4051 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, |
| 4052 | struct rps_dev_flow **rflowp) |
| 4053 | { |
| 4054 | const struct rps_sock_flow_table *sock_flow_table; |
| 4055 | struct netdev_rx_queue *rxqueue = dev->_rx; |
| 4056 | struct rps_dev_flow_table *flow_table; |
| 4057 | struct rps_map *map; |
| 4058 | int cpu = -1; |
| 4059 | u32 tcpu; |
| 4060 | u32 hash; |
| 4061 | |
| 4062 | if (skb_rx_queue_recorded(skb)) { |
| 4063 | u16 index = skb_get_rx_queue(skb); |
| 4064 | |
| 4065 | if (unlikely(index >= dev->real_num_rx_queues)) { |
| 4066 | WARN_ONCE(dev->real_num_rx_queues > 1, |
| 4067 | "%s received packet on queue %u, but number " |
| 4068 | "of RX queues is %u\n", |
| 4069 | dev->name, index, dev->real_num_rx_queues); |
| 4070 | goto done; |
| 4071 | } |
| 4072 | rxqueue += index; |
| 4073 | } |
| 4074 | |
| 4075 | /* Avoid computing hash if RFS/RPS is not active for this rxqueue */ |
| 4076 | |
| 4077 | flow_table = rcu_dereference(rxqueue->rps_flow_table); |
| 4078 | map = rcu_dereference(rxqueue->rps_map); |
| 4079 | if (!flow_table && !map) |
| 4080 | goto done; |
| 4081 | |
| 4082 | skb_reset_network_header(skb); |
| 4083 | hash = skb_get_hash(skb); |
| 4084 | if (!hash) |
| 4085 | goto done; |
| 4086 | |
| 4087 | sock_flow_table = rcu_dereference(rps_sock_flow_table); |
| 4088 | if (flow_table && sock_flow_table) { |
| 4089 | struct rps_dev_flow *rflow; |
| 4090 | u32 next_cpu; |
| 4091 | u32 ident; |
| 4092 | |
| 4093 | /* First check into global flow table if there is a match. |
| 4094 | * This READ_ONCE() pairs with WRITE_ONCE() from rps_record_sock_flow(). |
| 4095 | */ |
| 4096 | ident = READ_ONCE(sock_flow_table->ents[hash & sock_flow_table->mask]); |
| 4097 | if ((ident ^ hash) & ~rps_cpu_mask) |
| 4098 | goto try_rps; |
| 4099 | |
| 4100 | next_cpu = ident & rps_cpu_mask; |
| 4101 | |
| 4102 | /* OK, now we know there is a match, |
| 4103 | * we can look at the local (per receive queue) flow table |
| 4104 | */ |
| 4105 | rflow = &flow_table->flows[hash & flow_table->mask]; |
| 4106 | tcpu = rflow->cpu; |
| 4107 | |
| 4108 | /* |
| 4109 | * If the desired CPU (where last recvmsg was done) is |
| 4110 | * different from current CPU (one in the rx-queue flow |
| 4111 | * table entry), switch if one of the following holds: |
| 4112 | * - Current CPU is unset (>= nr_cpu_ids). |
| 4113 | * - Current CPU is offline. |
| 4114 | * - The current CPU's queue tail has advanced beyond the |
| 4115 | * last packet that was enqueued using this table entry. |
| 4116 | * This guarantees that all previous packets for the flow |
| 4117 | * have been dequeued, thus preserving in order delivery. |
| 4118 | */ |
| 4119 | if (unlikely(tcpu != next_cpu) && |
| 4120 | (tcpu >= nr_cpu_ids || !cpu_online(tcpu) || |
| 4121 | ((int)(per_cpu(softnet_data, tcpu).input_queue_head - |
| 4122 | rflow->last_qtail)) >= 0)) { |
| 4123 | tcpu = next_cpu; |
| 4124 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
| 4125 | } |
| 4126 | |
| 4127 | if (tcpu < nr_cpu_ids && cpu_online(tcpu)) { |
| 4128 | *rflowp = rflow; |
| 4129 | cpu = tcpu; |
| 4130 | goto done; |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | try_rps: |
| 4135 | |
| 4136 | if (map) { |
| 4137 | tcpu = map->cpus[reciprocal_scale(hash, map->len)]; |
| 4138 | if (cpu_online(tcpu)) { |
| 4139 | cpu = tcpu; |
| 4140 | goto done; |
| 4141 | } |
| 4142 | } |
| 4143 | |
| 4144 | done: |
| 4145 | return cpu; |
| 4146 | } |
| 4147 | |
| 4148 | #ifdef CONFIG_RFS_ACCEL |
| 4149 | |
| 4150 | /** |
| 4151 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed |
| 4152 | * @dev: Device on which the filter was set |
| 4153 | * @rxq_index: RX queue index |
| 4154 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() |
| 4155 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() |
| 4156 | * |
| 4157 | * Drivers that implement ndo_rx_flow_steer() should periodically call |
| 4158 | * this function for each installed filter and remove the filters for |
| 4159 | * which it returns %true. |
| 4160 | */ |
| 4161 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, |
| 4162 | u32 flow_id, u16 filter_id) |
| 4163 | { |
| 4164 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; |
| 4165 | struct rps_dev_flow_table *flow_table; |
| 4166 | struct rps_dev_flow *rflow; |
| 4167 | bool expire = true; |
| 4168 | unsigned int cpu; |
| 4169 | |
| 4170 | rcu_read_lock(); |
| 4171 | flow_table = rcu_dereference(rxqueue->rps_flow_table); |
| 4172 | if (flow_table && flow_id <= flow_table->mask) { |
| 4173 | rflow = &flow_table->flows[flow_id]; |
| 4174 | cpu = READ_ONCE(rflow->cpu); |
| 4175 | if (rflow->filter == filter_id && cpu < nr_cpu_ids && |
| 4176 | ((int)(per_cpu(softnet_data, cpu).input_queue_head - |
| 4177 | rflow->last_qtail) < |
| 4178 | (int)(10 * flow_table->mask))) |
| 4179 | expire = false; |
| 4180 | } |
| 4181 | rcu_read_unlock(); |
| 4182 | return expire; |
| 4183 | } |
| 4184 | EXPORT_SYMBOL(rps_may_expire_flow); |
| 4185 | |
| 4186 | #endif /* CONFIG_RFS_ACCEL */ |
| 4187 | |
| 4188 | /* Called from hardirq (IPI) context */ |
| 4189 | static void rps_trigger_softirq(void *data) |
| 4190 | { |
| 4191 | struct softnet_data *sd = data; |
| 4192 | |
| 4193 | ____napi_schedule(sd, &sd->backlog); |
| 4194 | sd->received_rps++; |
| 4195 | } |
| 4196 | |
| 4197 | #endif /* CONFIG_RPS */ |
| 4198 | |
| 4199 | /* |
| 4200 | * Check if this softnet_data structure is another cpu one |
| 4201 | * If yes, queue it to our IPI list and return 1 |
| 4202 | * If no, return 0 |
| 4203 | */ |
| 4204 | static int rps_ipi_queued(struct softnet_data *sd) |
| 4205 | { |
| 4206 | #ifdef CONFIG_RPS |
| 4207 | struct softnet_data *mysd = this_cpu_ptr(&softnet_data); |
| 4208 | |
| 4209 | if (sd != mysd) { |
| 4210 | sd->rps_ipi_next = mysd->rps_ipi_list; |
| 4211 | mysd->rps_ipi_list = sd; |
| 4212 | |
| 4213 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
| 4214 | return 1; |
| 4215 | } |
| 4216 | #endif /* CONFIG_RPS */ |
| 4217 | return 0; |
| 4218 | } |
| 4219 | |
| 4220 | #ifdef CONFIG_NET_FLOW_LIMIT |
| 4221 | int netdev_flow_limit_table_len __read_mostly = (1 << 12); |
| 4222 | #endif |
| 4223 | |
| 4224 | static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) |
| 4225 | { |
| 4226 | #ifdef CONFIG_NET_FLOW_LIMIT |
| 4227 | struct sd_flow_limit *fl; |
| 4228 | struct softnet_data *sd; |
| 4229 | unsigned int old_flow, new_flow; |
| 4230 | |
| 4231 | if (qlen < (netdev_max_backlog >> 1)) |
| 4232 | return false; |
| 4233 | |
| 4234 | sd = this_cpu_ptr(&softnet_data); |
| 4235 | |
| 4236 | rcu_read_lock(); |
| 4237 | fl = rcu_dereference(sd->flow_limit); |
| 4238 | if (fl) { |
| 4239 | new_flow = skb_get_hash(skb) & (fl->num_buckets - 1); |
| 4240 | old_flow = fl->history[fl->history_head]; |
| 4241 | fl->history[fl->history_head] = new_flow; |
| 4242 | |
| 4243 | fl->history_head++; |
| 4244 | fl->history_head &= FLOW_LIMIT_HISTORY - 1; |
| 4245 | |
| 4246 | if (likely(fl->buckets[old_flow])) |
| 4247 | fl->buckets[old_flow]--; |
| 4248 | |
| 4249 | if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { |
| 4250 | fl->count++; |
| 4251 | rcu_read_unlock(); |
| 4252 | return true; |
| 4253 | } |
| 4254 | } |
| 4255 | rcu_read_unlock(); |
| 4256 | #endif |
| 4257 | return false; |
| 4258 | } |
| 4259 | |
| 4260 | /* |
| 4261 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog |
| 4262 | * queue (may be a remote CPU queue). |
| 4263 | */ |
| 4264 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
| 4265 | unsigned int *qtail) |
| 4266 | { |
| 4267 | struct softnet_data *sd; |
| 4268 | unsigned long flags; |
| 4269 | unsigned int qlen; |
| 4270 | |
| 4271 | sd = &per_cpu(softnet_data, cpu); |
| 4272 | |
| 4273 | local_irq_save(flags); |
| 4274 | |
| 4275 | rps_lock(sd); |
| 4276 | if (!netif_running(skb->dev)) |
| 4277 | goto drop; |
| 4278 | qlen = skb_queue_len(&sd->input_pkt_queue); |
| 4279 | if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) { |
| 4280 | if (qlen) { |
| 4281 | enqueue: |
| 4282 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
| 4283 | input_queue_tail_incr_save(sd, qtail); |
| 4284 | rps_unlock(sd); |
| 4285 | local_irq_restore(flags); |
| 4286 | return NET_RX_SUCCESS; |
| 4287 | } |
| 4288 | |
| 4289 | /* Schedule NAPI for backlog device |
| 4290 | * We can use non atomic operation since we own the queue lock |
| 4291 | */ |
| 4292 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) { |
| 4293 | if (!rps_ipi_queued(sd)) |
| 4294 | ____napi_schedule(sd, &sd->backlog); |
| 4295 | } |
| 4296 | goto enqueue; |
| 4297 | } |
| 4298 | |
| 4299 | drop: |
| 4300 | sd->dropped++; |
| 4301 | rps_unlock(sd); |
| 4302 | |
| 4303 | local_irq_restore(flags); |
| 4304 | |
| 4305 | atomic_long_inc(&skb->dev->rx_dropped); |
| 4306 | kfree_skb(skb); |
| 4307 | return NET_RX_DROP; |
| 4308 | } |
| 4309 | |
| 4310 | #ifdef CONFIG_NETIF_RX_FASTPATH_HOOK |
| 4311 | static int netif_rx_fastpath_default(struct sk_buff *skb) |
| 4312 | { |
| 4313 | return 0; /* always slow */ |
| 4314 | } |
| 4315 | |
| 4316 | static int (*netif_rx_fastpath_hook)(struct sk_buff *skb) = &netif_rx_fastpath_default; |
| 4317 | |
| 4318 | void netif_rx_fastpath_register(int (*hook)(struct sk_buff *)) |
| 4319 | { |
| 4320 | pr_info("netif_rx_fastpath_register hook=%p\n", hook); |
| 4321 | netif_rx_fastpath_hook = hook; |
| 4322 | } |
| 4323 | EXPORT_SYMBOL(netif_rx_fastpath_register); |
| 4324 | |
| 4325 | void netif_rx_fastpath_unregister(void) |
| 4326 | { |
| 4327 | pr_info("netif_rx_fastpath_unregister\n"); |
| 4328 | netif_rx_fastpath_hook = &netif_rx_fastpath_default; |
| 4329 | } |
| 4330 | EXPORT_SYMBOL(netif_rx_fastpath_unregister); |
| 4331 | |
| 4332 | static inline int netif_rx_fastpath(struct sk_buff *skb) |
| 4333 | { |
| 4334 | return netif_rx_fastpath_hook(skb); |
| 4335 | } |
| 4336 | |
| 4337 | #else /* !CONFIG_NETIF_RX_FASTPATH_HOOK */ |
| 4338 | static inline int netif_rx_fastpath(struct sk_buff *skb) |
| 4339 | { |
| 4340 | return 0; |
| 4341 | } |
| 4342 | #endif |
| 4343 | |
| 4344 | #define IS_SKB_P(skb) ((skb)->shared_info_ptr) |
| 4345 | |
| 4346 | static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) |
| 4347 | { |
| 4348 | struct net_device *dev = skb->dev; |
| 4349 | struct netdev_rx_queue *rxqueue; |
| 4350 | |
| 4351 | rxqueue = dev->_rx; |
| 4352 | |
| 4353 | if (skb_rx_queue_recorded(skb)) { |
| 4354 | u16 index = skb_get_rx_queue(skb); |
| 4355 | |
| 4356 | if (unlikely(index >= dev->real_num_rx_queues)) { |
| 4357 | WARN_ONCE(dev->real_num_rx_queues > 1, |
| 4358 | "%s received packet on queue %u, but number " |
| 4359 | "of RX queues is %u\n", |
| 4360 | dev->name, index, dev->real_num_rx_queues); |
| 4361 | |
| 4362 | return rxqueue; /* Return first rxqueue */ |
| 4363 | } |
| 4364 | rxqueue += index; |
| 4365 | } |
| 4366 | return rxqueue; |
| 4367 | } |
| 4368 | |
| 4369 | static u32 netif_receive_generic_xdp(struct sk_buff *skb, |
| 4370 | struct xdp_buff *xdp, |
| 4371 | struct bpf_prog *xdp_prog) |
| 4372 | { |
| 4373 | struct netdev_rx_queue *rxqueue; |
| 4374 | void *orig_data, *orig_data_end; |
| 4375 | u32 metalen, act = XDP_DROP; |
| 4376 | __be16 orig_eth_type; |
| 4377 | struct ethhdr *eth; |
| 4378 | bool orig_bcast; |
| 4379 | int hlen, off; |
| 4380 | u32 mac_len; |
| 4381 | |
| 4382 | /* Reinjected packets coming from act_mirred or similar should |
| 4383 | * not get XDP generic processing. |
| 4384 | */ |
| 4385 | if (skb_is_redirected(skb)) |
| 4386 | return XDP_PASS; |
| 4387 | |
| 4388 | /* XDP packets must be linear and must have sufficient headroom |
| 4389 | * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also |
| 4390 | * native XDP provides, thus we need to do it here as well. |
| 4391 | */ |
| 4392 | if (skb_cloned(skb) || skb_is_nonlinear(skb) || |
| 4393 | skb_headroom(skb) < XDP_PACKET_HEADROOM) { |
| 4394 | int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb); |
| 4395 | int troom = skb->tail + skb->data_len - skb->end; |
| 4396 | |
| 4397 | /* In case we have to go down the path and also linearize, |
| 4398 | * then lets do the pskb_expand_head() work just once here. |
| 4399 | */ |
| 4400 | if (pskb_expand_head(skb, |
| 4401 | hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0, |
| 4402 | troom > 0 ? troom + 128 : 0, GFP_ATOMIC)) |
| 4403 | goto do_drop; |
| 4404 | if (skb_linearize(skb)) |
| 4405 | goto do_drop; |
| 4406 | } |
| 4407 | |
| 4408 | /* The XDP program wants to see the packet starting at the MAC |
| 4409 | * header. |
| 4410 | */ |
| 4411 | mac_len = skb->data - skb_mac_header(skb); |
| 4412 | hlen = skb_headlen(skb) + mac_len; |
| 4413 | xdp->data = skb->data - mac_len; |
| 4414 | xdp->data_meta = xdp->data; |
| 4415 | xdp->data_end = xdp->data + hlen; |
| 4416 | xdp->data_hard_start = skb->data - skb_headroom(skb); |
| 4417 | orig_data_end = xdp->data_end; |
| 4418 | orig_data = xdp->data; |
| 4419 | eth = (struct ethhdr *)xdp->data; |
| 4420 | orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); |
| 4421 | orig_eth_type = eth->h_proto; |
| 4422 | |
| 4423 | rxqueue = netif_get_rxqueue(skb); |
| 4424 | xdp->rxq = &rxqueue->xdp_rxq; |
| 4425 | |
| 4426 | act = bpf_prog_run_xdp(xdp_prog, xdp); |
| 4427 | |
| 4428 | /* check if bpf_xdp_adjust_head was used */ |
| 4429 | off = xdp->data - orig_data; |
| 4430 | if (off) { |
| 4431 | if (off > 0) |
| 4432 | __skb_pull(skb, off); |
| 4433 | else if (off < 0) |
| 4434 | __skb_push(skb, -off); |
| 4435 | |
| 4436 | skb->mac_header += off; |
| 4437 | skb_reset_network_header(skb); |
| 4438 | } |
| 4439 | |
| 4440 | /* check if bpf_xdp_adjust_tail was used. it can only "shrink" |
| 4441 | * pckt. |
| 4442 | */ |
| 4443 | off = orig_data_end - xdp->data_end; |
| 4444 | if (off != 0) { |
| 4445 | skb_set_tail_pointer(skb, xdp->data_end - xdp->data); |
| 4446 | skb->len -= off; |
| 4447 | |
| 4448 | } |
| 4449 | |
| 4450 | /* check if XDP changed eth hdr such SKB needs update */ |
| 4451 | eth = (struct ethhdr *)xdp->data; |
| 4452 | if ((orig_eth_type != eth->h_proto) || |
| 4453 | (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { |
| 4454 | __skb_push(skb, ETH_HLEN); |
| 4455 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 4456 | } |
| 4457 | |
| 4458 | switch (act) { |
| 4459 | case XDP_REDIRECT: |
| 4460 | case XDP_TX: |
| 4461 | __skb_push(skb, mac_len); |
| 4462 | break; |
| 4463 | case XDP_PASS: |
| 4464 | metalen = xdp->data - xdp->data_meta; |
| 4465 | if (metalen) |
| 4466 | skb_metadata_set(skb, metalen); |
| 4467 | break; |
| 4468 | default: |
| 4469 | bpf_warn_invalid_xdp_action(act); |
| 4470 | /* fall through */ |
| 4471 | case XDP_ABORTED: |
| 4472 | trace_xdp_exception(skb->dev, xdp_prog, act); |
| 4473 | /* fall through */ |
| 4474 | case XDP_DROP: |
| 4475 | do_drop: |
| 4476 | kfree_skb(skb); |
| 4477 | break; |
| 4478 | } |
| 4479 | |
| 4480 | return act; |
| 4481 | } |
| 4482 | |
| 4483 | /* When doing generic XDP we have to bypass the qdisc layer and the |
| 4484 | * network taps in order to match in-driver-XDP behavior. |
| 4485 | */ |
| 4486 | void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog) |
| 4487 | { |
| 4488 | struct net_device *dev = skb->dev; |
| 4489 | struct netdev_queue *txq; |
| 4490 | bool free_skb = true; |
| 4491 | int cpu, rc; |
| 4492 | |
| 4493 | txq = netdev_core_pick_tx(dev, skb, NULL); |
| 4494 | cpu = smp_processor_id(); |
| 4495 | HARD_TX_LOCK(dev, txq, cpu); |
| 4496 | if (!netif_xmit_stopped(txq)) { |
| 4497 | rc = netdev_start_xmit(skb, dev, txq, 0); |
| 4498 | if (dev_xmit_complete(rc)) |
| 4499 | free_skb = false; |
| 4500 | } |
| 4501 | HARD_TX_UNLOCK(dev, txq); |
| 4502 | if (free_skb) { |
| 4503 | trace_xdp_exception(dev, xdp_prog, XDP_TX); |
| 4504 | kfree_skb(skb); |
| 4505 | } |
| 4506 | } |
| 4507 | EXPORT_SYMBOL_GPL(generic_xdp_tx); |
| 4508 | |
| 4509 | static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); |
| 4510 | |
| 4511 | int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb) |
| 4512 | { |
| 4513 | if (xdp_prog) { |
| 4514 | struct xdp_buff xdp; |
| 4515 | u32 act; |
| 4516 | int err; |
| 4517 | |
| 4518 | act = netif_receive_generic_xdp(skb, &xdp, xdp_prog); |
| 4519 | if (act != XDP_PASS) { |
| 4520 | switch (act) { |
| 4521 | case XDP_REDIRECT: |
| 4522 | err = xdp_do_generic_redirect(skb->dev, skb, |
| 4523 | &xdp, xdp_prog); |
| 4524 | if (err) |
| 4525 | goto out_redir; |
| 4526 | break; |
| 4527 | case XDP_TX: |
| 4528 | generic_xdp_tx(skb, xdp_prog); |
| 4529 | break; |
| 4530 | } |
| 4531 | return XDP_DROP; |
| 4532 | } |
| 4533 | } |
| 4534 | return XDP_PASS; |
| 4535 | out_redir: |
| 4536 | kfree_skb(skb); |
| 4537 | return XDP_DROP; |
| 4538 | } |
| 4539 | EXPORT_SYMBOL_GPL(do_xdp_generic); |
| 4540 | |
| 4541 | static int netif_rx_internal(struct sk_buff *skb) |
| 4542 | { |
| 4543 | int ret; |
| 4544 | |
| 4545 | net_timestamp_check(READ_ONCE(netdev_tstamp_prequeue), skb); |
| 4546 | |
| 4547 | trace_netif_rx(skb); |
| 4548 | |
| 4549 | #ifdef CONFIG_RPS |
| 4550 | if (static_branch_unlikely(&rps_needed)) { |
| 4551 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
| 4552 | int cpu; |
| 4553 | |
| 4554 | preempt_disable(); |
| 4555 | rcu_read_lock(); |
| 4556 | |
| 4557 | cpu = get_rps_cpu(skb->dev, skb, &rflow); |
| 4558 | if (cpu < 0) |
| 4559 | cpu = smp_processor_id(); |
| 4560 | |
| 4561 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
| 4562 | |
| 4563 | rcu_read_unlock(); |
| 4564 | preempt_enable(); |
| 4565 | } else |
| 4566 | #endif |
| 4567 | { |
| 4568 | unsigned int qtail; |
| 4569 | |
| 4570 | ret = enqueue_to_backlog(skb, get_cpu(), &qtail); |
| 4571 | put_cpu(); |
| 4572 | } |
| 4573 | return ret; |
| 4574 | } |
| 4575 | |
| 4576 | /** |
| 4577 | * netif_rx - post buffer to the network code |
| 4578 | * @skb: buffer to post |
| 4579 | * |
| 4580 | * This function receives a packet from a device driver and queues it for |
| 4581 | * the upper (protocol) levels to process. It always succeeds. The buffer |
| 4582 | * may be dropped during processing for congestion control or by the |
| 4583 | * protocol layers. |
| 4584 | * |
| 4585 | * return values: |
| 4586 | * NET_RX_SUCCESS (no congestion) |
| 4587 | * NET_RX_DROP (packet was dropped) |
| 4588 | * |
| 4589 | */ |
| 4590 | |
| 4591 | int netif_rx(struct sk_buff *skb) |
| 4592 | { |
| 4593 | int ret; |
| 4594 | |
| 4595 | trace_netif_rx_entry(skb); |
| 4596 | |
| 4597 | ret = netif_rx_internal(skb); |
| 4598 | trace_netif_rx_exit(ret); |
| 4599 | |
| 4600 | return ret; |
| 4601 | } |
| 4602 | EXPORT_SYMBOL(netif_rx); |
| 4603 | |
| 4604 | int netif_rx_ni(struct sk_buff *skb) |
| 4605 | { |
| 4606 | int err; |
| 4607 | |
| 4608 | trace_netif_rx_ni_entry(skb); |
| 4609 | |
| 4610 | preempt_disable(); |
| 4611 | err = netif_rx_internal(skb); |
| 4612 | if (local_softirq_pending()) |
| 4613 | do_softirq(); |
| 4614 | preempt_enable(); |
| 4615 | trace_netif_rx_ni_exit(err); |
| 4616 | |
| 4617 | return err; |
| 4618 | } |
| 4619 | EXPORT_SYMBOL(netif_rx_ni); |
| 4620 | |
| 4621 | static __latent_entropy void net_tx_action(struct softirq_action *h) |
| 4622 | { |
| 4623 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
| 4624 | |
| 4625 | if (sd->completion_queue) { |
| 4626 | struct sk_buff *clist; |
| 4627 | |
| 4628 | local_irq_disable(); |
| 4629 | clist = sd->completion_queue; |
| 4630 | sd->completion_queue = NULL; |
| 4631 | local_irq_enable(); |
| 4632 | |
| 4633 | while (clist) { |
| 4634 | struct sk_buff *skb = clist; |
| 4635 | |
| 4636 | clist = clist->next; |
| 4637 | |
| 4638 | WARN_ON(refcount_read(&skb->users)); |
| 4639 | if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED)) |
| 4640 | trace_consume_skb(skb); |
| 4641 | else |
| 4642 | trace_kfree_skb(skb, net_tx_action); |
| 4643 | |
| 4644 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) |
| 4645 | __kfree_skb(skb); |
| 4646 | else |
| 4647 | __kfree_skb_defer(skb); |
| 4648 | } |
| 4649 | |
| 4650 | __kfree_skb_flush(); |
| 4651 | } |
| 4652 | |
| 4653 | if (sd->output_queue) { |
| 4654 | struct Qdisc *head; |
| 4655 | |
| 4656 | local_irq_disable(); |
| 4657 | head = sd->output_queue; |
| 4658 | sd->output_queue = NULL; |
| 4659 | sd->output_queue_tailp = &sd->output_queue; |
| 4660 | local_irq_enable(); |
| 4661 | |
| 4662 | rcu_read_lock(); |
| 4663 | |
| 4664 | while (head) { |
| 4665 | struct Qdisc *q = head; |
| 4666 | spinlock_t *root_lock = NULL; |
| 4667 | |
| 4668 | head = head->next_sched; |
| 4669 | |
| 4670 | /* We need to make sure head->next_sched is read |
| 4671 | * before clearing __QDISC_STATE_SCHED |
| 4672 | */ |
| 4673 | smp_mb__before_atomic(); |
| 4674 | |
| 4675 | if (!(q->flags & TCQ_F_NOLOCK)) { |
| 4676 | root_lock = qdisc_lock(q); |
| 4677 | spin_lock(root_lock); |
| 4678 | } else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, |
| 4679 | &q->state))) { |
| 4680 | /* There is a synchronize_net() between |
| 4681 | * STATE_DEACTIVATED flag being set and |
| 4682 | * qdisc_reset()/some_qdisc_is_busy() in |
| 4683 | * dev_deactivate(), so we can safely bail out |
| 4684 | * early here to avoid data race between |
| 4685 | * qdisc_deactivate() and some_qdisc_is_busy() |
| 4686 | * for lockless qdisc. |
| 4687 | */ |
| 4688 | clear_bit(__QDISC_STATE_SCHED, &q->state); |
| 4689 | continue; |
| 4690 | } |
| 4691 | |
| 4692 | clear_bit(__QDISC_STATE_SCHED, &q->state); |
| 4693 | qdisc_run(q); |
| 4694 | if (root_lock) |
| 4695 | spin_unlock(root_lock); |
| 4696 | } |
| 4697 | |
| 4698 | rcu_read_unlock(); |
| 4699 | } |
| 4700 | |
| 4701 | xfrm_dev_backlog(sd); |
| 4702 | } |
| 4703 | |
| 4704 | #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE) |
| 4705 | /* This hook is defined here for ATM LANE */ |
| 4706 | int (*br_fdb_test_addr_hook)(struct net_device *dev, |
| 4707 | unsigned char *addr) __read_mostly; |
| 4708 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
| 4709 | #endif |
| 4710 | |
| 4711 | static inline struct sk_buff * |
| 4712 | sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, |
| 4713 | struct net_device *orig_dev) |
| 4714 | { |
| 4715 | #ifdef CONFIG_NET_CLS_ACT |
| 4716 | struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress); |
| 4717 | struct tcf_result cl_res; |
| 4718 | |
| 4719 | /* If there's at least one ingress present somewhere (so |
| 4720 | * we get here via enabled static key), remaining devices |
| 4721 | * that are not configured with an ingress qdisc will bail |
| 4722 | * out here. |
| 4723 | */ |
| 4724 | if (!miniq) |
| 4725 | return skb; |
| 4726 | |
| 4727 | if (*pt_prev) { |
| 4728 | *ret = deliver_skb(skb, *pt_prev, orig_dev); |
| 4729 | *pt_prev = NULL; |
| 4730 | } |
| 4731 | |
| 4732 | qdisc_skb_cb(skb)->pkt_len = skb->len; |
| 4733 | skb->tc_at_ingress = 1; |
| 4734 | mini_qdisc_bstats_cpu_update(miniq, skb); |
| 4735 | |
| 4736 | switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) { |
| 4737 | case TC_ACT_OK: |
| 4738 | case TC_ACT_RECLASSIFY: |
| 4739 | skb->tc_index = TC_H_MIN(cl_res.classid); |
| 4740 | break; |
| 4741 | case TC_ACT_SHOT: |
| 4742 | mini_qdisc_qstats_cpu_drop(miniq); |
| 4743 | kfree_skb(skb); |
| 4744 | return NULL; |
| 4745 | case TC_ACT_STOLEN: |
| 4746 | case TC_ACT_QUEUED: |
| 4747 | case TC_ACT_TRAP: |
| 4748 | consume_skb(skb); |
| 4749 | return NULL; |
| 4750 | case TC_ACT_REDIRECT: |
| 4751 | /* skb_mac_header check was done by cls/act_bpf, so |
| 4752 | * we can safely push the L2 header back before |
| 4753 | * redirecting to another netdev |
| 4754 | */ |
| 4755 | __skb_push(skb, skb->mac_len); |
| 4756 | skb_do_redirect(skb); |
| 4757 | return NULL; |
| 4758 | case TC_ACT_CONSUMED: |
| 4759 | return NULL; |
| 4760 | default: |
| 4761 | break; |
| 4762 | } |
| 4763 | #endif /* CONFIG_NET_CLS_ACT */ |
| 4764 | return skb; |
| 4765 | } |
| 4766 | |
| 4767 | /** |
| 4768 | * netdev_is_rx_handler_busy - check if receive handler is registered |
| 4769 | * @dev: device to check |
| 4770 | * |
| 4771 | * Check if a receive handler is already registered for a given device. |
| 4772 | * Return true if there one. |
| 4773 | * |
| 4774 | * The caller must hold the rtnl_mutex. |
| 4775 | */ |
| 4776 | bool netdev_is_rx_handler_busy(struct net_device *dev) |
| 4777 | { |
| 4778 | ASSERT_RTNL(); |
| 4779 | return dev && rtnl_dereference(dev->rx_handler); |
| 4780 | } |
| 4781 | EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy); |
| 4782 | |
| 4783 | /** |
| 4784 | * netdev_rx_handler_register - register receive handler |
| 4785 | * @dev: device to register a handler for |
| 4786 | * @rx_handler: receive handler to register |
| 4787 | * @rx_handler_data: data pointer that is used by rx handler |
| 4788 | * |
| 4789 | * Register a receive handler for a device. This handler will then be |
| 4790 | * called from __netif_receive_skb. A negative errno code is returned |
| 4791 | * on a failure. |
| 4792 | * |
| 4793 | * The caller must hold the rtnl_mutex. |
| 4794 | * |
| 4795 | * For a general description of rx_handler, see enum rx_handler_result. |
| 4796 | */ |
| 4797 | int netdev_rx_handler_register(struct net_device *dev, |
| 4798 | rx_handler_func_t *rx_handler, |
| 4799 | void *rx_handler_data) |
| 4800 | { |
| 4801 | if (netdev_is_rx_handler_busy(dev)) |
| 4802 | return -EBUSY; |
| 4803 | |
| 4804 | if (dev->priv_flags & IFF_NO_RX_HANDLER) |
| 4805 | return -EINVAL; |
| 4806 | |
| 4807 | /* Note: rx_handler_data must be set before rx_handler */ |
| 4808 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
| 4809 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
| 4810 | |
| 4811 | return 0; |
| 4812 | } |
| 4813 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); |
| 4814 | |
| 4815 | /** |
| 4816 | * netdev_rx_handler_unregister - unregister receive handler |
| 4817 | * @dev: device to unregister a handler from |
| 4818 | * |
| 4819 | * Unregister a receive handler from a device. |
| 4820 | * |
| 4821 | * The caller must hold the rtnl_mutex. |
| 4822 | */ |
| 4823 | void netdev_rx_handler_unregister(struct net_device *dev) |
| 4824 | { |
| 4825 | |
| 4826 | ASSERT_RTNL(); |
| 4827 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
| 4828 | /* a reader seeing a non NULL rx_handler in a rcu_read_lock() |
| 4829 | * section has a guarantee to see a non NULL rx_handler_data |
| 4830 | * as well. |
| 4831 | */ |
| 4832 | synchronize_net(); |
| 4833 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); |
| 4834 | } |
| 4835 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); |
| 4836 | |
| 4837 | /* |
| 4838 | * Limit the use of PFMEMALLOC reserves to those protocols that implement |
| 4839 | * the special handling of PFMEMALLOC skbs. |
| 4840 | */ |
| 4841 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) |
| 4842 | { |
| 4843 | switch (skb->protocol) { |
| 4844 | case htons(ETH_P_ARP): |
| 4845 | case htons(ETH_P_IP): |
| 4846 | case htons(ETH_P_IPV6): |
| 4847 | case htons(ETH_P_8021Q): |
| 4848 | case htons(ETH_P_8021AD): |
| 4849 | return true; |
| 4850 | default: |
| 4851 | return false; |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev, |
| 4856 | int *ret, struct net_device *orig_dev) |
| 4857 | { |
| 4858 | #ifdef CONFIG_NETFILTER_INGRESS |
| 4859 | if (nf_hook_ingress_active(skb)) { |
| 4860 | int ingress_retval; |
| 4861 | |
| 4862 | if (*pt_prev) { |
| 4863 | *ret = deliver_skb(skb, *pt_prev, orig_dev); |
| 4864 | *pt_prev = NULL; |
| 4865 | } |
| 4866 | |
| 4867 | rcu_read_lock(); |
| 4868 | ingress_retval = nf_hook_ingress(skb); |
| 4869 | rcu_read_unlock(); |
| 4870 | return ingress_retval; |
| 4871 | } |
| 4872 | #endif /* CONFIG_NETFILTER_INGRESS */ |
| 4873 | return 0; |
| 4874 | } |
| 4875 | |
| 4876 | static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc, |
| 4877 | struct packet_type **ppt_prev) |
| 4878 | { |
| 4879 | struct packet_type *ptype, *pt_prev; |
| 4880 | rx_handler_func_t *rx_handler; |
| 4881 | struct sk_buff *skb = *pskb; |
| 4882 | struct net_device *orig_dev; |
| 4883 | bool deliver_exact = false; |
| 4884 | int ret = NET_RX_DROP; |
| 4885 | __be16 type; |
| 4886 | |
| 4887 | net_timestamp_check(!READ_ONCE(netdev_tstamp_prequeue), skb); |
| 4888 | |
| 4889 | trace_netif_receive_skb(skb); |
| 4890 | |
| 4891 | orig_dev = skb->dev; |
| 4892 | |
| 4893 | skb_reset_network_header(skb); |
| 4894 | if (!skb_transport_header_was_set(skb)) |
| 4895 | skb_reset_transport_header(skb); |
| 4896 | skb_reset_mac_len(skb); |
| 4897 | |
| 4898 | pt_prev = NULL; |
| 4899 | |
| 4900 | another_round: |
| 4901 | skb->skb_iif = skb->dev->ifindex; |
| 4902 | |
| 4903 | __this_cpu_inc(softnet_data.processed); |
| 4904 | |
| 4905 | if (static_branch_unlikely(&generic_xdp_needed_key)) { |
| 4906 | int ret2; |
| 4907 | |
| 4908 | preempt_disable(); |
| 4909 | ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb); |
| 4910 | preempt_enable(); |
| 4911 | |
| 4912 | if (ret2 != XDP_PASS) { |
| 4913 | ret = NET_RX_DROP; |
| 4914 | goto out; |
| 4915 | } |
| 4916 | skb_reset_mac_len(skb); |
| 4917 | } |
| 4918 | |
| 4919 | if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
| 4920 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { |
| 4921 | skb = skb_vlan_untag(skb); |
| 4922 | if (unlikely(!skb)) |
| 4923 | goto out; |
| 4924 | } |
| 4925 | |
| 4926 | if (skb_skip_tc_classify(skb)) |
| 4927 | goto skip_classify; |
| 4928 | |
| 4929 | if (pfmemalloc) |
| 4930 | goto skip_taps; |
| 4931 | |
| 4932 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
| 4933 | if (pt_prev) |
| 4934 | ret = deliver_skb(skb, pt_prev, orig_dev); |
| 4935 | pt_prev = ptype; |
| 4936 | } |
| 4937 | |
| 4938 | list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { |
| 4939 | if (pt_prev) |
| 4940 | ret = deliver_skb(skb, pt_prev, orig_dev); |
| 4941 | pt_prev = ptype; |
| 4942 | } |
| 4943 | |
| 4944 | skip_taps: |
| 4945 | #ifdef CONFIG_NET_INGRESS |
| 4946 | if (static_branch_unlikely(&ingress_needed_key)) { |
| 4947 | skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev); |
| 4948 | if (!skb) |
| 4949 | goto out; |
| 4950 | |
| 4951 | if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0) |
| 4952 | goto out; |
| 4953 | } |
| 4954 | #endif |
| 4955 | skb_reset_redirect(skb); |
| 4956 | skip_classify: |
| 4957 | if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) |
| 4958 | goto drop; |
| 4959 | |
| 4960 | if (skb_vlan_tag_present(skb)) { |
| 4961 | if (pt_prev) { |
| 4962 | ret = deliver_skb(skb, pt_prev, orig_dev); |
| 4963 | pt_prev = NULL; |
| 4964 | } |
| 4965 | if (vlan_do_receive(&skb)) |
| 4966 | goto another_round; |
| 4967 | else if (unlikely(!skb)) |
| 4968 | goto out; |
| 4969 | } |
| 4970 | |
| 4971 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
| 4972 | if (rx_handler) { |
| 4973 | if (pt_prev) { |
| 4974 | ret = deliver_skb(skb, pt_prev, orig_dev); |
| 4975 | pt_prev = NULL; |
| 4976 | } |
| 4977 | switch (rx_handler(&skb)) { |
| 4978 | case RX_HANDLER_CONSUMED: |
| 4979 | ret = NET_RX_SUCCESS; |
| 4980 | goto out; |
| 4981 | case RX_HANDLER_ANOTHER: |
| 4982 | goto another_round; |
| 4983 | case RX_HANDLER_EXACT: |
| 4984 | deliver_exact = true; |
| 4985 | case RX_HANDLER_PASS: |
| 4986 | break; |
| 4987 | default: |
| 4988 | BUG(); |
| 4989 | } |
| 4990 | } |
| 4991 | |
| 4992 | if (unlikely(skb_vlan_tag_present(skb))) { |
| 4993 | check_vlan_id: |
| 4994 | if (skb_vlan_tag_get_id(skb)) { |
| 4995 | /* Vlan id is non 0 and vlan_do_receive() above couldn't |
| 4996 | * find vlan device. |
| 4997 | */ |
| 4998 | skb->pkt_type = PACKET_OTHERHOST; |
| 4999 | } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
| 5000 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { |
| 5001 | /* Outer header is 802.1P with vlan 0, inner header is |
| 5002 | * 802.1Q or 802.1AD and vlan_do_receive() above could |
| 5003 | * not find vlan dev for vlan id 0. |
| 5004 | */ |
| 5005 | __vlan_hwaccel_clear_tag(skb); |
| 5006 | skb = skb_vlan_untag(skb); |
| 5007 | if (unlikely(!skb)) |
| 5008 | goto out; |
| 5009 | if (vlan_do_receive(&skb)) |
| 5010 | /* After stripping off 802.1P header with vlan 0 |
| 5011 | * vlan dev is found for inner header. |
| 5012 | */ |
| 5013 | goto another_round; |
| 5014 | else if (unlikely(!skb)) |
| 5015 | goto out; |
| 5016 | else |
| 5017 | /* We have stripped outer 802.1P vlan 0 header. |
| 5018 | * But could not find vlan dev. |
| 5019 | * check again for vlan id to set OTHERHOST. |
| 5020 | */ |
| 5021 | goto check_vlan_id; |
| 5022 | } |
| 5023 | /* Note: we might in the future use prio bits |
| 5024 | * and set skb->priority like in vlan_do_receive() |
| 5025 | * For the time being, just ignore Priority Code Point |
| 5026 | */ |
| 5027 | __vlan_hwaccel_clear_tag(skb); |
| 5028 | } |
| 5029 | |
| 5030 | type = skb->protocol; |
| 5031 | |
| 5032 | /* deliver only exact match when indicated */ |
| 5033 | if (likely(!deliver_exact)) { |
| 5034 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
| 5035 | &ptype_base[ntohs(type) & |
| 5036 | PTYPE_HASH_MASK]); |
| 5037 | } |
| 5038 | |
| 5039 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
| 5040 | &orig_dev->ptype_specific); |
| 5041 | |
| 5042 | if (unlikely(skb->dev != orig_dev)) { |
| 5043 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
| 5044 | &skb->dev->ptype_specific); |
| 5045 | } |
| 5046 | |
| 5047 | if (pt_prev) { |
| 5048 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
| 5049 | goto drop; |
| 5050 | *ppt_prev = pt_prev; |
| 5051 | } else { |
| 5052 | drop: |
| 5053 | if (!deliver_exact) |
| 5054 | atomic_long_inc(&skb->dev->rx_dropped); |
| 5055 | else |
| 5056 | atomic_long_inc(&skb->dev->rx_nohandler); |
| 5057 | kfree_skb(skb); |
| 5058 | /* Jamal, now you will not able to escape explaining |
| 5059 | * me how you were going to use this. :-) |
| 5060 | */ |
| 5061 | ret = NET_RX_DROP; |
| 5062 | } |
| 5063 | |
| 5064 | out: |
| 5065 | /* The invariant here is that if *ppt_prev is not NULL |
| 5066 | * then skb should also be non-NULL. |
| 5067 | * |
| 5068 | * Apparently *ppt_prev assignment above holds this invariant due to |
| 5069 | * skb dereferencing near it. |
| 5070 | */ |
| 5071 | *pskb = skb; |
| 5072 | return ret; |
| 5073 | } |
| 5074 | |
| 5075 | static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc) |
| 5076 | { |
| 5077 | struct net_device *orig_dev = skb->dev; |
| 5078 | struct packet_type *pt_prev = NULL; |
| 5079 | int ret; |
| 5080 | |
| 5081 | ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
| 5082 | if (pt_prev) |
| 5083 | ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb, |
| 5084 | skb->dev, pt_prev, orig_dev); |
| 5085 | return ret; |
| 5086 | } |
| 5087 | |
| 5088 | /** |
| 5089 | * netif_receive_skb_core - special purpose version of netif_receive_skb |
| 5090 | * @skb: buffer to process |
| 5091 | * |
| 5092 | * More direct receive version of netif_receive_skb(). It should |
| 5093 | * only be used by callers that have a need to skip RPS and Generic XDP. |
| 5094 | * Caller must also take care of handling if (page_is_)pfmemalloc. |
| 5095 | * |
| 5096 | * This function may only be called from softirq context and interrupts |
| 5097 | * should be enabled. |
| 5098 | * |
| 5099 | * Return values (usually ignored): |
| 5100 | * NET_RX_SUCCESS: no congestion |
| 5101 | * NET_RX_DROP: packet was dropped |
| 5102 | */ |
| 5103 | int netif_receive_skb_core(struct sk_buff *skb) |
| 5104 | { |
| 5105 | int ret; |
| 5106 | |
| 5107 | rcu_read_lock(); |
| 5108 | ret = __netif_receive_skb_one_core(skb, false); |
| 5109 | rcu_read_unlock(); |
| 5110 | |
| 5111 | return ret; |
| 5112 | } |
| 5113 | EXPORT_SYMBOL(netif_receive_skb_core); |
| 5114 | |
| 5115 | static inline void __netif_receive_skb_list_ptype(struct list_head *head, |
| 5116 | struct packet_type *pt_prev, |
| 5117 | struct net_device *orig_dev) |
| 5118 | { |
| 5119 | struct sk_buff *skb, *next; |
| 5120 | |
| 5121 | if (!pt_prev) |
| 5122 | return; |
| 5123 | if (list_empty(head)) |
| 5124 | return; |
| 5125 | if (pt_prev->list_func != NULL) |
| 5126 | INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv, |
| 5127 | ip_list_rcv, head, pt_prev, orig_dev); |
| 5128 | else |
| 5129 | list_for_each_entry_safe(skb, next, head, list) { |
| 5130 | skb_list_del_init(skb); |
| 5131 | pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
| 5132 | } |
| 5133 | } |
| 5134 | |
| 5135 | static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc) |
| 5136 | { |
| 5137 | /* Fast-path assumptions: |
| 5138 | * - There is no RX handler. |
| 5139 | * - Only one packet_type matches. |
| 5140 | * If either of these fails, we will end up doing some per-packet |
| 5141 | * processing in-line, then handling the 'last ptype' for the whole |
| 5142 | * sublist. This can't cause out-of-order delivery to any single ptype, |
| 5143 | * because the 'last ptype' must be constant across the sublist, and all |
| 5144 | * other ptypes are handled per-packet. |
| 5145 | */ |
| 5146 | /* Current (common) ptype of sublist */ |
| 5147 | struct packet_type *pt_curr = NULL; |
| 5148 | /* Current (common) orig_dev of sublist */ |
| 5149 | struct net_device *od_curr = NULL; |
| 5150 | struct list_head sublist; |
| 5151 | struct sk_buff *skb, *next; |
| 5152 | |
| 5153 | INIT_LIST_HEAD(&sublist); |
| 5154 | list_for_each_entry_safe(skb, next, head, list) { |
| 5155 | struct net_device *orig_dev = skb->dev; |
| 5156 | struct packet_type *pt_prev = NULL; |
| 5157 | |
| 5158 | skb_list_del_init(skb); |
| 5159 | __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev); |
| 5160 | if (!pt_prev) |
| 5161 | continue; |
| 5162 | if (pt_curr != pt_prev || od_curr != orig_dev) { |
| 5163 | /* dispatch old sublist */ |
| 5164 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
| 5165 | /* start new sublist */ |
| 5166 | INIT_LIST_HEAD(&sublist); |
| 5167 | pt_curr = pt_prev; |
| 5168 | od_curr = orig_dev; |
| 5169 | } |
| 5170 | list_add_tail(&skb->list, &sublist); |
| 5171 | } |
| 5172 | |
| 5173 | /* dispatch final sublist */ |
| 5174 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
| 5175 | } |
| 5176 | |
| 5177 | static int __netif_receive_skb(struct sk_buff *skb) |
| 5178 | { |
| 5179 | int ret; |
| 5180 | |
| 5181 | /* if fastpath forwarded it, return... */ |
| 5182 | if (netif_rx_fastpath(skb)) |
| 5183 | return NET_RX_SUCCESS; |
| 5184 | |
| 5185 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { |
| 5186 | unsigned int noreclaim_flag; |
| 5187 | |
| 5188 | /* |
| 5189 | * PFMEMALLOC skbs are special, they should |
| 5190 | * - be delivered to SOCK_MEMALLOC sockets only |
| 5191 | * - stay away from userspace |
| 5192 | * - have bounded memory usage |
| 5193 | * |
| 5194 | * Use PF_MEMALLOC as this saves us from propagating the allocation |
| 5195 | * context down to all allocation sites. |
| 5196 | */ |
| 5197 | noreclaim_flag = memalloc_noreclaim_save(); |
| 5198 | ret = __netif_receive_skb_one_core(skb, true); |
| 5199 | memalloc_noreclaim_restore(noreclaim_flag); |
| 5200 | } else |
| 5201 | ret = __netif_receive_skb_one_core(skb, false); |
| 5202 | |
| 5203 | return ret; |
| 5204 | } |
| 5205 | |
| 5206 | static void __netif_receive_skb_list(struct list_head *head) |
| 5207 | { |
| 5208 | unsigned long noreclaim_flag = 0; |
| 5209 | struct sk_buff *skb, *next; |
| 5210 | bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */ |
| 5211 | |
| 5212 | list_for_each_entry_safe(skb, next, head, list) { |
| 5213 | if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) { |
| 5214 | struct list_head sublist; |
| 5215 | |
| 5216 | /* Handle the previous sublist */ |
| 5217 | list_cut_before(&sublist, head, &skb->list); |
| 5218 | if (!list_empty(&sublist)) |
| 5219 | __netif_receive_skb_list_core(&sublist, pfmemalloc); |
| 5220 | pfmemalloc = !pfmemalloc; |
| 5221 | /* See comments in __netif_receive_skb */ |
| 5222 | if (pfmemalloc) |
| 5223 | noreclaim_flag = memalloc_noreclaim_save(); |
| 5224 | else |
| 5225 | memalloc_noreclaim_restore(noreclaim_flag); |
| 5226 | } |
| 5227 | } |
| 5228 | /* Handle the remaining sublist */ |
| 5229 | if (!list_empty(head)) |
| 5230 | __netif_receive_skb_list_core(head, pfmemalloc); |
| 5231 | /* Restore pflags */ |
| 5232 | if (pfmemalloc) |
| 5233 | memalloc_noreclaim_restore(noreclaim_flag); |
| 5234 | } |
| 5235 | |
| 5236 | static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) |
| 5237 | { |
| 5238 | struct bpf_prog *old = rtnl_dereference(dev->xdp_prog); |
| 5239 | struct bpf_prog *new = xdp->prog; |
| 5240 | int ret = 0; |
| 5241 | |
| 5242 | switch (xdp->command) { |
| 5243 | case XDP_SETUP_PROG: |
| 5244 | rcu_assign_pointer(dev->xdp_prog, new); |
| 5245 | if (old) |
| 5246 | bpf_prog_put(old); |
| 5247 | |
| 5248 | if (old && !new) { |
| 5249 | static_branch_dec(&generic_xdp_needed_key); |
| 5250 | } else if (new && !old) { |
| 5251 | static_branch_inc(&generic_xdp_needed_key); |
| 5252 | dev_disable_lro(dev); |
| 5253 | dev_disable_gro_hw(dev); |
| 5254 | } |
| 5255 | break; |
| 5256 | |
| 5257 | case XDP_QUERY_PROG: |
| 5258 | xdp->prog_id = old ? old->aux->id : 0; |
| 5259 | break; |
| 5260 | |
| 5261 | default: |
| 5262 | ret = -EINVAL; |
| 5263 | break; |
| 5264 | } |
| 5265 | |
| 5266 | return ret; |
| 5267 | } |
| 5268 | |
| 5269 | static int netif_receive_skb_internal(struct sk_buff *skb) |
| 5270 | { |
| 5271 | int ret; |
| 5272 | |
| 5273 | net_timestamp_check(READ_ONCE(netdev_tstamp_prequeue), skb); |
| 5274 | |
| 5275 | if (skb_defer_rx_timestamp(skb)) |
| 5276 | return NET_RX_SUCCESS; |
| 5277 | |
| 5278 | rcu_read_lock(); |
| 5279 | #ifdef CONFIG_RPS |
| 5280 | if (static_branch_unlikely(&rps_needed)) { |
| 5281 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
| 5282 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); |
| 5283 | |
| 5284 | if (cpu >= 0) { |
| 5285 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
| 5286 | rcu_read_unlock(); |
| 5287 | return ret; |
| 5288 | } |
| 5289 | } |
| 5290 | #endif |
| 5291 | ret = __netif_receive_skb(skb); |
| 5292 | rcu_read_unlock(); |
| 5293 | return ret; |
| 5294 | } |
| 5295 | |
| 5296 | static void netif_receive_skb_list_internal(struct list_head *head) |
| 5297 | { |
| 5298 | struct sk_buff *skb, *next; |
| 5299 | struct list_head sublist; |
| 5300 | |
| 5301 | INIT_LIST_HEAD(&sublist); |
| 5302 | list_for_each_entry_safe(skb, next, head, list) { |
| 5303 | net_timestamp_check(READ_ONCE(netdev_tstamp_prequeue), skb); |
| 5304 | skb_list_del_init(skb); |
| 5305 | if (!skb_defer_rx_timestamp(skb)) |
| 5306 | list_add_tail(&skb->list, &sublist); |
| 5307 | } |
| 5308 | list_splice_init(&sublist, head); |
| 5309 | |
| 5310 | rcu_read_lock(); |
| 5311 | #ifdef CONFIG_RPS |
| 5312 | if (static_branch_unlikely(&rps_needed)) { |
| 5313 | list_for_each_entry_safe(skb, next, head, list) { |
| 5314 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
| 5315 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); |
| 5316 | |
| 5317 | if (cpu >= 0) { |
| 5318 | /* Will be handled, remove from list */ |
| 5319 | skb_list_del_init(skb); |
| 5320 | enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
| 5321 | } |
| 5322 | } |
| 5323 | } |
| 5324 | #endif |
| 5325 | __netif_receive_skb_list(head); |
| 5326 | rcu_read_unlock(); |
| 5327 | } |
| 5328 | |
| 5329 | /** |
| 5330 | * netif_receive_skb - process receive buffer from network |
| 5331 | * @skb: buffer to process |
| 5332 | * |
| 5333 | * netif_receive_skb() is the main receive data processing function. |
| 5334 | * It always succeeds. The buffer may be dropped during processing |
| 5335 | * for congestion control or by the protocol layers. |
| 5336 | * |
| 5337 | * This function may only be called from softirq context and interrupts |
| 5338 | * should be enabled. |
| 5339 | * |
| 5340 | * Return values (usually ignored): |
| 5341 | * NET_RX_SUCCESS: no congestion |
| 5342 | * NET_RX_DROP: packet was dropped |
| 5343 | */ |
| 5344 | int netif_receive_skb(struct sk_buff *skb) |
| 5345 | { |
| 5346 | int ret; |
| 5347 | |
| 5348 | trace_netif_receive_skb_entry(skb); |
| 5349 | |
| 5350 | ret = netif_receive_skb_internal(skb); |
| 5351 | trace_netif_receive_skb_exit(ret); |
| 5352 | |
| 5353 | return ret; |
| 5354 | } |
| 5355 | EXPORT_SYMBOL(netif_receive_skb); |
| 5356 | |
| 5357 | /** |
| 5358 | * netif_receive_skb_list - process many receive buffers from network |
| 5359 | * @head: list of skbs to process. |
| 5360 | * |
| 5361 | * Since return value of netif_receive_skb() is normally ignored, and |
| 5362 | * wouldn't be meaningful for a list, this function returns void. |
| 5363 | * |
| 5364 | * This function may only be called from softirq context and interrupts |
| 5365 | * should be enabled. |
| 5366 | */ |
| 5367 | void netif_receive_skb_list(struct list_head *head) |
| 5368 | { |
| 5369 | struct sk_buff *skb; |
| 5370 | |
| 5371 | if (list_empty(head)) |
| 5372 | return; |
| 5373 | if (trace_netif_receive_skb_list_entry_enabled()) { |
| 5374 | list_for_each_entry(skb, head, list) |
| 5375 | trace_netif_receive_skb_list_entry(skb); |
| 5376 | } |
| 5377 | netif_receive_skb_list_internal(head); |
| 5378 | trace_netif_receive_skb_list_exit(0); |
| 5379 | } |
| 5380 | EXPORT_SYMBOL(netif_receive_skb_list); |
| 5381 | |
| 5382 | DEFINE_PER_CPU(struct work_struct, flush_works); |
| 5383 | |
| 5384 | /* Network device is going away, flush any packets still pending */ |
| 5385 | static void flush_backlog(struct work_struct *work) |
| 5386 | { |
| 5387 | struct sk_buff *skb, *tmp; |
| 5388 | struct softnet_data *sd; |
| 5389 | |
| 5390 | local_bh_disable(); |
| 5391 | sd = this_cpu_ptr(&softnet_data); |
| 5392 | |
| 5393 | local_irq_disable(); |
| 5394 | rps_lock(sd); |
| 5395 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
| 5396 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
| 5397 | __skb_unlink(skb, &sd->input_pkt_queue); |
| 5398 | dev_kfree_skb_irq(skb); |
| 5399 | input_queue_head_incr(sd); |
| 5400 | } |
| 5401 | } |
| 5402 | rps_unlock(sd); |
| 5403 | local_irq_enable(); |
| 5404 | |
| 5405 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { |
| 5406 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
| 5407 | __skb_unlink(skb, &sd->process_queue); |
| 5408 | kfree_skb(skb); |
| 5409 | input_queue_head_incr(sd); |
| 5410 | } |
| 5411 | } |
| 5412 | local_bh_enable(); |
| 5413 | } |
| 5414 | |
| 5415 | static void flush_all_backlogs(void) |
| 5416 | { |
| 5417 | unsigned int cpu; |
| 5418 | |
| 5419 | get_online_cpus(); |
| 5420 | |
| 5421 | for_each_online_cpu(cpu) |
| 5422 | queue_work_on(cpu, system_highpri_wq, |
| 5423 | per_cpu_ptr(&flush_works, cpu)); |
| 5424 | |
| 5425 | for_each_online_cpu(cpu) |
| 5426 | flush_work(per_cpu_ptr(&flush_works, cpu)); |
| 5427 | |
| 5428 | put_online_cpus(); |
| 5429 | } |
| 5430 | |
| 5431 | /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */ |
| 5432 | static void gro_normal_list(struct napi_struct *napi) |
| 5433 | { |
| 5434 | if (!napi->rx_count) |
| 5435 | return; |
| 5436 | netif_receive_skb_list_internal(&napi->rx_list); |
| 5437 | INIT_LIST_HEAD(&napi->rx_list); |
| 5438 | napi->rx_count = 0; |
| 5439 | } |
| 5440 | |
| 5441 | /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded, |
| 5442 | * pass the whole batch up to the stack. |
| 5443 | */ |
| 5444 | static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb, int segs) |
| 5445 | { |
| 5446 | list_add_tail(&skb->list, &napi->rx_list); |
| 5447 | napi->rx_count += segs; |
| 5448 | if (napi->rx_count >= gro_normal_batch) |
| 5449 | gro_normal_list(napi); |
| 5450 | } |
| 5451 | |
| 5452 | INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int)); |
| 5453 | INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int)); |
| 5454 | static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb) |
| 5455 | { |
| 5456 | struct packet_offload *ptype; |
| 5457 | __be16 type = skb->protocol; |
| 5458 | struct list_head *head = &offload_base; |
| 5459 | int err = -ENOENT; |
| 5460 | |
| 5461 | BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb)); |
| 5462 | |
| 5463 | if (NAPI_GRO_CB(skb)->count == 1) { |
| 5464 | skb_shinfo(skb)->gso_size = 0; |
| 5465 | goto out; |
| 5466 | } |
| 5467 | |
| 5468 | rcu_read_lock(); |
| 5469 | list_for_each_entry_rcu(ptype, head, list) { |
| 5470 | if (ptype->type != type || !ptype->callbacks.gro_complete) |
| 5471 | continue; |
| 5472 | |
| 5473 | err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete, |
| 5474 | ipv6_gro_complete, inet_gro_complete, |
| 5475 | skb, 0); |
| 5476 | break; |
| 5477 | } |
| 5478 | rcu_read_unlock(); |
| 5479 | |
| 5480 | if (err) { |
| 5481 | WARN_ON(&ptype->list == head); |
| 5482 | kfree_skb(skb); |
| 5483 | return NET_RX_SUCCESS; |
| 5484 | } |
| 5485 | |
| 5486 | out: |
| 5487 | gro_normal_one(napi, skb, NAPI_GRO_CB(skb)->count); |
| 5488 | return NET_RX_SUCCESS; |
| 5489 | } |
| 5490 | |
| 5491 | static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index, |
| 5492 | bool flush_old) |
| 5493 | { |
| 5494 | struct list_head *head = &napi->gro_hash[index].list; |
| 5495 | struct sk_buff *skb, *p; |
| 5496 | |
| 5497 | list_for_each_entry_safe_reverse(skb, p, head, list) { |
| 5498 | if (flush_old && NAPI_GRO_CB(skb)->age == jiffies) |
| 5499 | return; |
| 5500 | skb_list_del_init(skb); |
| 5501 | napi_gro_complete(napi, skb); |
| 5502 | napi->gro_hash[index].count--; |
| 5503 | } |
| 5504 | |
| 5505 | if (!napi->gro_hash[index].count) |
| 5506 | __clear_bit(index, &napi->gro_bitmask); |
| 5507 | } |
| 5508 | |
| 5509 | /* napi->gro_hash[].list contains packets ordered by age. |
| 5510 | * youngest packets at the head of it. |
| 5511 | * Complete skbs in reverse order to reduce latencies. |
| 5512 | */ |
| 5513 | void napi_gro_flush(struct napi_struct *napi, bool flush_old) |
| 5514 | { |
| 5515 | unsigned long bitmask = napi->gro_bitmask; |
| 5516 | unsigned int i, base = ~0U; |
| 5517 | |
| 5518 | while ((i = ffs(bitmask)) != 0) { |
| 5519 | bitmask >>= i; |
| 5520 | base += i; |
| 5521 | __napi_gro_flush_chain(napi, base, flush_old); |
| 5522 | } |
| 5523 | } |
| 5524 | EXPORT_SYMBOL(napi_gro_flush); |
| 5525 | |
| 5526 | static struct list_head *gro_list_prepare(struct napi_struct *napi, |
| 5527 | struct sk_buff *skb) |
| 5528 | { |
| 5529 | unsigned int maclen = skb->dev->hard_header_len; |
| 5530 | u32 hash = skb_get_hash_raw(skb); |
| 5531 | struct list_head *head; |
| 5532 | struct sk_buff *p; |
| 5533 | |
| 5534 | head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list; |
| 5535 | list_for_each_entry(p, head, list) { |
| 5536 | unsigned long diffs; |
| 5537 | |
| 5538 | NAPI_GRO_CB(p)->flush = 0; |
| 5539 | |
| 5540 | if (hash != skb_get_hash_raw(p)) { |
| 5541 | NAPI_GRO_CB(p)->same_flow = 0; |
| 5542 | continue; |
| 5543 | } |
| 5544 | |
| 5545 | diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev; |
| 5546 | diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb); |
| 5547 | if (skb_vlan_tag_present(p)) |
| 5548 | diffs |= p->vlan_tci ^ skb->vlan_tci; |
| 5549 | diffs |= skb_metadata_dst_cmp(p, skb); |
| 5550 | diffs |= skb_metadata_differs(p, skb); |
| 5551 | if (maclen == ETH_HLEN) |
| 5552 | diffs |= compare_ether_header(skb_mac_header(p), |
| 5553 | skb_mac_header(skb)); |
| 5554 | else if (!diffs) |
| 5555 | diffs = memcmp(skb_mac_header(p), |
| 5556 | skb_mac_header(skb), |
| 5557 | maclen); |
| 5558 | NAPI_GRO_CB(p)->same_flow = !diffs; |
| 5559 | } |
| 5560 | |
| 5561 | return head; |
| 5562 | } |
| 5563 | |
| 5564 | static inline void skb_gro_reset_offset(struct sk_buff *skb, u32 nhoff) |
| 5565 | { |
| 5566 | const struct skb_shared_info *pinfo = skb_shinfo(skb); |
| 5567 | const skb_frag_t *frag0 = &pinfo->frags[0]; |
| 5568 | |
| 5569 | NAPI_GRO_CB(skb)->data_offset = 0; |
| 5570 | NAPI_GRO_CB(skb)->frag0 = NULL; |
| 5571 | NAPI_GRO_CB(skb)->frag0_len = 0; |
| 5572 | |
| 5573 | if (!skb_headlen(skb) && pinfo->nr_frags && |
| 5574 | !PageHighMem(skb_frag_page(frag0)) && |
| 5575 | (!NET_IP_ALIGN || !((skb_frag_off(frag0) + nhoff) & 3))) { |
| 5576 | NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0); |
| 5577 | NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int, |
| 5578 | skb_frag_size(frag0), |
| 5579 | skb->end - skb->tail); |
| 5580 | } |
| 5581 | } |
| 5582 | |
| 5583 | static void gro_pull_from_frag0(struct sk_buff *skb, int grow) |
| 5584 | { |
| 5585 | struct skb_shared_info *pinfo = skb_shinfo(skb); |
| 5586 | |
| 5587 | BUG_ON(skb->end - skb->tail < grow); |
| 5588 | |
| 5589 | memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow); |
| 5590 | |
| 5591 | skb->data_len -= grow; |
| 5592 | skb->tail += grow; |
| 5593 | |
| 5594 | skb_frag_off_add(&pinfo->frags[0], grow); |
| 5595 | skb_frag_size_sub(&pinfo->frags[0], grow); |
| 5596 | |
| 5597 | if (unlikely(!skb_frag_size(&pinfo->frags[0]))) { |
| 5598 | skb_frag_unref(skb, 0); |
| 5599 | memmove(pinfo->frags, pinfo->frags + 1, |
| 5600 | --pinfo->nr_frags * sizeof(pinfo->frags[0])); |
| 5601 | } |
| 5602 | } |
| 5603 | |
| 5604 | static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head) |
| 5605 | { |
| 5606 | struct sk_buff *oldest; |
| 5607 | |
| 5608 | oldest = list_last_entry(head, struct sk_buff, list); |
| 5609 | |
| 5610 | /* We are called with head length >= MAX_GRO_SKBS, so this is |
| 5611 | * impossible. |
| 5612 | */ |
| 5613 | if (WARN_ON_ONCE(!oldest)) |
| 5614 | return; |
| 5615 | |
| 5616 | /* Do not adjust napi->gro_hash[].count, caller is adding a new |
| 5617 | * SKB to the chain. |
| 5618 | */ |
| 5619 | skb_list_del_init(oldest); |
| 5620 | napi_gro_complete(napi, oldest); |
| 5621 | } |
| 5622 | |
| 5623 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *, |
| 5624 | struct sk_buff *)); |
| 5625 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *, |
| 5626 | struct sk_buff *)); |
| 5627 | static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
| 5628 | { |
| 5629 | u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1); |
| 5630 | struct list_head *head = &offload_base; |
| 5631 | struct packet_offload *ptype; |
| 5632 | __be16 type = skb->protocol; |
| 5633 | struct list_head *gro_head; |
| 5634 | struct sk_buff *pp = NULL; |
| 5635 | enum gro_result ret; |
| 5636 | int same_flow; |
| 5637 | int grow; |
| 5638 | |
| 5639 | if (skb->gro_skip) |
| 5640 | goto normal; |
| 5641 | |
| 5642 | if (netif_elide_gro(skb->dev)) |
| 5643 | goto normal; |
| 5644 | |
| 5645 | gro_head = gro_list_prepare(napi, skb); |
| 5646 | |
| 5647 | rcu_read_lock(); |
| 5648 | list_for_each_entry_rcu(ptype, head, list) { |
| 5649 | if (ptype->type != type || !ptype->callbacks.gro_receive) |
| 5650 | continue; |
| 5651 | |
| 5652 | skb_set_network_header(skb, skb_gro_offset(skb)); |
| 5653 | skb_reset_mac_len(skb); |
| 5654 | NAPI_GRO_CB(skb)->same_flow = 0; |
| 5655 | NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb); |
| 5656 | NAPI_GRO_CB(skb)->free = 0; |
| 5657 | NAPI_GRO_CB(skb)->encap_mark = 0; |
| 5658 | NAPI_GRO_CB(skb)->recursion_counter = 0; |
| 5659 | NAPI_GRO_CB(skb)->is_fou = 0; |
| 5660 | NAPI_GRO_CB(skb)->is_atomic = 1; |
| 5661 | NAPI_GRO_CB(skb)->gro_remcsum_start = 0; |
| 5662 | |
| 5663 | /* Setup for GRO checksum validation */ |
| 5664 | switch (skb->ip_summed) { |
| 5665 | case CHECKSUM_COMPLETE: |
| 5666 | NAPI_GRO_CB(skb)->csum = skb->csum; |
| 5667 | NAPI_GRO_CB(skb)->csum_valid = 1; |
| 5668 | NAPI_GRO_CB(skb)->csum_cnt = 0; |
| 5669 | break; |
| 5670 | case CHECKSUM_UNNECESSARY: |
| 5671 | NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1; |
| 5672 | NAPI_GRO_CB(skb)->csum_valid = 0; |
| 5673 | break; |
| 5674 | default: |
| 5675 | NAPI_GRO_CB(skb)->csum_cnt = 0; |
| 5676 | NAPI_GRO_CB(skb)->csum_valid = 0; |
| 5677 | } |
| 5678 | |
| 5679 | pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive, |
| 5680 | ipv6_gro_receive, inet_gro_receive, |
| 5681 | gro_head, skb); |
| 5682 | break; |
| 5683 | } |
| 5684 | rcu_read_unlock(); |
| 5685 | |
| 5686 | if (&ptype->list == head) |
| 5687 | goto normal; |
| 5688 | |
| 5689 | if (IS_ERR(pp) && PTR_ERR(pp) == -EINPROGRESS) { |
| 5690 | ret = GRO_CONSUMED; |
| 5691 | goto ok; |
| 5692 | } |
| 5693 | |
| 5694 | same_flow = NAPI_GRO_CB(skb)->same_flow; |
| 5695 | ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED; |
| 5696 | |
| 5697 | if (pp) { |
| 5698 | skb_list_del_init(pp); |
| 5699 | napi_gro_complete(napi, pp); |
| 5700 | napi->gro_hash[hash].count--; |
| 5701 | } |
| 5702 | |
| 5703 | if (same_flow) |
| 5704 | goto ok; |
| 5705 | |
| 5706 | if (NAPI_GRO_CB(skb)->flush) |
| 5707 | goto normal; |
| 5708 | |
| 5709 | if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) { |
| 5710 | gro_flush_oldest(napi, gro_head); |
| 5711 | } else { |
| 5712 | napi->gro_hash[hash].count++; |
| 5713 | } |
| 5714 | NAPI_GRO_CB(skb)->count = 1; |
| 5715 | NAPI_GRO_CB(skb)->age = jiffies; |
| 5716 | NAPI_GRO_CB(skb)->last = skb; |
| 5717 | skb_shinfo(skb)->gso_size = skb_gro_len(skb); |
| 5718 | list_add(&skb->list, gro_head); |
| 5719 | ret = GRO_HELD; |
| 5720 | |
| 5721 | pull: |
| 5722 | grow = skb_gro_offset(skb) - skb_headlen(skb); |
| 5723 | if (grow > 0) |
| 5724 | gro_pull_from_frag0(skb, grow); |
| 5725 | ok: |
| 5726 | if (napi->gro_hash[hash].count) { |
| 5727 | if (!test_bit(hash, &napi->gro_bitmask)) |
| 5728 | __set_bit(hash, &napi->gro_bitmask); |
| 5729 | } else if (test_bit(hash, &napi->gro_bitmask)) { |
| 5730 | __clear_bit(hash, &napi->gro_bitmask); |
| 5731 | } |
| 5732 | |
| 5733 | return ret; |
| 5734 | |
| 5735 | normal: |
| 5736 | ret = GRO_NORMAL; |
| 5737 | goto pull; |
| 5738 | } |
| 5739 | |
| 5740 | struct packet_offload *gro_find_receive_by_type(__be16 type) |
| 5741 | { |
| 5742 | struct list_head *offload_head = &offload_base; |
| 5743 | struct packet_offload *ptype; |
| 5744 | |
| 5745 | list_for_each_entry_rcu(ptype, offload_head, list) { |
| 5746 | if (ptype->type != type || !ptype->callbacks.gro_receive) |
| 5747 | continue; |
| 5748 | return ptype; |
| 5749 | } |
| 5750 | return NULL; |
| 5751 | } |
| 5752 | EXPORT_SYMBOL(gro_find_receive_by_type); |
| 5753 | |
| 5754 | struct packet_offload *gro_find_complete_by_type(__be16 type) |
| 5755 | { |
| 5756 | struct list_head *offload_head = &offload_base; |
| 5757 | struct packet_offload *ptype; |
| 5758 | |
| 5759 | list_for_each_entry_rcu(ptype, offload_head, list) { |
| 5760 | if (ptype->type != type || !ptype->callbacks.gro_complete) |
| 5761 | continue; |
| 5762 | return ptype; |
| 5763 | } |
| 5764 | return NULL; |
| 5765 | } |
| 5766 | EXPORT_SYMBOL(gro_find_complete_by_type); |
| 5767 | |
| 5768 | static void napi_skb_free_stolen_head(struct sk_buff *skb) |
| 5769 | { |
| 5770 | skb_dst_drop(skb); |
| 5771 | skb_ext_put(skb); |
| 5772 | kmem_cache_free(skbuff_head_cache, skb); |
| 5773 | } |
| 5774 | |
| 5775 | static gro_result_t napi_skb_finish(struct napi_struct *napi, |
| 5776 | struct sk_buff *skb, |
| 5777 | gro_result_t ret) |
| 5778 | { |
| 5779 | switch (ret) { |
| 5780 | case GRO_NORMAL: |
| 5781 | gro_normal_one(napi, skb, 1); |
| 5782 | break; |
| 5783 | |
| 5784 | case GRO_DROP: |
| 5785 | kfree_skb(skb); |
| 5786 | break; |
| 5787 | |
| 5788 | case GRO_MERGED_FREE: |
| 5789 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) |
| 5790 | napi_skb_free_stolen_head(skb); |
| 5791 | else |
| 5792 | __kfree_skb(skb); |
| 5793 | break; |
| 5794 | |
| 5795 | case GRO_HELD: |
| 5796 | case GRO_MERGED: |
| 5797 | case GRO_CONSUMED: |
| 5798 | break; |
| 5799 | } |
| 5800 | |
| 5801 | return ret; |
| 5802 | } |
| 5803 | |
| 5804 | gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
| 5805 | { |
| 5806 | gro_result_t ret; |
| 5807 | |
| 5808 | /* if fastpath forwarded it, return... */ |
| 5809 | if (netif_rx_fastpath(skb)) |
| 5810 | return GRO_DROP; |
| 5811 | |
| 5812 | skb_mark_napi_id(skb, napi); |
| 5813 | trace_napi_gro_receive_entry(skb); |
| 5814 | |
| 5815 | skb_gro_reset_offset(skb, 0); |
| 5816 | |
| 5817 | ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb)); |
| 5818 | trace_napi_gro_receive_exit(ret); |
| 5819 | |
| 5820 | return ret; |
| 5821 | } |
| 5822 | EXPORT_SYMBOL(napi_gro_receive); |
| 5823 | |
| 5824 | static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) |
| 5825 | { |
| 5826 | if (unlikely(skb->pfmemalloc)) { |
| 5827 | consume_skb(skb); |
| 5828 | return; |
| 5829 | } |
| 5830 | __skb_pull(skb, skb_headlen(skb)); |
| 5831 | /* restore the reserve we had after netdev_alloc_skb_ip_align() */ |
| 5832 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb)); |
| 5833 | __vlan_hwaccel_clear_tag(skb); |
| 5834 | skb->dev = napi->dev; |
| 5835 | skb->skb_iif = 0; |
| 5836 | |
| 5837 | /* eth_type_trans() assumes pkt_type is PACKET_HOST */ |
| 5838 | skb->pkt_type = PACKET_HOST; |
| 5839 | |
| 5840 | skb->encapsulation = 0; |
| 5841 | skb_shinfo(skb)->gso_type = 0; |
| 5842 | skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); |
| 5843 | skb_ext_reset(skb); |
| 5844 | |
| 5845 | napi->skb = skb; |
| 5846 | } |
| 5847 | |
| 5848 | struct sk_buff *napi_get_frags(struct napi_struct *napi) |
| 5849 | { |
| 5850 | struct sk_buff *skb = napi->skb; |
| 5851 | |
| 5852 | if (!skb) { |
| 5853 | skb = napi_alloc_skb(napi, GRO_MAX_HEAD); |
| 5854 | if (skb) { |
| 5855 | napi->skb = skb; |
| 5856 | skb_mark_napi_id(skb, napi); |
| 5857 | } |
| 5858 | } |
| 5859 | return skb; |
| 5860 | } |
| 5861 | EXPORT_SYMBOL(napi_get_frags); |
| 5862 | |
| 5863 | static gro_result_t napi_frags_finish(struct napi_struct *napi, |
| 5864 | struct sk_buff *skb, |
| 5865 | gro_result_t ret) |
| 5866 | { |
| 5867 | switch (ret) { |
| 5868 | case GRO_NORMAL: |
| 5869 | case GRO_HELD: |
| 5870 | __skb_push(skb, ETH_HLEN); |
| 5871 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 5872 | if (ret == GRO_NORMAL) |
| 5873 | gro_normal_one(napi, skb, 1); |
| 5874 | break; |
| 5875 | |
| 5876 | case GRO_DROP: |
| 5877 | napi_reuse_skb(napi, skb); |
| 5878 | break; |
| 5879 | |
| 5880 | case GRO_MERGED_FREE: |
| 5881 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) |
| 5882 | napi_skb_free_stolen_head(skb); |
| 5883 | else |
| 5884 | napi_reuse_skb(napi, skb); |
| 5885 | break; |
| 5886 | |
| 5887 | case GRO_MERGED: |
| 5888 | case GRO_CONSUMED: |
| 5889 | break; |
| 5890 | } |
| 5891 | |
| 5892 | return ret; |
| 5893 | } |
| 5894 | |
| 5895 | /* Upper GRO stack assumes network header starts at gro_offset=0 |
| 5896 | * Drivers could call both napi_gro_frags() and napi_gro_receive() |
| 5897 | * We copy ethernet header into skb->data to have a common layout. |
| 5898 | */ |
| 5899 | static struct sk_buff *napi_frags_skb(struct napi_struct *napi) |
| 5900 | { |
| 5901 | struct sk_buff *skb = napi->skb; |
| 5902 | const struct ethhdr *eth; |
| 5903 | unsigned int hlen = sizeof(*eth); |
| 5904 | |
| 5905 | napi->skb = NULL; |
| 5906 | |
| 5907 | skb_reset_mac_header(skb); |
| 5908 | skb_gro_reset_offset(skb, hlen); |
| 5909 | |
| 5910 | if (unlikely(skb_gro_header_hard(skb, hlen))) { |
| 5911 | eth = skb_gro_header_slow(skb, hlen, 0); |
| 5912 | if (unlikely(!eth)) { |
| 5913 | net_warn_ratelimited("%s: dropping impossible skb from %s\n", |
| 5914 | __func__, napi->dev->name); |
| 5915 | napi_reuse_skb(napi, skb); |
| 5916 | return NULL; |
| 5917 | } |
| 5918 | } else { |
| 5919 | eth = (const struct ethhdr *)skb->data; |
| 5920 | gro_pull_from_frag0(skb, hlen); |
| 5921 | NAPI_GRO_CB(skb)->frag0 += hlen; |
| 5922 | NAPI_GRO_CB(skb)->frag0_len -= hlen; |
| 5923 | } |
| 5924 | __skb_pull(skb, hlen); |
| 5925 | |
| 5926 | /* |
| 5927 | * This works because the only protocols we care about don't require |
| 5928 | * special handling. |
| 5929 | * We'll fix it up properly in napi_frags_finish() |
| 5930 | */ |
| 5931 | skb->protocol = eth->h_proto; |
| 5932 | |
| 5933 | return skb; |
| 5934 | } |
| 5935 | |
| 5936 | gro_result_t napi_gro_frags(struct napi_struct *napi) |
| 5937 | { |
| 5938 | gro_result_t ret; |
| 5939 | struct sk_buff *skb = napi_frags_skb(napi); |
| 5940 | |
| 5941 | if (!skb) |
| 5942 | return GRO_DROP; |
| 5943 | |
| 5944 | trace_napi_gro_frags_entry(skb); |
| 5945 | |
| 5946 | ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb)); |
| 5947 | trace_napi_gro_frags_exit(ret); |
| 5948 | |
| 5949 | return ret; |
| 5950 | } |
| 5951 | EXPORT_SYMBOL(napi_gro_frags); |
| 5952 | |
| 5953 | /* Compute the checksum from gro_offset and return the folded value |
| 5954 | * after adding in any pseudo checksum. |
| 5955 | */ |
| 5956 | __sum16 __skb_gro_checksum_complete(struct sk_buff *skb) |
| 5957 | { |
| 5958 | __wsum wsum; |
| 5959 | __sum16 sum; |
| 5960 | |
| 5961 | wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0); |
| 5962 | |
| 5963 | /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */ |
| 5964 | sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum)); |
| 5965 | /* See comments in __skb_checksum_complete(). */ |
| 5966 | if (likely(!sum)) { |
| 5967 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && |
| 5968 | !skb->csum_complete_sw) |
| 5969 | netdev_rx_csum_fault(skb->dev, skb); |
| 5970 | } |
| 5971 | |
| 5972 | NAPI_GRO_CB(skb)->csum = wsum; |
| 5973 | NAPI_GRO_CB(skb)->csum_valid = 1; |
| 5974 | |
| 5975 | return sum; |
| 5976 | } |
| 5977 | EXPORT_SYMBOL(__skb_gro_checksum_complete); |
| 5978 | |
| 5979 | static void net_rps_send_ipi(struct softnet_data *remsd) |
| 5980 | { |
| 5981 | #ifdef CONFIG_RPS |
| 5982 | while (remsd) { |
| 5983 | struct softnet_data *next = remsd->rps_ipi_next; |
| 5984 | |
| 5985 | if (cpu_online(remsd->cpu)) |
| 5986 | smp_call_function_single_async(remsd->cpu, &remsd->csd); |
| 5987 | remsd = next; |
| 5988 | } |
| 5989 | #endif |
| 5990 | } |
| 5991 | |
| 5992 | /* |
| 5993 | * net_rps_action_and_irq_enable sends any pending IPI's for rps. |
| 5994 | * Note: called with local irq disabled, but exits with local irq enabled. |
| 5995 | */ |
| 5996 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) |
| 5997 | { |
| 5998 | #ifdef CONFIG_RPS |
| 5999 | struct softnet_data *remsd = sd->rps_ipi_list; |
| 6000 | |
| 6001 | if (remsd) { |
| 6002 | sd->rps_ipi_list = NULL; |
| 6003 | |
| 6004 | local_irq_enable(); |
| 6005 | |
| 6006 | /* Send pending IPI's to kick RPS processing on remote cpus. */ |
| 6007 | net_rps_send_ipi(remsd); |
| 6008 | } else |
| 6009 | #endif |
| 6010 | local_irq_enable(); |
| 6011 | } |
| 6012 | |
| 6013 | static bool sd_has_rps_ipi_waiting(struct softnet_data *sd) |
| 6014 | { |
| 6015 | #ifdef CONFIG_RPS |
| 6016 | return sd->rps_ipi_list != NULL; |
| 6017 | #else |
| 6018 | return false; |
| 6019 | #endif |
| 6020 | } |
| 6021 | |
| 6022 | static int process_backlog(struct napi_struct *napi, int quota) |
| 6023 | { |
| 6024 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
| 6025 | bool again = true; |
| 6026 | int work = 0; |
| 6027 | |
| 6028 | /* Check if we have pending ipi, its better to send them now, |
| 6029 | * not waiting net_rx_action() end. |
| 6030 | */ |
| 6031 | if (sd_has_rps_ipi_waiting(sd)) { |
| 6032 | local_irq_disable(); |
| 6033 | net_rps_action_and_irq_enable(sd); |
| 6034 | } |
| 6035 | |
| 6036 | napi->weight = READ_ONCE(dev_rx_weight); |
| 6037 | while (again) { |
| 6038 | struct sk_buff *skb; |
| 6039 | |
| 6040 | while ((skb = __skb_dequeue(&sd->process_queue))) { |
| 6041 | rcu_read_lock(); |
| 6042 | __netif_receive_skb(skb); |
| 6043 | rcu_read_unlock(); |
| 6044 | input_queue_head_incr(sd); |
| 6045 | if (++work >= quota) |
| 6046 | return work; |
| 6047 | |
| 6048 | } |
| 6049 | |
| 6050 | local_irq_disable(); |
| 6051 | rps_lock(sd); |
| 6052 | if (skb_queue_empty(&sd->input_pkt_queue)) { |
| 6053 | /* |
| 6054 | * Inline a custom version of __napi_complete(). |
| 6055 | * only current cpu owns and manipulates this napi, |
| 6056 | * and NAPI_STATE_SCHED is the only possible flag set |
| 6057 | * on backlog. |
| 6058 | * We can use a plain write instead of clear_bit(), |
| 6059 | * and we dont need an smp_mb() memory barrier. |
| 6060 | */ |
| 6061 | napi->state = 0; |
| 6062 | again = false; |
| 6063 | } else { |
| 6064 | skb_queue_splice_tail_init(&sd->input_pkt_queue, |
| 6065 | &sd->process_queue); |
| 6066 | } |
| 6067 | rps_unlock(sd); |
| 6068 | local_irq_enable(); |
| 6069 | } |
| 6070 | |
| 6071 | return work; |
| 6072 | } |
| 6073 | |
| 6074 | /** |
| 6075 | * __napi_schedule - schedule for receive |
| 6076 | * @n: entry to schedule |
| 6077 | * |
| 6078 | * The entry's receive function will be scheduled to run. |
| 6079 | * Consider using __napi_schedule_irqoff() if hard irqs are masked. |
| 6080 | */ |
| 6081 | void __napi_schedule(struct napi_struct *n) |
| 6082 | { |
| 6083 | unsigned long flags; |
| 6084 | |
| 6085 | local_irq_save(flags); |
| 6086 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); |
| 6087 | local_irq_restore(flags); |
| 6088 | } |
| 6089 | EXPORT_SYMBOL(__napi_schedule); |
| 6090 | |
| 6091 | /** |
| 6092 | * napi_schedule_prep - check if napi can be scheduled |
| 6093 | * @n: napi context |
| 6094 | * |
| 6095 | * Test if NAPI routine is already running, and if not mark |
| 6096 | * it as running. This is used as a condition variable |
| 6097 | * insure only one NAPI poll instance runs. We also make |
| 6098 | * sure there is no pending NAPI disable. |
| 6099 | */ |
| 6100 | bool napi_schedule_prep(struct napi_struct *n) |
| 6101 | { |
| 6102 | unsigned long val, new; |
| 6103 | |
| 6104 | do { |
| 6105 | val = READ_ONCE(n->state); |
| 6106 | if (unlikely(val & NAPIF_STATE_DISABLE)) |
| 6107 | return false; |
| 6108 | new = val | NAPIF_STATE_SCHED; |
| 6109 | |
| 6110 | /* Sets STATE_MISSED bit if STATE_SCHED was already set |
| 6111 | * This was suggested by Alexander Duyck, as compiler |
| 6112 | * emits better code than : |
| 6113 | * if (val & NAPIF_STATE_SCHED) |
| 6114 | * new |= NAPIF_STATE_MISSED; |
| 6115 | */ |
| 6116 | new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED * |
| 6117 | NAPIF_STATE_MISSED; |
| 6118 | } while (cmpxchg(&n->state, val, new) != val); |
| 6119 | |
| 6120 | return !(val & NAPIF_STATE_SCHED); |
| 6121 | } |
| 6122 | EXPORT_SYMBOL(napi_schedule_prep); |
| 6123 | |
| 6124 | /** |
| 6125 | * __napi_schedule_irqoff - schedule for receive |
| 6126 | * @n: entry to schedule |
| 6127 | * |
| 6128 | * Variant of __napi_schedule() assuming hard irqs are masked. |
| 6129 | * |
| 6130 | * On PREEMPT_RT enabled kernels this maps to __napi_schedule() |
| 6131 | * because the interrupt disabled assumption might not be true |
| 6132 | * due to force-threaded interrupts and spinlock substitution. |
| 6133 | */ |
| 6134 | void __napi_schedule_irqoff(struct napi_struct *n) |
| 6135 | { |
| 6136 | if (!IS_ENABLED(CONFIG_PREEMPT_RT)) |
| 6137 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); |
| 6138 | else |
| 6139 | __napi_schedule(n); |
| 6140 | } |
| 6141 | EXPORT_SYMBOL(__napi_schedule_irqoff); |
| 6142 | |
| 6143 | bool napi_complete_done(struct napi_struct *n, int work_done) |
| 6144 | { |
| 6145 | unsigned long flags, val, new; |
| 6146 | |
| 6147 | /* |
| 6148 | * 1) Don't let napi dequeue from the cpu poll list |
| 6149 | * just in case its running on a different cpu. |
| 6150 | * 2) If we are busy polling, do nothing here, we have |
| 6151 | * the guarantee we will be called later. |
| 6152 | */ |
| 6153 | if (unlikely(n->state & (NAPIF_STATE_NPSVC | |
| 6154 | NAPIF_STATE_IN_BUSY_POLL))) |
| 6155 | return false; |
| 6156 | |
| 6157 | if (n->gro_bitmask) { |
| 6158 | unsigned long timeout = 0; |
| 6159 | |
| 6160 | if (work_done) |
| 6161 | timeout = n->dev->gro_flush_timeout; |
| 6162 | |
| 6163 | /* When the NAPI instance uses a timeout and keeps postponing |
| 6164 | * it, we need to bound somehow the time packets are kept in |
| 6165 | * the GRO layer |
| 6166 | */ |
| 6167 | napi_gro_flush(n, !!timeout); |
| 6168 | if (timeout) |
| 6169 | hrtimer_start(&n->timer, ns_to_ktime(timeout), |
| 6170 | HRTIMER_MODE_REL_PINNED); |
| 6171 | } |
| 6172 | |
| 6173 | gro_normal_list(n); |
| 6174 | |
| 6175 | if (unlikely(!list_empty(&n->poll_list))) { |
| 6176 | /* If n->poll_list is not empty, we need to mask irqs */ |
| 6177 | local_irq_save(flags); |
| 6178 | list_del_init(&n->poll_list); |
| 6179 | local_irq_restore(flags); |
| 6180 | } |
| 6181 | |
| 6182 | do { |
| 6183 | val = READ_ONCE(n->state); |
| 6184 | |
| 6185 | WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED)); |
| 6186 | |
| 6187 | new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED | |
| 6188 | NAPIF_STATE_SCHED_THREADED); |
| 6189 | |
| 6190 | /* If STATE_MISSED was set, leave STATE_SCHED set, |
| 6191 | * because we will call napi->poll() one more time. |
| 6192 | * This C code was suggested by Alexander Duyck to help gcc. |
| 6193 | */ |
| 6194 | new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED * |
| 6195 | NAPIF_STATE_SCHED; |
| 6196 | } while (cmpxchg(&n->state, val, new) != val); |
| 6197 | |
| 6198 | if (unlikely(val & NAPIF_STATE_MISSED)) { |
| 6199 | __napi_schedule(n); |
| 6200 | return false; |
| 6201 | } |
| 6202 | |
| 6203 | return true; |
| 6204 | } |
| 6205 | EXPORT_SYMBOL(napi_complete_done); |
| 6206 | |
| 6207 | /* must be called under rcu_read_lock(), as we dont take a reference */ |
| 6208 | static struct napi_struct *napi_by_id(unsigned int napi_id) |
| 6209 | { |
| 6210 | unsigned int hash = napi_id % HASH_SIZE(napi_hash); |
| 6211 | struct napi_struct *napi; |
| 6212 | |
| 6213 | hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) |
| 6214 | if (napi->napi_id == napi_id) |
| 6215 | return napi; |
| 6216 | |
| 6217 | return NULL; |
| 6218 | } |
| 6219 | |
| 6220 | #if defined(CONFIG_NET_RX_BUSY_POLL) |
| 6221 | |
| 6222 | #define BUSY_POLL_BUDGET 8 |
| 6223 | |
| 6224 | static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock) |
| 6225 | { |
| 6226 | int rc; |
| 6227 | |
| 6228 | /* Busy polling means there is a high chance device driver hard irq |
| 6229 | * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was |
| 6230 | * set in napi_schedule_prep(). |
| 6231 | * Since we are about to call napi->poll() once more, we can safely |
| 6232 | * clear NAPI_STATE_MISSED. |
| 6233 | * |
| 6234 | * Note: x86 could use a single "lock and ..." instruction |
| 6235 | * to perform these two clear_bit() |
| 6236 | */ |
| 6237 | clear_bit(NAPI_STATE_MISSED, &napi->state); |
| 6238 | clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state); |
| 6239 | |
| 6240 | local_bh_disable(); |
| 6241 | |
| 6242 | /* All we really want here is to re-enable device interrupts. |
| 6243 | * Ideally, a new ndo_busy_poll_stop() could avoid another round. |
| 6244 | */ |
| 6245 | rc = napi->poll(napi, BUSY_POLL_BUDGET); |
| 6246 | /* We can't gro_normal_list() here, because napi->poll() might have |
| 6247 | * rearmed the napi (napi_complete_done()) in which case it could |
| 6248 | * already be running on another CPU. |
| 6249 | */ |
| 6250 | trace_napi_poll(napi, rc, BUSY_POLL_BUDGET); |
| 6251 | netpoll_poll_unlock(have_poll_lock); |
| 6252 | if (rc == BUSY_POLL_BUDGET) { |
| 6253 | /* As the whole budget was spent, we still own the napi so can |
| 6254 | * safely handle the rx_list. |
| 6255 | */ |
| 6256 | gro_normal_list(napi); |
| 6257 | __napi_schedule(napi); |
| 6258 | } |
| 6259 | local_bh_enable(); |
| 6260 | } |
| 6261 | |
| 6262 | void napi_busy_loop(unsigned int napi_id, |
| 6263 | bool (*loop_end)(void *, unsigned long), |
| 6264 | void *loop_end_arg) |
| 6265 | { |
| 6266 | unsigned long start_time = loop_end ? busy_loop_current_time() : 0; |
| 6267 | int (*napi_poll)(struct napi_struct *napi, int budget); |
| 6268 | void *have_poll_lock = NULL; |
| 6269 | struct napi_struct *napi; |
| 6270 | |
| 6271 | restart: |
| 6272 | napi_poll = NULL; |
| 6273 | |
| 6274 | rcu_read_lock(); |
| 6275 | |
| 6276 | napi = napi_by_id(napi_id); |
| 6277 | if (!napi) |
| 6278 | goto out; |
| 6279 | |
| 6280 | preempt_disable(); |
| 6281 | for (;;) { |
| 6282 | int work = 0; |
| 6283 | |
| 6284 | local_bh_disable(); |
| 6285 | if (!napi_poll) { |
| 6286 | unsigned long val = READ_ONCE(napi->state); |
| 6287 | |
| 6288 | /* If multiple threads are competing for this napi, |
| 6289 | * we avoid dirtying napi->state as much as we can. |
| 6290 | */ |
| 6291 | if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED | |
| 6292 | NAPIF_STATE_IN_BUSY_POLL)) |
| 6293 | goto count; |
| 6294 | if (cmpxchg(&napi->state, val, |
| 6295 | val | NAPIF_STATE_IN_BUSY_POLL | |
| 6296 | NAPIF_STATE_SCHED) != val) |
| 6297 | goto count; |
| 6298 | have_poll_lock = netpoll_poll_lock(napi); |
| 6299 | napi_poll = napi->poll; |
| 6300 | } |
| 6301 | work = napi_poll(napi, BUSY_POLL_BUDGET); |
| 6302 | trace_napi_poll(napi, work, BUSY_POLL_BUDGET); |
| 6303 | gro_normal_list(napi); |
| 6304 | count: |
| 6305 | if (work > 0) |
| 6306 | __NET_ADD_STATS(dev_net(napi->dev), |
| 6307 | LINUX_MIB_BUSYPOLLRXPACKETS, work); |
| 6308 | local_bh_enable(); |
| 6309 | |
| 6310 | if (!loop_end || loop_end(loop_end_arg, start_time)) |
| 6311 | break; |
| 6312 | |
| 6313 | if (unlikely(need_resched())) { |
| 6314 | if (napi_poll) |
| 6315 | busy_poll_stop(napi, have_poll_lock); |
| 6316 | preempt_enable(); |
| 6317 | rcu_read_unlock(); |
| 6318 | cond_resched(); |
| 6319 | if (loop_end(loop_end_arg, start_time)) |
| 6320 | return; |
| 6321 | goto restart; |
| 6322 | } |
| 6323 | cpu_relax(); |
| 6324 | } |
| 6325 | if (napi_poll) |
| 6326 | busy_poll_stop(napi, have_poll_lock); |
| 6327 | preempt_enable(); |
| 6328 | out: |
| 6329 | rcu_read_unlock(); |
| 6330 | } |
| 6331 | EXPORT_SYMBOL(napi_busy_loop); |
| 6332 | |
| 6333 | #endif /* CONFIG_NET_RX_BUSY_POLL */ |
| 6334 | |
| 6335 | static void napi_hash_add(struct napi_struct *napi) |
| 6336 | { |
| 6337 | if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) || |
| 6338 | test_and_set_bit(NAPI_STATE_HASHED, &napi->state)) |
| 6339 | return; |
| 6340 | |
| 6341 | spin_lock(&napi_hash_lock); |
| 6342 | |
| 6343 | /* 0..NR_CPUS range is reserved for sender_cpu use */ |
| 6344 | do { |
| 6345 | if (unlikely(++napi_gen_id < MIN_NAPI_ID)) |
| 6346 | napi_gen_id = MIN_NAPI_ID; |
| 6347 | } while (napi_by_id(napi_gen_id)); |
| 6348 | napi->napi_id = napi_gen_id; |
| 6349 | |
| 6350 | hlist_add_head_rcu(&napi->napi_hash_node, |
| 6351 | &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); |
| 6352 | |
| 6353 | spin_unlock(&napi_hash_lock); |
| 6354 | } |
| 6355 | |
| 6356 | /* Warning : caller is responsible to make sure rcu grace period |
| 6357 | * is respected before freeing memory containing @napi |
| 6358 | */ |
| 6359 | bool napi_hash_del(struct napi_struct *napi) |
| 6360 | { |
| 6361 | bool rcu_sync_needed = false; |
| 6362 | |
| 6363 | spin_lock(&napi_hash_lock); |
| 6364 | |
| 6365 | if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) { |
| 6366 | rcu_sync_needed = true; |
| 6367 | hlist_del_rcu(&napi->napi_hash_node); |
| 6368 | } |
| 6369 | spin_unlock(&napi_hash_lock); |
| 6370 | return rcu_sync_needed; |
| 6371 | } |
| 6372 | EXPORT_SYMBOL_GPL(napi_hash_del); |
| 6373 | |
| 6374 | static enum hrtimer_restart napi_watchdog(struct hrtimer *timer) |
| 6375 | { |
| 6376 | struct napi_struct *napi; |
| 6377 | |
| 6378 | napi = container_of(timer, struct napi_struct, timer); |
| 6379 | |
| 6380 | /* Note : we use a relaxed variant of napi_schedule_prep() not setting |
| 6381 | * NAPI_STATE_MISSED, since we do not react to a device IRQ. |
| 6382 | */ |
| 6383 | if (napi->gro_bitmask && !napi_disable_pending(napi) && |
| 6384 | !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) |
| 6385 | __napi_schedule_irqoff(napi); |
| 6386 | |
| 6387 | return HRTIMER_NORESTART; |
| 6388 | } |
| 6389 | |
| 6390 | static void init_gro_hash(struct napi_struct *napi) |
| 6391 | { |
| 6392 | int i; |
| 6393 | |
| 6394 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
| 6395 | INIT_LIST_HEAD(&napi->gro_hash[i].list); |
| 6396 | napi->gro_hash[i].count = 0; |
| 6397 | } |
| 6398 | napi->gro_bitmask = 0; |
| 6399 | } |
| 6400 | |
| 6401 | int dev_set_threaded(struct net_device *dev, bool threaded) |
| 6402 | { |
| 6403 | struct napi_struct *napi; |
| 6404 | int err = 0; |
| 6405 | |
| 6406 | if (dev->threaded == threaded) |
| 6407 | return 0; |
| 6408 | |
| 6409 | if (threaded) { |
| 6410 | list_for_each_entry(napi, &dev->napi_list, dev_list) { |
| 6411 | if (!napi->thread) { |
| 6412 | err = napi_kthread_create(napi); |
| 6413 | if (err) { |
| 6414 | threaded = false; |
| 6415 | break; |
| 6416 | } |
| 6417 | } |
| 6418 | } |
| 6419 | } |
| 6420 | |
| 6421 | dev->threaded = threaded; |
| 6422 | |
| 6423 | /* Make sure kthread is created before THREADED bit |
| 6424 | * is set. |
| 6425 | */ |
| 6426 | smp_mb__before_atomic(); |
| 6427 | |
| 6428 | /* Setting/unsetting threaded mode on a napi might not immediately |
| 6429 | * take effect, if the current napi instance is actively being |
| 6430 | * polled. In this case, the switch between threaded mode and |
| 6431 | * softirq mode will happen in the next round of napi_schedule(). |
| 6432 | * This should not cause hiccups/stalls to the live traffic. |
| 6433 | */ |
| 6434 | list_for_each_entry(napi, &dev->napi_list, dev_list) { |
| 6435 | if (threaded) |
| 6436 | set_bit(NAPI_STATE_THREADED, &napi->state); |
| 6437 | else |
| 6438 | clear_bit(NAPI_STATE_THREADED, &napi->state); |
| 6439 | } |
| 6440 | |
| 6441 | return err; |
| 6442 | } |
| 6443 | |
| 6444 | void netif_napi_add(struct net_device *dev, struct napi_struct *napi, |
| 6445 | int (*poll)(struct napi_struct *, int), int weight) |
| 6446 | { |
| 6447 | INIT_LIST_HEAD(&napi->poll_list); |
| 6448 | hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); |
| 6449 | napi->timer.function = napi_watchdog; |
| 6450 | init_gro_hash(napi); |
| 6451 | napi->skb = NULL; |
| 6452 | INIT_LIST_HEAD(&napi->rx_list); |
| 6453 | napi->rx_count = 0; |
| 6454 | napi->poll = poll; |
| 6455 | if (weight > NAPI_POLL_WEIGHT) |
| 6456 | netdev_err_once(dev, "%s() called with weight %d\n", __func__, |
| 6457 | weight); |
| 6458 | napi->weight = weight; |
| 6459 | napi->dev = dev; |
| 6460 | #ifdef CONFIG_NETPOLL |
| 6461 | napi->poll_owner = -1; |
| 6462 | #endif |
| 6463 | set_bit(NAPI_STATE_SCHED, &napi->state); |
| 6464 | set_bit(NAPI_STATE_NPSVC, &napi->state); |
| 6465 | list_add_rcu(&napi->dev_list, &dev->napi_list); |
| 6466 | napi_hash_add(napi); |
| 6467 | /* Create kthread for this napi if dev->threaded is set. |
| 6468 | * Clear dev->threaded if kthread creation failed so that |
| 6469 | * threaded mode will not be enabled in napi_enable(). |
| 6470 | */ |
| 6471 | if (dev->threaded && napi_kthread_create(napi)) |
| 6472 | dev->threaded = 0; |
| 6473 | } |
| 6474 | EXPORT_SYMBOL(netif_napi_add); |
| 6475 | |
| 6476 | void napi_disable(struct napi_struct *n) |
| 6477 | { |
| 6478 | might_sleep(); |
| 6479 | set_bit(NAPI_STATE_DISABLE, &n->state); |
| 6480 | |
| 6481 | while (test_and_set_bit(NAPI_STATE_SCHED, &n->state)) |
| 6482 | msleep(1); |
| 6483 | while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state)) |
| 6484 | msleep(1); |
| 6485 | |
| 6486 | hrtimer_cancel(&n->timer); |
| 6487 | |
| 6488 | clear_bit(NAPI_STATE_DISABLE, &n->state); |
| 6489 | clear_bit(NAPI_STATE_THREADED, &n->state); |
| 6490 | } |
| 6491 | EXPORT_SYMBOL(napi_disable); |
| 6492 | |
| 6493 | /** |
| 6494 | * napi_enable - enable NAPI scheduling |
| 6495 | * @n: NAPI context |
| 6496 | * |
| 6497 | * Resume NAPI from being scheduled on this context. |
| 6498 | * Must be paired with napi_disable. |
| 6499 | */ |
| 6500 | void napi_enable(struct napi_struct *n) |
| 6501 | { |
| 6502 | BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state)); |
| 6503 | smp_mb__before_atomic(); |
| 6504 | clear_bit(NAPI_STATE_SCHED, &n->state); |
| 6505 | clear_bit(NAPI_STATE_NPSVC, &n->state); |
| 6506 | if (n->dev->threaded && n->thread) |
| 6507 | set_bit(NAPI_STATE_THREADED, &n->state); |
| 6508 | } |
| 6509 | EXPORT_SYMBOL(napi_enable); |
| 6510 | |
| 6511 | static void flush_gro_hash(struct napi_struct *napi) |
| 6512 | { |
| 6513 | int i; |
| 6514 | |
| 6515 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
| 6516 | struct sk_buff *skb, *n; |
| 6517 | |
| 6518 | list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list) |
| 6519 | kfree_skb(skb); |
| 6520 | napi->gro_hash[i].count = 0; |
| 6521 | } |
| 6522 | } |
| 6523 | |
| 6524 | /* Must be called in process context */ |
| 6525 | void netif_napi_del(struct napi_struct *napi) |
| 6526 | { |
| 6527 | might_sleep(); |
| 6528 | if (napi_hash_del(napi)) |
| 6529 | synchronize_net(); |
| 6530 | list_del_init(&napi->dev_list); |
| 6531 | napi_free_frags(napi); |
| 6532 | |
| 6533 | flush_gro_hash(napi); |
| 6534 | napi->gro_bitmask = 0; |
| 6535 | |
| 6536 | if (napi->thread) { |
| 6537 | kthread_stop(napi->thread); |
| 6538 | napi->thread = NULL; |
| 6539 | } |
| 6540 | } |
| 6541 | EXPORT_SYMBOL(netif_napi_del); |
| 6542 | |
| 6543 | static int __napi_poll(struct napi_struct *n, bool *repoll) |
| 6544 | { |
| 6545 | int work, weight; |
| 6546 | |
| 6547 | weight = n->weight; |
| 6548 | |
| 6549 | /* This NAPI_STATE_SCHED test is for avoiding a race |
| 6550 | * with netpoll's poll_napi(). Only the entity which |
| 6551 | * obtains the lock and sees NAPI_STATE_SCHED set will |
| 6552 | * actually make the ->poll() call. Therefore we avoid |
| 6553 | * accidentally calling ->poll() when NAPI is not scheduled. |
| 6554 | */ |
| 6555 | work = 0; |
| 6556 | if (test_bit(NAPI_STATE_SCHED, &n->state)) { |
| 6557 | work = n->poll(n, weight); |
| 6558 | trace_napi_poll(n, work, weight); |
| 6559 | } |
| 6560 | |
| 6561 | WARN_ON_ONCE(work > weight); |
| 6562 | |
| 6563 | if (likely(work < weight)) |
| 6564 | return work; |
| 6565 | |
| 6566 | /* Drivers must not modify the NAPI state if they |
| 6567 | * consume the entire weight. In such cases this code |
| 6568 | * still "owns" the NAPI instance and therefore can |
| 6569 | * move the instance around on the list at-will. |
| 6570 | */ |
| 6571 | if (unlikely(napi_disable_pending(n))) { |
| 6572 | napi_complete(n); |
| 6573 | return work; |
| 6574 | } |
| 6575 | |
| 6576 | if (n->gro_bitmask) { |
| 6577 | /* flush too old packets |
| 6578 | * If HZ < 1000, flush all packets. |
| 6579 | */ |
| 6580 | napi_gro_flush(n, HZ >= 1000); |
| 6581 | } |
| 6582 | |
| 6583 | gro_normal_list(n); |
| 6584 | |
| 6585 | /* Some drivers may have called napi_schedule |
| 6586 | * prior to exhausting their budget. |
| 6587 | */ |
| 6588 | if (unlikely(!list_empty(&n->poll_list))) { |
| 6589 | pr_warn_once("%s: Budget exhausted after napi rescheduled\n", |
| 6590 | n->dev ? n->dev->name : "backlog"); |
| 6591 | return work; |
| 6592 | } |
| 6593 | |
| 6594 | *repoll = true; |
| 6595 | |
| 6596 | return work; |
| 6597 | } |
| 6598 | |
| 6599 | static int napi_poll(struct napi_struct *n, struct list_head *repoll) |
| 6600 | { |
| 6601 | bool do_repoll = false; |
| 6602 | void *have; |
| 6603 | int work; |
| 6604 | |
| 6605 | list_del_init(&n->poll_list); |
| 6606 | |
| 6607 | have = netpoll_poll_lock(n); |
| 6608 | |
| 6609 | work = __napi_poll(n, &do_repoll); |
| 6610 | |
| 6611 | if (do_repoll) |
| 6612 | list_add_tail(&n->poll_list, repoll); |
| 6613 | |
| 6614 | netpoll_poll_unlock(have); |
| 6615 | |
| 6616 | return work; |
| 6617 | } |
| 6618 | |
| 6619 | static int napi_thread_wait(struct napi_struct *napi) |
| 6620 | { |
| 6621 | bool woken = false; |
| 6622 | |
| 6623 | set_current_state(TASK_INTERRUPTIBLE); |
| 6624 | |
| 6625 | while (!kthread_should_stop()) { |
| 6626 | /* Testing SCHED_THREADED bit here to make sure the current |
| 6627 | * kthread owns this napi and could poll on this napi. |
| 6628 | * Testing SCHED bit is not enough because SCHED bit might be |
| 6629 | * set by some other busy poll thread or by napi_disable(). |
| 6630 | */ |
| 6631 | if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken) { |
| 6632 | WARN_ON(!list_empty(&napi->poll_list)); |
| 6633 | __set_current_state(TASK_RUNNING); |
| 6634 | return 0; |
| 6635 | } |
| 6636 | |
| 6637 | schedule(); |
| 6638 | /* woken being true indicates this thread owns this napi. */ |
| 6639 | woken = true; |
| 6640 | set_current_state(TASK_INTERRUPTIBLE); |
| 6641 | } |
| 6642 | __set_current_state(TASK_RUNNING); |
| 6643 | |
| 6644 | return -1; |
| 6645 | } |
| 6646 | |
| 6647 | static int napi_threaded_poll(void *data) |
| 6648 | { |
| 6649 | struct napi_struct *napi = data; |
| 6650 | void *have; |
| 6651 | |
| 6652 | while (!napi_thread_wait(napi)) { |
| 6653 | for (;;) { |
| 6654 | bool repoll = false; |
| 6655 | |
| 6656 | local_bh_disable(); |
| 6657 | |
| 6658 | have = netpoll_poll_lock(napi); |
| 6659 | __napi_poll(napi, &repoll); |
| 6660 | netpoll_poll_unlock(have); |
| 6661 | |
| 6662 | __kfree_skb_flush(); |
| 6663 | local_bh_enable(); |
| 6664 | |
| 6665 | if (!repoll) |
| 6666 | break; |
| 6667 | |
| 6668 | cond_resched(); |
| 6669 | } |
| 6670 | } |
| 6671 | return 0; |
| 6672 | } |
| 6673 | |
| 6674 | static __latent_entropy void net_rx_action(struct softirq_action *h) |
| 6675 | { |
| 6676 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
| 6677 | unsigned long time_limit = jiffies + |
| 6678 | usecs_to_jiffies(READ_ONCE(netdev_budget_usecs)); |
| 6679 | int budget = READ_ONCE(netdev_budget); |
| 6680 | LIST_HEAD(list); |
| 6681 | LIST_HEAD(repoll); |
| 6682 | |
| 6683 | local_irq_disable(); |
| 6684 | list_splice_init(&sd->poll_list, &list); |
| 6685 | local_irq_enable(); |
| 6686 | |
| 6687 | for (;;) { |
| 6688 | struct napi_struct *n; |
| 6689 | |
| 6690 | if (list_empty(&list)) { |
| 6691 | if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll)) |
| 6692 | goto out; |
| 6693 | break; |
| 6694 | } |
| 6695 | |
| 6696 | n = list_first_entry(&list, struct napi_struct, poll_list); |
| 6697 | budget -= napi_poll(n, &repoll); |
| 6698 | |
| 6699 | /* If softirq window is exhausted then punt. |
| 6700 | * Allow this to run for 2 jiffies since which will allow |
| 6701 | * an average latency of 1.5/HZ. |
| 6702 | */ |
| 6703 | if (unlikely(budget <= 0 || |
| 6704 | time_after_eq(jiffies, time_limit))) { |
| 6705 | sd->time_squeeze++; |
| 6706 | break; |
| 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | local_irq_disable(); |
| 6711 | |
| 6712 | list_splice_tail_init(&sd->poll_list, &list); |
| 6713 | list_splice_tail(&repoll, &list); |
| 6714 | list_splice(&list, &sd->poll_list); |
| 6715 | if (!list_empty(&sd->poll_list)) |
| 6716 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
| 6717 | |
| 6718 | net_rps_action_and_irq_enable(sd); |
| 6719 | out: |
| 6720 | __kfree_skb_flush(); |
| 6721 | } |
| 6722 | |
| 6723 | struct netdev_adjacent { |
| 6724 | struct net_device *dev; |
| 6725 | |
| 6726 | /* upper master flag, there can only be one master device per list */ |
| 6727 | bool master; |
| 6728 | |
| 6729 | /* lookup ignore flag */ |
| 6730 | bool ignore; |
| 6731 | |
| 6732 | /* counter for the number of times this device was added to us */ |
| 6733 | u16 ref_nr; |
| 6734 | |
| 6735 | /* private field for the users */ |
| 6736 | void *private; |
| 6737 | |
| 6738 | struct list_head list; |
| 6739 | struct rcu_head rcu; |
| 6740 | }; |
| 6741 | |
| 6742 | static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev, |
| 6743 | struct list_head *adj_list) |
| 6744 | { |
| 6745 | struct netdev_adjacent *adj; |
| 6746 | |
| 6747 | list_for_each_entry(adj, adj_list, list) { |
| 6748 | if (adj->dev == adj_dev) |
| 6749 | return adj; |
| 6750 | } |
| 6751 | return NULL; |
| 6752 | } |
| 6753 | |
| 6754 | static int ____netdev_has_upper_dev(struct net_device *upper_dev, void *data) |
| 6755 | { |
| 6756 | struct net_device *dev = data; |
| 6757 | |
| 6758 | return upper_dev == dev; |
| 6759 | } |
| 6760 | |
| 6761 | /** |
| 6762 | * netdev_has_upper_dev - Check if device is linked to an upper device |
| 6763 | * @dev: device |
| 6764 | * @upper_dev: upper device to check |
| 6765 | * |
| 6766 | * Find out if a device is linked to specified upper device and return true |
| 6767 | * in case it is. Note that this checks only immediate upper device, |
| 6768 | * not through a complete stack of devices. The caller must hold the RTNL lock. |
| 6769 | */ |
| 6770 | bool netdev_has_upper_dev(struct net_device *dev, |
| 6771 | struct net_device *upper_dev) |
| 6772 | { |
| 6773 | ASSERT_RTNL(); |
| 6774 | |
| 6775 | return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
| 6776 | upper_dev); |
| 6777 | } |
| 6778 | EXPORT_SYMBOL(netdev_has_upper_dev); |
| 6779 | |
| 6780 | /** |
| 6781 | * netdev_has_upper_dev_all - Check if device is linked to an upper device |
| 6782 | * @dev: device |
| 6783 | * @upper_dev: upper device to check |
| 6784 | * |
| 6785 | * Find out if a device is linked to specified upper device and return true |
| 6786 | * in case it is. Note that this checks the entire upper device chain. |
| 6787 | * The caller must hold rcu lock. |
| 6788 | */ |
| 6789 | |
| 6790 | bool netdev_has_upper_dev_all_rcu(struct net_device *dev, |
| 6791 | struct net_device *upper_dev) |
| 6792 | { |
| 6793 | return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
| 6794 | upper_dev); |
| 6795 | } |
| 6796 | EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu); |
| 6797 | |
| 6798 | /** |
| 6799 | * netdev_has_any_upper_dev - Check if device is linked to some device |
| 6800 | * @dev: device |
| 6801 | * |
| 6802 | * Find out if a device is linked to an upper device and return true in case |
| 6803 | * it is. The caller must hold the RTNL lock. |
| 6804 | */ |
| 6805 | bool netdev_has_any_upper_dev(struct net_device *dev) |
| 6806 | { |
| 6807 | ASSERT_RTNL(); |
| 6808 | |
| 6809 | return !list_empty(&dev->adj_list.upper); |
| 6810 | } |
| 6811 | EXPORT_SYMBOL(netdev_has_any_upper_dev); |
| 6812 | |
| 6813 | /** |
| 6814 | * netdev_master_upper_dev_get - Get master upper device |
| 6815 | * @dev: device |
| 6816 | * |
| 6817 | * Find a master upper device and return pointer to it or NULL in case |
| 6818 | * it's not there. The caller must hold the RTNL lock. |
| 6819 | */ |
| 6820 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) |
| 6821 | { |
| 6822 | struct netdev_adjacent *upper; |
| 6823 | |
| 6824 | ASSERT_RTNL(); |
| 6825 | |
| 6826 | if (list_empty(&dev->adj_list.upper)) |
| 6827 | return NULL; |
| 6828 | |
| 6829 | upper = list_first_entry(&dev->adj_list.upper, |
| 6830 | struct netdev_adjacent, list); |
| 6831 | if (likely(upper->master)) |
| 6832 | return upper->dev; |
| 6833 | return NULL; |
| 6834 | } |
| 6835 | EXPORT_SYMBOL(netdev_master_upper_dev_get); |
| 6836 | |
| 6837 | static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev) |
| 6838 | { |
| 6839 | struct netdev_adjacent *upper; |
| 6840 | |
| 6841 | ASSERT_RTNL(); |
| 6842 | |
| 6843 | if (list_empty(&dev->adj_list.upper)) |
| 6844 | return NULL; |
| 6845 | |
| 6846 | upper = list_first_entry(&dev->adj_list.upper, |
| 6847 | struct netdev_adjacent, list); |
| 6848 | if (likely(upper->master) && !upper->ignore) |
| 6849 | return upper->dev; |
| 6850 | return NULL; |
| 6851 | } |
| 6852 | |
| 6853 | /** |
| 6854 | * netdev_has_any_lower_dev - Check if device is linked to some device |
| 6855 | * @dev: device |
| 6856 | * |
| 6857 | * Find out if a device is linked to a lower device and return true in case |
| 6858 | * it is. The caller must hold the RTNL lock. |
| 6859 | */ |
| 6860 | static bool netdev_has_any_lower_dev(struct net_device *dev) |
| 6861 | { |
| 6862 | ASSERT_RTNL(); |
| 6863 | |
| 6864 | return !list_empty(&dev->adj_list.lower); |
| 6865 | } |
| 6866 | |
| 6867 | void *netdev_adjacent_get_private(struct list_head *adj_list) |
| 6868 | { |
| 6869 | struct netdev_adjacent *adj; |
| 6870 | |
| 6871 | adj = list_entry(adj_list, struct netdev_adjacent, list); |
| 6872 | |
| 6873 | return adj->private; |
| 6874 | } |
| 6875 | EXPORT_SYMBOL(netdev_adjacent_get_private); |
| 6876 | |
| 6877 | /** |
| 6878 | * netdev_upper_get_next_dev_rcu - Get the next dev from upper list |
| 6879 | * @dev: device |
| 6880 | * @iter: list_head ** of the current position |
| 6881 | * |
| 6882 | * Gets the next device from the dev's upper list, starting from iter |
| 6883 | * position. The caller must hold RCU read lock. |
| 6884 | */ |
| 6885 | struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, |
| 6886 | struct list_head **iter) |
| 6887 | { |
| 6888 | struct netdev_adjacent *upper; |
| 6889 | |
| 6890 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); |
| 6891 | |
| 6892 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); |
| 6893 | |
| 6894 | if (&upper->list == &dev->adj_list.upper) |
| 6895 | return NULL; |
| 6896 | |
| 6897 | *iter = &upper->list; |
| 6898 | |
| 6899 | return upper->dev; |
| 6900 | } |
| 6901 | EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); |
| 6902 | |
| 6903 | static struct net_device *__netdev_next_upper_dev(struct net_device *dev, |
| 6904 | struct list_head **iter, |
| 6905 | bool *ignore) |
| 6906 | { |
| 6907 | struct netdev_adjacent *upper; |
| 6908 | |
| 6909 | upper = list_entry((*iter)->next, struct netdev_adjacent, list); |
| 6910 | |
| 6911 | if (&upper->list == &dev->adj_list.upper) |
| 6912 | return NULL; |
| 6913 | |
| 6914 | *iter = &upper->list; |
| 6915 | *ignore = upper->ignore; |
| 6916 | |
| 6917 | return upper->dev; |
| 6918 | } |
| 6919 | |
| 6920 | static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev, |
| 6921 | struct list_head **iter) |
| 6922 | { |
| 6923 | struct netdev_adjacent *upper; |
| 6924 | |
| 6925 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); |
| 6926 | |
| 6927 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); |
| 6928 | |
| 6929 | if (&upper->list == &dev->adj_list.upper) |
| 6930 | return NULL; |
| 6931 | |
| 6932 | *iter = &upper->list; |
| 6933 | |
| 6934 | return upper->dev; |
| 6935 | } |
| 6936 | |
| 6937 | static int __netdev_walk_all_upper_dev(struct net_device *dev, |
| 6938 | int (*fn)(struct net_device *dev, |
| 6939 | void *data), |
| 6940 | void *data) |
| 6941 | { |
| 6942 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
| 6943 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; |
| 6944 | int ret, cur = 0; |
| 6945 | bool ignore; |
| 6946 | |
| 6947 | now = dev; |
| 6948 | iter = &dev->adj_list.upper; |
| 6949 | |
| 6950 | while (1) { |
| 6951 | if (now != dev) { |
| 6952 | ret = fn(now, data); |
| 6953 | if (ret) |
| 6954 | return ret; |
| 6955 | } |
| 6956 | |
| 6957 | next = NULL; |
| 6958 | while (1) { |
| 6959 | udev = __netdev_next_upper_dev(now, &iter, &ignore); |
| 6960 | if (!udev) |
| 6961 | break; |
| 6962 | if (ignore) |
| 6963 | continue; |
| 6964 | |
| 6965 | next = udev; |
| 6966 | niter = &udev->adj_list.upper; |
| 6967 | dev_stack[cur] = now; |
| 6968 | iter_stack[cur++] = iter; |
| 6969 | break; |
| 6970 | } |
| 6971 | |
| 6972 | if (!next) { |
| 6973 | if (!cur) |
| 6974 | return 0; |
| 6975 | next = dev_stack[--cur]; |
| 6976 | niter = iter_stack[cur]; |
| 6977 | } |
| 6978 | |
| 6979 | now = next; |
| 6980 | iter = niter; |
| 6981 | } |
| 6982 | |
| 6983 | return 0; |
| 6984 | } |
| 6985 | |
| 6986 | int netdev_walk_all_upper_dev_rcu(struct net_device *dev, |
| 6987 | int (*fn)(struct net_device *dev, |
| 6988 | void *data), |
| 6989 | void *data) |
| 6990 | { |
| 6991 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
| 6992 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; |
| 6993 | int ret, cur = 0; |
| 6994 | |
| 6995 | now = dev; |
| 6996 | iter = &dev->adj_list.upper; |
| 6997 | |
| 6998 | while (1) { |
| 6999 | if (now != dev) { |
| 7000 | ret = fn(now, data); |
| 7001 | if (ret) |
| 7002 | return ret; |
| 7003 | } |
| 7004 | |
| 7005 | next = NULL; |
| 7006 | while (1) { |
| 7007 | udev = netdev_next_upper_dev_rcu(now, &iter); |
| 7008 | if (!udev) |
| 7009 | break; |
| 7010 | |
| 7011 | next = udev; |
| 7012 | niter = &udev->adj_list.upper; |
| 7013 | dev_stack[cur] = now; |
| 7014 | iter_stack[cur++] = iter; |
| 7015 | break; |
| 7016 | } |
| 7017 | |
| 7018 | if (!next) { |
| 7019 | if (!cur) |
| 7020 | return 0; |
| 7021 | next = dev_stack[--cur]; |
| 7022 | niter = iter_stack[cur]; |
| 7023 | } |
| 7024 | |
| 7025 | now = next; |
| 7026 | iter = niter; |
| 7027 | } |
| 7028 | |
| 7029 | return 0; |
| 7030 | } |
| 7031 | EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu); |
| 7032 | |
| 7033 | static bool __netdev_has_upper_dev(struct net_device *dev, |
| 7034 | struct net_device *upper_dev) |
| 7035 | { |
| 7036 | ASSERT_RTNL(); |
| 7037 | |
| 7038 | return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev, |
| 7039 | upper_dev); |
| 7040 | } |
| 7041 | |
| 7042 | /** |
| 7043 | * netdev_lower_get_next_private - Get the next ->private from the |
| 7044 | * lower neighbour list |
| 7045 | * @dev: device |
| 7046 | * @iter: list_head ** of the current position |
| 7047 | * |
| 7048 | * Gets the next netdev_adjacent->private from the dev's lower neighbour |
| 7049 | * list, starting from iter position. The caller must hold either hold the |
| 7050 | * RTNL lock or its own locking that guarantees that the neighbour lower |
| 7051 | * list will remain unchanged. |
| 7052 | */ |
| 7053 | void *netdev_lower_get_next_private(struct net_device *dev, |
| 7054 | struct list_head **iter) |
| 7055 | { |
| 7056 | struct netdev_adjacent *lower; |
| 7057 | |
| 7058 | lower = list_entry(*iter, struct netdev_adjacent, list); |
| 7059 | |
| 7060 | if (&lower->list == &dev->adj_list.lower) |
| 7061 | return NULL; |
| 7062 | |
| 7063 | *iter = lower->list.next; |
| 7064 | |
| 7065 | return lower->private; |
| 7066 | } |
| 7067 | EXPORT_SYMBOL(netdev_lower_get_next_private); |
| 7068 | |
| 7069 | /** |
| 7070 | * netdev_lower_get_next_private_rcu - Get the next ->private from the |
| 7071 | * lower neighbour list, RCU |
| 7072 | * variant |
| 7073 | * @dev: device |
| 7074 | * @iter: list_head ** of the current position |
| 7075 | * |
| 7076 | * Gets the next netdev_adjacent->private from the dev's lower neighbour |
| 7077 | * list, starting from iter position. The caller must hold RCU read lock. |
| 7078 | */ |
| 7079 | void *netdev_lower_get_next_private_rcu(struct net_device *dev, |
| 7080 | struct list_head **iter) |
| 7081 | { |
| 7082 | struct netdev_adjacent *lower; |
| 7083 | |
| 7084 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 7085 | |
| 7086 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); |
| 7087 | |
| 7088 | if (&lower->list == &dev->adj_list.lower) |
| 7089 | return NULL; |
| 7090 | |
| 7091 | *iter = &lower->list; |
| 7092 | |
| 7093 | return lower->private; |
| 7094 | } |
| 7095 | EXPORT_SYMBOL(netdev_lower_get_next_private_rcu); |
| 7096 | |
| 7097 | /** |
| 7098 | * netdev_lower_get_next - Get the next device from the lower neighbour |
| 7099 | * list |
| 7100 | * @dev: device |
| 7101 | * @iter: list_head ** of the current position |
| 7102 | * |
| 7103 | * Gets the next netdev_adjacent from the dev's lower neighbour |
| 7104 | * list, starting from iter position. The caller must hold RTNL lock or |
| 7105 | * its own locking that guarantees that the neighbour lower |
| 7106 | * list will remain unchanged. |
| 7107 | */ |
| 7108 | void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter) |
| 7109 | { |
| 7110 | struct netdev_adjacent *lower; |
| 7111 | |
| 7112 | lower = list_entry(*iter, struct netdev_adjacent, list); |
| 7113 | |
| 7114 | if (&lower->list == &dev->adj_list.lower) |
| 7115 | return NULL; |
| 7116 | |
| 7117 | *iter = lower->list.next; |
| 7118 | |
| 7119 | return lower->dev; |
| 7120 | } |
| 7121 | EXPORT_SYMBOL(netdev_lower_get_next); |
| 7122 | |
| 7123 | static struct net_device *netdev_next_lower_dev(struct net_device *dev, |
| 7124 | struct list_head **iter) |
| 7125 | { |
| 7126 | struct netdev_adjacent *lower; |
| 7127 | |
| 7128 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); |
| 7129 | |
| 7130 | if (&lower->list == &dev->adj_list.lower) |
| 7131 | return NULL; |
| 7132 | |
| 7133 | *iter = &lower->list; |
| 7134 | |
| 7135 | return lower->dev; |
| 7136 | } |
| 7137 | |
| 7138 | static struct net_device *__netdev_next_lower_dev(struct net_device *dev, |
| 7139 | struct list_head **iter, |
| 7140 | bool *ignore) |
| 7141 | { |
| 7142 | struct netdev_adjacent *lower; |
| 7143 | |
| 7144 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); |
| 7145 | |
| 7146 | if (&lower->list == &dev->adj_list.lower) |
| 7147 | return NULL; |
| 7148 | |
| 7149 | *iter = &lower->list; |
| 7150 | *ignore = lower->ignore; |
| 7151 | |
| 7152 | return lower->dev; |
| 7153 | } |
| 7154 | |
| 7155 | int netdev_walk_all_lower_dev(struct net_device *dev, |
| 7156 | int (*fn)(struct net_device *dev, |
| 7157 | void *data), |
| 7158 | void *data) |
| 7159 | { |
| 7160 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
| 7161 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; |
| 7162 | int ret, cur = 0; |
| 7163 | |
| 7164 | now = dev; |
| 7165 | iter = &dev->adj_list.lower; |
| 7166 | |
| 7167 | while (1) { |
| 7168 | if (now != dev) { |
| 7169 | ret = fn(now, data); |
| 7170 | if (ret) |
| 7171 | return ret; |
| 7172 | } |
| 7173 | |
| 7174 | next = NULL; |
| 7175 | while (1) { |
| 7176 | ldev = netdev_next_lower_dev(now, &iter); |
| 7177 | if (!ldev) |
| 7178 | break; |
| 7179 | |
| 7180 | next = ldev; |
| 7181 | niter = &ldev->adj_list.lower; |
| 7182 | dev_stack[cur] = now; |
| 7183 | iter_stack[cur++] = iter; |
| 7184 | break; |
| 7185 | } |
| 7186 | |
| 7187 | if (!next) { |
| 7188 | if (!cur) |
| 7189 | return 0; |
| 7190 | next = dev_stack[--cur]; |
| 7191 | niter = iter_stack[cur]; |
| 7192 | } |
| 7193 | |
| 7194 | now = next; |
| 7195 | iter = niter; |
| 7196 | } |
| 7197 | |
| 7198 | return 0; |
| 7199 | } |
| 7200 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev); |
| 7201 | |
| 7202 | static int __netdev_walk_all_lower_dev(struct net_device *dev, |
| 7203 | int (*fn)(struct net_device *dev, |
| 7204 | void *data), |
| 7205 | void *data) |
| 7206 | { |
| 7207 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
| 7208 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; |
| 7209 | int ret, cur = 0; |
| 7210 | bool ignore; |
| 7211 | |
| 7212 | now = dev; |
| 7213 | iter = &dev->adj_list.lower; |
| 7214 | |
| 7215 | while (1) { |
| 7216 | if (now != dev) { |
| 7217 | ret = fn(now, data); |
| 7218 | if (ret) |
| 7219 | return ret; |
| 7220 | } |
| 7221 | |
| 7222 | next = NULL; |
| 7223 | while (1) { |
| 7224 | ldev = __netdev_next_lower_dev(now, &iter, &ignore); |
| 7225 | if (!ldev) |
| 7226 | break; |
| 7227 | if (ignore) |
| 7228 | continue; |
| 7229 | |
| 7230 | next = ldev; |
| 7231 | niter = &ldev->adj_list.lower; |
| 7232 | dev_stack[cur] = now; |
| 7233 | iter_stack[cur++] = iter; |
| 7234 | break; |
| 7235 | } |
| 7236 | |
| 7237 | if (!next) { |
| 7238 | if (!cur) |
| 7239 | return 0; |
| 7240 | next = dev_stack[--cur]; |
| 7241 | niter = iter_stack[cur]; |
| 7242 | } |
| 7243 | |
| 7244 | now = next; |
| 7245 | iter = niter; |
| 7246 | } |
| 7247 | |
| 7248 | return 0; |
| 7249 | } |
| 7250 | |
| 7251 | struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev, |
| 7252 | struct list_head **iter) |
| 7253 | { |
| 7254 | struct netdev_adjacent *lower; |
| 7255 | |
| 7256 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); |
| 7257 | if (&lower->list == &dev->adj_list.lower) |
| 7258 | return NULL; |
| 7259 | |
| 7260 | *iter = &lower->list; |
| 7261 | |
| 7262 | return lower->dev; |
| 7263 | } |
| 7264 | EXPORT_SYMBOL(netdev_next_lower_dev_rcu); |
| 7265 | |
| 7266 | static u8 __netdev_upper_depth(struct net_device *dev) |
| 7267 | { |
| 7268 | struct net_device *udev; |
| 7269 | struct list_head *iter; |
| 7270 | u8 max_depth = 0; |
| 7271 | bool ignore; |
| 7272 | |
| 7273 | for (iter = &dev->adj_list.upper, |
| 7274 | udev = __netdev_next_upper_dev(dev, &iter, &ignore); |
| 7275 | udev; |
| 7276 | udev = __netdev_next_upper_dev(dev, &iter, &ignore)) { |
| 7277 | if (ignore) |
| 7278 | continue; |
| 7279 | if (max_depth < udev->upper_level) |
| 7280 | max_depth = udev->upper_level; |
| 7281 | } |
| 7282 | |
| 7283 | return max_depth; |
| 7284 | } |
| 7285 | |
| 7286 | static u8 __netdev_lower_depth(struct net_device *dev) |
| 7287 | { |
| 7288 | struct net_device *ldev; |
| 7289 | struct list_head *iter; |
| 7290 | u8 max_depth = 0; |
| 7291 | bool ignore; |
| 7292 | |
| 7293 | for (iter = &dev->adj_list.lower, |
| 7294 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore); |
| 7295 | ldev; |
| 7296 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) { |
| 7297 | if (ignore) |
| 7298 | continue; |
| 7299 | if (max_depth < ldev->lower_level) |
| 7300 | max_depth = ldev->lower_level; |
| 7301 | } |
| 7302 | |
| 7303 | return max_depth; |
| 7304 | } |
| 7305 | |
| 7306 | static int __netdev_update_upper_level(struct net_device *dev, void *data) |
| 7307 | { |
| 7308 | dev->upper_level = __netdev_upper_depth(dev) + 1; |
| 7309 | return 0; |
| 7310 | } |
| 7311 | |
| 7312 | static int __netdev_update_lower_level(struct net_device *dev, void *data) |
| 7313 | { |
| 7314 | dev->lower_level = __netdev_lower_depth(dev) + 1; |
| 7315 | return 0; |
| 7316 | } |
| 7317 | |
| 7318 | int netdev_walk_all_lower_dev_rcu(struct net_device *dev, |
| 7319 | int (*fn)(struct net_device *dev, |
| 7320 | void *data), |
| 7321 | void *data) |
| 7322 | { |
| 7323 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
| 7324 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; |
| 7325 | int ret, cur = 0; |
| 7326 | |
| 7327 | now = dev; |
| 7328 | iter = &dev->adj_list.lower; |
| 7329 | |
| 7330 | while (1) { |
| 7331 | if (now != dev) { |
| 7332 | ret = fn(now, data); |
| 7333 | if (ret) |
| 7334 | return ret; |
| 7335 | } |
| 7336 | |
| 7337 | next = NULL; |
| 7338 | while (1) { |
| 7339 | ldev = netdev_next_lower_dev_rcu(now, &iter); |
| 7340 | if (!ldev) |
| 7341 | break; |
| 7342 | |
| 7343 | next = ldev; |
| 7344 | niter = &ldev->adj_list.lower; |
| 7345 | dev_stack[cur] = now; |
| 7346 | iter_stack[cur++] = iter; |
| 7347 | break; |
| 7348 | } |
| 7349 | |
| 7350 | if (!next) { |
| 7351 | if (!cur) |
| 7352 | return 0; |
| 7353 | next = dev_stack[--cur]; |
| 7354 | niter = iter_stack[cur]; |
| 7355 | } |
| 7356 | |
| 7357 | now = next; |
| 7358 | iter = niter; |
| 7359 | } |
| 7360 | |
| 7361 | return 0; |
| 7362 | } |
| 7363 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu); |
| 7364 | |
| 7365 | /** |
| 7366 | * netdev_lower_get_first_private_rcu - Get the first ->private from the |
| 7367 | * lower neighbour list, RCU |
| 7368 | * variant |
| 7369 | * @dev: device |
| 7370 | * |
| 7371 | * Gets the first netdev_adjacent->private from the dev's lower neighbour |
| 7372 | * list. The caller must hold RCU read lock. |
| 7373 | */ |
| 7374 | void *netdev_lower_get_first_private_rcu(struct net_device *dev) |
| 7375 | { |
| 7376 | struct netdev_adjacent *lower; |
| 7377 | |
| 7378 | lower = list_first_or_null_rcu(&dev->adj_list.lower, |
| 7379 | struct netdev_adjacent, list); |
| 7380 | if (lower) |
| 7381 | return lower->private; |
| 7382 | return NULL; |
| 7383 | } |
| 7384 | EXPORT_SYMBOL(netdev_lower_get_first_private_rcu); |
| 7385 | |
| 7386 | /** |
| 7387 | * netdev_master_upper_dev_get_rcu - Get master upper device |
| 7388 | * @dev: device |
| 7389 | * |
| 7390 | * Find a master upper device and return pointer to it or NULL in case |
| 7391 | * it's not there. The caller must hold the RCU read lock. |
| 7392 | */ |
| 7393 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) |
| 7394 | { |
| 7395 | struct netdev_adjacent *upper; |
| 7396 | |
| 7397 | upper = list_first_or_null_rcu(&dev->adj_list.upper, |
| 7398 | struct netdev_adjacent, list); |
| 7399 | if (upper && likely(upper->master)) |
| 7400 | return upper->dev; |
| 7401 | return NULL; |
| 7402 | } |
| 7403 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); |
| 7404 | |
| 7405 | static int netdev_adjacent_sysfs_add(struct net_device *dev, |
| 7406 | struct net_device *adj_dev, |
| 7407 | struct list_head *dev_list) |
| 7408 | { |
| 7409 | char linkname[IFNAMSIZ+7]; |
| 7410 | |
| 7411 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
| 7412 | "upper_%s" : "lower_%s", adj_dev->name); |
| 7413 | return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj), |
| 7414 | linkname); |
| 7415 | } |
| 7416 | static void netdev_adjacent_sysfs_del(struct net_device *dev, |
| 7417 | char *name, |
| 7418 | struct list_head *dev_list) |
| 7419 | { |
| 7420 | char linkname[IFNAMSIZ+7]; |
| 7421 | |
| 7422 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
| 7423 | "upper_%s" : "lower_%s", name); |
| 7424 | sysfs_remove_link(&(dev->dev.kobj), linkname); |
| 7425 | } |
| 7426 | |
| 7427 | static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev, |
| 7428 | struct net_device *adj_dev, |
| 7429 | struct list_head *dev_list) |
| 7430 | { |
| 7431 | return (dev_list == &dev->adj_list.upper || |
| 7432 | dev_list == &dev->adj_list.lower) && |
| 7433 | net_eq(dev_net(dev), dev_net(adj_dev)); |
| 7434 | } |
| 7435 | |
| 7436 | static int __netdev_adjacent_dev_insert(struct net_device *dev, |
| 7437 | struct net_device *adj_dev, |
| 7438 | struct list_head *dev_list, |
| 7439 | void *private, bool master) |
| 7440 | { |
| 7441 | struct netdev_adjacent *adj; |
| 7442 | int ret; |
| 7443 | |
| 7444 | adj = __netdev_find_adj(adj_dev, dev_list); |
| 7445 | |
| 7446 | if (adj) { |
| 7447 | adj->ref_nr += 1; |
| 7448 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n", |
| 7449 | dev->name, adj_dev->name, adj->ref_nr); |
| 7450 | |
| 7451 | return 0; |
| 7452 | } |
| 7453 | |
| 7454 | adj = kmalloc(sizeof(*adj), GFP_KERNEL); |
| 7455 | if (!adj) |
| 7456 | return -ENOMEM; |
| 7457 | |
| 7458 | adj->dev = adj_dev; |
| 7459 | adj->master = master; |
| 7460 | adj->ref_nr = 1; |
| 7461 | adj->private = private; |
| 7462 | adj->ignore = false; |
| 7463 | dev_hold(adj_dev); |
| 7464 | |
| 7465 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n", |
| 7466 | dev->name, adj_dev->name, adj->ref_nr, adj_dev->name); |
| 7467 | |
| 7468 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) { |
| 7469 | ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list); |
| 7470 | if (ret) |
| 7471 | goto free_adj; |
| 7472 | } |
| 7473 | |
| 7474 | /* Ensure that master link is always the first item in list. */ |
| 7475 | if (master) { |
| 7476 | ret = sysfs_create_link(&(dev->dev.kobj), |
| 7477 | &(adj_dev->dev.kobj), "master"); |
| 7478 | if (ret) |
| 7479 | goto remove_symlinks; |
| 7480 | |
| 7481 | list_add_rcu(&adj->list, dev_list); |
| 7482 | } else { |
| 7483 | list_add_tail_rcu(&adj->list, dev_list); |
| 7484 | } |
| 7485 | |
| 7486 | return 0; |
| 7487 | |
| 7488 | remove_symlinks: |
| 7489 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
| 7490 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
| 7491 | free_adj: |
| 7492 | kfree(adj); |
| 7493 | dev_put(adj_dev); |
| 7494 | |
| 7495 | return ret; |
| 7496 | } |
| 7497 | |
| 7498 | static void __netdev_adjacent_dev_remove(struct net_device *dev, |
| 7499 | struct net_device *adj_dev, |
| 7500 | u16 ref_nr, |
| 7501 | struct list_head *dev_list) |
| 7502 | { |
| 7503 | struct netdev_adjacent *adj; |
| 7504 | |
| 7505 | pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n", |
| 7506 | dev->name, adj_dev->name, ref_nr); |
| 7507 | |
| 7508 | adj = __netdev_find_adj(adj_dev, dev_list); |
| 7509 | |
| 7510 | if (!adj) { |
| 7511 | pr_err("Adjacency does not exist for device %s from %s\n", |
| 7512 | dev->name, adj_dev->name); |
| 7513 | WARN_ON(1); |
| 7514 | return; |
| 7515 | } |
| 7516 | |
| 7517 | if (adj->ref_nr > ref_nr) { |
| 7518 | pr_debug("adjacency: %s to %s ref_nr - %d = %d\n", |
| 7519 | dev->name, adj_dev->name, ref_nr, |
| 7520 | adj->ref_nr - ref_nr); |
| 7521 | adj->ref_nr -= ref_nr; |
| 7522 | return; |
| 7523 | } |
| 7524 | |
| 7525 | if (adj->master) |
| 7526 | sysfs_remove_link(&(dev->dev.kobj), "master"); |
| 7527 | |
| 7528 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
| 7529 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
| 7530 | |
| 7531 | list_del_rcu(&adj->list); |
| 7532 | pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n", |
| 7533 | adj_dev->name, dev->name, adj_dev->name); |
| 7534 | dev_put(adj_dev); |
| 7535 | kfree_rcu(adj, rcu); |
| 7536 | } |
| 7537 | |
| 7538 | static int __netdev_adjacent_dev_link_lists(struct net_device *dev, |
| 7539 | struct net_device *upper_dev, |
| 7540 | struct list_head *up_list, |
| 7541 | struct list_head *down_list, |
| 7542 | void *private, bool master) |
| 7543 | { |
| 7544 | int ret; |
| 7545 | |
| 7546 | ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, |
| 7547 | private, master); |
| 7548 | if (ret) |
| 7549 | return ret; |
| 7550 | |
| 7551 | ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, |
| 7552 | private, false); |
| 7553 | if (ret) { |
| 7554 | __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list); |
| 7555 | return ret; |
| 7556 | } |
| 7557 | |
| 7558 | return 0; |
| 7559 | } |
| 7560 | |
| 7561 | static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev, |
| 7562 | struct net_device *upper_dev, |
| 7563 | u16 ref_nr, |
| 7564 | struct list_head *up_list, |
| 7565 | struct list_head *down_list) |
| 7566 | { |
| 7567 | __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list); |
| 7568 | __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list); |
| 7569 | } |
| 7570 | |
| 7571 | static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, |
| 7572 | struct net_device *upper_dev, |
| 7573 | void *private, bool master) |
| 7574 | { |
| 7575 | return __netdev_adjacent_dev_link_lists(dev, upper_dev, |
| 7576 | &dev->adj_list.upper, |
| 7577 | &upper_dev->adj_list.lower, |
| 7578 | private, master); |
| 7579 | } |
| 7580 | |
| 7581 | static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev, |
| 7582 | struct net_device *upper_dev) |
| 7583 | { |
| 7584 | __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1, |
| 7585 | &dev->adj_list.upper, |
| 7586 | &upper_dev->adj_list.lower); |
| 7587 | } |
| 7588 | |
| 7589 | static void __netdev_addr_mask(unsigned char *mask, const unsigned char *addr, |
| 7590 | struct net_device *dev) |
| 7591 | { |
| 7592 | int i; |
| 7593 | |
| 7594 | for (i = 0; i < dev->addr_len; i++) |
| 7595 | mask[i] |= addr[i] ^ dev->dev_addr[i]; |
| 7596 | } |
| 7597 | |
| 7598 | static void __netdev_upper_mask(unsigned char *mask, struct net_device *dev, |
| 7599 | struct net_device *lower) |
| 7600 | { |
| 7601 | struct net_device *cur; |
| 7602 | struct list_head *iter; |
| 7603 | |
| 7604 | netdev_for_each_upper_dev_rcu(dev, cur, iter) { |
| 7605 | __netdev_addr_mask(mask, cur->dev_addr, lower); |
| 7606 | __netdev_upper_mask(mask, cur, lower); |
| 7607 | } |
| 7608 | } |
| 7609 | |
| 7610 | static void __netdev_update_addr_mask(struct net_device *dev) |
| 7611 | { |
| 7612 | unsigned char mask[MAX_ADDR_LEN]; |
| 7613 | struct net_device *cur; |
| 7614 | struct list_head *iter; |
| 7615 | |
| 7616 | memset(mask, 0, sizeof(mask)); |
| 7617 | __netdev_upper_mask(mask, dev, dev); |
| 7618 | memcpy(dev->local_addr_mask, mask, dev->addr_len); |
| 7619 | |
| 7620 | netdev_for_each_lower_dev(dev, cur, iter) |
| 7621 | __netdev_update_addr_mask(cur); |
| 7622 | } |
| 7623 | |
| 7624 | static void netdev_update_addr_mask(struct net_device *dev) |
| 7625 | { |
| 7626 | rcu_read_lock(); |
| 7627 | __netdev_update_addr_mask(dev); |
| 7628 | rcu_read_unlock(); |
| 7629 | } |
| 7630 | |
| 7631 | static int __netdev_upper_dev_link(struct net_device *dev, |
| 7632 | struct net_device *upper_dev, bool master, |
| 7633 | void *upper_priv, void *upper_info, |
| 7634 | struct netlink_ext_ack *extack) |
| 7635 | { |
| 7636 | struct netdev_notifier_changeupper_info changeupper_info = { |
| 7637 | .info = { |
| 7638 | .dev = dev, |
| 7639 | .extack = extack, |
| 7640 | }, |
| 7641 | .upper_dev = upper_dev, |
| 7642 | .master = master, |
| 7643 | .linking = true, |
| 7644 | .upper_info = upper_info, |
| 7645 | }; |
| 7646 | struct net_device *master_dev; |
| 7647 | int ret = 0; |
| 7648 | |
| 7649 | ASSERT_RTNL(); |
| 7650 | |
| 7651 | if (dev == upper_dev) |
| 7652 | return -EBUSY; |
| 7653 | |
| 7654 | /* To prevent loops, check if dev is not upper device to upper_dev. */ |
| 7655 | if (__netdev_has_upper_dev(upper_dev, dev)) |
| 7656 | return -EBUSY; |
| 7657 | |
| 7658 | if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV) |
| 7659 | return -EMLINK; |
| 7660 | |
| 7661 | if (!master) { |
| 7662 | if (__netdev_has_upper_dev(dev, upper_dev)) |
| 7663 | return -EEXIST; |
| 7664 | } else { |
| 7665 | master_dev = __netdev_master_upper_dev_get(dev); |
| 7666 | if (master_dev) |
| 7667 | return master_dev == upper_dev ? -EEXIST : -EBUSY; |
| 7668 | } |
| 7669 | |
| 7670 | ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
| 7671 | &changeupper_info.info); |
| 7672 | ret = notifier_to_errno(ret); |
| 7673 | if (ret) |
| 7674 | return ret; |
| 7675 | |
| 7676 | ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv, |
| 7677 | master); |
| 7678 | if (ret) |
| 7679 | return ret; |
| 7680 | |
| 7681 | netdev_update_addr_mask(dev); |
| 7682 | ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
| 7683 | &changeupper_info.info); |
| 7684 | ret = notifier_to_errno(ret); |
| 7685 | if (ret) |
| 7686 | goto rollback; |
| 7687 | |
| 7688 | __netdev_update_upper_level(dev, NULL); |
| 7689 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
| 7690 | |
| 7691 | __netdev_update_lower_level(upper_dev, NULL); |
| 7692 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
| 7693 | NULL); |
| 7694 | |
| 7695 | return 0; |
| 7696 | |
| 7697 | rollback: |
| 7698 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
| 7699 | |
| 7700 | return ret; |
| 7701 | } |
| 7702 | |
| 7703 | /** |
| 7704 | * netdev_upper_dev_link - Add a link to the upper device |
| 7705 | * @dev: device |
| 7706 | * @upper_dev: new upper device |
| 7707 | * @extack: netlink extended ack |
| 7708 | * |
| 7709 | * Adds a link to device which is upper to this one. The caller must hold |
| 7710 | * the RTNL lock. On a failure a negative errno code is returned. |
| 7711 | * On success the reference counts are adjusted and the function |
| 7712 | * returns zero. |
| 7713 | */ |
| 7714 | int netdev_upper_dev_link(struct net_device *dev, |
| 7715 | struct net_device *upper_dev, |
| 7716 | struct netlink_ext_ack *extack) |
| 7717 | { |
| 7718 | return __netdev_upper_dev_link(dev, upper_dev, false, |
| 7719 | NULL, NULL, extack); |
| 7720 | } |
| 7721 | EXPORT_SYMBOL(netdev_upper_dev_link); |
| 7722 | |
| 7723 | /** |
| 7724 | * netdev_master_upper_dev_link - Add a master link to the upper device |
| 7725 | * @dev: device |
| 7726 | * @upper_dev: new upper device |
| 7727 | * @upper_priv: upper device private |
| 7728 | * @upper_info: upper info to be passed down via notifier |
| 7729 | * @extack: netlink extended ack |
| 7730 | * |
| 7731 | * Adds a link to device which is upper to this one. In this case, only |
| 7732 | * one master upper device can be linked, although other non-master devices |
| 7733 | * might be linked as well. The caller must hold the RTNL lock. |
| 7734 | * On a failure a negative errno code is returned. On success the reference |
| 7735 | * counts are adjusted and the function returns zero. |
| 7736 | */ |
| 7737 | int netdev_master_upper_dev_link(struct net_device *dev, |
| 7738 | struct net_device *upper_dev, |
| 7739 | void *upper_priv, void *upper_info, |
| 7740 | struct netlink_ext_ack *extack) |
| 7741 | { |
| 7742 | return __netdev_upper_dev_link(dev, upper_dev, true, |
| 7743 | upper_priv, upper_info, extack); |
| 7744 | } |
| 7745 | EXPORT_SYMBOL(netdev_master_upper_dev_link); |
| 7746 | |
| 7747 | /** |
| 7748 | * netdev_upper_dev_unlink - Removes a link to upper device |
| 7749 | * @dev: device |
| 7750 | * @upper_dev: new upper device |
| 7751 | * |
| 7752 | * Removes a link to device which is upper to this one. The caller must hold |
| 7753 | * the RTNL lock. |
| 7754 | */ |
| 7755 | void netdev_upper_dev_unlink(struct net_device *dev, |
| 7756 | struct net_device *upper_dev) |
| 7757 | { |
| 7758 | struct netdev_notifier_changeupper_info changeupper_info = { |
| 7759 | .info = { |
| 7760 | .dev = dev, |
| 7761 | }, |
| 7762 | .upper_dev = upper_dev, |
| 7763 | .linking = false, |
| 7764 | }; |
| 7765 | |
| 7766 | ASSERT_RTNL(); |
| 7767 | |
| 7768 | changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev; |
| 7769 | |
| 7770 | call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
| 7771 | &changeupper_info.info); |
| 7772 | |
| 7773 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
| 7774 | |
| 7775 | netdev_update_addr_mask(dev); |
| 7776 | call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
| 7777 | &changeupper_info.info); |
| 7778 | |
| 7779 | __netdev_update_upper_level(dev, NULL); |
| 7780 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
| 7781 | |
| 7782 | __netdev_update_lower_level(upper_dev, NULL); |
| 7783 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
| 7784 | NULL); |
| 7785 | } |
| 7786 | EXPORT_SYMBOL(netdev_upper_dev_unlink); |
| 7787 | |
| 7788 | static void __netdev_adjacent_dev_set(struct net_device *upper_dev, |
| 7789 | struct net_device *lower_dev, |
| 7790 | bool val) |
| 7791 | { |
| 7792 | struct netdev_adjacent *adj; |
| 7793 | |
| 7794 | adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower); |
| 7795 | if (adj) |
| 7796 | adj->ignore = val; |
| 7797 | |
| 7798 | adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper); |
| 7799 | if (adj) |
| 7800 | adj->ignore = val; |
| 7801 | } |
| 7802 | |
| 7803 | static void netdev_adjacent_dev_disable(struct net_device *upper_dev, |
| 7804 | struct net_device *lower_dev) |
| 7805 | { |
| 7806 | __netdev_adjacent_dev_set(upper_dev, lower_dev, true); |
| 7807 | } |
| 7808 | |
| 7809 | static void netdev_adjacent_dev_enable(struct net_device *upper_dev, |
| 7810 | struct net_device *lower_dev) |
| 7811 | { |
| 7812 | __netdev_adjacent_dev_set(upper_dev, lower_dev, false); |
| 7813 | } |
| 7814 | |
| 7815 | int netdev_adjacent_change_prepare(struct net_device *old_dev, |
| 7816 | struct net_device *new_dev, |
| 7817 | struct net_device *dev, |
| 7818 | struct netlink_ext_ack *extack) |
| 7819 | { |
| 7820 | int err; |
| 7821 | |
| 7822 | if (!new_dev) |
| 7823 | return 0; |
| 7824 | |
| 7825 | if (old_dev && new_dev != old_dev) |
| 7826 | netdev_adjacent_dev_disable(dev, old_dev); |
| 7827 | |
| 7828 | err = netdev_upper_dev_link(new_dev, dev, extack); |
| 7829 | if (err) { |
| 7830 | if (old_dev && new_dev != old_dev) |
| 7831 | netdev_adjacent_dev_enable(dev, old_dev); |
| 7832 | return err; |
| 7833 | } |
| 7834 | |
| 7835 | return 0; |
| 7836 | } |
| 7837 | EXPORT_SYMBOL(netdev_adjacent_change_prepare); |
| 7838 | |
| 7839 | void netdev_adjacent_change_commit(struct net_device *old_dev, |
| 7840 | struct net_device *new_dev, |
| 7841 | struct net_device *dev) |
| 7842 | { |
| 7843 | if (!new_dev || !old_dev) |
| 7844 | return; |
| 7845 | |
| 7846 | if (new_dev == old_dev) |
| 7847 | return; |
| 7848 | |
| 7849 | netdev_adjacent_dev_enable(dev, old_dev); |
| 7850 | netdev_upper_dev_unlink(old_dev, dev); |
| 7851 | } |
| 7852 | EXPORT_SYMBOL(netdev_adjacent_change_commit); |
| 7853 | |
| 7854 | void netdev_adjacent_change_abort(struct net_device *old_dev, |
| 7855 | struct net_device *new_dev, |
| 7856 | struct net_device *dev) |
| 7857 | { |
| 7858 | if (!new_dev) |
| 7859 | return; |
| 7860 | |
| 7861 | if (old_dev && new_dev != old_dev) |
| 7862 | netdev_adjacent_dev_enable(dev, old_dev); |
| 7863 | |
| 7864 | netdev_upper_dev_unlink(new_dev, dev); |
| 7865 | } |
| 7866 | EXPORT_SYMBOL(netdev_adjacent_change_abort); |
| 7867 | |
| 7868 | /** |
| 7869 | * netdev_bonding_info_change - Dispatch event about slave change |
| 7870 | * @dev: device |
| 7871 | * @bonding_info: info to dispatch |
| 7872 | * |
| 7873 | * Send NETDEV_BONDING_INFO to netdev notifiers with info. |
| 7874 | * The caller must hold the RTNL lock. |
| 7875 | */ |
| 7876 | void netdev_bonding_info_change(struct net_device *dev, |
| 7877 | struct netdev_bonding_info *bonding_info) |
| 7878 | { |
| 7879 | struct netdev_notifier_bonding_info info = { |
| 7880 | .info.dev = dev, |
| 7881 | }; |
| 7882 | |
| 7883 | memcpy(&info.bonding_info, bonding_info, |
| 7884 | sizeof(struct netdev_bonding_info)); |
| 7885 | call_netdevice_notifiers_info(NETDEV_BONDING_INFO, |
| 7886 | &info.info); |
| 7887 | } |
| 7888 | EXPORT_SYMBOL(netdev_bonding_info_change); |
| 7889 | |
| 7890 | static void netdev_adjacent_add_links(struct net_device *dev) |
| 7891 | { |
| 7892 | struct netdev_adjacent *iter; |
| 7893 | |
| 7894 | struct net *net = dev_net(dev); |
| 7895 | |
| 7896 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
| 7897 | if (!net_eq(net, dev_net(iter->dev))) |
| 7898 | continue; |
| 7899 | netdev_adjacent_sysfs_add(iter->dev, dev, |
| 7900 | &iter->dev->adj_list.lower); |
| 7901 | netdev_adjacent_sysfs_add(dev, iter->dev, |
| 7902 | &dev->adj_list.upper); |
| 7903 | } |
| 7904 | |
| 7905 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
| 7906 | if (!net_eq(net, dev_net(iter->dev))) |
| 7907 | continue; |
| 7908 | netdev_adjacent_sysfs_add(iter->dev, dev, |
| 7909 | &iter->dev->adj_list.upper); |
| 7910 | netdev_adjacent_sysfs_add(dev, iter->dev, |
| 7911 | &dev->adj_list.lower); |
| 7912 | } |
| 7913 | } |
| 7914 | |
| 7915 | static void netdev_adjacent_del_links(struct net_device *dev) |
| 7916 | { |
| 7917 | struct netdev_adjacent *iter; |
| 7918 | |
| 7919 | struct net *net = dev_net(dev); |
| 7920 | |
| 7921 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
| 7922 | if (!net_eq(net, dev_net(iter->dev))) |
| 7923 | continue; |
| 7924 | netdev_adjacent_sysfs_del(iter->dev, dev->name, |
| 7925 | &iter->dev->adj_list.lower); |
| 7926 | netdev_adjacent_sysfs_del(dev, iter->dev->name, |
| 7927 | &dev->adj_list.upper); |
| 7928 | } |
| 7929 | |
| 7930 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
| 7931 | if (!net_eq(net, dev_net(iter->dev))) |
| 7932 | continue; |
| 7933 | netdev_adjacent_sysfs_del(iter->dev, dev->name, |
| 7934 | &iter->dev->adj_list.upper); |
| 7935 | netdev_adjacent_sysfs_del(dev, iter->dev->name, |
| 7936 | &dev->adj_list.lower); |
| 7937 | } |
| 7938 | } |
| 7939 | |
| 7940 | void netdev_adjacent_rename_links(struct net_device *dev, char *oldname) |
| 7941 | { |
| 7942 | struct netdev_adjacent *iter; |
| 7943 | |
| 7944 | struct net *net = dev_net(dev); |
| 7945 | |
| 7946 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
| 7947 | if (!net_eq(net, dev_net(iter->dev))) |
| 7948 | continue; |
| 7949 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
| 7950 | &iter->dev->adj_list.lower); |
| 7951 | netdev_adjacent_sysfs_add(iter->dev, dev, |
| 7952 | &iter->dev->adj_list.lower); |
| 7953 | } |
| 7954 | |
| 7955 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
| 7956 | if (!net_eq(net, dev_net(iter->dev))) |
| 7957 | continue; |
| 7958 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
| 7959 | &iter->dev->adj_list.upper); |
| 7960 | netdev_adjacent_sysfs_add(iter->dev, dev, |
| 7961 | &iter->dev->adj_list.upper); |
| 7962 | } |
| 7963 | } |
| 7964 | |
| 7965 | void *netdev_lower_dev_get_private(struct net_device *dev, |
| 7966 | struct net_device *lower_dev) |
| 7967 | { |
| 7968 | struct netdev_adjacent *lower; |
| 7969 | |
| 7970 | if (!lower_dev) |
| 7971 | return NULL; |
| 7972 | lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower); |
| 7973 | if (!lower) |
| 7974 | return NULL; |
| 7975 | |
| 7976 | return lower->private; |
| 7977 | } |
| 7978 | EXPORT_SYMBOL(netdev_lower_dev_get_private); |
| 7979 | |
| 7980 | |
| 7981 | /** |
| 7982 | * netdev_lower_change - Dispatch event about lower device state change |
| 7983 | * @lower_dev: device |
| 7984 | * @lower_state_info: state to dispatch |
| 7985 | * |
| 7986 | * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info. |
| 7987 | * The caller must hold the RTNL lock. |
| 7988 | */ |
| 7989 | void netdev_lower_state_changed(struct net_device *lower_dev, |
| 7990 | void *lower_state_info) |
| 7991 | { |
| 7992 | struct netdev_notifier_changelowerstate_info changelowerstate_info = { |
| 7993 | .info.dev = lower_dev, |
| 7994 | }; |
| 7995 | |
| 7996 | ASSERT_RTNL(); |
| 7997 | changelowerstate_info.lower_state_info = lower_state_info; |
| 7998 | call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, |
| 7999 | &changelowerstate_info.info); |
| 8000 | } |
| 8001 | EXPORT_SYMBOL(netdev_lower_state_changed); |
| 8002 | |
| 8003 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
| 8004 | { |
| 8005 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8006 | |
| 8007 | if (ops->ndo_change_rx_flags) |
| 8008 | ops->ndo_change_rx_flags(dev, flags); |
| 8009 | } |
| 8010 | |
| 8011 | static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify) |
| 8012 | { |
| 8013 | unsigned int old_flags = dev->flags; |
| 8014 | kuid_t uid; |
| 8015 | kgid_t gid; |
| 8016 | |
| 8017 | ASSERT_RTNL(); |
| 8018 | |
| 8019 | dev->flags |= IFF_PROMISC; |
| 8020 | dev->promiscuity += inc; |
| 8021 | if (dev->promiscuity == 0) { |
| 8022 | /* |
| 8023 | * Avoid overflow. |
| 8024 | * If inc causes overflow, untouch promisc and return error. |
| 8025 | */ |
| 8026 | if (inc < 0) |
| 8027 | dev->flags &= ~IFF_PROMISC; |
| 8028 | else { |
| 8029 | dev->promiscuity -= inc; |
| 8030 | pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n", |
| 8031 | dev->name); |
| 8032 | return -EOVERFLOW; |
| 8033 | } |
| 8034 | } |
| 8035 | if (dev->flags != old_flags) { |
| 8036 | pr_info("device %s %s promiscuous mode\n", |
| 8037 | dev->name, |
| 8038 | dev->flags & IFF_PROMISC ? "entered" : "left"); |
| 8039 | if (audit_enabled) { |
| 8040 | current_uid_gid(&uid, &gid); |
| 8041 | audit_log(audit_context(), GFP_ATOMIC, |
| 8042 | AUDIT_ANOM_PROMISCUOUS, |
| 8043 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", |
| 8044 | dev->name, (dev->flags & IFF_PROMISC), |
| 8045 | (old_flags & IFF_PROMISC), |
| 8046 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
| 8047 | from_kuid(&init_user_ns, uid), |
| 8048 | from_kgid(&init_user_ns, gid), |
| 8049 | audit_get_sessionid(current)); |
| 8050 | } |
| 8051 | |
| 8052 | dev_change_rx_flags(dev, IFF_PROMISC); |
| 8053 | } |
| 8054 | if (notify) |
| 8055 | __dev_notify_flags(dev, old_flags, IFF_PROMISC); |
| 8056 | return 0; |
| 8057 | } |
| 8058 | |
| 8059 | /** |
| 8060 | * dev_set_promiscuity - update promiscuity count on a device |
| 8061 | * @dev: device |
| 8062 | * @inc: modifier |
| 8063 | * |
| 8064 | * Add or remove promiscuity from a device. While the count in the device |
| 8065 | * remains above zero the interface remains promiscuous. Once it hits zero |
| 8066 | * the device reverts back to normal filtering operation. A negative inc |
| 8067 | * value is used to drop promiscuity on the device. |
| 8068 | * Return 0 if successful or a negative errno code on error. |
| 8069 | */ |
| 8070 | int dev_set_promiscuity(struct net_device *dev, int inc) |
| 8071 | { |
| 8072 | unsigned int old_flags = dev->flags; |
| 8073 | int err; |
| 8074 | |
| 8075 | err = __dev_set_promiscuity(dev, inc, true); |
| 8076 | if (err < 0) |
| 8077 | return err; |
| 8078 | if (dev->flags != old_flags) |
| 8079 | dev_set_rx_mode(dev); |
| 8080 | return err; |
| 8081 | } |
| 8082 | EXPORT_SYMBOL(dev_set_promiscuity); |
| 8083 | |
| 8084 | static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify) |
| 8085 | { |
| 8086 | unsigned int old_flags = dev->flags, old_gflags = dev->gflags; |
| 8087 | |
| 8088 | ASSERT_RTNL(); |
| 8089 | |
| 8090 | dev->flags |= IFF_ALLMULTI; |
| 8091 | dev->allmulti += inc; |
| 8092 | if (dev->allmulti == 0) { |
| 8093 | /* |
| 8094 | * Avoid overflow. |
| 8095 | * If inc causes overflow, untouch allmulti and return error. |
| 8096 | */ |
| 8097 | if (inc < 0) |
| 8098 | dev->flags &= ~IFF_ALLMULTI; |
| 8099 | else { |
| 8100 | dev->allmulti -= inc; |
| 8101 | pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n", |
| 8102 | dev->name); |
| 8103 | return -EOVERFLOW; |
| 8104 | } |
| 8105 | } |
| 8106 | if (dev->flags ^ old_flags) { |
| 8107 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
| 8108 | dev_set_rx_mode(dev); |
| 8109 | if (notify) |
| 8110 | __dev_notify_flags(dev, old_flags, |
| 8111 | dev->gflags ^ old_gflags); |
| 8112 | } |
| 8113 | return 0; |
| 8114 | } |
| 8115 | |
| 8116 | /** |
| 8117 | * dev_set_allmulti - update allmulti count on a device |
| 8118 | * @dev: device |
| 8119 | * @inc: modifier |
| 8120 | * |
| 8121 | * Add or remove reception of all multicast frames to a device. While the |
| 8122 | * count in the device remains above zero the interface remains listening |
| 8123 | * to all interfaces. Once it hits zero the device reverts back to normal |
| 8124 | * filtering operation. A negative @inc value is used to drop the counter |
| 8125 | * when releasing a resource needing all multicasts. |
| 8126 | * Return 0 if successful or a negative errno code on error. |
| 8127 | */ |
| 8128 | |
| 8129 | int dev_set_allmulti(struct net_device *dev, int inc) |
| 8130 | { |
| 8131 | return __dev_set_allmulti(dev, inc, true); |
| 8132 | } |
| 8133 | EXPORT_SYMBOL(dev_set_allmulti); |
| 8134 | |
| 8135 | /* |
| 8136 | * Upload unicast and multicast address lists to device and |
| 8137 | * configure RX filtering. When the device doesn't support unicast |
| 8138 | * filtering it is put in promiscuous mode while unicast addresses |
| 8139 | * are present. |
| 8140 | */ |
| 8141 | void __dev_set_rx_mode(struct net_device *dev) |
| 8142 | { |
| 8143 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8144 | |
| 8145 | /* dev_open will call this function so the list will stay sane. */ |
| 8146 | if (!(dev->flags&IFF_UP)) |
| 8147 | return; |
| 8148 | |
| 8149 | if (!netif_device_present(dev)) |
| 8150 | return; |
| 8151 | |
| 8152 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
| 8153 | /* Unicast addresses changes may only happen under the rtnl, |
| 8154 | * therefore calling __dev_set_promiscuity here is safe. |
| 8155 | */ |
| 8156 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
| 8157 | __dev_set_promiscuity(dev, 1, false); |
| 8158 | dev->uc_promisc = true; |
| 8159 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
| 8160 | __dev_set_promiscuity(dev, -1, false); |
| 8161 | dev->uc_promisc = false; |
| 8162 | } |
| 8163 | } |
| 8164 | |
| 8165 | if (ops->ndo_set_rx_mode) |
| 8166 | ops->ndo_set_rx_mode(dev); |
| 8167 | } |
| 8168 | |
| 8169 | void dev_set_rx_mode(struct net_device *dev) |
| 8170 | { |
| 8171 | netif_addr_lock_bh(dev); |
| 8172 | __dev_set_rx_mode(dev); |
| 8173 | netif_addr_unlock_bh(dev); |
| 8174 | } |
| 8175 | |
| 8176 | /** |
| 8177 | * dev_get_flags - get flags reported to userspace |
| 8178 | * @dev: device |
| 8179 | * |
| 8180 | * Get the combination of flag bits exported through APIs to userspace. |
| 8181 | */ |
| 8182 | unsigned int dev_get_flags(const struct net_device *dev) |
| 8183 | { |
| 8184 | unsigned int flags; |
| 8185 | |
| 8186 | flags = (dev->flags & ~(IFF_PROMISC | |
| 8187 | IFF_ALLMULTI | |
| 8188 | IFF_RUNNING | |
| 8189 | IFF_LOWER_UP | |
| 8190 | IFF_DORMANT)) | |
| 8191 | (dev->gflags & (IFF_PROMISC | |
| 8192 | IFF_ALLMULTI)); |
| 8193 | |
| 8194 | if (netif_running(dev)) { |
| 8195 | if (netif_oper_up(dev)) |
| 8196 | flags |= IFF_RUNNING; |
| 8197 | if (netif_carrier_ok(dev)) |
| 8198 | flags |= IFF_LOWER_UP; |
| 8199 | if (netif_dormant(dev)) |
| 8200 | flags |= IFF_DORMANT; |
| 8201 | } |
| 8202 | |
| 8203 | return flags; |
| 8204 | } |
| 8205 | EXPORT_SYMBOL(dev_get_flags); |
| 8206 | |
| 8207 | int __dev_change_flags(struct net_device *dev, unsigned int flags, |
| 8208 | struct netlink_ext_ack *extack) |
| 8209 | { |
| 8210 | unsigned int old_flags = dev->flags; |
| 8211 | int ret; |
| 8212 | |
| 8213 | ASSERT_RTNL(); |
| 8214 | |
| 8215 | /* |
| 8216 | * Set the flags on our device. |
| 8217 | */ |
| 8218 | |
| 8219 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | |
| 8220 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | |
| 8221 | IFF_AUTOMEDIA)) | |
| 8222 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | |
| 8223 | IFF_ALLMULTI)); |
| 8224 | |
| 8225 | /* |
| 8226 | * Load in the correct multicast list now the flags have changed. |
| 8227 | */ |
| 8228 | |
| 8229 | if ((old_flags ^ flags) & IFF_MULTICAST) |
| 8230 | dev_change_rx_flags(dev, IFF_MULTICAST); |
| 8231 | |
| 8232 | dev_set_rx_mode(dev); |
| 8233 | |
| 8234 | /* |
| 8235 | * Have we downed the interface. We handle IFF_UP ourselves |
| 8236 | * according to user attempts to set it, rather than blindly |
| 8237 | * setting it. |
| 8238 | */ |
| 8239 | |
| 8240 | ret = 0; |
| 8241 | if ((old_flags ^ flags) & IFF_UP) { |
| 8242 | if (old_flags & IFF_UP) |
| 8243 | __dev_close(dev); |
| 8244 | else |
| 8245 | ret = __dev_open(dev, extack); |
| 8246 | } |
| 8247 | |
| 8248 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
| 8249 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
| 8250 | unsigned int old_flags = dev->flags; |
| 8251 | |
| 8252 | dev->gflags ^= IFF_PROMISC; |
| 8253 | |
| 8254 | if (__dev_set_promiscuity(dev, inc, false) >= 0) |
| 8255 | if (dev->flags != old_flags) |
| 8256 | dev_set_rx_mode(dev); |
| 8257 | } |
| 8258 | |
| 8259 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI |
| 8260 | * is important. Some (broken) drivers set IFF_PROMISC, when |
| 8261 | * IFF_ALLMULTI is requested not asking us and not reporting. |
| 8262 | */ |
| 8263 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { |
| 8264 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
| 8265 | |
| 8266 | dev->gflags ^= IFF_ALLMULTI; |
| 8267 | __dev_set_allmulti(dev, inc, false); |
| 8268 | } |
| 8269 | |
| 8270 | return ret; |
| 8271 | } |
| 8272 | |
| 8273 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags, |
| 8274 | unsigned int gchanges) |
| 8275 | { |
| 8276 | unsigned int changes = dev->flags ^ old_flags; |
| 8277 | |
| 8278 | if (gchanges) |
| 8279 | rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC); |
| 8280 | |
| 8281 | if (changes & IFF_UP) { |
| 8282 | if (dev->flags & IFF_UP) |
| 8283 | call_netdevice_notifiers(NETDEV_UP, dev); |
| 8284 | else |
| 8285 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
| 8286 | } |
| 8287 | |
| 8288 | if (dev->flags & IFF_UP && |
| 8289 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { |
| 8290 | struct netdev_notifier_change_info change_info = { |
| 8291 | .info = { |
| 8292 | .dev = dev, |
| 8293 | }, |
| 8294 | .flags_changed = changes, |
| 8295 | }; |
| 8296 | |
| 8297 | call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info); |
| 8298 | } |
| 8299 | } |
| 8300 | |
| 8301 | /** |
| 8302 | * dev_change_flags - change device settings |
| 8303 | * @dev: device |
| 8304 | * @flags: device state flags |
| 8305 | * @extack: netlink extended ack |
| 8306 | * |
| 8307 | * Change settings on device based state flags. The flags are |
| 8308 | * in the userspace exported format. |
| 8309 | */ |
| 8310 | int dev_change_flags(struct net_device *dev, unsigned int flags, |
| 8311 | struct netlink_ext_ack *extack) |
| 8312 | { |
| 8313 | int ret; |
| 8314 | unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags; |
| 8315 | |
| 8316 | ret = __dev_change_flags(dev, flags, extack); |
| 8317 | if (ret < 0) |
| 8318 | return ret; |
| 8319 | |
| 8320 | changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags); |
| 8321 | __dev_notify_flags(dev, old_flags, changes); |
| 8322 | return ret; |
| 8323 | } |
| 8324 | EXPORT_SYMBOL(dev_change_flags); |
| 8325 | |
| 8326 | int __dev_set_mtu(struct net_device *dev, int new_mtu) |
| 8327 | { |
| 8328 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8329 | |
| 8330 | if (ops->ndo_change_mtu) |
| 8331 | return ops->ndo_change_mtu(dev, new_mtu); |
| 8332 | |
| 8333 | /* Pairs with all the lockless reads of dev->mtu in the stack */ |
| 8334 | WRITE_ONCE(dev->mtu, new_mtu); |
| 8335 | return 0; |
| 8336 | } |
| 8337 | EXPORT_SYMBOL(__dev_set_mtu); |
| 8338 | |
| 8339 | int dev_validate_mtu(struct net_device *dev, int new_mtu, |
| 8340 | struct netlink_ext_ack *extack) |
| 8341 | { |
| 8342 | /* MTU must be positive, and in range */ |
| 8343 | if (new_mtu < 0 || new_mtu < dev->min_mtu) { |
| 8344 | NL_SET_ERR_MSG(extack, "mtu less than device minimum"); |
| 8345 | return -EINVAL; |
| 8346 | } |
| 8347 | |
| 8348 | if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) { |
| 8349 | NL_SET_ERR_MSG(extack, "mtu greater than device maximum"); |
| 8350 | return -EINVAL; |
| 8351 | } |
| 8352 | return 0; |
| 8353 | } |
| 8354 | |
| 8355 | /** |
| 8356 | * dev_set_mtu_ext - Change maximum transfer unit |
| 8357 | * @dev: device |
| 8358 | * @new_mtu: new transfer unit |
| 8359 | * @extack: netlink extended ack |
| 8360 | * |
| 8361 | * Change the maximum transfer size of the network device. |
| 8362 | */ |
| 8363 | int dev_set_mtu_ext(struct net_device *dev, int new_mtu, |
| 8364 | struct netlink_ext_ack *extack) |
| 8365 | { |
| 8366 | int err, orig_mtu; |
| 8367 | |
| 8368 | if (new_mtu == dev->mtu) |
| 8369 | return 0; |
| 8370 | |
| 8371 | err = dev_validate_mtu(dev, new_mtu, extack); |
| 8372 | if (err) |
| 8373 | return err; |
| 8374 | |
| 8375 | if (!netif_device_present(dev)) |
| 8376 | return -ENODEV; |
| 8377 | |
| 8378 | err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev); |
| 8379 | err = notifier_to_errno(err); |
| 8380 | if (err) |
| 8381 | return err; |
| 8382 | |
| 8383 | orig_mtu = dev->mtu; |
| 8384 | err = __dev_set_mtu(dev, new_mtu); |
| 8385 | |
| 8386 | if (!err) { |
| 8387 | err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
| 8388 | orig_mtu); |
| 8389 | err = notifier_to_errno(err); |
| 8390 | if (err) { |
| 8391 | /* setting mtu back and notifying everyone again, |
| 8392 | * so that they have a chance to revert changes. |
| 8393 | */ |
| 8394 | __dev_set_mtu(dev, orig_mtu); |
| 8395 | call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
| 8396 | new_mtu); |
| 8397 | } |
| 8398 | } |
| 8399 | return err; |
| 8400 | } |
| 8401 | |
| 8402 | int dev_set_mtu(struct net_device *dev, int new_mtu) |
| 8403 | { |
| 8404 | struct netlink_ext_ack extack; |
| 8405 | int err; |
| 8406 | |
| 8407 | memset(&extack, 0, sizeof(extack)); |
| 8408 | err = dev_set_mtu_ext(dev, new_mtu, &extack); |
| 8409 | if (err && extack._msg) |
| 8410 | net_err_ratelimited("%s: %s\n", dev->name, extack._msg); |
| 8411 | return err; |
| 8412 | } |
| 8413 | EXPORT_SYMBOL(dev_set_mtu); |
| 8414 | |
| 8415 | /** |
| 8416 | * dev_change_tx_queue_len - Change TX queue length of a netdevice |
| 8417 | * @dev: device |
| 8418 | * @new_len: new tx queue length |
| 8419 | */ |
| 8420 | int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len) |
| 8421 | { |
| 8422 | unsigned int orig_len = dev->tx_queue_len; |
| 8423 | int res; |
| 8424 | |
| 8425 | if (new_len != (unsigned int)new_len) |
| 8426 | return -ERANGE; |
| 8427 | |
| 8428 | if (new_len != orig_len) { |
| 8429 | dev->tx_queue_len = new_len; |
| 8430 | res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); |
| 8431 | res = notifier_to_errno(res); |
| 8432 | if (res) |
| 8433 | goto err_rollback; |
| 8434 | res = dev_qdisc_change_tx_queue_len(dev); |
| 8435 | if (res) |
| 8436 | goto err_rollback; |
| 8437 | } |
| 8438 | |
| 8439 | return 0; |
| 8440 | |
| 8441 | err_rollback: |
| 8442 | netdev_err(dev, "refused to change device tx_queue_len\n"); |
| 8443 | dev->tx_queue_len = orig_len; |
| 8444 | return res; |
| 8445 | } |
| 8446 | |
| 8447 | /** |
| 8448 | * dev_set_group - Change group this device belongs to |
| 8449 | * @dev: device |
| 8450 | * @new_group: group this device should belong to |
| 8451 | */ |
| 8452 | void dev_set_group(struct net_device *dev, int new_group) |
| 8453 | { |
| 8454 | dev->group = new_group; |
| 8455 | } |
| 8456 | EXPORT_SYMBOL(dev_set_group); |
| 8457 | |
| 8458 | /** |
| 8459 | * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR. |
| 8460 | * @dev: device |
| 8461 | * @addr: new address |
| 8462 | * @extack: netlink extended ack |
| 8463 | */ |
| 8464 | int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr, |
| 8465 | struct netlink_ext_ack *extack) |
| 8466 | { |
| 8467 | struct netdev_notifier_pre_changeaddr_info info = { |
| 8468 | .info.dev = dev, |
| 8469 | .info.extack = extack, |
| 8470 | .dev_addr = addr, |
| 8471 | }; |
| 8472 | int rc; |
| 8473 | |
| 8474 | rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info); |
| 8475 | return notifier_to_errno(rc); |
| 8476 | } |
| 8477 | EXPORT_SYMBOL(dev_pre_changeaddr_notify); |
| 8478 | |
| 8479 | /** |
| 8480 | * dev_set_mac_address - Change Media Access Control Address |
| 8481 | * @dev: device |
| 8482 | * @sa: new address |
| 8483 | * @extack: netlink extended ack |
| 8484 | * |
| 8485 | * Change the hardware (MAC) address of the device |
| 8486 | */ |
| 8487 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, |
| 8488 | struct netlink_ext_ack *extack) |
| 8489 | { |
| 8490 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8491 | int err; |
| 8492 | |
| 8493 | if (!ops->ndo_set_mac_address) |
| 8494 | return -EOPNOTSUPP; |
| 8495 | if (sa->sa_family != dev->type) |
| 8496 | return -EINVAL; |
| 8497 | if (!netif_device_present(dev)) |
| 8498 | return -ENODEV; |
| 8499 | err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack); |
| 8500 | if (err) |
| 8501 | return err; |
| 8502 | err = ops->ndo_set_mac_address(dev, sa); |
| 8503 | if (err) |
| 8504 | return err; |
| 8505 | dev->addr_assign_type = NET_ADDR_SET; |
| 8506 | netdev_update_addr_mask(dev); |
| 8507 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
| 8508 | add_device_randomness(dev->dev_addr, dev->addr_len); |
| 8509 | return 0; |
| 8510 | } |
| 8511 | EXPORT_SYMBOL(dev_set_mac_address); |
| 8512 | |
| 8513 | static DECLARE_RWSEM(dev_addr_sem); |
| 8514 | |
| 8515 | int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa, |
| 8516 | struct netlink_ext_ack *extack) |
| 8517 | { |
| 8518 | int ret; |
| 8519 | |
| 8520 | down_write(&dev_addr_sem); |
| 8521 | ret = dev_set_mac_address(dev, sa, extack); |
| 8522 | up_write(&dev_addr_sem); |
| 8523 | return ret; |
| 8524 | } |
| 8525 | EXPORT_SYMBOL(dev_set_mac_address_user); |
| 8526 | |
| 8527 | int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name) |
| 8528 | { |
| 8529 | size_t size = sizeof(sa->sa_data); |
| 8530 | struct net_device *dev; |
| 8531 | int ret = 0; |
| 8532 | |
| 8533 | down_read(&dev_addr_sem); |
| 8534 | rcu_read_lock(); |
| 8535 | |
| 8536 | dev = dev_get_by_name_rcu(net, dev_name); |
| 8537 | if (!dev) { |
| 8538 | ret = -ENODEV; |
| 8539 | goto unlock; |
| 8540 | } |
| 8541 | if (!dev->addr_len) |
| 8542 | memset(sa->sa_data, 0, size); |
| 8543 | else |
| 8544 | memcpy(sa->sa_data, dev->dev_addr, |
| 8545 | min_t(size_t, size, dev->addr_len)); |
| 8546 | sa->sa_family = dev->type; |
| 8547 | |
| 8548 | unlock: |
| 8549 | rcu_read_unlock(); |
| 8550 | up_read(&dev_addr_sem); |
| 8551 | return ret; |
| 8552 | } |
| 8553 | EXPORT_SYMBOL(dev_get_mac_address); |
| 8554 | |
| 8555 | /** |
| 8556 | * dev_change_carrier - Change device carrier |
| 8557 | * @dev: device |
| 8558 | * @new_carrier: new value |
| 8559 | * |
| 8560 | * Change device carrier |
| 8561 | */ |
| 8562 | int dev_change_carrier(struct net_device *dev, bool new_carrier) |
| 8563 | { |
| 8564 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8565 | |
| 8566 | if (!ops->ndo_change_carrier) |
| 8567 | return -EOPNOTSUPP; |
| 8568 | if (!netif_device_present(dev)) |
| 8569 | return -ENODEV; |
| 8570 | return ops->ndo_change_carrier(dev, new_carrier); |
| 8571 | } |
| 8572 | EXPORT_SYMBOL(dev_change_carrier); |
| 8573 | |
| 8574 | /** |
| 8575 | * dev_get_phys_port_id - Get device physical port ID |
| 8576 | * @dev: device |
| 8577 | * @ppid: port ID |
| 8578 | * |
| 8579 | * Get device physical port ID |
| 8580 | */ |
| 8581 | int dev_get_phys_port_id(struct net_device *dev, |
| 8582 | struct netdev_phys_item_id *ppid) |
| 8583 | { |
| 8584 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8585 | |
| 8586 | if (!ops->ndo_get_phys_port_id) |
| 8587 | return -EOPNOTSUPP; |
| 8588 | return ops->ndo_get_phys_port_id(dev, ppid); |
| 8589 | } |
| 8590 | EXPORT_SYMBOL(dev_get_phys_port_id); |
| 8591 | |
| 8592 | /** |
| 8593 | * dev_get_phys_port_name - Get device physical port name |
| 8594 | * @dev: device |
| 8595 | * @name: port name |
| 8596 | * @len: limit of bytes to copy to name |
| 8597 | * |
| 8598 | * Get device physical port name |
| 8599 | */ |
| 8600 | int dev_get_phys_port_name(struct net_device *dev, |
| 8601 | char *name, size_t len) |
| 8602 | { |
| 8603 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8604 | int err; |
| 8605 | |
| 8606 | if (ops->ndo_get_phys_port_name) { |
| 8607 | err = ops->ndo_get_phys_port_name(dev, name, len); |
| 8608 | if (err != -EOPNOTSUPP) |
| 8609 | return err; |
| 8610 | } |
| 8611 | return devlink_compat_phys_port_name_get(dev, name, len); |
| 8612 | } |
| 8613 | EXPORT_SYMBOL(dev_get_phys_port_name); |
| 8614 | |
| 8615 | /** |
| 8616 | * dev_get_port_parent_id - Get the device's port parent identifier |
| 8617 | * @dev: network device |
| 8618 | * @ppid: pointer to a storage for the port's parent identifier |
| 8619 | * @recurse: allow/disallow recursion to lower devices |
| 8620 | * |
| 8621 | * Get the devices's port parent identifier |
| 8622 | */ |
| 8623 | int dev_get_port_parent_id(struct net_device *dev, |
| 8624 | struct netdev_phys_item_id *ppid, |
| 8625 | bool recurse) |
| 8626 | { |
| 8627 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8628 | struct netdev_phys_item_id first = { }; |
| 8629 | struct net_device *lower_dev; |
| 8630 | struct list_head *iter; |
| 8631 | int err; |
| 8632 | |
| 8633 | if (ops->ndo_get_port_parent_id) { |
| 8634 | err = ops->ndo_get_port_parent_id(dev, ppid); |
| 8635 | if (err != -EOPNOTSUPP) |
| 8636 | return err; |
| 8637 | } |
| 8638 | |
| 8639 | err = devlink_compat_switch_id_get(dev, ppid); |
| 8640 | if (!err || err != -EOPNOTSUPP) |
| 8641 | return err; |
| 8642 | |
| 8643 | if (!recurse) |
| 8644 | return -EOPNOTSUPP; |
| 8645 | |
| 8646 | netdev_for_each_lower_dev(dev, lower_dev, iter) { |
| 8647 | err = dev_get_port_parent_id(lower_dev, ppid, recurse); |
| 8648 | if (err) |
| 8649 | break; |
| 8650 | if (!first.id_len) |
| 8651 | first = *ppid; |
| 8652 | else if (memcmp(&first, ppid, sizeof(*ppid))) |
| 8653 | return -EOPNOTSUPP; |
| 8654 | } |
| 8655 | |
| 8656 | return err; |
| 8657 | } |
| 8658 | EXPORT_SYMBOL(dev_get_port_parent_id); |
| 8659 | |
| 8660 | /** |
| 8661 | * netdev_port_same_parent_id - Indicate if two network devices have |
| 8662 | * the same port parent identifier |
| 8663 | * @a: first network device |
| 8664 | * @b: second network device |
| 8665 | */ |
| 8666 | bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b) |
| 8667 | { |
| 8668 | struct netdev_phys_item_id a_id = { }; |
| 8669 | struct netdev_phys_item_id b_id = { }; |
| 8670 | |
| 8671 | if (dev_get_port_parent_id(a, &a_id, true) || |
| 8672 | dev_get_port_parent_id(b, &b_id, true)) |
| 8673 | return false; |
| 8674 | |
| 8675 | return netdev_phys_item_id_same(&a_id, &b_id); |
| 8676 | } |
| 8677 | EXPORT_SYMBOL(netdev_port_same_parent_id); |
| 8678 | |
| 8679 | /** |
| 8680 | * dev_change_proto_down - update protocol port state information |
| 8681 | * @dev: device |
| 8682 | * @proto_down: new value |
| 8683 | * |
| 8684 | * This info can be used by switch drivers to set the phys state of the |
| 8685 | * port. |
| 8686 | */ |
| 8687 | int dev_change_proto_down(struct net_device *dev, bool proto_down) |
| 8688 | { |
| 8689 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8690 | |
| 8691 | if (!ops->ndo_change_proto_down) |
| 8692 | return -EOPNOTSUPP; |
| 8693 | if (!netif_device_present(dev)) |
| 8694 | return -ENODEV; |
| 8695 | return ops->ndo_change_proto_down(dev, proto_down); |
| 8696 | } |
| 8697 | EXPORT_SYMBOL(dev_change_proto_down); |
| 8698 | |
| 8699 | /** |
| 8700 | * dev_change_proto_down_generic - generic implementation for |
| 8701 | * ndo_change_proto_down that sets carrier according to |
| 8702 | * proto_down. |
| 8703 | * |
| 8704 | * @dev: device |
| 8705 | * @proto_down: new value |
| 8706 | */ |
| 8707 | int dev_change_proto_down_generic(struct net_device *dev, bool proto_down) |
| 8708 | { |
| 8709 | if (proto_down) |
| 8710 | netif_carrier_off(dev); |
| 8711 | else |
| 8712 | netif_carrier_on(dev); |
| 8713 | dev->proto_down = proto_down; |
| 8714 | return 0; |
| 8715 | } |
| 8716 | EXPORT_SYMBOL(dev_change_proto_down_generic); |
| 8717 | |
| 8718 | u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op, |
| 8719 | enum bpf_netdev_command cmd) |
| 8720 | { |
| 8721 | struct netdev_bpf xdp; |
| 8722 | |
| 8723 | if (!bpf_op) |
| 8724 | return 0; |
| 8725 | |
| 8726 | memset(&xdp, 0, sizeof(xdp)); |
| 8727 | xdp.command = cmd; |
| 8728 | |
| 8729 | /* Query must always succeed. */ |
| 8730 | WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG); |
| 8731 | |
| 8732 | return xdp.prog_id; |
| 8733 | } |
| 8734 | |
| 8735 | static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op, |
| 8736 | struct netlink_ext_ack *extack, u32 flags, |
| 8737 | struct bpf_prog *prog) |
| 8738 | { |
| 8739 | struct netdev_bpf xdp; |
| 8740 | |
| 8741 | memset(&xdp, 0, sizeof(xdp)); |
| 8742 | if (flags & XDP_FLAGS_HW_MODE) |
| 8743 | xdp.command = XDP_SETUP_PROG_HW; |
| 8744 | else |
| 8745 | xdp.command = XDP_SETUP_PROG; |
| 8746 | xdp.extack = extack; |
| 8747 | xdp.flags = flags; |
| 8748 | xdp.prog = prog; |
| 8749 | |
| 8750 | return bpf_op(dev, &xdp); |
| 8751 | } |
| 8752 | |
| 8753 | static void dev_xdp_uninstall(struct net_device *dev) |
| 8754 | { |
| 8755 | struct netdev_bpf xdp; |
| 8756 | bpf_op_t ndo_bpf; |
| 8757 | |
| 8758 | /* Remove generic XDP */ |
| 8759 | WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL)); |
| 8760 | |
| 8761 | /* Remove from the driver */ |
| 8762 | ndo_bpf = dev->netdev_ops->ndo_bpf; |
| 8763 | if (!ndo_bpf) |
| 8764 | return; |
| 8765 | |
| 8766 | memset(&xdp, 0, sizeof(xdp)); |
| 8767 | xdp.command = XDP_QUERY_PROG; |
| 8768 | WARN_ON(ndo_bpf(dev, &xdp)); |
| 8769 | if (xdp.prog_id) |
| 8770 | WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags, |
| 8771 | NULL)); |
| 8772 | |
| 8773 | /* Remove HW offload */ |
| 8774 | memset(&xdp, 0, sizeof(xdp)); |
| 8775 | xdp.command = XDP_QUERY_PROG_HW; |
| 8776 | if (!ndo_bpf(dev, &xdp) && xdp.prog_id) |
| 8777 | WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags, |
| 8778 | NULL)); |
| 8779 | } |
| 8780 | |
| 8781 | /** |
| 8782 | * dev_change_xdp_fd - set or clear a bpf program for a device rx path |
| 8783 | * @dev: device |
| 8784 | * @extack: netlink extended ack |
| 8785 | * @fd: new program fd or negative value to clear |
| 8786 | * @flags: xdp-related flags |
| 8787 | * |
| 8788 | * Set or clear a bpf program for a device |
| 8789 | */ |
| 8790 | int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, |
| 8791 | int fd, u32 flags) |
| 8792 | { |
| 8793 | const struct net_device_ops *ops = dev->netdev_ops; |
| 8794 | enum bpf_netdev_command query; |
| 8795 | struct bpf_prog *prog = NULL; |
| 8796 | bpf_op_t bpf_op, bpf_chk; |
| 8797 | bool offload; |
| 8798 | int err; |
| 8799 | |
| 8800 | ASSERT_RTNL(); |
| 8801 | |
| 8802 | offload = flags & XDP_FLAGS_HW_MODE; |
| 8803 | query = offload ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG; |
| 8804 | |
| 8805 | bpf_op = bpf_chk = ops->ndo_bpf; |
| 8806 | if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE))) { |
| 8807 | NL_SET_ERR_MSG(extack, "underlying driver does not support XDP in native mode"); |
| 8808 | return -EOPNOTSUPP; |
| 8809 | } |
| 8810 | if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE)) |
| 8811 | bpf_op = generic_xdp_install; |
| 8812 | if (bpf_op == bpf_chk) |
| 8813 | bpf_chk = generic_xdp_install; |
| 8814 | |
| 8815 | if (fd >= 0) { |
| 8816 | u32 prog_id; |
| 8817 | |
| 8818 | if (!offload && __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG)) { |
| 8819 | NL_SET_ERR_MSG(extack, "native and generic XDP can't be active at the same time"); |
| 8820 | return -EEXIST; |
| 8821 | } |
| 8822 | |
| 8823 | prog_id = __dev_xdp_query(dev, bpf_op, query); |
| 8824 | if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) { |
| 8825 | NL_SET_ERR_MSG(extack, "XDP program already attached"); |
| 8826 | return -EBUSY; |
| 8827 | } |
| 8828 | |
| 8829 | prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, |
| 8830 | bpf_op == ops->ndo_bpf); |
| 8831 | if (IS_ERR(prog)) |
| 8832 | return PTR_ERR(prog); |
| 8833 | |
| 8834 | if (!offload && bpf_prog_is_dev_bound(prog->aux)) { |
| 8835 | NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported"); |
| 8836 | bpf_prog_put(prog); |
| 8837 | return -EINVAL; |
| 8838 | } |
| 8839 | |
| 8840 | /* prog->aux->id may be 0 for orphaned device-bound progs */ |
| 8841 | if (prog->aux->id && prog->aux->id == prog_id) { |
| 8842 | bpf_prog_put(prog); |
| 8843 | return 0; |
| 8844 | } |
| 8845 | } else { |
| 8846 | if (!__dev_xdp_query(dev, bpf_op, query)) |
| 8847 | return 0; |
| 8848 | } |
| 8849 | |
| 8850 | err = dev_xdp_install(dev, bpf_op, extack, flags, prog); |
| 8851 | if (err < 0 && prog) |
| 8852 | bpf_prog_put(prog); |
| 8853 | |
| 8854 | return err; |
| 8855 | } |
| 8856 | |
| 8857 | /** |
| 8858 | * dev_new_index - allocate an ifindex |
| 8859 | * @net: the applicable net namespace |
| 8860 | * |
| 8861 | * Returns a suitable unique value for a new device interface |
| 8862 | * number. The caller must hold the rtnl semaphore or the |
| 8863 | * dev_base_lock to be sure it remains unique. |
| 8864 | */ |
| 8865 | static int dev_new_index(struct net *net) |
| 8866 | { |
| 8867 | int ifindex = net->ifindex; |
| 8868 | |
| 8869 | for (;;) { |
| 8870 | if (++ifindex <= 0) |
| 8871 | ifindex = 1; |
| 8872 | if (!__dev_get_by_index(net, ifindex)) |
| 8873 | return net->ifindex = ifindex; |
| 8874 | } |
| 8875 | } |
| 8876 | |
| 8877 | /* Delayed registration/unregisteration */ |
| 8878 | static LIST_HEAD(net_todo_list); |
| 8879 | DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq); |
| 8880 | |
| 8881 | static void net_set_todo(struct net_device *dev) |
| 8882 | { |
| 8883 | list_add_tail(&dev->todo_list, &net_todo_list); |
| 8884 | dev_net(dev)->dev_unreg_count++; |
| 8885 | } |
| 8886 | |
| 8887 | static void rollback_registered_many(struct list_head *head) |
| 8888 | { |
| 8889 | struct net_device *dev, *tmp; |
| 8890 | LIST_HEAD(close_head); |
| 8891 | |
| 8892 | BUG_ON(dev_boot_phase); |
| 8893 | ASSERT_RTNL(); |
| 8894 | |
| 8895 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
| 8896 | /* Some devices call without registering |
| 8897 | * for initialization unwind. Remove those |
| 8898 | * devices and proceed with the remaining. |
| 8899 | */ |
| 8900 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
| 8901 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", |
| 8902 | dev->name, dev); |
| 8903 | |
| 8904 | WARN_ON(1); |
| 8905 | list_del(&dev->unreg_list); |
| 8906 | continue; |
| 8907 | } |
| 8908 | dev->dismantle = true; |
| 8909 | BUG_ON(dev->reg_state != NETREG_REGISTERED); |
| 8910 | } |
| 8911 | |
| 8912 | /* If device is running, close it first. */ |
| 8913 | list_for_each_entry(dev, head, unreg_list) |
| 8914 | list_add_tail(&dev->close_list, &close_head); |
| 8915 | dev_close_many(&close_head, true); |
| 8916 | |
| 8917 | list_for_each_entry(dev, head, unreg_list) { |
| 8918 | /* And unlink it from device chain. */ |
| 8919 | unlist_netdevice(dev); |
| 8920 | |
| 8921 | dev->reg_state = NETREG_UNREGISTERING; |
| 8922 | } |
| 8923 | flush_all_backlogs(); |
| 8924 | |
| 8925 | synchronize_net(); |
| 8926 | |
| 8927 | list_for_each_entry(dev, head, unreg_list) { |
| 8928 | struct sk_buff *skb = NULL; |
| 8929 | |
| 8930 | /* Shutdown queueing discipline. */ |
| 8931 | dev_shutdown(dev); |
| 8932 | |
| 8933 | dev_xdp_uninstall(dev); |
| 8934 | |
| 8935 | /* Notify protocols, that we are about to destroy |
| 8936 | * this device. They should clean all the things. |
| 8937 | */ |
| 8938 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
| 8939 | |
| 8940 | if (!dev->rtnl_link_ops || |
| 8941 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) |
| 8942 | skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0, |
| 8943 | GFP_KERNEL, NULL, 0); |
| 8944 | |
| 8945 | /* |
| 8946 | * Flush the unicast and multicast chains |
| 8947 | */ |
| 8948 | dev_uc_flush(dev); |
| 8949 | dev_mc_flush(dev); |
| 8950 | |
| 8951 | if (dev->netdev_ops->ndo_uninit) |
| 8952 | dev->netdev_ops->ndo_uninit(dev); |
| 8953 | |
| 8954 | if (skb) |
| 8955 | rtmsg_ifinfo_send(skb, dev, GFP_KERNEL); |
| 8956 | |
| 8957 | /* Notifier chain MUST detach us all upper devices. */ |
| 8958 | WARN_ON(netdev_has_any_upper_dev(dev)); |
| 8959 | WARN_ON(netdev_has_any_lower_dev(dev)); |
| 8960 | |
| 8961 | /* Remove entries from kobject tree */ |
| 8962 | netdev_unregister_kobject(dev); |
| 8963 | #ifdef CONFIG_XPS |
| 8964 | /* Remove XPS queueing entries */ |
| 8965 | netif_reset_xps_queues_gt(dev, 0); |
| 8966 | #endif |
| 8967 | } |
| 8968 | |
| 8969 | synchronize_net(); |
| 8970 | |
| 8971 | list_for_each_entry(dev, head, unreg_list) |
| 8972 | dev_put(dev); |
| 8973 | } |
| 8974 | |
| 8975 | static void rollback_registered(struct net_device *dev) |
| 8976 | { |
| 8977 | LIST_HEAD(single); |
| 8978 | |
| 8979 | list_add(&dev->unreg_list, &single); |
| 8980 | rollback_registered_many(&single); |
| 8981 | list_del(&single); |
| 8982 | } |
| 8983 | |
| 8984 | static netdev_features_t netdev_sync_upper_features(struct net_device *lower, |
| 8985 | struct net_device *upper, netdev_features_t features) |
| 8986 | { |
| 8987 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; |
| 8988 | netdev_features_t feature; |
| 8989 | int feature_bit; |
| 8990 | |
| 8991 | for_each_netdev_feature(upper_disables, feature_bit) { |
| 8992 | feature = __NETIF_F_BIT(feature_bit); |
| 8993 | if (!(upper->wanted_features & feature) |
| 8994 | && (features & feature)) { |
| 8995 | netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n", |
| 8996 | &feature, upper->name); |
| 8997 | features &= ~feature; |
| 8998 | } |
| 8999 | } |
| 9000 | |
| 9001 | return features; |
| 9002 | } |
| 9003 | |
| 9004 | static void netdev_sync_lower_features(struct net_device *upper, |
| 9005 | struct net_device *lower, netdev_features_t features) |
| 9006 | { |
| 9007 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; |
| 9008 | netdev_features_t feature; |
| 9009 | int feature_bit; |
| 9010 | |
| 9011 | for_each_netdev_feature(upper_disables, feature_bit) { |
| 9012 | feature = __NETIF_F_BIT(feature_bit); |
| 9013 | if (!(features & feature) && (lower->features & feature)) { |
| 9014 | netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n", |
| 9015 | &feature, lower->name); |
| 9016 | lower->wanted_features &= ~feature; |
| 9017 | __netdev_update_features(lower); |
| 9018 | |
| 9019 | if (unlikely(lower->features & feature)) |
| 9020 | netdev_WARN(upper, "failed to disable %pNF on %s!\n", |
| 9021 | &feature, lower->name); |
| 9022 | else |
| 9023 | netdev_features_change(lower); |
| 9024 | } |
| 9025 | } |
| 9026 | } |
| 9027 | |
| 9028 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
| 9029 | netdev_features_t features) |
| 9030 | { |
| 9031 | /* Fix illegal checksum combinations */ |
| 9032 | if ((features & NETIF_F_HW_CSUM) && |
| 9033 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { |
| 9034 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
| 9035 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
| 9036 | } |
| 9037 | |
| 9038 | /* TSO requires that SG is present as well. */ |
| 9039 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
| 9040 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
| 9041 | features &= ~NETIF_F_ALL_TSO; |
| 9042 | } |
| 9043 | |
| 9044 | if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && |
| 9045 | !(features & NETIF_F_IP_CSUM)) { |
| 9046 | netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); |
| 9047 | features &= ~NETIF_F_TSO; |
| 9048 | features &= ~NETIF_F_TSO_ECN; |
| 9049 | } |
| 9050 | |
| 9051 | if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && |
| 9052 | !(features & NETIF_F_IPV6_CSUM)) { |
| 9053 | netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); |
| 9054 | features &= ~NETIF_F_TSO6; |
| 9055 | } |
| 9056 | |
| 9057 | /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */ |
| 9058 | if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO)) |
| 9059 | features &= ~NETIF_F_TSO_MANGLEID; |
| 9060 | |
| 9061 | /* TSO ECN requires that TSO is present as well. */ |
| 9062 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) |
| 9063 | features &= ~NETIF_F_TSO_ECN; |
| 9064 | |
| 9065 | /* Software GSO depends on SG. */ |
| 9066 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { |
| 9067 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
| 9068 | features &= ~NETIF_F_GSO; |
| 9069 | } |
| 9070 | |
| 9071 | /* GSO partial features require GSO partial be set */ |
| 9072 | if ((features & dev->gso_partial_features) && |
| 9073 | !(features & NETIF_F_GSO_PARTIAL)) { |
| 9074 | netdev_dbg(dev, |
| 9075 | "Dropping partially supported GSO features since no GSO partial.\n"); |
| 9076 | features &= ~dev->gso_partial_features; |
| 9077 | } |
| 9078 | |
| 9079 | if (!(features & NETIF_F_RXCSUM)) { |
| 9080 | /* NETIF_F_GRO_HW implies doing RXCSUM since every packet |
| 9081 | * successfully merged by hardware must also have the |
| 9082 | * checksum verified by hardware. If the user does not |
| 9083 | * want to enable RXCSUM, logically, we should disable GRO_HW. |
| 9084 | */ |
| 9085 | if (features & NETIF_F_GRO_HW) { |
| 9086 | netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n"); |
| 9087 | features &= ~NETIF_F_GRO_HW; |
| 9088 | } |
| 9089 | } |
| 9090 | |
| 9091 | /* LRO/HW-GRO features cannot be combined with RX-FCS */ |
| 9092 | if (features & NETIF_F_RXFCS) { |
| 9093 | if (features & NETIF_F_LRO) { |
| 9094 | netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n"); |
| 9095 | features &= ~NETIF_F_LRO; |
| 9096 | } |
| 9097 | |
| 9098 | if (features & NETIF_F_GRO_HW) { |
| 9099 | netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n"); |
| 9100 | features &= ~NETIF_F_GRO_HW; |
| 9101 | } |
| 9102 | } |
| 9103 | |
| 9104 | if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) { |
| 9105 | netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n"); |
| 9106 | features &= ~NETIF_F_HW_TLS_RX; |
| 9107 | } |
| 9108 | |
| 9109 | return features; |
| 9110 | } |
| 9111 | |
| 9112 | int __netdev_update_features(struct net_device *dev) |
| 9113 | { |
| 9114 | struct net_device *upper, *lower; |
| 9115 | netdev_features_t features; |
| 9116 | struct list_head *iter; |
| 9117 | int err = -1; |
| 9118 | |
| 9119 | ASSERT_RTNL(); |
| 9120 | |
| 9121 | features = netdev_get_wanted_features(dev); |
| 9122 | |
| 9123 | if (dev->netdev_ops->ndo_fix_features) |
| 9124 | features = dev->netdev_ops->ndo_fix_features(dev, features); |
| 9125 | |
| 9126 | /* driver might be less strict about feature dependencies */ |
| 9127 | features = netdev_fix_features(dev, features); |
| 9128 | |
| 9129 | /* some features can't be enabled if they're off an an upper device */ |
| 9130 | netdev_for_each_upper_dev_rcu(dev, upper, iter) |
| 9131 | features = netdev_sync_upper_features(dev, upper, features); |
| 9132 | |
| 9133 | if (dev->features == features) |
| 9134 | goto sync_lower; |
| 9135 | |
| 9136 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
| 9137 | &dev->features, &features); |
| 9138 | |
| 9139 | if (dev->netdev_ops->ndo_set_features) |
| 9140 | err = dev->netdev_ops->ndo_set_features(dev, features); |
| 9141 | else |
| 9142 | err = 0; |
| 9143 | |
| 9144 | if (unlikely(err < 0)) { |
| 9145 | netdev_err(dev, |
| 9146 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
| 9147 | err, &features, &dev->features); |
| 9148 | /* return non-0 since some features might have changed and |
| 9149 | * it's better to fire a spurious notification than miss it |
| 9150 | */ |
| 9151 | return -1; |
| 9152 | } |
| 9153 | |
| 9154 | sync_lower: |
| 9155 | /* some features must be disabled on lower devices when disabled |
| 9156 | * on an upper device (think: bonding master or bridge) |
| 9157 | */ |
| 9158 | netdev_for_each_lower_dev(dev, lower, iter) |
| 9159 | netdev_sync_lower_features(dev, lower, features); |
| 9160 | |
| 9161 | if (!err) { |
| 9162 | netdev_features_t diff = features ^ dev->features; |
| 9163 | |
| 9164 | if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) { |
| 9165 | /* udp_tunnel_{get,drop}_rx_info both need |
| 9166 | * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the |
| 9167 | * device, or they won't do anything. |
| 9168 | * Thus we need to update dev->features |
| 9169 | * *before* calling udp_tunnel_get_rx_info, |
| 9170 | * but *after* calling udp_tunnel_drop_rx_info. |
| 9171 | */ |
| 9172 | if (features & NETIF_F_RX_UDP_TUNNEL_PORT) { |
| 9173 | dev->features = features; |
| 9174 | udp_tunnel_get_rx_info(dev); |
| 9175 | } else { |
| 9176 | udp_tunnel_drop_rx_info(dev); |
| 9177 | } |
| 9178 | } |
| 9179 | |
| 9180 | if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) { |
| 9181 | if (features & NETIF_F_HW_VLAN_CTAG_FILTER) { |
| 9182 | dev->features = features; |
| 9183 | err |= vlan_get_rx_ctag_filter_info(dev); |
| 9184 | } else { |
| 9185 | vlan_drop_rx_ctag_filter_info(dev); |
| 9186 | } |
| 9187 | } |
| 9188 | |
| 9189 | if (diff & NETIF_F_HW_VLAN_STAG_FILTER) { |
| 9190 | if (features & NETIF_F_HW_VLAN_STAG_FILTER) { |
| 9191 | dev->features = features; |
| 9192 | err |= vlan_get_rx_stag_filter_info(dev); |
| 9193 | } else { |
| 9194 | vlan_drop_rx_stag_filter_info(dev); |
| 9195 | } |
| 9196 | } |
| 9197 | |
| 9198 | dev->features = features; |
| 9199 | } |
| 9200 | |
| 9201 | return err < 0 ? 0 : 1; |
| 9202 | } |
| 9203 | |
| 9204 | /** |
| 9205 | * netdev_update_features - recalculate device features |
| 9206 | * @dev: the device to check |
| 9207 | * |
| 9208 | * Recalculate dev->features set and send notifications if it |
| 9209 | * has changed. Should be called after driver or hardware dependent |
| 9210 | * conditions might have changed that influence the features. |
| 9211 | */ |
| 9212 | void netdev_update_features(struct net_device *dev) |
| 9213 | { |
| 9214 | if (__netdev_update_features(dev)) |
| 9215 | netdev_features_change(dev); |
| 9216 | } |
| 9217 | EXPORT_SYMBOL(netdev_update_features); |
| 9218 | |
| 9219 | /** |
| 9220 | * netdev_change_features - recalculate device features |
| 9221 | * @dev: the device to check |
| 9222 | * |
| 9223 | * Recalculate dev->features set and send notifications even |
| 9224 | * if they have not changed. Should be called instead of |
| 9225 | * netdev_update_features() if also dev->vlan_features might |
| 9226 | * have changed to allow the changes to be propagated to stacked |
| 9227 | * VLAN devices. |
| 9228 | */ |
| 9229 | void netdev_change_features(struct net_device *dev) |
| 9230 | { |
| 9231 | __netdev_update_features(dev); |
| 9232 | netdev_features_change(dev); |
| 9233 | } |
| 9234 | EXPORT_SYMBOL(netdev_change_features); |
| 9235 | |
| 9236 | /** |
| 9237 | * netif_stacked_transfer_operstate - transfer operstate |
| 9238 | * @rootdev: the root or lower level device to transfer state from |
| 9239 | * @dev: the device to transfer operstate to |
| 9240 | * |
| 9241 | * Transfer operational state from root to device. This is normally |
| 9242 | * called when a stacking relationship exists between the root |
| 9243 | * device and the device(a leaf device). |
| 9244 | */ |
| 9245 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, |
| 9246 | struct net_device *dev) |
| 9247 | { |
| 9248 | if (rootdev->operstate == IF_OPER_DORMANT) |
| 9249 | netif_dormant_on(dev); |
| 9250 | else |
| 9251 | netif_dormant_off(dev); |
| 9252 | |
| 9253 | if (netif_carrier_ok(rootdev)) |
| 9254 | netif_carrier_on(dev); |
| 9255 | else |
| 9256 | netif_carrier_off(dev); |
| 9257 | } |
| 9258 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); |
| 9259 | |
| 9260 | static int netif_alloc_rx_queues(struct net_device *dev) |
| 9261 | { |
| 9262 | unsigned int i, count = dev->num_rx_queues; |
| 9263 | struct netdev_rx_queue *rx; |
| 9264 | size_t sz = count * sizeof(*rx); |
| 9265 | int err = 0; |
| 9266 | |
| 9267 | BUG_ON(count < 1); |
| 9268 | |
| 9269 | rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
| 9270 | if (!rx) |
| 9271 | return -ENOMEM; |
| 9272 | |
| 9273 | dev->_rx = rx; |
| 9274 | |
| 9275 | for (i = 0; i < count; i++) { |
| 9276 | rx[i].dev = dev; |
| 9277 | |
| 9278 | /* XDP RX-queue setup */ |
| 9279 | err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i); |
| 9280 | if (err < 0) |
| 9281 | goto err_rxq_info; |
| 9282 | } |
| 9283 | return 0; |
| 9284 | |
| 9285 | err_rxq_info: |
| 9286 | /* Rollback successful reg's and free other resources */ |
| 9287 | while (i--) |
| 9288 | xdp_rxq_info_unreg(&rx[i].xdp_rxq); |
| 9289 | kvfree(dev->_rx); |
| 9290 | dev->_rx = NULL; |
| 9291 | return err; |
| 9292 | } |
| 9293 | |
| 9294 | static void netif_free_rx_queues(struct net_device *dev) |
| 9295 | { |
| 9296 | unsigned int i, count = dev->num_rx_queues; |
| 9297 | |
| 9298 | /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */ |
| 9299 | if (!dev->_rx) |
| 9300 | return; |
| 9301 | |
| 9302 | for (i = 0; i < count; i++) |
| 9303 | xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq); |
| 9304 | |
| 9305 | kvfree(dev->_rx); |
| 9306 | } |
| 9307 | |
| 9308 | static void netdev_init_one_queue(struct net_device *dev, |
| 9309 | struct netdev_queue *queue, void *_unused) |
| 9310 | { |
| 9311 | /* Initialize queue lock */ |
| 9312 | spin_lock_init(&queue->_xmit_lock); |
| 9313 | lockdep_set_class(&queue->_xmit_lock, &dev->qdisc_xmit_lock_key); |
| 9314 | queue->xmit_lock_owner = -1; |
| 9315 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
| 9316 | queue->dev = dev; |
| 9317 | #ifdef CONFIG_BQL |
| 9318 | dql_init(&queue->dql, HZ); |
| 9319 | #endif |
| 9320 | } |
| 9321 | |
| 9322 | static void netif_free_tx_queues(struct net_device *dev) |
| 9323 | { |
| 9324 | kvfree(dev->_tx); |
| 9325 | } |
| 9326 | |
| 9327 | static int netif_alloc_netdev_queues(struct net_device *dev) |
| 9328 | { |
| 9329 | unsigned int count = dev->num_tx_queues; |
| 9330 | struct netdev_queue *tx; |
| 9331 | size_t sz = count * sizeof(*tx); |
| 9332 | |
| 9333 | if (count < 1 || count > 0xffff) |
| 9334 | return -EINVAL; |
| 9335 | |
| 9336 | tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
| 9337 | if (!tx) |
| 9338 | return -ENOMEM; |
| 9339 | |
| 9340 | dev->_tx = tx; |
| 9341 | |
| 9342 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
| 9343 | spin_lock_init(&dev->tx_global_lock); |
| 9344 | |
| 9345 | return 0; |
| 9346 | } |
| 9347 | |
| 9348 | void netif_tx_stop_all_queues(struct net_device *dev) |
| 9349 | { |
| 9350 | unsigned int i; |
| 9351 | |
| 9352 | for (i = 0; i < dev->num_tx_queues; i++) { |
| 9353 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); |
| 9354 | |
| 9355 | netif_tx_stop_queue(txq); |
| 9356 | } |
| 9357 | } |
| 9358 | EXPORT_SYMBOL(netif_tx_stop_all_queues); |
| 9359 | |
| 9360 | static void netdev_register_lockdep_key(struct net_device *dev) |
| 9361 | { |
| 9362 | lockdep_register_key(&dev->qdisc_tx_busylock_key); |
| 9363 | lockdep_register_key(&dev->qdisc_running_key); |
| 9364 | lockdep_register_key(&dev->qdisc_xmit_lock_key); |
| 9365 | lockdep_register_key(&dev->addr_list_lock_key); |
| 9366 | } |
| 9367 | |
| 9368 | static void netdev_unregister_lockdep_key(struct net_device *dev) |
| 9369 | { |
| 9370 | lockdep_unregister_key(&dev->qdisc_tx_busylock_key); |
| 9371 | lockdep_unregister_key(&dev->qdisc_running_key); |
| 9372 | lockdep_unregister_key(&dev->qdisc_xmit_lock_key); |
| 9373 | lockdep_unregister_key(&dev->addr_list_lock_key); |
| 9374 | } |
| 9375 | |
| 9376 | void netdev_update_lockdep_key(struct net_device *dev) |
| 9377 | { |
| 9378 | lockdep_unregister_key(&dev->addr_list_lock_key); |
| 9379 | lockdep_register_key(&dev->addr_list_lock_key); |
| 9380 | |
| 9381 | lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key); |
| 9382 | } |
| 9383 | EXPORT_SYMBOL(netdev_update_lockdep_key); |
| 9384 | |
| 9385 | /** |
| 9386 | * register_netdevice - register a network device |
| 9387 | * @dev: device to register |
| 9388 | * |
| 9389 | * Take a completed network device structure and add it to the kernel |
| 9390 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier |
| 9391 | * chain. 0 is returned on success. A negative errno code is returned |
| 9392 | * on a failure to set up the device, or if the name is a duplicate. |
| 9393 | * |
| 9394 | * Callers must hold the rtnl semaphore. You may want |
| 9395 | * register_netdev() instead of this. |
| 9396 | * |
| 9397 | * BUGS: |
| 9398 | * The locking appears insufficient to guarantee two parallel registers |
| 9399 | * will not get the same name. |
| 9400 | */ |
| 9401 | |
| 9402 | int register_netdevice(struct net_device *dev) |
| 9403 | { |
| 9404 | int ret; |
| 9405 | struct net *net = dev_net(dev); |
| 9406 | |
| 9407 | BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE < |
| 9408 | NETDEV_FEATURE_COUNT); |
| 9409 | BUG_ON(dev_boot_phase); |
| 9410 | ASSERT_RTNL(); |
| 9411 | |
| 9412 | might_sleep(); |
| 9413 | |
| 9414 | /* When net_device's are persistent, this will be fatal. */ |
| 9415 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); |
| 9416 | BUG_ON(!net); |
| 9417 | |
| 9418 | spin_lock_init(&dev->addr_list_lock); |
| 9419 | lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key); |
| 9420 | |
| 9421 | ret = dev_get_valid_name(net, dev, dev->name); |
| 9422 | if (ret < 0) |
| 9423 | goto out; |
| 9424 | |
| 9425 | /* Init, if this function is available */ |
| 9426 | if (dev->netdev_ops->ndo_init) { |
| 9427 | ret = dev->netdev_ops->ndo_init(dev); |
| 9428 | if (ret) { |
| 9429 | if (ret > 0) |
| 9430 | ret = -EIO; |
| 9431 | goto out; |
| 9432 | } |
| 9433 | } |
| 9434 | |
| 9435 | if (((dev->hw_features | dev->features) & |
| 9436 | NETIF_F_HW_VLAN_CTAG_FILTER) && |
| 9437 | (!dev->netdev_ops->ndo_vlan_rx_add_vid || |
| 9438 | !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { |
| 9439 | netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); |
| 9440 | ret = -EINVAL; |
| 9441 | goto err_uninit; |
| 9442 | } |
| 9443 | |
| 9444 | ret = -EBUSY; |
| 9445 | if (!dev->ifindex) |
| 9446 | dev->ifindex = dev_new_index(net); |
| 9447 | else if (__dev_get_by_index(net, dev->ifindex)) |
| 9448 | goto err_uninit; |
| 9449 | |
| 9450 | /* Transfer changeable features to wanted_features and enable |
| 9451 | * software offloads (GSO and GRO). |
| 9452 | */ |
| 9453 | dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF); |
| 9454 | dev->features |= NETIF_F_SOFT_FEATURES; |
| 9455 | |
| 9456 | if (dev->netdev_ops->ndo_udp_tunnel_add) { |
| 9457 | dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT; |
| 9458 | dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT; |
| 9459 | } |
| 9460 | |
| 9461 | dev->wanted_features = dev->features & dev->hw_features; |
| 9462 | |
| 9463 | if (!(dev->flags & IFF_LOOPBACK)) |
| 9464 | dev->hw_features |= NETIF_F_NOCACHE_COPY; |
| 9465 | |
| 9466 | /* If IPv4 TCP segmentation offload is supported we should also |
| 9467 | * allow the device to enable segmenting the frame with the option |
| 9468 | * of ignoring a static IP ID value. This doesn't enable the |
| 9469 | * feature itself but allows the user to enable it later. |
| 9470 | */ |
| 9471 | if (dev->hw_features & NETIF_F_TSO) |
| 9472 | dev->hw_features |= NETIF_F_TSO_MANGLEID; |
| 9473 | if (dev->vlan_features & NETIF_F_TSO) |
| 9474 | dev->vlan_features |= NETIF_F_TSO_MANGLEID; |
| 9475 | if (dev->mpls_features & NETIF_F_TSO) |
| 9476 | dev->mpls_features |= NETIF_F_TSO_MANGLEID; |
| 9477 | if (dev->hw_enc_features & NETIF_F_TSO) |
| 9478 | dev->hw_enc_features |= NETIF_F_TSO_MANGLEID; |
| 9479 | |
| 9480 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
| 9481 | */ |
| 9482 | dev->vlan_features |= NETIF_F_HIGHDMA; |
| 9483 | |
| 9484 | /* Make NETIF_F_SG inheritable to tunnel devices. |
| 9485 | */ |
| 9486 | dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL; |
| 9487 | |
| 9488 | /* Make NETIF_F_SG inheritable to MPLS. |
| 9489 | */ |
| 9490 | dev->mpls_features |= NETIF_F_SG; |
| 9491 | |
| 9492 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
| 9493 | ret = notifier_to_errno(ret); |
| 9494 | if (ret) |
| 9495 | goto err_uninit; |
| 9496 | |
| 9497 | ret = netdev_register_kobject(dev); |
| 9498 | if (ret) { |
| 9499 | dev->reg_state = NETREG_UNREGISTERED; |
| 9500 | goto err_uninit; |
| 9501 | } |
| 9502 | dev->reg_state = NETREG_REGISTERED; |
| 9503 | |
| 9504 | __netdev_update_features(dev); |
| 9505 | |
| 9506 | /* |
| 9507 | * Default initial state at registry is that the |
| 9508 | * device is present. |
| 9509 | */ |
| 9510 | |
| 9511 | set_bit(__LINK_STATE_PRESENT, &dev->state); |
| 9512 | |
| 9513 | linkwatch_init_dev(dev); |
| 9514 | |
| 9515 | dev_init_scheduler(dev); |
| 9516 | dev_hold(dev); |
| 9517 | list_netdevice(dev); |
| 9518 | add_device_randomness(dev->dev_addr, dev->addr_len); |
| 9519 | |
| 9520 | /* If the device has permanent device address, driver should |
| 9521 | * set dev_addr and also addr_assign_type should be set to |
| 9522 | * NET_ADDR_PERM (default value). |
| 9523 | */ |
| 9524 | if (dev->addr_assign_type == NET_ADDR_PERM) |
| 9525 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); |
| 9526 | |
| 9527 | /* Notify protocols, that a new device appeared. */ |
| 9528 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
| 9529 | ret = notifier_to_errno(ret); |
| 9530 | if (ret) { |
| 9531 | rollback_registered(dev); |
| 9532 | rcu_barrier(); |
| 9533 | |
| 9534 | dev->reg_state = NETREG_UNREGISTERED; |
| 9535 | /* We should put the kobject that hold in |
| 9536 | * netdev_unregister_kobject(), otherwise |
| 9537 | * the net device cannot be freed when |
| 9538 | * driver calls free_netdev(), because the |
| 9539 | * kobject is being hold. |
| 9540 | */ |
| 9541 | kobject_put(&dev->dev.kobj); |
| 9542 | } |
| 9543 | /* |
| 9544 | * Prevent userspace races by waiting until the network |
| 9545 | * device is fully setup before sending notifications. |
| 9546 | */ |
| 9547 | if (!dev->rtnl_link_ops || |
| 9548 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) |
| 9549 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
| 9550 | |
| 9551 | out: |
| 9552 | return ret; |
| 9553 | |
| 9554 | err_uninit: |
| 9555 | if (dev->netdev_ops->ndo_uninit) |
| 9556 | dev->netdev_ops->ndo_uninit(dev); |
| 9557 | if (dev->priv_destructor) |
| 9558 | dev->priv_destructor(dev); |
| 9559 | goto out; |
| 9560 | } |
| 9561 | EXPORT_SYMBOL(register_netdevice); |
| 9562 | |
| 9563 | /** |
| 9564 | * init_dummy_netdev - init a dummy network device for NAPI |
| 9565 | * @dev: device to init |
| 9566 | * |
| 9567 | * This takes a network device structure and initialize the minimum |
| 9568 | * amount of fields so it can be used to schedule NAPI polls without |
| 9569 | * registering a full blown interface. This is to be used by drivers |
| 9570 | * that need to tie several hardware interfaces to a single NAPI |
| 9571 | * poll scheduler due to HW limitations. |
| 9572 | */ |
| 9573 | int init_dummy_netdev(struct net_device *dev) |
| 9574 | { |
| 9575 | /* Clear everything. Note we don't initialize spinlocks |
| 9576 | * are they aren't supposed to be taken by any of the |
| 9577 | * NAPI code and this dummy netdev is supposed to be |
| 9578 | * only ever used for NAPI polls |
| 9579 | */ |
| 9580 | memset(dev, 0, sizeof(struct net_device)); |
| 9581 | |
| 9582 | /* make sure we BUG if trying to hit standard |
| 9583 | * register/unregister code path |
| 9584 | */ |
| 9585 | dev->reg_state = NETREG_DUMMY; |
| 9586 | |
| 9587 | /* NAPI wants this */ |
| 9588 | INIT_LIST_HEAD(&dev->napi_list); |
| 9589 | |
| 9590 | /* a dummy interface is started by default */ |
| 9591 | set_bit(__LINK_STATE_PRESENT, &dev->state); |
| 9592 | set_bit(__LINK_STATE_START, &dev->state); |
| 9593 | |
| 9594 | /* napi_busy_loop stats accounting wants this */ |
| 9595 | dev_net_set(dev, &init_net); |
| 9596 | |
| 9597 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
| 9598 | * because users of this 'device' dont need to change |
| 9599 | * its refcount. |
| 9600 | */ |
| 9601 | |
| 9602 | return 0; |
| 9603 | } |
| 9604 | EXPORT_SYMBOL_GPL(init_dummy_netdev); |
| 9605 | |
| 9606 | |
| 9607 | /** |
| 9608 | * register_netdev - register a network device |
| 9609 | * @dev: device to register |
| 9610 | * |
| 9611 | * Take a completed network device structure and add it to the kernel |
| 9612 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier |
| 9613 | * chain. 0 is returned on success. A negative errno code is returned |
| 9614 | * on a failure to set up the device, or if the name is a duplicate. |
| 9615 | * |
| 9616 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
| 9617 | * and expands the device name if you passed a format string to |
| 9618 | * alloc_netdev. |
| 9619 | */ |
| 9620 | int register_netdev(struct net_device *dev) |
| 9621 | { |
| 9622 | int err; |
| 9623 | |
| 9624 | if (rtnl_lock_killable()) |
| 9625 | return -EINTR; |
| 9626 | err = register_netdevice(dev); |
| 9627 | rtnl_unlock(); |
| 9628 | return err; |
| 9629 | } |
| 9630 | EXPORT_SYMBOL(register_netdev); |
| 9631 | |
| 9632 | int netdev_refcnt_read(const struct net_device *dev) |
| 9633 | { |
| 9634 | int i, refcnt = 0; |
| 9635 | |
| 9636 | for_each_possible_cpu(i) |
| 9637 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); |
| 9638 | return refcnt; |
| 9639 | } |
| 9640 | EXPORT_SYMBOL(netdev_refcnt_read); |
| 9641 | |
| 9642 | /** |
| 9643 | * netdev_wait_allrefs - wait until all references are gone. |
| 9644 | * @dev: target net_device |
| 9645 | * |
| 9646 | * This is called when unregistering network devices. |
| 9647 | * |
| 9648 | * Any protocol or device that holds a reference should register |
| 9649 | * for netdevice notification, and cleanup and put back the |
| 9650 | * reference if they receive an UNREGISTER event. |
| 9651 | * We can get stuck here if buggy protocols don't correctly |
| 9652 | * call dev_put. |
| 9653 | */ |
| 9654 | static void netdev_wait_allrefs(struct net_device *dev) |
| 9655 | { |
| 9656 | unsigned long rebroadcast_time, warning_time; |
| 9657 | int refcnt; |
| 9658 | |
| 9659 | linkwatch_forget_dev(dev); |
| 9660 | |
| 9661 | rebroadcast_time = warning_time = jiffies; |
| 9662 | refcnt = netdev_refcnt_read(dev); |
| 9663 | |
| 9664 | while (refcnt != 0) { |
| 9665 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
| 9666 | rtnl_lock(); |
| 9667 | |
| 9668 | /* Rebroadcast unregister notification */ |
| 9669 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
| 9670 | |
| 9671 | __rtnl_unlock(); |
| 9672 | rcu_barrier(); |
| 9673 | rtnl_lock(); |
| 9674 | |
| 9675 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, |
| 9676 | &dev->state)) { |
| 9677 | /* We must not have linkwatch events |
| 9678 | * pending on unregister. If this |
| 9679 | * happens, we simply run the queue |
| 9680 | * unscheduled, resulting in a noop |
| 9681 | * for this device. |
| 9682 | */ |
| 9683 | linkwatch_run_queue(); |
| 9684 | } |
| 9685 | |
| 9686 | __rtnl_unlock(); |
| 9687 | |
| 9688 | rebroadcast_time = jiffies; |
| 9689 | } |
| 9690 | |
| 9691 | msleep(250); |
| 9692 | |
| 9693 | refcnt = netdev_refcnt_read(dev); |
| 9694 | |
| 9695 | if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) { |
| 9696 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", |
| 9697 | dev->name, refcnt); |
| 9698 | warning_time = jiffies; |
| 9699 | } |
| 9700 | } |
| 9701 | } |
| 9702 | |
| 9703 | /* The sequence is: |
| 9704 | * |
| 9705 | * rtnl_lock(); |
| 9706 | * ... |
| 9707 | * register_netdevice(x1); |
| 9708 | * register_netdevice(x2); |
| 9709 | * ... |
| 9710 | * unregister_netdevice(y1); |
| 9711 | * unregister_netdevice(y2); |
| 9712 | * ... |
| 9713 | * rtnl_unlock(); |
| 9714 | * free_netdev(y1); |
| 9715 | * free_netdev(y2); |
| 9716 | * |
| 9717 | * We are invoked by rtnl_unlock(). |
| 9718 | * This allows us to deal with problems: |
| 9719 | * 1) We can delete sysfs objects which invoke hotplug |
| 9720 | * without deadlocking with linkwatch via keventd. |
| 9721 | * 2) Since we run with the RTNL semaphore not held, we can sleep |
| 9722 | * safely in order to wait for the netdev refcnt to drop to zero. |
| 9723 | * |
| 9724 | * We must not return until all unregister events added during |
| 9725 | * the interval the lock was held have been completed. |
| 9726 | */ |
| 9727 | void netdev_run_todo(void) |
| 9728 | { |
| 9729 | struct list_head list; |
| 9730 | |
| 9731 | /* Snapshot list, allow later requests */ |
| 9732 | list_replace_init(&net_todo_list, &list); |
| 9733 | |
| 9734 | __rtnl_unlock(); |
| 9735 | |
| 9736 | |
| 9737 | /* Wait for rcu callbacks to finish before next phase */ |
| 9738 | if (!list_empty(&list)) |
| 9739 | rcu_barrier(); |
| 9740 | |
| 9741 | while (!list_empty(&list)) { |
| 9742 | struct net_device *dev |
| 9743 | = list_first_entry(&list, struct net_device, todo_list); |
| 9744 | list_del(&dev->todo_list); |
| 9745 | |
| 9746 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
| 9747 | pr_err("network todo '%s' but state %d\n", |
| 9748 | dev->name, dev->reg_state); |
| 9749 | dump_stack(); |
| 9750 | continue; |
| 9751 | } |
| 9752 | |
| 9753 | dev->reg_state = NETREG_UNREGISTERED; |
| 9754 | |
| 9755 | netdev_wait_allrefs(dev); |
| 9756 | |
| 9757 | /* paranoia */ |
| 9758 | BUG_ON(netdev_refcnt_read(dev)); |
| 9759 | BUG_ON(!list_empty(&dev->ptype_all)); |
| 9760 | BUG_ON(!list_empty(&dev->ptype_specific)); |
| 9761 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
| 9762 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); |
| 9763 | |
| 9764 | if (dev->priv_destructor) |
| 9765 | dev->priv_destructor(dev); |
| 9766 | if (dev->needs_free_netdev) |
| 9767 | free_netdev(dev); |
| 9768 | |
| 9769 | /* Report a network device has been unregistered */ |
| 9770 | rtnl_lock(); |
| 9771 | dev_net(dev)->dev_unreg_count--; |
| 9772 | __rtnl_unlock(); |
| 9773 | wake_up(&netdev_unregistering_wq); |
| 9774 | |
| 9775 | /* Free network device */ |
| 9776 | kobject_put(&dev->dev.kobj); |
| 9777 | } |
| 9778 | } |
| 9779 | |
| 9780 | /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has |
| 9781 | * all the same fields in the same order as net_device_stats, with only |
| 9782 | * the type differing, but rtnl_link_stats64 may have additional fields |
| 9783 | * at the end for newer counters. |
| 9784 | */ |
| 9785 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
| 9786 | const struct net_device_stats *netdev_stats) |
| 9787 | { |
| 9788 | size_t i, n = sizeof(*netdev_stats) / sizeof(atomic_long_t); |
| 9789 | const atomic_long_t *src = (atomic_long_t *)netdev_stats; |
| 9790 | u64 *dst = (u64 *)stats64; |
| 9791 | |
| 9792 | BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64)); |
| 9793 | for (i = 0; i < n; i++) |
| 9794 | dst[i] = (unsigned long)atomic_long_read(&src[i]); |
| 9795 | /* zero out counters that only exist in rtnl_link_stats64 */ |
| 9796 | memset((char *)stats64 + n * sizeof(u64), 0, |
| 9797 | sizeof(*stats64) - n * sizeof(u64)); |
| 9798 | } |
| 9799 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
| 9800 | |
| 9801 | /** |
| 9802 | * dev_get_stats - get network device statistics |
| 9803 | * @dev: device to get statistics from |
| 9804 | * @storage: place to store stats |
| 9805 | * |
| 9806 | * Get network statistics from device. Return @storage. |
| 9807 | * The device driver may provide its own method by setting |
| 9808 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; |
| 9809 | * otherwise the internal statistics structure is used. |
| 9810 | */ |
| 9811 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
| 9812 | struct rtnl_link_stats64 *storage) |
| 9813 | { |
| 9814 | const struct net_device_ops *ops = dev->netdev_ops; |
| 9815 | |
| 9816 | if (ops->ndo_get_stats64) { |
| 9817 | memset(storage, 0, sizeof(*storage)); |
| 9818 | ops->ndo_get_stats64(dev, storage); |
| 9819 | } else if (ops->ndo_get_stats) { |
| 9820 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
| 9821 | } else { |
| 9822 | netdev_stats_to_stats64(storage, &dev->stats); |
| 9823 | } |
| 9824 | storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped); |
| 9825 | storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped); |
| 9826 | storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler); |
| 9827 | return storage; |
| 9828 | } |
| 9829 | EXPORT_SYMBOL(dev_get_stats); |
| 9830 | |
| 9831 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
| 9832 | { |
| 9833 | struct netdev_queue *queue = dev_ingress_queue(dev); |
| 9834 | |
| 9835 | #ifdef CONFIG_NET_CLS_ACT |
| 9836 | if (queue) |
| 9837 | return queue; |
| 9838 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); |
| 9839 | if (!queue) |
| 9840 | return NULL; |
| 9841 | netdev_init_one_queue(dev, queue, NULL); |
| 9842 | RCU_INIT_POINTER(queue->qdisc, &noop_qdisc); |
| 9843 | queue->qdisc_sleeping = &noop_qdisc; |
| 9844 | rcu_assign_pointer(dev->ingress_queue, queue); |
| 9845 | #endif |
| 9846 | return queue; |
| 9847 | } |
| 9848 | |
| 9849 | static const struct ethtool_ops default_ethtool_ops; |
| 9850 | |
| 9851 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
| 9852 | const struct ethtool_ops *ops) |
| 9853 | { |
| 9854 | if (dev->ethtool_ops == &default_ethtool_ops) |
| 9855 | dev->ethtool_ops = ops; |
| 9856 | } |
| 9857 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); |
| 9858 | |
| 9859 | void netdev_freemem(struct net_device *dev) |
| 9860 | { |
| 9861 | char *addr = (char *)dev - dev->padded; |
| 9862 | |
| 9863 | kvfree(addr); |
| 9864 | } |
| 9865 | |
| 9866 | /** |
| 9867 | * alloc_netdev_mqs - allocate network device |
| 9868 | * @sizeof_priv: size of private data to allocate space for |
| 9869 | * @name: device name format string |
| 9870 | * @name_assign_type: origin of device name |
| 9871 | * @setup: callback to initialize device |
| 9872 | * @txqs: the number of TX subqueues to allocate |
| 9873 | * @rxqs: the number of RX subqueues to allocate |
| 9874 | * |
| 9875 | * Allocates a struct net_device with private data area for driver use |
| 9876 | * and performs basic initialization. Also allocates subqueue structs |
| 9877 | * for each queue on the device. |
| 9878 | */ |
| 9879 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
| 9880 | unsigned char name_assign_type, |
| 9881 | void (*setup)(struct net_device *), |
| 9882 | unsigned int txqs, unsigned int rxqs) |
| 9883 | { |
| 9884 | struct net_device *dev; |
| 9885 | unsigned int alloc_size; |
| 9886 | struct net_device *p; |
| 9887 | |
| 9888 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
| 9889 | |
| 9890 | if (txqs < 1) { |
| 9891 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
| 9892 | return NULL; |
| 9893 | } |
| 9894 | |
| 9895 | if (rxqs < 1) { |
| 9896 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
| 9897 | return NULL; |
| 9898 | } |
| 9899 | |
| 9900 | alloc_size = sizeof(struct net_device); |
| 9901 | if (sizeof_priv) { |
| 9902 | /* ensure 32-byte alignment of private area */ |
| 9903 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
| 9904 | alloc_size += sizeof_priv; |
| 9905 | } |
| 9906 | /* ensure 32-byte alignment of whole construct */ |
| 9907 | alloc_size += NETDEV_ALIGN - 1; |
| 9908 | |
| 9909 | p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
| 9910 | if (!p) |
| 9911 | return NULL; |
| 9912 | |
| 9913 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
| 9914 | dev->padded = (char *)dev - (char *)p; |
| 9915 | |
| 9916 | dev->pcpu_refcnt = alloc_percpu(int); |
| 9917 | if (!dev->pcpu_refcnt) |
| 9918 | goto free_dev; |
| 9919 | |
| 9920 | if (dev_addr_init(dev)) |
| 9921 | goto free_pcpu; |
| 9922 | |
| 9923 | dev_mc_init(dev); |
| 9924 | dev_uc_init(dev); |
| 9925 | |
| 9926 | dev_net_set(dev, &init_net); |
| 9927 | |
| 9928 | netdev_register_lockdep_key(dev); |
| 9929 | |
| 9930 | dev->gso_max_size = GSO_MAX_SIZE; |
| 9931 | dev->gso_max_segs = GSO_MAX_SEGS; |
| 9932 | dev->upper_level = 1; |
| 9933 | dev->lower_level = 1; |
| 9934 | |
| 9935 | INIT_LIST_HEAD(&dev->napi_list); |
| 9936 | INIT_LIST_HEAD(&dev->unreg_list); |
| 9937 | INIT_LIST_HEAD(&dev->close_list); |
| 9938 | INIT_LIST_HEAD(&dev->link_watch_list); |
| 9939 | INIT_LIST_HEAD(&dev->adj_list.upper); |
| 9940 | INIT_LIST_HEAD(&dev->adj_list.lower); |
| 9941 | INIT_LIST_HEAD(&dev->ptype_all); |
| 9942 | INIT_LIST_HEAD(&dev->ptype_specific); |
| 9943 | #ifdef CONFIG_NET_SCHED |
| 9944 | hash_init(dev->qdisc_hash); |
| 9945 | #endif |
| 9946 | dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; |
| 9947 | setup(dev); |
| 9948 | |
| 9949 | if (!dev->tx_queue_len) { |
| 9950 | dev->priv_flags |= IFF_NO_QUEUE; |
| 9951 | dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; |
| 9952 | } |
| 9953 | |
| 9954 | dev->num_tx_queues = txqs; |
| 9955 | dev->real_num_tx_queues = txqs; |
| 9956 | if (netif_alloc_netdev_queues(dev)) |
| 9957 | goto free_all; |
| 9958 | |
| 9959 | dev->num_rx_queues = rxqs; |
| 9960 | dev->real_num_rx_queues = rxqs; |
| 9961 | if (netif_alloc_rx_queues(dev)) |
| 9962 | goto free_all; |
| 9963 | |
| 9964 | strcpy(dev->name, name); |
| 9965 | dev->name_assign_type = name_assign_type; |
| 9966 | dev->group = INIT_NETDEV_GROUP; |
| 9967 | if (!dev->ethtool_ops) |
| 9968 | dev->ethtool_ops = &default_ethtool_ops; |
| 9969 | |
| 9970 | nf_hook_ingress_init(dev); |
| 9971 | |
| 9972 | return dev; |
| 9973 | |
| 9974 | free_all: |
| 9975 | free_netdev(dev); |
| 9976 | return NULL; |
| 9977 | |
| 9978 | free_pcpu: |
| 9979 | free_percpu(dev->pcpu_refcnt); |
| 9980 | free_dev: |
| 9981 | netdev_freemem(dev); |
| 9982 | return NULL; |
| 9983 | } |
| 9984 | EXPORT_SYMBOL(alloc_netdev_mqs); |
| 9985 | |
| 9986 | /** |
| 9987 | * free_netdev - free network device |
| 9988 | * @dev: device |
| 9989 | * |
| 9990 | * This function does the last stage of destroying an allocated device |
| 9991 | * interface. The reference to the device object is released. If this |
| 9992 | * is the last reference then it will be freed.Must be called in process |
| 9993 | * context. |
| 9994 | */ |
| 9995 | void free_netdev(struct net_device *dev) |
| 9996 | { |
| 9997 | struct napi_struct *p, *n; |
| 9998 | |
| 9999 | might_sleep(); |
| 10000 | netif_free_tx_queues(dev); |
| 10001 | netif_free_rx_queues(dev); |
| 10002 | |
| 10003 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
| 10004 | |
| 10005 | /* Flush device addresses */ |
| 10006 | dev_addr_flush(dev); |
| 10007 | |
| 10008 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
| 10009 | netif_napi_del(p); |
| 10010 | |
| 10011 | free_percpu(dev->pcpu_refcnt); |
| 10012 | dev->pcpu_refcnt = NULL; |
| 10013 | |
| 10014 | netdev_unregister_lockdep_key(dev); |
| 10015 | |
| 10016 | /* Compatibility with error handling in drivers */ |
| 10017 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
| 10018 | netdev_freemem(dev); |
| 10019 | return; |
| 10020 | } |
| 10021 | |
| 10022 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); |
| 10023 | dev->reg_state = NETREG_RELEASED; |
| 10024 | |
| 10025 | /* will free via device release */ |
| 10026 | put_device(&dev->dev); |
| 10027 | } |
| 10028 | EXPORT_SYMBOL(free_netdev); |
| 10029 | |
| 10030 | /** |
| 10031 | * synchronize_net - Synchronize with packet receive processing |
| 10032 | * |
| 10033 | * Wait for packets currently being received to be done. |
| 10034 | * Does not block later packets from starting. |
| 10035 | */ |
| 10036 | void synchronize_net(void) |
| 10037 | { |
| 10038 | might_sleep(); |
| 10039 | if (rtnl_is_locked()) |
| 10040 | synchronize_rcu_expedited(); |
| 10041 | else |
| 10042 | synchronize_rcu(); |
| 10043 | } |
| 10044 | EXPORT_SYMBOL(synchronize_net); |
| 10045 | |
| 10046 | /** |
| 10047 | * unregister_netdevice_queue - remove device from the kernel |
| 10048 | * @dev: device |
| 10049 | * @head: list |
| 10050 | * |
| 10051 | * This function shuts down a device interface and removes it |
| 10052 | * from the kernel tables. |
| 10053 | * If head not NULL, device is queued to be unregistered later. |
| 10054 | * |
| 10055 | * Callers must hold the rtnl semaphore. You may want |
| 10056 | * unregister_netdev() instead of this. |
| 10057 | */ |
| 10058 | |
| 10059 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
| 10060 | { |
| 10061 | ASSERT_RTNL(); |
| 10062 | |
| 10063 | if (head) { |
| 10064 | list_move_tail(&dev->unreg_list, head); |
| 10065 | } else { |
| 10066 | rollback_registered(dev); |
| 10067 | /* Finish processing unregister after unlock */ |
| 10068 | net_set_todo(dev); |
| 10069 | } |
| 10070 | } |
| 10071 | EXPORT_SYMBOL(unregister_netdevice_queue); |
| 10072 | |
| 10073 | /** |
| 10074 | * unregister_netdevice_many - unregister many devices |
| 10075 | * @head: list of devices |
| 10076 | * |
| 10077 | * Note: As most callers use a stack allocated list_head, |
| 10078 | * we force a list_del() to make sure stack wont be corrupted later. |
| 10079 | */ |
| 10080 | void unregister_netdevice_many(struct list_head *head) |
| 10081 | { |
| 10082 | struct net_device *dev; |
| 10083 | |
| 10084 | if (!list_empty(head)) { |
| 10085 | rollback_registered_many(head); |
| 10086 | list_for_each_entry(dev, head, unreg_list) |
| 10087 | net_set_todo(dev); |
| 10088 | list_del(head); |
| 10089 | } |
| 10090 | } |
| 10091 | EXPORT_SYMBOL(unregister_netdevice_many); |
| 10092 | |
| 10093 | /** |
| 10094 | * unregister_netdev - remove device from the kernel |
| 10095 | * @dev: device |
| 10096 | * |
| 10097 | * This function shuts down a device interface and removes it |
| 10098 | * from the kernel tables. |
| 10099 | * |
| 10100 | * This is just a wrapper for unregister_netdevice that takes |
| 10101 | * the rtnl semaphore. In general you want to use this and not |
| 10102 | * unregister_netdevice. |
| 10103 | */ |
| 10104 | void unregister_netdev(struct net_device *dev) |
| 10105 | { |
| 10106 | rtnl_lock(); |
| 10107 | unregister_netdevice(dev); |
| 10108 | rtnl_unlock(); |
| 10109 | } |
| 10110 | EXPORT_SYMBOL(unregister_netdev); |
| 10111 | |
| 10112 | /** |
| 10113 | * dev_change_net_namespace - move device to different nethost namespace |
| 10114 | * @dev: device |
| 10115 | * @net: network namespace |
| 10116 | * @pat: If not NULL name pattern to try if the current device name |
| 10117 | * is already taken in the destination network namespace. |
| 10118 | * |
| 10119 | * This function shuts down a device interface and moves it |
| 10120 | * to a new network namespace. On success 0 is returned, on |
| 10121 | * a failure a netagive errno code is returned. |
| 10122 | * |
| 10123 | * Callers must hold the rtnl semaphore. |
| 10124 | */ |
| 10125 | |
| 10126 | int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat) |
| 10127 | { |
| 10128 | int err, new_nsid, new_ifindex; |
| 10129 | |
| 10130 | ASSERT_RTNL(); |
| 10131 | |
| 10132 | /* Don't allow namespace local devices to be moved. */ |
| 10133 | err = -EINVAL; |
| 10134 | if (dev->features & NETIF_F_NETNS_LOCAL) |
| 10135 | goto out; |
| 10136 | |
| 10137 | /* Ensure the device has been registrered */ |
| 10138 | if (dev->reg_state != NETREG_REGISTERED) |
| 10139 | goto out; |
| 10140 | |
| 10141 | /* Get out if there is nothing todo */ |
| 10142 | err = 0; |
| 10143 | if (net_eq(dev_net(dev), net)) |
| 10144 | goto out; |
| 10145 | |
| 10146 | /* Pick the destination device name, and ensure |
| 10147 | * we can use it in the destination network namespace. |
| 10148 | */ |
| 10149 | err = -EEXIST; |
| 10150 | if (__dev_get_by_name(net, dev->name)) { |
| 10151 | /* We get here if we can't use the current device name */ |
| 10152 | if (!pat) |
| 10153 | goto out; |
| 10154 | err = dev_get_valid_name(net, dev, pat); |
| 10155 | if (err < 0) |
| 10156 | goto out; |
| 10157 | } |
| 10158 | |
| 10159 | /* |
| 10160 | * And now a mini version of register_netdevice unregister_netdevice. |
| 10161 | */ |
| 10162 | |
| 10163 | /* If device is running close it first. */ |
| 10164 | dev_close(dev); |
| 10165 | |
| 10166 | /* And unlink it from device chain */ |
| 10167 | unlist_netdevice(dev); |
| 10168 | |
| 10169 | synchronize_net(); |
| 10170 | |
| 10171 | /* Shutdown queueing discipline. */ |
| 10172 | dev_shutdown(dev); |
| 10173 | |
| 10174 | /* Notify protocols, that we are about to destroy |
| 10175 | * this device. They should clean all the things. |
| 10176 | * |
| 10177 | * Note that dev->reg_state stays at NETREG_REGISTERED. |
| 10178 | * This is wanted because this way 8021q and macvlan know |
| 10179 | * the device is just moving and can keep their slaves up. |
| 10180 | */ |
| 10181 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
| 10182 | rcu_barrier(); |
| 10183 | |
| 10184 | new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL); |
| 10185 | /* If there is an ifindex conflict assign a new one */ |
| 10186 | if (__dev_get_by_index(net, dev->ifindex)) |
| 10187 | new_ifindex = dev_new_index(net); |
| 10188 | else |
| 10189 | new_ifindex = dev->ifindex; |
| 10190 | |
| 10191 | rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid, |
| 10192 | new_ifindex); |
| 10193 | |
| 10194 | /* |
| 10195 | * Flush the unicast and multicast chains |
| 10196 | */ |
| 10197 | dev_uc_flush(dev); |
| 10198 | dev_mc_flush(dev); |
| 10199 | |
| 10200 | /* Send a netdev-removed uevent to the old namespace */ |
| 10201 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); |
| 10202 | netdev_adjacent_del_links(dev); |
| 10203 | |
| 10204 | /* Actually switch the network namespace */ |
| 10205 | dev_net_set(dev, net); |
| 10206 | dev->ifindex = new_ifindex; |
| 10207 | |
| 10208 | /* Send a netdev-add uevent to the new namespace */ |
| 10209 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); |
| 10210 | netdev_adjacent_add_links(dev); |
| 10211 | |
| 10212 | /* Fixup kobjects */ |
| 10213 | err = device_rename(&dev->dev, dev->name); |
| 10214 | WARN_ON(err); |
| 10215 | |
| 10216 | /* Add the device back in the hashes */ |
| 10217 | list_netdevice(dev); |
| 10218 | |
| 10219 | /* Notify protocols, that a new device appeared. */ |
| 10220 | call_netdevice_notifiers(NETDEV_REGISTER, dev); |
| 10221 | |
| 10222 | /* |
| 10223 | * Prevent userspace races by waiting until the network |
| 10224 | * device is fully setup before sending notifications. |
| 10225 | */ |
| 10226 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
| 10227 | |
| 10228 | synchronize_net(); |
| 10229 | err = 0; |
| 10230 | out: |
| 10231 | return err; |
| 10232 | } |
| 10233 | EXPORT_SYMBOL_GPL(dev_change_net_namespace); |
| 10234 | |
| 10235 | static int dev_cpu_dead(unsigned int oldcpu) |
| 10236 | { |
| 10237 | struct sk_buff **list_skb; |
| 10238 | struct sk_buff *skb; |
| 10239 | unsigned int cpu; |
| 10240 | struct softnet_data *sd, *oldsd, *remsd = NULL; |
| 10241 | |
| 10242 | local_irq_disable(); |
| 10243 | cpu = smp_processor_id(); |
| 10244 | sd = &per_cpu(softnet_data, cpu); |
| 10245 | oldsd = &per_cpu(softnet_data, oldcpu); |
| 10246 | |
| 10247 | /* Find end of our completion_queue. */ |
| 10248 | list_skb = &sd->completion_queue; |
| 10249 | while (*list_skb) |
| 10250 | list_skb = &(*list_skb)->next; |
| 10251 | /* Append completion queue from offline CPU. */ |
| 10252 | *list_skb = oldsd->completion_queue; |
| 10253 | oldsd->completion_queue = NULL; |
| 10254 | |
| 10255 | /* Append output queue from offline CPU. */ |
| 10256 | if (oldsd->output_queue) { |
| 10257 | *sd->output_queue_tailp = oldsd->output_queue; |
| 10258 | sd->output_queue_tailp = oldsd->output_queue_tailp; |
| 10259 | oldsd->output_queue = NULL; |
| 10260 | oldsd->output_queue_tailp = &oldsd->output_queue; |
| 10261 | } |
| 10262 | /* Append NAPI poll list from offline CPU, with one exception : |
| 10263 | * process_backlog() must be called by cpu owning percpu backlog. |
| 10264 | * We properly handle process_queue & input_pkt_queue later. |
| 10265 | */ |
| 10266 | while (!list_empty(&oldsd->poll_list)) { |
| 10267 | struct napi_struct *napi = list_first_entry(&oldsd->poll_list, |
| 10268 | struct napi_struct, |
| 10269 | poll_list); |
| 10270 | |
| 10271 | list_del_init(&napi->poll_list); |
| 10272 | if (napi->poll == process_backlog) |
| 10273 | napi->state = 0; |
| 10274 | else |
| 10275 | ____napi_schedule(sd, napi); |
| 10276 | } |
| 10277 | |
| 10278 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
| 10279 | local_irq_enable(); |
| 10280 | |
| 10281 | #ifdef CONFIG_RPS |
| 10282 | remsd = oldsd->rps_ipi_list; |
| 10283 | oldsd->rps_ipi_list = NULL; |
| 10284 | #endif |
| 10285 | /* send out pending IPI's on offline CPU */ |
| 10286 | net_rps_send_ipi(remsd); |
| 10287 | |
| 10288 | /* Process offline CPU's input_pkt_queue */ |
| 10289 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
| 10290 | netif_rx_ni(skb); |
| 10291 | input_queue_head_incr(oldsd); |
| 10292 | } |
| 10293 | while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) { |
| 10294 | netif_rx_ni(skb); |
| 10295 | input_queue_head_incr(oldsd); |
| 10296 | } |
| 10297 | |
| 10298 | return 0; |
| 10299 | } |
| 10300 | |
| 10301 | /** |
| 10302 | * netdev_increment_features - increment feature set by one |
| 10303 | * @all: current feature set |
| 10304 | * @one: new feature set |
| 10305 | * @mask: mask feature set |
| 10306 | * |
| 10307 | * Computes a new feature set after adding a device with feature set |
| 10308 | * @one to the master device with current feature set @all. Will not |
| 10309 | * enable anything that is off in @mask. Returns the new feature set. |
| 10310 | */ |
| 10311 | netdev_features_t netdev_increment_features(netdev_features_t all, |
| 10312 | netdev_features_t one, netdev_features_t mask) |
| 10313 | { |
| 10314 | if (mask & NETIF_F_HW_CSUM) |
| 10315 | mask |= NETIF_F_CSUM_MASK; |
| 10316 | mask |= NETIF_F_VLAN_CHALLENGED; |
| 10317 | |
| 10318 | all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask; |
| 10319 | all &= one | ~NETIF_F_ALL_FOR_ALL; |
| 10320 | |
| 10321 | /* If one device supports hw checksumming, set for all. */ |
| 10322 | if (all & NETIF_F_HW_CSUM) |
| 10323 | all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM); |
| 10324 | |
| 10325 | return all; |
| 10326 | } |
| 10327 | EXPORT_SYMBOL(netdev_increment_features); |
| 10328 | |
| 10329 | static struct hlist_head * __net_init netdev_create_hash(void) |
| 10330 | { |
| 10331 | int i; |
| 10332 | struct hlist_head *hash; |
| 10333 | |
| 10334 | hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL); |
| 10335 | if (hash != NULL) |
| 10336 | for (i = 0; i < NETDEV_HASHENTRIES; i++) |
| 10337 | INIT_HLIST_HEAD(&hash[i]); |
| 10338 | |
| 10339 | return hash; |
| 10340 | } |
| 10341 | |
| 10342 | /* Initialize per network namespace state */ |
| 10343 | static int __net_init netdev_init(struct net *net) |
| 10344 | { |
| 10345 | BUILD_BUG_ON(GRO_HASH_BUCKETS > |
| 10346 | 8 * FIELD_SIZEOF(struct napi_struct, gro_bitmask)); |
| 10347 | |
| 10348 | if (net != &init_net) |
| 10349 | INIT_LIST_HEAD(&net->dev_base_head); |
| 10350 | |
| 10351 | net->dev_name_head = netdev_create_hash(); |
| 10352 | if (net->dev_name_head == NULL) |
| 10353 | goto err_name; |
| 10354 | |
| 10355 | net->dev_index_head = netdev_create_hash(); |
| 10356 | if (net->dev_index_head == NULL) |
| 10357 | goto err_idx; |
| 10358 | |
| 10359 | return 0; |
| 10360 | |
| 10361 | err_idx: |
| 10362 | kfree(net->dev_name_head); |
| 10363 | err_name: |
| 10364 | return -ENOMEM; |
| 10365 | } |
| 10366 | |
| 10367 | /** |
| 10368 | * netdev_drivername - network driver for the device |
| 10369 | * @dev: network device |
| 10370 | * |
| 10371 | * Determine network driver for device. |
| 10372 | */ |
| 10373 | const char *netdev_drivername(const struct net_device *dev) |
| 10374 | { |
| 10375 | const struct device_driver *driver; |
| 10376 | const struct device *parent; |
| 10377 | const char *empty = ""; |
| 10378 | |
| 10379 | parent = dev->dev.parent; |
| 10380 | if (!parent) |
| 10381 | return empty; |
| 10382 | |
| 10383 | driver = parent->driver; |
| 10384 | if (driver && driver->name) |
| 10385 | return driver->name; |
| 10386 | return empty; |
| 10387 | } |
| 10388 | |
| 10389 | static void __netdev_printk(const char *level, const struct net_device *dev, |
| 10390 | struct va_format *vaf) |
| 10391 | { |
| 10392 | if (dev && dev->dev.parent) { |
| 10393 | dev_printk_emit(level[1] - '0', |
| 10394 | dev->dev.parent, |
| 10395 | "%s %s %s%s: %pV", |
| 10396 | dev_driver_string(dev->dev.parent), |
| 10397 | dev_name(dev->dev.parent), |
| 10398 | netdev_name(dev), netdev_reg_state(dev), |
| 10399 | vaf); |
| 10400 | } else if (dev) { |
| 10401 | printk("%s%s%s: %pV", |
| 10402 | level, netdev_name(dev), netdev_reg_state(dev), vaf); |
| 10403 | } else { |
| 10404 | printk("%s(NULL net_device): %pV", level, vaf); |
| 10405 | } |
| 10406 | } |
| 10407 | |
| 10408 | void netdev_printk(const char *level, const struct net_device *dev, |
| 10409 | const char *format, ...) |
| 10410 | { |
| 10411 | struct va_format vaf; |
| 10412 | va_list args; |
| 10413 | |
| 10414 | va_start(args, format); |
| 10415 | |
| 10416 | vaf.fmt = format; |
| 10417 | vaf.va = &args; |
| 10418 | |
| 10419 | __netdev_printk(level, dev, &vaf); |
| 10420 | |
| 10421 | va_end(args); |
| 10422 | } |
| 10423 | EXPORT_SYMBOL(netdev_printk); |
| 10424 | |
| 10425 | #define define_netdev_printk_level(func, level) \ |
| 10426 | void func(const struct net_device *dev, const char *fmt, ...) \ |
| 10427 | { \ |
| 10428 | struct va_format vaf; \ |
| 10429 | va_list args; \ |
| 10430 | \ |
| 10431 | va_start(args, fmt); \ |
| 10432 | \ |
| 10433 | vaf.fmt = fmt; \ |
| 10434 | vaf.va = &args; \ |
| 10435 | \ |
| 10436 | __netdev_printk(level, dev, &vaf); \ |
| 10437 | \ |
| 10438 | va_end(args); \ |
| 10439 | } \ |
| 10440 | EXPORT_SYMBOL(func); |
| 10441 | |
| 10442 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); |
| 10443 | define_netdev_printk_level(netdev_alert, KERN_ALERT); |
| 10444 | define_netdev_printk_level(netdev_crit, KERN_CRIT); |
| 10445 | define_netdev_printk_level(netdev_err, KERN_ERR); |
| 10446 | define_netdev_printk_level(netdev_warn, KERN_WARNING); |
| 10447 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); |
| 10448 | define_netdev_printk_level(netdev_info, KERN_INFO); |
| 10449 | |
| 10450 | static void __net_exit netdev_exit(struct net *net) |
| 10451 | { |
| 10452 | kfree(net->dev_name_head); |
| 10453 | kfree(net->dev_index_head); |
| 10454 | if (net != &init_net) |
| 10455 | WARN_ON_ONCE(!list_empty(&net->dev_base_head)); |
| 10456 | } |
| 10457 | |
| 10458 | static struct pernet_operations __net_initdata netdev_net_ops = { |
| 10459 | .init = netdev_init, |
| 10460 | .exit = netdev_exit, |
| 10461 | }; |
| 10462 | |
| 10463 | static void __net_exit default_device_exit(struct net *net) |
| 10464 | { |
| 10465 | struct net_device *dev, *aux; |
| 10466 | /* |
| 10467 | * Push all migratable network devices back to the |
| 10468 | * initial network namespace |
| 10469 | */ |
| 10470 | rtnl_lock(); |
| 10471 | for_each_netdev_safe(net, dev, aux) { |
| 10472 | int err; |
| 10473 | char fb_name[IFNAMSIZ]; |
| 10474 | |
| 10475 | /* Ignore unmoveable devices (i.e. loopback) */ |
| 10476 | if (dev->features & NETIF_F_NETNS_LOCAL) |
| 10477 | continue; |
| 10478 | |
| 10479 | /* Leave virtual devices for the generic cleanup */ |
| 10480 | if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund) |
| 10481 | continue; |
| 10482 | |
| 10483 | /* Push remaining network devices to init_net */ |
| 10484 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
| 10485 | if (__dev_get_by_name(&init_net, fb_name)) |
| 10486 | snprintf(fb_name, IFNAMSIZ, "dev%%d"); |
| 10487 | err = dev_change_net_namespace(dev, &init_net, fb_name); |
| 10488 | if (err) { |
| 10489 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
| 10490 | __func__, dev->name, err); |
| 10491 | BUG(); |
| 10492 | } |
| 10493 | } |
| 10494 | rtnl_unlock(); |
| 10495 | } |
| 10496 | |
| 10497 | static void __net_exit rtnl_lock_unregistering(struct list_head *net_list) |
| 10498 | { |
| 10499 | /* Return with the rtnl_lock held when there are no network |
| 10500 | * devices unregistering in any network namespace in net_list. |
| 10501 | */ |
| 10502 | struct net *net; |
| 10503 | bool unregistering; |
| 10504 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 10505 | |
| 10506 | add_wait_queue(&netdev_unregistering_wq, &wait); |
| 10507 | for (;;) { |
| 10508 | unregistering = false; |
| 10509 | rtnl_lock(); |
| 10510 | list_for_each_entry(net, net_list, exit_list) { |
| 10511 | if (net->dev_unreg_count > 0) { |
| 10512 | unregistering = true; |
| 10513 | break; |
| 10514 | } |
| 10515 | } |
| 10516 | if (!unregistering) |
| 10517 | break; |
| 10518 | __rtnl_unlock(); |
| 10519 | |
| 10520 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
| 10521 | } |
| 10522 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
| 10523 | } |
| 10524 | |
| 10525 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
| 10526 | { |
| 10527 | /* At exit all network devices most be removed from a network |
| 10528 | * namespace. Do this in the reverse order of registration. |
| 10529 | * Do this across as many network namespaces as possible to |
| 10530 | * improve batching efficiency. |
| 10531 | */ |
| 10532 | struct net_device *dev; |
| 10533 | struct net *net; |
| 10534 | LIST_HEAD(dev_kill_list); |
| 10535 | |
| 10536 | /* To prevent network device cleanup code from dereferencing |
| 10537 | * loopback devices or network devices that have been freed |
| 10538 | * wait here for all pending unregistrations to complete, |
| 10539 | * before unregistring the loopback device and allowing the |
| 10540 | * network namespace be freed. |
| 10541 | * |
| 10542 | * The netdev todo list containing all network devices |
| 10543 | * unregistrations that happen in default_device_exit_batch |
| 10544 | * will run in the rtnl_unlock() at the end of |
| 10545 | * default_device_exit_batch. |
| 10546 | */ |
| 10547 | rtnl_lock_unregistering(net_list); |
| 10548 | list_for_each_entry(net, net_list, exit_list) { |
| 10549 | for_each_netdev_reverse(net, dev) { |
| 10550 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink) |
| 10551 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); |
| 10552 | else |
| 10553 | unregister_netdevice_queue(dev, &dev_kill_list); |
| 10554 | } |
| 10555 | } |
| 10556 | unregister_netdevice_many(&dev_kill_list); |
| 10557 | rtnl_unlock(); |
| 10558 | } |
| 10559 | |
| 10560 | static struct pernet_operations __net_initdata default_device_ops = { |
| 10561 | .exit = default_device_exit, |
| 10562 | .exit_batch = default_device_exit_batch, |
| 10563 | }; |
| 10564 | |
| 10565 | /* |
| 10566 | * Initialize the DEV module. At boot time this walks the device list and |
| 10567 | * unhooks any devices that fail to initialise (normally hardware not |
| 10568 | * present) and leaves us with a valid list of present and active devices. |
| 10569 | * |
| 10570 | */ |
| 10571 | |
| 10572 | /* |
| 10573 | * This is called single threaded during boot, so no need |
| 10574 | * to take the rtnl semaphore. |
| 10575 | */ |
| 10576 | static int __init net_dev_init(void) |
| 10577 | { |
| 10578 | int i, rc = -ENOMEM; |
| 10579 | |
| 10580 | BUG_ON(!dev_boot_phase); |
| 10581 | |
| 10582 | if (dev_proc_init()) |
| 10583 | goto out; |
| 10584 | |
| 10585 | if (netdev_kobject_init()) |
| 10586 | goto out; |
| 10587 | |
| 10588 | INIT_LIST_HEAD(&ptype_all); |
| 10589 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
| 10590 | INIT_LIST_HEAD(&ptype_base[i]); |
| 10591 | |
| 10592 | INIT_LIST_HEAD(&offload_base); |
| 10593 | |
| 10594 | if (register_pernet_subsys(&netdev_net_ops)) |
| 10595 | goto out; |
| 10596 | |
| 10597 | /* |
| 10598 | * Initialise the packet receive queues. |
| 10599 | */ |
| 10600 | |
| 10601 | for_each_possible_cpu(i) { |
| 10602 | struct work_struct *flush = per_cpu_ptr(&flush_works, i); |
| 10603 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
| 10604 | |
| 10605 | INIT_WORK(flush, flush_backlog); |
| 10606 | |
| 10607 | skb_queue_head_init(&sd->input_pkt_queue); |
| 10608 | skb_queue_head_init(&sd->process_queue); |
| 10609 | #ifdef CONFIG_XFRM_OFFLOAD |
| 10610 | skb_queue_head_init(&sd->xfrm_backlog); |
| 10611 | #endif |
| 10612 | INIT_LIST_HEAD(&sd->poll_list); |
| 10613 | sd->output_queue_tailp = &sd->output_queue; |
| 10614 | #ifdef CONFIG_RPS |
| 10615 | sd->csd.func = rps_trigger_softirq; |
| 10616 | sd->csd.info = sd; |
| 10617 | sd->cpu = i; |
| 10618 | #endif |
| 10619 | |
| 10620 | init_gro_hash(&sd->backlog); |
| 10621 | sd->backlog.poll = process_backlog; |
| 10622 | sd->backlog.weight = weight_p; |
| 10623 | } |
| 10624 | |
| 10625 | dev_boot_phase = 0; |
| 10626 | |
| 10627 | /* The loopback device is special if any other network devices |
| 10628 | * is present in a network namespace the loopback device must |
| 10629 | * be present. Since we now dynamically allocate and free the |
| 10630 | * loopback device ensure this invariant is maintained by |
| 10631 | * keeping the loopback device as the first device on the |
| 10632 | * list of network devices. Ensuring the loopback devices |
| 10633 | * is the first device that appears and the last network device |
| 10634 | * that disappears. |
| 10635 | */ |
| 10636 | if (register_pernet_device(&loopback_net_ops)) |
| 10637 | goto out; |
| 10638 | |
| 10639 | if (register_pernet_device(&default_device_ops)) |
| 10640 | goto out; |
| 10641 | |
| 10642 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
| 10643 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); |
| 10644 | |
| 10645 | rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", |
| 10646 | NULL, dev_cpu_dead); |
| 10647 | WARN_ON(rc < 0); |
| 10648 | rc = 0; |
| 10649 | out: |
| 10650 | return rc; |
| 10651 | } |
| 10652 | |
| 10653 | subsys_initcall(net_dev_init); |