README.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. eBPF sample programs
  2. ====================
  3. This directory contains a test stubs, verifier test-suite and examples
  4. for using eBPF. The examples use libbpf from tools/lib/bpf.
  5. Note that the XDP-specific samples have been removed from this directory and
  6. moved to the xdp-tools repository: https://github.com/xdp-project/xdp-tools
  7. See the commit messages removing each tool from this directory for how to
  8. convert specific command invocations between the old samples and the utilities
  9. in xdp-tools.
  10. Build dependencies
  11. ==================
  12. Compiling requires having installed:
  13. * clang
  14. * llvm
  15. * pahole
  16. Consult :ref:`Documentation/process/changes.rst <changes>` for the minimum
  17. version numbers required and how to update them. Note that LLVM's tool
  18. 'llc' must support target 'bpf', list version and supported targets with
  19. command: ``llc --version``
  20. Clean and configuration
  21. -----------------------
  22. It can be needed to clean tools, samples or kernel before trying new arch or
  23. after some changes (on demand)::
  24. make -C tools clean
  25. make -C samples/bpf clean
  26. make clean
  27. Configure kernel, defconfig for instance
  28. (see "tools/testing/selftests/bpf/config" for a reference config)::
  29. make defconfig
  30. Kernel headers
  31. --------------
  32. There are usually dependencies to header files of the current kernel.
  33. To avoid installing devel kernel headers system wide, as a normal
  34. user, simply call::
  35. make headers_install
  36. This will create a local "usr/include" directory in the git/build top
  37. level directory, that the make system will automatically pick up first.
  38. Compiling
  39. =========
  40. For building the BPF samples, issue the below command from the kernel
  41. top level directory::
  42. make M=samples/bpf
  43. It is also possible to call make from this directory. This will just
  44. hide the invocation of make as above.
  45. Manually compiling LLVM with 'bpf' support
  46. ------------------------------------------
  47. Since version 3.7.0, LLVM adds a proper LLVM backend target for the
  48. BPF bytecode architecture.
  49. By default llvm will build all non-experimental backends including bpf.
  50. To generate a smaller llc binary one can use::
  51. -DLLVM_TARGETS_TO_BUILD="BPF"
  52. We recommend that developers who want the fastest incremental builds
  53. use the Ninja build system, you can find it in your system's package
  54. manager, usually the package is ninja or ninja-build.
  55. Quick sniplet for manually compiling LLVM and clang
  56. (build dependencies are ninja, cmake and gcc-c++)::
  57. $ git clone https://github.com/llvm/llvm-project.git
  58. $ mkdir -p llvm-project/llvm/build
  59. $ cd llvm-project/llvm/build
  60. $ cmake .. -G "Ninja" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
  61. -DLLVM_ENABLE_PROJECTS="clang" \
  62. -DCMAKE_BUILD_TYPE=Release \
  63. -DLLVM_BUILD_RUNTIME=OFF
  64. $ ninja
  65. It is also possible to point make to the newly compiled 'llc' or
  66. 'clang' command via redefining LLC or CLANG on the make command line::
  67. make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang
  68. Cross compiling samples
  69. -----------------------
  70. In order to cross-compile, say for arm64 targets, export CROSS_COMPILE and ARCH
  71. environment variables before calling make. But do this before clean,
  72. configuration and header install steps described above. This will direct make to
  73. build samples for the cross target::
  74. export ARCH=arm64
  75. export CROSS_COMPILE="aarch64-linux-gnu-"
  76. Headers can be also installed on RFS of target board if need to keep them in
  77. sync (not necessarily and it creates a local "usr/include" directory also)::
  78. make INSTALL_HDR_PATH=~/some_sysroot/usr headers_install
  79. Pointing LLC and CLANG is not necessarily if it's installed on HOST and have
  80. in its targets appropriate arm64 arch (usually it has several arches).
  81. Build samples::
  82. make M=samples/bpf
  83. Or build samples with SYSROOT if some header or library is absent in toolchain,
  84. say libelf, providing address to file system containing headers and libs,
  85. can be RFS of target board::
  86. make M=samples/bpf SYSROOT=~/some_sysroot