Commit Graph

53 Commits

Author SHA1 Message Date
Jerome Marchand 57a23c9c54 bpftool: Improve btf c dump sorting stability
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit f8c6b7913dfaa67475883f94261c278adbcaa0ae
Author: Mykyta Yatsenko <yatsenko@meta.com>
Date:   Fri Sep 6 14:24:53 2024 +0100

    bpftool: Improve btf c dump sorting stability

    Existing algorithm for BTF C dump sorting uses only types and names of
    the structs and unions for ordering. As dump contains structs with the
    same names but different contents, relative to each other ordering of
    those structs will be accidental.
    This patch addresses this problem by introducing a new sorting field
    that contains hash of the struct/union field names and types to
    disambiguate comparison of the non-unique named structs.

    Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240906132453.146085-1-mykyta.yatsenko5@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:04 +01:00
Viktor Malik 4f9a1b5557
bpftool: Fix handling enum64 in btf dump sorting
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit b0222d1d9e6f8551a056b89b0bff38f515f3c9b5
Author: Mykyta Yatsenko <yatsenko@meta.com>
Date:   Mon Sep 2 18:17:21 2024 +0100

    bpftool: Fix handling enum64 in btf dump sorting
    
    Wrong function is used to access the first enum64 element. Substituting btf_enum(t)
    with btf_enum64(t) for BTF_KIND_ENUM64.
    
    Fixes: 94133cf24bb3 ("bpftool: Introduce btf c dump sorting")
    Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Quentin Monnet <qmo@kernel.org>
    Link: https://lore.kernel.org/bpf/20240902171721.105253-1-mykyta.yatsenko5@gmail.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:20 +01:00
Viktor Malik b1de25cdfe
bpftool: Support dumping kfunc prototypes from BTF
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 770abbb5a25a5b767f1c60ba366aea503728e957
Author: Daniel Xu <dxu@dxuuu.xyz>
Date:   Wed Jun 12 09:58:36 2024 -0600

    bpftool: Support dumping kfunc prototypes from BTF
    
    This patch enables dumping kfunc prototypes from bpftool. This is useful
    b/c with this patch, end users will no longer have to manually define
    kfunc prototypes. For the kernel tree, this also means we can optionally
    drop kfunc prototypes from:
    
            tools/testing/selftests/bpf/bpf_kfuncs.h
            tools/testing/selftests/bpf/bpf_experimental.h
    
    Example usage:
    
            $ make PAHOLE=/home/dxu/dev/pahole/build/pahole -j30 vmlinux
    
            $ ./tools/bpf/bpftool/bpftool btf dump file ./vmlinux format c | rg "__ksym;" | head -3
            extern void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) __weak __ksym;
            extern void cgroup_rstat_flush(struct cgroup *cgrp) __weak __ksym;
            extern struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags) __weak __ksym;
    
    Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
    Link: https://lore.kernel.org/r/bf6c08f9263c4bd9d10a717de95199d766a13f61.1718207789.git.dxu@dxuuu.xyz
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:07 +01:00
Viktor Malik 4c6f518e3f bpftool: Introduce btf c dump sorting
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 94133cf24bb33889aac267a7f0e3e6a08b8a8e5a
Author: Mykyta Yatsenko <yatsenko@meta.com>
Date:   Tue May 14 14:12:21 2024 +0100

    bpftool: Introduce btf c dump sorting
    
    Sort bpftool c dump output; aiming to simplify vmlinux.h diffing and
    forcing more natural type definitions ordering.
    
    Definitions are sorted first by their BTF kind ranks, then by their base
    type name and by their own name.
    
    Type ranks
    
    Assign ranks to btf kinds (defined in function btf_type_rank) to set
    next order:
    1. Anonymous enums/enums64
    2. Named enums/enums64
    3. Trivial types typedefs (ints, then floats)
    4. Structs/Unions
    5. Function prototypes
    6. Forward declarations
    
    Type rank is set to maximum for unnamed reference types, structs and
    unions to avoid emitting those types early. They will be emitted as
    part of the type chain starting with named type.
    
    Lexicographical ordering
    
    Each type is assigned a sort_name and own_name.
    sort_name is the resolved name of the final base type for reference
    types (typedef, pointer, array etc). Sorting by sort_name allows to
    group typedefs of the same base type. sort_name for non-reference type
    is the same as own_name. own_name is a direct name of particular type,
    is used as final sorting step.
    
    Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Tested-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <qmo@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240514131221.20585-1-yatsenko@meta.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 08:57:39 +01:00
Viktor Malik d152843a35 bpftool: Use bpf_{btf,link,map,prog}_get_info_by_fd()
Bugzilla: https://bugzilla.redhat.com/2178930

commit 38f0408ef756e738387f7d8f62b8d58ca5938da4
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Feb 15 00:12:16 2023 +0100

    bpftool: Use bpf_{btf,link,map,prog}_get_info_by_fd()
    
    Use the new type-safe wrappers around bpf_obj_get_info_by_fd().
    
    Split the bpf_obj_get_info_by_fd() call in build_btf_type_table() in
    two, since knowing the type helps with the Memory Sanitizer.
    
    Improve map_parse_fd_and_info() type safety by using
    struct bpf_map_info * instead of void * for info.
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20230214231221.249277-4-iii@linux.ibm.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:45:39 +02:00
Jerome Marchand c0a58b3566 bpftool: clean-up usage of libbpf_get_error()
Bugzilla: https://bugzilla.redhat.com/2177177

