BPF samples编译及错误解决

本文介绍了解决BPF(Berkeley Packet Filter)在Linux内核中编译及链接过程中遇到的问题,包括编译工具版本要求、配置选项设置等关键步骤。针对常见的编译错误提供了详细的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先阅读samples/bpf/README.rst。

非常关键:编译最新代码,llvm/llc和clang版本要高于10.0.0,使用llc-12和clang-12可以编译成功。

llc --version

  LLVM version 10.0.0

clang --version

clang version 10.0.0-4ubuntu1 

这两个版本,无法全部编译最新的bpf用例。

linux-next编译BPF:

make menuconfig

make bzImage

make headers_install

make M=samples/bpf/

或者指定LLC和CLANG版本:

make M=samples/bpf/ LLC=llc-12 CLANG=clang-12

如果要静态连接,可以加上选项:TPROGS_LDFLAGS=-static

例如:

make M=samples/bpf/ TPROGS_LDFLAGS=-static

注:使用make defconfig需要打开这两个宏

CONFIG_BPF=y

CONFIG_DEBUG_INFO_BTF=y

CONFIG_BPF_SYSCALL=y

alpha@mascot ~/Workspace/linux/linux-next $ grep BTF .config

CONFIG_VIDEO_SONY_BTF_MPX=m

CONFIG_DEBUG_INFO_BTF=y

CONFIG_PAHOLE_HAS_SPLIT_BTF=y

CONFIG_DEBUG_INFO_BTF_MODULES=y

# CONFIG_MODULE_ALLOW_BTF_MISMATCH is not set

alpha@mascot ~/Workspace/linux/linux-next $ grep BPF .config

CONFIG_BPF=y

CONFIG_HAVE_EBPF_JIT=y

CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y

# BPF subsystem

CONFIG_BPF_SYSCALL=y

CONFIG_BPF_JIT=y

CONFIG_BPF_JIT_ALWAYS_ON=y

CONFIG_BPF_JIT_DEFAULT_ON=y

CONFIG_BPF_UNPRIV_DEFAULT_OFF=y

# CONFIG_BPF_PRELOAD is not set

# CONFIG_BPF_LSM is not set

# end of BPF subsystem

CONFIG_CGROUP_BPF=y

CONFIG_IPV6_SEG6_BPF=y

CONFIG_NETFILTER_XT_MATCH_BPF=m

CONFIG_BPFILTER=y

CONFIG_BPFILTER_UMH=m

CONFIG_NET_CLS_BPF=m

CONFIG_NET_ACT_BPF=m

CONFIG_BPF_STREAM_PARSER=y

CONFIG_LWTUNNEL_BPF=y

CONFIG_BPF_EVENTS=y

CONFIG_BPF_KPROBE_OVERRIDE=y

CONFIG_TEST_BPF=m

===========================================

make[1]: *** No rule to make target 'debian/canonical-certs.pem', needed by 'certs/x509_certificate_list'.  Stop.

make[1]: *** No rule to make target 'debian/canonical-revoked-certs.pem', needed by 'certs/x509_revocation_list'.  Stop.

=================

alpha@mascot ~/Workspace/linux/linux-next $ grep 'debian/canonical-certs.pem' .config

CONFIG_SYSTEM_TRUSTED_KEYS="debian/canonical-certs.pem"

alpha@mascot ~/Workspace/linux/linux-next $ ./scripts/config -d CONFIG_SYSTEM_TRUSTED_KEYS

alpha@mascot ~/Workspace/linux/linux-next $ grep 'debian/canonical-certs.pem' .config

alpha@mascot ~/Workspace/linux/linux-next $ grep 'debian/canonical-revoked-certs.pem' .config

CONFIG_SYSTEM_REVOCATION_KEYS="debian/canonical-revoked-certs.pem"

alpha@mascot ~/Workspace/linux/linux-next $ ./scripts/config -d CONFIG_SYSTEM_REVOCATION_KEYS

