1*387f9dfdSAndroid Build Coastguard WorkerUseful links 2*387f9dfdSAndroid Build Coastguard Worker------------ 3*387f9dfdSAndroid Build Coastguard Worker 4*387f9dfdSAndroid Build Coastguard Worker- [BPF Portability and CO-RE](https://facebookmicrosites.github.io/bpf/blog/2020/02/19/bpf-portability-and-co-re.html) 5*387f9dfdSAndroid Build Coastguard Worker- [HOWTO: BCC to libbpf conversion](https://facebookmicrosites.github.io/bpf/blog/2020/02/20/bcc-to-libbpf-howto-guide.html) 6*387f9dfdSAndroid Build Coastguard Worker- [Tips & tricks for writing libbpf-tools](https://en.pingcap.com/blog/tips-and-tricks-for-writing-linux-bpf-applications-with-libbpf) 7*387f9dfdSAndroid Build Coastguard Worker 8*387f9dfdSAndroid Build Coastguard WorkerBuilding 9*387f9dfdSAndroid Build Coastguard Worker------- 10*387f9dfdSAndroid Build Coastguard Worker 11*387f9dfdSAndroid Build Coastguard WorkerTo build libbpf-based tools, simply run `make`. This will build all the listed 12*387f9dfdSAndroid Build Coastguard Workertools/applications. All the build artifacts, by default, go into .output 13*387f9dfdSAndroid Build Coastguard Workersubdirectory to keep source code and build artifacts completely separate. The 14*387f9dfdSAndroid Build Coastguard Workeronly exception is resulting tool binaries, which are put in a current 15*387f9dfdSAndroid Build Coastguard Workerdirectory. `make clean` will clean up all the build artifacts, including 16*387f9dfdSAndroid Build Coastguard Workergenerated binaries. 17*387f9dfdSAndroid Build Coastguard Worker 18*387f9dfdSAndroid Build Coastguard WorkerGiven that the libbpf package might not be available across wide variety of 19*387f9dfdSAndroid Build Coastguard Workerdistributions, all libbpf-based tools are linked statically against a version 20*387f9dfdSAndroid Build Coastguard Workerof libbpf that BCC links against (from submodule under src/cc/libbpf). This 21*387f9dfdSAndroid Build Coastguard Workerresults in binaries with minimal amount of dependencies (libc, libelf, and 22*387f9dfdSAndroid Build Coastguard Workerlibz are linked dynamically, though, given their widespread availability). 23*387f9dfdSAndroid Build Coastguard WorkerIf your build fails because the libbpf submodule is outdated, try running `git 24*387f9dfdSAndroid Build Coastguard Workersubmodule update --init --recursive`. 25*387f9dfdSAndroid Build Coastguard Worker 26*387f9dfdSAndroid Build Coastguard WorkerTools are expected to follow a simple naming convention: 27*387f9dfdSAndroid Build Coastguard Worker - <tool>.c contains userspace C code of a tool. 28*387f9dfdSAndroid Build Coastguard Worker - <tool>.bpf.c contains BPF C code, which gets compiled into BPF ELF file. 29*387f9dfdSAndroid Build Coastguard Worker This ELF file is used to generate BPF skeleton <tool>.skel.h, which is 30*387f9dfdSAndroid Build Coastguard Worker subsequently is included from <tool>.c. 31*387f9dfdSAndroid Build Coastguard Worker - <tool>.h can optionally contain any types and constants, shared by both 32*387f9dfdSAndroid Build Coastguard Worker BPF and userspace sides of a tool. 33*387f9dfdSAndroid Build Coastguard Worker 34*387f9dfdSAndroid Build Coastguard WorkerFor such cases, simply adding <tool> name to Makefile's APPS variable will 35*387f9dfdSAndroid Build Coastguard Workerensure this tool is built alongside others. 36*387f9dfdSAndroid Build Coastguard Worker 37*387f9dfdSAndroid Build Coastguard WorkerFor more complicated applications, some extra Makefile rules might need to be 38*387f9dfdSAndroid Build Coastguard Workercreated. For such cases, it is advised to put application into a dedicated 39*387f9dfdSAndroid Build Coastguard Workersubdirectory and link it from main Makefile. 40*387f9dfdSAndroid Build Coastguard Worker 41*387f9dfdSAndroid Build Coastguard Workervmlinux.h generation 42*387f9dfdSAndroid Build Coastguard Worker------------------- 43*387f9dfdSAndroid Build Coastguard Worker 44*387f9dfdSAndroid Build Coastguard Workervmlinux.h contains all kernel types, both exported and internal-only. BPF 45*387f9dfdSAndroid Build Coastguard WorkerCO-RE-based applications are expected to include this file in their BPF 46*387f9dfdSAndroid Build Coastguard Workerprogram C source code to avoid dependency on kernel headers package. 47*387f9dfdSAndroid Build Coastguard Worker 48*387f9dfdSAndroid Build Coastguard WorkerFor more reproducible builds, vmlinux.h header file is pre-generated and 49*387f9dfdSAndroid Build Coastguard Workerchecked in along the other sources. This is done to avoid dependency on 50*387f9dfdSAndroid Build Coastguard Workerspecific user/build server's kernel configuration, because vmlinux.h 51*387f9dfdSAndroid Build Coastguard Workergeneration depends on having a kernel with BTF type information built-in 52*387f9dfdSAndroid Build Coastguard Worker(which is enabled by `CONFIG_DEBUG_INFO_BTF=y` Kconfig option See below). 53*387f9dfdSAndroid Build Coastguard Worker 54*387f9dfdSAndroid Build Coastguard Workervmlinux.h is generated from upstream Linux version at particular minor 55*387f9dfdSAndroid Build Coastguard Workerversion tag. E.g., `vmlinux_505.h` is generated from v5.5 tag. Exact set of 56*387f9dfdSAndroid Build Coastguard Workertypes available in compiled kernel depends on configuration used to compile 57*387f9dfdSAndroid Build Coastguard Workerit. To generate present vmlinux.h header, default configuration was used, with 58*387f9dfdSAndroid Build Coastguard Workeronly extra `CONFIG_DEBUG_INFO_BTF=y` option enabled. 59*387f9dfdSAndroid Build Coastguard Worker 60*387f9dfdSAndroid Build Coastguard WorkerGiven different kernel version can have incompatible type definitions, it 61*387f9dfdSAndroid Build Coastguard Workermight be important to use vmlinux.h of a specific kernel version as a "base" 62*387f9dfdSAndroid Build Coastguard Workerversion of header. To that extent, all vmlinux.h headers are versioned by 63*387f9dfdSAndroid Build Coastguard Workerappending <MAJOR><MINOR> suffix to a file name. There is always a symbolic 64*387f9dfdSAndroid Build Coastguard Workerlink vmlinux.h, that points to whichever version is deemed to be default 65*387f9dfdSAndroid Build Coastguard Worker(usually, latest). 66*387f9dfdSAndroid Build Coastguard Worker 67*387f9dfdSAndroid Build Coastguard Workerbpftool 68*387f9dfdSAndroid Build Coastguard Worker------- 69*387f9dfdSAndroid Build Coastguard Worker 70*387f9dfdSAndroid Build Coastguard Workerbpftool is a universal tool used for inspection of BPF resources, as well as 71*387f9dfdSAndroid Build Coastguard Workerproviding various extra BPF-related facilities, like code-generation of BPF 72*387f9dfdSAndroid Build Coastguard Workerprogram skeletons. The latter functionality is heavily used by these tools to 73*387f9dfdSAndroid Build Coastguard Workerload and interact with BPF programs. 74*387f9dfdSAndroid Build Coastguard Worker 75*387f9dfdSAndroid Build Coastguard WorkerGiven bpftool package can't yet be expected to be available widely across many 76*387f9dfdSAndroid Build Coastguard Workerdistributions, bpftool binary is checked in into BCC repository in bin/ 77*387f9dfdSAndroid Build Coastguard Workersubdirectory. Once bpftool package is more widely available, this can be 78*387f9dfdSAndroid Build Coastguard Workerchanged in favor of using pre-packaged version of bpftool. 79*387f9dfdSAndroid Build Coastguard Worker 80*387f9dfdSAndroid Build Coastguard Worker 81*387f9dfdSAndroid Build Coastguard WorkerRe-compiling your Kernel with CONFIG_DEBUG_INFO_BTF=y 82*387f9dfdSAndroid Build Coastguard Worker----------------------------------------------------- 83*387f9dfdSAndroid Build Coastguard Workerlibbpf probes to see if your sys fs exports the file `/sys/kernel/btf/vmlinux` (from Kernel 5.5+) or if you have the ELF version in your system [`code`](https://github.com/libbpf/libbpf/blob/master/src/btf.c) 84*387f9dfdSAndroid Build Coastguard WorkerPlease note the ELF file could exist without the BTF info in it. Your Kconfig should contain the options below 85*387f9dfdSAndroid Build Coastguard Worker 86*387f9dfdSAndroid Build Coastguard Worker1. Compile options 87*387f9dfdSAndroid Build Coastguard Worker```code 88*387f9dfdSAndroid Build Coastguard WorkerCONFIG_DEBUG_INFO_BTF=y 89*387f9dfdSAndroid Build Coastguard WorkerCONFIG_DEBUG_INFO=y 90*387f9dfdSAndroid Build Coastguard Worker``` 91*387f9dfdSAndroid Build Coastguard Worker2. Also, make sure that you have pahole 1.13 (or preferably 1.16+) during the 92*387f9dfdSAndroid Build Coastguard Workerkernel build (it comes from dwarves package). Without it, BTF won't be 93*387f9dfdSAndroid Build Coastguard Workergenerated, and on older kernels you'd get only warning, but still would 94*387f9dfdSAndroid Build Coastguard Workerbuild kernel successfully 95*387f9dfdSAndroid Build Coastguard Worker 96*387f9dfdSAndroid Build Coastguard WorkerRunning in kernels without CONFIG_DEBUG_INFO_BTF=y 97*387f9dfdSAndroid Build Coastguard Worker-------------------------------------------------- 98*387f9dfdSAndroid Build Coastguard Worker 99*387f9dfdSAndroid Build Coastguard WorkerIt's possible to run some tools in kernels that don't expose 100*387f9dfdSAndroid Build Coastguard Worker`/sys/kernel/btf/vmlinux`. For those cases, 101*387f9dfdSAndroid Build Coastguard Worker[BTFGen](https://lore.kernel.org/bpf/[email protected]) 102*387f9dfdSAndroid Build Coastguard Workerand [BTFHub](https://github.com/aquasecurity/btfhub) can be used to 103*387f9dfdSAndroid Build Coastguard Workergenerate small BTF files for the most popular Linux distributions that 104*387f9dfdSAndroid Build Coastguard Workerare shipped with the tools in order to provide the needed information to 105*387f9dfdSAndroid Build Coastguard Workerperform the CO-RE relocations when loading the eBPF programs. 106*387f9dfdSAndroid Build Coastguard Worker 107*387f9dfdSAndroid Build Coastguard WorkerIf you haven't cloned the 108*387f9dfdSAndroid Build Coastguard Worker[btfhub-archive](https://github.com/aquasecurity/btfhub) repository, you 109*387f9dfdSAndroid Build Coastguard Workercan run make and it'll clone it for you into the `$HOME/.local/share` 110*387f9dfdSAndroid Build Coastguard Workerdirectory: 111*387f9dfdSAndroid Build Coastguard Worker 112*387f9dfdSAndroid Build Coastguard Worker```bash 113*387f9dfdSAndroid Build Coastguard Workermake ENABLE_MIN_CORE_BTFS=1 -j$(nproc) 114*387f9dfdSAndroid Build Coastguard Worker``` 115*387f9dfdSAndroid Build Coastguard Worker 116*387f9dfdSAndroid Build Coastguard WorkerIf you have a local copy of such repository, you can pass it's location 117*387f9dfdSAndroid Build Coastguard Workerto avoid cloning it again: 118*387f9dfdSAndroid Build Coastguard Worker 119*387f9dfdSAndroid Build Coastguard Worker```bash 120*387f9dfdSAndroid Build Coastguard Workermake ENABLE_MIN_CORE_BTFS=1 BTF_HUB_ARCHIVE=<path_to_btfhub-archive> -j$(nproc) 121*387f9dfdSAndroid Build Coastguard Worker``` 122