commit d1313e01271d2d8f33d6c82f1afb77e820a3540d
Author: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@industrialdiscipline.com>
Date:   Sun Nov 20 11:26:32 2022 +0000

    bpftool: clean-up usage of libbpf_get_error()

    bpftool is now totally compliant with libbpf 1.0 mode and is not
    expected to be compiled with pre-1.0, let's clean-up the usage of
    libbpf_get_error().

    The changes stay aligned with returned errors always negative.

    - In tools/bpf/bpftool/btf.c This fixes an uninitialized local
    variable `err` in function do_dump() because it may now be returned
    without having been set.
    - This also removes the checks on NULL pointers before calling
    btf__free() because that function already does the check.

    Signed-off-by: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@industrialdiscipline.com>
    Link: https://lore.kernel.org/r/20221120112515.38165-5-sahid.ferdjaoui@industrialdiscipline.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:09 +02:00
Jerome Marchand 24d396b55c libbpf: Hashmap interface update to allow both long and void* keys/values
Bugzilla: https://bugzilla.redhat.com/2177177

Conflicts: Some minor changes due to missing commits 09b73fe9e3de
("perf smt: Compute SMT from topology") and f0c4b97a2927 ("perf test:
Add basic core_wide expression test").

commit c302378bc157f6a73b6cae4ca67f5f6aa931dcec
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Wed Nov 9 16:26:09 2022 +0200

    libbpf: Hashmap interface update to allow both long and void* keys/values

    An update for libbpf's hashmap interface from void* -> void* to a
    polymorphic one, allowing both long and void* keys and values.

    This simplifies many use cases in libbpf as hashmaps there are mostly
    integer to integer.

    Perf copies hashmap implementation from libbpf and has to be
    updated as well.

    Changes to libbpf, selftests/bpf and perf are packed as a single
    commit to avoid compilation issues with any future bisect.

    Polymorphic interface is acheived by hiding hashmap interface
    functions behind auxiliary macros that take care of necessary
    type casts, for example:

        #define hashmap_cast_ptr(p)                                         \
            ({                                                              \
                    _Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),\
                                   #p " pointee should be a long-sized integer or a pointer"); \
                    (long *)(p);                                            \
            })

        bool hashmap_find(const struct hashmap *map, long key, long *value);

        #define hashmap__find(map, key, value) \
                    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))

    - hashmap__find macro casts key and value parameters to long
      and long* respectively
    - hashmap_cast_ptr ensures that value pointer points to a memory
      of appropriate size.

    This hack was suggested by Andrii Nakryiko in [1].
    This is a follow up for [2].

    [1] https://lore.kernel.org/bpf/CAEf4BzZ8KFneEJxFAaNCCFPGqp20hSpS2aCj76uRk3-qZUH5xg@mail.gmail.com/
    [2] https://lore.kernel.org/bpf/af1facf9-7bc8-8a3d-0db4-7b3f333589a2@meta.com/T/#m65b28f1d6d969fcd318b556db6a3ad499a42607d

    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20221109142611.879983-2-eddyz87@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:02 +02:00
Artem Savkov 68d730e069 bpftool: Fix error message of strerror
Bugzilla: https://bugzilla.redhat.com/2166911

commit 3ca2fb497440a3c8294f9df0ce7b2c3c9a1c5875
Author: Tianyi Liu <i.pear@outlook.com>
Date:   Wed Sep 28 16:09:32 2022 +0800

    bpftool: Fix error message of strerror
    
    strerror() expects a positive errno, however variable err will never be
    positive when an error occurs. This causes bpftool to output too many
    "unknown error", even a simple "file not exist" error can not get an
    accurate message.
    
    This patch fixed all "strerror(err)" patterns in bpftool.
    Specially in btf.c#L823, hashmap__append() is an internal function of
    libbpf and will not change errno, so there's a little difference.
    Some libbpf_get_error() calls are kept for return values.
    
    Changes since v1: https://lore.kernel.org/bpf/SY4P282MB1084B61CD8671DFA395AA8579D539@SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM/
    Check directly for NULL values instead of calling libbpf_get_error().
    
    Signed-off-by: Tianyi Liu <i.pear@outlook.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/SY4P282MB1084AD9CD84A920F08DF83E29D549@SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:21 +01:00
Artem Savkov cf8e1a1cf4 bpftool: Remove unused struct btf_attach_point
Bugzilla: https://bugzilla.redhat.com/2166911

