• Andrii Nakryiko's avatar
    libbpf: Add BTF field iterator · 68153bb2
    Andrii Nakryiko authored
    Implement iterator-based type ID and string offset BTF field iterator.
    This is used extensively in BTF-handling code and BPF linker code for
    various sanity checks, rewriting IDs/offsets, etc. Currently this is
    implemented as visitor pattern calling custom callbacks, which makes the
    logic (especially in simple cases) unnecessarily obscure and harder to
    follow.
    
    Having equivalent functionality using iterator pattern makes for simpler
    to understand and maintain code. As we add more code for BTF processing
    logic in libbpf, it's best to switch to iterator pattern before adding
    more callback-based code.
    
    The idea for iterator-based implementation is to record offsets of
    necessary fields within fixed btf_type parts (which should be iterated
    just once), and, for kinds that have multiple members (based on vlen
    field), record where in each member necessary fields are located.
    
    Generic iteration code then just keeps track of last offset that was
    returned and handles N members correctly. Return type is just u32
    pointer, where NULL is returned when all relevant fields were already
    iterated.
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
    Tested-by: default avatarAlan Maguire <alan.maguire@oracle.com>
    Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
    Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/bpf/20240605001629.4061937-2-andrii@kernel.org
    68153bb2
libbpf_internal.h 19.5 KB