Commit Graph

11 Commits

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

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

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

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

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

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

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

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

    bpftool: clean-up usage of libbpf_get_error()

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

    The changes stay aligned with returned errors always negative.

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

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:09 +02:00
Jerome Marchand 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 4638817e0a bpftool: Use libbpf_get_error() to check error
Bugzilla: https://bugzilla.redhat.com/2069046

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

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

    bpftool: Use libbpf_get_error() to check error

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

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

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:37 +02:00
Yauheni Kaliuta aa580f2f50 selftests/bpf: Switch to new bpf_object__next_{map,program} APIs
Bugzilla: http://bugzilla.redhat.com/2069045

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

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

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:39 +03:00
Jerome Marchand b1eab37952 tools: bpftool: Update and synchronise option list in doc and help msg
Bugzilla: http://bugzilla.redhat.com/2041365

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

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

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

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

    Let's update the lists:

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

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:37 +02:00
Yonghong Song 74fc097de3 tools/bpf: Support new uapi for map element bpf iterator
Previous commit adjusted kernel uapi for map
element bpf iterator. This patch adjusted libbpf API
due to uapi change. bpftool and bpf_iter selftests
are also changed accordingly.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200805055058.1457623-1-yhs@fb.com
2020-08-06 16:39:14 -07:00
Yonghong Song d8793aca70 tools/bpftool: Add bpftool support for bpf map element iterator
The optional parameter "map MAP" can be added to "bpftool iter"
command to create a bpf iterator for map elements. For example,
  bpftool iter pin ./prog.o /sys/fs/bpf/p1 map id 333

For map element bpf iterator "map MAP" parameter is required.
Otherwise, bpf link creation will return an error.

Quentin Monnet kindly provided bash-completion implementation
for new "map MAP" option.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200723184119.590799-1-yhs@fb.com
2020-07-25 20:16:33 -07:00
Quentin Monnet 90040351a8 tools, bpftool: Clean subcommand help messages
This is a clean-up for the formatting of the do_help functions for
bpftool's subcommands. The following fixes are included:

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

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

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

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

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200523010751.23465-1-quentin@isovalent.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-06-01 14:38:18 -07:00
Yonghong Song 9406b485de tools/bpftool: Add bpf_iter support for bptool
Currently, only one command is supported
  bpftool iter pin <bpf_prog.o> <path>

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

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

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

This patch also added BPF_LINK_TYPE_ITER to link query.

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

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

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200509175920.2477247-1-yhs@fb.com
2020-05-09 17:05:27 -07:00