Skip to content

Commit 84a20d8

Browse files
author
Alexei Starovoitov
committed
Merge branch 'Sockmap copying'
Lorenz Bauer says: ==================== Changes in v2: - Check sk_fullsock in map_update_elem (Martin) Enable calling map_update_elem on sockmaps from bpf_iter context. This in turn allows us to copy a sockmap by iterating its elements. The change itself is tiny, all thanks to the ground work from Martin, whose series [1] this patch is based on. I updated the tests to do some copying, and also included two cleanups. I'm sending this out now rather than when Martin's series has landed because I hope this can get in before the merge window (potentially) closes this weekend. 1: https://lore.kernel.org/bpf/[email protected]/ ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents efa90b5 + 5b87adc commit 84a20d8

File tree

5 files changed

+90
-50
lines changed

5 files changed

+90
-50
lines changed

kernel/bpf/verifier.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3943,7 +3943,7 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env,
39433943
case BPF_MAP_TYPE_SOCKMAP:
39443944
case BPF_MAP_TYPE_SOCKHASH:
39453945
if (*arg_type == ARG_PTR_TO_MAP_VALUE) {
3946-
*arg_type = ARG_PTR_TO_SOCKET;
3946+
*arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON;
39473947
} else {
39483948
verbose(env, "invalid arg_type for sockmap/sockhash\n");
39493949
return -EINVAL;

net/core/sock_map.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,9 @@ static int sock_map_update_elem(struct bpf_map *map, void *key,
610610
struct sock *sk = (struct sock *)value;
611611
int ret;
612612

613+
if (unlikely(!sk || !sk_fullsock(sk)))
614+
return -EINVAL;
615+
613616
if (!sock_map_sk_is_suitable(sk))
614617
return -EOPNOTSUPP;
615618

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

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include "test_sockmap_invalid_update.skel.h"
99
#include "bpf_iter_sockmap.skel.h"
1010

11-
#include "progs/bpf_iter_sockmap.h"
12-
1311
#define TCP_REPAIR 19 /* TCP sock is under repair right now */
1412

1513
#define TCP_REPAIR_ON 1
@@ -50,6 +48,37 @@ static int connected_socket_v4(void)
5048
return -1;
5149
}
5250

51+
static void compare_cookies(struct bpf_map *src, struct bpf_map *dst)
52+
{
53+
__u32 i, max_entries = bpf_map__max_entries(src);
54+
int err, duration = 0, src_fd, dst_fd;
55+
56+
src_fd = bpf_map__fd(src);
57+
dst_fd = bpf_map__fd(dst);
58+
59+
for (i = 0; i < max_entries; i++) {
60+
__u64 src_cookie, dst_cookie;
61+
62+
err = bpf_map_lookup_elem(src_fd, &i, &src_cookie);
63+
if (err && errno == ENOENT) {
64+
err = bpf_map_lookup_elem(dst_fd, &i, &dst_cookie);
65+
CHECK(!err, "map_lookup_elem(dst)", "element %u not deleted\n", i);
66+
CHECK(err && errno != ENOENT, "map_lookup_elem(dst)", "%s\n",
67+
strerror(errno));
68+
continue;
69+
}
70+
if (CHECK(err, "lookup_elem(src)", "%s\n", strerror(errno)))
71+
continue;
72+
73+
err = bpf_map_lookup_elem(dst_fd, &i, &dst_cookie);
74+
if (CHECK(err, "lookup_elem(dst)", "%s\n", strerror(errno)))
75+
continue;
76+
77+
CHECK(dst_cookie != src_cookie, "cookie mismatch",
78+
"%llu != %llu (pos %u)\n", dst_cookie, src_cookie, i);
79+
}
80+
}
81+
5382
/* Create a map, populate it with one socket, and free the map. */
5483
static void test_sockmap_create_update_free(enum bpf_map_type map_type)
5584
{
@@ -109,9 +138,9 @@ static void test_skmsg_helpers(enum bpf_map_type map_type)
109138
static void test_sockmap_update(enum bpf_map_type map_type)
110139
{
111140
struct bpf_prog_test_run_attr tattr;
112-
int err, prog, src, dst, duration = 0;
141+
int err, prog, src, duration = 0;
113142
struct test_sockmap_update *skel;
114-
__u64 src_cookie, dst_cookie;
143+
struct bpf_map *dst_map;
115144
const __u32 zero = 0;
116145
char dummy[14] = {0};
117146
__s64 sk;
@@ -127,18 +156,14 @@ static void test_sockmap_update(enum bpf_map_type map_type)
127156
prog = bpf_program__fd(skel->progs.copy_sock_map);
128157
src = bpf_map__fd(skel->maps.src);
129158
if (map_type == BPF_MAP_TYPE_SOCKMAP)
130-
dst = bpf_map__fd(skel->maps.dst_sock_map);
159+
dst_map = skel->maps.dst_sock_map;
131160
else
132-
dst = bpf_map__fd(skel->maps.dst_sock_hash);
161+
dst_map = skel->maps.dst_sock_hash;
133162

134163
err = bpf_map_update_elem(src, &zero, &sk, BPF_NOEXIST);
135164
if (CHECK(err, "update_elem(src)", "errno=%u\n", errno))
136165
goto out;
137166

138-
err = bpf_map_lookup_elem(src, &zero, &src_cookie);
139-
if (CHECK(err, "lookup_elem(src, cookie)", "errno=%u\n", errno))
140-
goto out;
141-
142167
tattr = (struct bpf_prog_test_run_attr){
143168
.prog_fd = prog,
144169
.repeat = 1,
@@ -151,12 +176,7 @@ static void test_sockmap_update(enum bpf_map_type map_type)
151176
"errno=%u retval=%u\n", errno, tattr.retval))
152177
goto out;
153178

154-
err = bpf_map_lookup_elem(dst, &zero, &dst_cookie);
155-
if (CHECK(err, "lookup_elem(dst, cookie)", "errno=%u\n", errno))
156-
goto out;
157-
158-
CHECK(dst_cookie != src_cookie, "cookie mismatch", "%llu != %llu\n",
159-
dst_cookie, src_cookie);
179+
compare_cookies(skel->maps.src, dst_map);
160180

161181
out:
162182
test_sockmap_update__destroy(skel);
@@ -174,14 +194,14 @@ static void test_sockmap_invalid_update(void)
174194
test_sockmap_invalid_update__destroy(skel);
175195
}
176196

177-
static void test_sockmap_iter(enum bpf_map_type map_type)
197+
static void test_sockmap_copy(enum bpf_map_type map_type)
178198
{
179199
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
180200
int err, len, src_fd, iter_fd, duration = 0;
181201
union bpf_iter_link_info linfo = {0};
182-
__s64 sock_fd[SOCKMAP_MAX_ENTRIES];
183-
__u32 i, num_sockets, max_elems;
202+
__u32 i, num_sockets, num_elems;
184203
struct bpf_iter_sockmap *skel;
204+
__s64 *sock_fd = NULL;
185205
struct bpf_link *link;
186206
struct bpf_map *src;
187207
char buf[64];
@@ -190,22 +210,23 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
190210
if (CHECK(!skel, "bpf_iter_sockmap__open_and_load", "skeleton open_and_load failed\n"))
191211
return;
192212

193-
for (i = 0; i < ARRAY_SIZE(sock_fd); i++)
194-
sock_fd[i] = -1;
195-
196-
/* Make sure we have at least one "empty" entry to test iteration of
197-
* an empty slot.
198-
*/
199-
num_sockets = ARRAY_SIZE(sock_fd) - 1;
200-
201213
if (map_type == BPF_MAP_TYPE_SOCKMAP) {
202214
src = skel->maps.sockmap;
203-
max_elems = bpf_map__max_entries(src);
215+
num_elems = bpf_map__max_entries(src);
216+
num_sockets = num_elems - 1;
204217
} else {
205218
src = skel->maps.sockhash;
206-
max_elems = num_sockets;
219+
num_elems = bpf_map__max_entries(src) - 1;
220+
num_sockets = num_elems;
207221
}
208222

223+
sock_fd = calloc(num_sockets, sizeof(*sock_fd));
224+
if (CHECK(!sock_fd, "calloc(sock_fd)", "failed to allocate\n"))
225+
goto out;
226+
227+
for (i = 0; i < num_sockets; i++)
228+
sock_fd[i] = -1;
229+
209230
src_fd = bpf_map__fd(src);
210231

211232
for (i = 0; i < num_sockets; i++) {
@@ -221,7 +242,7 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
221242
linfo.map.map_fd = src_fd;
222243
opts.link_info = &linfo;
223244
opts.link_info_len = sizeof(linfo);
224-
link = bpf_program__attach_iter(skel->progs.count_elems, &opts);
245+
link = bpf_program__attach_iter(skel->progs.copy, &opts);
225246
if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n"))
226247
goto out;
227248

@@ -236,23 +257,26 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
236257
goto close_iter;
237258

238259
/* test results */
239-
if (CHECK(skel->bss->elems != max_elems, "elems", "got %u expected %u\n",
240-
skel->bss->elems, max_elems))
260+
if (CHECK(skel->bss->elems != num_elems, "elems", "got %u expected %u\n",
261+
skel->bss->elems, num_elems))
241262
goto close_iter;
242263

243264
if (CHECK(skel->bss->socks != num_sockets, "socks", "got %u expected %u\n",
244265
skel->bss->socks, num_sockets))
245266
goto close_iter;
246267

268+
compare_cookies(src, skel->maps.dst);
269+
247270
close_iter:
248271
close(iter_fd);
249272
free_link:
250273
bpf_link__destroy(link);
251274
out:
252-
for (i = 0; i < num_sockets; i++) {
275+
for (i = 0; sock_fd && i < num_sockets; i++)
253276
if (sock_fd[i] >= 0)
254277
close(sock_fd[i]);
255-
}
278+
if (sock_fd)
279+
free(sock_fd);
256280
bpf_iter_sockmap__destroy(skel);
257281
}
258282

@@ -272,8 +296,8 @@ void test_sockmap_basic(void)
272296
test_sockmap_update(BPF_MAP_TYPE_SOCKHASH);
273297
if (test__start_subtest("sockmap update in unsafe context"))
274298
test_sockmap_invalid_update();
275-
if (test__start_subtest("sockmap iter"))
276-
test_sockmap_iter(BPF_MAP_TYPE_SOCKMAP);
277-
if (test__start_subtest("sockhash iter"))
278-
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);
279303
}

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* Copyright (c) 2020 Cloudflare */
33
#include "bpf_iter.h"
44
#include "bpf_tracing_net.h"
5-
#include "bpf_iter_sockmap.h"
65
#include <bpf/bpf_helpers.h>
76
#include <bpf/bpf_tracing.h>
87
#include <errno.h>
@@ -11,33 +10,50 @@ char _license[] SEC("license") = "GPL";
1110

