blob: 59f4db2a0c85c02df4f6cee3176ceb173333cac1 [file] [log] [blame]
Willem de Bruijn56c07272011-08-09 04:20:48 +00001Scaling in the Linux Networking Stack
2
3
4Introduction
5============
6
7This document describes a set of complementary techniques in the Linux
8networking stack to increase parallelism and improve performance for
9multi-processor systems.
10
11The following technologies are described:
12
13 RSS: Receive Side Scaling
14 RPS: Receive Packet Steering
15 RFS: Receive Flow Steering
16 Accelerated Receive Flow Steering
17 XPS: Transmit Packet Steering
18
19
20RSS: Receive Side Scaling
21=========================
22
23Contemporary NICs support multiple receive and transmit descriptor queues
24(multi-queue). On reception, a NIC can send different packets to different
25queues to distribute processing among CPUs. The NIC distributes packets by
26applying a filter to each packet that assigns it to one of a small number
27of logical flows. Packets for each flow are steered to a separate receive
28queue, which in turn can be processed by separate CPUs. This mechanism is
29generally known as “Receive-side Scaling” (RSS). The goal of RSS and
Benjamin Poirier186c6bbc2011-10-04 04:00:30 +000030the other scaling techniques is to increase performance uniformly.
Willem de Bruijn56c07272011-08-09 04:20:48 +000031Multi-queue distribution can also be used for traffic prioritization, but
32that is not the focus of these techniques.
33
34The filter used in RSS is typically a hash function over the network
35and/or transport layer headers-- for example, a 4-tuple hash over
36IP addresses and TCP ports of a packet. The most common hardware
37implementation of RSS uses a 128-entry indirection table where each entry
38stores a queue number. The receive queue for a packet is determined
39by masking out the low order seven bits of the computed hash for the
40packet (usually a Toeplitz hash), taking this number as a key into the
41indirection table and reading the corresponding value.
42
43Some advanced NICs allow steering packets to queues based on
44programmable filters. For example, webserver bound TCP port 80 packets
45can be directed to their own receive queue. Such “n-tuple” filters can
46be configured from ethtool (--config-ntuple).
47
48==== RSS Configuration
49
50The driver for a multi-queue capable NIC typically provides a kernel
51module parameter for specifying the number of hardware queues to
52configure. In the bnx2x driver, for instance, this parameter is called
53num_queues. A typical RSS configuration would be to have one receive queue
54for each CPU if the device supports enough queues, or otherwise at least
Willem de Bruijn320f24e2011-08-11 14:41:48 +000055one for each memory domain, where a memory domain is a set of CPUs that
56share a particular memory level (L1, L2, NUMA node, etc.).
Willem de Bruijn56c07272011-08-09 04:20:48 +000057
58The indirection table of an RSS device, which resolves a queue by masked
59hash, is usually programmed by the driver at initialization. The
60default mapping is to distribute the queues evenly in the table, but the
61indirection table can be retrieved and modified at runtime using ethtool
62commands (--show-rxfh-indir and --set-rxfh-indir). Modifying the
63indirection table could be done to give different queues different
64relative weights.
65
66== RSS IRQ Configuration
67
68Each receive queue has a separate IRQ associated with it. The NIC triggers
69this to notify a CPU when new packets arrive on the given queue. The
70signaling path for PCIe devices uses message signaled interrupts (MSI-X),
71that can route each interrupt to a particular CPU. The active mapping
72of queues to IRQs can be determined from /proc/interrupts. By default,
73an IRQ may be handled on any CPU. Because a non-negligible part of packet
74processing takes place in receive interrupt handling, it is advantageous
75to spread receive interrupts between CPUs. To manually adjust the IRQ
Paul Bolle395cf962011-08-15 02:02:26 +020076affinity of each interrupt see Documentation/IRQ-affinity.txt. Some systems
Willem de Bruijn56c07272011-08-09 04:20:48 +000077will be running irqbalance, a daemon that dynamically optimizes IRQ
78assignments and as a result may override any manual settings.
79
80== Suggested Configuration
81
82RSS should be enabled when latency is a concern or whenever receive
83interrupt processing forms a bottleneck. Spreading load between CPUs
84decreases queue length. For low latency networking, the optimal setting
85is to allocate as many queues as there are CPUs in the system (or the
Willem de Bruijn320f24e2011-08-11 14:41:48 +000086NIC maximum, if lower). The most efficient high-rate configuration
Willem de Bruijn56c07272011-08-09 04:20:48 +000087is likely the one with the smallest number of receive queues where no
Willem de Bruijn320f24e2011-08-11 14:41:48 +000088receive queue overflows due to a saturated CPU, because in default
89mode with interrupt coalescing enabled, the aggregate number of
90interrupts (and thus work) grows with each additional queue.
91
92Per-cpu load can be observed using the mpstat utility, but note that on
93processors with hyperthreading (HT), each hyperthread is represented as
94a separate CPU. For interrupt handling, HT has shown no benefit in
95initial tests, so limit the number of queues to the number of CPU cores
96in the system.
Willem de Bruijn56c07272011-08-09 04:20:48 +000097
98
99RPS: Receive Packet Steering
100============================
101
102Receive Packet Steering (RPS) is logically a software implementation of
103RSS. Being in software, it is necessarily called later in the datapath.
104Whereas RSS selects the queue and hence CPU that will run the hardware
105interrupt handler, RPS selects the CPU to perform protocol processing
106above the interrupt handler. This is accomplished by placing the packet
107on the desired CPU’s backlog queue and waking up the CPU for processing.
108RPS has some advantages over RSS: 1) it can be used with any NIC,
1092) software filters can easily be added to hash over new protocols,
1103) it does not increase hardware device interrupt rate (although it does
111introduce inter-processor interrupts (IPIs)).
112
113RPS is called during bottom half of the receive interrupt handler, when
114a driver sends a packet up the network stack with netif_rx() or
115netif_receive_skb(). These call the get_rps_cpu() function, which
116selects the queue that should process a packet.
117
118The first step in determining the target CPU for RPS is to calculate a
119flow hash over the packet’s addresses or ports (2-tuple or 4-tuple hash
120depending on the protocol). This serves as a consistent hash of the
121associated flow of the packet. The hash is either provided by hardware
122or will be computed in the stack. Capable hardware can pass the hash in
123the receive descriptor for the packet; this would usually be the same
124hash used for RSS (e.g. computed Toeplitz hash). The hash is saved in
125skb->rx_hash and can be used elsewhere in the stack as a hash of the
126packet’s flow.
127
128Each receive hardware queue has an associated list of CPUs to which
129RPS may enqueue packets for processing. For each received packet,
130an index into the list is computed from the flow hash modulo the size
131of the list. The indexed CPU is the target for processing the packet,
132and the packet is queued to the tail of that CPU’s backlog queue. At
133the end of the bottom half routine, IPIs are sent to any CPUs for which
134packets have been queued to their backlog queue. The IPI wakes backlog
135processing on the remote CPU, and any queued packets are then processed
136up the networking stack.
137
138==== RPS Configuration
139
140RPS requires a kernel compiled with the CONFIG_RPS kconfig symbol (on
141by default for SMP). Even when compiled in, RPS remains disabled until
142explicitly configured. The list of CPUs to which RPS may forward traffic
143can be configured for each receive queue using a sysfs file entry:
144
145 /sys/class/net/<dev>/queues/rx-<n>/rps_cpus
146
147This file implements a bitmap of CPUs. RPS is disabled when it is zero
148(the default), in which case packets are processed on the interrupting
149CPU. Documentation/IRQ-affinity.txt explains how CPUs are assigned to
150the bitmap.
151
152== Suggested Configuration
153
154For a single queue device, a typical RPS configuration would be to set
Willem de Bruijn320f24e2011-08-11 14:41:48 +0000155the rps_cpus to the CPUs in the same memory domain of the interrupting
Willem de Bruijn56c07272011-08-09 04:20:48 +0000156CPU. If NUMA locality is not an issue, this could also be all CPUs in
157the system. At high interrupt rate, it might be wise to exclude the
158interrupting CPU from the map since that already performs much work.
159
160For a multi-queue system, if RSS is configured so that a hardware
161receive queue is mapped to each CPU, then RPS is probably redundant
162and unnecessary. If there are fewer hardware queues than CPUs, then
163RPS might be beneficial if the rps_cpus for each queue are the ones that
Willem de Bruijn320f24e2011-08-11 14:41:48 +0000164share the same memory domain as the interrupting CPU for that queue.
Willem de Bruijn56c07272011-08-09 04:20:48 +0000165
Willem de Bruijn191cb1f2013-05-22 07:54:40 +0000166==== RPS Flow Limit
167
168RPS scales kernel receive processing across CPUs without introducing
169reordering. The trade-off to sending all packets from the same flow
170to the same CPU is CPU load imbalance if flows vary in packet rate.
171In the extreme case a single flow dominates traffic. Especially on
172common server workloads with many concurrent connections, such
173behavior indicates a problem such as a misconfiguration or spoofed
174source Denial of Service attack.
175
176Flow Limit is an optional RPS feature that prioritizes small flows
177during CPU contention by dropping packets from large flows slightly
178ahead of those from small flows. It is active only when an RPS or RFS
179destination CPU approaches saturation. Once a CPU's input packet
180queue exceeds half the maximum queue length (as set by sysctl
181net.core.netdev_max_backlog), the kernel starts a per-flow packet
182count over the last 256 packets. If a flow exceeds a set ratio (by
183default, half) of these packets when a new packet arrives, then the
184new packet is dropped. Packets from other flows are still only
185dropped once the input packet queue reaches netdev_max_backlog.
186No packets are dropped when the input packet queue length is below
187the threshold, so flow limit does not sever connections outright:
188even large flows maintain connectivity.
189
190== Interface
191
192Flow limit is compiled in by default (CONFIG_NET_FLOW_LIMIT), but not
193turned on. It is implemented for each CPU independently (to avoid lock
194and cache contention) and toggled per CPU by setting the relevant bit
195in sysctl net.core.flow_limit_cpu_bitmap. It exposes the same CPU
196bitmap interface as rps_cpus (see above) when called from procfs:
197
198 /proc/sys/net/core/flow_limit_cpu_bitmap
199
200Per-flow rate is calculated by hashing each packet into a hashtable
201bucket and incrementing a per-bucket counter. The hash function is
202the same that selects a CPU in RPS, but as the number of buckets can
203be much larger than the number of CPUs, flow limit has finer-grained
204identification of large flows and fewer false positives. The default
205table has 4096 buckets. This value can be modified through sysctl
206
207 net.core.flow_limit_table_len
208
209The value is only consulted when a new table is allocated. Modifying
210it does not update active tables.
211
212== Suggested Configuration
213
214Flow limit is useful on systems with many concurrent connections,
215where a single connection taking up 50% of a CPU indicates a problem.
216In such environments, enable the feature on all CPUs that handle
217network rx interrupts (as set in /proc/irq/N/smp_affinity).
218
219The feature depends on the input packet queue length to exceed
220the flow limit threshold (50%) + the flow history length (256).
221Setting net.core.netdev_max_backlog to either 1000 or 10000
222performed well in experiments.
223
Willem de Bruijn56c07272011-08-09 04:20:48 +0000224
225RFS: Receive Flow Steering
226==========================
227
228While RPS steers packets solely based on hash, and thus generally
229provides good load distribution, it does not take into account
230application locality. This is accomplished by Receive Flow Steering
231(RFS). The goal of RFS is to increase datacache hitrate by steering
232kernel processing of packets to the CPU where the application thread
233consuming the packet is running. RFS relies on the same RPS mechanisms
234to enqueue packets onto the backlog of another CPU and to wake up that
235CPU.
236
237In RFS, packets are not forwarded directly by the value of their hash,
238but the hash is used as index into a flow lookup table. This table maps
239flows to the CPUs where those flows are being processed. The flow hash
240(see RPS section above) is used to calculate the index into this table.
241The CPU recorded in each entry is the one which last processed the flow.
242If an entry does not hold a valid CPU, then packets mapped to that entry
243are steered using plain RPS. Multiple table entries may point to the
244same CPU. Indeed, with many flows and few CPUs, it is very likely that
245a single application thread handles flows with many different flow hashes.
246
Benjamin Poirier186c6bbc2011-10-04 04:00:30 +0000247rps_sock_flow_table is a global flow table that contains the *desired* CPU
248for flows: the CPU that is currently processing the flow in userspace.
249Each table value is a CPU index that is updated during calls to recvmsg
250and sendmsg (specifically, inet_recvmsg(), inet_sendmsg(), inet_sendpage()
Willem de Bruijn56c07272011-08-09 04:20:48 +0000251and tcp_splice_read()).
252
253When the scheduler moves a thread to a new CPU while it has outstanding
254receive packets on the old CPU, packets may arrive out of order. To
255avoid this, RFS uses a second flow table to track outstanding packets
256for each flow: rps_dev_flow_table is a table specific to each hardware
257receive queue of each device. Each table value stores a CPU index and a
258counter. The CPU index represents the *current* CPU onto which packets
259for this flow are enqueued for further kernel processing. Ideally, kernel
260and userspace processing occur on the same CPU, and hence the CPU index
261in both tables is identical. This is likely false if the scheduler has
262recently migrated a userspace thread while the kernel still has packets
263enqueued for kernel processing on the old CPU.
264
265The counter in rps_dev_flow_table values records the length of the current
266CPU's backlog when a packet in this flow was last enqueued. Each backlog
267queue has a head counter that is incremented on dequeue. A tail counter
268is computed as head counter + queue length. In other words, the counter
Shan Wei08f4fc92011-12-19 16:34:15 +0000269in rps_dev_flow[i] records the last element in flow i that has
Willem de Bruijn56c07272011-08-09 04:20:48 +0000270been enqueued onto the currently designated CPU for flow i (of course,
271entry i is actually selected by hash and multiple flows may hash to the
272same entry i).
273
274And now the trick for avoiding out of order packets: when selecting the
275CPU for packet processing (from get_rps_cpu()) the rps_sock_flow table
276and the rps_dev_flow table of the queue that the packet was received on
277are compared. If the desired CPU for the flow (found in the
278rps_sock_flow table) matches the current CPU (found in the rps_dev_flow
279table), the packet is enqueued onto that CPU’s backlog. If they differ,
280the current CPU is updated to match the desired CPU if one of the
281following is true:
282
283- The current CPU's queue head counter >= the recorded tail counter
284 value in rps_dev_flow[i]
Eric Dumazeta31196b2015-04-25 09:35:24 -0700285- The current CPU is unset (>= nr_cpu_ids)
Willem de Bruijn56c07272011-08-09 04:20:48 +0000286- The current CPU is offline
287
288After this check, the packet is sent to the (possibly updated) current
289CPU. These rules aim to ensure that a flow only moves to a new CPU when
290there are no packets outstanding on the old CPU, as the outstanding
291packets could arrive later than those about to be processed on the new
292CPU.
293
294==== RFS Configuration
295
Shan Wei08f4fc92011-12-19 16:34:15 +0000296RFS is only available if the kconfig symbol CONFIG_RPS is enabled (on
Willem de Bruijn56c07272011-08-09 04:20:48 +0000297by default for SMP). The functionality remains disabled until explicitly
298configured. The number of entries in the global flow table is set through:
299
300 /proc/sys/net/core/rps_sock_flow_entries
301
302The number of entries in the per-queue flow table are set through:
303
Jason Wange451e612011-09-27 13:26:27 -0400304 /sys/class/net/<dev>/queues/rx-<n>/rps_flow_cnt
Willem de Bruijn56c07272011-08-09 04:20:48 +0000305
306== Suggested Configuration
307
308Both of these need to be set before RFS is enabled for a receive queue.
309Values for both are rounded up to the nearest power of two. The
310suggested flow count depends on the expected number of active connections
311at any given time, which may be significantly less than the number of open
312connections. We have found that a value of 32768 for rps_sock_flow_entries
313works fairly well on a moderately loaded server.
314
315For a single queue device, the rps_flow_cnt value for the single queue
316would normally be configured to the same value as rps_sock_flow_entries.
317For a multi-queue device, the rps_flow_cnt for each queue might be
318configured as rps_sock_flow_entries / N, where N is the number of
Shan Wei08f4fc92011-12-19 16:34:15 +0000319queues. So for instance, if rps_sock_flow_entries is set to 32768 and there
Willem de Bruijn56c07272011-08-09 04:20:48 +0000320are 16 configured receive queues, rps_flow_cnt for each queue might be
321configured as 2048.
322
323
324Accelerated RFS
325===============
326
327Accelerated RFS is to RFS what RSS is to RPS: a hardware-accelerated load
328balancing mechanism that uses soft state to steer flows based on where
329the application thread consuming the packets of each flow is running.
330Accelerated RFS should perform better than RFS since packets are sent
331directly to a CPU local to the thread consuming the data. The target CPU
332will either be the same CPU where the application runs, or at least a CPU
333which is local to the application thread’s CPU in the cache hierarchy.
334
335To enable accelerated RFS, the networking stack calls the
336ndo_rx_flow_steer driver function to communicate the desired hardware
337queue for packets matching a particular flow. The network stack
338automatically calls this function every time a flow entry in
339rps_dev_flow_table is updated. The driver in turn uses a device specific
340method to program the NIC to steer the packets.
341
342The hardware queue for a flow is derived from the CPU recorded in
343rps_dev_flow_table. The stack consults a CPU to hardware queue map which
344is maintained by the NIC driver. This is an auto-generated reverse map of
345the IRQ affinity table shown by /proc/interrupts. Drivers can use
346functions in the cpu_rmap (“CPU affinity reverse map”) kernel library
347to populate the map. For each CPU, the corresponding queue in the map is
348set to be one whose processing CPU is closest in cache locality.
349
350==== Accelerated RFS Configuration
351
352Accelerated RFS is only available if the kernel is compiled with
353CONFIG_RFS_ACCEL and support is provided by the NIC device and driver.
354It also requires that ntuple filtering is enabled via ethtool. The map
355of CPU to queues is automatically deduced from the IRQ affinities
356configured for each receive queue by the driver, so no additional
357configuration should be necessary.
358
359== Suggested Configuration
360
361This technique should be enabled whenever one wants to use RFS and the
362NIC supports hardware acceleration.
363
364XPS: Transmit Packet Steering
365=============================
366
367Transmit Packet Steering is a mechanism for intelligently selecting
368which transmit queue to use when transmitting a packet on a multi-queue
369device. To accomplish this, a mapping from CPU to hardware queue(s) is
370recorded. The goal of this mapping is usually to assign queues
371exclusively to a subset of CPUs, where the transmit completions for
372these queues are processed on a CPU within this set. This choice
373provides two benefits. First, contention on the device queue lock is
374significantly reduced since fewer CPUs contend for the same queue
375(contention can be eliminated completely if each CPU has its own
376transmit queue). Secondly, cache miss rate on transmit completion is
377reduced, in particular for data cache lines that hold the sk_buff
378structures.
379
380XPS is configured per transmit queue by setting a bitmap of CPUs that
381may use that queue to transmit. The reverse mapping, from CPUs to
382transmit queues, is computed and maintained for each network device.
383When transmitting the first packet in a flow, the function
384get_xps_queue() is called to select a queue. This function uses the ID
385of the running CPU as a key into the CPU-to-queue lookup table. If the
386ID matches a single queue, that is used for transmission. If multiple
387queues match, one is selected by using the flow hash to compute an index
388into the set.
389
390The queue chosen for transmitting a particular flow is saved in the
391corresponding socket structure for the flow (e.g. a TCP connection).
392This transmit queue is used for subsequent packets sent on the flow to
393prevent out of order (ooo) packets. The choice also amortizes the cost
Willem de Bruijn320f24e2011-08-11 14:41:48 +0000394of calling get_xps_queues() over all packets in the flow. To avoid
Willem de Bruijn56c07272011-08-09 04:20:48 +0000395ooo packets, the queue for a flow can subsequently only be changed if
396skb->ooo_okay is set for a packet in the flow. This flag indicates that
397there are no outstanding packets in the flow, so the transmit queue can
398change without the risk of generating out of order packets. The
399transport layer is responsible for setting ooo_okay appropriately. TCP,
400for instance, sets the flag when all data for a connection has been
401acknowledged.
402
403==== XPS Configuration
404
405XPS is only available if the kconfig symbol CONFIG_XPS is enabled (on by
406default for SMP). The functionality remains disabled until explicitly
407configured. To enable XPS, the bitmap of CPUs that may use a transmit
408queue is configured using the sysfs file entry:
409
410/sys/class/net/<dev>/queues/tx-<n>/xps_cpus
411
412== Suggested Configuration
413
414For a network device with a single transmission queue, XPS configuration
415has no effect, since there is no choice in this case. In a multi-queue
416system, XPS is preferably configured so that each CPU maps onto one queue.
417If there are as many queues as there are CPUs in the system, then each
418queue can also map onto one CPU, resulting in exclusive pairings that
419experience no contention. If there are fewer queues than CPUs, then the
420best CPUs to share a given queue are probably those that share the cache
421with the CPU that processes transmit completions for that queue
422(transmit interrupts).
423
John Fastabend822b3b22015-03-18 14:57:33 +0200424Per TX Queue rate limitation:
425=============================
426
427These are rate-limitation mechanisms implemented by HW, where currently
428a max-rate attribute is supported, by setting a Mbps value to
429
430/sys/class/net/<dev>/queues/tx-<n>/tx_maxrate
431
432A value of zero means disabled, and this is the default.
Willem de Bruijn56c07272011-08-09 04:20:48 +0000433
434Further Information
435===================
436RPS and RFS were introduced in kernel 2.6.35. XPS was incorporated into
4372.6.38. Original patches were submitted by Tom Herbert
438(therbert@google.com)
439
440Accelerated RFS was introduced in 2.6.35. Original patches were
Ben Hutchingsc06cbcb2014-04-22 17:29:42 +0100441submitted by Ben Hutchings (bwh@kernel.org)
Willem de Bruijn56c07272011-08-09 04:20:48 +0000442
443Authors:
444Tom Herbert (therbert@google.com)
445Willem de Bruijn (willemb@google.com)