• Andrii Nakryiko's avatar
    libbpf: Refactor internal sec_def handling to enable pluggability · 12d9466d
    Andrii Nakryiko authored
    Refactor internals of libbpf to allow adding custom SEC() handling logic
    easily from outside of libbpf. To that effect, each SEC()-handling
    registration sets mandatory program type/expected attach type for
    a given prefix and can provide three callbacks called at different
    points of BPF program lifetime:
    
      - init callback for right after bpf_program is initialized and
      prog_type/expected_attach_type is set. This happens during
      bpf_object__open() step, close to the very end of constructing
      bpf_object, so all the libbpf APIs for querying and updating
      bpf_program properties should be available;
    
      - pre-load callback is called right before BPF_PROG_LOAD command is
      called in the kernel. This callbacks has ability to set both
      bpf_program properties, as well as program load attributes, overriding
      and augmenting the standard libbpf handling of them;
    
      - optional auto-attach callback, which makes a given SEC() handler
      support auto-attachment of a BPF program through bpf_program__attach()
      API and/or BPF skeletons <skel>__attach() method.
    
    Each callbacks gets a `long cookie` parameter passed in, which is
    specified during SEC() handling. This can be used by callbacks to lookup
    whatever additional information is necessary.
    
    This is not yet completely ready to be exposed to the outside world,
    mainly due to non-public nature of struct bpf_prog_load_params. Instead
    of making it part of public API, we'll wait until the planned low-level
    libbpf API improvements for BPF_PROG_LOAD and other typical bpf()
    syscall APIs, at which point we'll have a public, probably OPTS-based,
    way to fully specify BPF program load parameters, which will be used as
    an interface for custom pre-load callbacks.
    
    But this change itself is already a good first step to unify the BPF
    program hanling logic even within the libbpf itself. As one example, all
    the extra per-program type handling (sleepable bit, attach_btf_id
    resolution, unsetting optional expected attach type) is now more obvious
    and is gathered in one place.
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Acked-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
    Link: https://lore.kernel.org/bpf/20210928161946.2512801-6-andrii@kernel.org
    12d9466d
libbpf.c 284 KB