内核在 samples/bpf
中提供了许多示例。 .我有兴趣在树之外构建一个示例,就像我们构建一个内核模块一样,其中 Makefile
可以很简单。是否可以对 bpf 做同样的事情?我尝试从 samples/bpf/Makefile
中删除不必要的部分并保持对 libbpf
的依赖关系和其他人,但事实证明并不那么容易。
例如,尝试构建 samples/bpf/bpf_tcp_kern.c
在内核树之外,使用以下命令行(我查看了 samples/bpf/Makefile,以及 make samples/bpf V=1
的输出):
clang -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/5/include \
-I/home/mark/work/net-next.git/arch/x86/include -I/home/mark/work/net-next.git/arch/x86/include/generated -I./include -I/home/mark/work/net-next.git/arch/x86/include/uapi -I/home/mark/work/net-next.git/arch/x86/include/generated/uapi -I/home/mark/work/net-next.git/include -I/home/mark/work/net-next.git/generated/uapi -I./ \
-D__KERNEL__ -Wno-unused-value -Wno-pointer-sign \
-D__TARGET_ARCH_x86 -Wno-compare-distinct-pointer-types \
-Wno-gnu-variable-sized-type-not-at-end \
-Wno-address-of-packed-member -Wno-tautological-compare \
-Wno-unknown-warning-option \
-O2 -emit-llvm -c bpf_tcp_kern.c -o -| llc -march=bpf -filetype=obj -o bpf_tcp_kern.o
In file included from bpf_tcp_kern.c:15:
In file included from /home/mark/work/net-next.git/include/uapi/linux/bpf.h:11:
In file included from /home/mark/work/net-next.git/include/linux/types.h:6:
In file included from /home/mark/work/net-next.git/include/uapi/linux/types.h:5:
/home/mark/work/net-next.git/arch/x86/include/uapi/asm/types.h:5:10: fatal error: 'asm-generic/types.h' file not found
#include <asm-generic/types.h>
^
1 error generated
这是
clang-llvm 3.8.0
我需要
libbpf
构建用户端 bpf 应用程序。这部分工作得很好。我错过了什么吗?我相信这项任务应该相当容易;-)
最佳答案
假设这是“eBPF”。是的,这应该是可能的。基本上,您应该能够编译最简单的 eBPF 程序,如下所示:
clang -O2 -emit-llvm -c bpf.c -o - | llc -march=bpf -filetype=obj -o bpf.o
(取自
tc-bpf(8)
的手册页)当然,如果您的程序使用来自本地头文件的定义,您必须找到一种方法来包含它们(即,即使您“撕掉”其他所有内容,也可以从这些头文件中保留足够的内容来编译您的文件)。
一些注意事项:
<linux/bpf.h>
)以支持您的程序(另请参阅 this page )。 libbpf
为了。如果我没记错的话,它是用来从外部程序加载和管理 eBPF 程序的,而不是包含在 eBPF 程序本身中? samples/bpf
下的 eBPF 程序似乎是用内核模块基础设施构建的。它们本身不是模块,而是以某种方式编译,就好像它们一样,可以访问内核头文件。因此,如果您尝试在树之外编译它们并且没有内核 makefile,您可能会失去对 <linux/*.h>
的访问权限。 header ,并且必须替换 <uapi/linux/*.h>
通过 <linux/*.h>
相反.. 作为一般建议,尝试简化您的程序,直到它编译,然后再次添加功能:)。如果没有源代码或错误消息,恐怕无法真正为您提供更多帮助。祝你好运!
[ 编辑 问题本身更新后]
我可以通过在命令中添加以下三行来编译一个示例(通过运行获得它们
make samples/bpf/tcp_bufs_kern.o V=1
,不确定是否将它们修剪掉或者是否有不同的东西):…
-I/home/mark/work/net-next.git/include/generated/uapi \
-I/home/mark/work/net-next.git/tools/testing/selftests/bpf/ \
-include /home/mark/work/net-next.git/include/linux/kconfig.h \
…
asm-generic.h
的第一行您的命令提示的 header ;第二行是 "bpf-helpers.h"
,您可以轻松地将其复制到您的工作目录。最后一行可能更难删除,我没有详细搜索为什么kconfig.h
需要,您必须对此进行调查。
关于linux-kernel - 如何从内核树中构建 BPF 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47895179/