1. 20 3月, 2019 1 次提交
  2. 12 3月, 2019 1 次提交
    • A
      tools lib bpf: Fix the build by adding a missing stdarg.h include · dfcbc2f2
      Arnaldo Carvalho de Melo 提交于
      The libbpf_print_fn_t typedef uses va_list without including the header
      where that type is defined, stdarg.h, breaking in places where we're
      unlucky for that type not to be already defined by some previously
      included header.
      
      Noticed while building on fedora 24 cross building tools/perf to the ARC
      architecture using the uClibc C library:
      
        28 fedora:24-x-ARC-uClibc   : FAIL arc-linux-gcc (ARCompact ISA Linux uClibc toolchain 2017.09-rc2) 7.1.1 20170710
      
          CC       /tmp/build/perf/tests/llvm.o
        In file included from tests/llvm.c:3:0:
        /git/linux/tools/lib/bpf/libbpf.h:57:20: error: unknown type name 'va_list'
              const char *, va_list ap);
                            ^~~~~~~
        /git/linux/tools/lib/bpf/libbpf.h:59:34: error: unknown type name 'libbpf_print_fn_t'
         LIBBPF_API void libbpf_set_print(libbpf_print_fn_t fn);
                                          ^~~~~~~~~~~~~~~~~
        mv: cannot stat '/tmp/build/perf/tests/.llvm.o.tmp': No such file or directory
      
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: Jakub Kicinski <jakub.kicinski@netronome.com>
      Cc: Jiri Olsa <jolsa@kernel.org>
      Cc: Namhyung Kim <namhyung@kernel.org>
      Cc: Quentin Monnet <quentin.monnet@netronome.com>
      Cc: Stanislav Fomichev <sdf@google.com>
      Cc: Yonghong Song <yhs@fb.com>
      Fixes: a8a1f7d0 ("libbpf: fix libbpf_print")
      Link: https://lkml.kernel.org/n/tip-5270n2quu2gqz22o7itfdx00@git.kernel.orgSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
      dfcbc2f2
  3. 01 3月, 2019 1 次提交
  4. 15 2月, 2019 2 次提交
    • A
      libbpf: Introduce bpf_object__btf · 789f6bab
      Andrey Ignatov 提交于
      Add new accessor for bpf_object to get opaque struct btf * from it.
      
      struct btf * is needed for all operations with BTF and it's present in
      bpf_object. The only thing missing is a way to get it.
      
      Example use-case is to get BTF key_type_id and value_type_id for a map in
      bpf_object. It can be done with btf__get_map_kv_tids() but that function
      requires struct btf *.
      
      Similar API can be added for struct btf_ext but no use-case for it yet.
      Signed-off-by: NAndrey Ignatov <rdna@fb.com>
      Acked-by: NYonghong Song <yhs@fb.com>
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      789f6bab
    • A
      libbpf: Introduce bpf_map__resize · 1a11a4c7
      Andrey Ignatov 提交于
      Add bpf_map__resize() to change max_entries for a map.
      
      Quite often necessary map size is unknown at compile time and can be
      calculated only at run time.
      
      Currently the following approach is used to do so:
      * bpf_object__open_buffer() to open Elf file from a buffer;
      * bpf_object__find_map_by_name() to find relevant map;
      * bpf_map__def() to get map attributes and create struct
        bpf_create_map_attr from them;
      * update max_entries in bpf_create_map_attr;
      * bpf_create_map_xattr() to create new map with updated max_entries;
      * bpf_map__reuse_fd() to replace the map in bpf_object with newly
        created one.
      
      And after all this bpf_object can finally be loaded. The map will have
      new size.
      
      It 1) is quite a lot of steps; 2) doesn't take BTF into account.
      
      For "2)" even more steps should be made and some of them require changes
      to libbpf (e.g. to get struct btf * from bpf_object).
      
      Instead the whole problem can be solved by introducing simple
      bpf_map__resize() API that checks the map and sets new max_entries if
      the map is not loaded yet.
      
      So the new steps are:
      * bpf_object__open_buffer() to open Elf file from a buffer;
      * bpf_object__find_map_by_name() to find relevant map;
      * bpf_map__resize() to update max_entries.
      
      That's much simpler and works with BTF.
      Signed-off-by: NAndrey Ignatov <rdna@fb.com>
      Acked-by: NYonghong Song <yhs@fb.com>
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      1a11a4c7
  5. 05 2月, 2019 3 次提交
    • S
      libbpf: fix libbpf_print · a8a1f7d0
      Stanislav Fomichev 提交于
      With the recent print rework we now have the following problem:
      pr_{warning,info,debug} expand to __pr which calls libbpf_print.
      libbpf_print does va_start and calls __libbpf_pr with va_list argument.
      In __base_pr we again do va_start. Because the next argument is a
      va_list, we don't get correct pointer to the argument (and print noting
      in my case, I don't know why it doesn't crash tbh).
      
      Fix this by changing libbpf_print_fn_t signature to accept va_list and
      remove unneeded calls to va_start in the existing users.
      
      Alternatively, this can we solved by exporting __libbpf_pr and
      changing __pr macro to (and killing libbpf_print):
      {
      	if (__libbpf_pr)
      		__libbpf_pr(level, "libbpf: " fmt, ##__VA_ARGS__)
      }
      Signed-off-by: NStanislav Fomichev <sdf@google.com>
      Acked-by: NYonghong Song <yhs@fb.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      a8a1f7d0
    • Y
      tools/bpf: simplify libbpf API function libbpf_set_print() · 6f1ae8b6
      Yonghong Song 提交于
      Currently, the libbpf API function libbpf_set_print()
      takes three function pointer parameters for warning, info
      and debug printout respectively.
      
      This patch changes the API to have just one function pointer
      parameter and the function pointer has one additional
      parameter "debugging level". So if in the future, if
      the debug level is increased, the function signature
      won't change.
      Signed-off-by: NYonghong Song <yhs@fb.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      6f1ae8b6
    • Y
      tools/bpf: move libbpf pr_* debug print functions to headers · 8461ef8b
      Yonghong Song 提交于
      A global function libbpf_print, which is invisible
      outside the shared library, is defined to print based
      on levels. The pr_warning, pr_info and pr_debug
      macros are moved into the newly created header
      common.h. So any .c file including common.h can
      use these macros directly.
      
      Currently btf__new and btf_ext__new API has an argument getting
      __pr_debug function pointer into btf.c so the debugging information
      can be printed there. This patch removed this parameter
      from btf__new and btf_ext__new and directly using pr_debug in btf.c.
      
      Another global function libbpf_print_level_available, also
      invisible outside the shared library, can test
      whether a particular level debug printing is
      available or not. It is used in btf.c to
      test whether DEBUG level debug printing is availabl or not,
      based on which the log buffer will be allocated when loading
      btf to the kernel.
      Signed-off-by: NYonghong Song <yhs@fb.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      8461ef8b
  6. 02 2月, 2019 2 次提交
  7. 23 1月, 2019 3 次提交
    • Q
      tools: bpftool: add probes for eBPF helper functions · 2d3ea5e8
      Quentin Monnet 提交于
      Similarly to what was done for program types and map types, add a set of
      probes to test the availability of the different eBPF helper functions
      on the current system.
      
      For each known program type, all known helpers are tested, in order to
      establish a compatibility matrix. Output is provided as a set of lists
      of available helpers, one per program type.
      
      Sample output:
      
          # bpftool feature probe kernel
          ...
          Scanning eBPF helper functions...
          eBPF helpers supported for program type socket_filter:
                  - bpf_map_lookup_elem
                  - bpf_map_update_elem
                  - bpf_map_delete_elem
          ...
          eBPF helpers supported for program type kprobe:
                  - bpf_map_lookup_elem
                  - bpf_map_update_elem
                  - bpf_map_delete_elem
          ...
      
          # bpftool --json --pretty feature probe kernel
          {
              ...
              "helpers": {
                  "socket_filter_available_helpers": ["bpf_map_lookup_elem", \
                          "bpf_map_update_elem","bpf_map_delete_elem", ...
                  ],
                  "kprobe_available_helpers": ["bpf_map_lookup_elem", \
                          "bpf_map_update_elem","bpf_map_delete_elem", ...
                  ],
                  ...
              }
          }
      
      v5:
      - In libbpf.map, move global symbol to the new LIBBPF_0.0.2 section.
      
      v4:
      - Use "enum bpf_func_id" instead of "__u32" in bpf_probe_helper()
        declaration for the type of the argument used to pass the id of
        the helper to probe.
      - Undef BPF_HELPER_MAKE_ENTRY after using it.
      
      v3:
      - Do not pass kernel version from bpftool to libbpf probes (kernel
        version for testing program with kprobes is retrieved directly from
        libbpf).
      - Dump one list of available helpers per program type (instead of one
        list of compatible program types per helper).
      
      v2:
      - Move probes from bpftool to libbpf.
      - Test all program types for each helper, print a list of working prog
        types for each helper.
      - Fall back on include/uapi/linux/bpf.h for names and ids of helpers.
      - Remove C-style macros output from this patch.
      Signed-off-by: NQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: NJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: NStanislav Fomichev <sdf@google.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      2d3ea5e8
    • Q
      tools: bpftool: add probes for eBPF map types · f99e1663
      Quentin Monnet 提交于
      Add new probes for eBPF map types, to detect what are the ones available
      on the system. Try creating one map of each type, and see if the kernel
      complains.
      
      Sample output:
      
          # bpftool feature probe kernel
          ...
          Scanning eBPF map types...
          eBPF map_type hash is available
          eBPF map_type array is available
          eBPF map_type prog_array is available
          ...
      
          # bpftool --json --pretty feature probe kernel
          {
              ...
              "map_types": {
                  "have_hash_map_type": true,
                  "have_array_map_type": true,
                  "have_prog_array_map_type": true,
                  ...
              }
          }
      
      v5:
      - In libbpf.map, move global symbol to the new LIBBPF_0.0.2 section.
      
      v3:
      - Use a switch with all enum values for setting specific map parameters,
        so that gcc complains at compile time (-Wswitch-enum) if new map types
        were added to the kernel but libbpf was not updated.
      
      v2:
      - Move probes from bpftool to libbpf.
      - Remove C-style macros output from this patch.
      Signed-off-by: NQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: NJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: NStanislav Fomichev <sdf@google.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      f99e1663
    • Q
      tools: bpftool: add probes for eBPF program types · 1bf4b058
      Quentin Monnet 提交于
      Introduce probes for supported BPF program types in libbpf, and call it
      from bpftool to test what types are available on the system. The probe
      simply consists in loading a very basic program of that type and see if
      the verifier complains or not.
      
      Sample output:
      
          # bpftool feature probe kernel
          ...
          Scanning eBPF program types...
          eBPF program_type socket_filter is available
          eBPF program_type kprobe is available
          eBPF program_type sched_cls is available
          ...
      
          # bpftool --json --pretty feature probe kernel
          {
              ...
              "program_types": {
                  "have_socket_filter_prog_type": true,
                  "have_kprobe_prog_type": true,
                  "have_sched_cls_prog_type": true,
                  ...
              }
          }
      
      v5:
      - In libbpf.map, move global symbol to a new LIBBPF_0.0.2 section.
      - Rename (non-API function) prog_load() as probe_load().
      
      v3:
      - Get kernel version for checking kprobes availability from libbpf
        instead of from bpftool. Do not pass kernel_version as an argument
        when calling libbpf probes.
      - Use a switch with all enum values for setting specific program
        parameters just before probing, so that gcc complains at compile time
        (-Wswitch-enum) if new prog types were added to the kernel but libbpf
        was not updated.
      - Add a comment in libbpf.h about setrlimit() usage to allow many
        consecutive probe attempts.
      
      v2:
      - Move probes from bpftool to libbpf.
      - Remove C-style macros output from this patch.
      Signed-off-by: NQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: NJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: NStanislav Fomichev <sdf@google.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      1bf4b058
  8. 10 12月, 2018 1 次提交
    • M
      bpf: libbpf: bpftool: Print bpf_line_info during prog dump · b053b439
      Martin KaFai Lau 提交于
      This patch adds print bpf_line_info function in 'prog dump jitted'
      and 'prog dump xlated':
      
      [root@arch-fb-vm1 bpf]# ~/devshare/fb-kernel/linux/tools/bpf/bpftool/bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv
      [...]
      int test_long_fname_2(struct dummy_tracepoint_args * arg):
      bpf_prog_44a040bf25481309_test_long_fname_2:
      ; static int test_long_fname_2(struct dummy_tracepoint_args *arg)
         0:	push   %rbp
         1:	mov    %rsp,%rbp
         4:	sub    $0x30,%rsp
         b:	sub    $0x28,%rbp
         f:	mov    %rbx,0x0(%rbp)
        13:	mov    %r13,0x8(%rbp)
        17:	mov    %r14,0x10(%rbp)
        1b:	mov    %r15,0x18(%rbp)
        1f:	xor    %eax,%eax
        21:	mov    %rax,0x20(%rbp)
        25:	xor    %esi,%esi
      ; int key = 0;
        27:	mov    %esi,-0x4(%rbp)
      ; if (!arg->sock)
        2a:	mov    0x8(%rdi),%rdi
      ; if (!arg->sock)
        2e:	cmp    $0x0,%rdi
        32:	je     0x0000000000000070
        34:	mov    %rbp,%rsi
      ; counts = bpf_map_lookup_elem(&btf_map, &key);
        37:	add    $0xfffffffffffffffc,%rsi
        3b:	movabs $0xffff8881139d7480,%rdi
        45:	add    $0x110,%rdi
        4c:	mov    0x0(%rsi),%eax
        4f:	cmp    $0x4,%rax
        53:	jae    0x000000000000005e
        55:	shl    $0x3,%rax
        59:	add    %rdi,%rax
        5c:	jmp    0x0000000000000060
        5e:	xor    %eax,%eax
      ; if (!counts)
        60:	cmp    $0x0,%rax
        64:	je     0x0000000000000070
      ; counts->v6++;
        66:	mov    0x4(%rax),%edi
        69:	add    $0x1,%rdi
        6d:	mov    %edi,0x4(%rax)
        70:	mov    0x0(%rbp),%rbx
        74:	mov    0x8(%rbp),%r13
        78:	mov    0x10(%rbp),%r14
        7c:	mov    0x18(%rbp),%r15
        80:	add    $0x28,%rbp
        84:	leaveq
        85:	retq
      [...]
      
      With linum:
      [root@arch-fb-vm1 bpf]# ~/devshare/fb-kernel/linux/tools/bpf/bpftool/bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv linum
      int _dummy_tracepoint(struct dummy_tracepoint_args * arg):
      bpf_prog_b07ccb89267cf242__dummy_tracepoint:
      ; return test_long_fname_1(arg); [file:/data/users/kafai/fb-kernel/linux/tools/testing/selftests/bpf/test_btf_haskv.c line_num:54 line_col:9]
         0:	push   %rbp
         1:	mov    %rsp,%rbp
         4:	sub    $0x28,%rsp
         b:	sub    $0x28,%rbp
         f:	mov    %rbx,0x0(%rbp)
        13:	mov    %r13,0x8(%rbp)
        17:	mov    %r14,0x10(%rbp)
        1b:	mov    %r15,0x18(%rbp)
        1f:	xor    %eax,%eax
        21:	mov    %rax,0x20(%rbp)
        25:	callq  0x000000000000851e
      ; return test_long_fname_1(arg); [file:/data/users/kafai/fb-kernel/linux/tools/testing/selftests/bpf/test_btf_haskv.c line_num:54 line_col:2]
        2a:	xor    %eax,%eax
        2c:	mov    0x0(%rbp),%rbx
        30:	mov    0x8(%rbp),%r13
        34:	mov    0x10(%rbp),%r14
        38:	mov    0x18(%rbp),%r15
        3c:	add    $0x28,%rbp
        40:	leaveq
        41:	retq
      [...]
      Signed-off-by: NMartin KaFai Lau <kafai@fb.com>
      Acked-by: NYonghong Song <yhs@fb.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      b053b439
  9. 22 11月, 2018 2 次提交
  10. 11 11月, 2018 1 次提交
  11. 21 10月, 2018 1 次提交
    • D
      bpf, libbpf: simplify and cleanup perf ring buffer walk · 3dca2115
      Daniel Borkmann 提交于
      Simplify bpf_perf_event_read_simple() a bit and fix up some minor
      things along the way: the return code in the header is not of type
      int but enum bpf_perf_event_ret instead. Once callback indicated
      to break the loop walking event data, it also needs to be consumed
      in data_tail since it has been processed already.
      
      Moreover, bpf_perf_event_print_t callback should avoid void * as
      we actually get a pointer to struct perf_event_header and thus
      applications can make use of container_of() to have type checks.
      The walk also doesn't have to use modulo op since the ring size is
      required to be power of two.
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      3dca2115
  12. 17 10月, 2018 1 次提交
  13. 16 10月, 2018 1 次提交
    • J
      bpf: bpftool, add flag to allow non-compat map definitions · c034a177
      John Fastabend 提交于
      Multiple map definition structures exist and user may have non-zero
      fields in their definition that are not recognized by bpftool and
      libbpf. The normal behavior is to then fail loading the map. Although
      this is a good default behavior users may still want to load the map
      for debugging or other reasons. This patch adds a --mapcompat flag
      that can be used to override the default behavior and allow loading
      the map even when it has additional non-zero fields.
      
      For now the only user is 'bpftool prog' we can switch over other
      subcommands as needed. The library exposes an API that consumes
      a flags field now but I kept the original API around also in case
      users of the API don't want to expose this. The flags field is an
      int in case we need more control over how the API call handles
      errors/features/etc in the future.
      Signed-off-by: NJohn Fastabend <john.fastabend@gmail.com>
      Acked-by: NJakub Kicinski <jakub.kicinski@netronome.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      c034a177
  14. 08 10月, 2018 1 次提交
  15. 04 10月, 2018 4 次提交
  16. 03 10月, 2018 1 次提交
  17. 28 9月, 2018 1 次提交
    • A
      libbpf: Introduce libbpf_attach_type_by_name · 956b620f
      Andrey Ignatov 提交于
      There is a common use-case when ELF object contains multiple BPF
      programs and every program has its own section name. If it's cgroup-bpf
      then programs have to be 1) loaded and 2) attached to a cgroup.
      
      It's convenient to have information necessary to load BPF program
      together with program itself. This is where section name works fine in
      conjunction with libbpf_prog_type_by_name that identifies prog_type and
      expected_attach_type and these can be used with BPF_PROG_LOAD.
      
      But there is currently no way to identify attach_type by section name
      and it leads to messy code in user space that reinvents guessing logic
      every time it has to identify attach type to use with BPF_PROG_ATTACH.
      
      The patch introduces libbpf_attach_type_by_name that guesses attach type
      by section name if a program can be attached.
      
      The difference between expected_attach_type provided by
      libbpf_prog_type_by_name and attach_type provided by
      libbpf_attach_type_by_name is the former is used at BPF_PROG_LOAD time
      and can be zero if a program of prog_type X has only one corresponding
      attach type Y whether the latter provides specific attach type to use
      with BPF_PROG_ATTACH.
      
      No new section names were added to section_names array. Only existing
      ones were reorganized and attach_type was added where appropriate.
      Signed-off-by: NAndrey Ignatov <rdna@fb.com>
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      956b620f
  18. 07 9月, 2018 1 次提交
    • Y
      tools/bpf: add more netlink functionalities in lib/bpf · 36f1678d
      Yonghong Song 提交于
      This patch added a few netlink attribute parsing functions
      and the netlink API functions to query networking links, tc classes,
      tc qdiscs and tc filters. For example, the following API is
      to get networking links:
        int nl_get_link(int sock, unsigned int nl_pid,
                        dump_nlmsg_t dump_link_nlmsg,
                        void *cookie);
      
      Note that when the API is called, the user also provided a
      callback function with the following signature:
        int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
      
      The "cookie" is the parameter the user passed to the API and will
      be available for the callback function.
      The "msg" is the information about the result, e.g., ifinfomsg or
      tcmsg. The "tb" is the parsed netlink attributes.
      Signed-off-by: NYonghong Song <yhs@fb.com>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      36f1678d
  19. 27 7月, 2018 1 次提交
  20. 25 7月, 2018 1 次提交
  21. 12 7月, 2018 4 次提交
  22. 01 7月, 2018 1 次提交
  23. 23 5月, 2018 1 次提交
  24. 17 5月, 2018 1 次提交
  25. 11 5月, 2018 2 次提交
  26. 25 4月, 2018 1 次提交
    • J
      bpf: sockmap, add selftests · 16962b24
      John Fastabend 提交于
      This adds a new test program test_sockmap which is the old sample
      sockmap program. By moving the sample program here we can now run it
      as part of the self tests suite. To support this a populate_progs()
      routine is added to load programs and maps which was previously done
      with load_bpf_file(). This is needed because self test libs do not
      provide a similar routine. Also we now use the cgroup_helpers
      routines to manage cgroup use instead of manually creating one and
      supplying it to the CLI.
      
      Notice we keep the CLI around though because it is useful for dbg
      and specialized testing.
      
      To run use ./test_sockmap and the result should be,
      
      Summary 660 PASSED, 0 SKIPPED, 0 FAILED
      Signed-off-by: NJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      16962b24