From: Andrey Ignatov <rdna@fb.com>
To: Jakub Kicinski <jakub.kicinski@netronome.com>
Cc: <alexei.starovoitov@gmail.com>, <daniel@iogearbox.net>,
<oss-drivers@netronome.com>, <netdev@vger.kernel.org>
Subject: Re: [PATCH bpf-next v3 05/13] tools: libbpf: expose the prog type guessing from section name logic
Date: Tue, 10 Jul 2018 20:01:55 -0700 [thread overview]
Message-ID: <20180711030154.GA52033@rdna-mbp> (raw)
In-Reply-To: <20180710214307.4834-6-jakub.kicinski@netronome.com>
Jakub Kicinski <jakub.kicinski@netronome.com> [Tue, 2018-07-10 14:43 -0700]:
> libbpf can guess program type based on ELF section names. As libbpf
> becomes more popular its association between section name strings and
> types becomes more of a standard. Allow libbpf users to use the same
> logic for matching strings to types, e.g. when the string originates
> from command line.
>
> Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
> ---
> v3 (Andrey):
> - use -EINVAL error code;
> - rename helper to libbpf_prog_type_by_name().
> ---
> tools/lib/bpf/libbpf.c | 43 ++++++++++++++++++++++++------------------
> tools/lib/bpf/libbpf.h | 3 +++
> 2 files changed, 28 insertions(+), 18 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 38ed3e92e393..42f31eff5f3f 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -2081,23 +2081,31 @@ static const struct {
> #undef BPF_S_PROG_SEC
> #undef BPF_SA_PROG_SEC
>
> -static int bpf_program__identify_section(struct bpf_program *prog)
> +int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
> + enum bpf_attach_type *expected_attach_type)
> {
> int i;
>
> - if (!prog->section_name)
> - goto err;
> -
> - for (i = 0; i < ARRAY_SIZE(section_names); i++)
> - if (strncmp(prog->section_name, section_names[i].sec,
> - section_names[i].len) == 0)
> - return i;
> + if (!name)
> + return -EINVAL;
>
> -err:
> - pr_warning("failed to guess program type based on section name %s\n",
> - prog->section_name);
> + for (i = 0; i < ARRAY_SIZE(section_names); i++) {
> + if (strncmp(name, section_names[i].sec, section_names[i].len))
> + continue;
> + *prog_type = section_names[i].prog_type;
> + *expected_attach_type = section_names[i].expected_attach_type;
> + return 0;
> + }
> + return -EINVAL;
> +}
>
> - return -1;
> +static int
> +bpf_program__identify_section(struct bpf_program *prog,
> + enum bpf_prog_type *prog_type,
> + enum bpf_attach_type *expected_attach_type)
> +{
> + return libbpf_prog_type_by_name(prog->section_name, prog_type,
> + expected_attach_type);
> }
>
> int bpf_map__fd(struct bpf_map *map)
> @@ -2230,7 +2238,6 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
> enum bpf_prog_type prog_type;
> struct bpf_object *obj;
> struct bpf_map *map;
> - int section_idx;
> int err;
>
> if (!attr)
> @@ -2252,14 +2259,14 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
> prog->prog_ifindex = attr->ifindex;
> expected_attach_type = attr->expected_attach_type;
> if (prog_type == BPF_PROG_TYPE_UNSPEC) {
> - section_idx = bpf_program__identify_section(prog);
> - if (section_idx < 0) {
> + err = bpf_program__identify_section(prog, &prog_type,
> + &expected_attach_type);
> + if (err < 0) {
> + pr_warning("failed to guess program type based on section name %s\n",
> + prog->section_name);
> bpf_object__close(obj);
> return -EINVAL;
> }
> - prog_type = section_names[section_idx].prog_type;
> - expected_attach_type =
> - section_names[section_idx].expected_attach_type;
> }
>
> bpf_program__set_type(prog, prog_type);
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 564f4be9bae0..d1ce5c828e2e 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -92,6 +92,9 @@ int bpf_object__set_priv(struct bpf_object *obj, void *priv,
> bpf_object_clear_priv_t clear_priv);
> void *bpf_object__priv(struct bpf_object *prog);
>
> +int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
> + enum bpf_attach_type *expected_attach_type);
> +
> /* Accessors of bpf_program */
> struct bpf_program;
> struct bpf_program *bpf_program__next(struct bpf_program *prog,
> --
> 2.17.1
>
Thanks for changes Jakub!
Acked-by: Andrey Ignatov <rdna@fb.com>
--
Andrey Ignatov
next prev parent reply other threads:[~2018-07-11 3:04 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-10 21:42 [PATCH bpf-next v3 00/13] tools: bpf: extend bpftool prog load Jakub Kicinski
2018-07-10 21:42 ` [PATCH bpf-next v3 01/13] selftests/bpf: remove duplicated word from test offloads Jakub Kicinski
2018-07-10 21:42 ` [PATCH bpf-next v3 02/13] selftests/bpf: add Error: prefix in check_extack helper Jakub Kicinski
2018-07-10 21:42 ` [PATCH bpf-next v3 03/13] tools: bpftool: refactor argument parsing for prog load Jakub Kicinski
2018-07-10 21:42 ` [PATCH bpf-next v3 04/13] tools: bpftool: add support for loading programs for offload Jakub Kicinski
2018-07-10 21:42 ` [PATCH bpf-next v3 05/13] tools: libbpf: expose the prog type guessing from section name logic Jakub Kicinski
2018-07-11 3:01 ` Andrey Ignatov [this message]
2018-07-10 21:43 ` [PATCH bpf-next v3 06/13] tools: bpftool: allow users to specify program type for prog load Jakub Kicinski
2018-07-10 21:43 ` [PATCH bpf-next v3 07/13] tools: libbpf: recognize offload neutral maps Jakub Kicinski
2018-07-10 21:43 ` [PATCH bpf-next v3 08/13] tools: libbpf: add extended attributes version of bpf_object__open() Jakub Kicinski
2018-07-11 3:03 ` Andrey Ignatov
2018-07-10 21:43 ` [PATCH bpf-next v3 09/13] tools: bpftool: reimplement bpf_prog_load() for prog load Jakub Kicinski
2018-07-10 21:43 ` [PATCH bpf-next v3 10/13] tools: libbpf: move library error code into a separate file Jakub Kicinski
2018-07-10 21:43 ` [PATCH bpf-next v3 11/13] tools: bpf: make use of reallocarray Jakub Kicinski
2018-07-13 23:53 ` [bpf-next,v3,11/13] " Guenter Roeck
2018-07-14 0:07 ` Jakub Kicinski
2018-07-14 0:31 ` Guenter Roeck
2018-07-14 1:16 ` Jakub Kicinski
2018-07-10 21:43 ` [PATCH bpf-next v3 12/13] tools: libbpf: allow map reuse Jakub Kicinski
2018-07-11 3:45 ` Andrey Ignatov
2018-07-11 4:53 ` Jakub Kicinski
2018-07-10 21:43 ` [PATCH bpf-next v3 13/13] tools: bpftool: allow reuse of maps with bpftool prog load Jakub Kicinski
2018-07-11 20:18 ` [PATCH bpf-next v3 00/13] tools: bpf: extend " Daniel Borkmann
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=20180711030154.GA52033@rdna-mbp \
--to=rdna@fb.com \
--cc=alexei.starovoitov@gmail.com \
--cc=daniel@iogearbox.net \
--cc=jakub.kicinski@netronome.com \
--cc=netdev@vger.kernel.org \
--cc=oss-drivers@netronome.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.