libbpf-rs icon indicating copy to clipboard operation
libbpf-rs copied to clipboard

Recommended way of including kernel uapi

Open lbrndnr opened this issue 1 year ago • 1 comments

Hi everybody,

I have a question regarding the compilation of the bpf code. It took me a while to understand that I have to manually include the kernel uapi in the build script using something like:

.clang_args([
    OsStr::new("-I"),
    OsStr::new("vmlinux"),
    OsStr::new("-I"),
    OsStr::new("libbpf/include/uapi"),
])

Is this the recommended way or am I missing something? Also, given that this is a wrapper around libbpf, wouldn't it be possible to including this automatically?

In any case, thanks for your help! Cheers

lbrndnr avatar Aug 29 '24 13:08 lbrndnr

As per my understanding, uapi headers are shipped with libbpf-sys: https://docs.rs/libbpf-sys/1.4.5+v1.4.5/src/libbpf_sys/lib.rs.html#23-34

vmlinux.h isn't, as it's exposing kernel internals which are a moving target.

danielocfb avatar Aug 29 '24 16:08 danielocfb

Thanks, that's actually a good point! But these are the regular libbpf headers, not the kernel uapi headers. If I'm not mistaken, the uapi headers are needed in order to use something like bpf_for.

lbrndnr avatar Aug 30 '24 06:08 lbrndnr

bpf_for is coming from libbpf's own API headers, they are separate from kernel UAPI headers. For example, you shouldn't need this for libbpf's bpf/bpf_helpers.h header include. But some libbpf headers, like bpf/bpf_tracing.h expects that you either include vmlinux.h or kernel UAPI headers, so that types like struct pt_regs is defined properly (bpf_tracing.h doesn't define it itself and doesn't include any dependent header because it can be either vmlinux.h or kernel UAPI header, and those two are not compatible between each other).

Having said that, including libbpf's proved copy of latest kernel UAPI headers makes logistics much simpler, because you won't depend on your build system having recent enough kernel UAPI headers.

anakryiko avatar Aug 30 '24 15:08 anakryiko

Thanks for this detailed response Andrii! One thing though: bpf_for makes use of bpf_iter_num, which is declared in uapi/linux/bpf.h. Is this what you refer to as libbpf's own API headers? I thought these are the kernel UAPI headers because they're located in this linux directory. Either way, installing the UAPI headers that come with libbpf fixes the problem :)

lbrndnr avatar Sep 02 '24 07:09 lbrndnr

Thanks for this detailed response Andrii! One thing though: bpf_for makes use of bpf_iter_num, which is declared in uapi/linux/bpf.h. Is this what you refer to as libbpf's own API headers? I thought these are the kernel UAPI headers because they're located in this linux directory. Either way, installing the UAPI headers that come with libbpf fixes the problem :)

You mean headers in https://github.com/libbpf/libbpf/tree/master/include/uapi/linux ? Yeah, those are kernel UAPI headers, and the main purpose libbpf has copies of them is for libbpf's own compilation (to not have to rely on build systems having latest UAPI headers). You are free to use them, if it's convenient, but it's not really any sort of libbpf API. libbpf API headers are https://github.com/libbpf/libbpf/blob/master/src/Makefile#L70 and they are located in https://github.com/libbpf/libbpf/tree/master/src.

So as I mentioned, you can use those kernel UAPI headers that are part of libbpf repo, if that works for you, but by no means it's expected or required.

anakryiko avatar Sep 05 '24 03:09 anakryiko