netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Cc: "Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Martin KaFai Lau" <kafai@fb.com>,
	"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"KP Singh" <kpsingh@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	netdev@vger.kernel.org
Subject: [PATCH RFC bpf-next 15/15] samples: bpf: convert xdp_samples to use raw_tracepoints
Date: Sat, 29 May 2021 05:22:50 +0530	[thread overview]
Message-ID: <20210528235250.2635167-16-memxor@gmail.com> (raw)
In-Reply-To: <20210528235250.2635167-1-memxor@gmail.com>

These are faster, and hence speeds up cases where user passes --stats to
enable success case redirect accounting. We can extend this to all other
tracepoints as well, so make that part of this change.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 samples/bpf/xdp_sample_kern.h | 145 +++++++++++-----------------------
 samples/bpf/xdp_sample_user.c |   2 +-
 2 files changed, 45 insertions(+), 102 deletions(-)

diff --git a/samples/bpf/xdp_sample_kern.h b/samples/bpf/xdp_sample_kern.h
index dd7f7ea63166..08fbc55df3fd 100644
--- a/samples/bpf/xdp_sample_kern.h
+++ b/samples/bpf/xdp_sample_kern.h
@@ -3,6 +3,9 @@
 #pragma once
 
 #include <uapi/linux/bpf.h>
+#include <net/xdp.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_tracing.h>
 #include <bpf/bpf_helpers.h>
 
 #ifndef NR_CPUS