alpha@mascot ~/Workspace/linux/linux-next $ grep 'debian/canonical-revoked-certs.pem' .config

=====================================================

=====================================================

  CC  samples/bpf/xdp_rxq_info_user.o

skeleton/pid_iter.bpf.c:35:10: error: incomplete definition of type 'struct bpf_link'

                return BPF_CORE_READ((struct bpf_link *)ent, id);

                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:403:2: note: expanded from macro 'BPF_CORE_READ'

        ___type((src), a, ##__VA_ARGS__) __r;                               \

        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:274:29: note: expanded from macro '___type'

#define ___type(...) typeof(___arrow(__VA_ARGS__))

                            ^~~~~~~~~~~~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:272:23: note: expanded from macro '___arrow'

#define ___arrow(...) ___apply(___arrow, ___narg(__VA_ARGS__))(__VA_ARGS__)

                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:223:25: note: expanded from macro '___concat'

#define ___concat(a, b) a ## b

                        ^

<scratch space>:16:1: note: expanded from here

___arrow2

^

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:263:26: note: expanded from macro '___arrow2'

#define ___arrow2(a, b) a->b

                        ~^

skeleton/pid_iter.bpf.c:35:32: note: forward declaration of 'struct bpf_link'

                return BPF_CORE_READ((struct bpf_link *)ent, id);

                                             ^

skeleton/pid_iter.bpf.c:35:10: error: incomplete definition of type 'struct bpf_link'

                return BPF_CORE_READ((struct bpf_link *)ent, id);

                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:404:2: note: expanded from macro 'BPF_CORE_READ'

        BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);                  \

        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:311:2: note: expanded from macro 'BPF_CORE_READ_INTO'

        ___core_read(bpf_core_read, bpf_core_read,                          \

        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:302:2: note: expanded from macro '___core_read'

        ___apply(___core_read, ___empty(__VA_ARGS__))(fn, fn_ptr, dst,      \

        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

note: (skipping 3 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:296:2: note: expanded from macro '___core_read0'

        ___read(fn, dst, ___type(src), src, a);

        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:277:59: note: expanded from macro '___read'

        read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor)

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:206:79: note: expanded from macro 'bpf_core_read'

        bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src))

                                                                                     ^~~

skeleton/pid_iter.bpf.c:35:32: note: forward declaration of 'struct bpf_link'

                return BPF_CORE_READ((struct bpf_link *)ent, id);

                                             ^

skeleton/pid_iter.bpf.c:35:10: error: returning 'void' from a function with incompatible result type '__u32' (aka 'unsigned int')

                return BPF_CORE_READ((struct bpf_link *)ent, id);

                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/home/alpha/Workspace/linux/linux-next/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:402:36: note: expanded from macro 'BPF_CORE_READ'

#define BPF_CORE_READ(src, a, ...) ({                                       \

                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

skeleton/pid_iter.bpf.c:42:17: warning: declaration of 'struct bpf_iter__task_file' will not be visible outside of this function [-Wvisibility]

int iter(struct bpf_iter__task_file *ctx)

                ^

skeleton/pid_iter.bpf.c:44:25: error: incomplete definition of type 'struct bpf_iter__task_file'

        struct file *file = ctx->file;

                            ~~~^

skeleton/pid_iter.bpf.c:42:17: note: forward declaration of 'struct bpf_iter__task_file'

int iter(struct bpf_iter__task_file *ctx)

                ^

skeleton/pid_iter.bpf.c:45:32: error: incomplete definition of type 'struct bpf_iter__task_file'

        struct task_struct *task = ctx->task;

                                   ~~~^

skeleton/pid_iter.bpf.c:42:17: note: forward declaration of 'struct bpf_iter__task_file'

int iter(struct bpf_iter__task_file *ctx)

                ^

