本文将学习下Linux网络收包的NAPI机制。

What

NAPI (“new API,” though it is not so new anymore) is an interrupt mitigation mechanism used with network devices.

Why

随着网络带宽的发展,网速越来越快,之前的中断收包模式已经无法适应目前千兆,万兆的带宽了。如果每个数据包大小等于MTU大小(1460字节)。当驱动以千兆网速收包时,CPU将每秒被中断91829次。过多的中断会引起一个问题,CPU一直陷入硬中断而没有时间来处理别的事情了。为了解决这个问题,内核引入了NAPI机制。

NAPI就是混合中断和轮询的方式来收包,当有中断来了,驱动关闭中断,通知内核收包,内核软中断轮询当前网卡,在规定时间尽可能多的收包。时间用尽或者没有数据可收,内核再次开启中断,准备下一次收包。

When network traffic is heavy, the kernel can safely predict that incoming packets will be available anytime it gets around to looking, so there is no need to have the adapter interrupting it (possibly thousands of times per second) to tell it about those packets. So a NAPI-compliant driver will turn off the packet receive interrupt and provide a poll() method to the kernel. When the kernel is ready to deal with more packets, poll() will be called with a maximum number of packets it is allowed to feed into the kernel; it should process up to that many packets and quit.

Full Picture

总结


参考资料:

  1. Reworking NAPI
  2. Newer, newer NAPI
  3. NAPI
  4. Linux网络协议栈:NAPI机制与处理流程分析(图解)
  5. NAPI 内核机制与驱动实现
  6. Linux 网络数据接收流程(TCP)- NAPI
  7. Red Hat Enterprise Linux Network Performance Tuning Guide