1.. SPDX-License-Identifier: GPL-2.0 2 3===================================== 4Scaling in the Linux Networking Stack 5===================================== 6 7 8Introduction 9============ 10 11This document describes a set of complementary techniques in the Linux 12networking stack to increase parallelism and improve performance for 13multi-processor systems. 14 15The following technologies are described: 16 17- RSS: Receive Side Scaling 18- RPS: Receive Packet Steering 19- RFS: Receive Flow Steering 20- Accelerated Receive Flow Steering 21- XPS: Transmit Packet Steering 22 23 24RSS: Receive Side Scaling 25========================= 26 27Contemporary NICs support multiple receive and transmit descriptor queues 28(multi-queue). On reception, a NIC can send different packets to different 29queues to distribute processing among CPUs. The NIC distributes packets by 30applying a filter to each packet that assigns it to one of a small number 31of logical flows. Packets for each flow are steered to a separate receive 32queue, which in turn can be processed by separate CPUs. This mechanism is 33generally known as “Receive-side Scaling” (RSS). The goal of RSS and 34the other scaling techniques is to increase performance uniformly. 35Multi-queue distribution can also be used for traffic prioritization, but 36that is not the focus of these techniques. 37 38The filter used in RSS is typically a hash function over the network 39and/or transport layer headers-- for example, a 4-tuple hash over 40IP addresses and TCP ports of a packet. The most common hardware 41implementation of RSS uses a 128-entry indirection table where each entry 42stores a queue number. The receive queue for a packet is determined 43by masking out the low order seven bits of the computed hash for the 44packet (usually a Toeplitz hash), taking this number as a key into the 45indirection table and reading the corresponding value. 46 47Some NICs support symmetric RSS hashing where, if the IP (source address, 48destination address) and TCP/UDP (source port, destination port) tuples 49are swapped, the computed hash is the same. This is beneficial in some 50applications that monitor TCP/IP flows (IDS, firewalls, ...etc) and need 51both directions of the flow to land on the same Rx queue (and CPU). The 52"Symmetric-XOR" and "Symmetric-OR-XOR" are types of RSS algorithms that 53achieve this hash symmetry by XOR/ORing the input source and destination 54fields of the IP and/or L4 protocols. This, however, results in reduced 55input entropy and could potentially be exploited. 56 57Specifically, the "Symmetric-XOR" algorithm XORs the input 58as follows:: 59 60 # (SRC_IP ^ DST_IP, SRC_IP ^ DST_IP, SRC_PORT ^ DST_PORT, SRC_PORT ^ DST_PORT) 61 62The "Symmetric-OR-XOR" algorithm, on the other hand, transforms the input as 63follows:: 64 65 # (SRC_IP | DST_IP, SRC_IP ^ DST_IP, SRC_PORT | DST_PORT, SRC_PORT ^ DST_PORT) 66 67The result is then fed to the underlying RSS algorithm. 68 69Some advanced NICs allow steering packets to queues based on 70programmable filters. For example, webserver bound TCP port 80 packets 71can be directed to their own receive queue. Such “n-tuple” filters can 72be configured from ethtool (--config-ntuple). 73 74 75RSS Configuration 76----------------- 77 78The driver for a multi-queue capable NIC typically provides a kernel 79module parameter for specifying the number of hardware queues to 80configure. In the bnx2x driver, for instance, this parameter is called 81num_queues. A typical RSS configuration would be to have one receive queue 82for each CPU if the device supports enough queues, or otherwise at least 83one for each memory domain, where a memory domain is a set of CPUs that 84share a particular memory level (L1, L2, NUMA node, etc.). 85 86The indirection table of an RSS device, which resolves a queue by masked 87hash, is usually programmed by the driver at initialization. The 88default mapping is to distribute the queues evenly in the table, but the 89indirection table can be retrieved and modified at runtime using ethtool 90commands (--show-rxfh-indir and --set-rxfh-indir). Modifying the 91indirection table could be done to give different queues different 92relative weights. 93 94 95RSS IRQ Configuration 96~~~~~~~~~~~~~~~~~~~~~ 97 98Each receive queue has a separate IRQ associated with it. The NIC triggers 99this to notify a CPU when new packets arrive on the given queue. The 100signaling path for PCIe devices uses message signaled interrupts (MSI-X), 101that can route each interrupt to a particular CPU. The active mapping 102of queues to IRQs can be determined from /proc/interrupts. By default, 103an IRQ may be handled on any CPU. Because a non-negligible part of packet 104processing takes place in receive interrupt handling, it is advantageous 105to spread receive interrupts between CPUs. To manually adjust the IRQ 106affinity of each interrupt see Documentation/core-api/irq/irq-affinity.rst. Some systems 107will be running irqbalance, a daemon that dynamically optimizes IRQ 108assignments and as a result may override any manual settings. 109 110 111Suggested Configuration 112~~~~~~~~~~~~~~~~~~~~~~~ 113 114RSS should be enabled when latency is a concern or whenever receive 115interrupt processing forms a bottleneck. Spreading load between CPUs 116decreases queue length. For low latency networking, the optimal setting 117is to allocate as many queues as there are CPUs in the system (or the 118NIC maximum, if lower). The most efficient high-rate configuration 119is likely the one with the smallest number of receive queues where no 120receive queue overflows due to a saturated CPU, because in default 121mode with interrupt coalescing enabled, the aggregate number of 122interrupts (and thus work) grows with each additional queue. 123 124Per-cpu load can be observed using the mpstat utility, but note that on 125processors with hyperthreading (HT), each hyperthread is represented as 126a separate CPU. For interrupt handling, HT has shown no benefit in 127initial tests, so limit the number of queues to the number of CPU cores 128in the system. 129 130Dedicated RSS contexts 131~~~~~~~~~~~~~~~~~~~~~~ 132 133Modern NICs support creating multiple co-existing RSS configurations 134which are selected based on explicit matching rules. This can be very 135useful when application wants to constrain the set of queues receiving 136traffic for e.g. a particular destination port or IP address. 137The example below shows how to direct all traffic to TCP port 22 138to queues 0 and 1. 139 140To create an additional RSS context use:: 141 142 # ethtool -X eth0 hfunc toeplitz context new 143 New RSS context is 1 144 145Kernel reports back the ID of the allocated context (the default, always 146present RSS context has ID of 0). The new context can be queried and 147modified using the same APIs as the default context:: 148 149 # ethtool -x eth0 context 1 150 RX flow hash indirection table for eth0 with 13 RX ring(s): 151 0: 0 1 2 3 4 5 6 7 152 8: 8 9 10 11 12 0 1 2 153 [...] 154 # ethtool -X eth0 equal 2 context 1 155 # ethtool -x eth0 context 1 156 RX flow hash indirection table for eth0 with 13 RX ring(s): 157 0: 0 1 0 1 0 1 0 1 158 8: 0 1 0 1 0 1 0 1 159 [...] 160 161To make use of the new context direct traffic to it using an n-tuple 162filter:: 163 164 # ethtool -N eth0 flow-type tcp6 dst-port 22 context 1 165 Added rule with ID 1023 166 167When done, remove the context and the rule:: 168 169 # ethtool -N eth0 delete 1023 170 # ethtool -X eth0 context 1 delete 171 172 173RPS: Receive Packet Steering 174============================ 175 176Receive Packet Steering (RPS) is logically a software implementation of 177RSS. Being in software, it is necessarily called later in the datapath. 178Whereas RSS selects the queue and hence CPU that will run the hardware 179interrupt handler, RPS selects the CPU to perform protocol processing 180above the interrupt handler. This is accomplished by placing the packet 181on the desired CPU’s backlog queue and waking up the CPU for processing. 182RPS has some advantages over RSS: 183 1841) it can be used with any NIC 1852) software filters can easily be added to hash over new protocols 1863) it does not increase hardware device interrupt rate (although it does 187 introduce inter-processor interrupts (IPIs)) 188 189RPS is called during bottom half of the receive interrupt handler, when 190a driver sends a packet up the network stack with netif_rx() or 191netif_receive_skb(). These call the get_rps_cpu() function, which 192selects the queue that should process a packet. 193 194The first step in determining the target CPU for RPS is to calculate a 195flow hash over the packet’s addresses or ports (2-tuple or 4-tuple hash 196depending on the protocol). This serves as a consistent hash of the 197associated flow of the packet. The hash is either provided by hardware 198or will be computed in the stack. Capable hardware can pass the hash in 199the receive descriptor for the packet; this would usually be the same 200hash used for RSS (e.g. computed Toeplitz hash). The hash is saved in 201skb->hash and can be used elsewhere in the stack as a hash of the 202packet’s flow. 203 204Each receive hardware queue has an associated list of CPUs to which 205RPS may enqueue packets for processing. For each received packet, 206an index into the list is computed from the flow hash modulo the size 207of the list. The indexed CPU is the target for processing the packet, 208and the packet is queued to the tail of that CPU’s backlog queue. At 209the end of the bottom half routine, IPIs are sent to any CPUs for which 210packets have been queued to their backlog queue. The IPI wakes backlog 211processing on the remote CPU, and any queued packets are then processed 212up the networking stack. 213 214 215RPS Configuration 216----------------- 217 218RPS requires a kernel compiled with the CONFIG_RPS kconfig symbol (on 219by default for SMP). Even when compiled in, RPS remains disabled until 220explicitly configured. The list of CPUs to which RPS may forward traffic 221can be configured for each receive queue using a sysfs file entry:: 222 223 /sys/class/net/<dev>/queues/rx-<n>/rps_cpus 224 225This file implements a bitmap of CPUs. RPS is disabled when it is zero 226(the default), in which case packets are processed on the interrupting 227CPU. Documentation/core-api/irq/irq-affinity.rst explains how CPUs are assigned to 228the bitmap. 229 230 231Suggested Configuration 232~~~~~~~~~~~~~~~~~~~~~~~ 233 234For a single queue device, a typical RPS configuration would be to set 235the rps_cpus to the CPUs in the same memory domain of the interrupting 236CPU. If NUMA locality is not an issue, this could also be all CPUs in 237the system. At high interrupt rate, it might be wise to exclude the 238interrupting CPU from the map since that already performs much work. 239 240For a multi-queue system, if RSS is configured so that a hardware 241receive queue is mapped to each CPU, then RPS is probably redundant 242and unnecessary. If there are fewer hardware queues than CPUs, then 243RPS might be beneficial if the rps_cpus for each queue are the ones that 244share the same memory domain as the interrupting CPU for that queue. 245 246 247RPS Flow Limit 248-------------- 249 250RPS scales kernel receive processing across CPUs without introducing 251reordering. The trade-off to sending all packets from the same flow 252to the same CPU is CPU load imbalance if flows vary in packet rate. 253In the extreme case a single flow dominates traffic. Especially on 254common server workloads with many concurrent connections, such 255behavior indicates a problem such as a misconfiguration or spoofed 256source Denial of Service attack. 257 258Flow Limit is an optional RPS feature that prioritizes small flows 259during CPU contention by dropping packets from large flows slightly 260ahead of those from small flows. It is active only when an RPS or RFS 261destination CPU approaches saturation. Once a CPU's input packet 262queue exceeds half the maximum queue length (as set by sysctl 263net.core.netdev_max_backlog), the kernel starts a per-flow packet 264count over the last 256 packets. If a flow exceeds a set ratio (by 265default, half) of these packets when a new packet arrives, then the 266new packet is dropped. Packets from other flows are still only 267dropped once the input packet queue reaches netdev_max_backlog. 268No packets are dropped when the input packet queue length is below 269the threshold, so flow limit does not sever connections outright: 270even large flows maintain connectivity. 271 272 273Interface 274~~~~~~~~~ 275 276Flow limit is compiled in by default (CONFIG_NET_FLOW_LIMIT), but not 277turned on. It is implemented for each CPU independently (to avoid lock 278and cache contention) and toggled per CPU by setting the relevant bit 279in sysctl net.core.flow_limit_cpu_bitmap. It exposes the same CPU 280bitmap interface as rps_cpus (see above) when called from procfs:: 281 282 /proc/sys/net/core/flow_limit_cpu_bitmap 283 284Per-flow rate is calculated by hashing each packet into a hashtable 285bucket and incrementing a per-bucket counter. The hash function is 286the same that selects a CPU in RPS, but as the number of buckets can 287be much larger than the number of CPUs, flow limit has finer-grained 288identification of large flows and fewer false positives. The default 289table has 4096 buckets. This value can be modified through sysctl:: 290 291 net.core.flow_limit_table_len 292 293The value is only consulted when a new table is allocated. Modifying 294it does not update active tables. 295 296 297Suggested Configuration 298~~~~~~~~~~~~~~~~~~~~~~~ 299 300Flow limit is useful on systems with many concurrent connections, 301where a single connection taking up 50% of a CPU indicates a problem. 302In such environments, enable the feature on all CPUs that handle 303network rx interrupts (as set in /proc/irq/N/smp_affinity). 304 305The feature depends on the input packet queue length to exceed 306the flow limit threshold (50%) + the flow history length (256). 307Setting net.core.netdev_max_backlog to either 1000 or 10000 308performed well in experiments. 309 310 311RFS: Receive Flow Steering 312========================== 313 314While RPS steers packets solely based on hash, and thus generally 315provides good load distribution, it does not take into account 316application locality. This is accomplished by Receive Flow Steering 317(RFS). The goal of RFS is to increase datacache hitrate by steering 318kernel processing of packets to the CPU where the application thread 319consuming the packet is running. RFS relies on the same RPS mechanisms 320to enqueue packets onto the backlog of another CPU and to wake up that 321CPU. 322 323In RFS, packets are not forwarded directly by the value of their hash, 324but the hash is used as index into a flow lookup table. This table maps 325flows to the CPUs where those flows are being processed. The flow hash 326(see RPS section above) is used to calculate the index into this table. 327The CPU recorded in each entry is the one which last processed the flow. 328If an entry does not hold a valid CPU, then packets mapped to that entry 329are steered using plain RPS. Multiple table entries may point to the 330same CPU. Indeed, with many flows and few CPUs, it is very likely that 331a single application thread handles flows with many different flow hashes. 332 333rps_sock_flow_table is a global flow table that contains the *desired* CPU 334for flows: the CPU that is currently processing the flow in userspace. 335Each table value is a CPU index that is updated during calls to recvmsg 336and sendmsg (specifically, inet_recvmsg(), inet_sendmsg() and 337tcp_splice_read()). 338 339When the scheduler moves a thread to a new CPU while it has outstanding 340receive packets on the old CPU, packets may arrive out of order. To 341avoid this, RFS uses a second flow table to track outstanding packets 342for each flow: rps_dev_flow_table is a table specific to each hardware 343receive queue of each device. Each table value stores a CPU index and a 344counter. The CPU index represents the *current* CPU onto which packets 345for this flow are enqueued for further kernel processing. Ideally, kernel 346and userspace processing occur on the same CPU, and hence the CPU index 347in both tables is identical. This is likely false if the scheduler has 348recently migrated a userspace thread while the kernel still has packets 349enqueued for kernel processing on the old CPU. 350 351The counter in rps_dev_flow_table values records the length of the current 352CPU's backlog when a packet in this flow was last enqueued. Each backlog 353queue has a head counter that is incremented on dequeue. A tail counter 354is computed as head counter + queue length. In other words, the counter 355in rps_dev_flow[i] records the last element in flow i that has 356been enqueued onto the currently designated CPU for flow i (of course, 357entry i is actually selected by hash and multiple flows may hash to the 358same entry i). 359 360And now the trick for avoiding out of order packets: when selecting the 361CPU for packet processing (from get_rps_cpu()) the rps_sock_flow table 362and the rps_dev_flow table of the queue that the packet was received on 363are compared. If the desired CPU for the flow (found in the 364rps_sock_flow table) matches the current CPU (found in the rps_dev_flow 365table), the packet is enqueued onto that CPU’s backlog. If they differ, 366the current CPU is updated to match the desired CPU if one of the 367following is true: 368 369 - The current CPU's queue head counter >= the recorded tail counter 370 value in rps_dev_flow[i] 371 - The current CPU is unset (>= nr_cpu_ids) 372 - The current CPU is offline 373 374After this check, the packet is sent to the (possibly updated) current 375CPU. These rules aim to ensure that a flow only moves to a new CPU when 376there are no packets outstanding on the old CPU, as the outstanding 377packets could arrive later than those about to be processed on the new 378CPU. 379 380 381RFS Configuration 382----------------- 383 384RFS is only available if the kconfig symbol CONFIG_RPS is enabled (on 385by default for SMP). The functionality remains disabled until explicitly 386configured. The number of entries in the global flow table is set through:: 387 388 /proc/sys/net/core/rps_sock_flow_entries 389 390The number of entries in the per-queue flow table are set through:: 391 392 /sys/class/net/<dev>/queues/rx-<n>/rps_flow_cnt 393 394 395Suggested Configuration 396~~~~~~~~~~~~~~~~~~~~~~~ 397 398Both of these need to be set before RFS is enabled for a receive queue. 399Values for both are rounded up to the nearest power of two. The 400suggested flow count depends on the expected number of active connections 401at any given time, which may be significantly less than the number of open 402connections. We have found that a value of 32768 for rps_sock_flow_entries 403works fairly well on a moderately loaded server. 404 405For a single queue device, the rps_flow_cnt value for the single queue 406would normally be configured to the same value as rps_sock_flow_entries. 407For a multi-queue device, the rps_flow_cnt for each queue might be 408configured as rps_sock_flow_entries / N, where N is the number of 409queues. So for instance, if rps_sock_flow_entries is set to 32768 and there 410are 16 configured receive queues, rps_flow_cnt for each queue might be 411configured as 2048. 412 413 414Accelerated RFS 415=============== 416 417Accelerated RFS is to RFS what RSS is to RPS: a hardware-accelerated load 418balancing mechanism that uses soft state to steer flows based on where 419the application thread consuming the packets of each flow is running. 420Accelerated RFS should perform better than RFS since packets are sent 421directly to a CPU local to the thread consuming the data. The target CPU 422will either be the same CPU where the application runs, or at least a CPU 423which is local to the application thread’s CPU in the cache hierarchy. 424 425To enable accelerated RFS, the networking stack calls the 426ndo_rx_flow_steer driver function to communicate the desired hardware 427queue for packets matching a particular flow. The network stack 428automatically calls this function every time a flow entry in 429rps_dev_flow_table is updated. The driver in turn uses a device specific 430method to program the NIC to steer the packets. 431 432The hardware queue for a flow is derived from the CPU recorded in 433rps_dev_flow_table. The stack consults a CPU to hardware queue map which 434is maintained by the NIC driver. This is an auto-generated reverse map of 435the IRQ affinity table shown by /proc/interrupts. Drivers can use 436functions in the cpu_rmap (“CPU affinity reverse map”) kernel library 437to populate the map. Alternatively, drivers can delegate the cpu_rmap 438management to the Kernel by calling netif_enable_cpu_rmap(). For each CPU, 439the corresponding queue in the map is set to be one whose processing CPU is 440closest in cache locality. 441 442 443Accelerated RFS Configuration 444----------------------------- 445 446Accelerated RFS is only available if the kernel is compiled with 447CONFIG_RFS_ACCEL and support is provided by the NIC device and driver. 448It also requires that ntuple filtering is enabled via ethtool. The map 449of CPU to queues is automatically deduced from the IRQ affinities 450configured for each receive queue by the driver, so no additional 451configuration should be necessary. 452 453 454Suggested Configuration 455~~~~~~~~~~~~~~~~~~~~~~~ 456 457This technique should be enabled whenever one wants to use RFS and the 458NIC supports hardware acceleration. 459 460 461XPS: Transmit Packet Steering 462============================= 463 464Transmit Packet Steering is a mechanism for intelligently selecting 465which transmit queue to use when transmitting a packet on a multi-queue 466device. This can be accomplished by recording two kinds of maps, either 467a mapping of CPU to hardware queue(s) or a mapping of receive queue(s) 468to hardware transmit queue(s). 469 4701. XPS using CPUs map 471 472The goal of this mapping is usually to assign queues 473exclusively to a subset of CPUs, where the transmit completions for 474these queues are processed on a CPU within this set. This choice 475provides two benefits. First, contention on the device queue lock is 476significantly reduced since fewer CPUs contend for the same queue 477(contention can be eliminated completely if each CPU has its own 478transmit queue). Secondly, cache miss rate on transmit completion is 479reduced, in particular for data cache lines that hold the sk_buff 480structures. 481 4822. XPS using receive queues map 483 484This mapping is used to pick transmit queue based on the receive 485queue(s) map configuration set by the administrator. A set of receive 486queues can be mapped to a set of transmit queues (many:many), although 487the common use case is a 1:1 mapping. This will enable sending packets 488on the same queue associations for transmit and receive. This is useful for 489busy polling multi-threaded workloads where there are challenges in 490associating a given CPU to a given application thread. The application 491threads are not pinned to CPUs and each thread handles packets 492received on a single queue. The receive queue number is cached in the 493socket for the connection. In this model, sending the packets on the same 494transmit queue corresponding to the associated receive queue has benefits 495in keeping the CPU overhead low. Transmit completion work is locked into 496the same queue-association that a given application is polling on. This 497avoids the overhead of triggering an interrupt on another CPU. When the 498application cleans up the packets during the busy poll, transmit completion 499may be processed along with it in the same thread context and so result in 500reduced latency. 501 502XPS is configured per transmit queue by setting a bitmap of 503CPUs/receive-queues that may use that queue to transmit. The reverse 504mapping, from CPUs to transmit queues or from receive-queues to transmit 505queues, is computed and maintained for each network device. When 506transmitting the first packet in a flow, the function get_xps_queue() is 507called to select a queue. This function uses the ID of the receive queue 508for the socket connection for a match in the receive queue-to-transmit queue 509lookup table. Alternatively, this function can also use the ID of the 510running CPU as a key into the CPU-to-queue lookup table. If the 511ID matches a single queue, that is used for transmission. If multiple 512queues match, one is selected by using the flow hash to compute an index 513into the set. When selecting the transmit queue based on receive queue(s) 514map, the transmit device is not validated against the receive device as it 515requires expensive lookup operation in the datapath. 516 517The queue chosen for transmitting a particular flow is saved in the 518corresponding socket structure for the flow (e.g. a TCP connection). 519This transmit queue is used for subsequent packets sent on the flow to 520prevent out of order (ooo) packets. The choice also amortizes the cost 521of calling get_xps_queues() over all packets in the flow. To avoid 522ooo packets, the queue for a flow can subsequently only be changed if 523skb->ooo_okay is set for a packet in the flow. This flag indicates that 524there are no outstanding packets in the flow, so the transmit queue can 525change without the risk of generating out of order packets. The 526transport layer is responsible for setting ooo_okay appropriately. TCP, 527for instance, sets the flag when all data for a connection has been 528acknowledged. 529 530XPS Configuration 531----------------- 532 533XPS is only available if the kconfig symbol CONFIG_XPS is enabled (on by 534default for SMP). If compiled in, it is driver dependent whether, and 535how, XPS is configured at device init. The mapping of CPUs/receive-queues 536to transmit queue can be inspected and configured using sysfs: 537 538For selection based on CPUs map:: 539 540 /sys/class/net/<dev>/queues/tx-<n>/xps_cpus 541 542For selection based on receive-queues map:: 543 544 /sys/class/net/<dev>/queues/tx-<n>/xps_rxqs 545 546 547Suggested Configuration 548~~~~~~~~~~~~~~~~~~~~~~~ 549 550For a network device with a single transmission queue, XPS configuration 551has no effect, since there is no choice in this case. In a multi-queue 552system, XPS is preferably configured so that each CPU maps onto one queue. 553If there are as many queues as there are CPUs in the system, then each 554queue can also map onto one CPU, resulting in exclusive pairings that 555experience no contention. If there are fewer queues than CPUs, then the 556best CPUs to share a given queue are probably those that share the cache 557with the CPU that processes transmit completions for that queue 558(transmit interrupts). 559 560For transmit queue selection based on receive queue(s), XPS has to be 561explicitly configured mapping receive-queue(s) to transmit queue(s). If the 562user configuration for receive-queue map does not apply, then the transmit 563queue is selected based on the CPUs map. 564 565 566Per TX Queue rate limitation 567============================ 568 569These are rate-limitation mechanisms implemented by HW, where currently 570a max-rate attribute is supported, by setting a Mbps value to:: 571 572 /sys/class/net/<dev>/queues/tx-<n>/tx_maxrate 573 574A value of zero means disabled, and this is the default. 575 576 577Further Information 578=================== 579RPS and RFS were introduced in kernel 2.6.35. XPS was incorporated into 5802.6.38. Original patches were submitted by Tom Herbert 581(therbert@google.com) 582 583Accelerated RFS was introduced in 2.6.35. Original patches were 584submitted by Ben Hutchings (bwh@kernel.org) 585 586Authors: 587 588- Tom Herbert (therbert@google.com) 589- Willem de Bruijn (willemb@google.com) 590