Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 1 | eBPF sample programs |
| 2 | ==================== |
| 3 | |
Joe Stringer | 43371c8 | 2016-12-14 14:43:39 -0800 | [diff] [blame] | 4 | This directory contains a test stubs, verifier test-suite and examples |
| 5 | for using eBPF. The examples use libbpf from tools/lib/bpf. |
Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 6 | |
| 7 | Build dependencies |
| 8 | ================== |
| 9 | |
| 10 | Compiling requires having installed: |
| 11 | * clang >= version 3.4.0 |
| 12 | * llvm >= version 3.7.1 |
| 13 | |
| 14 | Note that LLVM's tool 'llc' must support target 'bpf', list version |
| 15 | and supported targets with command: ``llc --version`` |
| 16 | |
Ivan Khoronzhuk | 1600c9c | 2019-10-11 03:28:08 +0300 | [diff] [blame] | 17 | Clean and configuration |
| 18 | ----------------------- |
| 19 | |
| 20 | It can be needed to clean tools, samples or kernel before trying new arch or |
| 21 | after some changes (on demand):: |
| 22 | |
| 23 | make -C tools clean |
| 24 | make -C samples/bpf clean |
| 25 | make clean |
| 26 | |
| 27 | Configure kernel, defconfig for instance:: |
| 28 | |
| 29 | make defconfig |
| 30 | |
Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 31 | Kernel headers |
| 32 | -------------- |
| 33 | |
| 34 | There are usually dependencies to header files of the current kernel. |
| 35 | To avoid installing devel kernel headers system wide, as a normal |
| 36 | user, simply call:: |
| 37 | |
| 38 | make headers_install |
| 39 | |
| 40 | This will creates a local "usr/include" directory in the git/build top |
| 41 | level directory, that the make system automatically pickup first. |
| 42 | |
| 43 | Compiling |
| 44 | ========= |
| 45 | |
| 46 | For building the BPF samples, issue the below command from the kernel |
| 47 | top level directory:: |
| 48 | |
Jesper Dangaard Brouer | 6cc2c87 | 2019-11-10 17:31:16 +0100 | [diff] [blame] | 49 | make M=samples/bpf |
Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 50 | |
Jesper Dangaard Brouer | b62a796 | 2016-04-28 14:21:09 +0200 | [diff] [blame] | 51 | It is also possible to call make from this directory. This will just |
Jesper Dangaard Brouer | 6cc2c87 | 2019-11-10 17:31:16 +0100 | [diff] [blame] | 52 | hide the invocation of make as above. |
Jesper Dangaard Brouer | b62a796 | 2016-04-28 14:21:09 +0200 | [diff] [blame] | 53 | |
Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 54 | Manually compiling LLVM with 'bpf' support |
| 55 | ------------------------------------------ |
| 56 | |
| 57 | Since version 3.7.0, LLVM adds a proper LLVM backend target for the |
| 58 | BPF bytecode architecture. |
| 59 | |
| 60 | By default llvm will build all non-experimental backends including bpf. |
| 61 | To generate a smaller llc binary one can use:: |
| 62 | |
| 63 | -DLLVM_TARGETS_TO_BUILD="BPF" |
| 64 | |
Tiezhu Yang | 628add7 | 2021-01-22 09:39:44 +0800 | [diff] [blame] | 65 | We recommend that developers who want the fastest incremental builds |
| 66 | use the Ninja build system, you can find it in your system's package |
| 67 | manager, usually the package is ninja or ninja-build. |
Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 68 | |
Tiezhu Yang | 628add7 | 2021-01-22 09:39:44 +0800 | [diff] [blame] | 69 | Quick sniplet for manually compiling LLVM and clang |
| 70 | (build dependencies are ninja, cmake and gcc-c++):: |
| 71 | |
| 72 | $ git clone https://github.com/llvm/llvm-project.git |
| 73 | $ mkdir -p llvm-project/llvm/build |
| 74 | $ cd llvm-project/llvm/build |
| 75 | $ cmake .. -G "Ninja" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \ |
| 76 | -DLLVM_ENABLE_PROJECTS="clang" \ |
| 77 | -DCMAKE_BUILD_TYPE=Release \ |
| 78 | -DLLVM_BUILD_RUNTIME=OFF |
| 79 | $ ninja |
Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 80 | |
Jesper Dangaard Brouer | bdefbbf | 2016-04-28 14:21:14 +0200 | [diff] [blame] | 81 | It is also possible to point make to the newly compiled 'llc' or |
| 82 | 'clang' command via redefining LLC or CLANG on the make command line:: |
Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 83 | |
Tiezhu Yang | 628add7 | 2021-01-22 09:39:44 +0800 | [diff] [blame] | 84 | make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang |
Joel Fernandes | 8bf2ac2 | 2017-09-20 09:11:59 -0700 | [diff] [blame] | 85 | |
| 86 | Cross compiling samples |
| 87 | ----------------------- |
| 88 | In order to cross-compile, say for arm64 targets, export CROSS_COMPILE and ARCH |
Ivan Khoronzhuk | 1600c9c | 2019-10-11 03:28:08 +0300 | [diff] [blame] | 89 | environment variables before calling make. But do this before clean, |
| 90 | cofiguration and header install steps described above. This will direct make to |
| 91 | build samples for the cross target:: |
Joel Fernandes | 8bf2ac2 | 2017-09-20 09:11:59 -0700 | [diff] [blame] | 92 | |
Ivan Khoronzhuk | 1600c9c | 2019-10-11 03:28:08 +0300 | [diff] [blame] | 93 | export ARCH=arm64 |
| 94 | export CROSS_COMPILE="aarch64-linux-gnu-" |
| 95 | |
| 96 | Headers can be also installed on RFS of target board if need to keep them in |
| 97 | sync (not necessarily and it creates a local "usr/include" directory also):: |
| 98 | |
| 99 | make INSTALL_HDR_PATH=~/some_sysroot/usr headers_install |
| 100 | |
| 101 | Pointing LLC and CLANG is not necessarily if it's installed on HOST and have |
| 102 | in its targets appropriate arm64 arch (usually it has several arches). |
| 103 | Build samples:: |
| 104 | |
Jesper Dangaard Brouer | 6cc2c87 | 2019-11-10 17:31:16 +0100 | [diff] [blame] | 105 | make M=samples/bpf |
Ivan Khoronzhuk | 1600c9c | 2019-10-11 03:28:08 +0300 | [diff] [blame] | 106 | |
| 107 | Or build samples with SYSROOT if some header or library is absent in toolchain, |
| 108 | say libelf, providing address to file system containing headers and libs, |
| 109 | can be RFS of target board:: |
| 110 | |
Jesper Dangaard Brouer | 6cc2c87 | 2019-11-10 17:31:16 +0100 | [diff] [blame] | 111 | make M=samples/bpf SYSROOT=~/some_sysroot |