bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel Xu" <dxu@dxuuu.xyz>
To: "Andrii Nakryiko" <andrii.nakryiko@gmail.com>
Cc: "Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"Andrii Nakryiko" <andriin@fb.com>,
	"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
	"Kernel Team" <kernel-team@fb.com>
Subject: Re: [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link
Date: Thu, 08 Aug 2019 18:31:27 -0700	[thread overview]
Message-ID: <e8b7930a-ce08-4c05-a407-b7a92a89ded9@www.fastmail.com> (raw)
In-Reply-To: <CAEf4BzaQZrEuqqGhFrf1cDiWiUXYDy6x8zAMXayry6H2ow78Og@mail.gmail.com>

On Wed, Aug 7, 2019, at 11:57 AM, Andrii Nakryiko wrote:
> On Tue, Aug 6, 2019 at 4:42 PM Daniel Xu <dxu@dxuuu.xyz> wrote:
> >
> > It is sometimes necessary to perform ioctl's on the underlying perf fd.
> > There is not currently a way to extract the fd given a bpf_link, so add a
> > helper for it.
> > ---
> 
> So I've been going back and forth with this approach and the
> alternative one, and I think I'm leaning towards the alternative one
> still.
> 
> I think it's better to have a broad "categories" of bpf_links, e.g.:
> 
> - FD-based bpf_link (which is the only one we have right now):
> bpf_link_fd. It's not just for perf FD-based ones, raw tracepoint is
> not, but it's still FD-based;
> - for cgroup-related links (once they are added), it will be
> bpf_link_cg (or something along the lines);
> - there probably should be separate XDP-related bpf_link with device
> ID/name inside;
> - etc, whatever we'll need.
> 
> Then we can have a set of casting APIs and getter APIs that extract
> useful information from specific type of bpf_link. We can also add
> direct bpf_link creation API (e.g., from known FD), for cases where it
> makes sense.
> 
> So something like (in libbpf.h):
> 
> struct bpf_link_fd;
> struct bpf_link_cg;
> 
> /* casting APIs */
> const struct bpf_link_fd *bpf_link__as_fd(const struct bpf_link *link);
> const struct bpf_link_cg *bpf_link__as_cg(const struct bpf_link *link);
> 
> /* getters APIs */
> int bpf_link_fd__fd(const struct bpf_link_fd *link);
> int bpf_link_cg__cgroup_fd(const struct bpf_link_cg *link);
> 
> /* link factories (in addition to attach APIs) */
> const struct bpf_link_fd *bpf_link__from_fd(int fd);
> const struct bpf_link_cg *bpf_link__from_cg(int cg_fd, /* whatever
> else necessary */);
> 
> I think this way it becomes obvious what you can expect to get of each
> possible type of bpf_link and you'll have to explicitly cast to the
> right type. Yet we still hide implementation details, allow no-brainer
> bpf_link__destroy regardless of specific type of link (which probably
> will be a common case).
> 
> Thoughts?

Makes sense to me. This would probably result in a more predictable API when
new types are added. I'll make it this way in V2.

> 
> >  tools/lib/bpf/libbpf.c   | 13 +++++++++++++
> >  tools/lib/bpf/libbpf.h   |  1 +
> >  tools/lib/bpf/libbpf.map |  5 +++++
> >  3 files changed, 19 insertions(+)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index ead915aec349..8469d69448ae 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -4004,6 +4004,19 @@ static int bpf_link__destroy_perf_event(struct bpf_link *link)
> >         return err;
> >  }
> >
> > +int bpf_link__get_perf_fd(struct bpf_link *link)
> 
> this seems like a bit too specific name (and we should avoid "get"
> words, as we do in a bunch of other libbpf APIs for getters). Maybe
> just `bpf_link__fd`? This especially makes sense with a "file-based
> bpf_link" abstraction I proposed above.

Ok.

      reply	other threads:[~2019-08-09  1:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-06 23:42 [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Daniel Xu
2019-08-06 23:42 ` [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE Daniel Xu
2019-08-07  6:01   ` Yonghong Song
2019-08-07 21:49   ` Andrii Nakryiko
2019-08-07 18:19 ` [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Song Liu
2019-08-07 18:57 ` Andrii Nakryiko
2019-08-09  1:31   ` Daniel Xu [this message]

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=e8b7930a-ce08-4c05-a407-b7a92a89ded9@www.fastmail.com \
    --to=dxu@dxuuu.xyz \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=kernel-team@fb.com \
    --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).