Technical Blog

Linux networking internals: RSS, RPS and RFS

At the end of part 1 we had one CPU doing all of the receive work on a single-queue NIC. It takes the interrupt, runs the NAPI poll and pushes every packet up the stack on its own. Servers have dozens of cores these days and most NICs have several queues, so you'd want more of those cores helping out. The question is which core should get which packet.

Why not spread packets randomly?#

Two diagrams of a NIC feeding several CPUs through NAPI: “Which CPU?” and “Random?”, with one overloaded CPU highlighted in red
Which CPU gets which packet? If you pick at random, one busy core (red) can hold up part of a flow.

More cores do help, especially for latency-sensitive traffic where you don't want packets waiting in a queue. You can't just hand packets to random CPUs though, because packet order matters.

Say the packets of one TCP connection get spread over four cores and one of those cores is busy. Whatever landed on that core comes out late and reaches the socket out of order. TCP takes that as possible loss, so you get duplicate ACKs, maybe some retransmits, and the sender slows down. On top of that, the application can't use any data after the late packet until it shows up.

So you give CPUs flows instead of packets. A flow is roughly "same source, same destination", and in the kernel it's a hash of the source and destination IP addresses and ports. Every packet of a flow goes to the same core, so the flow stays in order even when that core is busy.

RSS#

Receive-side scaling (RSS) does this in hardware, on a NIC with multiple queues. The NIC hashes some header fields of every packet (usually with a Toeplitz hash), uses the low bits of the hash to look up an indirection table, and the table tells it which RX queue to put the packet on. Each queue has its own ring and its own interrupt, and you can pin each interrupt to a different CPU.

There's no learning involved. The same tuple always gives the same hash, so a flow always lands on the same queue. Most of the settings are in ethtool:

ethtool -l eth0                          # how many queues the NIC has and uses
ethtool -L eth0 combined 8
ethtool -x eth0                          # indirection table and hash key
ethtool -X eth0 equal 8                  # spread evenly over 8 queues
ethtool -n eth0 rx-flow-hash udp4        # which fields get hashed for UDP
ethtool -N eth0 rx-flow-hash udp4 sdfn   # include the ports too

grep eth0 /proc/interrupts               # one IRQ per queue
cat /proc/irq/<N>/smp_affinity_list      # which CPUs handle queue N

If you pin interrupts by hand, keep irqbalance away from them, otherwise it will move them around on you.

RPS#

One CPU receives the IRQ and computes the flow hash, then assigns the packet to one of several CPUs for processing
RPS: the CPU that takes the interrupt works out the flow and hands the packet to another CPU.

A lot of NICs don't have RSS. The one in your laptop probably has a single queue, and so do many virtual NICs. Receive packet steering (RPS) is the software version of RSS. The CPU that gets the interrupt computes the hash (or uses one the NIC already put in the descriptor), looks up which CPU owns that flow and queues the packet on that CPU's backlog. The protocol processing then happens on the other CPU.

RPS is off by default. You turn it on for each RX queue by writing a CPU mask:

# CPUs 0-3 handle protocol processing for packets arriving on rx-0
echo f > /sys/class/net/eth0/queues/rx-0/rps_cpus

# the per-CPU backlog RPS queues to; overflows show in column 2 of /proc/net/softnet_stat
sysctl net.core.netdev_max_backlog

It isn't free. You pay for the hashing and for the interrupts between processors. But on a single-queue NIC it turns a one-core bottleneck into something many cores can share. If RSS already gives every CPU its own queue, the kernel docs recommend leaving RPS off.

RFS#

RFS checks where the consumer application runs and assigns the packet to the CPU that the application is running on
RFS steers a flow to the CPU where the application reading it is running.

RSS and RPS keep a flow on one CPU, but the hash decides which CPU that is, and that has nothing to do with where your application is running. If Apache is on core 0 and its packets are processed on core 5, the data is warm in core 5's cache and core 0 gets the cache misses.

Receive flow steering (RFS) builds on RPS to fix this. When the application calls recvmsg() or sendmsg(), the kernel notes which CPU it's on, and later packets for that flow get sent to that CPU. The data then ends up in the cache of the core that's going to read it.

Applications move between cores, though, and moving a flow right away could reorder packets that are still queued on the old core. So RFS also keeps a table for each queue and only moves a flow after the old CPU has finished with its packets. The kernel docs suggest starting with these sizes:

sysctl -w net.core.rps_sock_flow_entries=32768

# per queue: rps_sock_flow_entries / number of RX queues (16 here)
for q in /sys/class/net/eth0/queues/rx-*; do echo 2048 > $q/rps_flow_cnt; done

There's also accelerated RFS, where the driver programs the NIC's flow filters so packets land on the right queue in the first place (it needs NIC support and ethtool -K eth0 ntuple on), and XPS, which does a similar mapping for transmit (/sys/class/net/eth0/queues/tx-*/xps_cpus). I won't go into either of them here.

All of this works per flow, so one big flow still lands on a single queue and a single core, however many cores the box has. That's why you run iperf3 with -P and several parallel streams when you want to fill a fat pipe.

In the next part we'll look at the NIC driver, sk_buff and qdiscs, and then follow a packet all the way through the stack.