skeleton/pid_iter.bpf.c:76:19: error: incomplete definition of type 'struct bpf_iter__task_file'

        bpf_seq_write(ctx->meta->seq, &e, sizeof(e));

                      ~~~^

skeleton/pid_iter.bpf.c:42:17: note: forward declaration of 'struct bpf_iter__task_file'

int iter(struct bpf_iter__task_file *ctx)

========================

这种错误,尝试执行命令:

make -C samples/bpf/ clean

make -C tools/ clean

make bzImage

make headers_install

make M=samples/bpf/ LLC=llc-12 CLANG=clang-12

============================================================

============================================================

Error: failed to load BTF from /home/alpha/Workspace/linux/mainline/linux/vmlinux: Unknown error -2

# CONFIG_DEBUG_INFO_BTF is not set

=================

要打开CONFIG_DEBUG_INFO_BTF:

/scripts/config -e CONFIG_DEBUG_INFO_BTF

============================================================

=======================================

libbpf: failed to find BTF info for global/extern symbol 'tx_mac_addr'

Error: failed to link 'samples/bpf/xdp_redirect_cpu.bpf.o': Unknown error -2 (-2)

make[1]: *** [samples/bpf/Makefile:429: samples/bpf/xdp_redirect_cpu.skel.h] Error 254

=================

这种错误是llc和clang版本不对,要高于10.x.x版本: 

make M=samples/bpf/ LLC=llc-12 CLANG=clang-12

===============================================

