netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Toke Høiland-Jørgensen" <toke@redhat.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>, Andrii Nakryiko <andriin@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	Jiri Olsa <jolsa@redhat.com>,
	Eelco Chaudron <echaudro@redhat.com>,
	KP Singh <kpsingh@chromium.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v5 6/8] libbpf: add support for freplace attachment in bpf_link_create
Date: Wed, 16 Sep 2020 23:21:11 +0200	[thread overview]
Message-ID: <87h7rxpge0.fsf@toke.dk> (raw)
In-Reply-To: <CAEf4Bzb5pLJaW_Rkiq+5QacH6G-FFmj6eRBiZKybYCkkBVMzLA@mail.gmail.com>

Andrii Nakryiko <andrii.nakryiko@gmail.com> writes:

> On Wed, Sep 16, 2020 at 1:37 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
>>
>> On Tue, Sep 15, 2020 at 5:50 PM Toke Høiland-Jørgensen <toke@redhat.com> wrote:
>> >
>> > From: Toke Høiland-Jørgensen <toke@redhat.com>
>> >
>> > This adds support for supplying a target btf ID for the bpf_link_create()
>> > operation, and adds a new bpf_program__attach_freplace() high-level API for
>> > attaching freplace functions with a target.
>> >
>> > Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
>> > ---
>> >  tools/lib/bpf/bpf.c      |    1 +
>> >  tools/lib/bpf/bpf.h      |    3 ++-
>> >  tools/lib/bpf/libbpf.c   |   24 ++++++++++++++++++------
>> >  tools/lib/bpf/libbpf.h   |    3 +++
>> >  tools/lib/bpf/libbpf.map |    1 +
>> >  5 files changed, 25 insertions(+), 7 deletions(-)
>> >
>> > diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
>> > index 82b983ff6569..e928456c0dd6 100644
>> > --- a/tools/lib/bpf/bpf.c
>> > +++ b/tools/lib/bpf/bpf.c
>> > @@ -599,6 +599,7 @@ int bpf_link_create(int prog_fd, int target_fd,
>> >         attr.link_create.iter_info =
>> >                 ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
>> >         attr.link_create.iter_info_len = OPTS_GET(opts, iter_info_len, 0);
>> > +       attr.link_create.target_btf_id = OPTS_GET(opts, target_btf_id, 0);
>> >
>> >         return sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
>> >  }
>> > diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
>> > index 015d13f25fcc..f8dbf666b62b 100644
>> > --- a/tools/lib/bpf/bpf.h
>> > +++ b/tools/lib/bpf/bpf.h
>> > @@ -174,8 +174,9 @@ struct bpf_link_create_opts {
>> >         __u32 flags;
>> >         union bpf_iter_link_info *iter_info;
>> >         __u32 iter_info_len;
>> > +       __u32 target_btf_id;
>> >  };
>> > -#define bpf_link_create_opts__last_field iter_info_len
>> > +#define bpf_link_create_opts__last_field target_btf_id
>> >
>> >  LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
>> >                                enum bpf_attach_type attach_type,
>> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> > index 550950eb1860..165131c73f40 100644
>> > --- a/tools/lib/bpf/libbpf.c
>> > +++ b/tools/lib/bpf/libbpf.c
>> > @@ -9322,12 +9322,14 @@ static struct bpf_link *attach_iter(const struct bpf_sec_def *sec,
>> >
>> >  static struct bpf_link *
>> >  bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
>> > -                      const char *target_name)
>> > +                      int target_btf_id, const char *target_name)
>> >  {
>> >         enum bpf_attach_type attach_type;
>> >         char errmsg[STRERR_BUFSIZE];
>> >         struct bpf_link *link;
>> >         int prog_fd, link_fd;
>> > +       DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
>> > +                           .target_btf_id = target_btf_id);
>> >
>> >         prog_fd = bpf_program__fd(prog);
>> >         if (prog_fd < 0) {
>> > @@ -9340,8 +9342,12 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
>> >                 return ERR_PTR(-ENOMEM);
>> >         link->detach = &bpf_link__detach_fd;
>> >
>> > -       attach_type = bpf_program__get_expected_attach_type(prog);
>> > -       link_fd = bpf_link_create(prog_fd, target_fd, attach_type, NULL);
>> > +       if (bpf_program__get_type(prog) == BPF_PROG_TYPE_EXT)
>> > +               attach_type = BPF_TRACE_FREPLACE;
>>
>> doing this unconditionally will break an old-style freplace without
>> target_fd/btf_id on older kernels. Safe and simple way would be to
>> continue using raw_tracepoint_open when there is no target_fd/btf_id,
>> and use LINK_CREATE for newer stuff. Alternatively, you'd need to do
>> feature detection, but it's still would be nice to handle old-style
>> attach through raw_tracepoint_open for bpf_program__attach_freplace.
>>
>> so I suggest leaving bpf_program__attach_fd() as is and to create a
>> custom bpf_program__attach_freplace() implementation.

Sure, I'll take another pass at this. Not sure how useful feature
detection in libbpf is; if the caller passes a target, libbpf can't
really do much if the kernel doesn't support it...

>> > +       else
>> > +               attach_type = bpf_program__get_expected_attach_type(prog);
>> > +
>> > +       link_fd = bpf_link_create(prog_fd, target_fd, attach_type, &opts);
>> >         if (link_fd < 0) {
>> >                 link_fd = -errno;
>> >                 free(link);
>> > @@ -9357,19 +9363,25 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd,
>> >  struct bpf_link *
>> >  bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd)
>> >  {
>> > -       return bpf_program__attach_fd(prog, cgroup_fd, "cgroup");
>> > +       return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
>> >  }
>> >
>> >  struct bpf_link *
>> >  bpf_program__attach_netns(struct bpf_program *prog, int netns_fd)
>> >  {
>> > -       return bpf_program__attach_fd(prog, netns_fd, "netns");
>> > +       return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
>> >  }
>> >
>> >  struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex)
>> >  {
>> >         /* target_fd/target_ifindex use the same field in LINK_CREATE */
>> > -       return bpf_program__attach_fd(prog, ifindex, "xdp");
>> > +       return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
>> > +}
>> > +
>> > +struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog,
>> > +                                             int target_fd, int target_btf_id)
>> > +{
>> > +       return bpf_program__attach_fd(prog, target_fd, target_btf_id, "freplace");
>> >  }
>> >
>> >  struct bpf_link *
>> > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
>> > index a750f67a23f6..ce5add9b9203 100644
>> > --- a/tools/lib/bpf/libbpf.h
>> > +++ b/tools/lib/bpf/libbpf.h
>> > @@ -261,6 +261,9 @@ LIBBPF_API struct bpf_link *
>> >  bpf_program__attach_netns(struct bpf_program *prog, int netns_fd);
>> >  LIBBPF_API struct bpf_link *
>> >  bpf_program__attach_xdp(struct bpf_program *prog, int ifindex);
>> > +LIBBPF_API struct bpf_link *
>> > +bpf_program__attach_freplace(struct bpf_program *prog,
>> > +                            int target_fd, int target_btf_id);
>>
>> maybe a const char * function name instead of target_btf_id would be a
>> nicer API? Users won't have to deal with fetching target prog's BTF,
>> searching it, etc. That's all pretty straightforward for libbpf to do,
>> leaving users with more natural and simpler API.
>>
>
> bpf_program__set_attach_target() uses string name for target
> functions, we should definitely be consistent here