commit d863f42930db35e82f47e4b4c78531a2b8d396ae
Author: Yuan Can <yuancan@huawei.com>
Date:   Wed Sep 28 09:04:39 2022 +0000

    bpftool: Remove unused struct btf_attach_point
    
    After commit 2828d0d75b73 ("bpftool: Switch to libbpf's hashmap for
    programs/maps in BTF listing"), struct btf_attach_point is not used
    anymore and can be removed as well.
    
    Signed-off-by: Yuan Can <yuancan@huawei.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220928090440.79637-2-yuancan@huawei.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:20 +01:00
Yauheni Kaliuta a9478e4ae2 bpftool: Add btf enum64 support
Bugzilla: http://bugzilla.redhat.com/2120968

commit 58a53978fdf65d12dae1798e44120efb992a3615
Author: Yonghong Song <yhs@fb.com>
Date:   Mon Jun 6 23:26:52 2022 -0700

    bpftool: Add btf enum64 support
    
    Add BTF_KIND_ENUM64 support.
    For example, the following enum is defined in uapi bpf.h.
      $ cat core.c
      enum A {
            BPF_F_INDEX_MASK                = 0xffffffffULL,
            BPF_F_CURRENT_CPU               = BPF_F_INDEX_MASK,
            BPF_F_CTXLEN_MASK               = (0xfffffULL << 32),
      } g;
    Compiled with
      clang -target bpf -O2 -g -c core.c
    Using bpftool to dump types and generate format C file:
      $ bpftool btf dump file core.o
      ...
      [1] ENUM64 'A' encoding=UNSIGNED size=8 vlen=3
            'BPF_F_INDEX_MASK' val=4294967295ULL
            'BPF_F_CURRENT_CPU' val=4294967295ULL
            'BPF_F_CTXLEN_MASK' val=4503595332403200ULL
      $ bpftool btf dump file core.o format c
      ...
      enum A {
            BPF_F_INDEX_MASK = 4294967295ULL,
            BPF_F_CURRENT_CPU = 4294967295ULL,
            BPF_F_CTXLEN_MASK = 4503595332403200ULL,
      };
      ...
    
    Note that for raw btf output, the encoding (UNSIGNED or SIGNED)
    is printed out as well. The 64bit value is also represented properly
    in BTF and C dump.
    
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20220607062652.3722649-1-yhs@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:12 +02:00
Yauheni Kaliuta 35725a8afd bpftool: Use sysfs vmlinux when dumping BTF by ID
Bugzilla: https://bugzilla.redhat.com/2120968

commit 418fbe82578e2889dcc2c0ae4d367ea4e28dd05c
Author: Larysa Zaremba <larysa.zaremba@intel.com>
Date:   Fri May 13 14:17:43 2022 +0200

    bpftool: Use sysfs vmlinux when dumping BTF by ID
    
    Currently, dumping almost all BTFs specified by id requires
    using the -B option to pass the base BTF. For kernel module
    BTFs the vmlinux BTF sysfs path should work.
    
    This patch simplifies dumping by ID usage by loading
    vmlinux BTF from sysfs as base, if base BTF was not specified
    and the ID corresponds to a kernel module BTF.
    
    Signed-off-by: Larysa Zaremba <larysa.zaremba@intel.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Alexander Lobakin <alexandr.lobakin@intel.com>
    Link: https://lore.kernel.org/bpf/20220513121743.12411-1-larysa.zaremba@intel.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:04 +02:00
Artem Savkov 4638817e0a bpftool: Use libbpf_get_error() to check error
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit e5043894b21f7d99d3db31ad06308d6c5726caa6
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Mon Nov 15 09:24:36 2021 +0800

    bpftool: Use libbpf_get_error() to check error

    Currently, LIBBPF_STRICT_ALL mode is enabled by default for
    bpftool which means on error cases, some libbpf APIs would
    return NULL pointers. This makes IS_ERR check failed to detect
    such cases and result in segfault error. Use libbpf_get_error()
    instead like we do in libbpf itself.

    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211115012436.3143318-1-hengqi.chen@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:37 +02:00
Artem Savkov 7786f15068 bpftool: Support BTF_KIND_TYPE_TAG
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 3da5ba6f0509ace03cad38b554c89797129e90be
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Nov 11 17:26:20 2021 -0800

    bpftool: Support BTF_KIND_TYPE_TAG

    Add bpftool support for BTF_KIND_TYPE_TAG.

    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211112012620.1505506-1-yhs@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:36 +02:00
Artem Savkov 443663d0b2 bpftool: Update btf_dump__new() and perf_buffer__new_raw() calls
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 164b04f27fbd769f57905dfddd2a8953974eeef4
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Nov 10 21:36:24 2021 -0800

    bpftool: Update btf_dump__new() and perf_buffer__new_raw() calls

    Use v1.0-compatible variants of btf_dump and perf_buffer "constructors".
    This is also a demonstration of reusing struct perf_buffer_raw_opts as
    OPTS-style option struct for new perf_buffer__new_raw() API.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211111053624.190580-10-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:35 +02:00
Yauheni Kaliuta 11cec39963 bpftool: Fix error check when calling hashmap__new()
Bugzilla: http://bugzilla.redhat.com/2069045

commit 622a5b582cc27d3deedc38fcef68da2972e8e58d
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Fri Jan 7 10:26:20 2022 -0500

    bpftool: Fix error check when calling hashmap__new()
    
    hashmap__new() encodes errors with ERR_PTR(), hence it's not valid to
    check the returned pointer against NULL and IS_ERR() has to be used
    instead.
    
    libbpf_get_error() can't be used in this case as hashmap__new() is not
    part of the public libbpf API and it'll continue using ERR_PTR() after
    libbpf 1.0.
    
    Fixes: 8f184732b60b ("bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects")
    Fixes: 2828d0d75b73 ("bpftool: Switch to libbpf's hashmap for programs/maps in BTF listing")
    Fixes: d6699f8e0f83 ("bpftool: Switch to libbpf's hashmap for PIDs/names references")
    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20220107152620.192327-2-mauricio@kinvolk.io

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:53 +03:00
Yauheni Kaliuta 765bcf6418 bpftool: Switch to libbpf's hashmap for PIDs/names references
Bugzilla: http://bugzilla.redhat.com/2069045

commit d6699f8e0f834b40db35466f704705ae757be11a
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Sat Oct 23 21:51:54 2021 +0100

    bpftool: Switch to libbpf's hashmap for PIDs/names references
    
    In order to show PIDs and names for processes holding references to BPF
    programs, maps, links, or BTF objects, bpftool creates hash maps to
    store all relevant information. This commit is part of a set that
    transitions from the kernel's hash map implementation to the one coming
    with libbpf.
    
    The motivation is to make bpftool less dependent of kernel headers, to
    ease the path to a potential out-of-tree mirror, like libbpf has.
    
    This is the third and final step of the transition, in which we convert
    the hash maps used for storing the information about the processes
    holding references to BPF objects (programs, maps, links, BTF), and at
    last we drop the inclusion of tools/include/linux/hashtable.h.
    
    Note: Checkpatch complains about the use of __weak declarations, and the
    missing empty lines after the bunch of empty function declarations when
    compiling without the BPF skeletons (none of these were introduced in
    this patch). We want to keep things as they are, and the reports should
    be safe to ignore.
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211023205154.6710-6-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:46 +03:00
Yauheni Kaliuta 30a3682b2d bpftool: Switch to libbpf's hashmap for programs/maps in BTF listing
Bugzilla: http://bugzilla.redhat.com/2069045

commit 2828d0d75b738c9cd98080c530b7b7ea90b279dd
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Sat Oct 23 21:51:53 2021 +0100

    bpftool: Switch to libbpf's hashmap for programs/maps in BTF listing
    
    In order to show BPF programs and maps using BTF objects when the latter
    are being listed, bpftool creates hash maps to store all relevant items.
    This commit is part of a set that transitions from the kernel's hash map
    implementation to the one coming with libbpf.
    
    The motivation is to make bpftool less dependent of kernel headers, to
    ease the path to a potential out-of-tree mirror, like libbpf has.
    
    This commit focuses on the two hash maps used by bpftool when listing
    BTF objects to store references to programs and maps, and convert them
    to the libbpf's implementation.
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211023205154.6710-5-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:46 +03:00
Yauheni Kaliuta ebe4d0acc6 bpftool: Switch to new btf__type_cnt API
Bugzilla: http://bugzilla.redhat.com/2069045

commit 58fc155b0e4bbd69584b7a241ab01d55ee7cfde6
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Fri Oct 22 21:06:22 2021 +0800

    bpftool: Switch to new btf__type_cnt API
    
    Replace the call to btf__get_nr_types with new API btf__type_cnt.
    The old API will be deprecated in libbpf v0.7+. No functionality
    change.
    
    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211022130623.1548429-5-hengqi.chen@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:44 +03:00
Yauheni Kaliuta fdbdd94ffe bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG
Bugzilla: http://bugzilla.redhat.com/2069045

commit 223f903e9c832699f4e5f422281a60756c1c6cfe
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Oct 12 09:48:38 2021 -0700

    bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG
    
    Patch set [1] introduced BTF_KIND_TAG to allow tagging
    declarations for struct/union, struct/union field, var, func
    and func arguments and these tags will be encoded into
    dwarf. They are also encoded to btf by llvm for the bpf target.
    
    After BTF_KIND_TAG is introduced, we intended to use it
    for kernel __user attributes. But kernel __user is actually
    a type attribute. Upstream and internal discussion showed
    it is not a good idea to mix declaration attribute and
    type attribute. So we proposed to introduce btf_type_tag
    as a type attribute and existing btf_tag renamed to
    btf_decl_tag ([2]).
    
    This patch renamed BTF_KIND_TAG to BTF_KIND_DECL_TAG and some
    other declarations with *_tag to *_decl_tag to make it clear
    the tag is for declaration. In the future, BTF_KIND_TYPE_TAG
    might be introduced per [3].
    
     [1] https://lore.kernel.org/bpf/20210914223004.244411-1-yhs@fb.com/
     [2] https://reviews.llvm.org/D111588
     [3] https://reviews.llvm.org/D111199
    
    Fixes: b5ea834dde6b ("bpf: Support for new btf kind BTF_KIND_TAG")
    Fixes: 5b84bd10363e ("libbpf: Add support for BTF_KIND_TAG")
    Fixes: 5c07f2fec003 ("bpftool: Add support for BTF_KIND_TAG")
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211012164838.3345699-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:42 +03:00
Yauheni Kaliuta 957b215b01 bpftool: Add support for BTF_KIND_TAG
Bugzilla: http://bugzilla.redhat.com/2069045

commit 5c07f2fec00361fb5e8cff8ba7fdede7b29f38bd
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Sep 14 15:30:31 2021 -0700

    bpftool: Add support for BTF_KIND_TAG
    
    Added bpftool support to dump BTF_KIND_TAG information.
    The new bpftool will be used in later patches to dump
    btf in the test bpf program object file.
    
    Currently, the tags are not emitted with
      bpftool btf dump file <path> format c
    and they are silently ignored.  The tag information is
    mostly used in the kernel for verification purpose and the kernel
    uses its own btf to check. With adding these tags
    to vmlinux.h, tags will be encoded in program's btf but
    they will not be used by the kernel, at least for now.
    So let us delay adding these tags to format C header files
    until there is a real need.
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210914223031.246951-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:16:12 +03:00
Jerome Marchand 765e926fe8 tools: bpftool: Document and add bash completion for -L, -B options
Bugzilla: http://bugzilla.redhat.com/2041365

commit 8cc8c6357c8fa763c650f1bddb69871a254f427c
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Fri Jul 30 22:54:34 2021 +0100

    tools: bpftool: Document and add bash completion for -L, -B options

    The -L|--use-loader option for using loader programs when loading, or
    when generating a skeleton, did not have any documentation or bash
    completion. Same thing goes for -B|--base-btf, used to pass a path to a
    base BTF object for split BTF such as BTF for kernel modules.

    This patch documents and adds bash completion for those options.

    Fixes: 75fa177769 ("tools/bpftool: Add bpftool support for split BTF")
    Fixes: d510296d33 ("bpftool: Use syscall/loader program in "prog load" and "gen skeleton" command.")
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210730215435.7095-7-quentin@isovalent.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:37 +02:00
Jerome Marchand b1eab37952 tools: bpftool: Update and synchronise option list in doc and help msg
Bugzilla: http://bugzilla.redhat.com/2041365

commit c07ba629df97b796ca7bbdfbf4748266ead27745
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Fri Jul 30 22:54:32 2021 +0100

    tools: bpftool: Update and synchronise option list in doc and help msg

    All bpftool commands support the options for JSON output and debug from
    libbpf. In addition, some commands support additional options
    corresponding to specific use cases.

    The list of options described in the man pages for the different
    commands are not always accurate. The messages for interactive help are
    mostly limited to HELP_SPEC_OPTIONS, and are even less representative of
    the actual set of options supported for the commands.

    Let's update the lists:

    - HELP_SPEC_OPTIONS is modified to contain the "default" options (JSON
      and debug), and to be extensible (no ending curly bracket).
    - All commands use HELP_SPEC_OPTIONS in their help message, and then
      complete the list with their specific options.
    - The lists of options in the man pages are updated.
    - The formatting of the list for bpftool.rst is adjusted to match
      formatting for the other man pages. This is for consistency, and also
      because it will be helpful in a future patch to automatically check
      that the files are synchronised.

    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210730215435.7095-5-quentin@isovalent.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:37 +02:00
Jerome Marchand bcde5315a0 tools: bpftool: Support dumping split BTF by id
Bugzilla: http://bugzilla.redhat.com/2041365

commit 211ab78f7658b50ea10c4569be63ca5009fd39b4
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Thu Jul 29 17:20:28 2021 +0100

    tools: bpftool: Support dumping split BTF by id

    Split BTF objects are typically BTF objects for kernel modules, which
    are incrementally built on top of kernel BTF instead of redefining all
    kernel symbols they need. We can use bpftool with its -B command-line
    option to dump split BTF objects. It works well when the handle provided
    for the BTF object to dump is a "path" to the BTF object, typically
    under /sys/kernel/btf, because bpftool internally calls
    btf__parse_split() which can take a "base_btf" pointer and resolve the
    BTF reconstruction (although in that case, the "-B" option is
    unnecessary because bpftool performs autodetection).

    However, it did not work so far when passing the BTF object through its
    id, because bpftool would call btf__get_from_id() which did not provide
    a way to pass a "base_btf" pointer.

    In other words, the following works:

        # bpftool btf dump file /sys/kernel/btf/i2c_smbus -B /sys/kernel/btf/vmlinux

    But this was not possible:

        # bpftool btf dump id 6 -B /sys/kernel/btf/vmlinux

    The libbpf API has recently changed, and btf__get_from_id() has been
    deprecated in favour of btf__load_from_kernel_by_id() and its version
    with support for split BTF, btf__load_from_kernel_by_id_split(). Let's
    update bpftool to make it able to dump the BTF object in the second case
    as well.

    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: John Fastabend <john.fastabend@gmail.com>
    Link: https://lore.kernel.org/bpf/20210729162028.29512-9-quentin@isovalent.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:36 +02:00
Jerome Marchand d12c67c42c tools: Replace btf__get_from_id() with btf__load_from_kernel_by_id()
Bugzilla: http://bugzilla.redhat.com/2041365

commit 86f4b7f2578f69284fa782be54e700c42c757897
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Thu Jul 29 17:20:25 2021 +0100

    tools: Replace btf__get_from_id() with btf__load_from_kernel_by_id()

    Replace the calls to function btf__get_from_id(), which we plan to
    deprecate before the library reaches v1.0, with calls to
    btf__load_from_kernel_by_id() in tools/ (bpftool, perf, selftests).
    Update the surrounding code accordingly (instead of passing a pointer to
    the btf struct, get it as a return value from the function).

    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: John Fastabend <john.fastabend@gmail.com>
    Link: https://lore.kernel.org/bpf/20210729162028.29512-6-quentin@isovalent.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:36 +02:00
Andrii Nakryiko 5b438f01d7 bpftool: Dump more info about DATASEC members
Dump succinct information for each member of DATASEC: its kinds and name. This
is extremely helpful to see at a quick glance what is inside each DATASEC of
a given BTF. Without this, one has to jump around BTF data to just find out
the name of a VAR or FUNC. DATASEC's var_secinfo member is special in that
regard because it doesn't itself contain the name of the member, delegating
that to the referenced VAR and FUNC kinds. Other kinds, like
STRUCT/UNION/FUNC/ENUM, encode member names directly and thus are clearly
identifiable in BTF dump.

The new output looks like this:

[35] DATASEC '.bss' size=0 vlen=6
        type_id=8 offset=0 size=4 (VAR 'input_bss1')
        type_id=13 offset=0 size=4 (VAR 'input_bss_weak')
        type_id=16 offset=0 size=4 (VAR 'output_bss1')
        type_id=17 offset=0 size=4 (VAR 'output_data1')
        type_id=18 offset=0 size=4 (VAR 'output_rodata1')
        type_id=20 offset=0 size=8 (VAR 'output_sink1')
[36] DATASEC '.data' size=0 vlen=2
        type_id=9 offset=0 size=4 (VAR 'input_data1')
        type_id=14 offset=0 size=4 (VAR 'input_data_weak')
[37] DATASEC '.kconfig' size=0 vlen=2
        type_id=25 offset=0 size=4 (VAR 'LINUX_KERNEL_VERSION')
        type_id=28 offset=0 size=1 (VAR 'CONFIG_BPF_SYSCALL')
[38] DATASEC '.ksyms' size=0 vlen=1
        type_id=30 offset=0 size=1 (VAR 'bpf_link_fops')
[39] DATASEC '.rodata' size=0 vlen=2
        type_id=12 offset=0 size=4 (VAR 'input_rodata1')
        type_id=15 offset=0 size=4 (VAR 'input_rodata_weak')
[40] DATASEC 'license' size=0 vlen=1
        type_id=24 offset=0 size=4 (VAR 'LICENSE')

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210423181348.1801389-3-andrii@kernel.org
2021-04-23 14:05:26 -07:00
Andrii Nakryiko 0dd7e456bb bpftool: Support dumping BTF VAR's "extern" linkage
Add dumping of "extern" linkage for BTF VAR kind. Also shorten
"global-allocated" to "global" to be in line with FUNC's "global".

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210423181348.1801389-2-andrii@kernel.org
2021-04-23 14:05:25 -07:00
Andrii Nakryiko 4bbb358368 bpftool: Fix maybe-uninitialized warnings
Somehow when bpftool is compiled in -Og mode, compiler produces new warnings
about possibly uninitialized variables. Fix all the reported problems.

Fixes: 2119f2189d ("bpftool: add C output format option to btf dump subcommand")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210313210920.1959628-3-andrii@kernel.org
2021-03-16 12:26:49 -07:00
Ilya Leoshkevich 737e0f919a tools/bpftool: Add BTF_KIND_FLOAT support
Only dumping support needs to be adjusted, the code structure follows
that of BTF_KIND_INT. Example plain and JSON outputs:

    [4] FLOAT 'float' size=4
    {"id":4,"kind":"FLOAT","name":"float","size":4}

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210226202256.116518-5-iii@linux.ibm.com
2021-03-04 17:58:15 -08:00
Jakub Kicinski a1dd1d8697 Merge https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Alexei Starovoitov says:

====================
pull-request: bpf-next 2020-12-03

The main changes are:

1) Support BTF in kernel modules, from Andrii.

