^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) eBPF sample programs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) ====================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) This directory contains a test stubs, verifier test-suite and examples
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) for using eBPF. The examples use libbpf from tools/lib/bpf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) Build dependencies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) ==================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) Compiling requires having installed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * clang >= version 3.4.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * llvm >= version 3.7.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) Note that LLVM's tool 'llc' must support target 'bpf', list version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) and supported targets with command: ``llc --version``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) Clean and configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) -----------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) It can be needed to clean tools, samples or kernel before trying new arch or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) after some changes (on demand)::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) make -C tools clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) make -C samples/bpf clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) make clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) Configure kernel, defconfig for instance::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) make defconfig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) Kernel headers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) --------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) There are usually dependencies to header files of the current kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) To avoid installing devel kernel headers system wide, as a normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) user, simply call::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) make headers_install
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) This will creates a local "usr/include" directory in the git/build top
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) level directory, that the make system automatically pickup first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) Compiling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) =========
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) For building the BPF samples, issue the below command from the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) top level directory::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) make M=samples/bpf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) It is also possible to call make from this directory. This will just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) hide the invocation of make as above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) Manually compiling LLVM with 'bpf' support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) Since version 3.7.0, LLVM adds a proper LLVM backend target for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) BPF bytecode architecture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) By default llvm will build all non-experimental backends including bpf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) To generate a smaller llc binary one can use::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) -DLLVM_TARGETS_TO_BUILD="BPF"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) Quick sniplet for manually compiling LLVM and clang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) (build dependencies are cmake and gcc-c++)::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) $ git clone http://llvm.org/git/llvm.git
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) $ cd llvm/tools
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) $ git clone --depth 1 http://llvm.org/git/clang.git
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) $ cd ..; mkdir build; cd build
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) $ cmake .. -DLLVM_TARGETS_TO_BUILD="BPF;X86"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) $ make -j $(getconf _NPROCESSORS_ONLN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) It is also possible to point make to the newly compiled 'llc' or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) 'clang' command via redefining LLC or CLANG on the make command line::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) make M=samples/bpf LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) Cross compiling samples
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) -----------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) In order to cross-compile, say for arm64 targets, export CROSS_COMPILE and ARCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) environment variables before calling make. But do this before clean,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) cofiguration and header install steps described above. This will direct make to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) build samples for the cross target::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) export ARCH=arm64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) export CROSS_COMPILE="aarch64-linux-gnu-"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) Headers can be also installed on RFS of target board if need to keep them in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) sync (not necessarily and it creates a local "usr/include" directory also)::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) make INSTALL_HDR_PATH=~/some_sysroot/usr headers_install
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) Pointing LLC and CLANG is not necessarily if it's installed on HOST and have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) in its targets appropriate arm64 arch (usually it has several arches).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) Build samples::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) make M=samples/bpf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) Or build samples with SYSROOT if some header or library is absent in toolchain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) say libelf, providing address to file system containing headers and libs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) can be RFS of target board::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) make M=samples/bpf SYSROOT=~/some_sysroot