In this lesson, you will learn how to optimize network throughput by balancing the kernel's workload between hardware interrupts and the New API (NAPI) polling mechanism.
The Interrupt Dilemma
In high-speed networking, if every incoming packet triggers a hardware interrupt, the CPU becomes overwhelmed by "interrupt storms." The system spends all its cycles saving context and handling individual packet arrival notices rather than processing the data itself.
How NAPI Solves It
NAPI (New API) is a hybrid approach designed to mitigate this overhead. It operates in two modes:
- Interrupt Mode: When traffic is light, the network card generates a hardware interrupt to tell the CPU a packet has arrived. The kernel drops into an Interrupt Service Routine (ISR) to handle it immediately.
- Polling Mode: When a high-traffic burst occurs, the kernel disables further hardware interrupts from that device and switches to a polling mechanism. The kernel schedules a "SoftIRQ" (software interrupt) to pull packets from the device’s ring buffer in batches.
By switching to polling, the kernel avoids the massive overhead of constant context switching, allowing it to process multiple packets in a single cycle.
Tuning the Behavior
You can observe the impact of these interrupts by checking /proc/interrupts. To influence how the kernel handles these, you can look at the weight assigned to NAPI polling via sysctl:
# Check the default weight for NAPI polling
sysctl net.core.dev_weight
Increasing net.core.dev_weight allows the kernel to process more packets per polling cycle before it yields to other tasks. While this can increase throughput for heavy loads, setting it too high may cause latency spikes for other system processes, as the CPU remains trapped in the polling loop longer.
Practical Optimization
On multi-core systems, the most effective way to optimize this is through Interrupt Affinity. By pinning specific hardware interrupts (found in /proc/interrupts) to specific CPU cores, you ensure that the cache remains warm and you avoid cross-core synchronization overhead. You can configure this by writing the hexadecimal CPU mask to /proc/irq/<irq_number>/smp_affinity.
Key Takeaway: NAPI optimizes network throughput by transitioning from high-overhead hardware interrupts to efficient, batch-processed software polling during periods of high congestion.