Commit 3cbbf919 authored by Andrii Nakryiko's avatar Andrii Nakryiko

Merge branch 'selftests-bpf-update-multiple-prog_tests-to-use-assert_-macros'

Yuran Pereira says:

====================
selftests/bpf: Update multiple prog_tests to use ASSERT_ macros

Multiple files/programs in `tools/testing/selftests/bpf/prog_tests/` still
heavily use the `CHECK` macro, even when better `ASSERT_` alternatives are
available.

As it was already pointed out by Yonghong Song [1] in the bpf selftests the use
of the ASSERT_* series of macros is preferred over the CHECK macro.

This patchset replaces the usage of `CHECK(` macros to the equivalent `ASSERT_`
family of macros in the following prog_tests:
- bind_perm.c
- bpf_obj_id.c
- bpf_tcp_ca.c
- vmlinux.c

[1] https://lore.kernel.org/lkml/0a142924-633c-44e6-9a92-2dc019656bf2@linux.dev

Changes in v3:
- Addressed the following points mentioned by Yonghong Song
- Improved `bpf_map_lookup_elem` assertion in bpf_tcp_ca.
- Replaced assertion introduced in v2 with one that checks `thread_ret`
  instead of `pthread_join`. This ensures that `server`'s return value
  (thread_ret) is the one being checked, as oposed to `pthread_join`'s
  return value, since the latter one is less likely to fail.

Changes in v2:
- Fixed pthread_join assertion that broke the previous test

Previous version:
v2 - https://lore.kernel.org/lkml/GV1PR10MB6563AECF8E94798A1E5B36A4E8B6A@GV1PR10MB6563.EURPRD10.PROD.OUTLOOK.COM
v1 - https://lore.kernel.org/lkml/GV1PR10MB6563FCFF1C5DEBE84FEA985FE8B0A@GV1PR10MB6563.EURPRD10.PROD.OUTLOOK.COM
====================

