On Sat, Oct 10, 2020 at 3:23 AM Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote: > > On Fri, Oct 9, 2020 at 9:04 AM Daniel T. Lee <danieltimlee@xxxxxxxxx> 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@xxxxxxxxx> > > --- > > 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++; > > } > > > > [...]