BPF List
 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>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	netdev@vger.kernel.org
Subject: [PATCH bpf-next v4 08/22] samples: bpf: Add BPF support for cpumap tracepoints
Date: Sat, 21 Aug 2021 05:49:56 +0530	[thread overview]
Message-ID: <20210821002010.845777-9-memxor@gmail.com> (raw)
In-Reply-To: <20210821002010.845777-1-memxor@gmail.com>

These are invoked in two places, when the XDP frame or SKB (for generic
XDP) enqueued to the ptr_ring (cpumap_enqueue) and when kthread processes
the frame after invoking the CPUMAP program for it (returning stats for
the batch).

We use cpumap_map_id to filter on the map_id as a way to avoid printing
incorrect stats for parallel sessions of xdp_redirect_cpu.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 samples/bpf/xdp_sample.bpf.c | 58 +++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/samples/bpf/xdp_sample.bpf.c b/samples/bpf/xdp_sample.bpf.c
index 53ab5a972405..f01a5529751c 100644
--- a/samples/bpf/xdp_sample.bpf.c
+++ b/samples/bpf/xdp_sample.bpf.c
@@ -8,6 +8,8 @@
 
 array_map rx_cnt SEC(".maps");
 array_map redir_err_cnt SEC(".maps");
+array_map cpumap_enqueue_cnt SEC(".maps");
+array_map cpumap_kthread_cnt SEC(".maps");
 array_map exception_cnt SEC(".maps");
 
 const volatile int nr_cpus = 0;
@@ -19,6 +21,8 @@ const volatile int nr_cpus = 0;
 const volatile int from_match[32] = {};
 const volatile int to_match[32] = {};
 
+int cpumap_map_id = 0;
+
 /* Find if b is part of set a, but if a is empty set then evaluate to true */
 #define IN_SET(a, b)                                                 \
 	({                                                           \
@@ -112,6 +116,59 @@ int BPF_PROG(tp_xdp_redirect_map, const struct net_device *dev,
 	return xdp_redirect_collect_stat(dev->ifindex, err);
 }
 
+SEC("tp_btf/xdp_cpumap_enqueue")
+int BPF_PROG(tp_xdp_cpumap_enqueue, int map_id, unsigned int processed,
+	     unsigned int drops, int to_cpu)
+{
+	u32 cpu = bpf_get_smp_processor_id();
+	struct datarec *rec;
+	u32 idx;
+
+	if (cpumap_map_id && cpumap_map_id != map_id)
+		return 0;
+
+	idx = to_cpu * nr_cpus + cpu;
+	rec = bpf_map_lookup_elem(&cpumap_enqueue_cnt, &idx);
+	if (!rec)
+		return 0;
+	NO_TEAR_ADD(rec->processed, processed);
+	NO_TEAR_ADD(rec->dropped, drops);
+	/* Record bulk events, then userspace can calc average bulk size */
+	if (processed > 0)
+		NO_TEAR_INC(rec->issue);
+	/* Inception: It's possible to detect overload situations, via
+	 * this tracepoint.  This can be used for creating a feedback
+	 * loop to XDP, which can take appropriate actions to mitigate
+	 * this overload situation.
+	 */
+	return 0;
+}
+
+SEC("tp_btf/xdp_cpumap_kthread")
+int BPF_PROG(tp_xdp_cpumap_kthread, int map_id, unsigned int processed,
+	     unsigned int drops, int sched, struct xdp_cpumap_stats *xdp_stats)
+{
+	struct datarec *rec;
+	u32 cpu;
+
+	if (cpumap_map_id && cpumap_map_id != map_id)
+		return 0;
+
+	cpu = bpf_get_smp_processor_id();
+	rec = bpf_map_lookup_elem(&cpumap_kthread_cnt, &cpu);
+	if (!rec)
+		return 0;
+	NO_TEAR_ADD(rec->processed, processed);
+	NO_TEAR_ADD(rec->dropped, drops);
+	NO_TEAR_ADD(rec->xdp_pass, xdp_stats->pass);
+	NO_TEAR_ADD(rec->xdp_drop, xdp_stats->drop);
+	NO_TEAR_ADD(rec->xdp_redirect, xdp_stats->redirect);
+	/* Count times kthread yielded CPU via schedule call */
+	if (sched)
+		NO_TEAR_INC(rec->issue);
+	return 0;
+}
+
 SEC("tp_btf/xdp_exception")
 int BPF_PROG(tp_xdp_exception, const struct net_device *dev,
 	     const struct bpf_prog *xdp, u32 act)
@@ -136,4 +193,3 @@ int BPF_PROG(tp_xdp_exception, const struct net_device *dev,
 
 	return 0;
 }
-
-- 
2.33.0


  parent reply	other threads:[~2021-08-21  0:20 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-21  0:19 [PATCH bpf-next v4 00/22] Improve XDP samples usability and output Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 01/22] samples: bpf: fix a couple of warnings Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 02/22] tools: include: add ethtool_drvinfo definition to UAPI header Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 03/22] samples: bpf: Add basic infrastructure for XDP samples Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 04/22] samples: bpf: Add BPF support for redirect tracepoint Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 05/22] samples: bpf: Add redirect tracepoint statistics support Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 06/22] samples: bpf: Add BPF support for xdp_exception tracepoint Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 07/22] samples: bpf: Add xdp_exception tracepoint statistics support Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` Kumar Kartikeya Dwivedi [this message]
2021-08-21  0:19 ` [PATCH bpf-next v4 09/22] samples: bpf: Add cpumap " Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 10/22] samples: bpf: Add BPF support for devmap_xmit tracepoint Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 11/22] samples: bpf: Add devmap_xmit tracepoint statistics support Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 12/22] samples: bpf: add vmlinux.h generation support Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 13/22] samples: bpf: Convert xdp_monitor_kern.o to XDP samples helper Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 14/22] samples: bpf: Convert xdp_monitor " Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 15/22] samples: bpf: Convert xdp_redirect_kern.o " Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 16/22] samples: bpf: Convert xdp_redirect " Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 17/22] samples: bpf: Convert xdp_redirect_cpu_kern.o " Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 18/22] samples: bpf: Convert xdp_redirect_cpu " Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 19/22] samples: bpf: Convert xdp_redirect_map_kern.o " Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 20/22] samples: bpf: Convert xdp_redirect_map " Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 21/22] samples: bpf: Convert xdp_redirect_map_multi_kern.o " Kumar Kartikeya Dwivedi
2021-08-21  0:20 ` [PATCH bpf-next v4 22/22] samples: bpf: Convert xdp_redirect_map_multi " Kumar Kartikeya Dwivedi

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=20210821002010.845777-9-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=netdev@vger.kernel.org \
    --cc=toke@redhat.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