Commit 5b87adc3 authored by Lorenz Bauer's avatar Lorenz Bauer Committed by Alexei Starovoitov

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: default avatarLorenz Bauer <lmb@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200928090805.23343-5-lmb@cloudflare.com
parent 27870317
...@@ -194,7 +194,7 @@ static void test_sockmap_invalid_update(void) ...@@ -194,7 +194,7 @@ static void test_sockmap_invalid_update(void)
test_sockmap_invalid_update__destroy(skel); test_sockmap_invalid_update__destroy(skel);
} }
static void test_sockmap_iter(enum bpf_map_type map_type) static void test_sockmap_copy(enum bpf_map_type map_type)
{ {
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
int err, len, src_fd, iter_fd, duration = 0; int err, len, src_fd, iter_fd, duration = 0;
...@@ -242,7 +242,7 @@ static void test_sockmap_iter(enum bpf_map_type map_type) ...@@ -242,7 +242,7 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
linfo.map.map_fd = src_fd; linfo.map.map_fd = src_fd;
opts.link_info = &linfo; opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo); opts.link_info_len = sizeof(linfo);
link = bpf_program__attach_iter(skel->progs.count_elems, &opts); link = bpf_program__attach_iter(skel->progs.copy, &opts);
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
goto out; goto out;
...@@ -265,6 +265,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type) ...@@ -265,6 +265,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
skel->bss->socks, num_sockets)) skel->bss->socks, num_sockets))
goto close_iter; goto close_iter;
compare_cookies(src, skel->maps.dst);
close_iter: close_iter:
close(iter_fd); close(iter_fd);
free_link: free_link:
...@@ -294,8 +296,8 @@ void test_sockmap_basic(void) ...@@ -294,8 +296,8 @@ void test_sockmap_basic(void)
test_sockmap_update(BPF_MAP_TYPE_SOCKHASH); test_sockmap_update(BPF_MAP_TYPE_SOCKHASH);
if (test__start_subtest("sockmap update in unsafe context")) if (test__start_subtest("sockmap update in unsafe context"))
test_sockmap_invalid_update(); test_sockmap_invalid_update();
if (test__start_subtest("sockmap iter")) if (test__start_subtest("sockmap copy"))
test_sockmap_iter(BPF_MAP_TYPE_SOCKMAP); test_sockmap_copy(BPF_MAP_TYPE_SOCKMAP);
if (test__start_subtest("sockhash iter")) if (test__start_subtest("sockhash copy"))
test_sockmap_iter(BPF_MAP_TYPE_SOCKHASH); test_sockmap_copy(BPF_MAP_TYPE_SOCKHASH);
} }
...@@ -22,21 +22,38 @@ struct { ...@@ -22,21 +22,38 @@ struct {
__type(value, __u64); __type(value, __u64);
} sockhash SEC(".maps"); } 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 elems = 0;
__u32 socks = 0; __u32 socks = 0;
SEC("iter/sockmap") SEC("iter/sockmap")
int count_elems(struct bpf_iter__sockmap *ctx) int copy(struct bpf_iter__sockmap *ctx)
{ {
struct sock *sk = ctx->sk; struct sock *sk = ctx->sk;
__u32 tmp, *key = ctx->key; __u32 tmp, *key = ctx->key;
int ret; int ret;
if (key) if (!key)
elems++; 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++; socks++;
return bpf_map_update_elem(&dst, &tmp, sk, 0) != 0;
}
return 0; ret = bpf_map_delete_elem(&dst, &tmp);
return ret && ret != -ENOENT;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment