先阅读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
===============================================