1. 03 6月, 2022 3 次提交
  2. 13 5月, 2022 1 次提交
    • A
      libbpf: Add safer high-level wrappers for map operations · 737d0646
      Andrii Nakryiko 提交于
      Add high-level API wrappers for most common and typical BPF map
      operations that works directly on instances of struct bpf_map * (so
      you don't have to call bpf_map__fd()) and validate key/value size
      expectations.
      
      These helpers require users to specify key (and value, where
      appropriate) sizes when performing lookup/update/delete/etc. This forces
      user to actually think and validate (for themselves) those. This is
      a good thing as user is expected by kernel to implicitly provide correct
      key/value buffer sizes and kernel will just read/write necessary amount
      of data. If it so happens that user doesn't set up buffers correctly
      (which bit people for per-CPU maps especially) kernel either randomly
      overwrites stack data or return -EFAULT, depending on user's luck and
      circumstances. These high-level APIs are meant to prevent such
      unpleasant and hard to debug bugs.
      
      This patch also adds bpf_map_delete_elem_flags() low-level API and
      requires passing flags to bpf_map__delete_elem() API for consistency
      across all similar APIs, even though currently kernel doesn't expect
      any extra flags for BPF_MAP_DELETE_ELEM operation.
      
      List of map operations that get these high-level APIs:
      
        - bpf_map_lookup_elem;
        - bpf_map_update_elem;
        - bpf_map_delete_elem;
        - bpf_map_lookup_and_delete_elem;
        - bpf_map_get_next_key.
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20220512220713.2617964-1-andrii@kernel.org
      737d0646
  3. 11 5月, 2022 2 次提交
  4. 29 4月, 2022 1 次提交
    • A
      libbpf: Allow to opt-out from creating BPF maps · ec41817b
      Andrii Nakryiko 提交于
      Add bpf_map__set_autocreate() API that allows user to opt-out from
      libbpf automatically creating BPF map during BPF object load.
      
      This is a useful feature when building CO-RE-enabled BPF application
      that takes advantage of some new-ish BPF map type (e.g., socket-local
      storage) if kernel supports it, but otherwise uses some alternative way
      (e.g., extra HASH map). In such case, being able to disable the creation
      of a map that kernel doesn't support allows to successfully create and
      load BPF object file with all its other maps and programs.
      
      It's still up to user to make sure that no "live" code in any of their BPF
      programs are referencing such map instance, which can be achieved by
      guarding such code with CO-RE relocation check or by using .rodata
      global variables.
      
      If user fails to properly guard such code to turn it into "dead code",
      libbpf will helpfully post-process BPF verifier log and will provide
      more meaningful error and map name that needs to be guarded properly. As
      such, instead of:
      
        ; value = bpf_map_lookup_elem(&missing_map, &zero);
        4: (85) call unknown#2001000000
        invalid func unknown#2001000000
      
      ... user will see:
      
        ; value = bpf_map_lookup_elem(&missing_map, &zero);
        4: <invalid BPF map reference>
        BPF map 'missing_map' is referenced but wasn't created
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220428041523.4089853-4-andrii@kernel.org
      ec41817b
  5. 21 4月, 2022 2 次提交
  6. 06 4月, 2022 1 次提交
    • A
      libbpf: Wire up USDT API and bpf_link integration · 2e4913e0
      Andrii Nakryiko 提交于
      Wire up libbpf USDT support APIs without yet implementing all the
      nitty-gritty details of USDT discovery, spec parsing, and BPF map
      initialization.
      
      User-visible user-space API is simple and is conceptually very similar
      to uprobe API.
      
      bpf_program__attach_usdt() API allows to programmatically attach given
      BPF program to a USDT, specified through binary path (executable or
      shared lib), USDT provider and name. Also, just like in uprobe case, PID
      filter is specified (0 - self, -1 - any process, or specific PID).
      Optionally, USDT cookie value can be specified. Such single API
      invocation will try to discover given USDT in specified binary and will
      use (potentially many) BPF uprobes to attach this program in correct
      locations.
      
      Just like any bpf_program__attach_xxx() APIs, bpf_link is returned that
      represents this attachment. It is a virtual BPF link that doesn't have
      direct kernel object, as it can consist of multiple underlying BPF
      uprobe links. As such, attachment is not atomic operation and there can
      be brief moment when some USDT call sites are attached while others are
      still in the process of attaching. This should be taken into
      consideration by user. But bpf_program__attach_usdt() guarantees that
      in the case of success all USDT call sites are successfully attached, or
      all the successfuly attachments will be detached as soon as some USDT
      call sites failed to be attached. So, in theory, there could be cases of
      failed bpf_program__attach_usdt() call which did trigger few USDT
      program invocations. This is unavoidable due to multi-uprobe nature of
      USDT and has to be handled by user, if it's important to create an
      illusion of atomicity.
      
      USDT BPF programs themselves are marked in BPF source code as either
      SEC("usdt"), in which case they won't be auto-attached through
      skeleton's <skel>__attach() method, or it can have a full definition,
      which follows the spirit of fully-specified uprobes:
      SEC("usdt/<path>:<provider>:<name>"). In the latter case skeleton's
      attach method will attempt auto-attachment. Similarly, generic
      bpf_program__attach() will have enought information to go off of for
      parameterless attachment.
      
      USDT BPF programs are actually uprobes, and as such for kernel they are
      marked as BPF_PROG_TYPE_KPROBE.
      
      Another part of this patch is USDT-related feature probing:
        - BPF cookie support detection from user-space;
        - detection of kernel support for auto-refcounting of USDT semaphore.
      
      The latter is optional. If kernel doesn't support such feature and USDT
      doesn't rely on USDT semaphores, no error is returned. But if libbpf
      detects that USDT requires setting semaphores and kernel doesn't support
      this, libbpf errors out with explicit pr_warn() message. Libbpf doesn't
      support poking process's memory directly to increment semaphore value,
      like BCC does on legacy kernels, due to inherent raciness and danger of
      such process memory manipulation. Libbpf let's kernel take care of this
      properly or gives up.
      
      Logistically, all the extra USDT-related infrastructure of libbpf is put
      into a separate usdt.c file and abstracted behind struct usdt_manager.
      Each bpf_object has lazily-initialized usdt_manager pointer, which is
      only instantiated if USDT programs are attempted to be attached. Closing
      BPF object frees up usdt_manager resources. usdt_manager keeps track of
      USDT spec ID assignment and few other small things.
      
      Subsequent patches will fill out remaining missing pieces of USDT
      initialization and setup logic.
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Reviewed-by: NAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220404234202.331384-3-andrii@kernel.org
      2e4913e0
  7. 04 4月, 2022 1 次提交
    • A
      libbpf: Support function name-based attach uprobes · 433966e3
      Alan Maguire 提交于
      kprobe attach is name-based, using lookups of kallsyms to translate
      a function name to an address.  Currently uprobe attach is done
      via an offset value as described in [1].  Extend uprobe opts
      for attach to include a function name which can then be converted
      into a uprobe-friendly offset.  The calcualation is done in
      several steps:
      
      1. First, determine the symbol address using libelf; this gives us
         the offset as reported by objdump
      2. If the function is a shared library function - and the binary
         provided is a shared library - no further work is required;
         the address found is the required address
      3. Finally, if the function is local, subtract the base address
         associated with the object, retrieved from ELF program headers.
      
      The resultant value is then added to the func_offset value passed
      in to specify the uprobe attach address.  So specifying a func_offset
      of 0 along with a function name "printf" will attach to printf entry.
      
      The modes of operation supported are then
      
      1. to attach to a local function in a binary; function "foo1" in
         "/usr/bin/foo"
      2. to attach to a shared library function in a shared library -
         function "malloc" in libc.
      
      [1] https://www.kernel.org/doc/html/latest/trace/uprobetracer.htmlSigned-off-by: NAlan Maguire <alan.maguire@oracle.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/1648654000-21758-3-git-send-email-alan.maguire@oracle.com
      433966e3
  8. 18 3月, 2022 2 次提交
    • D
      libbpf: Add subskeleton scaffolding · 430025e5
      Delyan Kratunov 提交于
      In symmetry with bpf_object__open_skeleton(),
      bpf_object__open_subskeleton() performs the actual walking and linking
      of maps, progs, and globals described by bpf_*_skeleton objects.
      Signed-off-by: NDelyan Kratunov <delyank@fb.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/6942a46fbe20e7ebf970affcca307ba616985b15.1647473511.git.delyank@fb.com
      430025e5
    • J
      libbpf: Add bpf_program__attach_kprobe_multi_opts function · ddc6b049
      Jiri Olsa 提交于
      Adding bpf_program__attach_kprobe_multi_opts function for attaching
      kprobe program to multiple functions.
      
        struct bpf_link *
        bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
                                              const char *pattern,
                                              const struct bpf_kprobe_multi_opts *opts);
      
      User can specify functions to attach with 'pattern' argument that
      allows wildcards (*?' supported) or provide symbols or addresses
      directly through opts argument. These 3 options are mutually
      exclusive.
      
      When using symbols or addresses, user can also provide cookie value
      for each symbol/address that can be retrieved later in bpf program
      with bpf_get_attach_cookie helper.
      
        struct bpf_kprobe_multi_opts {
                size_t sz;
                const char **syms;
                const unsigned long *addrs;
                const __u64 *cookies;
                size_t cnt;
                bool retprobe;
                size_t :0;
        };
      
      Symbols, addresses and cookies are provided through opts object
      (syms/addrs/cookies) as array pointers with specified count (cnt).
      
      Each cookie value is paired with provided function address or symbol
      with the same array index.
      
      The program can be also attached as return probe if 'retprobe' is set.
      
      For quick usage with NULL opts argument, like:
      
        bpf_program__attach_kprobe_multi_opts(prog, "ksys_*", NULL)
      
      the 'prog' will be attached as kprobe to 'ksys_*' functions.
      
      Also adding new program sections for automatic attachment:
      
        kprobe.multi/<symbol_pattern>
        kretprobe.multi/<symbol_pattern>
      
      The symbol_pattern is used as 'pattern' argument in
      bpf_program__attach_kprobe_multi_opts function.
      Signed-off-by: NJiri Olsa <jolsa@kernel.org>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20220316122419.933957-10-jolsa@kernel.org
      ddc6b049
  9. 06 3月, 2022 1 次提交
    • A
      libbpf: Support custom SEC() handlers · 697f104d
      Andrii Nakryiko 提交于
      Allow registering and unregistering custom handlers for BPF program.
      This allows user applications and libraries to plug into libbpf's
      declarative SEC() definition handling logic. This allows to offload
      complex and intricate custom logic into external libraries, but still
      provide a great user experience.
      
      One such example is USDT handling library, which has a lot of code and
      complexity which doesn't make sense to put into libbpf directly, but it
      would be really great for users to be able to specify BPF programs with
      something like SEC("usdt/<path-to-binary>:<usdt_provider>:<usdt_name>")
      and have correct BPF program type set (BPF_PROGRAM_TYPE_KPROBE, as it is
      uprobe) and even support BPF skeleton's auto-attach logic.
      
      In some cases, it might be even good idea to override libbpf's default
      handling, like for SEC("perf_event") programs. With custom library, it's
      possible to extend logic to support specifying perf event specification
      right there in SEC() definition without burdening libbpf with lots of
      custom logic or extra library dependecies (e.g., libpfm4). With current
      patch it's possible to override libbpf's SEC("perf_event") handling and
      specify a completely custom ones.
      
      Further, it's possible to specify a generic fallback handling for any
      SEC() that doesn't match any other custom or standard libbpf handlers.
      This allows to accommodate whatever legacy use cases there might be, if
      necessary.
      
      See doc comments for libbpf_register_prog_handler() and
      libbpf_unregister_prog_handler() for detailed semantics.
      
      This patch also bumps libbpf development version to v0.8 and adds new
      APIs there.
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
      Tested-by: NAlan Maguire <alan.maguire@oracle.com>
      Reviewed-by: NAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220305010129.1549719-3-andrii@kernel.org
      697f104d
  10. 04 2月, 2022 1 次提交
  11. 26 1月, 2022 3 次提交
  12. 25 1月, 2022 2 次提交
  13. 21 1月, 2022 1 次提交
    • A
      libbpf: streamline low-level XDP APIs · c359821a
      Andrii Nakryiko 提交于
      Introduce 4 new netlink-based XDP APIs for attaching, detaching, and
      querying XDP programs:
        - bpf_xdp_attach;
        - bpf_xdp_detach;
        - bpf_xdp_query;
        - bpf_xdp_query_id.
      
      These APIs replace bpf_set_link_xdp_fd, bpf_set_link_xdp_fd_opts,
      bpf_get_link_xdp_id, and bpf_get_link_xdp_info APIs ([0]). The latter
      don't follow a consistent naming pattern and some of them use
      non-extensible approaches (e.g., struct xdp_link_info which can't be
      modified without breaking libbpf ABI).
      
      The approach I took with these low-level XDP APIs is similar to what we
      did with low-level TC APIs. There is a nice duality of bpf_tc_attach vs
      bpf_xdp_attach, and so on. I left bpf_xdp_attach() to support detaching
      when -1 is specified for prog_fd for generality and convenience, but
      bpf_xdp_detach() is preferred due to clearer naming and associated
      semantics. Both bpf_xdp_attach() and bpf_xdp_detach() accept the same
      opts struct allowing to specify expected old_prog_fd.
      
      While doing the refactoring, I noticed that old APIs require users to
      specify opts with old_fd == -1 to declare "don't care about already
      attached XDP prog fd" condition. Otherwise, FD 0 is assumed, which is
      essentially never an intended behavior. So I made this behavior
      consistent with other kernel and libbpf APIs, in which zero FD means "no
      FD". This seems to be more in line with the latest thinking in BPF land
      and should cause less user confusion, hopefully.
      
      For querying, I left two APIs, both more generic bpf_xdp_query()
      allowing to query multiple IDs and attach mode, but also
      a specialization of it, bpf_xdp_query_id(), which returns only requested
      prog_id. Uses of prog_id returning bpf_get_link_xdp_id() were so
      prevalent across selftests and samples, that it seemed a very common use
      case and using bpf_xdp_query() for doing it felt very cumbersome with
      a highly branches if/else chain based on flags and attach mode.
      
      Old APIs are scheduled for deprecation in libbpf 0.8 release.
      
        [0] Closes: https://github.com/libbpf/libbpf/issues/309Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Acked-by: NToke Høiland-Jørgensen <toke@redhat.com>
      Link: https://lore.kernel.org/r/20220120061422.2710637-2-andrii@kernel.orgSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      c359821a
  14. 13 1月, 2022 1 次提交
  15. 06 1月, 2022 3 次提交
  16. 18 12月, 2021 1 次提交
    • A
      libbpf: Rework feature-probing APIs · 878d8def
      Andrii Nakryiko 提交于
      Create three extensible alternatives to inconsistently named
      feature-probing APIs:
      
        - libbpf_probe_bpf_prog_type() instead of bpf_probe_prog_type();
        - libbpf_probe_bpf_map_type() instead of bpf_probe_map_type();
        - libbpf_probe_bpf_helper() instead of bpf_probe_helper().
      
      Set up return values such that libbpf can report errors (e.g., if some
      combination of input arguments isn't possible to validate, etc), in
      addition to whether the feature is supported (return value 1) or not
      supported (return value 0).
      
      Also schedule deprecation of those three APIs. Also schedule deprecation
      of bpf_probe_large_insn_limit().
      
      Also fix all the existing detection logic for various program and map
      types that never worked:
      
        - BPF_PROG_TYPE_LIRC_MODE2;
        - BPF_PROG_TYPE_TRACING;
        - BPF_PROG_TYPE_LSM;
        - BPF_PROG_TYPE_EXT;
        - BPF_PROG_TYPE_SYSCALL;
        - BPF_PROG_TYPE_STRUCT_OPS;
        - BPF_MAP_TYPE_STRUCT_OPS;
        - BPF_MAP_TYPE_BLOOM_FILTER.
      
      Above prog/map types needed special setups and detection logic to work.
      Subsequent patch adds selftests that will make sure that all the
      detection logic keeps working for all current and future program and map
      types, avoiding otherwise inevitable bit rot.
      
        [0] Closes: https://github.com/libbpf/libbpf/issues/312Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: NDave Marchevsky <davemarchevsky@fb.com>
      Cc: Julia Kartseva <hex@fb.com>
      Link: https://lore.kernel.org/bpf/20211217171202.3352835-2-andrii@kernel.org
      878d8def
  17. 15 12月, 2021 1 次提交
  18. 14 12月, 2021 1 次提交
  19. 11 12月, 2021 3 次提交
  20. 07 12月, 2021 2 次提交
  21. 03 12月, 2021 2 次提交
  22. 20 11月, 2021 1 次提交
  23. 19 11月, 2021 1 次提交
  24. 12 11月, 2021 2 次提交
  25. 08 11月, 2021 1 次提交