From: Martin Lau <kafai@fb.com>
To: Andrii Nakryiko <andriin@fb.com>
Cc: "bpf@vger.kernel.org" <bpf@vger.kernel.org>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
Alexei Starovoitov <ast@fb.com>,
"daniel@iogearbox.net" <daniel@iogearbox.net>,
"andrii.nakryiko@gmail.com" <andrii.nakryiko@gmail.com>,
Kernel Team <Kernel-team@fb.com>
Subject: Re: [Potential Spoof] [PATCH bpf-next 06/15] libbpf: expose BPF program's function name
Date: Wed, 11 Dec 2019 19:38:11 +0000 [thread overview]
Message-ID: <20191211193807.raz42oiqmrm763tr@kafai-mbp> (raw)
In-Reply-To: <20191210011438.4182911-7-andriin@fb.com>
On Mon, Dec 09, 2019 at 05:14:29PM -0800, Andrii Nakryiko wrote:
> Add APIs to get BPF program function name, as opposed to bpf_program__title(),
> which returns BPF program function's section name. Function name has a benefit
> of being a valid C identifier and uniquely identifies a specific BPF program,
> while section name can be duplicated across multiple independent BPF programs.
>
> Add also bpf_object__find_program_by_name(), similar to
> bpf_object__find_program_by_title(), to facilitate looking up BPF programs by
> their C function names.
>
> Convert one of selftests to new API for look up.
>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> ---
> tools/lib/bpf/libbpf.c | 28 +++++++++++++++----
> tools/lib/bpf/libbpf.h | 9 ++++--
> tools/lib/bpf/libbpf.map | 2 ++
> .../selftests/bpf/prog_tests/rdonly_maps.c | 11 +++-----
> 4 files changed, 36 insertions(+), 14 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index edfe1cf1e940..f13752c4d271 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -209,8 +209,8 @@ static const char * const libbpf_type_to_btf_name[] = {
> };
>
> struct bpf_map {
> - int fd;
> char *name;
> + int fd;
This change, and
> int sec_idx;
> size_t sec_offset;
> int map_ifindex;
> @@ -1384,7 +1384,7 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
> }
>
> static int bpf_object__init_maps(struct bpf_object *obj,
> - struct bpf_object_open_opts *opts)
> + const struct bpf_object_open_opts *opts)
here, and a few other const changes,
are all good changes. If they are not in a separate patch, it will be useful
to the reviewer if there is commit messages mentioning there are some
unrelated cleanup changes. I have been looking at where it may cause
compiler warning because of this change, or I missed something?
> {
> const char *pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
> bool strict = !OPTS_GET(opts, relaxed_maps, false);
> @@ -1748,6 +1748,19 @@ bpf_object__find_program_by_title(const struct bpf_object *obj,
> return NULL;
> }
>
> +struct bpf_program *
> +bpf_object__find_program_by_name(const struct bpf_object *obj,
> + const char *name)
> +{
> + struct bpf_program *prog;
> +
> + bpf_object__for_each_program(prog, obj) {
> + if (!strcmp(prog->name, name))
> + return prog;
> + }
> + return NULL;
> +}
> +
> static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
> int shndx)
> {
> @@ -3893,7 +3906,7 @@ static int libbpf_find_attach_btf_id(const char *name,
> __u32 attach_prog_fd);
> static struct bpf_object *
> __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
> - struct bpf_object_open_opts *opts)
> + const struct bpf_object_open_opts *opts)
> {
> struct bpf_program *prog;
> struct bpf_object *obj;
> @@ -4002,7 +4015,7 @@ struct bpf_object *bpf_object__open(const char *path)
> }
>
> struct bpf_object *
> -bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts)
> +bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
> {
> if (!path)
> return ERR_PTR(-EINVAL);
> @@ -4014,7 +4027,7 @@ bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts)
>
> struct bpf_object *
> bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
> - struct bpf_object_open_opts *opts)
> + const struct bpf_object_open_opts *opts)
> {
> if (!obj_buf || obj_buf_sz == 0)
> return ERR_PTR(-EINVAL);
> @@ -4819,6 +4832,11 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
> prog->prog_ifindex = ifindex;
> }
>
> +const char *bpf_program__name(const struct bpf_program *prog)
> +{
> + return prog->name;
> +}
> +
> const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
> {
> const char *title;
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index fa803dde1f46..7fa583ebe56f 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -114,10 +114,10 @@ struct bpf_object_open_opts {
>
> LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
> LIBBPF_API struct bpf_object *
> -bpf_object__open_file(const char *path, struct bpf_object_open_opts *opts);
> +bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts);
> LIBBPF_API struct bpf_object *
> bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
> - struct bpf_object_open_opts *opts);
> + const struct bpf_object_open_opts *opts);
>
> /* deprecated bpf_object__open variants */
> LIBBPF_API struct bpf_object *
> @@ -156,6 +156,7 @@ struct bpf_object_load_attr {
> LIBBPF_API int bpf_object__load(struct bpf_object *obj);
> LIBBPF_API int bpf_object__load_xattr(struct bpf_object_load_attr *attr);
> LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
> +
> LIBBPF_API const char *bpf_object__name(const struct bpf_object *obj);
> LIBBPF_API unsigned int bpf_object__kversion(const struct bpf_object *obj);
>
> @@ -166,6 +167,9 @@ LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
> LIBBPF_API struct bpf_program *
> bpf_object__find_program_by_title(const struct bpf_object *obj,
> const char *title);
> +LIBBPF_API struct bpf_program *
> +bpf_object__find_program_by_name(const struct bpf_object *obj,
> + const char *name);
>
> LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
> #define bpf_object__for_each_safe(pos, tmp) \
> @@ -209,6 +213,7 @@ LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
> LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
> __u32 ifindex);
>
> +LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
> LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,
> bool needs_copy);
>
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index 757a88f64b5a..f2b2fa0f5c2a 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -211,5 +211,7 @@ LIBBPF_0.0.6 {
>
> LIBBPF_0.0.7 {
> global:
> + bpf_object__find_program_by_name;
> bpf_program__attach;
> + bpf_program__name;
> } LIBBPF_0.0.6;
> diff --git a/tools/testing/selftests/bpf/prog_tests/rdonly_maps.c b/tools/testing/selftests/bpf/prog_tests/rdonly_maps.c
> index d90acc13d1ec..563e12120e77 100644
> --- a/tools/testing/selftests/bpf/prog_tests/rdonly_maps.c
> +++ b/tools/testing/selftests/bpf/prog_tests/rdonly_maps.c
> @@ -16,14 +16,11 @@ struct rdonly_map_subtest {
>
> void test_rdonly_maps(void)
> {
> - const char *prog_name_skip_loop = "raw_tracepoint/sys_enter:skip_loop";
> - const char *prog_name_part_loop = "raw_tracepoint/sys_enter:part_loop";
> - const char *prog_name_full_loop = "raw_tracepoint/sys_enter:full_loop";
> const char *file = "test_rdonly_maps.o";
> struct rdonly_map_subtest subtests[] = {
> - { "skip loop", prog_name_skip_loop, 0, 0 },
> - { "part loop", prog_name_part_loop, 3, 2 + 3 + 4 },
> - { "full loop", prog_name_full_loop, 4, 2 + 3 + 4 + 5 },
> + { "skip loop", "skip_loop", 0, 0 },
> + { "part loop", "part_loop", 3, 2 + 3 + 4 },
> + { "full loop", "full_loop", 4, 2 + 3 + 4 + 5 },
> };
> int i, err, zero = 0, duration = 0;
> struct bpf_link *link = NULL;
> @@ -50,7 +47,7 @@ void test_rdonly_maps(void)
> if (!test__start_subtest(t->subtest_name))
> continue;
>
> - prog = bpf_object__find_program_by_title(obj, t->prog_name);
> + prog = bpf_object__find_program_by_name(obj, t->prog_name);
> if (CHECK(!prog, "find_prog", "prog '%s' not found\n",
> t->prog_name))
> goto cleanup;
> --
> 2.17.1
>
next prev parent reply other threads:[~2019-12-11 19:38 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-10 1:14 [PATCH bpf-next 00/15] Add code-generated BPF object skeleton support Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 01/15] libbpf: don't require root for bpf_object__open() Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 02/15] libbpf: add generic bpf_program__attach() Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 03/15] libbpf: move non-public APIs from libbpf.h to libbpf_internal.h Andrii Nakryiko
2019-12-10 1:33 ` Jakub Kicinski
2019-12-10 17:04 ` Andrii Nakryiko
2019-12-10 18:17 ` Jakub Kicinski
2019-12-10 18:47 ` Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 04/15] libbpf: add BPF_EMBED_OBJ macro for embedding BPF .o files Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 05/15] libbpf: expose field/var declaration emitting API internally Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 06/15] libbpf: expose BPF program's function name Andrii Nakryiko
2019-12-11 19:38 ` Martin Lau [this message]
2019-12-11 19:54 ` [Potential Spoof] " Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 07/15] libbpf: refactor global data map initialization Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 08/15] libbpf: postpone BTF ID finding for TRACING programs to load phase Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 09/15] libbpf: reduce log level of supported section names dump Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 10/15] libbpf: add experimental BPF object skeleton support Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 11/15] bpftool: add skeleton codegen command Andrii Nakryiko
2019-12-10 1:57 ` Jakub Kicinski
2019-12-10 17:11 ` Andrii Nakryiko
2019-12-10 18:05 ` Jakub Kicinski
2019-12-10 18:56 ` Andrii Nakryiko
2019-12-10 21:44 ` Stanislav Fomichev
2019-12-10 22:33 ` Andrii Nakryiko
2019-12-10 22:59 ` Stanislav Fomichev
2019-12-11 7:07 ` Andrii Nakryiko
2019-12-11 17:24 ` Stanislav Fomichev
2019-12-11 18:26 ` Andrii Nakryiko
2019-12-11 19:15 ` Stanislav Fomichev
2019-12-11 19:41 ` Andrii Nakryiko
2019-12-11 20:09 ` Stanislav Fomichev
2019-12-12 0:50 ` Andrii Nakryiko
2019-12-12 2:57 ` Stanislav Fomichev
2019-12-12 7:27 ` Andrii Nakryiko
2019-12-12 16:29 ` Stanislav Fomichev
2019-12-12 16:53 ` Andrii Nakryiko
2019-12-12 18:43 ` Jakub Kicinski
2019-12-12 18:58 ` Stanislav Fomichev
2019-12-12 19:23 ` Jakub Kicinski
2019-12-12 19:54 ` Alexei Starovoitov
2019-12-12 20:21 ` Jakub Kicinski
2019-12-12 21:28 ` Alexei Starovoitov
2019-12-12 21:59 ` Jakub Kicinski
2019-12-13 6:48 ` Andrii Nakryiko
2019-12-13 17:47 ` Jakub Kicinski
2019-12-12 21:45 ` Stanislav Fomichev
2019-12-13 6:23 ` Andrii Nakryiko
2019-12-11 22:50 ` [Potential Spoof] " Martin Lau
2019-12-16 14:16 ` Daniel Borkmann
2019-12-16 18:53 ` Andrii Nakryiko
2019-12-17 13:59 ` Daniel Borkmann
2019-12-17 15:45 ` Alexei Starovoitov
2019-12-10 1:14 ` [PATCH bpf-next 12/15] selftests/bpf: add BPF skeletons selftests and convert attach_probe.c Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 13/15] selftests/bpf: convert few more selftest to skeletons Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 14/15] selftests/bpf: add test validating data section to struct convertion layout Andrii Nakryiko
2019-12-10 1:14 ` [PATCH bpf-next 15/15] bpftool: add `gen skeleton` BASH completions Andrii Nakryiko
2019-12-11 22:55 ` [PATCH bpf-next 00/15] Add code-generated BPF object skeleton support Martin Lau
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=20191211193807.raz42oiqmrm763tr@kafai-mbp \
--to=kafai@fb.com \
--cc=Kernel-team@fb.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andriin@fb.com \
--cc=ast@fb.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
/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