Commit Graph

145 Commits

Author SHA1 Message Date
Yauheni Kaliuta 002bc665cb Revert "bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK"
Bugzilla: https://bugzilla.redhat.com/2120968

commit 6b4384ff108874cf336fe2fb1633313c2c7620bf
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Fri Jun 10 12:26:47 2022 +0100

    Revert "bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK"
    
    This reverts commit a777e18f1bcd32528ff5dfd10a6629b655b05eb8.
    
    In commit a777e18f1bcd ("bpftool: Use libbpf 1.0 API mode instead of
    RLIMIT_MEMLOCK"), we removed the rlimit bump in bpftool, because the
    kernel has switched to memcg-based memory accounting. Thanks to the
    LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK, we attempted to keep compatibility
    with other systems and ask libbpf to raise the limit for us if
    necessary.
    
    How do we know if memcg-based accounting is supported? There is a probe
    in libbpf to check this. But this probe currently relies on the
    availability of a given BPF helper, bpf_ktime_get_coarse_ns(), which
    landed in the same kernel version as the memory accounting change. This
    works in the generic case, but it may fail, for example, if the helper
    function has been backported to an older kernel. This has been observed
    for Google Cloud's Container-Optimized OS (COS), where the helper is
    available but rlimit is still in use. The probe succeeds, the rlimit is
    not raised, and probing features with bpftool, for example, fails.
    
    A patch was submitted [0] to update this probe in libbpf, based on what
    the cilium/ebpf Go library does [1]. It would lower the soft rlimit to
    0, attempt to load a BPF object, and reset the rlimit. But it may induce
    some hard-to-debug flakiness if another process starts, or the current
    application is killed, while the rlimit is reduced, and the approach was
    discarded.
    
    As a workaround to ensure that the rlimit bump does not depend on the
    availability of a given helper, we restore the unconditional rlimit bump
    in bpftool for now.
    
      [0] https://lore.kernel.org/bpf/20220609143614.97837-1-quentin@isovalent.com/
      [1] https://github.com/cilium/ebpf/blob/v0.9.0/rlimit/rlimit.go#L39
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Cc: Yafang Shao <laoar.shao@gmail.com>
    Cc: Stanislav Fomichev <sdf@google.com>
    Link: https://lore.kernel.org/bpf/20220610112648.29695-2-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:07 +02:00
Yauheni Kaliuta 24ef9e29c2 bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK
Bugzilla: https://bugzilla.redhat.com/2120968

commit a777e18f1bcd32528ff5dfd10a6629b655b05eb8
Author: Yafang Shao <laoar.shao@gmail.com>
Date:   Sat Apr 9 12:59:57 2022 +0000

    bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK
    
    We have switched to memcg-based memory accouting and thus the rlimit is
    not needed any more. LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK was introduced in
    libbpf for backward compatibility, so we can use it instead now.
    
    libbpf_set_strict_mode always return 0, so we don't need to check whether
    the return value is 0 or not.
    
    Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220409125958.92629-4-laoar.shao@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:58 +02:00
Yauheni Kaliuta d39f2cbbf4 bpftool: Add syscall prog type
Bugzilla: https://bugzilla.redhat.com/2120968

commit 380341637ebb41f56031a0fd0779e85413a1da59
Author: Milan Landaverde <milan@mdaverde.com>
Date:   Thu Mar 31 11:45:53 2022 -0400

    bpftool: Add syscall prog type
    
    In addition to displaying the program type in bpftool prog show
    this enables us to be able to query bpf_prog_type_syscall
    availability through feature probe as well as see
    which helpers are available in those programs (such as
    bpf_sys_bpf and bpf_sys_close)
    
    Signed-off-by: Milan Landaverde <milan@mdaverde.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220331154555.422506-2-milan@mdaverde.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:56 +02:00
Jerome Marchand 6555d9a912 bpftool: Ensure bytes_memlock json output is correct
Bugzilla: https://bugzilla.redhat.com/2120966

commit 357b3cc3c0467b2f7cd6c4a87f7a18bfd779ce5b
Author: Chris J Arges <carges@cloudflare.com>
Date:   Wed Mar 9 15:41:58 2022 -0600

    bpftool: Ensure bytes_memlock json output is correct

    If a BPF map is created over 2^32 the memlock value as displayed in JSON
    format will be incorrect. Use atoll instead of atoi so that the correct
    number is displayed.

      ```
      $ bpftool map create /sys/fs/bpf/test_bpfmap type hash key 4 \
        value 1024 entries 4194304 name test_bpfmap
      $ bpftool map list
      1: hash  name test_bpfmap  flags 0x0
              key 4B  value 1024B  max_entries 4194304  memlock 4328521728B
      $ sudo bpftool map list -j | jq .[].bytes_memlock
      33554432
      ```

    Signed-off-by: Chris J Arges <carges@cloudflare.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/b6601087-0b11-33cc-904a-1133d1500a10@cloudflare.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:02 +02:00
Jerome Marchand 7f2f400116 bpftool: Migrate from bpf_prog_test_run_xattr
Bugzilla: https://bugzilla.redhat.com/2120966

commit 9cce53138dd9ee8bd7354196297df24130f3529d
Author: Delyan Kratunov <delyank@fb.com>
Date:   Wed Feb 2 15:54:22 2022 -0800

    bpftool: Migrate from bpf_prog_test_run_xattr

    bpf_prog_test_run is being deprecated in favor of the OPTS-based
    bpf_prog_test_run_opts.

    Signed-off-by: Delyan Kratunov <delyank@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220202235423.1097270-4-delyank@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:55 +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 0b09f05340 bpftool: use preferred setters/getters instead of deprecated ones
Bugzilla: https://bugzilla.redhat.com/2120966

commit 39748db1d6bc12b9f749a0aebe7ec71b00bd60eb
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Jan 24 11:42:51 2022 -0800

    bpftool: use preferred setters/getters instead of deprecated ones

    Use bpf_program__type() instead of discouraged bpf_program__get_type().
    Also switch to bpf_map__set_max_entries() instead of bpf_map__resize().

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20220124194254.2051434-5-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:43 +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
Artem Savkov bba63c6329 libbpf 1.0: Deprecate bpf_map__is_offload_neutral()
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit 9855c131b9c8b0327ff5182f88bb1991f212415b
Author: Christy Lee <christylee@fb.com>
Date:   Tue Jan 4 16:06:01 2022 -0800

    libbpf 1.0: Deprecate bpf_map__is_offload_neutral()

    Deprecate bpf_map__is_offload_neutral(). It’s most probably broken
    already. PERF_EVENT_ARRAY isn’t the only map that’s not suitable
    for hardware offloading. Applications can directly check map type
    instead.

      [0] Closes: https://github.com/libbpf/libbpf/issues/306

    Signed-off-by: Christy Lee <christylee@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220105000601.2090044-1-christylee@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:53 +02:00
Artem Savkov b51d8e947e bpftool: Switch bpf_object__load_xattr() to bpf_object__load()
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit b59e4ce8bcaab6445f4a0d37a96ca8953caaf5cf
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Dec 9 11:38:40 2021 -0800

    bpftool: Switch bpf_object__load_xattr() to bpf_object__load()

    Switch all the uses of to-be-deprecated bpf_object__load_xattr() into
    a simple bpf_object__load() calls with optional log_level passed through
    open_opts.kernel_log_level, if -d option is specified.

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

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:46 +02:00
Artem Savkov af1bb73160 bpftool: Add debug mode for gen_loader.
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit 942df4dc5ea159100466f198d8687a49c2359ca3
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Sat Dec 4 11:46:23 2021 -0800

    bpftool: Add debug mode for gen_loader.

    Make -d flag functional for gen_loader style program loading.

    For example:
    $ bpftool prog load -L -d test_d_path.o
    ... // will print:
    libbpf: loading ./test_d_path.o
    libbpf: elf: section(3) fentry/security_inode_getattr, size 280, link 0, flags 6, type=1
    ...
    libbpf: prog 'prog_close': found data map 0 (test_d_p.bss, sec 7, off 0) for insn 30
    libbpf: gen: load_btf: size 5376
    libbpf: gen: map_create: test_d_p.bss idx 0 type 2 value_type_id 118
    libbpf: map 'test_d_p.bss': created successfully, fd=0
    libbpf: gen: map_update_elem: idx 0
    libbpf: sec 'fentry/filp_close': found 1 CO-RE relocations
    libbpf: record_relo_core: prog 1 insn[15] struct file 0:1 final insn_idx 15
    libbpf: gen: prog_load: type 26 insns_cnt 35 progi_idx 0
    libbpf: gen: find_attach_tgt security_inode_getattr 12
    libbpf: gen: prog_load: type 26 insns_cnt 37 progi_idx 1
    libbpf: gen: find_attach_tgt filp_close 12
    libbpf: gen: finish 0
    ... // at this point libbpf finished generating loader program
       0: (bf) r6 = r1
       1: (bf) r1 = r10
       2: (07) r1 += -136
       3: (b7) r2 = 136
       4: (b7) r3 = 0
       5: (85) call bpf_probe_read_kernel#113
       6: (05) goto pc+104
    ... // this is the assembly dump of the loader program
     390: (63) *(u32 *)(r6 +44) = r0
     391: (18) r1 = map[idx:0]+5584
     393: (61) r0 = *(u32 *)(r1 +0)
     394: (63) *(u32 *)(r6 +24) = r0
     395: (b7) r0 = 0
     396: (95) exit
    err 0  // the loader program was loaded and executed successfully
    (null)
    func#0 @0
    ...  // CO-RE in the kernel logs:
    CO-RE relocating STRUCT file: found target candidate [500]
    prog '': relo #0: kind <byte_off> (0), spec is [8] STRUCT file.f_path (0:1 @ offset 16)
    prog '': relo #0: matching candidate #0 [500] STRUCT file.f_path (0:1 @ offset 16)
    prog '': relo #0: patched insn #15 (ALU/ALU64) imm 16 -> 16
    vmlinux_cand_cache:[11]file(500),
    module_cand_cache:
    ... // verifier logs when it was checking test_d_path.o program:
    R1 type=ctx expected=fp
    0: R1=ctx(id=0,off=0,imm=0) R10=fp0
    ; int BPF_PROG(prog_close, struct file *file, void *id)
    0: (79) r6 = *(u64 *)(r1 +0)
    func 'filp_close' arg0 has btf_id 500 type STRUCT 'file'
    1: R1=ctx(id=0,off=0,imm=0) R6_w=ptr_file(id=0,off=0,imm=0) R10=fp0
    ; pid_t pid = bpf_get_current_pid_tgid() >> 32;
    1: (85) call bpf_get_current_pid_tgid#14

    ... // if there are multiple programs being loaded by the loader program
    ... // only the last program in the elf file will be printed, since
    ... // the same verifier log_buf is used for all PROG_LOAD commands.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211204194623.27779-1-alexei.starovoitov@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:45 +02:00
Artem Savkov 6c5210646e bpftool: Fix memory leak in prog_dump()
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit ebbd7f64a3fbe9e0f235e39fc244ee9735e2a52a
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Wed Nov 10 11:46:27 2021 +0000

    bpftool: Fix memory leak in prog_dump()

    Following the extraction of prog_dump() from do_dump(), the struct btf
    allocated in prog_dump() is no longer freed on error; the struct
    bpf_prog_linfo is not freed at all. Make sure we release them before
    exiting the function.

    Fixes: ec2025095c ("bpftool: Match several programs with same tag")
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211110114632.24537-2-quentin@isovalent.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:37 +02:00
Artem Savkov e3c0ae0ea8 bpftool: Use bpf_obj_get_info_by_fd directly
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit c59765cfd193382b00454b1a4424cb78d4c065e2
Author: Dave Marchevsky <davemarchevsky@fb.com>
Date:   Mon Nov 1 15:43:55 2021 -0700

    bpftool: Use bpf_obj_get_info_by_fd directly

    To prepare for impending deprecation of libbpf's
    bpf_program__get_prog_info_linear, migrate uses of this function to use
    bpf_obj_get_info_by_fd.

    Since the profile_target_name and dump_prog_id_as_func_ptr helpers were
    only looking at the first func_info, avoid grabbing the rest to save a
    malloc. For do_dump, add a more full-featured helper, but avoid
    free/realloc of buffer when possible for multi-prog dumps.

    Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20211101224357.2651181-3-davemarchevsky@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:32 +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 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 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
Yauheni Kaliuta 13ef58e1c8 bpftool: Avoid leaking the JSON writer prepared for program metadata
Bugzilla: http://bugzilla.redhat.com/2069045

commit e89ef634f81c9d90e1824ab183721f3b361472e6
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Fri Oct 22 10:47:43 2021 +0100

    bpftool: Avoid leaking the JSON writer prepared for program metadata
    
    Bpftool creates a new JSON object for writing program metadata in plain
    text mode, regardless of metadata being present or not. Then this writer
    is freed if any metadata has been found and printed, but it leaks
    otherwise. We cannot destroy the object unconditionally, because the
    destructor prints an undesirable line break. Instead, make sure the
    writer is created only after we have found program metadata to print.
    
    Found with valgrind.
    
    Fixes: aff52e685e ("bpftool: Support dumping metadata")
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211022094743.11052-1-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:45 +03:00
Yauheni Kaliuta f1e442bf06 bpftool: Remove unused includes to <bpf/bpf_gen_internal.h>
Bugzilla: http://bugzilla.redhat.com/2069045

commit c66a248f1950d41502fb67624147281d9de0e868
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Thu Oct 7 20:44:28 2021 +0100

    bpftool: Remove unused includes to <bpf/bpf_gen_internal.h>
    
    It seems that the header file was never necessary to compile bpftool,
    and it is not part of the headers exported from libbpf. Let's remove the
    includes from prog.c and gen.c.
    
    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/20211007194438.34443-3-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:40 +03:00
Yauheni Kaliuta aa580f2f50 selftests/bpf: Switch to new bpf_object__next_{map,program} APIs
Bugzilla: http://bugzilla.redhat.com/2069045

commit 6f2b219b62a4376ca2da15c503de79d0650c8155
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Mon Oct 4 00:58:44 2021 +0800

    selftests/bpf: Switch to new bpf_object__next_{map,program} APIs
    
    Replace deprecated bpf_{map,program}__next APIs with newly added
    bpf_object__next_{map,program} APIs, so that no compilation warnings
    emit.
    
    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20211003165844.4054931-3-hengqi.chen@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:39 +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 cf0368f888 tools: bpftool: Complete and synchronise attach or map types
Bugzilla: http://bugzilla.redhat.com/2041365

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

    tools: bpftool: Complete and synchronise attach or map types

    Update bpftool's list of attach type names to tell it about the latest
    attach types, or the "ringbuf" map. Also update the documentation, help
    messages, and bash completion when relevant.

    These missing items were reported by the newly added Python script used
    to help maintain consistency in bpftool.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:37 +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
Jerome Marchand 8dd396adf7 tools: Free BTF objects at various locations
Bugzilla: http://bugzilla.redhat.com/2041365

commit 369e955b3d1c12f6ec2e51a95911bb80ada55d79
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Thu Jul 29 17:20:24 2021 +0100

    tools: Free BTF objects at various locations

    Make sure to call btf__free() (and not simply free(), which does not
    free all pointers stored in the struct) on pointers to struct btf
    objects retrieved at various locations.

    These were found while updating the calls to btf__get_from_id().

    Fixes: 999d82cbc0 ("tools/bpf: enhance test_btf file testing to test func info")
    Fixes: 254471e57a ("tools/bpf: bpftool: add support for func types")
    Fixes: 7b612e291a ("perf tools: Synthesize PERF_RECORD_* for loaded BPF programs")
    Fixes: d56354dc49 ("perf tools: Save bpf_prog_info and BTF of new BPF programs")
    Fixes: 47c09d6a9f ("bpftool: Introduce "prog profile" command")
    Fixes: fa853c4b83 ("perf stat: Enable counting events for BPF programs")
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210729162028.29512-5-quentin@isovalent.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:36 +02:00
Jakub Kicinski 5ada57a9a6 Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
cdc-wdm: s/kill_urbs/poison_urbs/ to fix build

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2021-05-27 09:55:10 -07:00
Liu Jian a8deba8547 bpftool: Add sock_release help info for cgroup attach/prog load command
The help information was not added at the time when the function got added.
Fix this and add the missing information to its cli, documentation and bash
completion.

Fixes: db94cc0b48 ("bpftool: Add support for BPF_CGROUP_INET_SOCK_RELEASE")
Signed-off-by: Liu Jian <liujian56@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20210525014139.323859-1-liujian56@huawei.com
2021-05-25 16:18:32 +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
Cong Wang a7ba4558e6 sock_map: Introduce BPF_SK_SKB_VERDICT
Reusing BPF_SK_SKB_STREAM_VERDICT is possible but its name is
confusing and more importantly we still want to distinguish them
from user-space. So we can just reuse the stream verdict code but
introduce a new type of eBPF program, skb_verdict. Users are not
allowed to attach stream_verdict and skb_verdict programs to the
same map.

Signed-off-by: Cong Wang <cong.wang@bytedance.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20210331023237.41094-10-xiyou.wangcong@gmail.com
2021-04-01 10:56:14 -07:00
Alexei Starovoitov 9ed9e9ba23 bpf: Count the number of times recursion was prevented
Add per-program counter for number of times recursion prevention mechanism
was triggered and expose it via show_fdinfo and bpf_prog_info.
Teach bpftool to print it.

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/20210210033634.62081-7-alexei.starovoitov@gmail.com
2021-02-11 16:19:20 +01:00
Yonghong Song 450d060e8f bpftool: Add {i,d}tlb_misses support for bpftool profile
Commit 47c09d6a9f67("bpftool: Introduce "prog profile" command")
introduced "bpftool prog profile" command which can be used
to profile bpf program with metrics like # of instructions,

This patch added support for itlb_misses and dtlb_misses.
During an internal bpf program performance evaluation,
I found these two metrics are also very useful. The following
is an example output:

 $ bpftool prog profile id 324 duration 3 cycles itlb_misses

           1885029 run_cnt
        5134686073 cycles
            306893 itlb_misses

 $ bpftool prog profile id 324 duration 3 cycles dtlb_misses

           1827382 run_cnt
        4943593648 cycles
           5975636 dtlb_misses

 $ bpftool prog profile id 324 duration 3 cycles llc_misses

           1836527 run_cnt
        5019612972 cycles
           4161041 llc_misses

From the above, we can see quite some dtlb misses, 3 dtlb misses
perf prog run. This might be something worth further investigation.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20201119073039.4060095-1-yhs@fb.com
2020-11-20 15:50:38 +01:00
Lorenz Bauer f9b7ff0d7f tools/bpftool: Fix attaching flow dissector
My earlier patch to reject non-zero arguments to flow dissector attach
broke attaching via bpftool. Instead of 0 it uses -1 for target_fd.
Fix this by passing a zero argument when attaching the flow dissector.

Fixes: 1b514239e8 ("bpf: flow_dissector: Check value of unused flags to BPF_PROG_ATTACH")
Reported-by: Jiri Benc <jbenc@redhat.com>
Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20201105115230.296657-1-lmb@cloudflare.com
2020-11-05 18:20:41 -08:00
YiFei Zhu aff52e685e bpftool: Support dumping metadata
Dump metadata in the 'bpftool prog' list if it's present.
For some formatting some BTF code is put directly in the
metadata dumping. Sanity checks on the map and the kind of the btf_type
to make sure we are actually dumping what we are expecting.

A helper jsonw_reset is added to json writer so we can reuse the same
json writer without having extraneous commas.

Sample output:

  $ bpftool prog
  6: cgroup_skb  name prog  tag bcf7977d3b93787c  gpl
  [...]
  	btf_id 4
  	metadata:
  		a = "foo"
  		b = 1

  $ bpftool prog --json --pretty
  [{
          "id": 6,
  [...]
          "btf_id": 4,
          "metadata": {
              "a": "foo",
              "b": 1
          }
      }
  ]

Signed-off-by: YiFei Zhu <zhuyifei@google.com>
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Cc: YiFei Zhu <zhuyifei1999@gmail.com>
Link: https://lore.kernel.org/bpf/20200915234543.3220146-5-sdf@google.com
2020-09-15 18:28:27 -07:00
Andrii Nakryiko fd17e272be tools/bpftool: Replace bpf_program__title() with bpf_program__section_name()
bpf_program__title() is deprecated, switch to bpf_program__section_name() and
avoid compilation warnings.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200903203542.15944-9-andriin@fb.com
2020-09-03 17:14:40 -07: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
Quentin Monnet 9a97c9d2af tools, bpftool: Add LSM type to array of prog names
Assign "lsm" as a printed name for BPF_PROG_TYPE_LSM in bpftool, so that
it can use it when listing programs loaded on the system or when probing
features.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200724090618.16378-3-quentin@isovalent.com
2020-07-27 22:40:54 +02:00
Jakub Sitnicki 93a3545d81 tools/bpftool: Add name mappings for SK_LOOKUP prog and attach type
Make bpftool show human-friendly identifiers for newly introduced program
and attach type, BPF_PROG_TYPE_SK_LOOKUP and BPF_SK_LOOKUP, respectively.

Also, add the new prog type bash-completion, man page and help message.

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200717103536.397595-14-jakub@cloudflare.com
2020-07-17 20:18:17 -07: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 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
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
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
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
Song Liu 14e5728ff8 bpftool: Only build bpftool-prog-profile if supported by clang
bpftool-prog-profile requires clang to generate BTF for global variables.
When compared with older clang, skip this command. This is achieved by
adding a new feature, clang-bpf-global-var, to tools/build/feature.

Signed-off-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200312182332.3953408-2-songliubraving@fb.com
2020-03-13 00:08:33 +01:00
Song Liu 47c09d6a9f bpftool: Introduce "prog profile" command
With fentry/fexit programs, it is possible to profile BPF program with
hardware counters. Introduce bpftool "prog profile", which measures key
metrics of a BPF program.

bpftool prog profile command creates per-cpu perf events. Then it attaches
fentry/fexit programs to the target BPF program. The fentry program saves
perf event value to a map. The fexit program reads the perf event again,
and calculates the difference, which is the instructions/cycles used by
the target program.

Example input and output:

  ./bpftool prog profile id 337 duration 3 cycles instructions llc_misses

        4228 run_cnt
     3403698 cycles                                              (84.08%)
     3525294 instructions   #  1.04 insn per cycle               (84.05%)
          13 llc_misses     #  3.69 LLC misses per million isns  (83.50%)

This command measures cycles and instructions for BPF program with id
337 for 3 seconds. The program has triggered 4228 times. The rest of the
output is similar to perf-stat. In this example, the counters were only
counting ~84% of the time because of time multiplexing of perf counters.

Note that, this approach measures cycles and instructions in very small
increments. So the fentry/fexit programs introduce noticeable errors to
the measurement results.

The fentry/fexit programs are generated with BPF skeletons. Therefore, we
build bpftool twice. The first time _bpftool is built without skeletons.
Then, _bpftool is used to generate the skeletons. The second time, bpftool
is built with skeletons.

Signed-off-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200309173218.2739965-2-songliubraving@fb.com
2020-03-10 00:04:07 +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
Toke Høiland-Jørgensen d95f1e8b46 bpftool: Don't crash on missing xlated program instructions
Turns out the xlated program instructions can also be missing if
kptr_restrict sysctl is set. This means that the previous fix to check the
jited_prog_insns pointer was insufficient; add another check of the
xlated_prog_insns pointer as well.

Fixes: 5b79bcdf03 ("bpftool: Don't crash on missing jited insns or ksyms")
Fixes: cae73f2339 ("bpftool: use bpf_program__get_prog_info_linear() in prog.c:do_dump()")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200206102906.112551-1-toke@redhat.com
2020-02-07 22:29:45 +01:00