Skip to content

Commit 651e618

Browse files
lmbkernel-patches-bot
authored andcommitted
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 <[email protected]>
1 parent c9f8dfb commit 651e618

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

tools/testing/selftests/bpf/prog_tests/sockmap_basic.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static void test_sockmap_invalid_update(void)
194194
test_sockmap_invalid_update__destroy(skel);
195195
}
196196

197-
static void test_sockmap_iter(enum bpf_map_type map_type)
197+
static void test_sockmap_copy(enum bpf_map_type map_type)
198198
{
199199
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
200200
int err, len, src_fd, iter_fd, duration = 0;
@@ -242,7 +242,7 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
242242
linfo.map.map_fd = src_fd;
243243
opts.link_info = &linfo;
244244
opts.link_info_len = sizeof(linfo);
245-
link = bpf_program__attach_iter(skel->progs.count_elems, &opts);
245+
link = bpf_program__attach_iter(skel->progs.copy, &opts);
246246
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
247247
goto out;
248248

@@ -265,6 +265,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
265265
skel->bss->socks, num_sockets))
266266
goto close_iter;
267267

268+
compare_cookies(src, skel->maps.dst);
269+
268270
close_iter:
269271
close(iter_fd);
270272
free_link:
@@ -294,8 +296,8 @@ void test_sockmap_basic(void)
294296
test_sockmap_update(BPF_MAP_TYPE_SOCKHASH);
295297
if (test__start_subtest("sockmap update in unsafe context"))
296298
test_sockmap_invalid_update();
297-
if (test__start_subtest("sockmap iter"))
298-
test_sockmap_iter(BPF_MAP_TYPE_SOCKMAP);
299-
if (test__start_subtest("sockhash iter"))
300-
test_sockmap_iter(BPF_MAP_TYPE_SOCKHASH);
299+
if (test__start_subtest("sockmap copy"))
300+
test_sockmap_copy(BPF_MAP_TYPE_SOCKMAP);
301+
if (test__start_subtest("sockhash copy"))
302+
test_sockmap_copy(BPF_MAP_TYPE_SOCKHASH);
301303
}

tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,38 @@ struct {
2222
__type(value, __u64);
2323
} sockhash SEC(".maps");
2424

25+
struct {
26+
__uint(type, BPF_MAP_TYPE_SOCKHASH);
27+
__uint(max_entries, 64);
28+
__type(key, __u32);
29+
__type(value, __u64);
30+
} dst SEC(".maps");
31+
2532
__u32 elems = 0;
2633
__u32 socks = 0;
2734

2835
SEC("iter/sockmap")
29-
int count_elems(struct bpf_iter__sockmap *ctx)
36+
int copy(struct bpf_iter__sockmap *ctx)
3037
{
3138
struct sock *sk = ctx->sk;
3239
__u32 tmp, *key = ctx->key;
3340
int ret;
3441

35-
if (key)
36-
elems++;
42+
if (!key)
43+
return 0;
44+
45+
elems++;
46+
47+
/* We need a temporary buffer on the stack, since the verifier doesn't
48+
* let us use the pointer from the context as an argument to the helper.
49+
*/
50+
tmp = *key;
3751

38-
if (sk)
52+
if (sk) {
3953
socks++;
54+
return bpf_map_update_elem(&dst, &tmp, sk, 0) != 0;
55+
}
4056

41-
return 0;
57+
ret = bpf_map_delete_elem(&dst, &tmp);
58+
return ret && ret != -ENOENT;
4259
}

0 commit comments

Comments
 (0)