All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: "Daniel T. Lee" <danieltimlee@gmail.com>
Cc: "Daniel Borkmann" <daniel@iogearbox.net>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Andrii Nakryiko" <andrii@kernel.org>, brakmo <brakmo@fb.com>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>,
	"Lorenzo Bianconi" <lorenzo@kernel.org>,
	"David Ahern" <dsa@cumulusnetworks.com>,
	"Yonghong Song" <yhs@fb.com>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Ira Weiny" <ira.weiny@intel.com>, "Thomas Graf" <tgraf@suug.ch>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Martin KaFai Lau" <kafai@fb.com>,
	"John Fastabend" <john.fastabend@gmail.com>,
	bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
	Xdp <xdp-newbies@vger.kernel.org>
Subject: Re: [PATCH bpf-next 5/9] samples: bpf: refactor ibumad program with libbpf
Date: Tue, 17 Nov 2020 19:10:46 -0800	[thread overview]
Message-ID: <CAEf4BzbOLqX-xXjjbzc_5pDKZzKH+X74oAsXgs_1PtWod=3TAA@mail.gmail.com> (raw)
In-Reply-To: <CAEKGpzisb+6nZ8AQ6nOu6tdPPZzjqAox1AzvYYd5-J3c7N9joQ@mail.gmail.com>

