Commit Graph

68 Commits

Author SHA1 Message Date
Viktor Malik 21bd1c32be bpftool: Un-const bpf_func_info to fix it for llvm 17 and newer
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit f4aba3471cfb9ccf69b476463f19b4c50fef6b14
Author: Ivan Babrou <ivan@cloudflare.com>
Date:   Mon May 20 15:51:49 2024 -0700

    bpftool: Un-const bpf_func_info to fix it for llvm 17 and newer
    
    LLVM 17 started treating const structs as constants:
    
    * https://github.com/llvm/llvm-project/commit/0b2d5b967d98
    
    Combined with pointer laundering via ptr_to_u64, which takes a const ptr,
    but in reality treats the underlying memory as mutable, this makes clang
    always pass zero to btf__type_by_id, which breaks full name resolution.
    
    Disassembly before (LLVM 16) and after (LLVM 17):
    
        -    8b 75 cc                 mov    -0x34(%rbp),%esi
        -    e8 47 8d 02 00           call   3f5b0 <btf__type_by_id>
        +    31 f6                    xor    %esi,%esi
        +    e8 a9 8c 02 00           call   3f510 <btf__type_by_id>
    
    It's a bigger project to fix this properly (and a question whether LLVM
    itself should detect this), but for right now let's just fix bpftool.
    
    For more information, see this thread in bpf mailing list:
    
    * https://lore.kernel.org/bpf/CABWYdi0ymezpYsQsPv7qzpx2fWuTkoD1-wG1eT-9x-TSREFrQg@mail.gmail.com/T/
    
    Fixes: b662000aff84 ("bpftool: Adding support for BTF program names")
    Signed-off-by: Ivan Babrou <ivan@cloudflare.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Nick Desaulniers <ndesaulniers@google.com>
    Acked-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/bpf/20240520225149.5517-1-ivan@cloudflare.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 08:57:39 +01:00
Viktor Malik a411de1c99
bpftool: Mount bpffs on provided dir instead of parent dir
JIRA: https://issues.redhat.com/browse/RHEL-30773

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

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

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

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

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

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-10-12 11:41:06 +02:00
Artem Savkov c0e51e119e bpftool: Register struct_ops with a link.
Bugzilla: https://bugzilla.redhat.com/2221599

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

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

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:32 +02:00
Viktor Malik d152843a35 bpftool: Use bpf_{btf,link,map,prog}_get_info_by_fd()
Bugzilla: https://bugzilla.redhat.com/2178930

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

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

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:45:39 +02:00
Jerome Marchand 3e21d7e8f3 bpftool: Fix memory leak in do_build_table_cb
Bugzilla: https://bugzilla.redhat.com/2177177

commit fa55ef14ef4fe06198c0ce811b603aec24134bc2
Author: Miaoqian Lin <linmq006@gmail.com>
Date:   Tue Dec 6 11:19:06 2022 +0400

    bpftool: Fix memory leak in do_build_table_cb
    
    strdup() allocates memory for path. We need to release the memory in the
    following error path. Add free() to avoid memory leak.
    
    Fixes: 8f184732b60b ("bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects")
    Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20221206071906.806384-1-linmq006@gmail.com

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:02 +02:00
Jerome Marchand cf1ae9f9e0 bpftool: Support setting alternative arch for JIT disasm with LLVM
Bugzilla: https://bugzilla.redhat.com/2177177

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

    bpftool: Support setting alternative arch for JIT disasm with LLVM

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

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

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:56 +02:00
Jerome Marchand bbd94960ae bpftool: Define _GNU_SOURCE only once
Bugzilla: https://bugzilla.redhat.com/2177177

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

    bpftool: Define _GNU_SOURCE only once

    _GNU_SOURCE is defined in several source files for bpftool, but only one
    of them takes the precaution of checking whether the value is already
    defined. Add #ifndef for other occurrences too.

    This is in preparation for the support of disassembling JIT-ed programs
    with LLVM, with $(llvm-config --cflags) passing -D_GNU_SOURCE as a
    compilation argument.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:55 +02:00
Artem Savkov 6ad48578c7 bpftool: Fix NULL pointer dereference when pin {PROG, MAP, LINK} without FILE
Bugzilla: https://bugzilla.redhat.com/2166911

