1. 24 4月, 2021 2 次提交
  2. 05 4月, 2021 1 次提交
  3. 18 3月, 2021 1 次提交
    • A
      libbpf: provide NULL and KERNEL_VERSION macros in bpf_helpers.h · 9ae2c26e
      Andrii Nakryiko 提交于
      Given that vmlinux.h is not compatible with headers like stddef.h, NULL poses
      an annoying problem: it is defined as #define, so is not captured in BTF, so
      is not emitted into vmlinux.h. This leads to users either sticking to explicit
      0, or defining their own NULL (as progs/skb_pkt_end.c does).
      
      But it's easy for bpf_helpers.h to provide (conditionally) NULL definition.
      Similarly, KERNEL_VERSION is another commonly missed macro that came up
      multiple times. So this patch adds both of them, along with offsetof(), that
      also is typically defined in stddef.h, just like NULL.
      
      This might cause compilation warning for existing BPF applications defining
      their own NULL and/or KERNEL_VERSION already:
      
        progs/skb_pkt_end.c:7:9: warning: 'NULL' macro redefined [-Wmacro-redefined]
        #define NULL 0
                ^
        /tmp/linux/tools/testing/selftests/bpf/tools/include/vmlinux.h:4:9: note: previous definition is here
        #define NULL ((void *)0)
      	  ^
      
      It is trivial to fix, though, so long-term benefits outweight temporary
      inconveniences.
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20210317200510.1354627-2-andrii@kernel.orgSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      9ae2c26e
  4. 16 3月, 2021 1 次提交
  5. 14 1月, 2021 1 次提交
  6. 22 10月, 2020 1 次提交
    • D
      bpf, libbpf: Guard bpf inline asm from bpf_tail_call_static · 3652c9a1
      Daniel Borkmann 提交于
      Yaniv reported a compilation error after pulling latest libbpf:
      
        [...]
        ../libbpf/src/root/usr/include/bpf/bpf_helpers.h:99:10: error:
        unknown register name 'r0' in asm
                           : "r0", "r1", "r2", "r3", "r4", "r5");
        [...]
      
      The issue got triggered given Yaniv was compiling tracing programs with native
      target (e.g. x86) instead of BPF target, hence no BTF generated vmlinux.h nor
      CO-RE used, and later llc with -march=bpf was invoked to compile from LLVM IR
      to BPF object file. Given that clang was expecting x86 inline asm and not BPF
      one the error complained that these regs don't exist on the former.
      
      Guard bpf_tail_call_static() with defined(__bpf__) where BPF inline asm is valid
      to use. BPF tracing programs on more modern kernels use BPF target anyway and
      thus the bpf_tail_call_static() function will be available for them. BPF inline
      asm is supported since clang 7 (clang <= 6 otherwise throws same above error),
      and __bpf_unreachable() since clang 8, therefore include the latter condition
      in order to prevent compilation errors for older clang versions. Given even an
      old Ubuntu 18.04 LTS has official LLVM packages all the way up to llvm-10, I did
      not bother to special case the __bpf_unreachable() inside bpf_tail_call_static()
      further.
      
      Also, undo the sockex3_kern's use of bpf_tail_call_static() sample given they
      still have the old hacky way to even compile networking progs with native instead
      of BPF target so bpf_tail_call_static() won't be defined there anymore.
      
      Fixes: 0e9f6841 ("bpf, libbpf: Add bpf_tail_call_static helper for bpf programs")
      Reported-by: NYaniv Agman <yanivagman@gmail.com>
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: NAndrii Nakryiko <andrii@kernel.org>
      Acked-by: NYonghong Song <yhs@fb.com>
      Tested-by: NYaniv Agman <yanivagman@gmail.com>
      Link: https://lore.kernel.org/bpf/CAMy7=ZUk08w5Gc2Z-EKi4JFtuUCaZYmE4yzhJjrExXpYKR4L8w@mail.gmail.com
      Link: https://lore.kernel.org/bpf/20201021203257.26223-1-daniel@iogearbox.net
      3652c9a1
  7. 01 10月, 2020 1 次提交
  8. 22 8月, 2020 1 次提交
  9. 11 8月, 2020 1 次提交
  10. 22 7月, 2020 1 次提交
  11. 23 6月, 2020 1 次提交
    • A
      libbpf: Add support for extracting kernel symbol addresses · 1c0c7074
      Andrii Nakryiko 提交于
      Add support for another (in addition to existing Kconfig) special kind of
      externs in BPF code, kernel symbol externs. Such externs allow BPF code to
      "know" kernel symbol address and either use it for comparisons with kernel
      data structures (e.g., struct file's f_op pointer, to distinguish different
      kinds of file), or, with the help of bpf_probe_user_kernel(), to follow
      pointers and read data from global variables. Kernel symbol addresses are
      found through /proc/kallsyms, which should be present in the system.
      
      Currently, such kernel symbol variables are typeless: they have to be defined
      as `extern const void <symbol>` and the only operation you can do (in C code)
      with them is to take its address. Such extern should reside in a special
      section '.ksyms'. bpf_helpers.h header provides __ksym macro for this. Strong
      vs weak semantics stays the same as with Kconfig externs. If symbol is not
      found in /proc/kallsyms, this will be a failure for strong (non-weak) extern,
      but will be defaulted to 0 for weak externs.
      
      If the same symbol is defined multiple times in /proc/kallsyms, then it will
      be error if any of the associated addresses differs. In that case, address is
      ambiguous, so libbpf falls on the side of caution, rather than confusing user
      with randomly chosen address.
      
      In the future, once kernel is extended with variables BTF information, such
      ksym externs will be supported in a typed version, which will allow BPF
      program to read variable's contents directly, similarly to how it's done for
      fentry/fexit input arguments.
      Signed-off-by: NAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Reviewed-by: NHao Luo <haoluo@google.com>
      Link: https://lore.kernel.org/bpf/20200619231703.738941-3-andriin@fb.com
      1c0c7074
  12. 10 5月, 2020 1 次提交
  13. 29 4月, 2020 1 次提交
    • A
      libbpf: Add BTF-defined map-in-map support · 646f02ff
      Andrii Nakryiko 提交于
      As discussed at LPC 2019 ([0]), this patch brings (a quite belated) support
      for declarative BTF-defined map-in-map support in libbpf. It allows to define
      ARRAY_OF_MAPS and HASH_OF_MAPS BPF maps without any user-space initialization
      code involved.
      
      Additionally, it allows to initialize outer map's slots with references to
      respective inner maps at load time, also completely declaratively.
      
      Despite a weak type system of C, the way BTF-defined map-in-map definition
      works, it's actually quite hard to accidentally initialize outer map with
      incompatible inner maps. This being C, of course, it's still possible, but
      even that would be caught at load time and error returned with helpful debug
      log pointing exactly to the slot that failed to be initialized.
      
      As an example, here's a rather advanced HASH_OF_MAPS declaration and
      initialization example, filling slots #0 and #4 with two inner maps:
      
        #include <bpf/bpf_helpers.h>
      
        struct inner_map {
                __uint(type, BPF_MAP_TYPE_ARRAY);
                __uint(max_entries, 1);
                __type(key, int);
                __type(value, int);
        } inner_map1 SEC(".maps"),
          inner_map2 SEC(".maps");
      
        struct outer_hash {
                __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
                __uint(max_entries, 5);
                __uint(key_size, sizeof(int));
                __array(values, struct inner_map);
        } outer_hash SEC(".maps") = {
                .values = {
                        [0] = &inner_map2,
                        [4] = &inner_map1,
                },
        };
      
      Here's the relevant part of libbpf debug log showing pretty clearly of what's
      going on with map-in-map initialization:
      
        libbpf: .maps relo #0: for 6 value 0 rel.r_offset 96 name 260 ('inner_map1')
        libbpf: .maps relo #0: map 'outer_arr' slot [0] points to map 'inner_map1'
        libbpf: .maps relo #1: for 7 value 32 rel.r_offset 112 name 249 ('inner_map2')
        libbpf: .maps relo #1: map 'outer_arr' slot [2] points to map 'inner_map2'
        libbpf: .maps relo #2: for 7 value 32 rel.r_offset 144 name 249 ('inner_map2')
        libbpf: .maps relo #2: map 'outer_hash' slot [0] points to map 'inner_map2'
        libbpf: .maps relo #3: for 6 value 0 rel.r_offset 176 name 260 ('inner_map1')
        libbpf: .maps relo #3: map 'outer_hash' slot [4] points to map 'inner_map1'
        libbpf: map 'inner_map1': created successfully, fd=4
        libbpf: map 'inner_map2': created successfully, fd=5
        libbpf: map 'outer_hash': created successfully, fd=7
        libbpf: map 'outer_hash': slot [0] set to map 'inner_map2' fd=5
        libbpf: map 'outer_hash': slot [4] set to map 'inner_map1' fd=4
      
      Notice from the log above that fd=6 (not logged explicitly) is used for inner
      "prototype" map, necessary for creation of outer map. It is destroyed
      immediately after outer map is created.
      
      See also included selftest with some extra comments explaining extra details
      of usage. Additionally, similar initialization syntax and libbpf functionality
      can be used to do initialization of BPF_PROG_ARRAY with references to BPF
      sub-programs. This can be done in follow up patches, if there will be a demand
      for this.
      
        [0] https://linuxplumbersconf.org/event/4/contributions/448/Signed-off-by: NAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Acked-by: NToke Høiland-Jørgensen <toke@redhat.com>
      Link: https://lore.kernel.org/bpf/20200429002739.48006-4-andriin@fb.com
      646f02ff
  14. 26 4月, 2020 1 次提交
  15. 17 1月, 2020 1 次提交
  16. 10 1月, 2020 1 次提交
  17. 19 12月, 2019 1 次提交
  18. 16 12月, 2019 1 次提交
    • A
      libbpf: Support libbpf-provided extern variables · 166750bc
      Andrii Nakryiko 提交于
      Add support for extern variables, provided to BPF program by libbpf. Currently
      the following extern variables are supported:
        - LINUX_KERNEL_VERSION; version of a kernel in which BPF program is
          executing, follows KERNEL_VERSION() macro convention, can be 4- and 8-byte
          long;
        - CONFIG_xxx values; a set of values of actual kernel config. Tristate,
          boolean, strings, and integer values are supported.
      
      Set of possible values is determined by declared type of extern variable.
      Supported types of variables are:
      - Tristate values. Are represented as `enum libbpf_tristate`. Accepted values
        are **strictly** 'y', 'n', or 'm', which are represented as TRI_YES, TRI_NO,
        or TRI_MODULE, respectively.
      - Boolean values. Are represented as bool (_Bool) types. Accepted values are
        'y' and 'n' only, turning into true/false values, respectively.
      - Single-character values. Can be used both as a substritute for
        bool/tristate, or as a small-range integer:
        - 'y'/'n'/'m' are represented as is, as characters 'y', 'n', or 'm';
        - integers in a range [-128, 127] or [0, 255] (depending on signedness of
          char in target architecture) are recognized and represented with
          respective values of char type.
      - Strings. String values are declared as fixed-length char arrays. String of
        up to that length will be accepted and put in first N bytes of char array,
        with the rest of bytes zeroed out. If config string value is longer than
        space alloted, it will be truncated and warning message emitted. Char array
        is always zero terminated. String literals in config have to be enclosed in
        double quotes, just like C-style string literals.
      - Integers. 8-, 16-, 32-, and 64-bit integers are supported, both signed and
        unsigned variants. Libbpf enforces parsed config value to be in the
        supported range of corresponding integer type. Integers values in config can
        be:
        - decimal integers, with optional + and - signs;
        - hexadecimal integers, prefixed with 0x or 0X;
        - octal integers, starting with 0.
      
      Config file itself is searched in /boot/config-$(uname -r) location with
      fallback to /proc/config.gz, unless config path is specified explicitly
      through bpf_object_open_opts' kernel_config_path option. Both gzipped and
      plain text formats are supported. Libbpf adds explicit dependency on zlib
      because of this, but this shouldn't be a problem, given libelf already depends
      on zlib.
      
      All detected extern variables, are put into a separate .extern internal map.
      It, similarly to .rodata map, is marked as read-only from BPF program side, as
      well as is frozen on load. This allows BPF verifier to track extern values as
      constants and perform enhanced branch prediction and dead code elimination.
      This can be relied upon for doing kernel version/feature detection and using
      potentially unsupported field relocations or BPF helpers in a CO-RE-based BPF
      program, while still having a single version of BPF program running on old and
      new kernels. Selftests are validating this explicitly for unexisting BPF
      helper.
      Signed-off-by: NAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191214014710.3449601-3-andriin@fb.com
      166750bc
  19. 25 11月, 2019 1 次提交
  20. 16 11月, 2019 1 次提交
  21. 03 11月, 2019 1 次提交
    • T
      libbpf: Add auto-pinning of maps when loading BPF objects · 57a00f41
      Toke Høiland-Jørgensen 提交于
      This adds support to libbpf for setting map pinning information as part of
      the BTF map declaration, to get automatic map pinning (and reuse) on load.
      The pinning type currently only supports a single PIN_BY_NAME mode, where
      each map will be pinned by its name in a path that can be overridden, but
      defaults to /sys/fs/bpf.
      
      Since auto-pinning only does something if any maps actually have a
      'pinning' BTF attribute set, we default the new option to enabled, on the
      assumption that seamless pinning is what most callers want.
      
      When a map has a pin_path set at load time, libbpf will compare the map
      pinned at that location (if any), and if the attributes match, will re-use
      that map instead of creating a new one. If no existing map is found, the
      newly created map will instead be pinned at the location.
      
      Programs wanting to customise the pinning can override the pinning paths
      using bpf_map__set_pin_path() before calling bpf_object__load() (including
      setting it to NULL to disable pinning of a particular map).
      Signed-off-by: NToke Høiland-Jørgensen <toke@redhat.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Acked-by: NAndrii Nakryiko <andriin@fb.com>
      Link: https://lore.kernel.org/bpf/157269298092.394725.3966306029218559681.stgit@toke.dk
      57a00f41
  22. 09 10月, 2019 6 次提交
  23. 07 10月, 2019 1 次提交
  24. 06 10月, 2019 1 次提交
  25. 16 9月, 2019 1 次提交
    • I
      selftests/bpf: add bpf-gcc support · 4ce150b6
      Ilya Leoshkevich 提交于
      Now that binutils and gcc support for BPF is upstream, make use of it in
      BPF selftests using alu32-like approach. Share as much as possible of
      CFLAGS calculation with clang.
      
      Fixes only obvious issues, leaving more complex ones for later:
      - Use gcc-provided bpf-helpers.h instead of manually defining the
        helpers, change bpf_helpers.h include guard to avoid conflict.
      - Include <linux/stddef.h> for __always_inline.
      - Add $(OUTPUT)/../usr/include to include path in order to use local
        kernel headers instead of system kernel headers when building with O=.
      
      In order to activate the bpf-gcc support, one needs to configure
      binutils and gcc with --target=bpf and make them available in $PATH. In
      particular, gcc must be installed as `bpf-gcc`, which is the default.
      
      Right now with binutils 25a2915e8dba and gcc r275589 only a handful of
      tests work:
      
      	# ./test_progs_bpf_gcc
      	# Summary: 7/39 PASSED, 1 SKIPPED, 98 FAILED
      
      The reason for those failures are as follows:
      
      - Build errors:
        - `error: too many function arguments for eBPF` for __always_inline
          functions read_str_var and read_map_var - must be inlining issue,
          and for process_l3_headers_v6, which relies on optimizing away
          function arguments.
        - `error: indirect call in function, which are not supported by eBPF`
          where there are no obvious indirect calls in the source calls, e.g.
          in __encap_ipip_none.
        - `error: field 'lock' has incomplete type` for fields of `struct
          bpf_spin_lock` type - bpf_spin_lock is re#defined by bpf-helpers.h,
          so its usage is sensitive to order of #includes.
        - `error: eBPF stack limit exceeded` in sysctl_tcp_mem.
      - Load errors:
        - Missing object files due to above build errors.
        - `libbpf: failed to create map (name: 'test_ver.bss')`.
        - `libbpf: object file doesn't contain bpf program`.
        - `libbpf: Program '.text' contains unrecognized relo data pointing to
          section 0`.
        - `libbpf: BTF is required, but is missing or corrupted` - no BTF
          support in gcc yet.
      Signed-off-by: NIlya Leoshkevich <iii@linux.ibm.com>
      Cc: Jose E. Marchesi <jose.marchesi@oracle.com>
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      4ce150b6
  26. 18 8月, 2019 1 次提交
  27. 08 8月, 2019 1 次提交
  28. 31 7月, 2019 1 次提交
  29. 12 7月, 2019 3 次提交
  30. 06 7月, 2019 1 次提交
  31. 11 6月, 2019 1 次提交
  32. 25 5月, 2019 1 次提交