blob: cc1f00a1ee060d438cc90171934e88e4193fe6be [file] [log] [blame]
Jesper Dangaard Brouer1c975662016-04-28 14:21:04 +02001eBPF sample programs
2====================
3
Joe Stringer43371c82016-12-14 14:43:39 -08004This directory contains a test stubs, verifier test-suite and examples
5for using eBPF. The examples use libbpf from tools/lib/bpf.
Jesper Dangaard Brouer1c975662016-04-28 14:21:04 +02006
7Build dependencies
8==================
9
10Compiling requires having installed:
11 * clang >= version 3.4.0
12 * llvm >= version 3.7.1
13
14Note that LLVM's tool 'llc' must support target 'bpf', list version
15and supported targets with command: ``llc --version``
16
Ivan Khoronzhuk1600c9c2019-10-11 03:28:08 +030017Clean and configuration
18-----------------------
19
20It can be needed to clean tools, samples or kernel before trying new arch or
21after some changes (on demand)::
22
23 make -C tools clean
24 make -C samples/bpf clean
25 make clean
26
27Configure kernel, defconfig for instance::
28
29 make defconfig
30
Jesper Dangaard Brouer1c975662016-04-28 14:21:04 +020031Kernel headers
32--------------
33
34There are usually dependencies to header files of the current kernel.
35To avoid installing devel kernel headers system wide, as a normal
36user, simply call::
37
38 make headers_install
39
40This will creates a local "usr/include" directory in the git/build top
41level directory, that the make system automatically pickup first.
42
43Compiling
44=========
45
46For building the BPF samples, issue the below command from the kernel
47top level directory::
48
49 make samples/bpf/
50
51Do notice the "/" slash after the directory name.
52
Jesper Dangaard Brouerb62a7962016-04-28 14:21:09 +020053It is also possible to call make from this directory. This will just
54hide the the invocation of make as above with the appended "/".
55
Jesper Dangaard Brouer1c975662016-04-28 14:21:04 +020056Manually compiling LLVM with 'bpf' support
57------------------------------------------
58
59Since version 3.7.0, LLVM adds a proper LLVM backend target for the
60BPF bytecode architecture.
61
62By default llvm will build all non-experimental backends including bpf.
63To generate a smaller llc binary one can use::
64
65 -DLLVM_TARGETS_TO_BUILD="BPF"
66
67Quick 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 Brouerbdefbbf2016-04-28 14:21:14 +020077It 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 Brouer1c975662016-04-28 14:21:04 +020079
Jesper Dangaard Brouerbdefbbf2016-04-28 14:21:14 +020080 make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
Joel Fernandes8bf2ac22017-09-20 09:11:59 -070081
82Cross compiling samples
83-----------------------
84In order to cross-compile, say for arm64 targets, export CROSS_COMPILE and ARCH
Ivan Khoronzhuk1600c9c2019-10-11 03:28:08 +030085environment variables before calling make. But do this before clean,
86cofiguration and header install steps described above. This will direct make to
87build samples for the cross target::
Joel Fernandes8bf2ac22017-09-20 09:11:59 -070088
Ivan Khoronzhuk1600c9c2019-10-11 03:28:08 +030089 export ARCH=arm64
90 export CROSS_COMPILE="aarch64-linux-gnu-"
91
92Headers can be also installed on RFS of target board if need to keep them in
93sync (not necessarily and it creates a local "usr/include" directory also)::
94
95 make INSTALL_HDR_PATH=~/some_sysroot/usr headers_install
96
97Pointing LLC and CLANG is not necessarily if it's installed on HOST and have
98in its targets appropriate arm64 arch (usually it has several arches).
99Build samples::
100
101 make samples/bpf/
102
103Or build samples with SYSROOT if some header or library is absent in toolchain,
104say libelf, providing address to file system containing headers and libs,
105can be RFS of target board::
106
107 make samples/bpf/ SYSROOT=~/some_sysroot