diff mbox series

[RFC,bpf-next,08/15] samples: bpf: add per exception reporting for xdp_exception

Message ID 20210528235250.2635167-9-memxor@gmail.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series Improve XDP samples usability and output | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 1 maintainers not CCed: hawk@kernel.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning WARNING: __always_unused or __maybe_unused is preferred over __attribute__((__unused__)) WARNING: line length of 90 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Kumar Kartikeya Dwivedi May 28, 2021, 11:52 p.m. UTC
This is taken from xdp_monitor, in preparation for the conversion in a
subsequent patch.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 samples/bpf/xdp_sample_kern.h |  8 ++++--
 samples/bpf/xdp_sample_user.c | 47 ++++++++++++++++++++---------------
 samples/bpf/xdp_sample_user.h | 24 ++++++++++++++++--
 3 files changed, 55 insertions(+), 24 deletions(-)
diff mbox series

Patch

diff --git a/samples/bpf/xdp_sample_kern.h b/samples/bpf/xdp_sample_kern.h
index 4131b9cb1ec4..ec36e7b4a3ba 100644
--- a/samples/bpf/xdp_sample_kern.h
+++ b/samples/bpf/xdp_sample_kern.h
@@ -63,12 +63,13 @@  struct {
 	__uint(max_entries, 1);
 } cpumap_kthread_cnt SEC(".maps");
 
