aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
diff options
context:
space:
mode:
authorLorenz Bauer <lmb@cloudflare.com>2020-09-28 10:08:05 +0100
committerAlexei Starovoitov <ast@kernel.org>2020-09-28 16:48:02 -0700
commit5b87adc3ceee3c3131e24e9bc9ef92dd41db089f (patch)
treeae664a475c782e9d22991f2672eef4b56e4fd72f /tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
parentselftests: bpf: Remove shared header from sockmap iter test (diff)
downloadlinux-dev-5b87adc3ceee3c3131e24e9bc9ef92dd41db089f.tar.xz
linux-dev-5b87adc3ceee3c3131e24e9bc9ef92dd41db089f.zip
selftest: bpf: Test copying a sockmap and sockhash
Since we can now call map_update_elem(sockmap) from bpf_iter context it's possible to copy a sockmap or sockhash in the kernel. Add a selftest which exercises this. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200928090805.23343-5-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.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c b/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
index 1af7555f6057..f3af0e30cead 100644
--- a/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
+++ b/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c
@@ -22,21 +22,38 @@ struct {
__type(value, __u64);
} sockhash SEC(".maps");
+struct {
+ __uint(type, BPF_MAP_TYPE_SOCKHASH);
+ __uint(max_entries, 64);
+ __type(key, __u32);
+ __type(value, __u64);
+} dst SEC(".maps");
+
__u32 elems = 0;
__u32 socks = 0;
SEC("iter/sockmap")
-int count_elems(struct bpf_iter__sockmap *ctx)
+int copy(struct bpf_iter__sockmap *ctx)
{
struct sock *sk = ctx->sk;
__u32 tmp, *key = ctx->key;
int ret;
- if (key)
- elems++;
+ if (!key)
+ return 0;
+
+ elems++;
+
+ /* We need a temporary buffer on the stack, since the verifier doesn't
+ * let us use the pointer from the context as an argument to the helper.
+ */
+ tmp = *key;
- if (sk)
+ if (sk) {
socks++;
+ return bpf_map_update_elem(&dst, &tmp, sk, 0) != 0;
+ }
- return 0;
+ ret = bpf_map_delete_elem(&dst, &tmp);
+ return ret && ret != -ENOENT;
}