简体   繁体   中英

Error compiling eBPF C code inside kernel source files

I'm compiling the eBPf programs inside Linux kernel tree , after I run the command sudo make headers_install && sudo make M=samples/bpf , error shows agian. In fact, I'm in stuck here for quite a long time. The first time the error is something wrong with my ubuntu. So I reinstalled it. But now the error is strange.

skeleton/pid_iter.bpf.c:47:14: error: incomplete definition of type 'struct bpf_perf_link'
        perf_link = container_of(link, struct bpf_perf_link, link);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helpers.h:74:22: note: expanded from macro 'container_of'
                ((type *)(__mptr - offsetof(type, member)));    \
                                   ^~~~~~~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_helpers.h:68:60: note: expanded from macro 'offsetof'
#define offsetof(TYPE, MEMBER)  ((unsigned long)&((TYPE *)0)->MEMBER)
                                                 ~~~~~~~~~~~^
skeleton/pid_iter.bpf.c:44:9: note: forward declaration of 'struct bpf_perf_link'
        struct bpf_perf_link *perf_link;
               ^
skeleton/pid_iter.bpf.c:48:10: error: incomplete definition of type 'struct bpf_perf_link'
        event = BPF_CORE_READ(perf_link, perf_file, private_data);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/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/liusy/Downloads/linux-5.18/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/liusy/Downloads/linux-5.18/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/liusy/Downloads/linux-5.18/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>:19:1: note: expanded from here
___arrow3
^
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:264:29: note: expanded from macro '___arrow3'
#define ___arrow3(a, b, c) a->b->c
                           ~^
skeleton/pid_iter.bpf.c:44:9: note: forward declaration of 'struct bpf_perf_link'
        struct bpf_perf_link *perf_link;
               ^
skeleton/pid_iter.bpf.c:48:10: error: incomplete definition of type 'struct bpf_perf_link'
        event = BPF_CORE_READ(perf_link, perf_file, private_data);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/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/liusy/Downloads/linux-5.18/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/liusy/Downloads/linux-5.18/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 9 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:280:33: note: expanded from macro '___rd_first'
#define ___rd_first(fn, src, a) ___read(fn, &__t, ___type(src), src, a);
                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/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/liusy/Downloads/linux-5.18/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:44:9: note: forward declaration of 'struct bpf_perf_link'
        struct bpf_perf_link *perf_link;
               ^
skeleton/pid_iter.bpf.c:48:10: error: incomplete definition of type 'struct bpf_perf_link'
        event = BPF_CORE_READ(perf_link, perf_file, private_data);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/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/liusy/Downloads/linux-5.18/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/liusy/Downloads/linux-5.18/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 9 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:263:26: note: expanded from macro '___arrow2'
#define ___arrow2(a, b) a->b
                         ^
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:277:44: note: expanded from macro '___read'
        read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/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:44:9: note: forward declaration of 'struct bpf_perf_link'
        struct bpf_perf_link *perf_link;
               ^
skeleton/pid_iter.bpf.c:48:8: warning: incompatible integer to pointer conversion assigning to 'struct perf_event *' from 'long' [-Wint-conversion]
        event = BPF_CORE_READ(perf_link, perf_file, private_data);
              ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