+#define XDP_UNKNOWN (XDP_REDIRECT + 1)
 /* Used by trace point */
 struct {
 	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
 	__type(key, u32);
 	__type(value, struct datarec);
-	__uint(max_entries, 1);
+	__uint(max_entries, XDP_UNKNOWN + 1);
 } exception_cnt SEC(".maps");
 
 struct {
@@ -184,7 +185,10 @@  SEC("tracepoint/xdp/xdp_exception")
 int trace_xdp_exception(struct xdp_exception_ctx *ctx)
 {
 	struct datarec *rec;
-	u32 key = 0;
+	u32 key = ctx->act;
+
+	if (key > XDP_REDIRECT)
+		key = XDP_UNKNOWN;
 
 	rec = bpf_map_lookup_elem(&exception_cnt, &key);
 	if (!rec)
diff --git a/samples/bpf/xdp_sample_user.c b/samples/bpf/xdp_sample_user.c
index 29410d551574..446668edf8d8 100644
--- a/samples/bpf/xdp_sample_user.c
+++ b/samples/bpf/xdp_sample_user.c
@@ -124,7 +124,8 @@  struct stats_record *alloc_stats_record(void)
 	rec->redir_err[0].cpu = alloc_record_per_cpu();
 	rec->redir_err[1].cpu = alloc_record_per_cpu();
 	rec->kthread.cpu   = alloc_record_per_cpu();
-	rec->exception.cpu = alloc_record_per_cpu();
+	for (i = 0; i < XDP_ACTION_MAX; i++)
+		rec->exception[i].cpu = alloc_record_per_cpu();
 	rec->devmap_xmit.cpu = alloc_record_per_cpu();
 	for (i = 0; i < n_cpus; i++)
 		rec->enq[i].cpu = alloc_record_per_cpu();
@@ -139,7 +140,8 @@  void free_stats_record(struct stats_record *r)
 	for (i = 0; i < n_cpus; i++)
 		free(r->enq[i].cpu);
 	free(r->devmap_xmit.cpu);
-	free(r->exception.cpu);
+	for (i = 0; i < XDP_ACTION_MAX; i++)
+		free(r->exception[i].cpu);
 	free(r->kthread.cpu);
 	free(r->redir_err[1].cpu);
 	free(r->redir_err[0].cpu);
@@ -388,27 +390,31 @@  static void stats_print_exception_cnt(struct stats_record *stats_rec,
 				      struct stats_record *stats_prev,
 				      unsigned int nr_cpus)
 {
-	char *fmt_err = "%-15s %-7d %'-14.0f %'-11.0f\n";
-	char *fm2_err = "%-15s %-7s %'-14.0f %'-11.0f\n";
+	char *fmt1 = "%-15s %-7d %'-12.0f %'-12.0f %s\n";
+	char *fmt2 = "%-15s %-7s %'-12.0f %'-12.0f %s\n";
 	struct record *rec, *prev;
-	double t, pps, drop;
-	int i;
+	double t, drop;
+	int rec_i, i;
 
-	rec = &stats_rec->exception;
-	prev = &stats_prev->exception;
-	t = calc_period(rec, prev);
-	for (i = 0; i < nr_cpus; i++) {
-		struct datarec *r = &rec->cpu[i];
-		struct datarec *p = &prev->cpu[i];
+	for (rec_i = 0; rec_i < XDP_ACTION_MAX; rec_i++) {
+		rec  = &stats_rec->exception[rec_i];
+		prev = &stats_prev->exception[rec_i];
+		t = calc_period(rec, prev);
 
-		pps = calc_pps(r, p, t);
-		drop = calc_drop_pps(r, p, t);
-		if (pps > 0)
-			printf(fmt_err, "xdp_exception", i, pps, drop);
+		for (i = 0; i < nr_cpus; i++) {
+			struct datarec *r = &rec->cpu[i];
+			struct datarec *p = &prev->cpu[i];
+
+			drop = calc_drop_pps(r, p, t);
+			if (drop > 0)
+				printf(fmt1, "xdp_exception", i,
+				       0.0, drop, action2str(rec_i));
+		}
+		drop = calc_drop_pps(&rec->total, &prev->total, t);
+		if (drop > 0)
+			printf(fmt2, "xdp_exception", "total",
+			       0.0, drop, action2str(rec_i));
 	}
-	pps = calc_pps(&rec->total, &prev->total, t);
-	drop = calc_drop_pps(&rec->total, &prev->total, t);
-	printf(fm2_err, "xdp_exception", "total", pps, drop);
 }
 
 void sample_stats_print_cpumap_remote(struct stats_record *stats_rec,
@@ -564,7 +570,8 @@  void sample_stats_collect(int mask, struct stats_record *rec)
 		map_collect_percpu(map_fds[CPUMAP_KTHREAD_CNT], 0, &rec->kthread);
 
 	if (mask & SAMPLE_EXCEPTION_CNT)
-		map_collect_percpu(map_fds[EXCEPTION_CNT], 0, &rec->exception);
+		for (i = 0; i < XDP_ACTION_MAX; i++)
+			map_collect_percpu(map_fds[EXCEPTION_CNT], i, &rec->exception[i]);
 
 	if (mask & SAMPLE_DEVMAP_XMIT_CNT)
 		map_collect_percpu(map_fds[DEVMAP_XMIT_CNT], 0, &rec->devmap_xmit);
diff --git a/samples/bpf/xdp_sample_user.h b/samples/bpf/xdp_sample_user.h
index a3a3c746e73e..bc0362575d4b 100644
--- a/samples/bpf/xdp_sample_user.h
+++ b/samples/bpf/xdp_sample_user.h
@@ -59,12 +59,32 @@  extern int tp_cnt;
 
 #define XDP_REDIRECT_ERR_MAX 6
 
-static const char *xdp_redirect_err_names[XDP_REDIRECT_ERR_MAX] = {
+__attribute__((unused)) static const char *xdp_redirect_err_names[XDP_REDIRECT_ERR_MAX] = {
 	/* Key=1 keeps unknown errors */
 	"Success", "Unknown", "EINVAL", "ENETDOWN", "EMSGSIZE",
 	"EOPNOTSUPP",
 };
 
+/* enum xdp_action */
+#define XDP_UNKNOWN (XDP_REDIRECT + 1)
+#define XDP_ACTION_MAX (XDP_UNKNOWN + 1)
+
+static const char *xdp_action_names[XDP_ACTION_MAX] = {
+	[XDP_ABORTED]	= "XDP_ABORTED",
+	[XDP_DROP]	= "XDP_DROP",
+	[XDP_PASS]	= "XDP_PASS",
+	[XDP_TX]	= "XDP_TX",
+	[XDP_REDIRECT]	= "XDP_REDIRECT",
+	[XDP_UNKNOWN]	= "XDP_UNKNOWN",
+};
+
+__attribute__((unused)) static inline const char *action2str(int action)
+{
+	if (action < XDP_ACTION_MAX)
+		return xdp_action_names[action];
+	return NULL;
+}
+
 /* Common stats data record shared with _kern.c */
 struct datarec {
 	__u64 processed;
@@ -88,7 +108,7 @@  struct stats_record {
 	struct record rx_cnt;
 	struct record redir_err[XDP_REDIRECT_ERR_MAX];
 	struct record kthread;
-	struct record exception;
+	struct record exception[XDP_ACTION_MAX];
 	struct record devmap_xmit;
 	struct record enq[];
 };