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