commit 34de8e6e0e1f66e431abf4123934a2581cb5f133
Author: Pu Lehui <pulehui@huawei.com>
Date:   Wed Nov 2 16:40:34 2022 +0800

    bpftool: Fix NULL pointer dereference when pin {PROG, MAP, LINK} without FILE
    
    When using bpftool to pin {PROG, MAP, LINK} without FILE,
    segmentation fault will occur. The reson is that the lack
    of FILE will cause strlen to trigger NULL pointer dereference.
    The corresponding stacktrace is shown below:
    
    do_pin
      do_pin_any
        do_pin_fd
          mount_bpffs_for_pin
            strlen(name) <- NULL pointer dereference
    
    Fix it by adding validation to the common process.
    
    Fixes: 75a1e792c3 ("tools: bpftool: Allow all prog/map handles for pinning objects")
    Signed-off-by: Pu Lehui <pulehui@huawei.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20221102084034.3342995-1-pulehui@huaweicloud.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:21 +01:00
Artem Savkov a5ed91916f bpftool: Remove BPF_OBJ_NAME_LEN restriction when looking up bpf program by name
Bugzilla: https://bugzilla.redhat.com/2166911

commit d55dfe587bc0670f90564a962615723fe7749ab1
Author: Manu Bretelle <chantr4@gmail.com>
Date:   Mon Aug 1 06:24:09 2022 -0700

    bpftool: Remove BPF_OBJ_NAME_LEN restriction when looking up bpf program by name
    
    bpftool was limiting the length of names to BPF_OBJ_NAME_LEN in prog_parse
    fds.
    
    Since commit b662000aff84 ("bpftool: Adding support for BTF program names")
    we can get the full program name from BTF.
    
    This patch removes the restriction of name length when running `bpftool
    prog show name ${name}`.
    
    Test:
    Tested against some internal program names that were longer than
    `BPF_OBJ_NAME_LEN`, here a redacted example of what was ran to test.
    
        # previous behaviour
        $ sudo bpftool prog show name some_long_program_name
        Error: can't parse name
        # with the patch
        $ sudo ./bpftool prog show name some_long_program_name
        123456789: tracing  name some_long_program_name  tag taghexa  gpl ....
        ...
        ...
        ...
        # too long
        sudo ./bpftool prog show name $(python3 -c 'print("A"*128)')
        Error: can't parse name
        # not too long but no match
        $ sudo ./bpftool prog show name $(python3 -c 'print("A"*127)')
    
    Signed-off-by: Manu Bretelle <chantr4@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Tested-by: Jiri Olsa <jolsa@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220801132409.4147849-1-chantr4@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-03 15:25:55 +01:00
Artem Savkov 07fd736bc2 bpftool: Show also the name of type BPF_OBJ_LINK
Bugzilla: https://bugzilla.redhat.com/2137876

commit 7a255ae77216237a4ce83ddea595aa4e0a812f46
Author: Yafang Shao <laoar.shao@gmail.com>
Date:   Wed Jun 29 15:48:32 2022 +0000

    bpftool: Show also the name of type BPF_OBJ_LINK
    
    For example, /sys/fs/bpf/maps.debug is a BPF link. When you run `bpftool map show`
    to show it:
    
    Before:
    
      $ bpftool map show pinned /sys/fs/bpf/maps.debug
      Error: incorrect object type: unknown
    
    After:
    
      $ bpftool map show pinned /sys/fs/bpf/maps.debug
      Error: incorrect object type: link
    
    Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220629154832.56986-5-laoar.shao@gmail.com

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

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

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

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:28 +01:00
Yauheni Kaliuta cdd6373641 bpftool: Probe for memcg-based accounting before bumping rlimit
Bugzilla: https://bugzilla.redhat.com/2120968

