Commit Graph

619 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 35725a8afd bpftool: Use sysfs vmlinux when dumping BTF by ID
Bugzilla: https://bugzilla.redhat.com/2120968

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

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

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:04 +02:00
Yauheni Kaliuta 503bec2387 bpf, x86: Generate trampolines from bpf_tramp_links
Bugzilla: https://bugzilla.redhat.com/2120968
Conflicts: already applied
  1d5f82d9dd47 ("bpf, x86: fix freeing of not-finalized bpf_prog_pack")

commit f7e0beaf39d3868dc700d4954b26cf8443c5d423
Author: Kui-Feng Lee <kuifeng@fb.com>
Date:   Tue May 10 13:59:19 2022 -0700

    bpf, x86: Generate trampolines from bpf_tramp_links

    Replace struct bpf_tramp_progs with struct bpf_tramp_links to collect
    struct bpf_tramp_link(s) for a trampoline.  struct bpf_tramp_link
    extends bpf_link to act as a linked list node.

    arch_prepare_bpf_trampoline() accepts a struct bpf_tramp_links to
    collects all bpf_tramp_link(s) that a trampoline should call.

    Change BPF trampoline and bpf_struct_ops to pass bpf_tramp_links
    instead of bpf_tramp_progs.

    Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220510205923.3206889-2-kuifeng@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:03 +02:00
Yauheni Kaliuta 5da73b1bf6 bpftool: bpf_link_get_from_fd support for LSM programs in lskel
Bugzilla: https://bugzilla.redhat.com/2120968

commit bd2331b3757f5b2ab4aafc591b55fa2a592abf7c
Author: KP Singh <kpsingh@kernel.org>
Date:   Mon May 9 21:49:05 2022 +0000

    bpftool: bpf_link_get_from_fd support for LSM programs in lskel
    
    bpf_link_get_from_fd currently returns a NULL fd for LSM programs.
    LSM programs are similar to tracing programs and can also use
    skel_raw_tracepoint_open.
    
    Signed-off-by: KP Singh <kpsingh@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220509214905.3754984-1-kpsingh@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:02 +02:00
Yauheni Kaliuta 27b6a5eee8 bpftool: Declare generator name
Bugzilla: https://bugzilla.redhat.com/2120968

commit 56c3e749d08a041454f5d75273c24d16240f26dc
Author: Jason Wang <jasowang@redhat.com>
Date:   Mon May 9 17:02:47 2022 +0800

    bpftool: Declare generator name
    
    Most code generators declare its name so did this for bfptool.
    
    Signed-off-by: Jason Wang <jasowang@redhat.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220509090247.5457-1-jasowang@redhat.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:02 +02:00
Yauheni Kaliuta 44073c4cdc bpftool: Output message if no helpers found in feature probing
Bugzilla: https://bugzilla.redhat.com/2120968

commit b06a92a18d4651c983c60d83935a76b2d47d85e0
Author: Milan Landaverde <milan@mdaverde.com>
Date:   Wed May 4 12:13:32 2022 -0400

    bpftool: Output message if no helpers found in feature probing
    
    Currently in libbpf, we have hardcoded program types that are not
    supported for helper function probing (e.g. tracing, ext, lsm).
    Due to this (and other legitimate failures), bpftool feature probe returns
    empty for those program type helper functions.
    
    Instead of implying to the user that there are no helper functions
    available for a program type, we output a message to the user explaining
    that helper function probing failed for that program type.
    
    Signed-off-by: Milan Landaverde <milan@mdaverde.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220504161356.3497972-3-milan@mdaverde.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:02 +02:00
Yauheni Kaliuta a9c5ca82fc bpftool: Adjust for error codes from libbpf probes
Bugzilla: https://bugzilla.redhat.com/2120968

commit 6d9f63b9df5ed7ffe10ac70025f408d28b3dd260
Author: Milan Landaverde <milan@mdaverde.com>
Date:   Wed May 4 12:13:31 2022 -0400

    bpftool: Adjust for error codes from libbpf probes
    
    Originally [1], libbpf's (now deprecated) probe functions returned a bool
    to acknowledge support but the new APIs return an int with a possible
    negative error code to reflect probe failure. This change decides for
    bpftool to declare maps and helpers are not available on probe failures.
    
    [1]: https://lore.kernel.org/bpf/20220202225916.3313522-3-andrii@kernel.org/
    
    Signed-off-by: Milan Landaverde <milan@mdaverde.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220504161356.3497972-2-milan@mdaverde.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:02 +02:00
Yauheni Kaliuta 6264b07b5b bpftool, musl compat: Replace sys/fcntl.h by fcntl.h
Bugzilla: https://bugzilla.redhat.com/2120968

commit 246bdfa52f33ecfa52546ed67287de4eab165b2e
Author: Dominique Martinet <asmadeus@codewreck.org>
Date:   Sun Apr 24 14:10:22 2022 +0900

    bpftool, musl compat: Replace sys/fcntl.h by fcntl.h
    
    musl does not like including sys/fcntl.h directly:
    
        [...]
        1 | #warning redirecting incorrect #include <sys/fcntl.h> to <fcntl.h>
        [...]
    
    Signed-off-by: Dominique Martinet <asmadeus@codewreck.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220424051022.2619648-5-asmadeus@codewreck.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:11 +02:00
Yauheni Kaliuta 99e08b3b64 bpftool, musl compat: Replace nftw with FTW_ACTIONRETVAL
Bugzilla: https://bugzilla.redhat.com/2120968

