💡 BPF映射如何创建
程序员经常向程序传递消息引起程序行为被调用。bpf 最神奇的功能就是内核中运行的代码,和加载这些代码的程序可以通过消息传递方式实现实时通信。
BPF 映射是键值保存在内核中,可以被 BPF 程序访问。用户空间的程序也可以通过文件描述符访问 bpf 映射。bpf 映射中可以保存事先指定的大小任何类型数据。
内核将键值作为二进制块,内核并不关系 bpf 映射的具体内容,由验证器来保证安全。
创建 BPF 映射
创建 BPF 映射最直接的方法是使用 bpf 系统调用,第一个参数设置为 BPF_MAP_CREATE,则表示创建一个新的映射。
该调用将返回与创建映射相关的文件描述符。bpf 系统调用的第二个参数如下所示:
https://man7.org/linux/man-pages/man2/bpf.2.htm[1]
BPF_PROG_LOAD
Verify and load an eBPF program, returning a new file
descriptor associated with the program. The close-on-exec
file descriptor flag (seefcntl(2)) is automatically
enabled for the new file descriptor.
Thebpf_attr union consists of various anonymous
structures that are used by differentbpf() commands:
union bpf_attr {
struct { /* Used by BPF_