2) Introduce preferred busy-polling, from Björn.

3) bpf_ima_inode_hash() and bpf_bprm_opts_set() helpers, from KP Singh.

4) Memcg-based memory accounting for bpf objects, from Roman.

5) Allow bpf_{s,g}etsockopt from cgroup bind{4,6} hooks, from Stanislav.

* https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: (118 commits)
  selftests/bpf: Fix invalid use of strncat in test_sockmap
  libbpf: Use memcpy instead of strncpy to please GCC
  selftests/bpf: Add fentry/fexit/fmod_ret selftest for kernel module
  selftests/bpf: Add tp_btf CO-RE reloc test for modules
  libbpf: Support attachment of BPF tracing programs to kernel modules
  libbpf: Factor out low-level BPF program loading helper
  bpf: Allow to specify kernel module BTFs when attaching BPF programs
  bpf: Remove hard-coded btf_vmlinux assumption from BPF verifier
  selftests/bpf: Add CO-RE relocs selftest relying on kernel module BTF
  selftests/bpf: Add support for marking sub-tests as skipped
  selftests/bpf: Add bpf_testmod kernel module for testing
  libbpf: Add kernel module BTF support for CO-RE relocations
  libbpf: Refactor CO-RE relocs to not assume a single BTF object
  libbpf: Add internal helper to load BTF data by FD
  bpf: Keep module's btf_data_size intact after load
  bpf: Fix bpf_put_raw_tracepoint()'s use of __module_address()
  selftests/bpf: Add Userspace tests for TCP_WINDOW_CLAMP
  bpf: Adds support for setting window clamp
  samples/bpf: Fix spelling mistake "recieving" -> "receiving"
  bpf: Fix cold build of test_progs-no_alu32
  ...
