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 | |
| 49 | make samples/bpf/ |
| 50 | |
| 51 | Do notice the "/" slash after the directory name. |
| 52 | |
Jesper Dangaard Brouer | b62a796 | 2016-04-28 14:21:09 +0200 | [diff] [blame] | 53 | It is also possible to call make from this directory. This will just |
| 54 | hide the the invocation of make as above with the appended "/". |
| 55 | |
Jesper Dangaard Brouer | 1c97566 | 2016-04-28 14:21:04 +0200 | [diff] [blame] | 56 | Manually compiling LLVM with 'bpf' support |
| 57 | ------------------------------------------ |
| 58 | |
| 59 | Since version 3.7.0, LLVM adds a proper LLVM backend target for the |
| 60 | BPF bytecode architecture. |
| 61 | |
| 62 | By default llvm will build all non-experimental backends including bpf. |
| 63 | To generate a smaller llc binary one can use:: |
| 64 | |
| 65 | -DLLVM_TARGETS_TO_BUILD="BPF" |
| 66 | |
| 67 | Quick sniplet for manually compiling LLVM and clang |
| 68 | (build dependencies are cmake and gcc-c++):: |
| 69 | |
| 70 | $ git clone http://llvm.org/git/llvm.git |
| 71 | $ cd llvm/tools |
| 72 | $ git clone --depth 1 http://llvm.org/git/clang.git |
| 73 | $ cd ..; mkdir build; cd build |
| 74 | $ cmake .. -DLLVM_TARGETS_TO_BUILD="BPF;X86" |
| 75 | $ make -j $(getconf _NPROCESSORS_ONLN) |
| 76 | |
Jesper Dangaard Brouer | bdefbbf | 2016-04-28 14:21:14 +0200 | [diff] [blame] | 77 | It is also possible to point make to the newly compiled 'llc' or |
| 78 | '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] | 79 | |
Jesper Dangaard Brouer | bdefbbf | 2016-04-28 14:21:14 +0200 | [diff] [blame] | 80 | make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang |
Joel Fernandes | 8bf2ac2 | 2017-09-20 09:11:59 -0700 | [diff] [blame] | 81 | |
| 82 | Cross compiling samples |
| 83 | ----------------------- |
| 84 | 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^] | 85 | environment variables before calling make. But do this before clean, |
| 86 | cofiguration and header install steps described above. This will direct make to |
| 87 | build samples for the cross target:: |
Joel Fernandes | 8bf2ac2 | 2017-09-20 09:11:59 -0700 | [diff] [blame] | 88 | |
Ivan Khoronzhuk | 1600c9c | 2019-10-11 03:28:08 +0300 | [diff] [blame^] | 89 | export ARCH=arm64 |
| 90 | export CROSS_COMPILE="aarch64-linux-gnu-" |
| 91 | |
| 92 | Headers can be also installed on RFS of target board if need to keep them in |
| 93 | sync (not necessarily and it creates a local "usr/include" directory also):: |
| 94 | |
| 95 | make INSTALL_HDR_PATH=~/some_sysroot/usr headers_install |
| 96 | |
| 97 | Pointing LLC and CLANG is not necessarily if it's installed on HOST and have |
| 98 | in its targets appropriate arm64 arch (usually it has several arches). |
| 99 | Build samples:: |
| 100 | |
| 101 | make samples/bpf/ |
| 102 | |
| 103 | Or build samples with SYSROOT if some header or library is absent in toolchain, |
| 104 | say libelf, providing address to file system containing headers and libs, |
| 105 | can be RFS of target board:: |
| 106 | |
| 107 | make samples/bpf/ SYSROOT=~/some_sysroot |