1211
struct {
1312
__uint(type, BPF_MAP_TYPE_SOCKMAP);
14-
__uint(max_entries, SOCKMAP_MAX_ENTRIES);
13+
__uint(max_entries, 64);
1514
__type(key, __u32);
1615
__type(value, __u64);
1716
} sockmap SEC(".maps");
1817

1918
struct {
2019
__uint(type, BPF_MAP_TYPE_SOCKHASH);
21-
__uint(max_entries, SOCKMAP_MAX_ENTRIES);
20+
__uint(max_entries, 64);
2221
__type(key, __u32);
2322
__type(value, __u64);
2423
} sockhash SEC(".maps");
2524

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+
2632
__u32 elems = 0;
2733
__u32 socks = 0;
2834

2935
SEC("iter/sockmap")
30-
int count_elems(struct bpf_iter__sockmap *ctx)
36+
int copy(struct bpf_iter__sockmap *ctx)
3137
{
3238
struct sock *sk = ctx->sk;
3339
__u32 tmp, *key = ctx->key;
3440
int ret;
3541

36-
if (key)
37-
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;
3851

39-
if (sk)
52+
if (sk) {
4053
socks++;
54+
return bpf_map_update_elem(&dst, &tmp, sk, 0) != 0;
55+
}
4156

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

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)