From: Quentin Monnet <quentin@isovalent.com>
To: Yonghong Song <yhs@fb.com>, Andrii Nakryiko <andriin@fb.com>,
bpf@vger.kernel.org, Martin KaFai Lau <kafai@fb.com>,
netdev@vger.kernel.org
Cc: Alexei Starovoitov <ast@fb.com>,
Daniel Borkmann <daniel@iogearbox.net>,
kernel-team@fb.com
Subject: Re: [PATCH bpf-next v1 16/19] tools/bpftool: add bpf_iter support for bptool
Date: Tue, 28 Apr 2020 10:27:23 +0100 [thread overview]
Message-ID: <05d9c82d-8cba-db77-02af-265e4d200946@isovalent.com> (raw)
In-Reply-To: <20200427201253.2996156-1-yhs@fb.com>
2020-04-27 13:12 UTC-0700 ~ Yonghong Song <yhs@fb.com>
> 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.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
> .../bpftool/Documentation/bpftool-iter.rst | 71 ++++++++++++++++
> tools/bpf/bpftool/bash-completion/bpftool | 13 +++
> tools/bpf/bpftool/iter.c | 84 +++++++++++++++++++
> tools/bpf/bpftool/main.c | 3 +-
> tools/bpf/bpftool/main.h | 1 +
> 5 files changed, 171 insertions(+), 1 deletion(-)
> create mode 100644 tools/bpf/bpftool/Documentation/bpftool-iter.rst
> create mode 100644 tools/bpf/bpftool/iter.c
>
> diff --git a/tools/bpf/bpftool/Documentation/bpftool-iter.rst b/tools/bpf/bpftool/Documentation/bpftool-iter.rst
> new file mode 100644
> index 000000000000..1997a6bac4a0
> --- /dev/null
> +++ b/tools/bpf/bpftool/Documentation/bpftool-iter.rst
> @@ -0,0 +1,71 @@
> +============
> +bpftool-iter
> +============
> +-------------------------------------------------------------------------------
> +tool to create BPF iterators
> +-------------------------------------------------------------------------------
> +
> +:Manual section: 8
> +
> +SYNOPSIS
> +========
> +
> + **bpftool** [*OPTIONS*] **iter** *COMMAND*
> +
> + *COMMANDS* := { **pin** | **help** }
> +
> +STRUCT_OPS COMMANDS
s/STRUCT_OPS/ITER/
> +===================
> +
> +| **bpftool** **iter pin** *OBJ* *PATH*
> +| **bpftool** **struct_ops help**
s/struct_ops/iter/
> +|
> +| *OBJ* := /a/file/of/bpf_iter_target.o
> +
> +
> +DESCRIPTION
> +===========
> + **bpftool iter pin** *OBJ* *PATH*
Would be great to have a small blurb on what BPF iterators are and what
they can do. I'm afraid users reading this man page will have no idea
whatsoever.
> + Create a bpf iterator from *OBJ*, and pin it to
> + *PATH*. The *PATH* should be located in *bpffs* mount.
Can you keep the note that other pages have about the dot character
being forbidden in *PATH* basename, please?
> +
> + **bpftool struct_ops help**
s/struct_ops/iter/
> + Print short help message.
> +
> +OPTIONS
> +=======
> + -h, --help
> + Print short generic help message (similar to **bpftool help**).
> +
> + -V, --version
> + Print version number (similar to **bpftool version**).
> +
> + -d, --debug
> + Print all logs available, even debug-level information. This
> + includes logs from libbpf as well as from the verifier, when
> + attempting to load programs.> +
> +EXAMPLES
> +========
> +**# bpftool iter pin bpf_iter_netlink.o /sys/fs/bpf/my_netlink**
> +
> +::
> +
> + Create a file-based bpf iterator from bpf_iter_netlink.o and pin it
> + to /sys/fs/bpf/my_netlink
> +
> +
> +SEE ALSO
> +========
> + **bpf**\ (2),
> + **bpf-helpers**\ (7),
> + **bpftool**\ (8),
> + **bpftool-prog**\ (8),
> + **bpftool-map**\ (8),
> + **bpftool-cgroup**\ (8),
> + **bpftool-feature**\ (8),
> + **bpftool-net**\ (8),
> + **bpftool-perf**\ (8),
> + **bpftool-btf**\ (8)
> + **bpftool-gen**\ (8)
> + **bpftool-struct_ops**\ (8)
> diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
> index 45ee99b159e2..17a81695da0f 100644
> --- a/tools/bpf/bpftool/bash-completion/bpftool
> +++ b/tools/bpf/bpftool/bash-completion/bpftool
> @@ -604,6 +604,19 @@ _bpftool()
> ;;
> esac
> ;;
> + iter)
> + case $command in
> + pin)
> + _filedir
> + return 0
> + ;;
> + *)
> + [[ $prev == $object ]] && \
> + COMPREPLY=( $( compgen -W 'help' \
> + -- "$cur" ) )
You should probably offer "pin" here in addition to "help".
> + ;;
> + esac
> + ;;
> map)
> local MAP_TYPE='id pinned name'
> case $command in
> diff --git a/tools/bpf/bpftool/iter.c b/tools/bpf/bpftool/iter.c
> new file mode 100644
> index 000000000000..db9fae6be716
> --- /dev/null
> +++ b/tools/bpf/bpftool/iter.c
> @@ -0,0 +1,84 @@
> +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +// Copyright (C) 2020 Facebook
> +
> +#define _GNU_SOURCE
> +#include <linux/err.h>
> +#include <bpf/libbpf.h>
> +
> +#include "main.h"
> +
> +static int do_pin(int argc, char **argv)
> +{
> + const char *objfile, *path;
> + struct bpf_program *prog;
> + struct bpf_object *obj;
> + struct bpf_link *link;
> + int err;
Nit: initialise err t0 -1 do you don't have to set it three times below?
> +
> + if (!REQ_ARGS(2))
> + usage();
> +
> + objfile = GET_ARG();
> + path = GET_ARG();
> +
> + obj = bpf_object__open(objfile);
> + if (IS_ERR_OR_NULL(obj)) {
> + p_err("can't open objfile %s", objfile);
> + return -1;
> + }
> +
> + err = bpf_object__load(obj);
> + if (err < 0) {
> + err = -1;
> + p_err("can't load objfile %s", objfile);
> + goto close_obj;
> + }
> +
> + prog = bpf_program__next(NULL, obj);
> + link = bpf_program__attach_iter(prog, NULL);
> + if (IS_ERR(link)) {
> + err = -1;
> + p_err("attach_iter failed for program %s",
> + bpf_program__name(prog));
> + goto close_obj;
> + }
> +
> + err = bpf_link__pin(link, path);
Try to mount bpffs before that if "-n" is not passed? You could even
call do_pin_any() from common.c by passing bpf_link__fd().
> + if (err) {
> + err = -1;
> + p_err("pin_iter failed for program %s to path %s",
> + bpf_program__name(prog), path);
> + goto close_link;
> + }
> +
> + err = 0;
> +
> +close_link:
> + bpf_link__disconnect(link);
> + bpf_link__destroy(link);
> +close_obj:
> + bpf_object__close(obj);
> + return err;
> +}
> +
> +static int do_help(int argc, char **argv)
> +{
> + fprintf(stderr,
> + "Usage: %s %s pin OBJ PATH\n"
> + " %s %s help\n"
> + "\n",
> + bin_name, argv[-2], bin_name, argv[-2]);
> +
> + return 0;
> +}
> +
> +static const struct cmd cmds[] = {
> + { "help", do_help },
> + { "pin", do_pin },
> + { 0 }
> +};
> +
> +int do_iter(int argc, char **argv)
> +{
> + return cmd_select(cmds, argc, argv, do_help);
> +}
> dif "",
> bin_name, bin_name, bin_name);
> @@ -222,6 +222,7 @@ static const struct cmd cmds[] = {
> { "btf", do_btf },
> { "gen", do_gen },
> { "struct_ops", do_struct_ops },
> + { "iter", do_iter },
> { "version", do_version },
> { 0 }
> };
> diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
> index 86f14ce26fd7..2b5d4a616b48 100644
> --- a/tools/bpf/bpftool/main.h
> +++ b/tools/bpf/bpftool/main.h
> @@ -162,6 +162,7 @@ int do_feature(int argc, char **argv);
> int do_btf(int argc, char **argv);
> int do_gen(int argc, char **argv);
> int do_struct_ops(int argc, char **argv);
> +int do_iter(int argc, char **argv);
>
> int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
> int prog_parse_fd(int *argc, char ***argv);
> f --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
> index 466c269eabdd..6805b77789cb 100644
> --- a/tools/bpf/bpftool/main.c
> +++ b/tools/bpf/bpftool/main.c
> @@ -58,7 +58,7 @@ static int do_help(int argc, char **argv)
> " %s batch file FILE\n"
> " %s version\n"
> "\n"
> - " OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops }\n"
> + " OBJECT := { prog | map | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
> " " HELP_SPEC_OPTIONS "\n"
> "",
> bin_name, bin_name, bin_name);
> @@ -222,6 +222,7 @@ static const struct cmd cmds[] = {
> { "btf", do_btf },
> { "gen", do_gen },
> { "struct_ops", do_struct_ops },
> + { "iter", do_iter },
> { "version", do_version },
> { 0 }
> };
> diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
> index 86f14ce26fd7..2b5d4a616b48 100644
> --- a/tools/bpf/bpftool/main.h
> +++ b/tools/bpf/bpftool/main.h
> @@ -162,6 +162,7 @@ int do_feature(int argc, char **argv);
> int do_btf(int argc, char **argv);
> int do_gen(int argc, char **argv);
> int do_struct_ops(int argc, char **argv);
> +int do_iter(int argc, char **argv);
>
> int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
> int prog_parse_fd(int *argc, char ***argv);
>
Have you considered simply adapting the more traditional workflow
"bpftool prog load && bpftool prog attach" so that it supports iterators
instead of adding a new command? It would:
- Avoid adding yet another bpftool command with a single subcommand
- Enable to reuse the code from prog load, in particular for map reuse
(I'm not sure how relevant maps are for iterators, but I wouldn't be
surprised if someone finds a use case at some point?)
- Avoid users naively trying to run "bpftool prog load && bpftool prog
attach <prog> iter" and not understanding why it fails
Quentin
next prev parent reply other threads:[~2020-04-28 9:27 UTC|newest]
Thread overview: 81+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-27 20:12 [PATCH bpf-next v1 00/19] bpf: implement bpf iterator for kernel data Yonghong Song
2020-04-27 20:12 ` [PATCH bpf-next v1 01/19] net: refactor net assignment for seq_net_private structure Yonghong Song
2020-04-29 5:38 ` Andrii Nakryiko
2020-04-27 20:12 ` [PATCH bpf-next v1 02/19] bpf: implement an interface to register bpf_iter targets Yonghong Song
2020-04-28 16:20 ` Martin KaFai Lau
2020-04-28 16:50 ` Yonghong Song
2020-04-27 20:12 ` [PATCH bpf-next v1 03/19] bpf: add bpf_map iterator Yonghong Song
2020-04-29 0:37 ` Martin KaFai Lau
2020-04-29 0:48 ` Alexei Starovoitov
2020-04-29 1:15 ` Yonghong Song
2020-04-29 2:44 ` Alexei Starovoitov
2020-04-29 5:09 ` Yonghong Song
2020-04-29 6:08 ` Andrii Nakryiko
2020-04-29 6:20 ` Yonghong Song
2020-04-29 6:30 ` Alexei Starovoitov
2020-04-29 6:40 ` Andrii Nakryiko
2020-04-29 6:44 ` Yonghong Song
2020-04-29 15:34 ` Alexei Starovoitov
2020-04-29 18:14 ` Yonghong Song
2020-04-29 19:19 ` Andrii Nakryiko
2020-04-29 20:15 ` Yonghong Song
2020-04-30 3:06 ` Alexei Starovoitov
2020-04-30 4:01 ` Yonghong Song
2020-04-29 6:34 ` Martin KaFai Lau
2020-04-29 6:51 ` Yonghong Song
2020-04-29 19:25 ` Andrii Nakryiko
2020-04-29 1:02 ` Yonghong Song
2020-04-29 6:04 ` Andrii Nakryiko
2020-04-27 20:12 ` [PATCH bpf-next v1 04/19] bpf: allow loading of a bpf_iter program Yonghong Song
2020-04-29 0:54 ` Martin KaFai Lau
2020-04-29 1:27 ` Yonghong Song
2020-04-27 20:12 ` [PATCH bpf-next v1 05/19] bpf: support bpf tracing/iter programs for BPF_LINK_CREATE Yonghong Song
2020-04-29 1:17 ` [Potential Spoof] " Martin KaFai Lau
2020-04-29 6:25 ` Andrii Nakryiko
2020-04-27 20:12 ` [PATCH bpf-next v1 06/19] bpf: support bpf tracing/iter programs for BPF_LINK_UPDATE Yonghong Song
2020-04-29 1:32 ` Martin KaFai Lau
2020-04-29 5:04 ` Yonghong Song
2020-04-29 5:58 ` Martin KaFai Lau
2020-04-29 6:32 ` Andrii Nakryiko
2020-04-29 6:41 ` Martin KaFai Lau
2020-04-27 20:12 ` [PATCH bpf-next v1 07/19] bpf: create anonymous bpf iterator Yonghong Song
2020-04-29 5:39 ` Martin KaFai Lau
2020-04-29 6:56 ` Andrii Nakryiko
2020-04-29 7:06 ` Yonghong Song
2020-04-29 18:16 ` Andrii Nakryiko
2020-04-29 18:46 ` Martin KaFai Lau
2020-04-29 19:20 ` Yonghong Song
2020-04-29 20:50 ` Martin KaFai Lau
2020-04-29 20:54 ` Yonghong Song
2020-04-29 19:39 ` Andrii Nakryiko
2020-04-27 20:12 ` [PATCH bpf-next v1 08/19] bpf: create file " Yonghong Song
2020-04-29 20:40 ` Andrii Nakryiko
2020-04-30 18:02 ` Yonghong Song
2020-04-27 20:12 ` [PATCH bpf-next v1 09/19] bpf: add PTR_TO_BTF_ID_OR_NULL support Yonghong Song
2020-04-29 20:46 ` Andrii Nakryiko
2020-04-29 20:51 ` Yonghong Song
2020-04-27 20:12 ` [PATCH bpf-next v1 10/19] bpf: add netlink and ipv6_route targets Yonghong Song
2020-04-28 19:49 ` kbuild test robot
2020-04-28 19:50 ` [RFC PATCH] bpf: __bpf_iter__netlink() can be static kbuild test robot
2020-04-27 20:12 ` [PATCH bpf-next v1 11/19] bpf: add task and task/file targets Yonghong Song
2020-04-30 2:08 ` Andrii Nakryiko
2020-05-01 17:23 ` Yonghong Song
2020-05-01 19:01 ` Andrii Nakryiko
2020-04-27 20:12 ` [PATCH bpf-next v1 12/19] bpf: add bpf_seq_printf and bpf_seq_write helpers Yonghong Song
2020-04-28 6:02 ` kbuild test robot
2020-04-28 16:35 ` Yonghong Song
2020-04-30 20:06 ` Andrii Nakryiko
2020-04-27 20:12 ` [PATCH bpf-next v1 13/19] bpf: handle spilled PTR_TO_BTF_ID properly when checking stack_boundary Yonghong Song
2020-04-27 20:12 ` [PATCH bpf-next v1 14/19] bpf: support variable length array in tracing programs Yonghong Song
2020-04-30 20:04 ` Andrii Nakryiko
2020-04-27 20:12 ` [PATCH bpf-next v1 15/19] tools/libbpf: add bpf_iter support Yonghong Song
2020-04-30 1:41 ` Andrii Nakryiko
2020-05-02 7:17 ` Yonghong Song
2020-04-27 20:12 ` [PATCH bpf-next v1 16/19] tools/bpftool: add bpf_iter support for bptool Yonghong Song
2020-04-28 9:27 ` Quentin Monnet [this message]
2020-04-28 17:35 ` Yonghong Song
2020-04-29 8:37 ` Quentin Monnet
2020-04-27 20:12 ` [PATCH bpf-next v1 17/19] tools/bpf: selftests: add iterator programs for ipv6_route and netlink Yonghong Song
2020-04-30 2:12 ` Andrii Nakryiko
2020-04-27 20:12 ` [PATCH bpf-next v1 18/19] tools/bpf: selftests: add iter progs for bpf_map/task/task_file Yonghong Song
2020-04-27 20:12 ` [PATCH bpf-next v1 19/19] tools/bpf: selftests: add bpf_iter selftests Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=05d9c82d-8cba-db77-02af-265e4d200946@isovalent.com \
--to=quentin@isovalent.com \
--cc=andriin@fb.com \
--cc=ast@fb.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kafai@fb.com \
--cc=kernel-team@fb.com \
--cc=netdev@vger.kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).