0001 eBPF sample programs
0002 ====================
0003
0004 This directory contains a test stubs, verifier test-suite and examples
0005 for using eBPF. The examples use libbpf from tools/lib/bpf.
0006
0007 Build dependencies
0008 ==================
0009
0010 Compiling requires having installed:
0011 * clang >= version 3.4.0
0012 * llvm >= version 3.7.1
0013
0014 Note that LLVM's tool 'llc' must support target 'bpf', list version
0015 and supported targets with command: ``llc --version``
0016
0017 Clean and configuration
0018 -----------------------
0019
0020 It can be needed to clean tools, samples or kernel before trying new arch or
0021 after some changes (on demand)::
0022
0023 make -C tools clean
0024 make -C samples/bpf clean
0025 make clean
0026
0027 Configure kernel, defconfig for instance::
0028
0029 make defconfig
0030
0031 Kernel headers
0032 --------------
0033
0034 There are usually dependencies to header files of the current kernel.
0035 To avoid installing devel kernel headers system wide, as a normal
0036 user, simply call::
0037
0038 make headers_install
0039
0040 This will creates a local "usr/include" directory in the git/build top
0041 level directory, that the make system automatically pickup first.
0042
0043 Compiling
0044 =========
0045
0046 For building the BPF samples, issue the below command from the kernel
0047 top level directory::
0048
0049 make M=samples/bpf
0050
0051 It is also possible to call make from this directory. This will just
0052 hide the invocation of make as above.
0053
0054 Manually compiling LLVM with 'bpf' support
0055 ------------------------------------------
0056
0057 Since version 3.7.0, LLVM adds a proper LLVM backend target for the
0058 BPF bytecode architecture.
0059
0060 By default llvm will build all non-experimental backends including bpf.
0061 To generate a smaller llc binary one can use::
0062
0063 -DLLVM_TARGETS_TO_BUILD="BPF"
0064
0065 We recommend that developers who want the fastest incremental builds
0066 use the Ninja build system, you can find it in your system's package
0067 manager, usually the package is ninja or ninja-build.
0068
0069 Quick sniplet for manually compiling LLVM and clang
0070 (build dependencies are ninja, cmake and gcc-c++)::
0071
0072 $ git clone https://github.com/llvm/llvm-project.git
0073 $ mkdir -p llvm-project/llvm/build
0074 $ cd llvm-project/llvm/build
0075 $ cmake .. -G "Ninja" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
0076 -DLLVM_ENABLE_PROJECTS="clang" \
0077 -DCMAKE_BUILD_TYPE=Release \
0078 -DLLVM_BUILD_RUNTIME=OFF
0079 $ ninja
0080
0081 It is also possible to point make to the newly compiled 'llc' or
0082 'clang' command via redefining LLC or CLANG on the make command line::
0083
0084 make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang
0085
0086 Cross compiling samples
0087 -----------------------
0088 In order to cross-compile, say for arm64 targets, export CROSS_COMPILE and ARCH
0089 environment variables before calling make. But do this before clean,
0090 cofiguration and header install steps described above. This will direct make to
0091 build samples for the cross target::
0092
0093 export ARCH=arm64
0094 export CROSS_COMPILE="aarch64-linux-gnu-"
0095
0096 Headers can be also installed on RFS of target board if need to keep them in
0097 sync (not necessarily and it creates a local "usr/include" directory also)::
0098
0099 make INSTALL_HDR_PATH=~/some_sysroot/usr headers_install
0100
0101 Pointing LLC and CLANG is not necessarily if it's installed on HOST and have
0102 in its targets appropriate arm64 arch (usually it has several arches).
0103 Build samples::
0104
0105 make M=samples/bpf
0106
0107 Or build samples with SYSROOT if some header or library is absent in toolchain,
0108 say libelf, providing address to file system containing headers and libs,
0109 can be RFS of target board::
0110
0111 make M=samples/bpf SYSROOT=~/some_sysroot