commit 93bc2e9e943d20a51473a49009db3243de6e098d
Author: Dominique Martinet <asmadeus@codewreck.org>
Date:   Sun Apr 24 14:10:21 2022 +0900

    bpftool, musl compat: Replace nftw with FTW_ACTIONRETVAL
    
    musl nftw implementation does not support FTW_ACTIONRETVAL. There have been
    multiple attempts at pushing the feature in musl upstream, but it has been
    refused or ignored all the times:
    
      https://www.openwall.com/lists/musl/2021/03/26/1
      https://www.openwall.com/lists/musl/2022/01/22/1
    
    In this case we only care about /proc/<pid>/fd/<fd>, so it's not too difficult
    to reimplement directly instead, and the new implementation makes 'bpftool perf'
    slightly faster because it doesn't needlessly stat/readdir unneeded directories
    (54ms -> 13ms on my machine).
    
    Signed-off-by: Dominique Martinet <asmadeus@codewreck.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220424051022.2619648-4-asmadeus@codewreck.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:11 +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 a8d34c4dc0 bpftool: Handle libbpf_probe_prog_type errors
Bugzilla: https://bugzilla.redhat.com/2120968

commit 7b53eaa656c34d5b521e245cbfc3aee2d7b89eac
Author: Milan Landaverde <milan@mdaverde.com>
Date:   Thu Mar 31 11:45:55 2022 -0400

    bpftool: Handle libbpf_probe_prog_type errors
    
    Previously [1], we were using bpf_probe_prog_type which returned a
    bool, but the new libbpf_probe_bpf_prog_type can return a negative
    error code on failure. This change decides for bpftool to declare
    a program type is not available on probe failure.
    
    [1] https://lore.kernel.org/bpf/20220202225916.3313522-3-andrii@kernel.org/
    
    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-4-milan@mdaverde.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:56 +02:00
Yauheni Kaliuta 952abb92ab bpftool: Add missing link types
Bugzilla: https://bugzilla.redhat.com/2120968

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

    bpftool: Add missing link types
    
    Will display the link type names in bpftool link show output
    
    Signed-off-by: Milan Landaverde <milan@mdaverde.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220331154555.422506-3-milan@mdaverde.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:56 +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
Frantisek Hrbata 1269719102 Merge: BPF and XDP rebase to v5.18
Merge conflicts:
-----------------
arch/x86/net/bpf_jit_comp.c
        - bpf_arch_text_poke()
          HEAD(!1464) contains b73b002f7f ("x86/ibt,bpf: Add ENDBR instructions to prologue and trampoline")
          Resolved in favour of !1464, but keep the return statement from !1477

MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/1477

Bugzilla: https://bugzilla.redhat.com/2120966

Rebase BPF and XDP to the upstream kernel version 5.18

Patch applied, then reverted:
```
544356 selftests/bpf: switch to new libbpf XDP APIs
0bfb95 selftests, bpf: Do not yet switch to new libbpf XDP APIs
```
Taken in the perf rebase:
```
23fcfc perf: use generic bpf_program__set_type() to set BPF prog type
```
Unsuported arches:
```
5c1011 libbpf: Fix riscv register names
cf0b5b libbpf: Fix accessing syscall arguments on riscv
```
Depends on changes of other subsystems:
```
7fc8c3 s390/bpf: encode register within extable entry
aebfd1 x86/ibt,ftrace: Search for __fentry__ location
589127 x86/ibt,bpf: Add ENDBR instructions to prologue and trampoline
```
Broken selftest:
```
edae34 selftests net: add UDP GRO fraglist + bpf self-tests
cf6783 selftests net: fix bpf build error
7b92aa selftests net: fix kselftest net fatal error
```
Out of scope:
```
baebdf net: dev: Makes sure netif_rx() can be invoked in any context.
5c8166 kbuild: replace $(if A,A,B) with $(or A,B)
1a97ce perf maps: Use a pointer for kmaps
967747 uaccess: remove CONFIG_SET_FS
42b01a s390: always use the packed stack layout
bf0882 flow_dissector: Add support for HSR
d09a30 s390/extable: move EX_TABLE define to asm-extable.h
3d6671 s390/extable: convert to relative table with data
4efd41 s390: raise minimum supported machine generation to z10
f65e58 flow_dissector: Add support for HSRv0
1a6d7a netdevsim: Introduce support for L3 offload xstats
9b1894 selftests: netdevsim: hw_stats_l3: Add a new test
84005b perf ftrace latency: Add -n/--use-nsec option
36c4a7 kasan, arm64: don't tag executable vmalloc allocations
8df013 docs: netdev: move the netdev-FAQ to the process pages
4d4d00 perf tools: Update copy of libbpf's hashmap.c
0df6ad perf evlist: Rename cpus to user_requested_cpus
1b8089 flow_dissector: fix false-positive __read_overflow2_field() warning
0ae065 perf build: Fix check for btf__load_from_kernel_by_id() in libbpf
8994e9 perf test bpf: Skip test if clang is not present
735346 perf build: Fix btf__load_from_kernel_by_id() feature check
f037ac s390/stack: merge empty stack frame slots
335220 docs: netdev: update maintainer-netdev.rst reference
a0b098 s390/nospec: remove unneeded header includes
34513a netdevsim: Fix hwstats debugfs file permissions
```

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>

Approved-by: John W. Linville <linville@redhat.com>
Approved-by: Wander Lairson Costa <wander@redhat.com>
Approved-by: Torez Smith <torez@redhat.com>
Approved-by: Jan Stancek <jstancek@redhat.com>
Approved-by: Prarit Bhargava <prarit@redhat.com>
Approved-by: Felix Maurer <fmaurer@redhat.com>
Approved-by: Viktor Malik <vmalik@redhat.com>