使用上面代码编译出现以下问题:make -C ../../ /root/bpf/linux-4.19.90/samples/bpf/ BPF_SAMPLES_PATH=/root/bpf/linux-4.19.90/samples/bpf make[1]: Entering directory '/root/bpf/linux-4.19.90' CALL scripts/checksyscalls.sh DESCEND objtool make -C /root/bpf/linux-4.19.90/samples/bpf/../../tools/lib/bpf/ RM='rm -rf' LDFLAGS= srctree=/root/bpf/linux-4.19.90/samples/bpf/../../ O= Warning: Kernel ABI header at 'tools/include/uapi/linux/bpf.h' differs from latest version at 'include/uapi/linux/bpf.h' HOSTCC /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.o /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: In function ‘print_violation’: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:53:26: warning: implicit declaration of function ‘localtime’; did you mean ‘st_atime’? [-Wimplicit-function-declaration] struct tm *tm_info = localtime(&ts); ^~~~~~~~~ st_atime /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:53:26: warning: initialization makes pointer from integer without a cast [-Wint-conversion] /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:55:5: warning: implicit declaration of function ‘strftime’ [-Wimplicit-function-declaration] strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", tm_info); ^~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:55:5: warning: incompatible implicit declaration of built-in function ‘strftime’ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:55:5: note: include ‘<time.h>’ or provide a declaration of ‘strftime’ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: At top level: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:69:13: warning: function declaration isn’t a prototype [-Wstrict-prototypes] static void read_violations() { ^~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: In function ‘read_violations’: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:77:12: error: variable ‘attr’ has initializer but incomplete type struct perf_event_attr attr = { ^~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:10: error: ‘struct perf_event_attr’ has no member named ‘sample_type’ .sample_type = PERF_SAMPLE_RAW, ^~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:24: error: ‘PERF_SAMPLE_RAW’ undeclared (first use in this function) .sample_type = PERF_SAMPLE_RAW, ^~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:24: note: each undeclared identifier is reported only once for each function it appears in /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:24: warning: excess elements in struct initializer /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:78:24: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:79:10: error: ‘struct perf_event_attr’ has no member named ‘type’ .type = PERF_TYPE_SOFTWARE, ^~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:79:17: error: ‘PERF_TYPE_SOFTWARE’ undeclared (first use in this function) .type = PERF_TYPE_SOFTWARE, ^~~~~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:79:17: warning: excess elements in struct initializer /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:79:17: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:80:10: error: ‘struct perf_event_attr’ has no member named ‘config’ .config = PERF_COUNT_SW_BPF_OUTPUT, ^~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:80:19: error: ‘PERF_COUNT_SW_BPF_OUTPUT’ undeclared (first use in this function) .config = PERF_COUNT_SW_BPF_OUTPUT, ^~~~~~~~~~~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:80:19: warning: excess elements in struct initializer /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:80:19: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:81:10: error: ‘struct perf_event_attr’ has no member named ‘size’ .size = sizeof(attr), ^~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:81:23: error: invalid application of ‘sizeof’ to incomplete type ‘struct perf_event_attr’ .size = sizeof(attr), ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:81:17: warning: excess elements in struct initializer .size = sizeof(attr), ^~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:81:17: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:82:10: error: ‘struct perf_event_attr’ has no member named ‘wakeup_events’ .wakeup_events = 1, ^~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:82:26: warning: excess elements in struct initializer .wakeup_events = 1, ^ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:82:26: note: (near initialization for ‘attr’) /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:77:28: error: storage size of ‘attr’ isn’t known struct perf_event_attr attr = { ^~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:85:23: error: ‘__NR_perf_event_open’ undeclared (first use in this function); did you mean ‘bpf_perf_event_ret’? perf_fd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, 0); ^~~~~~~~~~~~~~~~~~~~ bpf_perf_event_ret /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:100:11: warning: implicit declaration of function ‘mmap’ [-Wimplicit-function-declaration] buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0); ^~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:100:33: error: ‘PROT_READ’ undeclared (first use in this function); did you mean ‘IPPROTO_RAW’? buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0); ^~~~~~~~~ IPPROTO_RAW /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:100:45: error: ‘PROT_WRITE’ undeclared (first use in this function); did you mean ‘PROT_READ’? buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0); ^~~~~~~~~~ PROT_READ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:100:57: error: ‘MAP_SHARED’ undeclared (first use in this function) buf = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0); ^~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:101:16: error: ‘MAP_FAILED’ undeclared (first use in this function); did you mean ‘MAP_SHARED’? if (buf == MAP_FAILED) { ^~~~~~~~~~ MAP_SHARED /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:113:55: error: dereferencing pointer to incomplete type ‘struct perf_event_mmap_page’ __u64 data_head = __sync_fetch_and_add(&header->data_head, 0); ^~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:131:5: warning: implicit declaration of function ‘munmap’ [-Wimplicit-function-declaration] munmap(buf, mmap_size); ^~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: In function ‘load_xdp’: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:194:15: warning: implicit declaration of function ‘if_nametoindex’ [-Wimplicit-function-declaration] ifindex = if_nametoindex(iface); ^~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:214:31: warning: implicit declaration of function ‘bpf_object__find_program_by_name’; did you mean ‘bpf_object__find_program_by_title’? [-Wimplicit-function-declaration] prog_fd = bpf_program__fd(bpf_object__find_program_by_name(obj, "xdp_whitelist")); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bpf_object__find_program_by_title /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:214:31: warning: passing argument 1 ofbpf_program__fd’ makes pointer from integer without a cast [-Wint-conversion] In file included from /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:11:0: ./tools/lib/bpf/libbpf.h:128:5: note: expected ‘struct bpf_program *’ but argument is of type ‘int’ int bpf_program__fd(struct bpf_program *prog); ^~~~~~~~~~~~~~~ /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c: At top level: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.c:238:13: warning: function declaration isn’t a prototype [-Wstrict-prototypes] static void unload_xdp() { ^~~~~~~~~~ make[2]: *** [scripts/Makefile.host:107: /root/bpf/linux-4.19.90/samples/bpf/xdp_whitelist_user.o] Error 1 make[1]: *** [Makefile:1695: /root/bpf/linux-4.19.90/samples/bpf/] Error 2 make[1]: Leaving directory '/root/bpf/linux-4.19.90' make: *** [Makefile:224: all] Error 2
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值