Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 =====================================
0004 Scaling in the Linux Networking Stack
0005 =====================================
0006 
0007 
0008 Introduction
0009 ============
0010 
0011 This document describes a set of complementary techniques in the Linux
0012 networking stack to increase parallelism and improve performance for
0013 multi-processor systems.
0014 
0015 The following technologies are described:
0016 
0017 - RSS: Receive Side Scaling
0018 - RPS: Receive Packet Steering
0019 - RFS: Receive Flow Steering
0020 - Accelerated Receive Flow Steering
0021 - XPS: Transmit Packet Steering
0022 
0023 
0024 RSS: Receive Side Scaling
0025 =========================
0026 
0027 Contemporary NICs support multiple receive and transmit descriptor queues
0028 (multi-queue). On reception, a NIC can send different packets to different
0029 queues to distribute processing among CPUs. The NIC distributes packets by
0030 applying a filter to each packet that assigns it to one of a small number
0031 of logical flows. Packets for each flow are steered to a separate receive
0032 queue, which in turn can be processed by separate CPUs. This mechanism is
0033 generally known as “Receive-side Scaling” (RSS). The goal of RSS and
0034 the other scaling techniques is to increase performance uniformly.
0035 Multi-queue distribution can also be used for traffic prioritization, but
0036 that is not the focus of these techniques.
0037 
0038 The filter used in RSS is typically a hash function over the network
0039 and/or transport layer headers-- for example, a 4-tuple hash over
0040 IP addresses and TCP ports of a packet. The most common hardware
0041 implementation of RSS uses a 128-entry indirection table where each entry
0042 stores a queue number. The receive queue for a packet is determined
0043 by masking out the low order seven bits of the computed hash for the
0044 packet (usually a Toeplitz hash), taking this number as a key into the
0045 indirection table and reading the corresponding value.
0046 
0047 Some advanced NICs allow steering packets to queues based on
0048 programmable filters. For example, webserver bound TCP port 80 packets
0049 can be directed to their own receive queue. Such “n-tuple” filters can
0050 be configured from ethtool (--config-ntuple).
0051 
0052 
0053 RSS Configuration
0054 -----------------
0055 
0056 The driver for a multi-queue capable NIC typically provides a kernel
0057 module parameter for specifying the number of hardware queues to
0058 configure. In the bnx2x driver, for instance, this parameter is called
0059 num_queues. A typical RSS configuration would be to have one receive queue
0060 for each CPU if the device supports enough queues, or otherwise at least
0061 one for each memory domain, where a memory domain is a set of CPUs that
0062 share a particular memory level (L1, L2, NUMA node, etc.).
0063 
0064 The indirection table of an RSS device, which resolves a queue by masked
0065 hash, is usually programmed by the driver at initialization. The
0066 default mapping is to distribute the queues evenly in the table, but the
0067 indirection table can be retrieved and modified at runtime using ethtool
0068 commands (--show-rxfh-indir and --set-rxfh-indir). Modifying the
0069 indirection table could be done to give different queues different
0070 relative weights.
0071 
0072 
0073 RSS IRQ Configuration
0074 ~~~~~~~~~~~~~~~~~~~~~
0075 
0076 Each receive queue has a separate IRQ associated with it. The NIC triggers
0077 this to notify a CPU when new packets arrive on the given queue. The
0078 signaling path for PCIe devices uses message signaled interrupts (MSI-X),
0079 that can route each interrupt to a particular CPU. The active mapping
0080 of queues to IRQs can be determined from /proc/interrupts. By default,
0081 an IRQ may be handled on any CPU. Because a non-negligible part of packet
0082 processing takes place in receive interrupt handling, it is advantageous
0083 to spread receive interrupts between CPUs. To manually adjust the IRQ
0084 affinity of each interrupt see Documentation/core-api/irq/irq-affinity.rst. Some systems
0085 will be running irqbalance, a daemon that dynamically optimizes IRQ
0086 assignments and as a result may override any manual settings.
0087 
0088 
0089 Suggested Configuration
0090 ~~~~~~~~~~~~~~~~~~~~~~~
0091 
0092 RSS should be enabled when latency is a concern or whenever receive
0093 interrupt processing forms a bottleneck. Spreading load between CPUs
0094 decreases queue length. For low latency networking, the optimal setting
0095 is to allocate as many queues as there are CPUs in the system (or the
0096 NIC maximum, if lower). The most efficient high-rate configuration
0097 is likely the one with the smallest number of receive queues where no
0098 receive queue overflows due to a saturated CPU, because in default
0099 mode with interrupt coalescing enabled, the aggregate number of
0100 interrupts (and thus work) grows with each additional queue.
0101 
0102 Per-cpu load can be observed using the mpstat utility, but note that on
0103 processors with hyperthreading (HT), each hyperthread is represented as
0104 a separate CPU. For interrupt handling, HT has shown no benefit in
0105 initial tests, so limit the number of queues to the number of CPU cores
0106 in the system.
0107 
0108 
0109 RPS: Receive Packet Steering
0110 ============================
0111 
0112 Receive Packet Steering (RPS) is logically a software implementation of
0113 RSS. Being in software, it is necessarily called later in the datapath.
0114 Whereas RSS selects the queue and hence CPU that will run the hardware
0115 interrupt handler, RPS selects the CPU to perform protocol processing
0116 above the interrupt handler. This is accomplished by placing the packet
0117 on the desired CPU’s backlog queue and waking up the CPU for processing.
0118 RPS has some advantages over RSS:
0119 
0120 1) it can be used with any NIC
0121 2) software filters can easily be added to hash over new protocols
0122 3) it does not increase hardware device interrupt rate (although it does
0123    introduce inter-processor interrupts (IPIs))
0124 
0125 RPS is called during bottom half of the receive interrupt handler, when
0126 a driver sends a packet up the network stack with netif_rx() or
0127 netif_receive_skb(). These call the get_rps_cpu() function, which
0128 selects the queue that should process a packet.
0129 
0130 The first step in determining the target CPU for RPS is to calculate a
0131 flow hash over the packet’s addresses or ports (2-tuple or 4-tuple hash
0132 depending on the protocol). This serves as a consistent hash of the
0133 associated flow of the packet. The hash is either provided by hardware
0134 or will be computed in the stack. Capable hardware can pass the hash in
0135 the receive descriptor for the packet; this would usually be the same
0136 hash used for RSS (e.g. computed Toeplitz hash). The hash is saved in
0137 skb->hash and can be used elsewhere in the stack as a hash of the
0138 packet’s flow.
0139 
0140 Each receive hardware queue has an associated list of CPUs to which
0141 RPS may enqueue packets for processing. For each received packet,
0142 an index into the list is computed from the flow hash modulo the size
0143 of the list. The indexed CPU is the target for processing the packet,
0144 and the packet is queued to the tail of that CPU’s backlog queue. At
0145 the end of the bottom half routine, IPIs are sent to any CPUs for which
0146 packets have been queued to their backlog queue. The IPI wakes backlog
0147 processing on the remote CPU, and any queued packets are then processed
0148 up the networking stack.
0149 
0150 
0151 RPS Configuration
0152 -----------------
0153 
0154 RPS requires a kernel compiled with the CONFIG_RPS kconfig symbol (on
0155 by default for SMP). Even when compiled in, RPS remains disabled until
0156 explicitly configured. The list of CPUs to which RPS may forward traffic
0157 can be configured for each receive queue using a sysfs file entry::
0158 
0159   /sys/class/net/<dev>/queues/rx-<n>/rps_cpus
0160 
0161 This file implements a bitmap of CPUs. RPS is disabled when it is zero
0162 (the default), in which case packets are processed on the interrupting
0163 CPU. Documentation/core-api/irq/irq-affinity.rst explains how CPUs are assigned to
0164 the bitmap.
0165 
0166 
0167 Suggested Configuration
0168 ~~~~~~~~~~~~~~~~~~~~~~~
0169 
0170 For a single queue device, a typical RPS configuration would be to set
0171 the rps_cpus to the CPUs in the same memory domain of the interrupting
0172 CPU. If NUMA locality is not an issue, this could also be all CPUs in
0173 the system. At high interrupt rate, it might be wise to exclude the
0174 interrupting CPU from the map since that already performs much work.
0175 
0176 For a multi-queue system, if RSS is configured so that a hardware
0177 receive queue is mapped to each CPU, then RPS is probably redundant
0178 and unnecessary. If there are fewer hardware queues than CPUs, then
0179 RPS might be beneficial if the rps_cpus for each queue are the ones that
0180 share the same memory domain as the interrupting CPU for that queue.
0181 
0182 
0183 RPS Flow Limit
0184 --------------
0185 
0186 RPS scales kernel receive processing across CPUs without introducing
0187 reordering. The trade-off to sending all packets from the same flow
0188 to the same CPU is CPU load imbalance if flows vary in packet rate.
0189 In the extreme case a single flow dominates traffic. Especially on
0190 common server workloads with many concurrent connections, such
0191 behavior indicates a problem such as a misconfiguration or spoofed
0192 source Denial of Service attack.
0193 
0194 Flow Limit is an optional RPS feature that prioritizes small flows
0195 during CPU contention by dropping packets from large flows slightly
0196 ahead of those from small flows. It is active only when an RPS or RFS
0197 destination CPU approaches saturation.  Once a CPU's input packet
0198 queue exceeds half the maximum queue length (as set by sysctl
0199 net.core.netdev_max_backlog), the kernel starts a per-flow packet
0200 count over the last 256 packets. If a flow exceeds a set ratio (by
0201 default, half) of these packets when a new packet arrives, then the
0202 new packet is dropped. Packets from other flows are still only
0203 dropped once the input packet queue reaches netdev_max_backlog.
0204 No packets are dropped when the input packet queue length is below
0205 the threshold, so flow limit does not sever connections outright:
0206 even large flows maintain connectivity.
0207 
0208 
0209 Interface
0210 ~~~~~~~~~
0211 
0212 Flow limit is compiled in by default (CONFIG_NET_FLOW_LIMIT), but not
0213 turned on. It is implemented for each CPU independently (to avoid lock
0214 and cache contention) and toggled per CPU by setting the relevant bit
0215 in sysctl net.core.flow_limit_cpu_bitmap. It exposes the same CPU
0216 bitmap interface as rps_cpus (see above) when called from procfs::
0217 
0218   /proc/sys/net/core/flow_limit_cpu_bitmap
0219 
0220 Per-flow rate is calculated by hashing each packet into a hashtable
0221 bucket and incrementing a per-bucket counter. The hash function is
0222 the same that selects a CPU in RPS, but as the number of buckets can
0223 be much larger than the number of CPUs, flow limit has finer-grained
0224 identification of large flows and fewer false positives. The default
0225 table has 4096 buckets. This value can be modified through sysctl::
0226 
0227   net.core.flow_limit_table_len
0228 
0229 The value is only consulted when a new table is allocated. Modifying
0230 it does not update active tables.
0231 
0232 
0233 Suggested Configuration
0234 ~~~~~~~~~~~~~~~~~~~~~~~
0235 
0236 Flow limit is useful on systems with many concurrent connections,
0237 where a single connection taking up 50% of a CPU indicates a problem.
0238 In such environments, enable the feature on all CPUs that handle
0239 network rx interrupts (as set in /proc/irq/N/smp_affinity).
0240 
0241 The feature depends on the input packet queue length to exceed
0242 the flow limit threshold (50%) + the flow history length (256).
0243 Setting net.core.netdev_max_backlog to either 1000 or 10000
0244 performed well in experiments.
0245 
0246 
0247 RFS: Receive Flow Steering
0248 ==========================
0249 
0250 While RPS steers packets solely based on hash, and thus generally
0251 provides good load distribution, it does not take into account
0252 application locality. This is accomplished by Receive Flow Steering
0253 (RFS). The goal of RFS is to increase datacache hitrate by steering
0254 kernel processing of packets to the CPU where the application thread
0255 consuming the packet is running. RFS relies on the same RPS mechanisms
0256 to enqueue packets onto the backlog of another CPU and to wake up that
0257 CPU.
0258 
0259 In RFS, packets are not forwarded directly by the value of their hash,
0260 but the hash is used as index into a flow lookup table. This table maps
0261 flows to the CPUs where those flows are being processed. The flow hash
0262 (see RPS section above) is used to calculate the index into this table.
0263 The CPU recorded in each entry is the one which last processed the flow.
0264 If an entry does not hold a valid CPU, then packets mapped to that entry
0265 are steered using plain RPS. Multiple table entries may point to the
0266 same CPU. Indeed, with many flows and few CPUs, it is very likely that
0267 a single application thread handles flows with many different flow hashes.
0268 
0269 rps_sock_flow_table is a global flow table that contains the *desired* CPU
0270 for flows: the CPU that is currently processing the flow in userspace.
0271 Each table value is a CPU index that is updated during calls to recvmsg
0272 and sendmsg (specifically, inet_recvmsg(), inet_sendmsg(), inet_sendpage()
0273 and tcp_splice_read()).
0274 
0275 When the scheduler moves a thread to a new CPU while it has outstanding
0276 receive packets on the old CPU, packets may arrive out of order. To
0277 avoid this, RFS uses a second flow table to track outstanding packets
0278 for each flow: rps_dev_flow_table is a table specific to each hardware
0279 receive queue of each device. Each table value stores a CPU index and a
0280 counter. The CPU index represents the *current* CPU onto which packets
0281 for this flow are enqueued for further kernel processing. Ideally, kernel
0282 and userspace processing occur on the same CPU, and hence the CPU index
0283 in both tables is identical. This is likely false if the scheduler has
0284 recently migrated a userspace thread while the kernel still has packets
0285 enqueued for kernel processing on the old CPU.
0286 
0287 The counter in rps_dev_flow_table values records the length of the current
0288 CPU's backlog when a packet in this flow was last enqueued. Each backlog
0289 queue has a head counter that is incremented on dequeue. A tail counter
0290 is computed as head counter + queue length. In other words, the counter
0291 in rps_dev_flow[i] records the last element in flow i that has
0292 been enqueued onto the currently designated CPU for flow i (of course,
0293 entry i is actually selected by hash and multiple flows may hash to the
0294 same entry i).
0295 
0296 And now the trick for avoiding out of order packets: when selecting the
0297 CPU for packet processing (from get_rps_cpu()) the rps_sock_flow table
0298 and the rps_dev_flow table of the queue that the packet was received on
0299 are compared. If the desired CPU for the flow (found in the
0300 rps_sock_flow table) matches the current CPU (found in the rps_dev_flow
0301 table), the packet is enqueued onto that CPU’s backlog. If they differ,
0302 the current CPU is updated to match the desired CPU if one of the
0303 following is true:
0304 
0305   - The current CPU's queue head counter >= the recorded tail counter
0306     value in rps_dev_flow[i]
0307   - The current CPU is unset (>= nr_cpu_ids)
0308   - The current CPU is offline
0309 
0310 After this check, the packet is sent to the (possibly updated) current
0311 CPU. These rules aim to ensure that a flow only moves to a new CPU when
0312 there are no packets outstanding on the old CPU, as the outstanding
0313 packets could arrive later than those about to be processed on the new
0314 CPU.
0315 
0316 
0317 RFS Configuration
0318 -----------------
0319 
0320 RFS is only available if the kconfig symbol CONFIG_RPS is enabled (on
0321 by default for SMP). The functionality remains disabled until explicitly
0322 configured. The number of entries in the global flow table is set through::
0323 
0324   /proc/sys/net/core/rps_sock_flow_entries
0325 
0326 The number of entries in the per-queue flow table are set through::
0327 
0328   /sys/class/net/<dev>/queues/rx-<n>/rps_flow_cnt
0329 
0330 
0331 Suggested Configuration
0332 ~~~~~~~~~~~~~~~~~~~~~~~
0333 
0334 Both of these need to be set before RFS is enabled for a receive queue.
0335 Values for both are rounded up to the nearest power of two. The
0336 suggested flow count depends on the expected number of active connections
0337 at any given time, which may be significantly less than the number of open
0338 connections. We have found that a value of 32768 for rps_sock_flow_entries
0339 works fairly well on a moderately loaded server.
0340 
0341 For a single queue device, the rps_flow_cnt value for the single queue
0342 would normally be configured to the same value as rps_sock_flow_entries.
0343 For a multi-queue device, the rps_flow_cnt for each queue might be
0344 configured as rps_sock_flow_entries / N, where N is the number of
0345 queues. So for instance, if rps_sock_flow_entries is set to 32768 and there
0346 are 16 configured receive queues, rps_flow_cnt for each queue might be
0347 configured as 2048.
0348 
0349 
0350 Accelerated RFS
0351 ===============
0352 
0353 Accelerated RFS is to RFS what RSS is to RPS: a hardware-accelerated load
0354 balancing mechanism that uses soft state to steer flows based on where
0355 the application thread consuming the packets of each flow is running.
0356 Accelerated RFS should perform better than RFS since packets are sent
0357 directly to a CPU local to the thread consuming the data. The target CPU
0358 will either be the same CPU where the application runs, or at least a CPU
0359 which is local to the application thread’s CPU in the cache hierarchy.
0360 
0361 To enable accelerated RFS, the networking stack calls the
0362 ndo_rx_flow_steer driver function to communicate the desired hardware
0363 queue for packets matching a particular flow. The network stack
0364 automatically calls this function every time a flow entry in
0365 rps_dev_flow_table is updated. The driver in turn uses a device specific
0366 method to program the NIC to steer the packets.
0367 
0368 The hardware queue for a flow is derived from the CPU recorded in
0369 rps_dev_flow_table. The stack consults a CPU to hardware queue map which
0370 is maintained by the NIC driver. This is an auto-generated reverse map of
0371 the IRQ affinity table shown by /proc/interrupts. Drivers can use
0372 functions in the cpu_rmap (“CPU affinity reverse map”) kernel library
0373 to populate the map. For each CPU, the corresponding queue in the map is
0374 set to be one whose processing CPU is closest in cache locality.
0375 
0376 
0377 Accelerated RFS Configuration
0378 -----------------------------
0379 
0380 Accelerated RFS is only available if the kernel is compiled with
0381 CONFIG_RFS_ACCEL and support is provided by the NIC device and driver.
0382 It also requires that ntuple filtering is enabled via ethtool. The map
0383 of CPU to queues is automatically deduced from the IRQ affinities
0384 configured for each receive queue by the driver, so no additional
0385 configuration should be necessary.
0386 
0387 
0388 Suggested Configuration
0389 ~~~~~~~~~~~~~~~~~~~~~~~
0390 
0391 This technique should be enabled whenever one wants to use RFS and the
0392 NIC supports hardware acceleration.
0393 
0394 
0395 XPS: Transmit Packet Steering
0396 =============================
0397 
0398 Transmit Packet Steering is a mechanism for intelligently selecting
0399 which transmit queue to use when transmitting a packet on a multi-queue
0400 device. This can be accomplished by recording two kinds of maps, either
0401 a mapping of CPU to hardware queue(s) or a mapping of receive queue(s)
0402 to hardware transmit queue(s).
0403 
0404 1. XPS using CPUs map
0405 
0406 The goal of this mapping is usually to assign queues
0407 exclusively to a subset of CPUs, where the transmit completions for
0408 these queues are processed on a CPU within this set. This choice
0409 provides two benefits. First, contention on the device queue lock is
0410 significantly reduced since fewer CPUs contend for the same queue
0411 (contention can be eliminated completely if each CPU has its own
0412 transmit queue). Secondly, cache miss rate on transmit completion is
0413 reduced, in particular for data cache lines that hold the sk_buff
0414 structures.
0415 
0416 2. XPS using receive queues map
0417 
0418 This mapping is used to pick transmit queue based on the receive
0419 queue(s) map configuration set by the administrator. A set of receive
0420 queues can be mapped to a set of transmit queues (many:many), although
0421 the common use case is a 1:1 mapping. This will enable sending packets
0422 on the same queue associations for transmit and receive. This is useful for
0423 busy polling multi-threaded workloads where there are challenges in
0424 associating a given CPU to a given application thread. The application
0425 threads are not pinned to CPUs and each thread handles packets
0426 received on a single queue. The receive queue number is cached in the
0427 socket for the connection. In this model, sending the packets on the same
0428 transmit queue corresponding to the associated receive queue has benefits
0429 in keeping the CPU overhead low. Transmit completion work is locked into
0430 the same queue-association that a given application is polling on. This
0431 avoids the overhead of triggering an interrupt on another CPU. When the
0432 application cleans up the packets during the busy poll, transmit completion
0433 may be processed along with it in the same thread context and so result in
0434 reduced latency.
0435 
0436 XPS is configured per transmit queue by setting a bitmap of
0437 CPUs/receive-queues that may use that queue to transmit. The reverse
0438 mapping, from CPUs to transmit queues or from receive-queues to transmit
0439 queues, is computed and maintained for each network device. When
0440 transmitting the first packet in a flow, the function get_xps_queue() is
0441 called to select a queue. This function uses the ID of the receive queue
0442 for the socket connection for a match in the receive queue-to-transmit queue
0443 lookup table. Alternatively, this function can also use the ID of the
0444 running CPU as a key into the CPU-to-queue lookup table. If the
0445 ID matches a single queue, that is used for transmission. If multiple
0446 queues match, one is selected by using the flow hash to compute an index
0447 into the set. When selecting the transmit queue based on receive queue(s)
0448 map, the transmit device is not validated against the receive device as it
0449 requires expensive lookup operation in the datapath.
0450 
0451 The queue chosen for transmitting a particular flow is saved in the
0452 corresponding socket structure for the flow (e.g. a TCP connection).
0453 This transmit queue is used for subsequent packets sent on the flow to
0454 prevent out of order (ooo) packets. The choice also amortizes the cost
0455 of calling get_xps_queues() over all packets in the flow. To avoid
0456 ooo packets, the queue for a flow can subsequently only be changed if
0457 skb->ooo_okay is set for a packet in the flow. This flag indicates that
0458 there are no outstanding packets in the flow, so the transmit queue can
0459 change without the risk of generating out of order packets. The
0460 transport layer is responsible for setting ooo_okay appropriately. TCP,
0461 for instance, sets the flag when all data for a connection has been
0462 acknowledged.
0463 
0464 XPS Configuration
0465 -----------------
0466 
0467 XPS is only available if the kconfig symbol CONFIG_XPS is enabled (on by
0468 default for SMP). If compiled in, it is driver dependent whether, and
0469 how, XPS is configured at device init. The mapping of CPUs/receive-queues
0470 to transmit queue can be inspected and configured using sysfs:
0471 
0472 For selection based on CPUs map::
0473 
0474   /sys/class/net/<dev>/queues/tx-<n>/xps_cpus
0475 
0476 For selection based on receive-queues map::
0477 
0478   /sys/class/net/<dev>/queues/tx-<n>/xps_rxqs
0479 
0480 
0481 Suggested Configuration
0482 ~~~~~~~~~~~~~~~~~~~~~~~
0483 
0484 For a network device with a single transmission queue, XPS configuration
0485 has no effect, since there is no choice in this case. In a multi-queue
0486 system, XPS is preferably configured so that each CPU maps onto one queue.
0487 If there are as many queues as there are CPUs in the system, then each
0488 queue can also map onto one CPU, resulting in exclusive pairings that
0489 experience no contention. If there are fewer queues than CPUs, then the
0490 best CPUs to share a given queue are probably those that share the cache
0491 with the CPU that processes transmit completions for that queue
0492 (transmit interrupts).
0493 
0494 For transmit queue selection based on receive queue(s), XPS has to be
0495 explicitly configured mapping receive-queue(s) to transmit queue(s). If the
0496 user configuration for receive-queue map does not apply, then the transmit
0497 queue is selected based on the CPUs map.
0498 
0499 
0500 Per TX Queue rate limitation
0501 ============================
0502 
0503 These are rate-limitation mechanisms implemented by HW, where currently
0504 a max-rate attribute is supported, by setting a Mbps value to::
0505 
0506   /sys/class/net/<dev>/queues/tx-<n>/tx_maxrate
0507 
0508 A value of zero means disabled, and this is the default.
0509 
0510 
0511 Further Information
0512 ===================
0513 RPS and RFS were introduced in kernel 2.6.35. XPS was incorporated into
0514 2.6.38. Original patches were submitted by Tom Herbert
0515 (therbert@google.com)
0516 
0517 Accelerated RFS was introduced in 2.6.35. Original patches were
0518 submitted by Ben Hutchings (bwh@kernel.org)
0519 
0520 Authors:
0521 
0522 - Tom Herbert (therbert@google.com)
0523 - Willem de Bruijn (willemb@google.com)