Signed-off-by: Frantisek Hrbata <fhrbata@redhat.com>
2022-11-21 05:30:47 -05:00
Joe Lawrence 403009a4d8 kbuild: replace $(if A,A,B) with $(or A,B)
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2121207
Conflicts:
	tools/counter/Makefile
	tools/tracing/rtla/Makefile
	- These do not exist in rhel9

commit 5c8166419acf468b5bc3e48f928a040485d3e0c2
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Fri Feb 11 14:14:11 2022 +0900

    kbuild: replace $(if A,A,B) with $(or A,B)

    $(or ...) is available since GNU Make 3.81, and useful to shorten the
    code in some places.

    Covert as follows:

      $(if A,A,B)  -->  $(or A,B)

    This patch also converts:

      $(if A, A, B) --> $(or A, B)

    Strictly speaking, the latter is not an equivalent conversion because
    GNU Make keeps spaces after commas; if A is not empty, $(if A, A, B)
    expands to " A", while $(or A, B) expands to "A".

    Anyway, preceding spaces are not significant in the code hunks I touched.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
2022-10-27 14:27:59 -04:00
Jerome Marchand f0b1684768 bpftool: Explicit errno handling in skeletons
Bugzilla: https://bugzilla.redhat.com/2120966

commit 522574fd7864e091d473765102e866414979b2ab
Author: Delyan Kratunov <delyank@fb.com>
Date:   Mon Mar 21 23:29:18 2022 +0000

    bpftool: Explicit errno handling in skeletons

    Andrii noticed that since f97b8b9bd630 ("bpftool: Fix a bug in subskeleton
    code generation") the subskeleton code allows bpf_object__destroy_subskeleton
    to overwrite the errno that subskeleton__open would return with. While this
    is not currently an issue, let's make it future-proof.

    This patch explicitly tracks err in subskeleton__open and skeleton__create
    (i.e. calloc failure is explicitly ENOMEM) and ensures that errno is -err on
    the error return path. The skeleton code had to be changed since maps and
    progs codegen is shared with subskeletons.

    Fixes: f97b8b9bd630 ("bpftool: Fix a bug in subskeleton code generation")
    Signed-off-by: Delyan Kratunov <delyank@fb.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/3b6bfbb770c79ae64d8de26c1c1bd9d53a4b85f8.camel@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:08 +02:00
Jerome Marchand 9a68327061 bpftool: Fix generated code in codegen_asserts
Bugzilla: https://bugzilla.redhat.com/2120966

commit ef8a257b4e499a979364b1f9caf25a325f6ee8b8
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Mon Mar 28 10:37:03 2022 +0200

    bpftool: Fix generated code in codegen_asserts

    Arnaldo reported perf compilation fail with:

      $ make -k BUILD_BPF_SKEL=1 CORESIGHT=1 PYTHON=python3
      ...
      In file included from util/bpf_counter.c:28:
      /tmp/build/perf//util/bpf_skel/bperf_leader.skel.h: In function ‘bperf_leader_bpf__assert’:
      /tmp/build/perf//util/bpf_skel/bperf_leader.skel.h:351:51: error: unused parameter ‘s’ [-Werror=unused-parameter]
        351 | bperf_leader_bpf__assert(struct bperf_leader_bpf *s)
            |                          ~~~~~~~~~~~~~~~~~~~~~~~~~^
      cc1: all warnings being treated as errors

    If there's nothing to generate in the new assert function,
    we will get unused 's' warn/error, adding 'unused' attribute to it.

    Fixes: 08d4dba6ae77 ("bpftool: Bpf skeletons assert type sizes")
    Reported-by: Arnaldo Carvalho de Melo <acme@redhat.com>
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
    Link: https://lore.kernel.org/bpf/20220328083703.2880079-1-jolsa@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:08 +02:00
Jerome Marchand 061f0bf0d4 bpf/bpftool: Add unprivileged_bpf_disabled check against value of 2
Bugzilla: https://bugzilla.redhat.com/2120966

commit 8c1b21198551d795a44f08ad185f716732b47bbe
Author: Milan Landaverde <milan@mdaverde.com>
Date:   Tue Mar 22 10:49:45 2022 -0400

    bpf/bpftool: Add unprivileged_bpf_disabled check against value of 2

    In [1], we added a kconfig knob that can set
    /proc/sys/kernel/unprivileged_bpf_disabled to 2

    We now check against this value in bpftool feature probe

    [1] https://lore.kernel.org/bpf/74ec548079189e4e4dffaeb42b8987bb3c852eee.1620765074.git.daniel@iogearbox.net

    Signed-off-by: Milan Landaverde <milan@mdaverde.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: KP Singh <kpsingh@kernel.org>
    Link: https://lore.kernel.org/bpf/20220322145012.1315376-1-milan@mdaverde.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:07 +02:00
Jerome Marchand 261df64038 bpftool: Fix a bug in subskeleton code generation
Bugzilla: https://bugzilla.redhat.com/2120966

commit f97b8b9bd630fb76c0e9e11cbf390e3d64a144d7
Author: Yonghong Song <yhs@fb.com>
Date:   Sat Mar 19 20:20:09 2022 -0700

    bpftool: Fix a bug in subskeleton code generation

    Compiled with clang by adding LLVM=1 both kernel and selftests/bpf
    build, I hit the following compilation error:

    In file included from /.../tools/testing/selftests/bpf/prog_tests/subskeleton.c:6:
      ./test_subskeleton_lib.subskel.h:168:6: error: variable 'err' is used uninitialized whenever
          'if' condition is true [-Werror,-Wsometimes-uninitialized]
              if (!s->progs)
                  ^~~~~~~~~
      ./test_subskeleton_lib.subskel.h:181:11: note: uninitialized use occurs here
              errno = -err;
                       ^~~
      ./test_subskeleton_lib.subskel.h:168:2: note: remove the 'if' if its condition is always false
              if (!s->progs)
              ^~~~~~~~~~~~~~

    The compilation error is triggered by the following code
            ...
            int err;

            obj = (struct test_subskeleton_lib *)calloc(1, sizeof(*obj));
            if (!obj) {
                    errno = ENOMEM;
                    goto err;
            }
            ...

      err:
            test_subskeleton_lib__destroy(obj);
            errno = -err;
            ...
    in test_subskeleton_lib__open(). The 'err' is not initialized, yet it
    is used in 'errno = -err' later.

    The fix is to remove 'errno = -err' since errno has been set properly
    in all incoming branches.

    Fixes: 00389c58ffe9 ("bpftool: Add support for subskeletons")
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220320032009.3106133-1-yhs@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:07 +02:00
Jerome Marchand 7fa35c6c9d bpftool: Add BPF_TRACE_KPROBE_MULTI to attach type names table
Bugzilla: https://bugzilla.redhat.com/2120966

commit 08063b4bc1581bbdcae99da4a54f546a50045fc0
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Mar 18 08:01:06 2022 -0700

    bpftool: Add BPF_TRACE_KPROBE_MULTI to attach type names table

    BPF_TRACE_KPROBE_MULTI is a new attach type name, add it to bpftool's
    table. This fixes a currently failing CI bpftool check.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20220318150106.2933343-1-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:05 +02:00
Jerome Marchand 6bb9c2053e bpftool: Add support for subskeletons
Bugzilla: https://bugzilla.redhat.com/2120966

commit 00389c58ffe993782a8ba4bb5a34a102b1f6fe24
Author: Delyan Kratunov <delyank@fb.com>
Date:   Wed Mar 16 23:37:28 2022 +0000

    bpftool: Add support for subskeletons

    Subskeletons are headers which require an already loaded program to
    operate.

    For example, when a BPF library is linked into a larger BPF object file,
    the library userspace needs a way to access its own global variables
    without requiring knowledge about the larger program at build time.

    As a result, subskeletons require a loaded bpf_object to open().
    Further, they find their own symbols in the larger program by
    walking BTF type data at run time.

    At this time, programs, maps, and globals are supported through
    non-owning pointers.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:05 +02:00
Jerome Marchand 53db8d0115 bpftool: man: Add missing top level docs
Bugzilla: https://bugzilla.redhat.com/2120966

commit 6585abea98ae5f750358a6427f2ddf7715393f69
Author: Daniel Xu <dxu@dxuuu.xyz>
Date:   Sun Mar 13 16:19:46 2022 -0700

    bpftool: man: Add missing top level docs

    The top-level (bpftool.8) man page was missing docs for a few
    subcommands and their respective sub-sub-commands.

    This commit brings the top level man page up to date. Note that I've
    kept the ordering of the subcommands the same as in `bpftool help`.

    Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/3049ef5dc509c0d1832f0a8b2dba2ccaad0af688.1647213551.git.dxu@dxuuu.xyz

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:03 +02: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 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 d3fdb0c0d4 bpftool: Restore support for BPF offload-enabled feature probing
Bugzilla: https://bugzilla.redhat.com/2120966

commit f655c088e74f4681134f650b5743174586a61016
Author: Niklas Söderlund <niklas.soderlund@corigine.com>
Date:   Thu Mar 10 13:18:46 2022 +0100

    bpftool: Restore support for BPF offload-enabled feature probing

    Commit 1a56c18e6c2e4e74 ("bpftool: Stop supporting BPF offload-enabled
    feature probing") removed the support to probe for BPF offload features.
    This is still something that is useful for NFP NIC that can support
    offloading of BPF programs.

    The reason for the dropped support was that libbpf starting with v1.0
    would drop support for passing the ifindex to the BPF prog/map/helper
    feature probing APIs. In order to keep this useful feature for NFP
    restore the functionality by moving it directly into bpftool.

    The code restored is a simplified version of the code that existed in
    libbpf which supposed passing the ifindex. The simplification is that it
    only targets the cases where ifindex is given and call into libbpf for
    the cases where it's not.

    Before restoring support for probing offload features:

      # bpftool feature probe dev ens4np0
      Scanning system call availability...
      bpf() syscall is available

      Scanning eBPF program types...

      Scanning eBPF map types...

      Scanning eBPF helper functions...
      eBPF helpers supported for program type sched_cls:
      eBPF helpers supported for program type xdp:

      Scanning miscellaneous eBPF features...
      Large program size limit is NOT available
      Bounded loop support is NOT available
      ISA extension v2 is NOT available
      ISA extension v3 is NOT available

    With support for probing offload features restored:

      # bpftool feature probe dev ens4np0
      Scanning system call availability...
      bpf() syscall is available

      Scanning eBPF program types...
      eBPF program_type sched_cls is available
      eBPF program_type xdp is available

      Scanning eBPF map types...
      eBPF map_type hash is available
      eBPF map_type array is available

      Scanning eBPF helper functions...
      eBPF helpers supported for program type sched_cls:
      	- bpf_map_lookup_elem
      	- bpf_get_prandom_u32
      	- bpf_perf_event_output
      eBPF helpers supported for program type xdp:
      	- bpf_map_lookup_elem
      	- bpf_get_prandom_u32
      	- bpf_perf_event_output
      	- bpf_xdp_adjust_head
      	- bpf_xdp_adjust_tail

      Scanning miscellaneous eBPF features...
      Large program size limit is NOT available
      Bounded loop support is NOT available
      ISA extension v2 is NOT available
      ISA extension v3 is NOT available

    Signed-off-by: Niklas Söderlund <niklas.soderlund@corigine.com>
    Signed-off-by: Simon Horman <simon.horman@corigine.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220310121846.921256-1-niklas.soderlund@corigine.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:59 +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 ea7905cea9 bpftool: Remove redundant slashes
Bugzilla: https://bugzilla.redhat.com/2120966

commit c62dd8a58d19fa35b60c84ab2435ac3ad0d3777e
Author: Yuntao Wang <ytcoode@gmail.com>
Date:   Sun Feb 27 00:38:15 2022 +0800

    bpftool: Remove redundant slashes

    Because the OUTPUT variable ends with a slash but CURDIR doesn't, to keep
    the _OUTPUT value consistent, we add a trailing slash to CURDIR when
    defining _OUTPUT variable.

    Since the _OUTPUT variable holds a value ending with a trailing slash,
    there is no need to add another one when defining BOOTSTRAP_OUTPUT and
    LIBBPF_OUTPUT variables. Likewise, when defining LIBBPF_INCLUDE and
    LIBBPF_BOOTSTRAP_INCLUDE, we shouldn't add an extra slash either for the
    same reason.

    When building libbpf, the value of the DESTDIR argument should also not
    end with a trailing slash.

    Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220226163815.520133-1-ytcoode@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:52 +02:00
Jerome Marchand 1ed8e8f551 bpftool: Bpf skeletons assert type sizes
Bugzilla: https://bugzilla.redhat.com/2120966

commit 08d4dba6ae77aaec0e0c79dcfcb0613cb7426b2c
Author: Delyan Kratunov <delyank@fb.com>
Date:   Wed Feb 23 22:01:58 2022 +0000

    bpftool: Bpf skeletons assert type sizes

    When emitting type declarations in skeletons, bpftool will now also emit
    static assertions on the size of the data/bss/rodata/etc fields. This
    ensures that in situations where userspace and kernel types have the same
    name but differ in size we do not silently produce incorrect results but
    instead break the build.

    This was reported in [1] and as expected the repro in [2] fails to build
    on the new size assert after this change.

      [1]: Closes: https://github.com/libbpf/libbpf/issues/433
      [2]: https://github.com/fuweid/iovisor-bcc-pr-3777

    Signed-off-by: Delyan Kratunov <delyank@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Tested-by: Hengqi Chen <hengqi.chen@gmail.com>
    Acked-by: Hengqi Chen <hengqi.chen@gmail.com>
    Link: https://lore.kernel.org/bpf/f562455d7b3cf338e59a7976f4690ec5a0057f7f.camel@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:51 +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 90f7bd03c2 bpftool: Fix C++ additions to skeleton
Bugzilla: https://bugzilla.redhat.com/2120966

commit 9b6eb0478dfad3b0e7af6c73523d96826210f4fe
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Feb 16 15:35:40 2022 -0800

    bpftool: Fix C++ additions to skeleton

    Mark C++-specific T::open() and other methods as static inline to avoid
    symbol redefinition when multiple files use the same skeleton header in
    an application.

    Fixes: bb8ffe61ea45 ("bpftool: Add C++-specific open/load/etc skeleton wrappers")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220216233540.216642-1-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:50 +02:00
Jerome Marchand e0e69fa653 bpftool: Gen min_core_btf explanation and examples
Bugzilla: https://bugzilla.redhat.com/2120966

commit 1d1ffbf7f0b261fd5dcac2cd40a92b9394df08f6
Author: Rafael David Tinoco <rafaeldtinoco@gmail.com>
Date:   Tue Feb 15 17:58:55 2022 -0500

    bpftool: Gen min_core_btf explanation and examples

    Add "min_core_btf" feature explanation and one example of how to use it
    to bpftool-gen man page.

    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Rafael David Tinoco <rafael.tinoco@aquasec.com>
    Signed-off-by: Lorenzo Fontana <lorenzo.fontana@elastic.co>
    Signed-off-by: Leonardo Di Donato <leonardo.didonato@elastic.co>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220215225856.671072-7-mauricio@kinvolk.io

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:50 +02:00
Jerome Marchand 2a397ff302 bpftool: Implement btfgen_get_btf()
Bugzilla: https://bugzilla.redhat.com/2120966

commit dc695516b6f5cb322b95de7ef3a6ec1db707ff8b
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Tue Feb 15 17:58:54 2022 -0500

    bpftool: Implement btfgen_get_btf()

    The last part of the BTFGen algorithm is to create a new BTF object with
    all the types that were recorded in the previous steps.

    This function performs two different steps:
    1. Add the types to the new BTF object by using btf__add_type(). Some
    special logic around struct and unions is implemented to only add the
    members that are really used in the field-based relocations. The type
    ID on the new and old BTF objects is stored on a map.
    2. Fix all the type IDs on the new BTF object by using the IDs saved in
    the previous step.

    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Rafael David Tinoco <rafael.tinoco@aquasec.com>
    Signed-off-by: Lorenzo Fontana <lorenzo.fontana@elastic.co>
    Signed-off-by: Leonardo Di Donato <leonardo.didonato@elastic.co>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220215225856.671072-6-mauricio@kinvolk.io

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:50 +02:00
Jerome Marchand 06c6de356e bpftool: Implement "gen min_core_btf" logic
Bugzilla: https://bugzilla.redhat.com/2120966

commit a9caaba399f9cff34c6bffa1b1fd673d3d6043a0
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Tue Feb 15 17:58:53 2022 -0500

    bpftool: Implement "gen min_core_btf" logic

    This commit implements the logic for the gen min_core_btf command.
    Specifically, it implements the following functions:

    - minimize_btf(): receives the path of a source and destination BTF
    files and a list of BPF objects. This function records the relocations
    for all objects and then generates the BTF file by calling
    btfgen_get_btf() (implemented in the following commit).

    - btfgen_record_obj(): loads the BTF and BTF.ext sections of the BPF
    objects and loops through all CO-RE relocations. It uses
    bpf_core_calc_relo_insn() from libbpf and passes the target spec to
    btfgen_record_reloc(), that calls one of the following functions
    depending on the relocation kind.

    - btfgen_record_field_relo(): uses the target specification to mark all
    the types that are involved in a field-based CO-RE relocation. In this
    case types resolved and marked recursively using btfgen_mark_type().
    Only the struct and union members (and their types) involved in the
    relocation are marked to optimize the size of the generated BTF file.

    - btfgen_record_type_relo(): marks the types involved in a type-based
    CO-RE relocation. In this case no members for the struct and union types
    are marked as libbpf doesn't use them while performing this kind of
    relocation. Pointed types are marked as they are used by libbpf in this
    case.

    - btfgen_record_enumval_relo(): marks the whole enum type for enum-based
    relocations.

    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Rafael David Tinoco <rafael.tinoco@aquasec.com>
    Signed-off-by: Lorenzo Fontana <lorenzo.fontana@elastic.co>
    Signed-off-by: Leonardo Di Donato <leonardo.didonato@elastic.co>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220215225856.671072-5-mauricio@kinvolk.io

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:50 +02:00
Jerome Marchand 25b6d15483 bpftool: Add gen min_core_btf command
Bugzilla: https://bugzilla.redhat.com/2120966

commit 0a9f4a20c6153d187c8ee58133357ac671372f5f
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Tue Feb 15 17:58:52 2022 -0500

    bpftool: Add gen min_core_btf command

    This command is implemented under the "gen" command in bpftool and the
    syntax is the following:

    $ bpftool gen min_core_btf INPUT OUTPUT OBJECT [OBJECT...]

    INPUT is the file that contains all the BTF types for a kernel and
    OUTPUT is the path of the minimize BTF file that will be created with
    only the types needed by the objects.

    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Rafael David Tinoco <rafael.tinoco@aquasec.com>
    Signed-off-by: Lorenzo Fontana <lorenzo.fontana@elastic.co>
    Signed-off-by: Leonardo Di Donato <leonardo.didonato@elastic.co>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220215225856.671072-4-mauricio@kinvolk.io

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:50 +02:00
Jerome Marchand 85d86088c5 bpftool: Add C++-specific open/load/etc skeleton wrappers
Bugzilla: https://bugzilla.redhat.com/2120966

commit bb8ffe61ea454a565e4fb1f450ef71237c9f032c
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Feb 11 21:57:32 2022 -0800

    bpftool: Add C++-specific open/load/etc skeleton wrappers

    Add C++-specific static methods for code-generated BPF skeleton for each
    skeleton operation: open, open_opts, open_and_load, load, attach,
    detach, destroy, and elf_bytes. This is to facilitate easier C++
    templating on top of pure C BPF skeleton.

    In C, open/load/destroy/etc "methods" are of the form
    <skeleton_name>__<method>() to avoid name collision with similar
    "methods" of other skeletons withint the same application. This works
    well, but is very inconvenient for C++ applications that would like to
    write generic (templated) wrappers around BPF skeleton to fit in with
    C++ code base and take advantage of destructors and other convenient C++
    constructs.

    This patch makes it easier to build such generic templated wrappers by
    additionally defining C++ static methods for skeleton's struct with
    fixed names. This allows to refer to, say, open method as `T::open()`
    instead of having to somehow generate `T__open()` function call.

    Next patch adds an example template to test_cpp selftest to demonstrate
    how it's possible to have all the operations wrapped in a generic
    Skeleton<my_skeleton> type without explicitly passing function references.

    An example of generated declaration section without %1$s placeholders:

      #ifdef __cplusplus
          static struct test_attach_probe *open(const struct bpf_object_open_opts *opts = nullptr);
          static struct test_attach_probe *open_and_load();
          static int load(struct test_attach_probe *skel);
          static int attach(struct test_attach_probe *skel);
          static void detach(struct test_attach_probe *skel);
          static void destroy(struct test_attach_probe *skel);
          static const void *elf_bytes(size_t *sz);
      #endif /* __cplusplus */

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:49 +02:00
Jerome Marchand 507b7df24f bpftool: Update versioning scheme, align on libbpf's version number
Bugzilla: https://bugzilla.redhat.com/2120966

commit 9910a74d6ebf6e35d7adfae665022674fb90ea78
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Thu Feb 10 10:42:37 2022 +0000

    bpftool: Update versioning scheme, align on libbpf's version number

    Since the notion of versions was introduced for bpftool, it has been
    following the version number of the kernel (using the version number
    corresponding to the tree in which bpftool's sources are located). The
    rationale was that bpftool's features are loosely tied to BPF features
    in the kernel, and that we could defer versioning to the kernel
    repository itself.

    But this versioning scheme is confusing today, because a bpftool binary
    should be able to work with both older and newer kernels, even if some
    of its recent features won't be available on older systems. Furthermore,
    if bpftool is ported to other systems in the future, keeping a
    Linux-based version number is not a good option.

    Looking at other options, we could either have a totally independent
    scheme for bpftool, or we could align it on libbpf's version number
    (with an offset on the major version number, to avoid going backwards).
    The latter comes with a few drawbacks:

    - We may want bpftool releases in-between two libbpf versions. We can
      always append pre-release numbers to distinguish versions, although
      those won't look as "official" as something with a proper release
      number. But at the same time, having bpftool with version numbers that
      look "official" hasn't really been an issue so far.

    - If no new feature lands in bpftool for some time, we may move from
      e.g. 6.7.0 to 6.8.0 when libbpf levels up and have two different
      versions which are in fact the same.

    - Following libbpf's versioning scheme sounds better than kernel's, but
      ultimately it doesn't make too much sense either, because even though
      bpftool uses the lib a lot, its behaviour is not that much conditioned
      by the internal evolution of the library (or by new APIs that it may
      not use).

    Having an independent versioning scheme solves the above, but at the
    cost of heavier maintenance. Developers will likely forget to increase
    the numbers when adding features or bug fixes, and we would take the
    risk of having to send occasional "catch-up" patches just to update the
    version number.

    Based on these considerations, this patch aligns bpftool's version
    number on libbpf's. This is not a perfect solution, but 1) it's
    certainly an improvement over the current scheme, 2) the issues raised
    above are all minor at the moment, and 3) we can still move to an
    independent scheme in the future if we realise we need it.

    Given that libbpf is currently at version 0.7.0, and bpftool, before
    this patch, was at 5.16, we use an offset of 6 for the major version,
    bumping bpftool to 6.7.0. Libbpf does not export its patch number;
    leave bpftool's patch number at 0 for now.

    It remains possible to manually override the version number by setting
    BPFTOOL_VERSION when calling make.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:49 +02:00
Jerome Marchand c84f094d9e bpftool: Add libbpf's version number to "bpftool version" output
Bugzilla: https://bugzilla.redhat.com/2120966

commit 61fce9693f0311949c59cc62a8fdee6e36243553
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Thu Feb 10 10:42:36 2022 +0000

    bpftool: Add libbpf's version number to "bpftool version" output

    To help users check what version of libbpf is being used with bpftool,
    print the number along with bpftool's own version number.

    Output:

        $ ./bpftool version
        ./bpftool v5.16.0
        using libbpf v0.7
        features: libbfd, libbpf_strict, skeletons

        $ ./bpftool version --json --pretty
        {
            "version": "5.16.0",
            "libbpf_version": "0.7",
            "features": {
                "libbfd": true,
                "libbpf_strict": true,
                "skeletons": true
            }
        }

    Note that libbpf does not expose its patch number.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:49 +02:00
Jerome Marchand 354e190758 bpftool: Generalize light skeleton generation.
Bugzilla: https://bugzilla.redhat.com/2120966

commit 28d743f671272d7a5f676669c84438b0f9600936
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Wed Feb 9 15:19:59 2022 -0800

    bpftool: Generalize light skeleton generation.

    Generealize light skeleton by hiding mmap details in skel_internal.h
    In this form generated lskel.h is usable both by user space and by the kernel.

    Note that previously #include <bpf/bpf.h> was in *.lskel.h file.
    To avoid #ifdef-s in a generated lskel.h the include of bpf.h is moved
    to skel_internal.h, but skel_internal.h is also used by gen_loader.c
    which is part of libbpf. Therefore skel_internal.h does #include "bpf.h"
    in case of user space, so gen_loader.c and lskel.h have necessary definitions.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Yonghong Song <yhs@fb.com>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220209232001.27490-4-alexei.starovoitov@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:49 +02:00
Jerome Marchand 42c0d665b3 bpftool: Fix strict mode calculation
Bugzilla: https://bugzilla.redhat.com/2120966

commit da7af0aa20f8ad89f09d50d262e44cd5eafab816
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Mon Feb 7 09:50:51 2022 -0500

    bpftool: Fix strict mode calculation

    "(__LIBBPF_STRICT_LAST - 1) & ~LIBBPF_STRICT_MAP_DEFINITIONS" is wrong
    as it is equal to 0 (LIBBPF_STRICT_NONE). Let's use
    "LIBBPF_STRICT_ALL & ~LIBBPF_STRICT_MAP_DEFINITIONS" now that the
    previous commit makes it possible in libbpf.

    Fixes: 93b8952d223a ("libbpf: deprecate legacy BPF map definitions")
    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220207145052.124421-3-mauricio@kinvolk.io

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:47 +02:00
Jerome Marchand b3f9d95bd8 bpftool: Fix uninit variable compilation warning
Bugzilla: https://bugzilla.redhat.com/2120966

commit a9a8ac592e47ff35363308ad4c66740724132aa3
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Feb 2 14:59:13 2022 -0800

    bpftool: Fix uninit variable compilation warning

    Newer GCC complains about capturing the address of unitialized variable.
    While there is nothing wrong with the code (the variable is filled out
    by the kernel), initialize the variable anyway to make compiler happy.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220202225916.3313522-4-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:46 +02:00
Jerome Marchand 858b02e61f bpftool: Stop supporting BPF offload-enabled feature probing
Bugzilla: https://bugzilla.redhat.com/2120966

commit 1a56c18e6c2e4e7482ac9e1a4ffe1770841dd6bd
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Feb 2 14:59:12 2022 -0800

    bpftool: Stop supporting BPF offload-enabled feature probing

    libbpf 1.0 is not going to support passing ifindex to BPF
    prog/map/helper feature probing APIs. Remove the support for BPF offload
    feature probing.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220202225916.3313522-3-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:46 +02:00
Jerome Marchand 691f6ba70b libbpf: Open code raw_tp_open and link_create commands.
Bugzilla: https://bugzilla.redhat.com/2120966

commit c69f94a33d12a9c49f1800c54838ee19447ac176
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Mon Jan 31 14:05:24 2022 -0800

    libbpf: Open code raw_tp_open and link_create commands.

    Open code raw_tracepoint_open and link_create used by light skeleton
    to be able to avoid full libbpf eventually.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Link: https://lore.kernel.org/bpf/20220131220528.98088-4-alexei.starovoitov@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:45 +02:00
Jerome Marchand b1efe0942b libbpf: Add support for bpf iter in light skeleton.
Bugzilla: https://bugzilla.redhat.com/2120966

commit 42d1d53fedc9980e8fed98a5a03762cba7d2e9f6
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Mon Jan 31 14:05:22 2022 -0800

    libbpf: Add support for bpf iter in light skeleton.

    bpf iterator programs should use bpf_link_create to attach instead of
    bpf_raw_tracepoint_open like other tracing programs.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Link: https://lore.kernel.org/bpf/20220131220528.98088-2-alexei.starovoitov@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:45 +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 a7e37db5c4 bpftool: use new API for attaching XDP program
Bugzilla: https://bugzilla.redhat.com/2120966

commit c86575eccab24d583db0169ded342eb215b781c9
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Jan 19 22:14:20 2022 -0800

    bpftool: use new API for attaching XDP program

    Switch to new bpf_xdp_attach() API to avoid deprecation warnings.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:41 +02:00
Jerome Marchand f55ca6e379 libbpf: deprecate legacy BPF map definitions
Bugzilla: https://bugzilla.redhat.com/2120966

commit 93b8952d223af03c51fba0c6258173d2ffbd2cb7
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Jan 19 22:05:28 2022 -0800

    libbpf: deprecate legacy BPF map definitions

    Enact deprecation of legacy BPF map definition in SEC("maps") ([0]). For
    the definitions themselves introduce LIBBPF_STRICT_MAP_DEFINITIONS flag
    for libbpf strict mode. If it is set, error out on any struct
    bpf_map_def-based map definition. If not set, libbpf will print out
    a warning for each legacy BPF map to raise awareness that it goes away.

    For any use of BPF_ANNOTATE_KV_PAIR() macro providing a legacy way to
    associate BTF key/value type information with legacy BPF map definition,
    warn through libbpf's pr_warn() error message (but don't fail BPF object
    open).

    BPF-side struct bpf_map_def is marked as deprecated. User-space struct
    bpf_map_def has to be used internally in libbpf, so it is left
    untouched. It should be enough for bpf_map__def() to be marked
    deprecated to raise awareness that it goes away.

    bpftool is an interesting case that utilizes libbpf to open BPF ELF
    object to generate skeleton. As such, even though bpftool itself uses
    full on strict libbpf mode (LIBBPF_STRICT_ALL), it has to relax it a bit
    for BPF map definition handling to minimize unnecessary disruptions. So
    opt-out of LIBBPF_STRICT_MAP_DEFINITIONS for bpftool. User's code that
    will later use generated skeleton will make its own decision whether to
    enforce LIBBPF_STRICT_MAP_DEFINITIONS or not.

    There are few tests in selftests/bpf that are consciously using legacy
    BPF map definitions to test libbpf functionality. For those, temporary
    opt out of LIBBPF_STRICT_MAP_DEFINITIONS mode for the duration of those
    tests.

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

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:41 +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
Jerome Marchand 3b2c47f45c bpftool: Stop using bpf_map__def() API
Bugzilla: https://bugzilla.redhat.com/2120966

