Linux Internals
Linux NAPI Explained: Interrupts, Polling, Budgets and Busy Polling
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيA modern network card can deliver millions of packets per second. If every one of them triggered its own interrupt, the CPU would spend all its time entering and leaving interrupt handlers and never get to the actual work, a failure mode known as receive livelock. Linux solved this two decades ago with NAPI (originally "New API"), a receive model that uses interrupts when traffic is light and switches to polling when it is heavy. Every major driver uses it today, including the virtio-net driver behind most VPS network interfaces, so understanding NAPI is the foundation for any network performance work.
The problem with pure interrupts
With a classic interrupt-per-packet design, cost grows linearly with packet rate. Each interrupt flushes pipelines, pollutes caches and preempts whatever was running. Beyond a certain rate, the system processes interrupts but drops the packets they announced because there is no time left to push them up the stack. Pure polling has the opposite problem: it burns CPU constantly even when the link is idle.
How NAPI works
NAPI combines both. The cycle for a receive queue looks like this:
- A packet arrives and the NIC raises an interrupt.
- The driver's interrupt handler disables further receive interrupts for that queue and calls
napi_schedule(), which adds the queue'snapi_structto the current CPU's poll list and raises theNET_RXsoftirq. - The softirq runs
net_rx_action(), which calls the driver'spoll()function for each scheduled queue. poll()pulls up to a fixed number of packets (the weight, usually 64) from the receive ring, builds socket buffers and passes them up the stack, often through GRO, which merges segments of the same flow.- If the ring is drained before the weight is used up, the driver calls
napi_complete_done()and re-enables interrupts. If not, the queue stays on the poll list and is polled again.
Under light load, NAPI behaves like interrupt-driven I/O with low latency. Under heavy load, interrupts stay off and the kernel keeps polling, so the cost per packet falls as the rate rises. The softirq side of this story is covered in our article on softirqs and ksoftirqd.
The budget: netdev_budget and netdev_budget_usecs
One net_rx_action() run cannot poll forever, or it would starve user space. Two sysctls bound it:
net.core.netdev_budget(default 300): the total number of packets processed across all queues in one softirq run.net.core.netdev_budget_usecs(default 2000): the maximum time in microseconds for one run.
When either limit is hit while work remains, the kernel increments the per-CPU time_squeeze counter, the third column of /proc/net/softnet_stat, and re-raises the softirq, which may then move to ksoftirqd.
sysctl net.core.netdev_budget net.core.netdev_budget_usecs
awk '{print "cpu" NR-1, "processed=0x"$1, "dropped=0x"$2, "squeeze=0x"$3}' /proc/net/softnet_statIf time_squeeze grows steadily during peaks and you see drops, a moderate increase can help:
sudo sysctl -w net.core.netdev_budget=600
sudo sysctl -w net.core.netdev_budget_usecs=4000Persist changes in a file such as /etc/sysctl.d/90-net.conf. Larger budgets favour throughput at the expense of scheduling latency for other tasks on the same CPU, so change them in small steps and measure.
Interrupt coalescing
NAPI decides when to poll; coalescing decides how eagerly the NIC interrupts. Most physical NICs, and many virtual ones, let you delay the interrupt until a number of packets arrive or a timer expires:
ethtool -c eth0
sudo ethtool -C eth0 rx-usecs 50 rx-frames 32Higher values mean fewer interrupts and better throughput; lower values mean lower latency. Many drivers also support adaptive coalescing (adaptive-rx on). Support varies by driver, and ethtool will report an error for unsupported fields.
Busy polling
For latency-critical applications, NAPI can be driven directly from the application's system call. With busy polling, a thread waiting in recvmsg(), poll() or epoll_wait() spins on the device queue for a short time instead of sleeping until a softirq delivers data.
sudo sysctl -w net.core.busy_read=50 # microseconds, for blocking reads
sudo sysctl -w net.core.busy_poll=50 # microseconds, for poll/select/epollAn application can opt in per socket with the SO_BUSY_POLL socket option. Busy polling trades CPU for latency: it can shave tens of microseconds off each request, but a spinning core does nothing else. It makes sense on dedicated cores for trading engines or game servers, rarely for general web hosting.
Related newer knobs include napi_defer_hard_irqs and gro_flush_timeout under /sys/class/net/eth0/, which let the kernel keep interrupts masked across several empty polls, and threaded NAPI, enabled with echo 1 > /sys/class/net/eth0/threaded, which moves polling into per-queue kernel threads named napi/eth0-<id> that the scheduler can place and prioritise like any task.
How this affects your VPS
On a typical VPS, NAPI runs inside the virtio-net driver, and the host's vhost backend feeds the guest's rings. The number of queues limits parallelism:
ethtool -i eth0 # driver name, usually virtio_net
ethtool -l eth0 # channels
ethtool -S eth0 | head -40
grep -E 'virtio|eth0' /proc/interruptsIf your plan has several vCPUs but only one queue is active, try sudo ethtool -L eth0 combined 4 when the maximum allows it, so receive processing can spread. Watch /proc/net/softnet_stat and mpstat -P ALL 1 during load to confirm work is distributed. Tuning cannot create CPU that is not there, though: if softirq time saturates every vCPU, a larger plan from the pricing page is the honest answer. Deploying a fresh instance to benchmark is quick from the deploy page.
FAQ
Does NAPI add latency?
Very little in normal conditions. At low packet rates the first packet still triggers an interrupt, so latency is close to the pure-interrupt model. At high rates, polling adds some queuing, but the alternative would be dropped packets or livelock.
What value should netdev_budget have?
The default of 300 suits most servers. Raise it only if time_squeeze grows during traffic peaks and you also see drops or rising latency. Values between 600 and 1000 are common on busy hosts; much larger values can hurt other tasks on the same CPU.
Is busy polling worth enabling on a web server?
Usually not. Web workloads gain more from multiple queues, RPS and efficient application code. Busy polling pays off when microseconds matter and you can dedicate CPU cores to spinning, such as low-latency trading or real-time game servers.