fatal error: 'linux/bio.h' file not found

fatal error: 'linux/bio.h' file not found

当我尝试编译包含 bio.h 的 eBPF 程序时,出现错误。 为什么我可以包括其他 Linux headers,而不是这个特定的?

我使用以下命令编译

clang -O2 -target bpf -c src/bpf_program.c -Ikernel-src/tools/testing/selftests/bpf -Ikernel-src/tools/lib/bpf -o src/bpf_program.o

计划:

#include <asm/ptrace.h>
#include <linux/bpf.h>
#include "bpf_helpers.h"
#include <linux/bio.h>

SEC("kprobe/nvme_queue_rq")
int bpf_prog(struct pt_regs *ctx) {
    ..
}

char _license[] SEC("license") = "GPL";

我收到这个错误

src/bpf_program.c:4:10: fatal error: 'linux/bio.h' file not found
#include <linux/bio.h>
         ^~~~~~~~~~~~~
1 error generated.
make: *** [Makefile:36: build] Error 1

你拉入的header文件来自/usr/include/linux(从include/uapi安装在内核仓库中),当你编译用户space程序时#include .

内核 space header 故意不同,以防止您尝试访问内核外部无法访问的内容。

更新: 我不是 bpf 程序的专家,但快速阅读表明 bpf 样本是(或曾经)使用 Linux 内核的 KConfig Makefile 系统编译的。如果您使用 samples/bpf/Makefile 作为模板,您可能能够针对更多 in-kernel header 进行编译。我不确定这是不是故意的。