Skip to content

Commit fc3533e

Browse files
lmbtsipa
authored andcommitted
We compare socket cookies to ensure that insertion into a sockmap worked.
Pull this out into a helper function for use in other tests. Signed-off-by: Lorenz Bauer <[email protected]> --- .../selftests/bpf/prog_tests/sockmap_basic.c | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-)
1 parent cec8a80 commit fc3533e

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

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

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ static int connected_socket_v4(void)
4747
return -1;
4848
}
4949

50+
static void compare_cookies(struct bpf_map *src, struct bpf_map *dst)
51+
{
52+
__u32 i, max_entries = bpf_map__max_entries(src);
53+
int err, duration, src_fd, dst_fd;
54+
55+
src_fd = bpf_map__fd(src);
56+
dst_fd = bpf_map__fd(dst);
57+
58+
for (i = 0; i < max_entries; i++) {
59+
__u64 src_cookie, dst_cookie;
60+
61+
err = bpf_map_lookup_elem(src_fd, &i, &src_cookie);
62+
if (err && errno == ENOENT) {
63+
err = bpf_map_lookup_elem(dst_fd, &i, &dst_cookie);
64+
CHECK(!err, "map_lookup_elem(dst)", "element %u not deleted\n", i);
65+
CHECK(err && errno != ENOENT, "map_lookup_elem(dst)", "%s\n",
66+
strerror(errno));
67+
continue;
68+
}
69+
if (CHECK(err, "lookup_elem(src)", "%s\n", strerror(errno)))
70+
continue;
71+
72+
err = bpf_map_lookup_elem(dst_fd, &i, &dst_cookie);
73+
if (CHECK(err, "lookup_elem(dst)", "%s\n", strerror(errno)))
74+
continue;
75+
76+
CHECK(dst_cookie != src_cookie, "cookie mismatch",
77+
"%llu != %llu (pos %u)\n", dst_cookie, src_cookie, i);
78+
}
79+
}
80+
5081
/* Create a map, populate it with one socket, and free the map. */
5182
static void test_sockmap_create_update_free(enum bpf_map_type map_type)
5283
{
@@ -106,9 +137,9 @@ static void test_skmsg_helpers(enum bpf_map_type map_type)
106137
static void test_sockmap_update(enum bpf_map_type map_type)
107138
{
108139
struct bpf_prog_test_run_attr tattr;
109-
int err, prog, src, dst, duration = 0;
140+
int err, prog, src, duration = 0;
110141
struct test_sockmap_update *skel;
111-
__u64 src_cookie, dst_cookie;
142+
struct bpf_map *dst_map;
112143
const __u32 zero = 0;
113144
char dummy[14] = {0};
114145
__s64 sk;
@@ -124,18 +155,14 @@ static void test_sockmap_update(enum bpf_map_type map_type)
124155
prog = bpf_program__fd(skel->progs.copy_sock_map);
125156
src = bpf_map__fd(skel->maps.src);
126157
if (map_type == BPF_MAP_TYPE_SOCKMAP)
127-
dst = bpf_map__fd(skel->maps.dst_sock_map);
158+
dst_map = skel->maps.dst_sock_map;
128159
else
129-
dst = bpf_map__fd(skel->maps.dst_sock_hash);
160+
dst_map = skel->maps.dst_sock_hash;
130161

131162
err = bpf_map_update_elem(src, &zero, &sk, BPF_NOEXIST);
132163
if (CHECK(err, "update_elem(src)", "errno=%u\n", errno))
133164
goto out;
134165

135-
err = bpf_map_lookup_elem(src, &zero, &src_cookie);
136-
if (CHECK(err, "lookup_elem(src, cookie)", "errno=%u\n", errno))
137-
goto out;
138-
139166
tattr = (struct bpf_prog_test_run_attr){
140167
.prog_fd = prog,
141168
.repeat = 1,
@@ -148,12 +175,7 @@ static void test_sockmap_update(enum bpf_map_type map_type)
148175
"errno=%u retval=%u\n", errno, tattr.retval))
149176
goto out;
150177

151-
err = bpf_map_lookup_elem(dst, &zero, &dst_cookie);
152-
if (CHECK(err, "lookup_elem(dst, cookie)", "errno=%u\n", errno))
153-
goto out;
154-
155-
CHECK(dst_cookie != src_cookie, "cookie mismatch", "%llu != %llu\n",
156-
dst_cookie, src_cookie);
178+
compare_cookies(skel->maps.src, dst_map);
157179

158180
out:
159181
test_sockmap_update__destroy(skel);

0 commit comments

Comments
 (0)