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 10/22] samples: bpf: Add BPF support for devmap_xmit tracepoint
Date: Sat, 21 Aug 2021 05:49:58 +0530	[thread overview]
Message-ID: <20210821002010.845777-11-memxor@gmail.com> (raw)
In-Reply-To: <20210821002010.845777-1-memxor@gmail.com>

This adds support for the devmap_xmit tracepoint, and its multi device
variant that can be used to obtain streams for each individual
net_device to net_device redirection. This is useful for decomposing
total xmit stats in xdp_monitor.

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

diff --git a/samples/bpf/xdp_sample.bpf.c b/samples/bpf/xdp_sample.bpf.c
index f01a5529751c..0eb7e1dcae22 100644
--- a/samples/bpf/xdp_sample.bpf.c
+++ b/samples/bpf/xdp_sample.bpf.c
@@ -11,6 +11,14 @@ 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");
+array_map devmap_xmit_cnt SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
+	__uint(max_entries, 32 * 32);
+	__type(key, u64);
+	__type(value, struct datarec);
+} devmap_xmit_cnt_multi SEC(".maps");
 
 const volatile int nr_cpus = 0;
 
@@ -193,3 +201,66 @@ int BPF_PROG(tp_xdp_exception, const struct net_device *dev,
 
 	return 0;
 }
+
+SEC("tp_btf/xdp_devmap_xmit")
+int BPF_PROG(tp_xdp_devmap_xmit, const struct net_device *from_dev,
+	     const struct net_device *to_dev, int sent, int drops, int err)
+{
+	struct datarec *rec;
+	int idx_in, idx_out;
+	u32 cpu;
+
+	idx_in = from_dev->ifindex;
+	idx_out = to_dev->ifindex;
+
+	if (!IN_SET(from_match, idx_in))
+		return 0;
+	if (!IN_SET(to_match, idx_out))
+		return 0;
+
+	cpu = bpf_get_smp_processor_id();
+	rec = bpf_map_lookup_elem(&devmap_xmit_cnt, &cpu);
+	if (!rec)
+		return 0;
+	NO_TEAR_ADD(rec->processed, sent);
+	NO_TEAR_ADD(rec->dropped, drops);
+	/* Record bulk events, then userspace can calc average bulk size */
+	NO_TEAR_INC(rec->info);
+	/* Record error cases, where no frame were sent */
+	/* Catch API error of drv ndo_xdp_xmit sent more than count */
+	if (err || drops < 0)
+		NO_TEAR_INC(rec->issue);
+	return 0;
+}
+
+SEC("tp_btf/xdp_devmap_xmit")
+int BPF_PROG(tp_xdp_devmap_xmit_multi, const struct net_device *from_dev,
+	     const struct net_device *to_dev, int sent, int drops, int err)
+{
+	struct datarec empty = {};
+	struct datarec *rec;
+	int idx_in, idx_out;
+	u64 idx;
+
+	idx_in = from_dev->ifindex;
+	idx_out = to_dev->ifindex;
+	idx = idx_in;
+	idx = idx << 32 | idx_out;
+
+	if (!IN_SET(from_match, idx_in))
+		return 0;
+	if (!IN_SET(to_match, idx_out))
+		return 0;
+
+	bpf_map_update_elem(&devmap_xmit_cnt_multi, &idx, &empty, BPF_NOEXIST);
+	rec = bpf_map_lookup_elem(&devmap_xmit_cnt_multi, &idx);
+	if (!rec)
+		return 0;
+
+	NO_TEAR_ADD(rec->processed, sent);
+	NO_TEAR_ADD(rec->dropped, drops);
+	NO_TEAR_INC(rec->info);
+	if (err || drops < 0)
+		NO_TEAR_INC(rec->issue);
+	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 ` [PATCH bpf-next v4 08/22] samples: bpf: Add BPF support for cpumap tracepoints Kumar Kartikeya Dwivedi
2021-08-21  0:19 ` [PATCH bpf-next v4 09/22] samples: bpf: Add cpumap 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 11/22] samples: bpf: Add devmap_xmit " 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-11-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