====================

Link: https://lore.kernel.org/r/20201204021936.85653-1-alexei.starovoitov@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2020-12-04 07:48:12 -08:00
Jakub Kicinski 55fd59b003 Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
Conflicts:
	drivers/net/ethernet/ibm/ibmvnic.c

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2020-12-03 15:44:09 -08:00
Andrii Nakryiko fa4528379a tools/bpftool: Auto-detect split BTFs in common cases
In case of working with module's split BTF from /sys/kernel/btf/*,
auto-substitute /sys/kernel/btf/vmlinux as the base BTF. This makes using
bpftool with module BTFs faster and simpler.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201202065244.530571-4-andrii@kernel.org
2020-12-03 10:17:27 -08:00
Andrii Nakryiko 71ccb50074 tools/bpftool: Emit name <anon> for anonymous BTFs
For consistency of output, emit "name <anon>" for BTFs without the name. This
keeps output more consistent and obvious.

Suggested-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201202065244.530571-2-andrii@kernel.org
2020-12-03 10:17:26 -08:00
Zhen Lei 68878a5c5b bpftool: Fix error return value in build_btf_type_table
An appropriate return value should be set on the failed path.

Fixes: 4d374ba0bf ("tools: bpftool: implement "bpftool btf show|list"")
Reported-by: Hulk Robot <hulkci@huawei.com>
Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20201124104100.491-1-thunder.leizhen@huawei.com
2020-11-24 23:19:18 +01:00
Andrii Nakryiko cecaf4a0f2 tools/bpftool: Add support for in-kernel and named BTF in `btf show`
Display vmlinux BTF name and kernel module names when listing available BTFs
on the system.

