Documentation/translations/zh_CN/networking/msg_zerocopy.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/networking/msg_zerocopy.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_CN/networking/msg_zerocopy.rst- Extension
.rst- Size
- 9148 bytes
- Lines
- 224
- 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
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
.. SPDX-License-Identifier: GPL-2.0
.. include:: ../disclaimer-zh_CN.rst
:Original: Documentation/networking/msg_zerocopy.rst
:翻译:
王亚鑫 Wang Yaxin <wang.yaxin@zte.com.cn>
:校译:
- 徐鑫 xu xin <xu.xin16@zte.com.cn>
- 何配林 He Peilin <he.peilin@zte.com.cn>
============
MSG_ZEROCOPY
============
简介
====
MSG_ZEROCOPY 标志用于启用套接字发送调用的免拷贝功能。该功能目前适用于 TCP、UDP 和 VSOCK
(使用 virtio 传输)套接字。
机遇与注意事项
--------------
在用户进程与内核之间拷贝大型缓冲区可能会消耗大量资源。Linux 支持多种免拷贝的接口,如sendfile
和 splice。MSG_ZEROCOPY 标志将底层的拷贝避免机制扩展到了常见的套接字发送调用中。
免拷贝并非毫无代价。在实现上,它通过页面固定(page pinning)将按字节拷贝的成本替换为页面统计
(page accounting)和完成通知的开销。因此,MSG_ZEROCOPY 通常仅在写入量超过大约 10 KB 时
才有效。
页面固定还会改变系统调用的语义。它会暂时在进程和网络堆栈之间共享缓冲区。与拷贝不同,进程在系统
调用返回后不能立即覆盖缓冲区,否则可能会修改正在传输中的数据。内核的完整性不会受到影响,但有缺
陷的程序可能会破坏自己的数据流。
当内核返回数据可以安全修改的通知时,进程才可以修改数据。因此,将现有应用程序转换为使用
MSG_ZEROCOPY 并非总是像简单地传递该标志那样容易。
更多信息
--------
本文档的大部分内容是来自于 netdev 2.1 上发表的一篇长篇论文。如需更深入的信息,请参阅该论文和
演讲,或者浏览 LWN.net 上的精彩报道,也可以直接阅读源码。
论文、幻灯片、视频:
https://netdevconf.org/2.1/session.html?debruijn
LWN 文章:
https://lwn.net/Articles/726917/
补丁集:
[PATCH net-next v4 0/9] socket sendmsg MSG_ZEROCOPY
https://lore.kernel.org/netdev/20170803202945.70750-1-willemdebruijn.kernel@gmail.com
接口
====
传递 MSG_ZEROCOPY 标志是启用免拷贝功能的最明显步骤,但并非唯一的步骤。
套接字设置
----------
当应用程序向 send 系统调用传递未定义的标志时,内核通常会宽容对待。默认情况下,它会简单地忽略
这些标志。为了避免为那些偶然传递此标志的遗留进程启用免拷贝模式,进程必须首先通过设置套接字选项
来表明意图:
Annotation
- 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.