From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Song Liu <songliubraving@fb.com>
Cc: "Eelco Chaudron" <echaudro@redhat.com>, bpf <bpf@vger.kernel.org>,
"David S. Miller" <davem@davemloft.net>,
Networking <netdev@vger.kernel.org>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Martin Lau" <kafai@fb.com>, "Yonghong Song" <yhs@fb.com>,
"Andrii Nakryiko" <andriin@fb.com>,
"Toke Høiland-Jørgensen" <toke@redhat.com>
Subject: Re: [PATCH bpf-next] libbpf: Add support for dynamic program attach target
Date: Wed, 12 Feb 2020 10:14:30 -0800 [thread overview]
Message-ID: <CAEf4BzYFVtgW4Zyz09vuppAJA3oQ-UAT4yALeFJk2JQ70+mE2g@mail.gmail.com> (raw)
In-Reply-To: <628E972C-1156-46F8-AC61-DB0D38C34C81@fb.com>
On Wed, Feb 12, 2020 at 10:07 AM Song Liu <songliubraving@fb.com> wrote:
>
>
>
> > On Feb 12, 2020, at 9:34 AM, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
> >
> > On Wed, Feb 12, 2020 at 4:32 AM Eelco Chaudron <echaudro@redhat.com> wrote:
> >>
> >> Currently when you want to attach a trace program to a bpf program
> >> the section name needs to match the tracepoint/function semantics.
> >>
> >> However the addition of the bpf_program__set_attach_target() API
> >> allows you to specify the tracepoint/function dynamically.
> >>
> >> The call flow would look something like this:
> >>
> >> xdp_fd = bpf_prog_get_fd_by_id(id);
> >> trace_obj = bpf_object__open_file("func.o", NULL);
> >> prog = bpf_object__find_program_by_title(trace_obj,
> >> "fentry/myfunc");
> >> bpf_program__set_attach_target(prog, xdp_fd,
> >> "fentry/xdpfilt_blk_all");
> >> bpf_object__load(trace_obj)
> >>
> >> Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
>
>
> I am trying to solve the same problem with slightly different approach.
>
> It works as the following (with skeleton):
>
> obj = myobject_bpf__open_opts(&opts);
> bpf_object__for_each_program(prog, obj->obj)
> bpf_program__overwrite_section_name(prog, new_names[id++]);
> err = myobject_bpf__load(obj);
>
> I don't have very strong preference. But I think my approach is simpler?
I prefer bpf_program__set_attach_target() approach. Section name is a
program identifier and a *hint* for libbpf to determine program type,
attach type, and whatever else makes sense. But there still should be
an API to set all that manually at runtime, thus
bpf_program__set_attach_target(). Doing same by overriding section
name feels like a hack, plus it doesn't handle overriding
attach_program_fd at all.
>
> Thanks,
> Song
>
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 514b1a524abb..4c29a7181d57 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -238,6 +238,8 @@ struct bpf_program {
> __u32 line_info_rec_size;
> __u32 line_info_cnt;
> __u32 prog_flags;
> +
> + char *overwritten_section_name;
> };
>
> struct bpf_struct_ops {
> @@ -442,6 +444,7 @@ static void bpf_program__exit(struct bpf_program *prog)
> zfree(&prog->pin_name);
> zfree(&prog->insns);
> zfree(&prog->reloc_desc);
> + zfree(&prog->overwritten_section_name);
>
> prog->nr_reloc = 0;
> prog->insns_cnt = 0;
> @@ -6637,7 +6640,7 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog)
> {
> enum bpf_attach_type attach_type = prog->expected_attach_type;
> __u32 attach_prog_fd = prog->attach_prog_fd;
> - const char *name = prog->section_name;
> + const char *name = prog->overwritten_section_name ? : prog->section_name;
> int i, err;
>
> if (!name)
> @@ -8396,3 +8399,11 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
> free(s->progs);
> free(s);
> }
> +
> +char *bpf_program__overwrite_section_name(struct bpf_program *prog,
> + const char *sec_name)
> +{
> + prog->overwritten_section_name = strdup(sec_name);
> +
> + return prog->overwritten_section_name;
> +}
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 3fe12c9d1f92..02f0d8b57cc4 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -595,6 +595,10 @@ bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
> LIBBPF_API void
> bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
>
> +LIBBPF_API char *
> +bpf_program__overwrite_section_name(struct bpf_program *prog,
> + const char *sec_name);
> +
> /*
> * A helper function to get the number of possible CPUs before looking up
> * per-CPU maps. Negative errno is returned on failure.
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index b035122142bb..ed26c20729db 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -235,3 +235,8 @@ LIBBPF_0.0.7 {
> btf__align_of;
> libbpf_find_kernel_btf;
> } LIBBPF_0.0.6;
> +
> +LIBBPF_0.0.8 {
> + global:
> + bpf_program__overwrite_section_name;
> +} LIBBPF_0.0.7;
next prev parent reply other threads:[~2020-02-12 18:14 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-12 12:31 [PATCH bpf-next] libbpf: Add support for dynamic program attach target Eelco Chaudron
2020-02-12 13:05 ` Toke Høiland-Jørgensen
2020-02-12 17:35 ` Andrii Nakryiko
2020-02-12 21:52 ` Toke Høiland-Jørgensen
2020-02-13 14:41 ` Eelco Chaudron
2020-02-12 17:34 ` Andrii Nakryiko
2020-02-12 17:56 ` Song Liu
2020-02-12 18:14 ` Andrii Nakryiko [this message]
2020-02-12 18:28 ` Song Liu
2020-02-12 18:34 ` Andrii Nakryiko
2020-02-12 18:40 ` Song Liu
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=CAEf4BzYFVtgW4Zyz09vuppAJA3oQ-UAT4yALeFJk2JQ70+mE2g@mail.gmail.com \
--to=andrii.nakryiko@gmail.com \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=echaudro@redhat.com \
--cc=kafai@fb.com \
--cc=netdev@vger.kernel.org \
--cc=songliubraving@fb.com \
--cc=toke@redhat.com \
--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).