aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
diff options
context:
space:
mode:
authorLorenz Bauer <lmb@cloudflare.com>2020-09-09 17:27:12 +0100
committerAlexei Starovoitov <ast@kernel.org>2020-09-10 12:35:26 -0700
commit2f7de9865ba3cbfcf8b504f07154fdb6124176a4 (patch)
tree3960be828aa4e194fbc497e85e27d2049009ece2 /tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
parentnet: Allow iterating sockmap and sockhash (diff)
downloadlinux-dev-2f7de9865ba3cbfcf8b504f07154fdb6124176a4.tar.xz
linux-dev-2f7de9865ba3cbfcf8b504f07154fdb6124176a4.zip
selftests: bpf: Test iterating a sockmap
Add a test that exercises a basic sockmap / sockhash iteration. For now we simply count the number of elements seen. Once sockmap update from iterators works we can extend this to perform a full copy. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200909162712.221874-4-lmb@cloudflare.com
Diffstat (limited to 'tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c')
-rw-r--r--tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c b/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
new file mode 100644
index 000000000000..0e27f73dd803
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Cloudflare */
+#include "bpf_iter.h"
+#include "bpf_tracing_net.h"
+#include "bpf_iter_sockmap.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <errno.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+ __uint(type, BPF_MAP_TYPE_SOCKMAP);
+ __uint(max_entries, SOCKMAP_MAX_ENTRIES);
+ __type(key, __u32);
+ __type(value, __u64);
+} sockmap SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_SOCKHASH);
+ __uint(max_entries, SOCKMAP_MAX_ENTRIES);
+ __type(key, __u32);
+ __type(value, __u64);
+} sockhash SEC(".maps");
+
+__u32 elems = 0;
+__u32 socks = 0;
+
+SEC("iter/sockmap")
+int count_elems(struct bpf_iter__sockmap *ctx)
+{
+ struct sock *sk = ctx->sk;
+ __u32 tmp, *key = ctx->key;
+ int ret;
+
+ if (key)
+ elems++;
+
+ if (sk)
+ socks++;
+
+ return 0;
+}