bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel T. Lee" <danieltimlee@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>,
	Alexei Starovoitov <ast@kernel.org>,
	Jesper Dangaard Brouer <brouer@redhat.com>,
	Lorenzo Bianconi <lorenzo@kernel.org>, bpf <bpf@vger.kernel.org>,
	Networking <netdev@vger.kernel.org>,
	Xdp <xdp-newbies@vger.kernel.org>
Subject: Re: [PATCH bpf-next 2/3] samples: bpf: Replace attach_tracepoint() to attach() in xdp_redirect_cpu
Date: Sat, 10 Oct 2020 18:58:31 +0900	[thread overview]
Message-ID: <CAEKGpzgXLbSyEuZ9x01k2vkRPFXJDTsreCqXUov7sfiL+BTE5A@mail.gmail.com> (raw)
In-Reply-To: <CAEf4Bzao-mCaQ4BnvoFZ_-wMSuoJ3JMJw1SAuy9bNRwy0E7qdg@mail.gmail.com>

On Sat, Oct 10, 2020 at 3:23 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Oct 9, 2020 at 9:04 AM Daniel T. Lee <danieltimlee@gmail.com> wrote:
> >
> > From commit d7a18ea7e8b6 ("libbpf: Add generic bpf_program__attach()"),
> > for some BPF programs, it is now possible to attach BPF programs
> > with __attach() instead of explicitly calling __attach_<type>().
> >
> > This commit refactors the __attach_tracepoint() with libbpf's generic
> > __attach() method. In addition, this refactors the logic of setting
> > the map FD to simplify the code. Also, the missing removal of
> > bpf_load.o in Makefile has been fixed.
> >
> > Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
> > ---
> >  samples/bpf/Makefile                |   2 +-
> >  samples/bpf/xdp_redirect_cpu_user.c | 138 +++++++++++++---------------
> >  2 files changed, 67 insertions(+), 73 deletions(-)
> >
>
> [...]
>
> >  #define NUM_TP 5
> > +#define NUM_MAP 9
> >  struct bpf_link *tp_links[NUM_TP] = { 0 };
>
> = {}
>
> > +static int map_fds[NUM_MAP];
> >  static int tp_cnt = 0;
> >
> >  /* Exit return codes */
>
> [...]
>
> > -static struct bpf_link * attach_tp(struct bpf_object *obj,
> > -                                  const char *tp_category,
> > -                                  const char* tp_name)
> > +static int init_tracepoints(struct bpf_object *obj)
> >  {
> > +       char *tp_section = "tracepoint/";
> >         struct bpf_program *prog;
> > -       struct bpf_link *link;
> > -       char sec_name[PATH_MAX];
> > -       int len;
> > +       const char *section;
> >
> > -       len = snprintf(sec_name, PATH_MAX, "tracepoint/%s/%s",
> > -                      tp_category, tp_name);
> > -       if (len < 0)
> > -               exit(EXIT_FAIL);
> > +       bpf_object__for_each_program(prog, obj) {
> > +               section = bpf_program__section_name(prog);
> > +               if (strncmp(section, tp_section, strlen(tp_section)) != 0)
> > +                       continue;
>
> that's a convoluted and error-prone way (you can also use "tp/bla/bla"
> for tracepoint programs, for example). Use
> bpf_program__is_tracepoint() check.
>

Thanks for the review!
I think that's a much better way. I will send the next patch with applying
that method.

> >
> > -       prog = bpf_object__find_program_by_title(obj, sec_name);
> > -       if (!prog) {
> > -               fprintf(stderr, "ERR: finding progsec: %s\n", sec_name);
> > -               exit(EXIT_FAIL_BPF);
> > +               tp_links[tp_cnt] = bpf_program__attach(prog);
> > +               if (libbpf_get_error(tp_links[tp_cnt])) {
> > +                       tp_links[tp_cnt] = NULL;
> > +                       return -EINVAL;
> > +               }
> > +               tp_cnt++;
> >         }
> >
>
> [...]

  reply	other threads:[~2020-10-10 10:37 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-09 16:03 [PATCH bpf-next 0/3] samples: bpf: Refactor XDP programs with libbpf Daniel T. Lee
2020-10-09 16:03 ` [PATCH bpf-next 1/3] samples: bpf: Refactor xdp_monitor " Daniel T. Lee
2020-10-09 18:17   ` Andrii Nakryiko
2020-10-10 10:08     ` Daniel T. Lee
2020-10-09 16:03 ` [PATCH bpf-next 2/3] samples: bpf: Replace attach_tracepoint() to attach() in xdp_redirect_cpu Daniel T. Lee
2020-10-09 18:23   ` Andrii Nakryiko
2020-10-10  9:58     ` Daniel T. Lee [this message]
2020-10-09 16:03 ` [PATCH bpf-next 3/3] samples: bpf: refactor XDP kern program maps with BTF-defined map Daniel T. Lee
2020-10-09 18:25   ` Andrii Nakryiko
2020-10-10  9:55     ` Daniel T. Lee
2020-10-09 18:29 ` [PATCH bpf-next 0/3] samples: bpf: Refactor XDP programs with libbpf Andrii Nakryiko
2020-10-10 10:41   ` Daniel T. Lee

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=CAEKGpzgXLbSyEuZ9x01k2vkRPFXJDTsreCqXUov7sfiL+BTE5A@mail.gmail.com \
    --to=danieltimlee@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=lorenzo@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=xdp-newbies@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;
as well as URLs for NNTP newsgroup(s).