Skip to content

Commit f9e6bfd

Browse files
netoptimizerAlexei Starovoitov
authored andcommitted
samples/bpf: Xdp_redirect_cpu fix missing tracepoint attach
When sample xdp_redirect_cpu was converted to use libbpf, the tracepoints used by this sample were not getting attached automatically like with bpf_load.c. The BPF-maps was still getting loaded, thus nobody notice that the tracepoints were not updating these maps. This fix doesn't use the new skeleton code, as this bug was introduced in v5.1 and stable might want to backport this. E.g. Red Hat QA uses this sample as part of their testing. Fixes: bbaf602 ("samples/bpf: Convert XDP samples to libbpf usage") Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/157685877642.26195.2798780195186786841.stgit@firesoul
1 parent 6cd6e64 commit f9e6bfd

1 file changed

Lines changed: 55 additions & 4 deletions

File tree

‎samples/bpf/xdp_redirect_cpu_user.c‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ static const char *__doc__ =
1616
#include <getopt.h>
1717
#include <net/if.h>
1818
#include <time.h>
19+
#include <linux/limits.h>
20+
21+
#define __must_check
22+
#include <linux/err.h>
1923

2024
#include <arpa/inet.h>
2125
#include <linux/if_link.h>
@@ -46,6 +50,10 @@ static int cpus_count_map_fd;
4650
static int cpus_iterator_map_fd;
4751
static int exception_cnt_map_fd;
4852

53+
#define NUM_TP 5
54+
struct bpf_link *tp_links[NUM_TP] = { 0 };
55+
static int tp_cnt = 0;
56+
4957
/* Exit return codes */
5058
#define EXIT_OK 0
5159
#define EXIT_FAIL 1
@@ -88,6 +96,10 @@ static void int_exit(int sig)
8896
printf("program on interface changed, not removing\n");
8997
}
9098
}
99+
/* Detach tracepoints */
100+
while (tp_cnt)
101+
bpf_link__destroy(tp_links[--tp_cnt]);
102+
91103
exit(EXIT_OK);
92104
}
93105

@@ -588,23 +600,61 @@ static void stats_poll(int interval, bool use_separators, char *prog_name,
588600
free_stats_record(prev);
589601
}
590602

603+
static struct bpf_link * attach_tp(struct bpf_object *obj,
604+
const char *tp_category,
605+
const char* tp_name)
606+
{
607+
struct bpf_program *prog;
608+
struct bpf_link *link;
609+
char sec_name[PATH_MAX];
610+
int len;
611+
612+
len = snprintf(sec_name, PATH_MAX, "tracepoint/%s/%s",
613+
tp_category, tp_name);
614+
if (len < 0)
615+
exit(EXIT_FAIL);
616+
617+
prog = bpf_object__find_program_by_title(obj, sec_name);
618+
if (!prog) {
619+
fprintf(stderr, "ERR: finding progsec: %s\n", sec_name);
620+
exit(EXIT_FAIL_BPF);
621+
}
622+
623+
link = bpf_program__attach_tracepoint(prog, tp_category, tp_name);
624+
if (IS_ERR(link))
625+
exit(EXIT_FAIL_BPF);
626+
627+
return link;
628+
}
629+
630+
static void init_tracepoints(struct bpf_object *obj) {
631+
tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_redirect_err");
632+
tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_redirect_map_err");
633+
tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_exception");
634+
tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_cpumap_enqueue");
635+
tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_cpumap_kthread");
636+
}
637+
591638
static int init_map_fds(struct bpf_object *obj)
592639
{
593-
cpu_map_fd = bpf_object__find_map_fd_by_name(obj, "cpu_map");
594-
rx_cnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rx_cnt");
640+
/* Maps updated by tracepoints */
595641
redirect_err_cnt_map_fd =
596642
bpf_object__find_map_fd_by_name(obj, "redirect_err_cnt");
643+
exception_cnt_map_fd =
644+
bpf_object__find_map_fd_by_name(obj, "exception_cnt");
597645
cpumap_enqueue_cnt_map_fd =
598646
bpf_object__find_map_fd_by_name(obj, "cpumap_enqueue_cnt");
599647
cpumap_kthread_cnt_map_fd =
600648
bpf_object__find_map_fd_by_name(obj, "cpumap_kthread_cnt");
649+
650+
/* Maps used by XDP */
651+
rx_cnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rx_cnt");
652+
cpu_map_fd = bpf_object__find_map_fd_by_name(obj, "cpu_map");
601653
cpus_available_map_fd =
602654
bpf_object__find_map_fd_by_name(obj, "cpus_available");
603655
cpus_count_map_fd = bpf_object__find_map_fd_by_name(obj, "cpus_count");
604656
cpus_iterator_map_fd =
605657
bpf_object__find_map_fd_by_name(obj, "cpus_iterator");
606-
exception_cnt_map_fd =
607-
bpf_object__find_map_fd_by_name(obj, "exception_cnt");
608658

609659
if (cpu_map_fd < 0 || rx_cnt_map_fd < 0 ||
610660
redirect_err_cnt_map_fd < 0 || cpumap_enqueue_cnt_map_fd < 0 ||
@@ -662,6 +712,7 @@ int main(int argc, char **argv)
662712
strerror(errno));
663713
return EXIT_FAIL;
664714
}
715+
init_tracepoints(obj);
665716
if (init_map_fds(obj) < 0) {
666717
fprintf(stderr, "bpf_object__find_map_fd_by_name failed\n");
667718
return EXIT_FAIL;

0 commit comments

Comments
 (0)