6

内核在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 应用程序。这部分工作得很好。

我错过了什么吗?我相信这项任务应该相当容易;-)

4

1 回答 1

7

假设这是“eBPF”。是的,这应该是可能的。基本上,您应该能够编译最简单的 eBPF 程序,如下所示:

clang -O2 -emit-llvm -c bpf.c -o - | llc -march=bpf -filetype=obj -o bpf.o

(取自手册页tc-bpf(8)

当然,如果您的程序使用来自本地头文件的定义,您必须找到一种方法来包含它们(即保留足够的头文件以编译您的文件,即使您“撕掉”其他所有内容)。

一些注意事项:

  • clang 和 llvm (llc) 应该是 3.7 或更高版本(越高越好)。
  • 根据您尝试编译的 eBPF 功能,您需要内核头文件(特别是<linux/bpf.h>)足够新以支持您的程序(另请参阅此页面)。
  • 不确定你打算用来做什么libbpf。如果我没记错的话,它是用来从外部程序加载和管理 eBPF 程序的,而不是包含在 eBPF 程序本身中?
  • [编辑]此外,eBPF 程序samples/bpf似乎是使用内核模块基础架构构建的。它们本身不是模块,而是以某种方式编译,就好像它们一样,可以访问内核头文件。因此,如果您尝试在树之外编译它们并且没有内核 makefile,您可能会失去对<linux/*.h>标头的访问权限,而必须替换<uapi/linux/*.h>by <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您的命令抱怨的标题的第一行;第二行是 for "bpf-helpers.h",您可以轻松地将其复制到您的工作目录中。最后一行可能更难删除,我没有详细搜索为什么kconfig.h需要,您必须对此进行调查。

于 2017-12-19T21:25:50.007 回答