Link: https://lore.kernel.org/r/GV1PR10MB6563BEFEA4269E1DDBC264B1E8BBA@GV1PR10MB6563.EURPRD10.PROD.OUTLOOK.COMSigned-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents 57b97ecb 3ece0e85
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include "cap_helpers.h" #include "cap_helpers.h"
#include "bind_perm.skel.h" #include "bind_perm.skel.h"
static int duration;
static int create_netns(void) static int create_netns(void)
{ {
if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns")) if (!ASSERT_OK(unshare(CLONE_NEWNET), "create netns"))
...@@ -27,7 +25,7 @@ void try_bind(int family, int port, int expected_errno) ...@@ -27,7 +25,7 @@ void try_bind(int family, int port, int expected_errno)
int fd = -1; int fd = -1;
fd = socket(family, SOCK_STREAM, 0); fd = socket(family, SOCK_STREAM, 0);
if (CHECK(fd < 0, "fd", "errno %d", errno)) if (!ASSERT_GE(fd, 0, "socket"))
goto close_socket; goto close_socket;
if (family == AF_INET) { if (family == AF_INET) {
...@@ -60,7 +58,7 @@ void test_bind_perm(void) ...@@ -60,7 +58,7 @@ void test_bind_perm(void)
return; return;
cgroup_fd = test__join_cgroup("/bind_perm"); cgroup_fd = test__join_cgroup("/bind_perm");
if (CHECK(cgroup_fd < 0, "cg-join", "errno %d", errno)) if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
return; return;
skel = bind_perm__open_and_load(); skel = bind_perm__open_and_load();
......
...@@ -25,7 +25,7 @@ void serial_test_bpf_obj_id(void) ...@@ -25,7 +25,7 @@ void serial_test_bpf_obj_id(void)
*/ */
__u32 map_ids[nr_iters + 1]; __u32 map_ids[nr_iters + 1];
char jited_insns[128], xlated_insns[128], zeros[128], tp_name[128]; char jited_insns[128], xlated_insns[128], zeros[128], tp_name[128];
__u32 i, next_id, info_len, nr_id_found, duration = 0; __u32 i, next_id, info_len, nr_id_found;
struct timespec real_time_ts, boot_time_ts; struct timespec real_time_ts, boot_time_ts;
int err = 0; int err = 0;
__u64 array_value; __u64 array_value;
...@@ -33,16 +33,16 @@ void serial_test_bpf_obj_id(void) ...@@ -33,16 +33,16 @@ void serial_test_bpf_obj_id(void)
time_t now, load_time; time_t now, load_time;
err = bpf_prog_get_fd_by_id(0); err = bpf_prog_get_fd_by_id(0);
CHECK(err >= 0 || errno != ENOENT, ASSERT_LT(err, 0, "bpf_prog_get_fd_by_id");
"get-fd-by-notexist-prog-id", "err %d errno %d\n", err, errno); ASSERT_EQ(errno, ENOENT, "bpf_prog_get_fd_by_id");
err = bpf_map_get_fd_by_id(0); err = bpf_map_get_fd_by_id(0);
CHECK(err >= 0 || errno != ENOENT, ASSERT_LT(err, 0, "bpf_map_get_fd_by_id");
"get-fd-by-notexist-map-id", "err %d errno %d\n", err, errno); ASSERT_EQ(errno, ENOENT, "bpf_map_get_fd_by_id");
err = bpf_link_get_fd_by_id(0); err = bpf_link_get_fd_by_id(0);
CHECK(err >= 0 || errno != ENOENT, ASSERT_LT(err, 0, "bpf_map_get_fd_by_id");
"get-fd-by-notexist-link-id", "err %d errno %d\n", err, errno); ASSERT_EQ(errno, ENOENT, "bpf_map_get_fd_by_id");
/* Check bpf_map_get_info_by_fd() */ /* Check bpf_map_get_info_by_fd() */
bzero(zeros, sizeof(zeros)); bzero(zeros, sizeof(zeros));
...@@ -53,25 +53,26 @@ void serial_test_bpf_obj_id(void) ...@@ -53,25 +53,26 @@ void serial_test_bpf_obj_id(void)
/* test_obj_id.o is a dumb prog. It should never fail /* test_obj_id.o is a dumb prog. It should never fail
* to load. * to load.
*/ */
if (CHECK_FAIL(err)) if (!ASSERT_OK(err, "bpf_prog_test_load"))
continue; continue;
/* Insert a magic value to the map */ /* Insert a magic value to the map */
map_fds[i] = bpf_find_map(__func__, objs[i], "test_map_id"); map_fds[i] = bpf_find_map(__func__, objs[i], "test_map_id");
if (CHECK_FAIL(map_fds[i] < 0)) if (!ASSERT_GE(map_fds[i], 0, "bpf_find_map"))
goto done; goto done;
err = bpf_map_update_elem(map_fds[i], &array_key, err = bpf_map_update_elem(map_fds[i], &array_key,
&array_magic_value, 0); &array_magic_value, 0);
if (CHECK_FAIL(err)) if (!ASSERT_OK(err, "bpf_map_update_elem"))
goto done; goto done;
prog = bpf_object__find_program_by_name(objs[i], prog = bpf_object__find_program_by_name(objs[i], "test_obj_id");
"test_obj_id"); if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
if (CHECK_FAIL(!prog))
goto done; goto done;
links[i] = bpf_program__attach(prog); links[i] = bpf_program__attach(prog);
err = libbpf_get_error(links[i]); err = libbpf_get_error(links[i]);
if (CHECK(err, "prog_attach", "prog #%d, err %d\n", i, err)) { if (!ASSERT_OK(err, "bpf_program__attach")) {
links[i] = NULL; links[i] = NULL;
goto done; goto done;
} }
...@@ -81,24 +82,14 @@ void serial_test_bpf_obj_id(void) ...@@ -81,24 +82,14 @@ void serial_test_bpf_obj_id(void)
bzero(&map_infos[i], info_len); bzero(&map_infos[i], info_len);
err = bpf_map_get_info_by_fd(map_fds[i], &map_infos[i], err = bpf_map_get_info_by_fd(map_fds[i], &map_infos[i],
&info_len); &info_len);
if (CHECK(err || if (!ASSERT_OK(err, "bpf_map_get_info_by_fd") ||
map_infos[i].type != BPF_MAP_TYPE_ARRAY || !ASSERT_EQ(map_infos[i].type, BPF_MAP_TYPE_ARRAY, "map_type") ||
map_infos[i].key_size != sizeof(__u32) || !ASSERT_EQ(map_infos[i].key_size, sizeof(__u32), "key_size") ||
map_infos[i].value_size != sizeof(__u64) || !ASSERT_EQ(map_infos[i].value_size, sizeof(__u64), "value_size") ||
map_infos[i].max_entries != 1 || !ASSERT_EQ(map_infos[i].max_entries, 1, "max_entries") ||
map_infos[i].map_flags != 0 || !ASSERT_EQ(map_infos[i].map_flags, 0, "map_flags") ||
info_len != sizeof(struct bpf_map_info) || !ASSERT_EQ(info_len, sizeof(struct bpf_map_info), "map_info_len") ||
strcmp((char *)map_infos[i].name, expected_map_name), !ASSERT_STREQ((char *)map_infos[i].name, expected_map_name, "map_name"))
"get-map-info(fd)",
"err %d errno %d type %d(%d) info_len %u(%zu) key_size %u value_size %u max_entries %u map_flags %X name %s(%s)\n",
err, errno,
map_infos[i].type, BPF_MAP_TYPE_ARRAY,
info_len, sizeof(struct bpf_map_info),
map_infos[i].key_size,
map_infos[i].value_size,
map_infos[i].max_entries,
map_infos[i].map_flags,
map_infos[i].name, expected_map_name))
goto done; goto done;
/* Check getting prog info */ /* Check getting prog info */
...@@ -112,48 +103,34 @@ void serial_test_bpf_obj_id(void) ...@@ -112,48 +103,34 @@ void serial_test_bpf_obj_id(void)
prog_infos[i].xlated_prog_len = sizeof(xlated_insns); prog_infos[i].xlated_prog_len = sizeof(xlated_insns);
prog_infos[i].map_ids = ptr_to_u64(map_ids + i); prog_infos[i].map_ids = ptr_to_u64(map_ids + i);
prog_infos[i].nr_map_ids = 2; prog_infos[i].nr_map_ids = 2;
err = clock_gettime(CLOCK_REALTIME, &real_time_ts); err = clock_gettime(CLOCK_REALTIME, &real_time_ts);
if (CHECK_FAIL(err)) if (!ASSERT_OK(err, "clock_gettime"))
goto done; goto done;
err = clock_gettime(CLOCK_BOOTTIME, &boot_time_ts); err = clock_gettime(CLOCK_BOOTTIME, &boot_time_ts);
if (CHECK_FAIL(err)) if (!ASSERT_OK(err, "clock_gettime"))
goto done; goto done;
err = bpf_prog_get_info_by_fd(prog_fds[i], &prog_infos[i], err = bpf_prog_get_info_by_fd(prog_fds[i], &prog_infos[i],
&info_len); &info_len);
load_time = (real_time_ts.tv_sec - boot_time_ts.tv_sec) load_time = (real_time_ts.tv_sec - boot_time_ts.tv_sec)
+ (prog_infos[i].load_time / nsec_per_sec); + (prog_infos[i].load_time / nsec_per_sec);
if (CHECK(err ||
prog_infos[i].type != BPF_PROG_TYPE_RAW_TRACEPOINT || if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd") ||
info_len != sizeof(struct bpf_prog_info) || !ASSERT_EQ(prog_infos[i].type, BPF_PROG_TYPE_RAW_TRACEPOINT, "prog_type") ||
(env.jit_enabled && !prog_infos[i].jited_prog_len) || !ASSERT_EQ(info_len, sizeof(struct bpf_prog_info), "prog_info_len") ||
(env.jit_enabled && !ASSERT_FALSE((env.jit_enabled && !prog_infos[i].jited_prog_len), "jited_prog_len") ||
!memcmp(jited_insns, zeros, sizeof(zeros))) || !ASSERT_FALSE((env.jit_enabled && !memcmp(jited_insns, zeros, sizeof(zeros))),
!prog_infos[i].xlated_prog_len || "jited_insns") ||
!memcmp(xlated_insns, zeros, sizeof(zeros)) || !ASSERT_NEQ(prog_infos[i].xlated_prog_len, 0, "xlated_prog_len") ||
load_time < now - 60 || load_time > now + 60 || !ASSERT_NEQ(memcmp(xlated_insns, zeros, sizeof(zeros)), 0, "xlated_insns") ||
prog_infos[i].created_by_uid != my_uid || !ASSERT_GE(load_time, (now - 60), "load_time") ||
prog_infos[i].nr_map_ids != 1 || !ASSERT_LE(load_time, (now + 60), "load_time") ||
*(int *)(long)prog_infos[i].map_ids != map_infos[i].id || !ASSERT_EQ(prog_infos[i].created_by_uid, my_uid, "created_by_uid") ||
strcmp((char *)prog_infos[i].name, expected_prog_name), !ASSERT_EQ(prog_infos[i].nr_map_ids, 1, "nr_map_ids") ||
"get-prog-info(fd)", !ASSERT_EQ(*(int *)(long)prog_infos[i].map_ids, map_infos[i].id, "map_ids") ||
"err %d errno %d i %d type %d(%d) info_len %u(%zu) " !ASSERT_STREQ((char *)prog_infos[i].name, expected_prog_name, "prog_name"))
"jit_enabled %d jited_prog_len %u xlated_prog_len %u "
"jited_prog %d xlated_prog %d load_time %lu(%lu) "
"uid %u(%u) nr_map_ids %u(%u) map_id %u(%u) "
"name %s(%s)\n",
err, errno, i,
prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER,
info_len, sizeof(struct bpf_prog_info),
env.jit_enabled,
prog_infos[i].jited_prog_len,
prog_infos[i].xlated_prog_len,
!!memcmp(jited_insns, zeros, sizeof(zeros)),
!!memcmp(xlated_insns, zeros, sizeof(zeros)),
load_time, now,
prog_infos[i].created_by_uid, my_uid,
prog_infos[i].nr_map_ids, 1,
*(int *)(long)prog_infos[i].map_ids, map_infos[i].id,
prog_infos[i].name, expected_prog_name))
goto done; goto done;
/* Check getting link info */ /* Check getting link info */
...@@ -163,25 +140,12 @@ void serial_test_bpf_obj_id(void) ...@@ -163,25 +140,12 @@ void serial_test_bpf_obj_id(void)
link_infos[i].raw_tracepoint.tp_name_len = sizeof(tp_name); link_infos[i].raw_tracepoint.tp_name_len = sizeof(tp_name);
err = bpf_link_get_info_by_fd(bpf_link__fd(links[i]), err = bpf_link_get_info_by_fd(bpf_link__fd(links[i]),
&link_infos[i], &info_len); &link_infos[i], &info_len);
if (CHECK(err || if (!ASSERT_OK(err, "bpf_link_get_info_by_fd") ||
link_infos[i].type != BPF_LINK_TYPE_RAW_TRACEPOINT || !ASSERT_EQ(link_infos[i].type, BPF_LINK_TYPE_RAW_TRACEPOINT, "link_type") ||
link_infos[i].prog_id != prog_infos[i].id || !ASSERT_EQ(link_infos[i].prog_id, prog_infos[i].id, "prog_id") ||
link_infos[i].raw_tracepoint.tp_name != ptr_to_u64(&tp_name) || !ASSERT_EQ(link_infos[i].raw_tracepoint.tp_name, ptr_to_u64(&tp_name), "&tp_name") ||
strcmp(u64_to_ptr(link_infos[i].raw_tracepoint.tp_name), !ASSERT_STREQ(u64_to_ptr(link_infos[i].raw_tracepoint.tp_name), "sys_enter", "tp_name"))
"sys_enter") ||
info_len != sizeof(struct bpf_link_info),
"get-link-info(fd)",
"err %d errno %d info_len %u(%zu) type %d(%d) id %d "
"prog_id %d (%d) tp_name %s(%s)\n",
err, errno,
info_len, sizeof(struct bpf_link_info),
link_infos[i].type, BPF_LINK_TYPE_RAW_TRACEPOINT,
link_infos[i].id,
link_infos[i].prog_id, prog_infos[i].id,
(const char *)u64_to_ptr(link_infos[i].raw_tracepoint.tp_name),
"sys_enter"))
goto done; goto done;
} }
/* Check bpf_prog_get_next_id() */ /* Check bpf_prog_get_next_id() */
...@@ -190,7 +154,7 @@ void serial_test_bpf_obj_id(void) ...@@ -190,7 +154,7 @@ void serial_test_bpf_obj_id(void)
while (!bpf_prog_get_next_id(next_id, &next_id)) { while (!bpf_prog_get_next_id(next_id, &next_id)) {
struct bpf_prog_info prog_info = {}; struct bpf_prog_info prog_info = {};
__u32 saved_map_id; __u32 saved_map_id;
int prog_fd; int prog_fd, cmp_res;
info_len = sizeof(prog_info); info_len = sizeof(prog_info);
...@@ -198,9 +162,7 @@ void serial_test_bpf_obj_id(void) ...@@ -198,9 +162,7 @@ void serial_test_bpf_obj_id(void)
if (prog_fd < 0 && errno == ENOENT) if (prog_fd < 0 && errno == ENOENT)
/* The bpf_prog is in the dead row */ /* The bpf_prog is in the dead row */
continue; continue;
if (CHECK(prog_fd < 0, "get-prog-fd(next_id)", if (!ASSERT_GE(prog_fd, 0, "bpf_prog_get_fd_by_id"))
"prog_fd %d next_id %d errno %d\n",
prog_fd, next_id, errno))
break; break;
for (i = 0; i < nr_iters; i++) for (i = 0; i < nr_iters; i++)
...@@ -218,9 +180,8 @@ void serial_test_bpf_obj_id(void) ...@@ -218,9 +180,8 @@ void serial_test_bpf_obj_id(void)
*/ */
prog_info.nr_map_ids = 1; prog_info.nr_map_ids = 1;
err = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &info_len); err = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &info_len);
if (CHECK(!err || errno != EFAULT, if (!ASSERT_ERR(err, "bpf_prog_get_info_by_fd") ||
"get-prog-fd-bad-nr-map-ids", "err %d errno %d(%d)", !ASSERT_EQ(errno, EFAULT, "bpf_prog_get_info_by_fd"))
err, errno, EFAULT))
break; break;
bzero(&prog_info, sizeof(prog_info)); bzero(&prog_info, sizeof(prog_info));
info_len = sizeof(prog_info); info_len = sizeof(prog_info);
...@@ -231,27 +192,22 @@ void serial_test_bpf_obj_id(void) ...@@ -231,27 +192,22 @@ void serial_test_bpf_obj_id(void)
err = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &info_len); err = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &info_len);
prog_infos[i].jited_prog_insns = 0; prog_infos[i].jited_prog_insns = 0;
prog_infos[i].xlated_prog_insns = 0; prog_infos[i].xlated_prog_insns = 0;
CHECK(err || info_len != sizeof(struct bpf_prog_info) || cmp_res = memcmp(&prog_info, &prog_infos[i], info_len);
memcmp(&prog_info, &prog_infos[i], info_len) ||
*(int *)(long)prog_info.map_ids != saved_map_id, ASSERT_OK(err, "bpf_prog_get_info_by_fd");
"get-prog-info(next_id->fd)", ASSERT_EQ(info_len, sizeof(struct bpf_prog_info), "prog_info_len");
"err %d errno %d info_len %u(%zu) memcmp %d map_id %u(%u)\n", ASSERT_OK(cmp_res, "memcmp");
err, errno, info_len, sizeof(struct bpf_prog_info), ASSERT_EQ(*(int *)(long)prog_info.map_ids, saved_map_id, "map_id");
memcmp(&prog_info, &prog_infos[i], info_len),
*(int *)(long)prog_info.map_ids, saved_map_id);
close(prog_fd); close(prog_fd);
} }
CHECK(nr_id_found != nr_iters, ASSERT_EQ(nr_id_found, nr_iters, "prog_nr_id_found");
"check total prog id found by get_next_id",
"nr_id_found %u(%u)\n",
nr_id_found, nr_iters);
/* Check bpf_map_get_next_id() */ /* Check bpf_map_get_next_id() */
nr_id_found = 0; nr_id_found = 0;
next_id = 0; next_id = 0;
while (!bpf_map_get_next_id(next_id, &next_id)) { while (!bpf_map_get_next_id(next_id, &next_id)) {
struct bpf_map_info map_info = {}; struct bpf_map_info map_info = {};
int map_fd; int map_fd, cmp_res;
info_len = sizeof(map_info); info_len = sizeof(map_info);
...@@ -259,9 +215,7 @@ void serial_test_bpf_obj_id(void) ...@@ -259,9 +215,7 @@ void serial_test_bpf_obj_id(void)
if (map_fd < 0 && errno == ENOENT) if (map_fd < 0 && errno == ENOENT)
/* The bpf_map is in the dead row */ /* The bpf_map is in the dead row */
continue; continue;
if (CHECK(map_fd < 0, "get-map-fd(next_id)", if (!ASSERT_GE(map_fd, 0, "bpf_map_get_fd_by_id"))
"map_fd %d next_id %u errno %d\n",
map_fd, next_id, errno))
break; break;
for (i = 0; i < nr_iters; i++) for (i = 0; i < nr_iters; i++)
...@@ -274,25 +228,19 @@ void serial_test_bpf_obj_id(void) ...@@ -274,25 +228,19 @@ void serial_test_bpf_obj_id(void)
nr_id_found++; nr_id_found++;
err = bpf_map_lookup_elem(map_fd, &array_key, &array_value); err = bpf_map_lookup_elem(map_fd, &array_key, &array_value);
if (CHECK_FAIL(err)) if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
goto done; goto done;
err = bpf_map_get_info_by_fd(map_fd, &map_info, &info_len); err = bpf_map_get_info_by_fd(map_fd, &map_info, &info_len);
CHECK(err || info_len != sizeof(struct bpf_map_info) || cmp_res = memcmp(&map_info, &map_infos[i], info_len);
memcmp(&map_info, &map_infos[i], info_len) || ASSERT_OK(err, "bpf_map_get_info_by_fd");
array_value != array_magic_value, ASSERT_EQ(info_len, sizeof(struct bpf_map_info), "info_len");
"check get-map-info(next_id->fd)", ASSERT_OK(cmp_res, "memcmp");
"err %d errno %d info_len %u(%zu) memcmp %d array_value %llu(%llu)\n", ASSERT_EQ(array_value, array_magic_value, "array_value");
err, errno, info_len, sizeof(struct bpf_map_info),
memcmp(&map_info, &map_infos[i], info_len),
array_value, array_magic_value);
close(map_fd); close(map_fd);
} }
CHECK(nr_id_found != nr_iters, ASSERT_EQ(nr_id_found, nr_iters, "map_nr_id_found");
"check total map id found by get_next_id",
"nr_id_found %u(%u)\n",
nr_id_found, nr_iters);
/* Check bpf_link_get_next_id() */ /* Check bpf_link_get_next_id() */
nr_id_found = 0; nr_id_found = 0;
...@@ -308,9 +256,7 @@ void serial_test_bpf_obj_id(void) ...@@ -308,9 +256,7 @@ void serial_test_bpf_obj_id(void)
if (link_fd < 0 && errno == ENOENT) if (link_fd < 0 && errno == ENOENT)
/* The bpf_link is in the dead row */ /* The bpf_link is in the dead row */
continue; continue;
if (CHECK(link_fd < 0, "get-link-fd(next_id)", if (!ASSERT_GE(link_fd, 0, "bpf_link_get_fd_by_id"))
"link_fd %d next_id %u errno %d\n",
link_fd, next_id, errno))
break; break;
for (i = 0; i < nr_iters; i++) for (i = 0; i < nr_iters; i++)
...@@ -325,17 +271,13 @@ void serial_test_bpf_obj_id(void) ...@@ -325,17 +271,13 @@ void serial_test_bpf_obj_id(void)
err = bpf_link_get_info_by_fd(link_fd, &link_info, &info_len); err = bpf_link_get_info_by_fd(link_fd, &link_info, &info_len);
cmp_res = memcmp(&link_info, &link_infos[i], cmp_res = memcmp(&link_info, &link_infos[i],
offsetof(struct bpf_link_info, raw_tracepoint)); offsetof(struct bpf_link_info, raw_tracepoint));
CHECK(err || info_len != sizeof(link_info) || cmp_res, ASSERT_OK(err, "bpf_link_get_info_by_fd");
"check get-link-info(next_id->fd)", ASSERT_EQ(info_len, sizeof(link_info), "info_len");
"err %d errno %d info_len %u(%zu) memcmp %d\n", ASSERT_OK(cmp_res, "memcmp");
err, errno, info_len, sizeof(struct bpf_link_info),
cmp_res);
close(link_fd); close(link_fd);
} }
CHECK(nr_id_found != nr_iters, ASSERT_EQ(nr_id_found, nr_iters, "link_nr_id_found");
"check total link id found by get_next_id",
"nr_id_found %u(%u)\n", nr_id_found, nr_iters);
done: done:
for (i = 0; i < nr_iters; i++) { for (i = 0; i < nr_iters; i++) {
......
...@@ -20,15 +20,14 @@ ...@@ -20,15 +20,14 @@
static const unsigned int total_bytes = 10 * 1024 * 1024; static const unsigned int total_bytes = 10 * 1024 * 1024;
static int expected_stg = 0xeB9F; static int expected_stg = 0xeB9F;
static int stop, duration; static int stop;
static int settcpca(int fd, const char *tcp_ca) static int settcpca(int fd, const char *tcp_ca)
{ {
int err; int err;
err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca)); err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca));
if (CHECK(err == -1, "setsockopt(fd, TCP_CONGESTION)", "errno:%d\n", if (!ASSERT_NEQ(err, -1, "setsockopt"))
errno))
return -1; return -1;
return 0; return 0;
...@@ -65,8 +64,7 @@ static void *server(void *arg) ...@@ -65,8 +64,7 @@ static void *server(void *arg)
bytes += nr_sent; bytes += nr_sent;
} }
CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n", ASSERT_EQ(bytes, total_bytes, "send");
bytes, total_bytes, nr_sent, errno);
done: done:
if (fd >= 0) if (fd >= 0)
...@@ -92,10 +90,11 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) ...@@ -92,10 +90,11 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
WRITE_ONCE(stop, 0); WRITE_ONCE(stop, 0);
lfd = socket(AF_INET6, SOCK_STREAM, 0); lfd = socket(AF_INET6, SOCK_STREAM, 0);
if (CHECK(lfd == -1, "socket", "errno:%d\n", errno)) if (!ASSERT_NEQ(lfd, -1, "socket"))
return; return;
fd = socket(AF_INET6, SOCK_STREAM, 0); fd = socket(AF_INET6, SOCK_STREAM, 0);
if (CHECK(fd == -1, "socket", "errno:%d\n", errno)) { if (!ASSERT_NEQ(fd, -1, "socket")) {
close(lfd); close(lfd);
return; return;
} }
...@@ -108,26 +107,27 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) ...@@ -108,26 +107,27 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
sa6.sin6_family = AF_INET6; sa6.sin6_family = AF_INET6;
sa6.sin6_addr = in6addr_loopback; sa6.sin6_addr = in6addr_loopback;
err = bind(lfd, (struct sockaddr *)&sa6, addrlen); err = bind(lfd, (struct sockaddr *)&sa6, addrlen);
if (CHECK(err == -1, "bind", "errno:%d\n", errno)) if (!ASSERT_NEQ(err, -1, "bind"))
goto done; goto done;
err = getsockname(lfd, (struct sockaddr *)&sa6, &addrlen); err = getsockname(lfd, (struct sockaddr *)&sa6, &addrlen);
if (CHECK(err == -1, "getsockname", "errno:%d\n", errno)) if (!ASSERT_NEQ(err, -1, "getsockname"))
goto done; goto done;
err = listen(lfd, 1); err = listen(lfd, 1);
if (CHECK(err == -1, "listen", "errno:%d\n", errno)) if (!ASSERT_NEQ(err, -1, "listen"))
goto done; goto done;
if (sk_stg_map) { if (sk_stg_map) {
err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd, err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
&expected_stg, BPF_NOEXIST); &expected_stg, BPF_NOEXIST);
if (CHECK(err, "bpf_map_update_elem(sk_stg_map)", if (!ASSERT_OK(err, "bpf_map_update_elem(sk_stg_map)"))
"err:%d errno:%d\n", err, errno))
goto done; goto done;
} }
/* connect to server */ /* connect to server */
err = connect(fd, (struct sockaddr *)&sa6, addrlen); err = connect(fd, (struct sockaddr *)&sa6, addrlen);
if (CHECK(err == -1, "connect", "errno:%d\n", errno)) if (!ASSERT_NEQ(err, -1, "connect"))
goto done; goto done;
if (sk_stg_map) { if (sk_stg_map) {
...@@ -135,14 +135,13 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) ...@@ -135,14 +135,13 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd, err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd,
&tmp_stg); &tmp_stg);
if (CHECK(!err || errno != ENOENT, if (!ASSERT_ERR(err, "bpf_map_lookup_elem(sk_stg_map)") ||
"bpf_map_lookup_elem(sk_stg_map)", !ASSERT_EQ(errno, ENOENT, "bpf_map_lookup_elem(sk_stg_map)"))
"err:%d errno:%d\n", err, errno))
goto done; goto done;
} }
err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd); err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd);
if (CHECK(err != 0, "pthread_create", "err:%d errno:%d\n", err, errno)) if (!ASSERT_OK(err, "pthread_create"))
goto done; goto done;
/* recv total_bytes */ /* recv total_bytes */
...@@ -156,13 +155,12 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) ...@@ -156,13 +155,12 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
bytes += nr_recv; bytes += nr_recv;
} }
CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n", ASSERT_EQ(bytes, total_bytes, "recv");
bytes, total_bytes, nr_recv, errno);
WRITE_ONCE(stop, 1); WRITE_ONCE(stop, 1);
pthread_join(srv_thread, &thread_ret); pthread_join(srv_thread, &thread_ret);
CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld", ASSERT_OK(IS_ERR(thread_ret), "thread_ret");
PTR_ERR(thread_ret));
done: done:
close(lfd); close(lfd);
close(fd); close(fd);
...@@ -174,7 +172,7 @@ static void test_cubic(void) ...@@ -174,7 +172,7 @@ static void test_cubic(void)
struct bpf_link *link; struct bpf_link *link;
cubic_skel = bpf_cubic__open_and_load(); cubic_skel = bpf_cubic__open_and_load();
if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n")) if (!ASSERT_OK_PTR(cubic_skel, "bpf_cubic__open_and_load"))
return; return;
link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic); link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
...@@ -197,7 +195,7 @@ static void test_dctcp(void) ...@@ -197,7 +195,7 @@ static void test_dctcp(void)
struct bpf_link *link; struct bpf_link *link;
dctcp_skel = bpf_dctcp__open_and_load(); dctcp_skel = bpf_dctcp__open_and_load();
if (CHECK(!dctcp_skel, "bpf_dctcp__open_and_load", "failed\n")) if (!ASSERT_OK_PTR(dctcp_skel, "bpf_dctcp__open_and_load"))
return; return;
link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp); link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
...@@ -207,9 +205,7 @@ static void test_dctcp(void) ...@@ -207,9 +205,7 @@ static void test_dctcp(void)
} }
do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map); do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
CHECK(dctcp_skel->bss->stg_result != expected_stg, ASSERT_EQ(dctcp_skel->bss->stg_result, expected_stg, "stg_result");
"Unexpected stg_result", "stg_result (%x) != expected_stg (%x)\n",
dctcp_skel->bss->stg_result, expected_stg);
bpf_link__destroy(link); bpf_link__destroy(link);
bpf_dctcp__destroy(dctcp_skel); bpf_dctcp__destroy(dctcp_skel);
......
...@@ -16,27 +16,27 @@ static void nsleep() ...@@ -16,27 +16,27 @@ static void nsleep()
void test_vmlinux(void) void test_vmlinux(void)
{ {
int duration = 0, err; int err;
struct test_vmlinux* skel; struct test_vmlinux* skel;
struct test_vmlinux__bss *bss; struct test_vmlinux__bss *bss;
skel = test_vmlinux__open_and_load(); skel = test_vmlinux__open_and_load();
if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) if (!ASSERT_OK_PTR(skel, "test_vmlinux__open_and_load"))
return; return;
bss = skel->bss; bss = skel->bss;
err = test_vmlinux__attach(skel); err = test_vmlinux__attach(skel);
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) if (!ASSERT_OK(err, "test_vmlinux__attach"))
goto cleanup; goto cleanup;
/* trigger everything */ /* trigger everything */
nsleep(); nsleep();
CHECK(!bss->tp_called, "tp", "not called\n"); ASSERT_TRUE(bss->tp_called, "tp");
CHECK(!bss->raw_tp_called, "raw_tp", "not called\n"); ASSERT_TRUE(bss->raw_tp_called, "raw_tp");
CHECK(!bss->tp_btf_called, "tp_btf", "not called\n"); ASSERT_TRUE(bss->tp_btf_called, "tp_btf");
CHECK(!bss->kprobe_called, "kprobe", "not called\n"); ASSERT_TRUE(bss->kprobe_called, "kprobe");
CHECK(!bss->fentry_called, "fentry", "not called\n"); ASSERT_TRUE(bss->fentry_called, "fentry");
cleanup: cleanup:
test_vmlinux__destroy(skel); test_vmlinux__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