commit f0cf642c56b76dfbbb5f2be67fa180191d5ab0ef
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Wed Jun 29 12:13:51 2022 +0100

    bpftool: Probe for memcg-based accounting before bumping rlimit
    
    Bpftool used to bump the memlock rlimit to make sure to be able to load
    BPF objects. After the kernel has switched to memcg-based memory
    accounting [0] in 5.11, bpftool has relied on libbpf to probe the system
    for memcg-based accounting support and for raising the rlimit if
    necessary [1]. But this was later reverted, because the probe would
    sometimes fail, resulting in bpftool not being able to load all required
    objects [2].
    
    Here we add a more efficient probe, in bpftool itself. We first lower
    the rlimit to 0, then we attempt to load a BPF object (and finally reset
    the rlimit): if the load succeeds, then memcg-based memory accounting is
    supported.
    
    This approach was earlier proposed for the probe in libbpf itself [3],
    but given that the library may be used in multithreaded applications,
    the probe could have undesirable consequences if one thread attempts to
    lock kernel memory while memlock rlimit is at 0. Since bpftool is
    single-threaded and the rlimit is process-based, this is fine to do in
    bpftool itself.
    
    This probe was inspired by the similar one from the cilium/ebpf Go
    library [4].
    
      [0] commit 97306be45f ("Merge branch 'switch to memcg-based memory accounting'")
      [1] commit a777e18f1bcd ("bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK")
      [2] commit 6b4384ff1088 ("Revert "bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK"")
      [3] https://lore.kernel.org/bpf/20220609143614.97837-1-quentin@isovalent.com/t/#u
      [4] https://github.com/cilium/ebpf/blob/v0.9.0/rlimit/rlimit.go#L39
    
    Suggested-by: Daniel Borkmann <daniel@iogearbox.net>
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Stanislav Fomichev <sdf@google.com>
    Acked-by: Yafang Shao <laoar.shao@gmail.com>
    Link: https://lore.kernel.org/bpf/20220629111351.47699-1-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:08 +02:00
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
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 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 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 db7ced1257 bpftool: Update the lists of names for maps and prog-attach types
Bugzilla: https://bugzilla.redhat.com/2069046

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

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

    bpftool: Update the lists of names for maps and prog-attach types

    To support the different BPF map or attach types, bpftool must remain
    up-to-date with the types supported by the kernel. Let's update the
    lists, by adding the missing Bloom filter map type and the perf_event
    attach type.

    Both missing items were found with test_bpftool_synctypes.py.

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

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:37 +02: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
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
Tobias Klauser d444b06e40 bpftool: Check malloc return value in mount_bpffs_for_pin
Fix and add a missing NULL check for the prior malloc() call.

Fixes: 49a086c201 ("bpftool: implement prog load command")
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Roman Gushchin <guro@fb.com>
Link: https://lore.kernel.org/bpf/20210715110609.29364-1-tklauser@distanz.ch
2021-07-15 20:01:36 +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
Tony Ambardar 9165e1d70f bpftool: Use only nftw for file tree parsing
The bpftool sources include code to walk file trees, but use multiple
frameworks to do so: nftw and fts. While nftw conforms to POSIX/SUSv3 and
is widely available, fts is not conformant and less common, especially on
non-glibc systems. The inconsistent framework usage hampers maintenance
and portability of bpftool, in particular for embedded systems.

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

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

Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200721024817.13701-1-Tony.Ambardar@gmail.com
2020-07-21 23:42:56 +02:00
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
Stanislav Fomichev db94cc0b48 bpftool: Add support for BPF_CGROUP_INET_SOCK_RELEASE
Support attaching to BPF_CGROUP_INET_SOCK_RELEASE and properly
display attach type upon prog dump.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200706230128.4073544-4-sdf@google.com
2020-07-08 01:07:36 +02:00
Tobias Klauser 16d37ee3d2 tools, bpftool: Define attach_type_name array only once
Define attach_type_name in common.c instead of main.h so it is only
defined once. This leads to a slight decrease in the binary size of
bpftool.

Before:

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