@@ -85,20 +88,6 @@ struct {
 
 /*** Trace point code ***/
 
-/* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_redirect/format
- * Code in:                kernel/include/trace/events/xdp.h
- */
-struct xdp_redirect_ctx {
-	u64 __pad;	// First 8 bytes are not accessible by bpf code
-	int prog_id;	//	offset:8;  size:4; signed:1;
-	u32 act;	//	offset:12  size:4; signed:0;
-	int ifindex;	//	offset:16  size:4; signed:1;
-	int err;	//	offset:20  size:4; signed:1;
-	int to_ifindex;	//	offset:24  size:4; signed:1;
-	u32 map_id;	//	offset:28  size:4; signed:0;
-	int map_index;	//	offset:32  size:4; signed:1;
-};			//	offset:36
-
 enum {
 	XDP_REDIRECT_SUCCESS = 0,
 	XDP_REDIRECT_ERROR = 1
@@ -124,11 +113,11 @@ __u32 xdp_get_err_key(int err)
 }
 
 static __always_inline
-int xdp_redirect_collect_stat(struct xdp_redirect_ctx *ctx)
+int xdp_redirect_collect_stat(struct bpf_raw_tracepoint_args *ctx)
 {
 	u32 key = XDP_REDIRECT_ERROR;
+	int err = ctx->args[3];
 	struct datarec *rec;
-	int err = ctx->err;
 
 	key = xdp_get_err_key(err);
 
@@ -149,47 +138,35 @@ int xdp_redirect_collect_stat(struct xdp_redirect_ctx *ctx)
 	 */
 }
 
-SEC("tracepoint/xdp/xdp_redirect_err")
-int trace_xdp_redirect_err(struct xdp_redirect_ctx *ctx)
+SEC("raw_tracepoint/xdp_redirect_err")
+int trace_xdp_redirect_err(struct bpf_raw_tracepoint_args *ctx)
 {
 	return xdp_redirect_collect_stat(ctx);
 }
 
-SEC("tracepoint/xdp/xdp_redirect_map_err")
-int trace_xdp_redirect_map_err(struct xdp_redirect_ctx *ctx)
+SEC("raw_tracepoint/xdp_redirect_map_err")
+int trace_xdp_redirect_map_err(struct bpf_raw_tracepoint_args *ctx)
 {
 	return xdp_redirect_collect_stat(ctx);
 }
 
-/* Likely unloaded when prog starts */
-SEC("tracepoint/xdp/xdp_redirect")
-int trace_xdp_redirect(struct xdp_redirect_ctx *ctx)
+SEC("raw_tracepoint/xdp_redirect")
+int trace_xdp_redirect(struct bpf_raw_tracepoint_args *ctx)
 {
 	return xdp_redirect_collect_stat(ctx);
 }
 
-/* Likely unloaded when prog starts */
-SEC("tracepoint/xdp/xdp_redirect_map")
-int trace_xdp_redirect_map(struct xdp_redirect_ctx *ctx)
+SEC("raw_tracepoint/xdp_redirect_map")
+int trace_xdp_redirect_map(struct bpf_raw_tracepoint_args *ctx)
 {
 	return xdp_redirect_collect_stat(ctx);
 }
 
-/* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_exception/format
- * Code in:                kernel/include/trace/events/xdp.h
- */
-struct xdp_exception_ctx {
-	u64 __pad;	// First 8 bytes are not accessible by bpf code
-	int prog_id;	//	offset:8;  size:4; signed:1;
-	u32 act;	//	offset:12; size:4; signed:0;
-	int ifindex;	//	offset:16; size:4; signed:1;
-};
-
-SEC("tracepoint/xdp/xdp_exception")
-int trace_xdp_exception(struct xdp_exception_ctx *ctx)
+SEC("raw_tracepoint/xdp_exception")
+int trace_xdp_exception(struct bpf_raw_tracepoint_args *ctx)
 {
+	u32 key = ctx->args[2];
 	struct datarec *rec;
-	u32 key = ctx->act;
 
 	if (key > XDP_REDIRECT)
 		key = XDP_UNKNOWN;
@@ -202,23 +179,10 @@ int trace_xdp_exception(struct xdp_exception_ctx *ctx)
 	return 0;
 }
 
-/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_enqueue/format
- * Code in:         kernel/include/trace/events/xdp.h
- */
-struct cpumap_enqueue_ctx {
-	u64 __pad;		// First 8 bytes are not accessible by bpf code
-	int map_id;		//	offset:8;  size:4; signed:1;
-	u32 act;		//	offset:12; size:4; signed:0;
-	int cpu;		//	offset:16; size:4; signed:1;
-	unsigned int drops;	//	offset:20; size:4; signed:0;
-	unsigned int processed;	//	offset:24; size:4; signed:0;
-	int to_cpu;		//	offset:28; size:4; signed:1;
-};
-
-SEC("tracepoint/xdp/xdp_cpumap_enqueue")
-int trace_xdp_cpumap_enqueue(struct cpumap_enqueue_ctx *ctx)
+SEC("raw_tracepoint/xdp_cpumap_enqueue")
+int trace_xdp_cpumap_enqueue(struct bpf_raw_tracepoint_args *ctx)
 {
-	u32 to_cpu = ctx->to_cpu;
+	u32 to_cpu = ctx->args[3];
 	struct datarec *rec;
 
 	if (to_cpu >= MAX_CPUS)
@@ -227,11 +191,11 @@ int trace_xdp_cpumap_enqueue(struct cpumap_enqueue_ctx *ctx)
 	rec = bpf_map_lookup_elem(&cpumap_enqueue_cnt, &to_cpu);
 	if (!rec)
 		return 0;
-	rec->processed += ctx->processed;
-	rec->dropped   += ctx->drops;
+	rec->processed += ctx->args[1];
+	rec->dropped   += ctx->args[2];
 
 	/* Record bulk events, then userspace can calc average bulk size */
-	if (ctx->processed > 0)
+	if (ctx->args[1] > 0)
 		rec->issue += 1;
 
 	/* Inception: It's possible to detect overload situations, via
@@ -242,78 +206,57 @@ int trace_xdp_cpumap_enqueue(struct cpumap_enqueue_ctx *ctx)
 	return 0;
 }
 
-/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_kthread/format
- * Code in:         kernel/include/trace/events/xdp.h
- */
-struct cpumap_kthread_ctx {
-	u64 __pad;			// First 8 bytes are not accessible
-	int map_id;			//	offset:8;  size:4; signed:1;
-	u32 act;			//	offset:12; size:4; signed:0;
-	int cpu;			//	offset:16; size:4; signed:1;
-	unsigned int drops;		//	offset:20; size:4; signed:0;
-	unsigned int processed;		//	offset:24; size:4; signed:0;
-	int sched;			//	offset:28; size:4; signed:1;
-	unsigned int xdp_pass;		//	offset:32; size:4; signed:0;
-	unsigned int xdp_drop;		//	offset:36; size:4; signed:0;
-	unsigned int xdp_redirect;	//	offset:40; size:4; signed:0;
-};
-
-SEC("tracepoint/xdp/xdp_cpumap_kthread")
-int trace_xdp_cpumap_kthread(struct cpumap_kthread_ctx *ctx)
+SEC("raw_tracepoint/xdp_cpumap_kthread")
+int trace_xdp_cpumap_kthread(struct bpf_raw_tracepoint_args *ctx)
 {
+	struct xdp_cpumap_stats *stats;
 	struct datarec *rec;
 	u32 key = 0;
 
+	stats = (struct xdp_cpumap_stats *) ctx->args[4];
+	if (!stats)
+		return 0;
+
 	rec = bpf_map_lookup_elem(&cpumap_kthread_cnt, &key);
 	if (!rec)
 		return 0;
-	rec->processed += ctx->processed;
-	rec->dropped   += ctx->drops;
-	rec->xdp_pass  += ctx->xdp_pass;
-	rec->xdp_drop  += ctx->xdp_drop;
-	rec->xdp_redirect  += ctx->xdp_redirect;
+	rec->processed += ctx->args[1];
+	rec->dropped   += ctx->args[2];
+
+	rec->xdp_pass  += BPF_CORE_READ(stats, pass);
+	rec->xdp_drop  += BPF_CORE_READ(stats, drop);
+	rec->xdp_redirect  += BPF_CORE_READ(stats, redirect);
 
 	/* Count times kthread yielded CPU via schedule call */
-	if (ctx->sched)
+	if (ctx->args[3])
 		rec->issue++;
 
 	return 0;
 }
 
-/* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_devmap_xmit/format
- * Code in:         kernel/include/trace/events/xdp.h
- */
-struct devmap_xmit_ctx {
-	u64 __pad;		// First 8 bytes are not accessible by bpf code
-	int from_ifindex;	//	offset:8;  size:4; signed:1;
-	u32 act;		//	offset:12; size:4; signed:0;
-	int to_ifindex;		//	offset:16; size:4; signed:1;
-	int drops;		//	offset:20; size:4; signed:1;
-	int sent;		//	offset:24; size:4; signed:1;
-	int err;		//	offset:28; size:4; signed:1;
-};
-
-SEC("tracepoint/xdp/xdp_devmap_xmit")
-int trace_xdp_devmap_xmit(struct devmap_xmit_ctx *ctx)
+SEC("raw_tracepoint/xdp_devmap_xmit")
+int trace_xdp_devmap_xmit(struct bpf_raw_tracepoint_args *ctx)
 {
 	struct datarec *rec;
 	u32 key = 0;
+	int drops;
 
 	rec = bpf_map_lookup_elem(&devmap_xmit_cnt, &key);
 	if (!rec)
 		return 0;
-	rec->processed += ctx->sent;
-	rec->dropped   += ctx->drops;
+	rec->processed += ctx->args[2];
+	rec->dropped   += ctx->args[3];
 
 	/* Record bulk events, then userspace can calc average bulk size */
 	rec->info += 1;
 
 	/* Record error cases, where no frame were sent */
-	if (ctx->err)
+	if (ctx->args[4])
 		rec->issue++;
 
+	drops = ctx->args[3];
 	/* Catch API error of drv ndo_xdp_xmit sent more than count */
-	if (ctx->drops < 0)
+	if (drops < 0)
 		rec->issue++;
 
 	return 1;
diff --git a/samples/bpf/xdp_sample_user.c b/samples/bpf/xdp_sample_user.c
index aa02d9bbea6c..539c0c78fcb0 100644
--- a/samples/bpf/xdp_sample_user.c
+++ b/samples/bpf/xdp_sample_user.c
@@ -805,7 +805,7 @@ static int init_tracepoints(struct bpf_object *obj)
 	struct bpf_program *prog;
 
 	bpf_object__for_each_program(prog, obj) {
-		if (bpf_program__is_tracepoint(prog) != true)
+		if (!bpf_program__is_raw_tracepoint(prog))
 			continue;
 
 		tp_links[tp_cnt] = bpf_program__attach(prog);
-- 
2.31.1


  parent reply	other threads:[~2021-05-28 23:54 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-28 23:52 [PATCH RFC bpf-next 00/15] Improve XDP samples usability and output Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 01/15] samples: bpf: fix a couple of NULL dereferences Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 02/15] samples: bpf: fix a couple of warnings Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 03/15] samples: bpf: split out common bpf progs to its own file Kumar Kartikeya Dwivedi
2021-05-30  3:05   ` Andrii Nakryiko
2021-05-28 23:52 ` [PATCH RFC bpf-next 04/15] samples: bpf: refactor generic parts out of xdp_redirect_cpu_user Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 05/15] samples: bpf: convert xdp_redirect_map to use xdp_samples Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 06/15] samples: bpf: prepare devmap_xmit support in xdp_sample Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 07/15] samples: bpf: add extended reporting for xdp redirect error Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 08/15] samples: bpf: add per exception reporting for xdp_exception Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 09/15] samples: bpf: convert xdp_monitor to use xdp_samples Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 10/15] samples: bpf: implement terse output mode and make it default Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 11/15] samples: bpf: print summary of session on exit Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 12/15] samples: bpf: subtract time spent in collection from polling interval Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 13/15] samples: bpf: add new options for xdp samples Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` [PATCH RFC bpf-next 14/15] samples: bpf: add documentation Kumar Kartikeya Dwivedi
2021-05-28 23:52 ` Kumar Kartikeya Dwivedi [this message]
2021-05-30  3:07   ` [PATCH RFC bpf-next 15/15] samples: bpf: convert xdp_samples to use raw_tracepoints Andrii Nakryiko

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=20210528235250.2635167-16-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=toke@redhat.com \
    --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 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).