skeleton/pid_iter.bpf.c:49:30: error: no member named 'bpf_cookie' in 'struct perf_event'
        return BPF_CORE_READ(event, bpf_cookie);
               ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:403:17: note: expanded from macro 'BPF_CORE_READ'
        ___type((src), a, ##__VA_ARGS__) __r;                               \
        ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:274:38: note: expanded from macro '___type'
#define ___type(...) typeof(___arrow(__VA_ARGS__))
                            ~~~~~~~~~^~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:272:64: note: expanded from macro '___arrow'
#define ___arrow(...) ___apply(___arrow, ___narg(__VA_ARGS__))(__VA_ARGS__)
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:263:28: note: expanded from macro '___arrow2'
#define ___arrow2(a, b) a->b
                        ~  ^
skeleton/pid_iter.bpf.c:49:30: error: no member named 'bpf_cookie' in 'struct perf_event'
        return BPF_CORE_READ(event, bpf_cookie);
               ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:404:34: note: expanded from macro 'BPF_CORE_READ'
        BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__);                  \
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:312:20: note: expanded from macro 'BPF_CORE_READ_INTO'
                     dst, (src), a, ##__VA_ARGS__)                          \
                     ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:303:18: note: expanded from macro '___core_read'
                                                      src, a, ##__VA_ARGS__)
                                                      ~~~~~^~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:296:38: note: expanded from macro '___core_read0'
        ___read(fn, dst, ___type(src), src, a);
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:277:61: note: expanded from macro '___read'
        read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
/home/liusy/Downloads/linux-5.18/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:49:9: error: returning 'void' from a function with incompatible result type '__u64' (aka 'unsigned long long')
        return BPF_CORE_READ(event, bpf_cookie);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/liusy/Downloads/linux-5.18/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:90:36: error: use of undeclared identifier 'BPF_LINK_TYPE_PERF_EVENT'; did you mean 'BPF_PROG_TYPE_PERF_EVENT'?
                if (BPF_CORE_READ(link, type) == BPF_LINK_TYPE_PERF_EVENT) {
                                                 ^~~~~~~~~~~~~~~~~~~~~~~~
                                                 BPF_PROG_TYPE_PERF_EVENT
/home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/vmlinux.h:9615:2: note: 'BPF_PROG_TYPE_PERF_EVENT' declared here
        BPF_PROG_TYPE_PERF_EVENT = 7,
        ^
1 warning and 8 errors generated.
make[2]: *** [Makefile:182: /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/pid_iter.bpf.o] Error 1
make[1]: *** [samples/bpf/Makefile:296: /home/liusy/Downloads/linux-5.18/samples/bpf/bpftool/bpftool] Error 2

I searched for solutions for quite a long time. I found this https://www.spinics.net/lists/kernel/msg4322624.html . But I cannot get solution from it. Any help?

The error is in bpftool , because someone recently introduced a struct bpf_perf_link in the skeletons used by the tool to gather the PIDs of processes.

To compile without error, you need a kernel recent enough to have struct bpf_perf_link defined (it's 5.15+), and compiled with option CONFIG_PERF_EVENTS , as mentioned in the message you found.

Alternatively, there's a patch that was submitted upstream, but has not been merged yet as of this writing: it adds a local definition of the struct in the skeleton, which gets overwritten by the definition from the kernel BTF if present. You can find the patch here (it's a follow-up to the discussion you found) and apply it to bpftool. This should fix the error.

This answer is in response to @Nicholas' question:

./include/linux/compiler.h:248:10: fatal error: 'asm/rwonce.h' file not found

(I need to have 50 reputation to comment below @Nicholas)

In my case I had the same problem when I tried to write a simple eBPF program. The solution is to modify Makefile:

linuxbuildhdrs ?= /lib/modules/`uname -r`/build
linuxhdrs ?= /lib/modules/`uname -r`/source
LINUXINCLUDE =  -I$(linuxhdrs)/include \
                -I$(linuxhdrs)/include/uapi \
                -I$(linuxhdrs)/include/generated \
                -I$(linuxhdrs)/include/generated/uapi \
                -I$(linuxhdrs)/arch/x86/include \
                -I$(linuxhdrs)/arch/x86/include/uapi \
                -I$(linuxhdrs)/arch/x86/include/generated \
                -I$(linuxhdrs)/arch/x86/include/generated/uapi\
                -I$(linuxbuildhdrs)/include \
                -I$(linuxbuildhdrs)/arch/x86/include/generated

And if your code must include linux/kconfig.h , remember to include it before including other header files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM