bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] samples/bpf: xdp_redirect_cpu fix missing tracepoint attach
@ 2019-12-20 16:19 Jesper Dangaard Brouer
  2019-12-20 17:44 ` Andrii Nakryiko
  0 siblings, 1 reply; 3+ messages in thread
From: Jesper Dangaard Brouer @ 2019-12-20 16:19 UTC (permalink / raw)
  To: bpf
  Cc: Jesper Dangaard Brouer, netdev, maciejromanfijalkowski,
	Daniel Borkmann, Alexei Starovoitov, Björn Töpel,
	andrii.nakryiko

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: bbaf6029c49c ("samples/bpf: Convert XDP samples to libbpf usage")
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
---
 samples/bpf/xdp_redirect_cpu_user.c |   59 +++++++++++++++++++++++++++++++++--
 1 file changed, 55 insertions(+), 4 deletions(-)

diff --git a/samples/bpf/xdp_redirect_cpu_user.c b/samples/bpf/xdp_redirect_cpu_user.c
index 72af628529b5..79a2fb7d16cb 100644
--- a/samples/bpf/xdp_redirect_cpu_user.c
+++ b/samples/bpf/xdp_redirect_cpu_user.c
@@ -16,6 +16,10 @@ static const char *__doc__ =
 #include <getopt.h>
 #include <net/if.h>
 #include <time.h>
+#include <linux/limits.h>
+
+#define __must_check
+#include <linux/err.h>
 
 #include <arpa/inet.h>
 #include <linux/if_link.h>
@@ -46,6 +50,10 @@ static int cpus_count_map_fd;
 static int cpus_iterator_map_fd;
 static int exception_cnt_map_fd;
 
+#define NUM_TP 5
+struct bpf_link *tp_links[NUM_TP] = { 0 };
+static int tp_cnt = 0;
+
 /* Exit return codes */
 #define EXIT_OK		0
 #define EXIT_FAIL		1
@@ -88,6 +96,10 @@ static void int_exit(int sig)
 			printf("program on interface changed, not removing\n");
 		}
 	}
+	/* Detach tracepoints */
+	while (tp_cnt)
+		bpf_link__destroy(tp_links[--tp_cnt]);
+
 	exit(EXIT_OK);
 }
 
@@ -588,23 +600,61 @@ static void stats_poll(int interval, bool use_separators, char *prog_name,
 	free_stats_record(prev);
 }
 
+static struct bpf_link * attach_tp(struct bpf_object *obj,
+				   const char *tp_category,
+				   const char* tp_name)
+{
+	struct bpf_program *prog;
+	struct bpf_link *link;
+	char sec_name[PATH_MAX];
+	int len;
+
+	len = snprintf(sec_name, PATH_MAX, "tracepoint/%s/%s",
+		       tp_category, tp_name);
+	if (len < 0)
+		exit(EXIT_FAIL);
+
+	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);
+	}
+
+	link = bpf_program__attach_tracepoint(prog, tp_category, tp_name);
+	if (IS_ERR(link))
+		exit(EXIT_FAIL_BPF);
+
+	return link;
+}
+
+static void init_tracepoints(struct bpf_object *obj) {
+	tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_redirect_err");
+	tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_redirect_map_err");
+	tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_exception");
+	tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_cpumap_enqueue");
+	tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_cpumap_kthread");
+}
+
 static int init_map_fds(struct bpf_object *obj)
 {
-	cpu_map_fd = bpf_object__find_map_fd_by_name(obj, "cpu_map");
-	rx_cnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rx_cnt");
+	/* Maps updated by tracepoints */
 	redirect_err_cnt_map_fd =
 		bpf_object__find_map_fd_by_name(obj, "redirect_err_cnt");
+	exception_cnt_map_fd =
+		bpf_object__find_map_fd_by_name(obj, "exception_cnt");
 	cpumap_enqueue_cnt_map_fd =
 		bpf_object__find_map_fd_by_name(obj, "cpumap_enqueue_cnt");
 	cpumap_kthread_cnt_map_fd =
 		bpf_object__find_map_fd_by_name(obj, "cpumap_kthread_cnt");
+
+	/* Maps used by XDP */
+	rx_cnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rx_cnt");
+	cpu_map_fd = bpf_object__find_map_fd_by_name(obj, "cpu_map");
 	cpus_available_map_fd =
 		bpf_object__find_map_fd_by_name(obj, "cpus_available");
 	cpus_count_map_fd = bpf_object__find_map_fd_by_name(obj, "cpus_count");
 	cpus_iterator_map_fd =
 		bpf_object__find_map_fd_by_name(obj, "cpus_iterator");
-	exception_cnt_map_fd =
-		bpf_object__find_map_fd_by_name(obj, "exception_cnt");
 
 	if (cpu_map_fd < 0 || rx_cnt_map_fd < 0 ||
 	    redirect_err_cnt_map_fd < 0 || cpumap_enqueue_cnt_map_fd < 0 ||
@@ -662,6 +712,7 @@ int main(int argc, char **argv)
 			strerror(errno));
 		return EXIT_FAIL;
 	}
+	init_tracepoints(obj);
 	if (init_map_fds(obj) < 0) {
 		fprintf(stderr, "bpf_object__find_map_fd_by_name failed\n");
 		return EXIT_FAIL;



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf] samples/bpf: xdp_redirect_cpu fix missing tracepoint attach
  2019-12-20 16:19 [PATCH bpf] samples/bpf: xdp_redirect_cpu fix missing tracepoint attach Jesper Dangaard Brouer
@ 2019-12-20 17:44 ` Andrii Nakryiko
  2019-12-21  0:14   ` Alexei Starovoitov
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2019-12-20 17:44 UTC (permalink / raw)
  To: Jesper Dangaard Brouer
  Cc: bpf, Networking, Maciek Fijałkowski, Daniel Borkmann,
	Alexei Starovoitov, Björn Töpel

On Fri, Dec 20, 2019 at 8:19 AM Jesper Dangaard Brouer
<brouer@redhat.com> wrote:
>
> 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: bbaf6029c49c ("samples/bpf: Convert XDP samples to libbpf usage")
> Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
> ---

Acked-by: Andrii Nakryiko <andriin@fb.com>

>  samples/bpf/xdp_redirect_cpu_user.c |   59 +++++++++++++++++++++++++++++++++--
>  1 file changed, 55 insertions(+), 4 deletions(-)
>

[...]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf] samples/bpf: xdp_redirect_cpu fix missing tracepoint attach
  2019-12-20 17:44 ` Andrii Nakryiko
@ 2019-12-21  0:14   ` Alexei Starovoitov
  0 siblings, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2019-12-21  0:14 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Jesper Dangaard Brouer, bpf, Networking, Maciek Fijałkowski,
	Daniel Borkmann, Björn Töpel

On Fri, Dec 20, 2019 at 09:44:42AM -0800, Andrii Nakryiko wrote:
> On Fri, Dec 20, 2019 at 8:19 AM Jesper Dangaard Brouer
> <brouer@redhat.com> wrote:
> >
> > 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: bbaf6029c49c ("samples/bpf: Convert XDP samples to libbpf usage")
> > Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
> > ---
> 
> Acked-by: Andrii Nakryiko <andriin@fb.com>

Applied to bpf-next. Thanks

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-12-21  0:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-20 16:19 [PATCH bpf] samples/bpf: xdp_redirect_cpu fix missing tracepoint attach Jesper Dangaard Brouer
2019-12-20 17:44 ` Andrii Nakryiko
2019-12-21  0:14   ` Alexei Starovoitov

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).