Commit Graph

103 Commits

Author SHA1 Message Date
Viktor Malik a411de1c99
bpftool: Mount bpffs on provided dir instead of parent dir
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 478a535ae54ad3831371904d93b5dfc403222e17
Author: Sahil Siddiq <icegambit91@gmail.com>
Date:   Fri Apr 5 00:52:19 2024 +0530

    bpftool: Mount bpffs on provided dir instead of parent dir
    
    When pinning programs/objects under PATH (eg: during "bpftool prog
    loadall") the bpffs is mounted on the parent dir of PATH in the
    following situations:
    - the given dir exists but it is not bpffs.
    - the given dir doesn't exist and the parent dir is not bpffs.
    
    Mounting on the parent dir can also have the unintentional side-
    effect of hiding other files located under the parent dir.
    
    If the given dir exists but is not bpffs, then the bpffs should
    be mounted on the given dir and not its parent dir.
    
    Similarly, if the given dir doesn't exist and its parent dir is not
    bpffs, then the given dir should be created and the bpffs should be
    mounted on this new dir.
    
    Fixes: 2a36c26fe3b8 ("bpftool: Support bpffs mountpoint as pin path for prog loadall")
    Signed-off-by: Sahil Siddiq <icegambit91@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/2da44d24-74ae-a564-1764-afccf395eeec@isovalent.com/T/#t
    Link: https://lore.kernel.org/bpf/20240404192219.52373-1-icegambit91@gmail.com
    
    Closes: https://github.com/libbpf/bpftool/issues/100
    
    Changes since v1:
     - Split "mount_bpffs_for_pin" into two functions.
       This is done to improve maintainability and readability.
    
    Changes since v2:
    - mount_bpffs_for_pin: rename to "create_and_mount_bpffs_dir".
    - mount_bpffs_given_file: rename to "mount_bpffs_given_file".
    - create_and_mount_bpffs_dir:
      - introduce "dir_exists" boolean.
      - remove new dir if "mnt_fs" fails.
    - improve error handling and error messages.
    
    Changes since v3:
    - Rectify function name.
    - Improve error messages and formatting.
    - mount_bpffs_for_file:
      - Check if dir exists before block_mount check.
    
    Changes since v4:
    - Use strdup instead of strcpy.
    - create_and_mount_bpffs_dir:
      - Use S_IRWXU instead of 0700.
    - Improve error handling and formatting.

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:44 +01:00
Viktor Malik 484e6a1603
bpftool: Support bpffs mountpoint as pin path for prog loadall
JIRA: https://issues.redhat.com/browse/RHEL-9957

commit 2a36c26fe3b8e2cf39e15e80ba1abc889a75da4f
Author: Pengcheng Yang <yangpc@wangsu.com>
Date:   Sat May 6 11:07:19 2023 +0800

    bpftool: Support bpffs mountpoint as pin path for prog loadall
    
    Currently, when using prog loadall and the pin path is a bpffs mountpoint,
    bpffs will be repeatedly mounted to the parent directory of the bpffs
    mountpoint path. For example, a `bpftool prog loadall test.o /sys/fs/bpf`
    will trigger this.
    
    Signed-off-by: Pengcheng Yang <yangpc@wangsu.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/1683342439-3677-1-git-send-email-yangpc@wangsu.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-10-12 11:41:06 +02:00
Artem Savkov 9bff341d53 tools: bpftool: print netfilter link info
Bugzilla: https://bugzilla.redhat.com/2221599

commit d0fe92fb5e3df6991c640fb9205d880b68603259
Author: Florian Westphal <fw@strlen.de>
Date:   Fri Apr 21 19:02:58 2023 +0200

    tools: bpftool: print netfilter link info
    
    Dump protocol family, hook and priority value:
    $ bpftool link
    2: netfilter  prog 14
            ip input prio -128
            pids install(3264)
    5: netfilter  prog 14
            ip6 forward prio 21
            pids a.out(3387)
    9: netfilter  prog 14
            ip prerouting prio 123
            pids a.out(5700)
    10: netfilter  prog 14
            ip input prio 21
            pids test2(5701)
    
    v2: Quentin Monnet suggested to also add 'bpftool net' support:
    
    $ bpftool net
    xdp:
    
    tc:
    
    flow_dissector:
    
    netfilter:
    
            ip prerouting prio 21 prog_id 14
            ip input prio -128 prog_id 14
            ip input prio 21 prog_id 14
            ip forward prio 21 prog_id 14
            ip output prio 21 prog_id 14
            ip postrouting prio 21 prog_id 14
    
    'bpftool net' only dumps netfilter link type, links are sorted by protocol
    family, hook and priority.
    
    v5: fix bpf ci failure: libbpf needs small update to prog_type_name[]
        and probe_prog_load helper.
    v4: don't fail with -EOPNOTSUPP in libbpf probe_prog_load, update
        prog_type_name[] with "netfilter" entry (bpf ci)
    v3: fix bpf.h copy, 'reserved' member was removed (Alexei)
        use p_err, not fprintf (Quentin)
    
    Suggested-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/eeeaac99-9053-90c2-aa33-cc1ecb1ae9ca@isovalent.com/
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Florian Westphal <fw@strlen.de>
    Link: https://lore.kernel.org/r/20230421170300.24115-6-fw@strlen.de
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:33 +02:00
Artem Savkov c0e51e119e bpftool: Register struct_ops with a link.
Bugzilla: https://bugzilla.redhat.com/2221599

commit 0232b788978652571c4f4e57dc26ff8e4837926a
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Wed Apr 19 17:28:21 2023 -0700

    bpftool: Register struct_ops with a link.
    
    You can include an optional path after specifying the object name for the
    'struct_ops register' subcommand.
    
    Since the commit 226bc6ae6405 ("Merge branch 'Transit between BPF TCP
    congestion controls.'") has been accepted, it is now possible to create a
    link for a struct_ops. This can be done by defining a struct_ops in
    SEC(".struct_ops.link") to make libbpf returns a real link. If we don't pin
    the links before leaving bpftool, they will disappear. To instruct bpftool
    to pin the links in a directory with the names of the maps, we need to
    provide the path of that directory.
    
    Signed-off-by: Kui-Feng Lee <kuifeng@meta.com>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/r/20230420002822.345222-1-kuifeng@meta.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:32 +02:00
Artem Savkov 788b01ae0e bpftool: Support printing opcodes and source file references in CFG
Bugzilla: https://bugzilla.redhat.com/2221599

commit 7483a7a70a12d7c00c9f80574d533b01689d39a7
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Wed Apr 5 14:21:19 2023 +0100

    bpftool: Support printing opcodes and source file references in CFG
    
    Add support for displaying opcodes or/and file references (filepath,
    line and column numbers) when dumping the control flow graphs of loaded
    BPF programs with bpftool.
    
    The filepaths in the records are absolute. To avoid blocks on the graph
    to get too wide, we truncate them when they get too long (but we always
    keep the entire file name). In the unlikely case where the resulting
    file name is ambiguous, it remains possible to get the full path with a
    regular dump (no CFG).
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/r/20230405132120.59886-7-quentin@isovalent.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:26 +02:00
Artem Savkov 14e208e7fd bpftool: Support inline annotations when dumping the CFG of a program
Bugzilla: https://bugzilla.redhat.com/2221599

commit 9fd496848b1c4cd04fee5f152bb7bcec11e1b901
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Wed Apr 5 14:21:16 2023 +0100

    bpftool: Support inline annotations when dumping the CFG of a program
    
    We support dumping the control flow graph of loaded programs to the DOT
    format with bpftool, but so far this feature wouldn't display the source
    code lines available through BTF along with the eBPF bytecode. Let's add
    support for these annotations, to make it easier to read the graph.
    
    In prog.c, we move the call to dump_xlated_cfg() in order to pass and
    use the full struct dump_data, instead of creating a minimal one in
    draw_bb_node().
    
    We pass the pointer to this struct down to dump_xlated_for_graph() in
    xlated_dumper.c, where most of the logics is added. We deal with BTF
    mostly like we do for plain or JSON output, except that we cannot use a
    "nr_skip" value to skip a given number of linfo records (we don't
    process the BPF instructions linearly, and apart from the root of the
    graph we don't know how many records we should skip, so we just store
    the last linfo and make sure the new one we find is different before
    printing it).
    
    When printing the source instructions to the label of a DOT graph node,
    there are a few subtleties to address. We want some special newline
    markers, and there are some characters that we must escape. To deal with
    them, we introduce a new dedicated function btf_dump_linfo_dotlabel() in
    btf_dumper.c. We'll reuse this function in a later commit to format the
    filepath, line, and column references as well.
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/r/20230405132120.59886-4-quentin@isovalent.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:26 +02: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 c6027db6c9 bpftool: remove support of --legacy option for bpftool
Bugzilla: https://bugzilla.redhat.com/2177177

commit 9b8107553424fd87955fed257a807672c2097297
Author: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@industrialdiscipline.com>
Date:   Sun Nov 20 11:25:55 2022 +0000

    bpftool: remove support of --legacy option for bpftool

    Following:
      commit bd054102a8c7 ("libbpf: enforce strict libbpf 1.0 behaviors")
      commit 93b8952d223a ("libbpf: deprecate legacy BPF map definitions")

    The --legacy option is no longer relevant as libbpf no longer supports
    it. libbpf_set_strict_mode() is a no-op operation.

    Signed-off-by: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@industrialdiscipline.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/r/20221120112515.38165-2-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
Jerome Marchand cf1ae9f9e0 bpftool: Support setting alternative arch for JIT disasm with LLVM
Bugzilla: https://bugzilla.redhat.com/2177177

commit ce4f660862359f2172ff4e3df30e641df5475cf6
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Tue Oct 25 16:03:28 2022 +0100

    bpftool: Support setting alternative arch for JIT disasm with LLVM

    For offloaded BPF programs, instead of failing to create the
    LLVM disassembler without even looking for a triple at all, do run the
    function that attempts to retrieve a valid architecture name for the
    device.

    It will still fail for the LLVM disassembler, because currently we have
    no valid triple to return (NFP disassembly is not supported by LLVM).
    But failing in that function is more logical than to assume in
    jit_disasm.c that passing an "arch" name is simply not supported.

    Suggested-by: Song Liu <song@kernel.org>
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/r/20221025150329.97371-8-quentin@isovalent.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:56 +02:00
Jerome Marchand c4937c4f03 bpftool: Add LLVM as default library for disassembling JIT-ed programs
Bugzilla: https://bugzilla.redhat.com/2177177

commit eb9d1acf634baf6401dfb4f67dc895290713a357
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Tue Oct 25 16:03:27 2022 +0100

    bpftool: Add LLVM as default library for disassembling JIT-ed programs

    To disassemble instructions for JIT-ed programs, bpftool has relied on
    the libbfd library. This has been problematic in the past: libbfd's
    interface is not meant to be stable and has changed several times. For
    building bpftool, we have to detect how the libbfd version on the system
    behaves, which is why we have to handle features disassembler-four-args
    and disassembler-init-styled in the Makefile. When it comes to shipping
    bpftool, this has also caused issues with several distribution
    maintainers unwilling to support the feature (see for example Debian's
    page for binutils-dev, which ships libbfd: "Note that building Debian
    packages which depend on the shared libbfd is Not Allowed." [0]).

    For these reasons, we add support for LLVM as an alternative to libbfd
    for disassembling instructions of JIT-ed programs. Thanks to the
    preparation work in the previous commits, it's easy to add the library
    by passing the relevant compilation options in the Makefile, and by
    adding the functions for setting up the LLVM disassembler in file
    jit_disasm.c.

    The LLVM disassembler requires the LLVM development package (usually
    llvm-dev or llvm-devel).

    The expectation is that the interface for this disassembler will be more
    stable. There is a note in LLVM's Developer Policy [1] stating that the
    stability for the C API is "best effort" and not guaranteed, but at
    least there is some effort to keep compatibility when possible (which
    hasn't really been the case for libbfd so far). Furthermore, the Debian
    page for the related LLVM package does not caution against linking to
    the lib, as binutils-dev page does.

    Naturally, the display of disassembled instructions comes with a few
    minor differences. Here is a sample output with libbfd (already
    supported before this patch):

        # bpftool prog dump jited id 56
        bpf_prog_6deef7357e7b4530:
           0:   nopl   0x0(%rax,%rax,1)
           5:   xchg   %ax,%ax
           7:   push   %rbp
           8:   mov    %rsp,%rbp
           b:   push   %rbx
           c:   push   %r13
           e:   push   %r14
          10:   mov    %rdi,%rbx
          13:   movzwq 0xb4(%rbx),%r13
          1b:   xor    %r14d,%r14d
          1e:   or     $0x2,%r14d
          22:   mov    $0x1,%eax
          27:   cmp    $0x2,%r14
          2b:   jne    0x000000000000002f
          2d:   xor    %eax,%eax
          2f:   pop    %r14
          31:   pop    %r13
          33:   pop    %rbx
          34:   leave
          35:   ret

    LLVM supports several variants that we could set when initialising the
    disassembler, for example with:

        LLVMSetDisasmOptions(*ctx,
                             LLVMDisassembler_Option_AsmPrinterVariant);

    but the default printer is used for now. Here is the output with LLVM:

        # bpftool prog dump jited id 56
        bpf_prog_6deef7357e7b4530:
           0:   nopl    (%rax,%rax)
           5:   nop
           7:   pushq   %rbp
           8:   movq    %rsp, %rbp
           b:   pushq   %rbx
           c:   pushq   %r13
           e:   pushq   %r14
          10:   movq    %rdi, %rbx
          13:   movzwq  180(%rbx), %r13
          1b:   xorl    %r14d, %r14d
          1e:   orl     $2, %r14d
          22:   movl    $1, %eax
          27:   cmpq    $2, %r14
          2b:   jne     0x2f
          2d:   xorl    %eax, %eax
          2f:   popq    %r14
          31:   popq    %r13
          33:   popq    %rbx
          34:   leave
          35:   retq

    The LLVM disassembler comes as the default choice, with libbfd as a
    fall-back.

    Of course, we could replace libbfd entirely and avoid supporting two
    different libraries. One reason for keeping libbfd is that, right now,
    it works well, we have all we need in terms of features detection in the
    Makefile, so it provides a fallback for disassembling JIT-ed programs if
    libbfd is installed but LLVM is not. The other motivation is that libbfd
    supports nfp instruction for Netronome's SmartNICs and can be used to
    disassemble offloaded programs, something that LLVM cannot do. If
    libbfd's interface breaks again in the future, we might reconsider
    keeping support for it.

    [0] https://packages.debian.org/buster/binutils-dev
    [1] https://llvm.org/docs/DeveloperPolicy.html#c-api-changes

    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Tested-by: Niklas Söderlund <niklas.soderlund@corigine.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20221025150329.97371-7-quentin@isovalent.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:56 +02:00
Jerome Marchand f083571145 bpftool: Remove asserts from JIT disassembler
Bugzilla: https://bugzilla.redhat.com/2177177

commit 55b4de58d0e2aca810ed2b198a0173640300acf8
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Tue Oct 25 16:03:23 2022 +0100

    bpftool: Remove asserts from JIT disassembler

    The JIT disassembler in bpftool is the only components (with the JSON
    writer) using asserts to check the return values of functions. But it
    does not do so in a consistent way, and diasm_print_insn() returns no
    value, although sometimes the operation failed.

    Remove the asserts, and instead check the return values, print messages
    on errors, and propagate the error to the caller from prog.c.

    Remove the inclusion of assert.h from jit_disasm.c, and also from map.c
    where it is unused.

    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Tested-by: Niklas Söderlund <niklas.soderlund@corigine.com>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/r/20221025150329.97371-3-quentin@isovalent.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:55 +02:00
Artem Savkov 9c9dbe8c86 bpftool: Remove attach_type_name forward declaration
Bugzilla: https://bugzilla.redhat.com/2137876

commit b0cbd6154a9a3964490cafa0a9444be626271bd9
Author: Tobias Klauser <tklauser@distanz.ch>
Date:   Thu Jun 30 11:36:38 2022 +0200

    bpftool: Remove attach_type_name forward declaration
    
    The attach_type_name definition was removed in commit 1ba5ad36e00f
    ("bpftool: Use libbpf_bpf_attach_type_str"). Remove its forward
    declaration in main.h as well.
    
    Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220630093638.25916-1-tklauser@distanz.ch

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:34 +01:00
Artem Savkov 03a91b85bf bpftool: Use libbpf_bpf_attach_type_str
Bugzilla: https://bugzilla.redhat.com/2137876

commit 1ba5ad36e00f46e3f7676f5de6b87f5a2f57f1f1
Author: Daniel Müller <deso@posteo.net>
Date:   Mon May 23 23:04:25 2022 +0000

    bpftool: Use libbpf_bpf_attach_type_str
    
    This change switches bpftool over to using the recently introduced
    libbpf_bpf_attach_type_str function instead of maintaining its own
    string representation for the bpf_attach_type enum.
    
    Note that contrary to other enum types, the variant names that bpftool
    maps bpf_attach_type to do not adhere a simple to follow rule. With
    bpf_prog_type, for example, the textual representation can easily be
    inferred by stripping the BPF_PROG_TYPE_ prefix and lowercasing the
    remaining string. bpf_attach_type violates this rule for various
    variants.
    We decided to fix up this deficiency with this change, meaning that
    bpftool uses the same textual representations as libbpf. Supporting
    tests, completion scripts, and man pages have been adjusted accordingly.
    However, we did add support for accepting (the now undocumented)
    original attach type names when they are provided by users.
    
    For the test (test_bpftool_synctypes.py), I have removed the enum
    representation checks, because we no longer mirror the various enum
    variant names in bpftool source code. For the man page, help text, and
    completion script checks we are now using enum definitions from
    uapi/linux/bpf.h as the source of truth directly.
    
    Signed-off-by: Daniel Müller <deso@posteo.net>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220523230428.3077108-10-deso@posteo.net

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:28 +01:00
Artem Savkov 2aeec8f07b bpftool: Use libbpf_bpf_map_type_str
Bugzilla: https://bugzilla.redhat.com/2137876

commit 2e98964bd6e283568730b1a4da3b1e4da3306a8e
Author: Daniel Müller <deso@posteo.net>
Date:   Mon May 23 23:04:22 2022 +0000

    bpftool: Use libbpf_bpf_map_type_str
    
    This change switches bpftool over to using the recently introduced
    libbpf_bpf_map_type_str function instead of maintaining its own string
    representation for the bpf_map_type enum.
    
    Signed-off-by: Daniel Müller <deso@posteo.net>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220523230428.3077108-7-deso@posteo.net

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:28 +01:00
Artem Savkov ea100e07ed bpftool: Use libbpf_bpf_prog_type_str
Bugzilla: https://bugzilla.redhat.com/2137876

commit b700eeb406a6c1f4d955242e06151f11f13d3e29
Author: Daniel Müller <deso@posteo.net>
Date:   Mon May 23 23:04:19 2022 +0000

    bpftool: Use libbpf_bpf_prog_type_str
    
    This change switches bpftool over to using the recently introduced
    libbpf_bpf_prog_type_str function instead of maintaining its own string
    representation for the bpf_prog_type enum.
    
    Signed-off-by: Daniel Müller <deso@posteo.net>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Yonghong Song <yhs@fb.com>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220523230428.3077108-4-deso@posteo.net

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:28 +01:00
Jerome Marchand 89008b6063 bpftool: Add bpf_cookie to link output
Bugzilla: https://bugzilla.redhat.com/2120966

commit cbdaf71f7e65a45d6e96378ee7bfe3da39c30908
Author: Dmitrii Dolgov <9erthalion6@gmail.com>
Date:   Wed Mar 9 17:31:12 2022 +0100

    bpftool: Add bpf_cookie to link output

    Commit 82e6b1eee6a8 ("bpf: Allow to specify user-provided bpf_cookie for
    BPF perf links") introduced the concept of user specified bpf_cookie,
    which could be accessed by BPF programs using bpf_get_attach_cookie().
    For troubleshooting purposes it is convenient to expose bpf_cookie via
    bpftool as well, so there is no need to meddle with the target BPF
    program itself.

    Implemented using the pid iterator BPF program to actually fetch
    bpf_cookies, which allows constraining code changes only to bpftool.

    $ bpftool link
    1: type 7  prog 5
            bpf_cookie 123
            pids bootstrap(81)

    Signed-off-by: Dmitrii Dolgov <9erthalion6@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Yonghong Song <yhs@fb.com>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220309163112.24141-1-9erthalion6@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:03 +02:00
Jerome Marchand 8aff886903 bpftool: Remove usage of reallocarray()
Bugzilla: https://bugzilla.redhat.com/2120966

commit a19df7139440258e02126f1c795ba64932a8e949
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Mon Feb 21 07:56:17 2022 -0500

    bpftool: Remove usage of reallocarray()

    This commit fixes a compilation error on systems with glibc < 2.26 [0]:

    ```
    In file included from main.h:14:0,
                     from gen.c:24:
    linux/tools/include/tools/libc_compat.h:11:21: error: attempt to use poisoned "reallocarray"
     static inline void *reallocarray(void *ptr, size_t nmemb, size_t size)
    ```

    This happens because gen.c pulls <bpf/libbpf_internal.h>, and then
    <tools/libc_compat.h> (through main.h). When
    COMPAT_NEED_REALLOCARRAY is set, libc_compat.h defines reallocarray()
    which libbpf_internal.h poisons with a GCC pragma.

    This commit reuses libbpf_reallocarray() implemented in commit
    029258d7b2 ("libbpf: Remove any use of reallocarray() in libbpf").

    v1 -> v2:
    - reuse libbpf_reallocarray() instead of reimplementing it

    Fixes: a9caaba399f9 ("bpftool: Implement "gen min_core_btf" logic")
    Reported-by: Quentin Monnet <quentin@isovalent.com>
    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/20220221125617.39610-1-mauricio@kinvolk.io

    [0]: https://lore.kernel.org/bpf/3bf2bd49-9f2d-a2df-5536-bc0dde70a83b@isovalent.com/

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:51 +02:00
Jerome Marchand 8e560069b1 bpftool: Adding support for BTF program names
Bugzilla: https://bugzilla.redhat.com/2120966

commit b662000aff84f2ca9660db15e5f8ac926681df27
Author: Raman Shukhau <ramasha@fb.com>
Date:   Wed Jan 19 02:02:55 2022 -0800

    bpftool: Adding support for BTF program names

    `bpftool prog list` and other bpftool subcommands that show
    BPF program names currently get them from bpf_prog_info.name.
    That field is limited to 16 (BPF_OBJ_NAME_LEN) chars which leads
    to truncated names since many progs have much longer names.

    The idea of this change is to improve all bpftool commands that
    output prog name so that bpftool uses info from BTF to print
    program names if available.

    It tries bpf_prog_info.name first and fall back to btf only if
    the name is suspected to be truncated (has 15 chars length).

    Right now `bpftool p show id <id>` returns capped prog name

    <id>: kprobe  name example_cap_cap  tag 712e...
    ...

    With this change it would return

    <id>: kprobe  name example_cap_capable  tag 712e...
    ...

    Note, other commands that print prog names (e.g. "bpftool
    cgroup tree") are also addressed in this change.

    Signed-off-by: Raman Shukhau <ramasha@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220119100255.1068997-1-ramasha@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:40 +02:00
Yauheni Kaliuta 7a361a5ee9 bpftool: Enable libbpf's strict mode by default
Bugzilla: http://bugzilla.redhat.com/2100795

commit 314f14abdeca78de6b16f97d796a9966ce4b90ae
Author: Stanislav Fomichev <sdf@google.com>
Date:   Wed Nov 10 11:23:24 2021 -0800

    bpftool: Enable libbpf's strict mode by default
    
    Otherwise, attaching with bpftool doesn't work with strict section names.
    
    Also:
    
      - Add --legacy option to switch back to pre-1.0 behavior
      - Print a warning when program fails to load in strict mode to
        point to --legacy flag
      - By default, don't append / to the section name; in strict
        mode it's relevant only for a small subset of prog types
    
    + bpftool --legacy prog loadall tools/testing/selftests/bpf/test_cgroup_link.o /sys/fs/bpf/kprobe type kprobe
    libbpf: failed to pin program: File exists
    Error: failed to pin all programs
    + bpftool prog loadall tools/testing/selftests/bpf/test_cgroup_link.o /sys/fs/bpf/kprobe type kprobe
    
    v1 -> v2:
      - strict by default (Quentin Monnet)
      - add more info to --legacy description (Quentin Monnet)
      - add bash completion (Quentin Monnet)
    
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20211110192324.920934-1-sdf@google.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-28 18:32:56 +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 abac33d1f9 bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects
Bugzilla: http://bugzilla.redhat.com/2069045

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

    bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects
    
    In order to show pinned paths for BPF programs, maps, or links when
    listing them with the "-f" option, bpftool creates hash maps to store
    all relevant paths under the bpffs. So far, it would rely on the
    kernel implementation (from tools/include/linux/hashtable.h).
    
    We can make bpftool rely on libbpf's implementation instead. 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 is the first step of the conversion: the hash maps for
    pinned paths for programs, maps, and links are converted to libbpf's
    hashmap.{c,h}. Other hash maps used for the PIDs of process holding
    references to BPF objects are left unchanged for now. On the build side,
    this requires adding a dependency to a second header internal to libbpf,
    and making it a dependency for the bootstrap bpftool version as well.
    The rest of the changes are a rather straightforward conversion.
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211023205154.6710-4-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:46 +03:00
Yauheni Kaliuta a947303109 bpftool: Do not expose and init hash maps for pinned path in main.c
Bugzilla: http://bugzilla.redhat.com/2069045

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

    bpftool: Do not expose and init hash maps for pinned path in main.c
    
    BPF programs, maps, and links, can all be listed with their pinned paths
    by bpftool, when the "-f" option is provided. To do so, bpftool builds
    hash maps containing all pinned paths for each kind of objects.
    
    These three hash maps are always initialised in main.c, and exposed
    through main.h. There appear to be no particular reason to do so: we can
    just as well make them static to the files that need them (prog.c,
    map.c, and link.c respectively), and initialise them only when we want
    to show objects and the "-f" switch is provided.
    
    This may prevent unnecessary memory allocations if the implementation of
    the hash maps was to change in the future.
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211023205154.6710-3-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:46 +03: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
Alexei Starovoitov d510296d33 bpftool: Use syscall/loader program in "prog load" and "gen skeleton" command.
Add -L flag to bpftool to use libbpf gen_trace facility and syscall/loader program
for skeleton generation and program loading.

"bpftool gen skeleton -L" command will generate a "light skeleton" or "loader skeleton"
that is similar to existing skeleton, but has one major difference:
$ bpftool gen skeleton lsm.o > lsm.skel.h
$ bpftool gen skeleton -L lsm.o > lsm.lskel.h
$ diff lsm.skel.h lsm.lskel.h
@@ -5,34 +4,34 @@
 #define __LSM_SKEL_H__

 #include <stdlib.h>
-#include <bpf/libbpf.h>
+#include <bpf/bpf.h>

The light skeleton does not use majority of libbpf infrastructure.
It doesn't need libelf. It doesn't parse .o file.
It only needs few sys_bpf wrappers. All of them are in bpf/bpf.h file.
In future libbpf/bpf.c can be inlined into bpf.h, so not even libbpf.a would be
needed to work with light skeleton.

"bpftool prog load -L file.o" command is introduced for debugging of syscall/loader
program generation. Just like the same command without -L it will try to load
the programs from file.o into the kernel. It won't even try to pin them.

"bpftool prog load -L -d file.o" command will provide additional debug messages
on how syscall/loader program was generated.
Also the execution of syscall/loader program will use bpf_trace_printk() for
each step of loading BTF, creating maps, and loading programs.
The user can do "cat /.../trace_pipe" for further debug.

An example of fexit_sleep.lskel.h generated from progs/fexit_sleep.c:
struct fexit_sleep {
	struct bpf_loader_ctx ctx;
	struct {
		struct bpf_map_desc bss;
	} maps;
	struct {
		struct bpf_prog_desc nanosleep_fentry;
		struct bpf_prog_desc nanosleep_fexit;
	} progs;
	struct {
		int nanosleep_fentry_fd;
		int nanosleep_fexit_fd;
	} links;
	struct fexit_sleep__bss {
		int pid;
		int fentry_cnt;
		int fexit_cnt;
	} *bss;
};

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210514003623.28033-18-alexei.starovoitov@gmail.com
2021-05-19 00:41:31 +02: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 09f44b753a tools/bpftool: Fix compilation warnings in 32-bit mode
Fix few compilation warnings in bpftool when compiling in 32-bit mode.
Abstract away u64 to pointer conversion into a helper function.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200813204945.1020225-2-andriin@fb.com
2020-08-13 16:45:41 -07:00
Tony Ambardar 9165e1d70f bpftool: Use only nftw for file tree parsing
The bpftool sources include code to walk file trees, but use multiple
frameworks to do so: nftw and fts. While nftw conforms to POSIX/SUSv3 and
is widely available, fts is not conformant and less common, especially on
non-glibc systems. The inconsistent framework usage hampers maintenance
and portability of bpftool, in particular for embedded systems.

Standardize code usage by rewriting one fts-based function to use nftw and
clean up some related function warnings by extending use of "const char *"
arguments. This change helps in building bpftool against musl for OpenWrt.

Also fix an unsafe call to dirname() by duplicating the string to pass,
since some implementations may directly alter it. The same approach is
used in libbpf.c.

Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200721024817.13701-1-Tony.Ambardar@gmail.com
2020-07-21 23:42:56 +02:00
Tobias Klauser 16d37ee3d2 tools, bpftool: Define attach_type_name array only once
Define attach_type_name in common.c instead of main.h so it is only
defined once. This leads to a slight decrease in the binary size of
bpftool.

Before:

   text	   data	    bss	    dec	    hex	filename
 399024	  11168	1573160	1983352	 1e4378	bpftool

After:

   text	   data	    bss	    dec	    hex	filename
 398256	  10880	1573160	1982296	 1e3f58	bpftool

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200624143154.13145-1-tklauser@distanz.ch
2020-06-25 16:06:01 +02:00
Tobias Klauser 9023497d87 tools, bpftool: Define prog_type_name array only once
Define prog_type_name in prog.c instead of main.h so it is only defined
once. This leads to a slight decrease in the binary size of bpftool.

Before:

   text	   data	    bss	    dec	    hex	filename
 401032	  11936	1573160	1986128	 1e4e50	bpftool

After:

   text	   data	    bss	    dec	    hex	filename
 399024	  11168	1573160	1983352	 1e4378	bpftool

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200624143124.12914-1-tklauser@distanz.ch
2020-06-25 16:06:01 +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
Andrii Nakryiko 16e9b187ab tools/bpftool: Minimize bootstrap bpftool
Build minimal "bootstrap mode" bpftool to enable skeleton (and, later,
vmlinux.h generation), instead of building almost complete, but slightly
different (w/o skeletons, etc) bpftool to bootstrap complete bpftool build.

Current approach doesn't scale well (engineering-wise) when adding more BPF
programs to bpftool and other complicated functionality, as it requires
constant adjusting of the code to work in both bootstrapped mode and normal
mode.

So it's better to build only minimal bpftool version that supports only BPF
skeleton code generation and BTF-to-C conversion. Thankfully, this is quite
easy to accomplish due to internal modularity of bpftool commands. This will
also allow to keep adding new functionality to bpftool in general, without the
need to care about bootstrap mode for those new parts of bpftool.

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-6-andriin@fb.com
2020-06-22 17:01:48 -07:00
Andrii Nakryiko a479b8ce4e tools/bpftool: Move map/prog parsing logic into common
Move functions that parse map and prog by id/tag/name/etc outside of
map.c/prog.c, respectively. These functions are used outside of those files
and are generic enough to be in common. This also makes heavy-weight map.c and
prog.c more decoupled from the rest of bpftool files and facilitates more
lightweight bootstrap bpftool variant.

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-5-andriin@fb.com
2020-06-22 17:01:48 -07:00
Daniel Borkmann 05ee19c18c bpf, bpftool: Enable get{peer, sock}name attach types
Make bpftool aware and add the new get{peer,sock}name attach types to its
cli, documentation and bash completion to allow attachment/detachment of
sock_addr programs there.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Andrey Ignatov <rdna@fb.com>
Link: https://lore.kernel.org/bpf/9765b3d03e4c29210c4df56a9cc7e52f5f7bb5ef.1589841594.git.daniel@iogearbox.net
2020-05-19 11:32:04 -07:00
Quentin Monnet 6e7e034e88 tools, bpftool: Poison and replace kernel integer typedefs
Replace the use of kernel-only integer typedefs (u8, u32, etc.) by their
user space counterpart (__u8, __u32, etc.).

Similarly to what libbpf does, poison the typedefs to avoid introducing
them again in the future.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200511161536.29853-2-quentin@isovalent.com
2020-05-11 21:20:46 +02:00
Yonghong Song 9406b485de tools/bpftool: Add bpf_iter support for bptool
Currently, only one command is supported
  bpftool iter pin <bpf_prog.o> <path>

It will pin the trace/iter bpf program in
the object file <bpf_prog.o> to the <path>
where <path> should be on a bpffs mount.

For example,
  $ bpftool iter pin ./bpf_iter_ipv6_route.o \
    /sys/fs/bpf/my_route
User can then do a `cat` to print out the results:
  $ cat /sys/fs/bpf/my_route
    fe800000000000000000000000000000 40 00000000000000000000000000000000 ...
    00000000000000000000000000000000 00 00000000000000000000000000000000 ...
    00000000000000000000000000000001 80 00000000000000000000000000000000 ...
    fe800000000000008c0162fffebdfd57 80 00000000000000000000000000000000 ...
    ff000000000000000000000000000000 08 00000000000000000000000000000000 ...
    00000000000000000000000000000000 00 00000000000000000000000000000000 ...

The implementation for ipv6_route iterator is in one of subsequent
patches.

This patch also added BPF_LINK_TYPE_ITER to link query.

In the future, we may add additional parameters to pin command
by parameterizing the bpf iterator. For example, a map_id or pid
may be added to let bpf program only traverses a single map or task,
similar to kernel seq_file single_open().

We may also add introspection command for targets/iterators by
leveraging the bpf_iter itself.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200509175920.2477247-1-yhs@fb.com
2020-05-09 17:05:27 -07:00
Andrii Nakryiko c5481f9a95 bpftool: Add bpf_link show and pin support
Add `bpftool link show` and `bpftool link pin` commands.

Example plain output for `link show` (with showing pinned paths):

[vmuser@archvm bpf]$ sudo ~/local/linux/tools/bpf/bpftool/bpftool -f link
1: tracing  prog 12
        prog_type tracing  attach_type fentry
        pinned /sys/fs/bpf/my_test_link
        pinned /sys/fs/bpf/my_test_link2
2: tracing  prog 13
        prog_type tracing  attach_type fentry
3: tracing  prog 14
        prog_type tracing  attach_type fentry
4: tracing  prog 15
        prog_type tracing  attach_type fentry
5: tracing  prog 16
        prog_type tracing  attach_type fentry
6: tracing  prog 17
        prog_type tracing  attach_type fentry
7: raw_tracepoint  prog 21
        tp 'sys_enter'
8: cgroup  prog 25
        cgroup_id 584  attach_type egress
9: cgroup  prog 25
        cgroup_id 599  attach_type egress
10: cgroup  prog 25
        cgroup_id 614  attach_type egress
11: cgroup  prog 25
        cgroup_id 629  attach_type egress

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/20200429001614.1544-9-andriin@fb.com
2020-04-28 17:27:08 -07:00
Andrii Nakryiko 50325b1761 bpftool: Expose attach_type-to-string array to non-cgroup code
Move attach_type_strings into main.h for access in non-cgroup code.
bpf_attach_type is used for non-cgroup attach types quite widely now. So also
complete missing string translations for non-cgroup attach types.

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/20200429001614.1544-8-andriin@fb.com
2020-04-28 17:27:08 -07:00
Martin KaFai Lau 65c9362859 bpftool: Add struct_ops support
This patch adds struct_ops support to the bpftool.

To recap a bit on the recent bpf_struct_ops feature on the kernel side:
It currently supports "struct tcp_congestion_ops" to be implemented
in bpf.  At a high level, bpf_struct_ops is struct_ops map populated
with a number of bpf progs.  bpf_struct_ops currently supports the
"struct tcp_congestion_ops".  However, the bpf_struct_ops design is
generic enough that other kernel struct ops can be supported in
the future.

Although struct_ops is map+progs at a high lever, there are differences
in details.  For example,
1) After registering a struct_ops, the struct_ops is held by the kernel
   subsystem (e.g. tcp-cc).  Thus, there is no need to pin a
   struct_ops map or its progs in order to keep them around.
2) To iterate all struct_ops in a system, it iterates all maps
   in type BPF_MAP_TYPE_STRUCT_OPS.  BPF_MAP_TYPE_STRUCT_OPS is
   the current usual filter.  In the future, it may need to
   filter by other struct_ops specific properties.  e.g. filter by
   tcp_congestion_ops or other kernel subsystem ops in the future.
3) struct_ops requires the running kernel having BTF info.  That allows
   more flexibility in handling other kernel structs.  e.g. it can
   always dump the latest bpf_map_info.
4) Also, "struct_ops" command is not intended to repeat all features
   already provided by "map" or "prog".  For example, if there really
   is a need to pin the struct_ops map, the user can use the "map" cmd
   to do that.

While the first attempt was to reuse parts from map/prog.c,  it ended up
not a lot to share.  The only obvious item is the map_parse_fds() but
that still requires modifications to accommodate struct_ops map specific
filtering (for the immediate and the future needs).  Together with the
earlier mentioned differences, it is better to part away from map/prog.c.

The initial set of subcmds are, register, unregister, show, and dump.

For register, it registers all struct_ops maps that can be found in an
obj file.  Option can be added in the future to specify a particular
struct_ops map.  Also, the common bpf_tcp_cc is stateless (e.g.
bpf_cubic.c and bpf_dctcp.c).  The "reuse map" feature is not
implemented in this patch and it can be considered later also.

For other subcmds, please see the man doc for details.

A sample output of dump:
[root@arch-fb-vm1 bpf]# bpftool struct_ops dump name cubic
[{
        "bpf_map_info": {
            "type": 26,
            "id": 64,
            "key_size": 4,
            "value_size": 256,
            "max_entries": 1,
            "map_flags": 0,
            "name": "cubic",
            "ifindex": 0,
            "btf_vmlinux_value_type_id": 18452,
            "netns_dev": 0,
            "netns_ino": 0,
            "btf_id": 52,
            "btf_key_type_id": 0,
            "btf_value_type_id": 0
        }
    },{
        "bpf_struct_ops_tcp_congestion_ops": {
            "refcnt": {
                "refs": {
                    "counter": 1
                }
            },
            "state": "BPF_STRUCT_OPS_STATE_INUSE",
            "data": {
                "list": {
                    "next": 0,
                    "prev": 0
                },
                "key": 0,
                "flags": 0,
                "init": "void (struct sock *) bictcp_init/prog_id:138",
                "release": "void (struct sock *) 0",
                "ssthresh": "u32 (struct sock *) bictcp_recalc_ssthresh/prog_id:141",
                "cong_avoid": "void (struct sock *, u32, u32) bictcp_cong_avoid/prog_id:140",
                "set_state": "void (struct sock *, u8) bictcp_state/prog_id:142",
                "cwnd_event": "void (struct sock *, enum tcp_ca_event) bictcp_cwnd_event/prog_id:139",
                "in_ack_event": "void (struct sock *, u32) 0",
                "undo_cwnd": "u32 (struct sock *) tcp_reno_undo_cwnd/prog_id:144",
                "pkts_acked": "void (struct sock *, const struct ack_sample *) bictcp_acked/prog_id:143",
                "min_tso_segs": "u32 (struct sock *) 0",
                "sndbuf_expand": "u32 (struct sock *) 0",
                "cong_control": "void (struct sock *, const struct rate_sample *) 0",
                "get_info": "size_t (struct sock *, u32, int *, union tcp_cc_info *) 0",
                "name": "bpf_cubic",
                "owner": 0
            }
        }
    }
]

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200318171656.129650-1-kafai@fb.com
2020-03-20 15:51:35 +01:00
Martin KaFai Lau d5ae04da34 bpftool: Translate prog_id to its bpf prog_name
The kernel struct_ops obj has kernel's func ptrs implemented by bpf_progs.
The bpf prog_id is stored as the value of the func ptr for introspection
purpose.  In the latter patch, a struct_ops dump subcmd will be added
to introspect these func ptrs.  It is desired to print the actual bpf
prog_name instead of only printing the prog_id.

Since struct_ops is the only usecase storing prog_id in the func ptr,
this patch adds a prog_id_as_func_ptr bool (default is false) to
"struct btf_dumper" in order not to mis-interpret the ptr value
for the other existing use-cases.

While printing a func_ptr as a bpf prog_name,
this patch also prefix the bpf prog_name with the ptr's func_proto.
[ Note that it is the ptr's func_proto instead of the bpf prog's
  func_proto ]
It reuses the current btf_dump_func() to obtain the ptr's func_proto
string.

Here is an example from the bpf_cubic.c:
"void (struct sock *, u32, u32) bictcp_cong_avoid/prog_id:140"

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200318171650.129252-1-kafai@fb.com
2020-03-20 15:51:35 +01:00
Quentin Monnet 6ae32b29c0 tools: bpftool: Restore message on failure to guess program type
In commit 4a3d6c6a6e ("libbpf: Reduce log level for custom section
names"), log level for messages for libbpf_attach_type_by_name() and
libbpf_prog_type_by_name() was downgraded from "info" to "debug". The
latter function, in particular, is used by bpftool when attempting to
load programs, and this change caused bpftool to exit with no hint or
error message when it fails to detect the type of the program to load
(unless "-d" option was provided).

To help users understand why bpftool fails to load the program, let's do
a second run of the function with log level in "debug" mode in case of
failure.

Before:

    # bpftool prog load sample_ret0.o /sys/fs/bpf/sample_ret0
    # echo $?
    255

Or really verbose with -d flag:

    # bpftool -d prog load sample_ret0.o /sys/fs/bpf/sample_ret0
    libbpf: loading sample_ret0.o
    libbpf: section(1) .strtab, size 134, link 0, flags 0, type=3
    libbpf: skip section(1) .strtab
    libbpf: section(2) .text, size 16, link 0, flags 6, type=1
    libbpf: found program .text
    libbpf: section(3) .debug_abbrev, size 55, link 0, flags 0, type=1
    libbpf: skip section(3) .debug_abbrev
    libbpf: section(4) .debug_info, size 75, link 0, flags 0, type=1
    libbpf: skip section(4) .debug_info
    libbpf: section(5) .rel.debug_info, size 32, link 14, flags 0, type=9
    libbpf: skip relo .rel.debug_info(5) for section(4)
    libbpf: section(6) .debug_str, size 150, link 0, flags 30, type=1
    libbpf: skip section(6) .debug_str
    libbpf: section(7) .BTF, size 155, link 0, flags 0, type=1
    libbpf: section(8) .BTF.ext, size 80, link 0, flags 0, type=1
    libbpf: section(9) .rel.BTF.ext, size 32, link 14, flags 0, type=9
    libbpf: skip relo .rel.BTF.ext(9) for section(8)
    libbpf: section(10) .debug_frame, size 40, link 0, flags 0, type=1
    libbpf: skip section(10) .debug_frame
    libbpf: section(11) .rel.debug_frame, size 16, link 14, flags 0, type=9
    libbpf: skip relo .rel.debug_frame(11) for section(10)
    libbpf: section(12) .debug_line, size 74, link 0, flags 0, type=1
    libbpf: skip section(12) .debug_line
    libbpf: section(13) .rel.debug_line, size 16, link 14, flags 0, type=9
    libbpf: skip relo .rel.debug_line(13) for section(12)
    libbpf: section(14) .symtab, size 96, link 1, flags 0, type=2
    libbpf: looking for externs among 4 symbols...
    libbpf: collected 0 externs total
    libbpf: failed to guess program type from ELF section '.text'
    libbpf: supported section(type) names are: socket sk_reuseport kprobe/ [...]

After:

    # bpftool prog load sample_ret0.o /sys/fs/bpf/sample_ret0
    libbpf: failed to guess program type from ELF section '.text'
    libbpf: supported section(type) names are: socket sk_reuseport kprobe/ [...]

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200311021205.9755-1-quentin@isovalent.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-03-13 12:49:51 -07:00
Quentin Monnet 75a1e792c3 tools: bpftool: Allow all prog/map handles for pinning objects
Documentation and interactive help for bpftool have always explained
that the regular handles for programs (id|name|tag|pinned) and maps
(id|name|pinned) can be passed to the utility when attempting to pin
objects (bpftool prog pin PROG / bpftool map pin MAP).

THIS IS A LIE!! The tool actually accepts only ids, as the parsing is
done in do_pin_any() in common.c instead of reusing the parsing
functions that have long been generic for program and map handles.

Instead of fixing the doc, fix the code. It is trivial to reuse the
generic parsing, and to simplify do_pin_any() in the process.

Do not accept to pin multiple objects at the same time with
prog_parse_fds() or map_parse_fds() (this would require a more complex
syntax for passing multiple sysfs paths and validating that they
correspond to the number of e.g. programs we find for a given name or
tag).

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200312184608.12050-2-quentin@isovalent.com
2020-03-13 00:24:08 +01:00
Andrey Ignatov 3494bec0f6 bpftool: Support struct_ops, tracing, ext prog types
Add support for prog types that were added to kernel but not present in
bpftool yet: struct_ops, tracing, ext prog types and corresponding
section names.

Before:
  # bpftool p l
  ...
  184: type 26  name test_subprog3  tag dda135a7dc0daf54  gpl
          loaded_at 2020-02-25T13:28:33-0800  uid 0
          xlated 112B  jited 103B  memlock 4096B  map_ids 136
          btf_id 85
  185: type 28  name new_get_skb_len  tag d2de5b87d8e5dc49  gpl
          loaded_at 2020-02-25T13:28:33-0800  uid 0
          xlated 72B  jited 69B  memlock 4096B  map_ids 136
          btf_id 85

After:
  # bpftool p l
  ...
  184: tracing  name test_subprog3  tag dda135a7dc0daf54  gpl
          loaded_at 2020-02-25T13:28:33-0800  uid 0
          xlated 112B  jited 103B  memlock 4096B  map_ids 136
          btf_id 85
  185: ext  name new_get_skb_len  tag d2de5b87d8e5dc49  gpl
          loaded_at 2020-02-25T13:28:33-0800  uid 0
          xlated 72B  jited 69B  memlock 4096B  map_ids 136
          btf_id 85

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20200225223441.689109-1-rdna@fb.com
2020-02-26 16:40:53 +01:00
Andrii Nakryiko 985ead416d bpftool: Add skeleton codegen command
Add `bpftool gen skeleton` command, which takes in compiled BPF .o object file
and dumps a BPF skeleton struct and related code to work with that skeleton.
Skeleton itself is tailored to a specific structure of provided BPF object
file, containing accessors (just plain struct fields) for every map and
program, as well as dedicated space for bpf_links. If BPF program is using
global variables, corresponding structure definitions of compatible memory
layout are emitted as well, making it possible to initialize and subsequently
read/update global variables values using simple and clear C syntax for
accessing fields. This skeleton majorly improves usability of
opening/loading/attaching of BPF object, as well as interacting with it
throughout the lifetime of loaded BPF object.

Generated skeleton struct has the following structure:

struct <object-name> {
	/* used by libbpf's skeleton API */
	struct bpf_object_skeleton *skeleton;
	/* bpf_object for libbpf APIs */
	struct bpf_object *obj;
	struct {
		/* for every defined map in BPF object: */
		struct bpf_map *<map-name>;
	} maps;
	struct {
		/* for every program in BPF object: */
		struct bpf_program *<program-name>;
	} progs;
	struct {
		/* for every program in BPF object: */
		struct bpf_link *<program-name>;
	} links;
	/* for every present global data section: */
	struct <object-name>__<one of bss, data, or rodata> {
		/* memory layout of corresponding data section,
		 * with every defined variable represented as a struct field
		 * with exactly the same type, but without const/volatile
		 * modifiers, e.g.:
		 */
		 int *my_var_1;
		 ...
	} *<one of bss, data, or rodata>;
};

This provides great usability improvements:
- no need to look up maps and programs by name, instead just
  my_obj->maps.my_map or my_obj->progs.my_prog would give necessary
  bpf_map/bpf_program pointers, which user can pass to existing libbpf APIs;
- pre-defined places for bpf_links, which will be automatically populated for
  program types that libbpf knows how to attach automatically (currently
  tracepoints, kprobe/kretprobe, raw tracepoint and tracing programs). On
  tearing down skeleton, all active bpf_links will be destroyed (meaning BPF
  programs will be detached, if they are attached). For cases in which libbpf
  doesn't know how to auto-attach BPF program, user can manually create link
  after loading skeleton and they will be auto-detached on skeleton
  destruction:

	my_obj->links.my_fancy_prog = bpf_program__attach_cgroup_whatever(
		my_obj->progs.my_fancy_prog, <whatever extra param);

- it's extremely easy and convenient to work with global data from userspace
  now. Both for read-only and read/write variables, it's possible to
  pre-initialize them before skeleton is loaded:

	skel = my_obj__open(raw_embed_data);
	my_obj->rodata->my_var = 123;
	my_obj__load(skel); /* 123 will be initialization value for my_var */

  After load, if kernel supports mmap() for BPF arrays, user can still read
  (and write for .bss and .data) variables values, but at that point it will
  be directly mmap()-ed to BPF array, backing global variables. This allows to
  seamlessly exchange data with BPF side. From userspace program's POV, all
  the pointers and memory contents stay the same, but mapped kernel memory
  changes to point to created map.
  If kernel doesn't yet support mmap() for BPF arrays, it's still possible to
  use those data section structs to pre-initialize .bss, .data, and .rodata,
  but after load their pointers will be reset to NULL, allowing user code to
  gracefully handle this condition, if necessary.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191214014341.3442258-14-andriin@fb.com
2019-12-15 15:58:05 -08:00
Paul Chaignon 99f9863a0c bpftool: Match maps by name
This patch implements lookup by name for maps and changes the behavior of
lookups by tag to be consistent with prog subcommands.  Similarly to
program subcommands, the show and dump commands will return all maps with
the given name (or tag), whereas other commands will error out if several
maps have the same name (resp. tag).

When a map has BTF info, it is dumped in JSON with available BTF info.
This patch requires that all matched maps have BTF info before switching
the output format to JSON.

Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/8de1c9f273860b3ea1680502928f4da2336b853e.1576263640.git.paul.chaignon@gmail.com
2019-12-15 09:03:18 -08:00
Paul Chaignon a7d22ca2a4 bpftool: Match programs by name
When working with frequently modified BPF programs, both the ID and the
tag may change.  bpftool currently doesn't provide a "stable" way to match
such programs.

This patch implements lookup by name for programs.  The show and dump
commands will return all programs with the given name, whereas other
commands will error out if several programs have the same name.

Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Link: https://lore.kernel.org/bpf/b5fc1a5dcfaeb5f16fc80295cdaa606dd2d91534.1576263640.git.paul.chaignon@gmail.com
2019-12-15 09:03:18 -08:00
Andrii Nakryiko 32e3e58e4c bpftool: Fix bpftool build by switching to bpf_object__open_file()
As part of libbpf in 5e61f27070 ("libbpf: stop enforcing kern_version,
populate it for users") non-LIBBPF_API __bpf_object__open_xattr() API
was removed from libbpf.h header. This broke bpftool, which relied on
that function. This patch fixes the build by switching to newly added
bpf_object__open_file() which provides the same capabilities, but is
official and future-proof API.

v1->v2:
- fix prog_type shadowing (Stanislav).

Fixes: 5e61f27070 ("libbpf: stop enforcing kern_version, populate it for users")
Reported-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Stanislav Fomichev <sdf@google.com>
Link: https://lore.kernel.org/bpf/20191007225604.2006146-1-andriin@fb.com
2019-10-07 18:44:28 -07:00
Quentin Monnet 8918dc42dc tools: bpftool: move "__printf()" attributes to header file
Some functions in bpftool have a "__printf()" format attributes to tell
the compiler they should expect printf()-like arguments. But because
these attributes are not used for the function prototypes in the header
files, the compiler does not run the checks everywhere the functions are
used, and some mistakes on format string and corresponding arguments
slipped in over time.

Let's move the __printf() attributes to the correct places.

Note: We add guards around the definition of GCC_VERSION in
tools/include/linux/compiler-gcc.h to prevent a conflict in jit_disasm.c
on GCC_VERSION from headers pulled via libbfd.

Fixes: c101189bc9 ("tools: bpftool: fix -Wmissing declaration warnings")
Reported-by: Jakub Kicinski <jakub.kicinski@netronome.com>
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
Daniel T. Lee 216b65fb70 tools: bpftool: add raw_tracepoint_writable prog type to header
From commit 9df1c28bb7 ("bpf: add writable context for raw tracepoints"),
a new type of BPF_PROG, RAW_TRACEPOINT_WRITABLE has been added.

Since this BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE is not listed at
bpftool's header, it causes a segfault when executing 'bpftool feature'.

This commit adds BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE entry to
prog_type_name enum, and will eventually fixes the segfault issue.

Fixes: 9df1c28bb7 ("bpf: add writable context for raw tracepoints")
Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-07-12 15:14:42 +02:00