On Tue, Nov 17, 2020 at 7:05 PM Daniel T. Lee <danieltimlee@gmail.com> wrote:
>
> On Wed, Nov 18, 2020 at 11:52 AM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Tue, Nov 17, 2020 at 6:57 AM Daniel T. Lee <danieltimlee@gmail.com> wrote:
> > >
> > > This commit refactors the existing ibumad program with libbpf bpf
> > > loader. Attach/detach of Tracepoint bpf programs has been managed
> > > with the generic bpf_program__attach() and bpf_link__destroy() from
> > > the libbpf.
> > >
> > > Also, instead of using the previous BPF MAP definition, this commit
> > > refactors ibumad MAP definition with the new BTF-defined MAP format.
> > >
> > > To verify that this bpf program works without an infiniband device,
> > > try loading ib_umad kernel module and test the program as follows:
> > >
> > >     # modprobe ib_umad
> > >     # ./ibumad
> > >
> > > Moreover, TRACE_HELPERS has been removed from the Makefile since it is
> > > not used on this program.
> > >
> > > Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
> > > ---
> > >  samples/bpf/Makefile      |  2 +-
> > >  samples/bpf/ibumad_kern.c | 26 +++++++--------
> > >  samples/bpf/ibumad_user.c | 66 ++++++++++++++++++++++++++++++---------
> > >  3 files changed, 65 insertions(+), 29 deletions(-)
> > >
> > > diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> > > index 36b261c7afc7..bfa595379493 100644
> > > --- a/samples/bpf/Makefile
> > > +++ b/samples/bpf/Makefile
> > > @@ -109,7 +109,7 @@ xsk_fwd-objs := xsk_fwd.o
> > >  xdp_fwd-objs := xdp_fwd_user.o
> > >  task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)
> > >  xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS)
> > > -ibumad-objs := bpf_load.o ibumad_user.o $(TRACE_HELPERS)
> > > +ibumad-objs := ibumad_user.o
> > >  hbm-objs := hbm.o $(CGROUP_HELPERS) $(TRACE_HELPERS)
> > >
> > >  # Tell kbuild to always build the programs
> > > diff --git a/samples/bpf/ibumad_kern.c b/samples/bpf/ibumad_kern.c
> > > index 3a91b4c1989a..26dcd4dde946 100644
> > > --- a/samples/bpf/ibumad_kern.c
> > > +++ b/samples/bpf/ibumad_kern.c
> > > @@ -16,19 +16,19 @@
> > >  #include <bpf/bpf_helpers.h>
> > >
> > >
> > > -struct bpf_map_def SEC("maps") read_count = {
> > > -       .type        = BPF_MAP_TYPE_ARRAY,
> > > -       .key_size    = sizeof(u32), /* class; u32 required */
> > > -       .value_size  = sizeof(u64), /* count of mads read */
> > > -       .max_entries = 256, /* Room for all Classes */
> > > -};
> > > -
> > > -struct bpf_map_def SEC("maps") write_count = {
> > > -       .type        = BPF_MAP_TYPE_ARRAY,
> > > -       .key_size    = sizeof(u32), /* class; u32 required */
> > > -       .value_size  = sizeof(u64), /* count of mads written */
> > > -       .max_entries = 256, /* Room for all Classes */
> > > -};
> > > +struct {
> > > +       __uint(type, BPF_MAP_TYPE_ARRAY);
> > > +       __type(key, u32); /* class; u32 required */
> > > +       __type(value, u64); /* count of mads read */
> > > +       __uint(max_entries, 256); /* Room for all Classes */
> > > +} read_count SEC(".maps");
> > > +
> > > +struct {
> > > +       __uint(type, BPF_MAP_TYPE_ARRAY);
> > > +       __type(key, u32); /* class; u32 required */
> > > +       __type(value, u64); /* count of mads written */
> > > +       __uint(max_entries, 256); /* Room for all Classes */
> > > +} write_count SEC(".maps");
> > >
> > >  #undef DEBUG
> > >  #ifndef DEBUG
> > > diff --git a/samples/bpf/ibumad_user.c b/samples/bpf/ibumad_user.c
> > > index fa06eef31a84..66a06272f242 100644
> > > --- a/samples/bpf/ibumad_user.c
> > > +++ b/samples/bpf/ibumad_user.c
> > > @@ -23,10 +23,15 @@
> > >  #include <getopt.h>
> > >  #include <net/if.h>
> > >
> > > -#include "bpf_load.h"
> > > +#include <bpf/bpf.h>
> > >  #include "bpf_util.h"
> > >  #include <bpf/libbpf.h>
> > >
> > > +struct bpf_link *tp_links[3] = {};
> > > +struct bpf_object *obj;
> >
> > statics and you can drop = {} part.
> >
> > > +static int map_fd[2];
> > > +static int tp_cnt;
> > > +
> > >  static void dump_counts(int fd)
> > >  {
> > >         __u32 key;
> > > @@ -53,6 +58,11 @@ static void dump_all_counts(void)
> > >  static void dump_exit(int sig)
> > >  {
> > >         dump_all_counts();
> > > +       /* Detach tracepoints */
> > > +       while (tp_cnt)
> > > +               bpf_link__destroy(tp_links[--tp_cnt]);
> > > +
> > > +       bpf_object__close(obj);
> > >         exit(0);
> > >  }
> > >
> > > @@ -73,19 +83,11 @@ static void usage(char *cmd)
> > >
> > >  int main(int argc, char **argv)
> > >  {
> > > +       struct bpf_program *prog;
> > >         unsigned long delay = 5;
> > > +       char filename[256];
> > >         int longindex = 0;
> > >         int opt;
> > > -       char bpf_file[256];
> > > -
> > > -       /* Create the eBPF kernel code path name.
> > > -        * This follows the pattern of all of the other bpf samples
> > > -        */
> > > -       snprintf(bpf_file, sizeof(bpf_file), "%s_kern.o", argv[0]);
> > > -
> > > -       /* Do one final dump when exiting */
> > > -       signal(SIGINT, dump_exit);
> > > -       signal(SIGTERM, dump_exit);
> > >
> > >         while ((opt = getopt_long(argc, argv, "hd:rSw",
> > >                                   long_options, &longindex)) != -1) {
> > > @@ -107,10 +109,38 @@ int main(int argc, char **argv)
> > >                 }
> > >         }
> > >
> > > -       if (load_bpf_file(bpf_file)) {
> > > -               fprintf(stderr, "ERROR: failed to load eBPF from file : %s\n",
> > > -                       bpf_file);
> > > -               return 1;
> > > +       /* Do one final dump when exiting */
> > > +       signal(SIGINT, dump_exit);
> > > +       signal(SIGTERM, dump_exit);
> > > +
> > > +       snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
> > > +       obj = bpf_object__open_file(filename, NULL);
> > > +       if (libbpf_get_error(obj)) {
> > > +               fprintf(stderr, "ERROR: opening BPF object file failed\n");
> > > +               return 0;
> >
> > not really a success, no?
> >
> > > +       }
> > > +
> > > +       /* load BPF program */
> > > +       if (bpf_object__load(obj)) {
> > > +               fprintf(stderr, "ERROR: loading BPF object file failed\n");
> > > +               goto cleanup;
> > > +       }
> > > +
> > > +       map_fd[0] = bpf_object__find_map_fd_by_name(obj, "read_count");
> > > +       map_fd[1] = bpf_object__find_map_fd_by_name(obj, "write_count");
> > > +       if (map_fd[0] < 0 || map_fd[1] < 0) {
> > > +               fprintf(stderr, "ERROR: finding a map in obj file failed\n");
> > > +               goto cleanup;
> > > +       }
> > > +
> > > +       bpf_object__for_each_program(prog, obj) {
> > > +               tp_links[tp_cnt] = bpf_program__attach(prog);
> > > +               if (libbpf_get_error(tp_links[tp_cnt])) {
> > > +                       fprintf(stderr, "ERROR: bpf_program__attach failed\n");
> > > +                       tp_links[tp_cnt] = NULL;
> > > +                       goto cleanup;
> > > +               }
> > > +               tp_cnt++;
> > >         }
> >
> > This cries for the BPF skeleton... But one step at a time :)
> >
>
> I will make sure it'll be updated by using skeleton next time. :D
>
> > >
> > >         while (1) {
> > > @@ -118,5 +148,11 @@ int main(int argc, char **argv)
> > >                 dump_all_counts();
> > >         }
> > >
> > > +cleanup:
> > > +       /* Detach tracepoints */
> > > +       while (tp_cnt)
> > > +               bpf_link__destroy(tp_links[--tp_cnt]);
> > > +
> > > +       bpf_object__close(obj);
> > >         return 0;
> >
> > same, in a lot of cases it's not a success, probably need int err
> > variable somewhere.
> >
>
> I will correct the return code and send the next version of patch.
>
> Thanks for your time and effort for the review.

You don't have to write that every time :) It's an implicit contract:
you contribute your time to prepare, test, and submit a patch.
Maintainers and reviewers contribute theirs to review, maybe improve,
and eventually apply it. Together as a community we move the needle.

>
> --
> Best,
> Daniel T. Lee

  reply	other threads:[~2020-11-18  3:11 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-17 14:56 [PATCH bpf-next 0/9] bpf: remove bpf_load loader completely Daniel T. Lee
2020-11-17 14:56 ` [PATCH bpf-next 1/9] selftests: bpf: move tracing helpers to trace_helper Daniel T. Lee
2020-11-18  1:19   ` Martin KaFai Lau
2020-11-18  2:44     ` Daniel T. Lee
2020-11-18  1:58   ` Andrii Nakryiko
2020-11-18  2:54     ` Daniel T. Lee
2020-11-18  3:04       ` Andrii Nakryiko
2020-11-17 14:56 ` [PATCH bpf-next 2/9] samples: bpf: refactor hbm program with libbpf Daniel T. Lee
2020-11-18  2:10   ` Martin KaFai Lau
2020-11-18  9:31     ` Daniel T. Lee
2020-11-17 14:56 ` [PATCH bpf-next 3/9] samples: bpf: refactor test_cgrp2_sock2 " Daniel T. Lee
2020-11-18  3:02   ` Andrii Nakryiko
2020-11-18  3:21     ` Daniel T. Lee
2020-11-18  5:58   ` Martin KaFai Lau
2020-11-18  9:03     ` Daniel T. Lee
2020-11-17 14:56 ` [PATCH bpf-next 4/9] samples: bpf: refactor task_fd_query " Daniel T. Lee
2020-11-18  2:58   ` Andrii Nakryiko
2020-11-18  3:19     ` Daniel T. Lee
2020-11-18  6:15   ` Martin KaFai Lau
2020-11-17 14:56 ` [PATCH bpf-next 5/9] samples: bpf: refactor ibumad " Daniel T. Lee
2020-11-18  2:52   ` Andrii Nakryiko
2020-11-18  3:05     ` Daniel T. Lee
2020-11-18  3:10       ` Andrii Nakryiko [this message]
2020-11-18  5:04         ` Daniel T. Lee
2020-11-17 14:56 ` [PATCH bpf-next 6/9] samples: bpf: refactor test_overhead " Daniel T. Lee
2020-11-18  2:45   ` Andrii Nakryiko
2020-11-17 14:56 ` [PATCH bpf-next 7/9] samples: bpf: fix lwt_len_hist reusing previous BPF map Daniel T. Lee
2020-11-17 14:56 ` [PATCH bpf-next 8/9] samples: bpf: remove unused trace_helper and bpf_load from Makefile Daniel T. Lee
2020-11-17 14:56 ` [PATCH bpf-next 9/9] samples: bpf: remove bpf_load loader completely Daniel T. Lee
2020-11-17 16:12   ` Jesper Dangaard Brouer
2020-11-18  2:48   ` Andrii Nakryiko
2020-11-18  2:57     ` 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='CAEf4BzbOLqX-xXjjbzc_5pDKZzKH+X74oAsXgs_1PtWod=3TAA@mail.gmail.com' \
    --to=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brakmo@fb.com \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=danieltimlee@gmail.com \
    --cc=dsa@cumulusnetworks.com \
    --cc=ira.weiny@intel.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kuba@kernel.org \
    --cc=lorenzo@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=tgraf@suug.ch \
    --cc=toke@redhat.com \
    --cc=xdp-newbies@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.