All right, fair enough :)

-Toke


  reply	other threads:[~2020-09-16 22:21 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-15 11:40 [PATCH bpf-next v5 0/8] bpf: Support multi-attach for freplace programs Toke Høiland-Jørgensen
2020-09-15 11:40 ` [PATCH bpf-next v5 1/8] bpf: change logging calls from verbose() to bpf_log() and use log pointer Toke Høiland-Jørgensen
2020-09-16 17:08   ` Andrii Nakryiko
2020-09-15 11:40 ` [PATCH bpf-next v5 2/8] bpf: verifier: refactor check_attach_btf_id() Toke Høiland-Jørgensen
2020-09-16 17:32   ` Andrii Nakryiko
2020-09-16 21:07     ` Toke Høiland-Jørgensen
2020-09-17 10:06     ` Toke Høiland-Jørgensen
2020-09-17 16:38       ` Andrii Nakryiko
2020-09-17 16:54         ` Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 3/8] bpf: move prog->aux->linked_prog and trampoline into bpf_link on attach Toke Høiland-Jørgensen
2020-09-16 18:22   ` Andrii Nakryiko
2020-09-15 11:41 ` [PATCH bpf-next v5 4/8] bpf: support attaching freplace programs to multiple attach points Toke Høiland-Jørgensen
2020-09-16 19:49   ` Andrii Nakryiko
2020-09-16 21:13     ` Toke Høiland-Jørgensen
2020-09-16 21:17       ` Andrii Nakryiko
2020-09-16 21:27         ` Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 5/8] bpf: Fix context type resolving for extension programs Toke Høiland-Jørgensen
2020-09-16 19:59   ` Andrii Nakryiko
2020-09-16 20:28     ` Andrii Nakryiko
2020-09-16 21:15       ` Toke Høiland-Jørgensen
2020-09-17 17:10       ` Toke Høiland-Jørgensen
2020-09-17 18:06         ` Andrii Nakryiko
2020-09-17 18:44           ` Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 6/8] libbpf: add support for freplace attachment in bpf_link_create Toke Høiland-Jørgensen
2020-09-16 20:37   ` Andrii Nakryiko
2020-09-16 20:45     ` Andrii Nakryiko
2020-09-16 21:21       ` Toke Høiland-Jørgensen [this message]
2020-09-16 21:24         ` Andrii Nakryiko
2020-09-16 21:41           ` Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 7/8] selftests: add test for multiple attachments of freplace program Toke Høiland-Jørgensen
2020-09-15 11:41 ` [PATCH bpf-next v5 8/8] selftests/bpf: Adding test for arg dereference in extension trace Toke Høiland-Jørgensen
2020-09-16 20:44   ` Andrii Nakryiko

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=87h7rxpge0.fsf@toke.dk \
    --to=toke@redhat.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=echaudro@redhat.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@chromium.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.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).