【问题标题】:How to compile tool and samples from within the kernel source tree? (e.g. bpftool, bpf samples)如何从内核源代码树中编译工具和示例? (例如 bpftool、bpf 样本)
【发布时间】:2019-07-31 08:55:26
【问题描述】:

目标:编译samples/bpf,编译bpf/bpftool并使用它们。

问题:在带有内核 4.18.0-25-generic 的 Ubuntu 18.04 仿生虚拟机上,我安装了执行 apt install linux-source-4.18.0 的内核 src 代码。 现在我将cd 转换为/usr/src/linux-source-4.18.0/linux-source-4.18.0/samples/bpf 并运行make,结果是

make -C ../../ /usr/src/linux-source-4.18.0/linux-source-4.18.0/samples/bpf/ BPF_SAMPLES_PATH=/usr/src/linux-source-4.18.0/linux-source-4.18.0/samples/bpf
make[1]: Entering directory '/usr/src/linux-source-4.18.0/linux-source-4.18.0'
scripts/kconfig/conf  --syncconfig Kconfig
***
*** Configuration file ".config" not found!
***
*** Please run some configurator (e.g. "make oldconfig" or
*** "make menuconfig" or "make xconfig").
***
scripts/kconfig/Makefile:40: recipe for target 'syncconfig' failed
make[3]: *** [syncconfig] Error 1
Makefile:562: recipe for target 'syncconfig' failed
make[2]: *** [syncconfig] Error 2
make[1]: *** No rule to make target 'include/config/auto.conf', needed by 'include/config/kernel.release'.  Stop.
make[1]: Leaving directory '/usr/src/linux-source-4.18.0/linux-source-4.18.0'
Makefile:203: recipe for target 'all' failed
make: *** [all] Error 2

如果我将cd 转换为../samples/bpf 并运行sudo make,则结果是

Auto-detecting system features:
...                        libbfd: [ OFF ]
...        disassembler-four-args: [ OFF ]

  CC       map_perf_ring.o
  CC       xlated_dumper.o
  CC       perf.o
  CC       cfg.o
  CC       common.o
  CC       cgroup.o
  CC       main.o
main.c:36:10: fatal error: bfd.h: No such file or directory
 #include <bfd.h>
          ^~~~~~~
compilation terminated.
Makefile:92: recipe for target 'main.o' failed
make: *** [main.o] Error 1

问题:我错过了什么?在我编译它们之后,如果我想编写一个程序,例如需要使用bpftool,我必须在源内核目录中编写程序,或者我可以在任何地方编写它?

【问题讨论】:

    标签: makefile kernel bpf


    【解决方案1】:

    构建错误

    第一种情况 (Makefile:562: recipe for target 'syncconfig' failed) 失败,因为您从 linux 内核存储库顶部运行 make,并且在尝试编译示例之前,构建系统会尝试加载配置用于您的系统的文件(但没有找到)。

    在尝试构建示例 (make -C samples/bpf) 之前,您可以从当前内核配置中创建一个 .config 文件,如下所示:

    $ cp /usr/src/linux-headers-$(uname -r)/.config <path to repo>/.config
    $ make olddefconfig
    

    或者甚至只是从头开始生成一个默认配置文件:

    $ make defconfig
    

    查看顶级目录中的 make help 以查看可用的 make 选项。

    您的第二个错误,关于未找到bfd.h,是您错过了一个库。 Ubuntu 上的 libbfd 带有 binutils-dev,所以 apt install binutils-dev 应该可以解决问题。

    编译程序

    最后,关于编译程序的问题:

    • 您可以从内核存储库编写和构建程序,只需创建一个新示例并重用现有的 Makefile。
    • 您还可以在内核树之外编写和编译程序。用于编译它们的基本clang(v4.0 或更高版本,如果可能,v6.0 或更高版本)命令通常如下所示:
    $ clang -O2 -emit-llvm -c my_bpf_prog.c -o - | \
              llc -march=bpf -filetype=obj -o my_bpf_prog.o
    

    您可以在in that repository(免责声明:由我的公司提供)或XDP tutorial repo 中找到编译出内核树的程序示例。

    【讨论】:

    • 再次感谢,现在建筑物更进一步,但我仍然遇到很多错误。像这样 /usr/src/linux-source-4.18.0/linux-source-4.18.0/samples/bpf/bpf_load.c:90:15: error: ‘BPF_PROG_TYPE_RAW_TRACEPOINT’ undeclared (第一次在这个函数中使用);你的意思是“BPF_PROG_TYPE_TRACEPOINT”吗? prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ BPF_PROG_TYPE_TRACEPOINT 之前我也得到了一个“重新定义错误”和一个“缺少类型定义错误”,我解决了修改此处报告的代码lkml.org/lkml/2019/5/17/783
    • 我没有执行 sudo make headers_install。在我这样做之后, sudo make -C samples/bpf 工作。
    • 是的,您需要安装标头。如果您还没有,最新的内核会告诉您这样做,但 4.18 可能还不是这样。很高兴您设法编译了样本! :)
    • 1) 是。对于 2),取决于您的钩子:您可以使用 bpftool 加载 prog,但它是 doesn't support attaching the program yet。 XDP 的加载需要ip link,TC 需要tc qdisc/tc filter,套接字需要一些带有setsockopt() 的C 应用程序。 ip link/tc 直接从目标文件 (.o) 工作。对于套接字,您需要一个 FD,您可以从程序 id 或固定路径(使用 libbpf)获取。对于 bpftool 也可以查看 at this thread
    • 例如通常对于套接字,man 7 socket 告诉您 SO_ATTACH_BPF for setsockopt() 需要文件描述符。一旦你的程序被加载到系统上(例如bpftool prog load my_prog.o /sys/fs/bpf/foo),你可以看到它的id(bpftool prog list)并使用它来获取一个FD(libbpf:bpf_prog_get_fd_by_id())。或者使用固定路径 (/sys/fs/bpf/foo) 获得相同的 FD (libbpf: bpf_obj_get())。或者,如果您首先使用 libbpf 加载程序(不涉及 bpftool),如果我没记错的话,您会自动从 bpf_object__load() 检索 FD。
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多