Documentation/translations/zh_CN/networking/napi.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/networking/napi.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/networking/napi.rst- Extension
.rst- Size
- 18417 bytes
- Lines
- 363
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
struct epoll_params
Annotated Snippet
struct epoll_params {
uint32_t busy_poll_usecs;
uint16_t busy_poll_budget;
uint8_t prefer_busy_poll;
/* 将结构填充到 64 位的倍数 */
uint8_t __pad;
};
IRQ缓解
-------
虽然忙轮询旨在用于低延迟应用,但类似的机制可用于减少中断请求。
每秒高请求的应用程序(尤其是路由/转发应用程序和特别使用 AF_XDP 套接字的应用程序)
可能希望在处理完一个请求或一批数据包之前不被中断。
此类应用程序可以向内核承诺会定期执行忙轮询操作,而驱动程序应将设备的中断请求永久屏蔽。
通过使用 ``SO_PREFER_BUSY_POLL`` 套接字选项可启用此模式。为避免系统出现异常,如果
在 ``gro_flush_timeout`` 时间内没有进行任何忙轮询调用,该承诺将被撤销。对于基于
epoll 的忙轮询应用程序,可以将 ``struct epoll_params`` 结构体中的 ``prefer_busy_poll``
字段设置为 1,并使用 ``EPIOCSPARAMS`` 输入 / 输出控制(ioctl)操作来启用此模式。
更多详情请参阅上述章节。
NAPI 忙轮询的 budget 低于默认值(这符合正常忙轮询的低延迟意图)。减少中断请求的场景中
并非如此,因此 budget 可以通过 ``SO_BUSY_POLL_BUDGET`` 套接字选项进行调整。对于基于
epoll 的忙轮询应用程序,可以通过调整 ``struct epoll_params`` 中的 ``busy_poll_budget``
字段为特定值,并使用 ``EPIOCSPARAMS`` ioctl 在特定 epoll 上下文中设置。更多详细信
息请参见上述部分。
需要注意的是,为 ``gro_flush_timeout`` 选择较大的值会延迟中断请求,以实现更好的批
量处理,但在系统未满载时会增加延迟。为 ``gro_flush_timeout`` 选择较小的值可能会因
设备中断请求和软中断处理而干扰尝试进行忙轮询的用户应用程序。应权衡这些因素后谨慎选择
该值。基于 epoll 的忙轮询应用程序可以通过为 ``maxevents`` 选择合适的值来减少用户
处理的干扰。
用户可能需要考虑使用另一种方法,IRQ 挂起,以帮助应对这些权衡问题。
IRQ挂起
-------
IRQ 挂起是一种机制,其中设备 IRQ 在 epoll 触发 NAPI 数据包处理期间被屏蔽。
只要应用程序对 epoll_wait 的调用成功获取事件,内核就会推迟 IRQ 挂起定时器。如果
在忙轮询期间没有获取任何事件(例如,因为网络流量减少),则会禁用IRQ挂起功能,并启
用上述减少中断请求的策略。
这允许用户在 CPU 消耗和网络处理效率之间取得平衡。
要使用此机制:
1. 每个 NAPI 的配置参数 ``irq-suspend-timeout`` 应设置为应用程序可以挂起
IRQ 的最大时间(纳秒)。这通过 netlink 完成,如上所述。此超时时间作为一
种安全机制,如果应用程序停滞,将重新启动中断驱动程序的中断处理。此值应选择
为覆盖用户应用程序调用 epoll_wait 处理数据所需的时间,需注意的是,应用程
序可通过在调用 epoll_wait 时设置 ``max_events`` 来控制获取的数据量。
2. sysfs 参数或每个 NAPI 的配置参数 ``gro_flush_timeout`` 和 ``napi_defer_hard_irqs``
可以设置为较低值。它们将用于在忙轮询未找到数据时延迟 IRQs。
3. 必须将 ``prefer_busy_poll`` 标志设置为 true。如前文所述,可使用 ``EPIOCSPARAMS``
ioctl操作来完成此设置。
4. 应用程序按照上述方式使用 epoll 触发 NAPI 数据包处理。
如上所述,只要后续对 epoll_wait 的调用向用户空间返回事件,``irq-suspend-timeout``
就会被推迟并且 IRQ 会被禁用。这允许应用程序在无干扰的情况下处理数据。
一旦 epoll_wait 的调用没有找到任何事件,IRQ 挂起会被自动禁用,并且 ``gro_flush_timeout``
和 ``napi_defer_hard_irqs`` 缓解机制将开始起作用。
Annotation
- Detected declarations: `struct epoll_params`.
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.