Technical Blog

Linux networking internals: interrupts, descriptor rings, DMA and NAPI

Sending and receiving packets on Linux mostly just works, so most of us never look at how it's done. That changes once you're chasing packet drops or latency on a busy server. Almost every step between the NIC and your application has a setting you can tune, and you need to know which step your problem is coming from. This first part covers the bottom of the receive path, from the NIC up to the point where the kernel picks the packet up.

Mohamed Elsakhawy presenting the title slide of “A deep dive into Linux Networking” at SCaLE 22x
Title slide: A deep dive into Linux Networking.

If you want to see how big the full picture is, have a look at Adrien Mahieux's Saruspete/LinuxNetworking project. It maps every kernel function a received packet goes through, and that's only the receive side.

The stack#

The Linux network stack: physical, link, IP, TCP/UDP, socket and application layers, with netfilter hooks alongside
The Linux network stack, with netfilter hooking in from the side.

The Linux network stack looks a lot like the OSI model. The NIC is at the bottom. On top of it is the link layer, where the driver deals with frames and MAC addresses, then IP, then TCP or UDP, and then the socket layer, which your application uses through system calls. Packets you send go down the stack and packets you receive go up.

Netfilter sits to the side. It's what iptables, nftables and firewalld are built on, and it hooks into the path at a few fixed points so packets can be filtered as they pass.

User space and kernel space#

Your application runs in user space. A process there can only touch its own memory, and it has no way to program a device or tell it where to write. NIC drivers are kernel modules, so they run in kernel space, with access to all of memory and to the hardware. That's how a driver can set the NIC's registers, and setting registers is how you tell a device where to read and write. It also means every packet you receive has to cross from kernel space into user space before your application can see it. In part 4 we'll move the NIC itself over to user space.

IRQs and softirqs#

When packets arrive, the NIC raises an interrupt (IRQ) so the OS comes and handles them. The driver registers an interrupt service routine (ISR) for that, and the kernel runs it whenever the interrupt fires.

An interrupt stops the CPU in the middle of whatever it was doing, and that work has to wait until the handler is finished. A busy NIC can receive millions of packets a second, and pushing every one of them up the stack is a lot of work, so you can't do it all inside the interrupt handler.

So the work is split in two. The hardware interrupt handler does as little as it can. It acknowledges the device and schedules the rest of the work, which runs as a softirq. Softirqs are raised by the kernel itself and run with interrupts enabled, usually right after the handler returns. If there's more softirq work than can be done there, it's handed to a per-CPU kernel thread called ksoftirqd/N. You can see all of this on any Linux box:

# hardware interrupts per CPU (look for your NIC's queues)
grep -E 'CPU|eth0' /proc/interrupts

# softirqs per CPU; NET_RX is receive, NET_TX is transmit
grep -E 'CPU|NET_RX|NET_TX' /proc/softirqs

ps -eo pid,comm | grep ksoftirqd

If one ksoftirqd is at 100% in top while the other cores sit idle, your receive work isn't being spread across CPUs.

RX and TX rings#

Kernel-space structures between the NIC and the stack: packet buffers, TX and RX rings, IRQ handler, poll list and softirq
The pieces in kernel memory: packet buffers, the TX/RX rings, the IRQ handler, the poll list and the softirq.

When a packet comes in, the NIC has to know where in memory to put it. That's what the RX ring is for, and the TX ring does the same job for sending. Both rings live in the host's RAM, not on the NIC. The driver allocates them and writes their addresses into the NIC's registers.

Each slot in a ring holds a descriptor, which is why they're also called descriptor rings. A descriptor doesn't hold the packet. It points to a packet buffer somewhere else in memory, and that's where the data goes. On receive, the NIC takes the next free descriptor, writes the frame into its buffer and marks the descriptor as done. On transmit, the driver puts the packet in a buffer, fills in a TX descriptor and tells the NIC to send it.

A ring has a fixed number of slots. If the kernel doesn't hand buffers back fast enough, the NIC runs out of free descriptors and starts dropping frames. Most of the time the actual bottleneck is further up the stack, but this is where the drops get counted. Every driver names these counters a little differently:

ethtool -g eth0                  # current and maximum ring sizes
ethtool -G eth0 rx 4096          # bigger ring: absorbs bursts, costs memory and some latency
ethtool -S eth0 | grep -iE 'miss|no_buf|fifo|drop'

DMA#

The NIC writing a packet into packet buffers in kernel memory via DMA, after fetching an RX descriptor
The NIC fetches an RX descriptor, then DMAs the packet into the buffer it points to.

DMA lets the NIC write into those packet buffers directly, without the CPU copying anything. Without it, the CPU would have to stop for every packet and copy the bytes out of the NIC's small on-board memory. With DMA, the frame lands in the NIC's FIFO, the NIC fetches the next RX descriptor, writes the packet into the buffer that descriptor points to, updates the descriptor's status and raises an interrupt. Up to this point the CPU hasn't touched the packet at all.

A device that can write straight into memory is a security risk, so the kernel controls which memory the NIC is allowed to write to. That's fine as long as the kernel owns the NIC. Once you give the NIC to a user-space program you need the IOMMU, which comes up again in part 4.

NAPI#

Two diagrams: before NAPI, every packet from the wire triggers an IRQ to the CPU; with NAPI, an IRQ arrives for the first packet and NAPI then polls the NIC for new packets
Top: an interrupt for every packet. Bottom: NAPI takes the first interrupt, then polls.

Even with the work split, one interrupt per packet is still too many. Packets also tend to come in bunches. If one packet of a YouTube video just arrived, there are more right behind it. NAPI (short for "new API", although it's been the normal way Linux receives packets for years) uses an interrupt to get started and then switches to polling:

  1. The first packet raises an interrupt as usual.
  2. The driver's handler masks further interrupts from that queue and calls napi_schedule(), which puts the queue on the CPU's poll list and raises the NET_RX softirq.
  3. The softirq calls the driver's poll function, which takes completed descriptors off the RX ring and passes the packets up the stack. Packets that arrive in the meantime are picked up by the next poll, without any new interrupts.
  4. When a poll finds the ring empty, the driver calls napi_complete_done() and turns interrupts back on.

The polling has limits so a flood of traffic can't take over the CPU. Each poll handles a small batch, usually 64 packets, and each softirq run stops after net.core.netdev_budget packets or netdev_budget_usecs, whichever comes first. If it has to stop with work still left, the kernel counts a time squeeze.

sysctl net.core.netdev_budget net.core.netdev_budget_usecs

# one row per CPU, in hex: processed, dropped, time_squeeze, ...
cat /proc/net/softnet_stat

If the third column keeps going up, that CPU isn't keeping up. You can raise the budget, but spreading the work over more cores usually helps more. The NIC can also help with interrupt coalescing, where it waits a few microseconds or a few packets before it interrupts. You get fewer interrupts and pay for it with a bit of latency. ethtool -c eth0 shows the current settings and ethtool -C eth0 rx-usecs 50 changes them.

On a NIC with a single queue, the CPU that takes the interrupt also runs the poll and pushes every packet up the stack, so one core ends up doing all the work. In the next part we'll look at how to spread that over more cores.