After:

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

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200624143154.13145-1-tklauser@distanz.ch
2020-06-25 16:06:01 +02:00
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
Andrii Nakryiko c5481f9a95 bpftool: Add bpf_link show and pin support
Add `bpftool link show` and `bpftool link pin` commands.

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

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

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200429001614.1544-9-andriin@fb.com
2020-04-28 17:27:08 -07:00
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
Toke Høiland-Jørgensen 229c3b47b7 bpftool: Use consistent include paths for libbpf
Fix bpftool to include libbpf header files with the bpf/ prefix, to be
consistent with external users of the library. Also ensure that all
includes of exported libbpf header files (those that are exported on 'make
install' of the library) use bracketed includes instead of quoted.

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

Fixes: 6910d7d386 ("selftests/bpf: Ensure bpf_helper_defs.h are taken from selftests dir")
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/157952560684.1683545.4765181397974997027.stgit@toke.dk
2020-01-20 16:37:45 -08:00
David S. Miller 1e46c09ec1 Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next
Daniel Borkmann says:

====================
The following pull-request contains BPF updates for your *net-next* tree.

The main changes are:

1) Add the ability to use unaligned chunks in the AF_XDP umem. By
   relaxing where the chunks can be placed, it allows to use an
   arbitrary buffer size and place whenever there is a free
   address in the umem. Helps more seamless DPDK AF_XDP driver
   integration. Support for i40e, ixgbe and mlx5e, from Kevin and
   Maxim.

2) Addition of a wakeup flag for AF_XDP tx and fill rings so the
   application can wake up the kernel for rx/tx processing which
   avoids busy-spinning of the latter, useful when app and driver
   is located on the same core. Support for i40e, ixgbe and mlx5e,
   from Magnus and Maxim.

3) bpftool fixes for printf()-like functions so compiler can actually
   enforce checks, bpftool build system improvements for custom output
   directories, and addition of 'bpftool map freeze' command, from Quentin.

4) Support attaching/detaching XDP programs from 'bpftool net' command,
   from Daniel.

5) Automatic xskmap cleanup when AF_XDP socket is released, and several
   barrier/{read,write}_once fixes in AF_XDP code, from Björn.

6) Relicense of bpf_helpers.h/bpf_endian.h for future libbpf
   inclusion as well as libbpf versioning improvements, from Andrii.

7) Several new BPF kselftests for verifier precision tracking, from Alexei.

8) Several BPF kselftest fixes wrt endianess to run on s390x, from Ilya.

9) And more BPF kselftest improvements all over the place, from Stanislav.

10) Add simple BPF map op cache for nfp driver to batch dumps, from Jakub.

11) AF_XDP socket umem mapping improvements for 32bit archs, from Ivan.

12) Add BPF-to-BPF call and BTF line info support for s390x JIT, from Yauheni.

13) Small optimization in arm64 JIT to spare 1 insns for BPF_MOD, from Jerin.

14) Fix an error check in bpf_tcp_gen_syncookie() helper, from Petar.

15) Various minor fixes and cleanups, from Nathan, Masahiro, Masanari,
    Peter, Wei, Yue.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
2019-09-06 16:49:17 +02:00
Quentin Monnet 8918dc42dc tools: bpftool: move "__printf()" attributes to header file
Some functions in bpftool have a "__printf()" format attributes to tell
the compiler they should expect printf()-like arguments. But because
these attributes are not used for the function prototypes in the header
files, the compiler does not run the checks everywhere the functions are
used, and some mistakes on format string and corresponding arguments
slipped in over time.

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

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

Fixes: c101189bc9 ("tools: bpftool: fix -Wmissing declaration warnings")
Reported-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-08-15 22:06:46 -07:00
Jakub Kicinski 3c7be384fe tools: bpftool: add error message on pin failure
No error message is currently printed if the pin syscall
itself fails. It got lost in the loadall refactoring.

Fixes: 77380998d9 ("bpftool: add loadall command")
Reported-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-08-09 17:38:53 +02:00
Jakub Kicinski b3e78adcbf tools: bpftool: fix error message (prog -> object)
Change an error message to work for any object being
pinned not just programs.

Fixes: 71bb428fe2 ("tools: bpf: add bpftool")
Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-08-09 17:38:53 +02:00
Hechao Li 4c587c196d bpf: use libbpf_num_possible_cpus internally
Use the newly added bpf_num_possible_cpus() in bpftool and selftests
and remove duplicate implementations.

Signed-off-by: Hechao Li <hechaol@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-06-11 10:36:02 +02:00
Jakub Kicinski 8c79b35693 tools: bpftool: fix crash with un-owned prog arrays
Prog arrays don't have 'owner_prog_type' and 'owner_jited'
fields in their fdinfo when they are created.  Those fields
are set and reported when first program is checked for
compatibility by bpf_prog_array_compatible().

This means that bpftool cannot expect the fields to always
be there.  Currently trying to show maps on a system with
an un-owned prog array leads to a crash:

$ bpftool map show
389: prog_array  name tail_call_map  flags 0x0
Error: key 'owner_prog_type' not found in fdinfo
Error: key 'owner_jited' not found in fdinfo
       key 4B  value 4B  max_entries 4  memlock 4096B
       Segmentation fault (core dumped)

We pass a NULL pointer to atoi().

Remove the assumption that fdinfo keys are always present.
Add missing validations and remove the p_err() calls which
may lead to broken JSON output as caller will not propagate
the failure.

Fixes: 99a44bef58 ("tools: bpftool: add owner_prog_type and owner_jited to bpftool output")
Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-01-28 23:59:46 +01:00
Quentin Monnet 33221307c3 tools: bpftool: add an option to prevent auto-mount of bpffs, tracefs
In order to make life easier for users, bpftool automatically attempts
to mount the BPF virtual file system, if it is not mounted already,
before trying to pin objects in it. Similarly, it attempts to mount
tracefs if necessary before trying to dump the trace pipe to the
console.

While mounting file systems on-the-fly can improve user experience, some
administrators might prefer to avoid that. Let's add an option to block
these mount attempts. Note that it does not prevent automatic mounting
of tracefs by debugfs for the "bpftool prog tracelog" command.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-12-18 14:47:17 +01:00
Quentin Monnet be3245e22d tools: bpftool: attempt to mount tracefs if required for tracelog cmd
As a follow-up to commit 30da46b5dc ("tools: bpftool: add a command to
dump the trace pipe"), attempt to mount the tracefs virtual file system
if it is not detected on the system before trying to dump content of the
tracing pipe on an invocation of "bpftool prog tracelog".

Usually, tracefs in automatically mounted by debugfs when the user tries
to access it (e.g. "ls /sys/kernel/debug/tracing" mounts the tracefs).
So if we failed to find it, it is probably that debugfs is not here
either. Therefore, we just attempt a single mount, at a location that
does not involve debugfs: /sys/kernel/tracing.

Suggested-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-12-18 14:47:17 +01:00
Quentin Monnet c101189bc9 tools: bpftool: fix -Wmissing declaration warnings
Help compiler check arguments for several utility functions used to
print items to the console by adding the "printf" attribute when
declaring those functions.

Also, declare as "static" two functions that are only used in prog.c.

All of them discovered by compiling bpftool with
-Wmissing-format-attribute -Wmissing-declarations.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-12-15 01:31:49 +01:00
Jakub Kicinski 02ff58dcf7 tools: bpftool: replace Netronome boilerplate with SPDX license headers
Replace the repeated license text with SDPX identifiers.

Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Acked-by: Roman Gushchin <guro@fb.com>
Acked-by: YueHaibing <yuehaibing@huawei.com>
Acked-by: Yonghong Song <yhs@fb.com>
Acked-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Sean Young <sean@mess.org>
Acked-by: Jiri Benc <jbenc@redhat.com>
Acked-by: David Calavera <david.calavera@gmail.com>
Acked-by: Andrey Ignatov <rdna@fb.com>
Acked-by: Joe Stringer <joe@wand.net.nz>
Acked-by: David Ahern <dsahern@gmail.com>
Acked-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Acked-by: Petar Penkov <ppenkov@stanford.edu>
Acked-by: Sandipan Das <sandipan@linux.ibm.com>
Acked-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Acked-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Taeung Song <treeze.taeung@gmail.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
CC: okash.khawaja@gmail.com
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-12-13 12:08:44 +01:00
Quentin Monnet 73f0b9db53 tools: bpftool: mark offloaded programs more explicitly in plain output
In bpftool (plain) output for "bpftool prog show" or "bpftool map show",
an offloaded BPF object is simply denoted with "dev ifname", which is
not really explicit. Change it with something that clearly shows the
program is offloaded.

While at it also add an additional space, as done between other
information fields.

Example output, before:

    # bpftool prog show
    1337: xdp  tag a04f5eef06a7f555 dev foo
            loaded_at 2018-10-19T16:40:36+0100  uid 0
            xlated 16B  not jited  memlock 4096B

After:

    # bpftool prog show
    1337: xdp  tag a04f5eef06a7f555  offloaded_to foo
            loaded_at 2018-10-19T16:40:36+0100  uid 0
            xlated 16B  not jited  memlock 4096B

Suggested-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-11-30 14:06:06 -08:00
Quentin Monnet 327e5dab6d tools: bpftool: use "/proc/self/" i.o. crafting links with getpid()
The getpid() function is called in a couple of places in bpftool to
craft links of the shape "/proc/<pid>/...". Instead, it is possible to
use the "/proc/self/" shortcut, which makes things a bit easier, in
particular in jit_disasm.c.

Do the replacement, and remove the includes of <sys/types.h> from the
relevant files, now we do not use getpid() anymore.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-11-30 14:06:06 -08:00
David S. Miller e561bb29b6 Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
Trivial conflict in net/core/filter.c, a locally computed
'sdif' is now an argument to the function.

Signed-off-by: David S. Miller <davem@davemloft.net>
2018-11-28 22:10:54 -08:00
Stanislav Fomichev 77380998d9 bpftool: add loadall command
This patch adds new *loadall* command which slightly differs from the
existing *load*. *load* command loads all programs from the obj file,
but pins only the first programs. *loadall* pins all programs from the
obj file under specified directory.

The intended usecase is flow_dissector, where we want to load a bunch
of progs, pin them all and after that construct a jump table.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-11-10 15:56:11 -08:00
Quentin Monnet f120919f99 tools: bpftool: pass an argument to silence open_obj_pinned()
Function open_obj_pinned() prints error messages when it fails to open a
link in the BPF virtual file system. However, in some occasions it is
not desirable to print an error, for example when we parse all links
under the bpffs root, and the error is due to some paths actually being
symbolic links.

Example output:

    # ls -l /sys/fs/bpf/
    lrwxrwxrwx 1 root root 0 Oct 18 19:00 ip -> /sys/fs/bpf/tc/
    drwx------ 3 root root 0 Oct 18 19:00 tc
    lrwxrwxrwx 1 root root 0 Oct 18 19:00 xdp -> /sys/fs/bpf/tc/

    # bpftool --bpffs prog show
    Error: bpf obj get (/sys/fs/bpf): Permission denied
    Error: bpf obj get (/sys/fs/bpf): Permission denied

    # strace -e bpf bpftool --bpffs prog show
    bpf(BPF_OBJ_GET, {pathname="/sys/fs/bpf/ip", bpf_fd=0}, 72) = -1 EACCES (Permission denied)
    Error: bpf obj get (/sys/fs/bpf): Permission denied
    bpf(BPF_OBJ_GET, {pathname="/sys/fs/bpf/xdp", bpf_fd=0}, 72) = -1 EACCES (Permission denied)
    Error: bpf obj get (/sys/fs/bpf): Permission denied
    ...

To fix it, pass a bool as a second argument to the function, and prevent
it from printing an error when the argument is set to true.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-11-09 08:20:52 +01:00
Quentin Monnet 53909030aa tools: bpftool: prevent infinite loop in get_fdinfo()
Function getline() returns -1 on failure to read a line, thus creating
an infinite loop in get_fdinfo() if the key is not found. Fix it by
calling the function only as long as we get a strictly positive return
value.

Found by copying the code for a key which is not always present...

Fixes: 71bb428fe2 ("tools: bpf: add bpftool")
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-11-09 08:20:52 +01:00
Quentin Monnet 8302b9bd31 tools: bpftool: adjust rlimit RLIMIT_MEMLOCK when loading programs, maps
The limit for memory locked in the kernel by a process is usually set to
64 kbytes by default. This can be an issue when creating large BPF maps
and/or loading many programs. A workaround is to raise this limit for
the current process before trying to create a new BPF map. Changing the
hard limit requires the CAP_SYS_RESOURCE and can usually only be done by
root user (for non-root users, a call to setrlimit fails (and sets
errno) and the program simply goes on with its rlimit unchanged).

There is no API to get the current amount of memory locked for a user,
therefore we cannot raise the limit only when required. One solution,
used by bcc, is to try to create the map, and on getting a EPERM error,
raising the limit to infinity before giving another try. Another
approach, used in iproute2, is to raise the limit in all cases, before
trying to create the map.

Here we do the same as in iproute2: the rlimit is raised to infinity
before trying to load programs or to create maps with bpftool.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2018-11-07 22:22:21 +01:00