In human-readable output mode, module BTFs are reported with "name
[module-name]", while vmlinux BTF will be reported as "name [vmlinux]".
Square brackets are added by bpftool and follow kernel convention when
displaying modules in human-readable text outputs.

[vmuser@archvm bpf]$ sudo ../../../bpf/bpftool/bpftool btf s
1: name [vmlinux]  size 4082281B
6: size 2365B  prog_ids 8,6  map_ids 3
7: name [button]  size 46895B
8: name [pcspkr]  size 42328B
9: name [serio_raw]  size 39375B
10: name [floppy]  size 57185B
11: name [i2c_core]  size 76186B
12: name [crc32c_intel]  size 16036B
13: name [i2c_piix4]  size 50497B
14: name [irqbypass]  size 14124B
15: name [kvm]  size 197985B
16: name [kvm_intel]  size 123564B
17: name [cryptd]  size 42466B
18: name [crypto_simd]  size 17187B
19: name [glue_helper]  size 39205B
20: name [aesni_intel]  size 41034B
25: size 36150B
        pids bpftool(2519)

In JSON mode, two fields (boolean "kernel" and string "name") are reported for
each BTF object. vmlinux BTF is reported with name "vmlinux" (kernel itself
returns and empty name for vmlinux BTF).

[vmuser@archvm bpf]$ sudo ../../../bpf/bpftool/bpftool btf s -jp
[{
        "id": 1,
        "size": 4082281,
        "prog_ids": [],
        "map_ids": [],
        "kernel": true,
        "name": "vmlinux"
    },{
        "id": 6,
        "size": 2365,
        "prog_ids": [8,6
        ],
        "map_ids": [3
        ],
        "kernel": false
    },{
        "id": 7,
        "size": 46895,
        "prog_ids": [],
        "map_ids": [],
        "kernel": true,
        "name": "button"
    },{

...

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Tested-by: Alan Maguire <alan.maguire@oracle.com>
Link: https://lore.kernel.org/bpf/20201110011932.3201430-6-andrii@kernel.org
2020-11-10 15:25:53 -08:00
Andrii Nakryiko 75fa177769 tools/bpftool: Add bpftool support for split BTF
Add ability to work with split BTF by providing extra -B flag, which allows to
specify the path to the base BTF file.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20201105043402.2530976-12-andrii@kernel.org
2020-11-05 18:37:31 -08:00
Andrii Nakryiko 8526df0457 tools/bpftool: Use libbpf's btf__parse() API for parsing BTF from file
Use generic libbpf API to parse BTF data from file, instead of re-implementing
it in bpftool.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200802013219.864880-3-andriin@fb.com
2020-08-03 16:39:48 +02:00
Tianjia Zhang 041549b7b2 tools, bpftool: Fix wrong return value in do_dump()
In case of btf_id does not exist, a negative error code -ENOENT
should be returned.

Fixes: c93cc69004 ("bpftool: add ability to dump BTF types")
Signed-off-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Tobias Klauser <tklauser@distanz.ch>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200802111540.5384-1-tianjia.zhang@linux.alibaba.com
2020-08-03 16:31:18 +02:00
Andrii Nakryiko d53dee3fe0 tools/bpftool: Show info for processes holding BPF map/prog/link/btf FDs
Add bpf_iter-based way to find all the processes that hold open FDs against
BPF object (map, prog, link, btf). bpftool always attempts to discover this,
but will silently give up if kernel doesn't yet support bpf_iter BPF programs.
Process name and PID are emitted for each process (task group).

Sample output for each of 4 BPF objects:

$ sudo ./bpftool prog show
2694: cgroup_device  tag 8c42dee26e8cd4c2  gpl
        loaded_at 2020-06-16T15:34:32-0700  uid 0
        xlated 648B  jited 409B  memlock 4096B
        pids systemd(1)
2907: cgroup_skb  name egress  tag 9ad187367cf2b9e8  gpl
        loaded_at 2020-06-16T18:06:54-0700  uid 0
        xlated 48B  jited 59B  memlock 4096B  map_ids 2436
        btf_id 1202
        pids test_progs(2238417), test_progs(2238445)

$ sudo ./bpftool map show
2436: array  name test_cgr.bss  flags 0x400
        key 4B  value 8B  max_entries 1  memlock 8192B
        btf_id 1202
        pids test_progs(2238417), test_progs(2238445)
2445: array  name pid_iter.rodata  flags 0x480
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 1214  frozen
        pids bpftool(2239612)

$ sudo ./bpftool link show
61: cgroup  prog 2908
        cgroup_id 375301  attach_type egress
        pids test_progs(2238417), test_progs(2238445)
62: cgroup  prog 2908
        cgroup_id 375344  attach_type egress
        pids test_progs(2238417), test_progs(2238445)

$ sudo ./bpftool btf show
1202: size 1527B  prog_ids 2908,2907  map_ids 2436
        pids test_progs(2238417), test_progs(2238445)
1242: size 34684B
        pids bpftool(2258892)

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200619231703.738941-9-andriin@fb.com
2020-06-22 17:01:49 -07:00
Tobias Klauser dc3ca5cf3e tools, bpftool: Print correct error message when failing to load BTF
btf__parse_raw and btf__parse_elf return negative error numbers wrapped
in an ERR_PTR, so the extracted value needs to be negated before passing
them to strerror which expects a positive error number.

Before:
  Error: failed to load BTF from .../vmlinux: Unknown error -2

After:
  Error: failed to load BTF from .../vmlinux: No such file or directory

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200525135421.4154-1-tklauser@distanz.ch
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-06-01 14:38:19 -07:00
Quentin Monnet 90040351a8 tools, bpftool: Clean subcommand help messages
This is a clean-up for the formatting of the do_help functions for
bpftool's subcommands. The following fixes are included:

- Do not use argv[-2] for "iter" help message, as the help is shown by
  default if no "iter" action is selected, resulting in messages looking
  like "./bpftool bpftool pin...".

- Do not print unused HELP_SPEC_PROGRAM in help message for "bpftool
  link".

- Andrii used argument indexing to avoid having multiple occurrences of
  bin_name and argv[-2] in the fprintf() for the help message, for
  "bpftool gen" and "bpftool link". Let's reuse this for all other help
  functions. We can remove up to thirty arguments for the "bpftool map"
  help message.

- Harmonise all functions, e.g. use ending quotes-comma on a separate
  line.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200523010751.23465-1-quentin@isovalent.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-06-01 14:38:18 -07:00
Jagadeesh Pagadala 93e5168947 tools/bpf/bpftool: Remove duplicate headers
Code cleanup: Remove duplicate headers which are included twice.

Signed-off-by: Jagadeesh Pagadala <jagdsh.linux@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/1587274757-14101-1-git-send-email-jagdsh.linux@gmail.com
2020-04-26 08:40:01 -07:00
Andrii Nakryiko ca7dc2791b bpftool: Add header guards to generated vmlinux.h
Add canonical #ifndef/#define/#endif guard for generated vmlinux.h header with
__VMLINUX_H__ symbol. __VMLINUX_H__ is also going to play double role of
identifying whether vmlinux.h is being used, versus, say, BCC or non-CO-RE
libbpf modes with dependency on kernel headers. This will make it possible to
write helper macro/functions, agnostic to exact BPF program set up.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200229231112.1240137-2-andriin@fb.com
2020-03-02 16:25:14 -08:00
Andrii Nakryiko 41258289a8 bpftool: Print function linkage in BTF dump
Add printing out BTF_KIND_FUNC's linkage.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20200124054317.2459436-1-andriin@fb.com
2020-01-24 11:09:56 +01:00
Toke Høiland-Jørgensen 229c3b47b7 bpftool: Use consistent include paths for libbpf
Fix bpftool to include libbpf header files with the bpf/ prefix, to be
consistent with external users of the library. Also ensure that all
includes of exported libbpf header files (those that are exported on 'make
install' of the library) use bracketed includes instead of quoted.

To make sure no new files are introduced that doesn't include the bpf/
prefix in its include, remove tools/lib/bpf from the include path entirely,
and use tools/lib instead.

Fixes: 6910d7d386 ("selftests/bpf: Ensure bpf_helper_defs.h are taken from selftests dir")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/157952560684.1683545.4765181397974997027.stgit@toke.dk
2020-01-20 16:37:45 -08:00
Andrii Nakryiko 1cf5b23988 bpftool: Apply preserve_access_index attribute to all types in BTF dump
This patch makes structs and unions, emitted through BTF dump, automatically
CO-RE-relocatable (unless disabled with `#define BPF_NO_PRESERVE_ACCESS_INDEX`,
specified before including generated header file).

This effectivaly turns usual bpf_probe_read() call into equivalent of
bpf_core_read(), by automatically applying builtin_preserve_access_index to
any field accesses of types in generated C types header.

This is especially useful for tp_btf/fentry/fexit BPF program types. They
allow direct memory access, so BPF C code just uses straightfoward a->b->c
access pattern to read data from kernel. But without kernel structs marked as
CO-RE relocatable through preserve_access_index attribute, one has to enclose
all the data reads into a special __builtin_preserve_access_index code block,
like so:

__builtin_preserve_access_index(({
    x = p->pid; /* where p is struct task_struct *, for example */
}));

This is very inconvenient and obscures the logic quite a bit. By marking all
auto-generated types with preserve_access_index attribute the above code is
reduced to just a clean and natural `x = p->pid;`.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200113073143.1779940-5-andriin@fb.com
2020-01-13 17:48:13 -08:00
Quentin Monnet a0f17cc666 tools, bpftool: Fix warning on ignored return value for 'read'
When building bpftool, a warning was introduced by commit a943646036
("bpftool: Allow to read btf as raw data"), because the return value
from a call to 'read()' is ignored. Let's address it.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20191119111706.22440-1-quentin.monnet@netronome.com
2019-11-24 16:58:44 -08:00
Jiri Olsa a943646036 bpftool: Allow to read btf as raw data
The bpftool interface stays the same, but now it's possible
to run it over BTF raw data, like:

  $ bpftool btf dump file /sys/kernel/btf/vmlinux
  [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
  [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
  [3] CONST '(anon)' type_id=2

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Link: https://lore.kernel.org/bpf/20191024133025.10691-1-jolsa@kernel.org
2019-10-25 23:34:47 +02:00
Quentin Monnet 4d374ba0bf tools: bpftool: implement "bpftool btf show|list"
Add a "btf list" (alias: "btf show") subcommand to bpftool in order to
dump all BTF objects loaded on a system.

When running the command, hash tables are built in bpftool to retrieve
all the associations between BTF objects and BPF maps and programs. This
allows for printing all such associations when listing the BTF objects.

The command is added at the top of the subcommands for "bpftool btf", so
that typing only "bpftool btf" also comes down to listing the programs.
We could not have this with the previous command ("dump"), which
required a BTF object id, so it should not break any previous behaviour.
This also makes the "btf" command behaviour consistent with "prog" or
"map".

Bash completion is updated to use "bpftool btf" instead of "bpftool
prog" to list the BTF ids, as it looks more consistent.

Example output (plain):

    # bpftool btf show
    9: size 2989B  prog_ids 21  map_ids 15
    17: size 2847B  prog_ids 36  map_ids 30,29,28
    26: size 2847B

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-08-20 09:51:06 -07:00
Quentin Monnet ed4a3983cd tools: bpftool: fix argument for p_err() in BTF do_dump()
The last argument passed to one call to the p_err() function is not
correct, it should be "*argv" instead of "**argv". This may lead to a
segmentation fault error if BTF id cannot be parsed correctly. Let's fix
this.

Fixes: c93cc69004dt ("bpftool: add ability to dump BTF types")
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-08-15 22:06:46 -07:00
Andrii Nakryiko 2119f2189d bpftool: add C output format option to btf dump subcommand
Utilize new libbpf's btf_dump API to emit BTF as a C definitions.

Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-24 14:05:58 -07:00