commit 3c28919f0652a1952333b88e1af5ce408fafe238
Author: Christy Lee <christylee@fb.com>
Date:   Fri Jan 7 16:42:15 2022 -0800

    bpftool: Stop using bpf_map__def() API

    libbpf bpf_map__def() API is being deprecated, replace bpftool's
    usage with the appropriate getters and setters

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:39 +02:00
Jerome Marchand e8bc956f61 bpftool: Only set obj->skeleton on complete success
Bugzilla: https://bugzilla.redhat.com/2120966

commit 0991f6a38f576aa9a5e34713e23c998a3310d4d0
Author: Wei Fu <fuweid89@gmail.com>
Date:   Sat Jan 8 16:40:08 2022 +0800

    bpftool: Only set obj->skeleton on complete success

    After `bpftool gen skeleton`, the ${bpf_app}.skel.h will provide that
    ${bpf_app_name}__open helper to load bpf. If there is some error
    like ENOMEM, the ${bpf_app_name}__open will rollback(free) the allocated
    object, including `bpf_object_skeleton`.

    Since the ${bpf_app_name}__create_skeleton set the obj->skeleton first
    and not rollback it when error, it will cause double-free in
    ${bpf_app_name}__destory at ${bpf_app_name}__open. Therefore, we should
    set the obj->skeleton before return 0;

    Fixes: 5dc7a8b211 ("bpftool, selftests/bpf: Embed object file inside skeleton")
    Signed-off-by: Wei Fu <fuweid89@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220108084008.1053111-1-fuweid89@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:39 +02:00
Artem Savkov 35436adcb8 bpftool: Fix pretty print dump for maps without BTF loaded
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit f76d8507d23834f7e56b0fe95c82605e7d7e0efe
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Feb 16 10:21:02 2022 +0100

    bpftool: Fix pretty print dump for maps without BTF loaded

    The commit e5043894b21f ("bpftool: Use libbpf_get_error() to check error")
    fails to dump map without BTF loaded in pretty mode (-p option).

    Fixing this by making sure get_map_kv_btf won't fail in case there's
    no BTF available for the map.

    Fixes: e5043894b21f ("bpftool: Use libbpf_get_error() to check error")
    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220216092102.125448-1-jolsa@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:56 +02:00