Commit 26c3270d authored by Lorenz Bauer's avatar Lorenz Bauer Committed by Alexei Starovoitov

selftests: bpf: Add helper to compare socket cookies

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: default avatarLorenz Bauer <lmb@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200928090805.23343-3-lmb@cloudflare.com
parent 6550f2dd
...@@ -50,6 +50,37 @@ static int connected_socket_v4(void) ...@@ -50,6 +50,37 @@ static int connected_socket_v4(void)
return -1; return -1;
} }
static void compare_cookies(struct bpf_map *src, struct bpf_map *dst)
{
__u32 i, max_entries = bpf_map__max_entries(src);
int err, duration = 0, src_fd, dst_fd;
src_fd = bpf_map__fd(src);
dst_fd = bpf_map__fd(dst);
for (i = 0; i < max_entries; i++) {
__u64 src_cookie, dst_cookie;
err = bpf_map_lookup_elem(src_fd, &i, &src_cookie);
if (err && errno == ENOENT) {
err = bpf_map_lookup_elem(dst_fd, &i, &dst_cookie);
CHECK(!err, "map_lookup_elem(dst)", "element %u not deleted\n", i);
CHECK(err && errno != ENOENT, "map_lookup_elem(dst)", "%s\n",
strerror(errno));
continue;
}
if (CHECK(err, "lookup_elem(src)", "%s\n", strerror(errno)))
continue;
err = bpf_map_lookup_elem(dst_fd, &i, &dst_cookie);
if (CHECK(err, "lookup_elem(dst)", "%s\n", strerror(errno)))
continue;
CHECK(dst_cookie != src_cookie, "cookie mismatch",
"%llu != %llu (pos %u)\n", dst_cookie, src_cookie, i);
}
}
/* Create a map, populate it with one socket, and free the map. */ /* Create a map, populate it with one socket, and free the map. */
static void test_sockmap_create_update_free(enum bpf_map_type map_type) static void test_sockmap_create_update_free(enum bpf_map_type map_type)
{ {
...@@ -109,9 +140,9 @@ static void test_skmsg_helpers(enum bpf_map_type map_type) ...@@ -109,9 +140,9 @@ static void test_skmsg_helpers(enum bpf_map_type map_type)
static void test_sockmap_update(enum bpf_map_type map_type) static void test_sockmap_update(enum bpf_map_type map_type)
{ {
struct bpf_prog_test_run_attr tattr; struct bpf_prog_test_run_attr tattr;
int err, prog, src, dst, duration = 0; int err, prog, src, duration = 0;
struct test_sockmap_update *skel; struct test_sockmap_update *skel;
__u64 src_cookie, dst_cookie; struct bpf_map *dst_map;
const __u32 zero = 0; const __u32 zero = 0;
char dummy[14] = {0}; char dummy[14] = {0};
__s64 sk; __s64 sk;
...@@ -127,18 +158,14 @@ static void test_sockmap_update(enum bpf_map_type map_type) ...@@ -127,18 +158,14 @@ static void test_sockmap_update(enum bpf_map_type map_type)
prog = bpf_program__fd(skel->progs.copy_sock_map); prog = bpf_program__fd(skel->progs.copy_sock_map);
src = bpf_map__fd(skel->maps.src); src = bpf_map__fd(skel->maps.src);
if (map_type == BPF_MAP_TYPE_SOCKMAP) if (map_type == BPF_MAP_TYPE_SOCKMAP)
dst = bpf_map__fd(skel->maps.dst_sock_map); dst_map = skel->maps.dst_sock_map;
else else
dst = bpf_map__fd(skel->maps.dst_sock_hash); dst_map = skel->maps.dst_sock_hash;
err = bpf_map_update_elem(src, &zero, &sk, BPF_NOEXIST); err = bpf_map_update_elem(src, &zero, &sk, BPF_NOEXIST);
if (CHECK(err, "update_elem(src)", "errno=%u\n", errno)) if (CHECK(err, "update_elem(src)", "errno=%u\n", errno))
goto out; goto out;
err = bpf_map_lookup_elem(src, &zero, &src_cookie);
if (CHECK(err, "lookup_elem(src, cookie)", "errno=%u\n", errno))
goto out;
tattr = (struct bpf_prog_test_run_attr){ tattr = (struct bpf_prog_test_run_attr){
.prog_fd = prog, .prog_fd = prog,
.repeat = 1, .repeat = 1,
...@@ -151,12 +178,7 @@ static void test_sockmap_update(enum bpf_map_type map_type) ...@@ -151,12 +178,7 @@ static void test_sockmap_update(enum bpf_map_type map_type)
"errno=%u retval=%u\n", errno, tattr.retval)) "errno=%u retval=%u\n", errno, tattr.retval))
goto out; goto out;
err = bpf_map_lookup_elem(dst, &zero, &dst_cookie); compare_cookies(skel->maps.src, dst_map);
if (CHECK(err, "lookup_elem(dst, cookie)", "errno=%u\n", errno))
goto out;
CHECK(dst_cookie != src_cookie, "cookie mismatch", "%llu != %llu\n",
dst_cookie, src_cookie);
out: out:
test_sockmap_update__destroy(skel); test_sockmap_update__destroy(skel);
......
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