Commit 4b27480d authored by Toke Høiland-Jørgensen's avatar Toke Høiland-Jørgensen Committed by Alexei Starovoitov

bpf/selftests: convert xdp_link test to ASSERT_* macros

Convert the selftest to use the preferred ASSERT_* macros instead of the
deprecated CHECK().

v2:
- Don't add if statements around checks if they weren't there before.
Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/r/20220107221115.326171-2-toke@redhat.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 382778ed
...@@ -8,46 +8,47 @@ ...@@ -8,46 +8,47 @@
void serial_test_xdp_link(void) void serial_test_xdp_link(void)
{ {
__u32 duration = 0, id1, id2, id0 = 0, prog_fd1, prog_fd2, err;
DECLARE_LIBBPF_OPTS(bpf_xdp_set_link_opts, opts, .old_fd = -1); DECLARE_LIBBPF_OPTS(bpf_xdp_set_link_opts, opts, .old_fd = -1);
struct test_xdp_link *skel1 = NULL, *skel2 = NULL; struct test_xdp_link *skel1 = NULL, *skel2 = NULL;
__u32 id1, id2, id0 = 0, prog_fd1, prog_fd2;
struct bpf_link_info link_info; struct bpf_link_info link_info;
struct bpf_prog_info prog_info; struct bpf_prog_info prog_info;
struct bpf_link *link; struct bpf_link *link;
int err;
__u32 link_info_len = sizeof(link_info); __u32 link_info_len = sizeof(link_info);
__u32 prog_info_len = sizeof(prog_info); __u32 prog_info_len = sizeof(prog_info);
skel1 = test_xdp_link__open_and_load(); skel1 = test_xdp_link__open_and_load();
if (CHECK(!skel1, "skel_load", "skeleton open and load failed\n")) if (!ASSERT_OK_PTR(skel1, "skel_load"))
goto cleanup; goto cleanup;
prog_fd1 = bpf_program__fd(skel1->progs.xdp_handler); prog_fd1 = bpf_program__fd(skel1->progs.xdp_handler);
skel2 = test_xdp_link__open_and_load(); skel2 = test_xdp_link__open_and_load();
if (CHECK(!skel2, "skel_load", "skeleton open and load failed\n")) if (!ASSERT_OK_PTR(skel2, "skel_load"))
goto cleanup; goto cleanup;
prog_fd2 = bpf_program__fd(skel2->progs.xdp_handler); prog_fd2 = bpf_program__fd(skel2->progs.xdp_handler);
memset(&prog_info, 0, sizeof(prog_info)); memset(&prog_info, 0, sizeof(prog_info));
err = bpf_obj_get_info_by_fd(prog_fd1, &prog_info, &prog_info_len); err = bpf_obj_get_info_by_fd(prog_fd1, &prog_info, &prog_info_len);
if (CHECK(err, "fd_info1", "failed %d\n", -errno)) if (!ASSERT_OK(err, "fd_info1"))
goto cleanup; goto cleanup;
id1 = prog_info.id; id1 = prog_info.id;
memset(&prog_info, 0, sizeof(prog_info)); memset(&prog_info, 0, sizeof(prog_info));
err = bpf_obj_get_info_by_fd(prog_fd2, &prog_info, &prog_info_len); err = bpf_obj_get_info_by_fd(prog_fd2, &prog_info, &prog_info_len);
if (CHECK(err, "fd_info2", "failed %d\n", -errno)) if (!ASSERT_OK(err, "fd_info2"))
goto cleanup; goto cleanup;
id2 = prog_info.id; id2 = prog_info.id;
/* set initial prog attachment */ /* set initial prog attachment */
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, prog_fd1, XDP_FLAGS_REPLACE, &opts); err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, prog_fd1, XDP_FLAGS_REPLACE, &opts);
if (CHECK(err, "fd_attach", "initial prog attach failed: %d\n", err)) if (!ASSERT_OK(err, "fd_attach"))
goto cleanup; goto cleanup;
/* validate prog ID */ /* validate prog ID */
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0);
CHECK(err || id0 != id1, "id1_check", if (!ASSERT_OK(err, "id1_check_err") || !ASSERT_EQ(id0, id1, "id1_check_val"))
"loaded prog id %u != id1 %u, err %d", id0, id1, err); goto cleanup;
/* BPF link is not allowed to replace prog attachment */ /* BPF link is not allowed to replace prog attachment */
link = bpf_program__attach_xdp(skel1->progs.xdp_handler, IFINDEX_LO); link = bpf_program__attach_xdp(skel1->progs.xdp_handler, IFINDEX_LO);
...@@ -62,7 +63,7 @@ void serial_test_xdp_link(void) ...@@ -62,7 +63,7 @@ void serial_test_xdp_link(void)
/* detach BPF program */ /* detach BPF program */
opts.old_fd = prog_fd1; opts.old_fd = prog_fd1;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, -1, XDP_FLAGS_REPLACE, &opts); err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, -1, XDP_FLAGS_REPLACE, &opts);
if (CHECK(err, "prog_detach", "failed %d\n", err)) if (!ASSERT_OK(err, "prog_detach"))
goto cleanup; goto cleanup;
/* now BPF link should attach successfully */ /* now BPF link should attach successfully */
...@@ -73,24 +74,23 @@ void serial_test_xdp_link(void) ...@@ -73,24 +74,23 @@ void serial_test_xdp_link(void)
/* validate prog ID */ /* validate prog ID */
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0);
if (CHECK(err || id0 != id1, "id1_check", if (!ASSERT_OK(err, "id1_check_err") || !ASSERT_EQ(id0, id1, "id1_check_val"))
"loaded prog id %u != id1 %u, err %d", id0, id1, err))
goto cleanup; goto cleanup;
/* BPF prog attach is not allowed to replace BPF link */ /* BPF prog attach is not allowed to replace BPF link */
opts.old_fd = prog_fd1; opts.old_fd = prog_fd1;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, prog_fd2, XDP_FLAGS_REPLACE, &opts); err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, prog_fd2, XDP_FLAGS_REPLACE, &opts);
if (CHECK(!err, "prog_attach_fail", "unexpected success\n")) if (!ASSERT_ERR(err, "prog_attach_fail"))
goto cleanup; goto cleanup;
/* Can't force-update when BPF link is active */ /* Can't force-update when BPF link is active */
err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd2, 0); err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd2, 0);
if (CHECK(!err, "prog_update_fail", "unexpected success\n")) if (!ASSERT_ERR(err, "prog_update_fail"))
goto cleanup; goto cleanup;
/* Can't force-detach when BPF link is active */ /* Can't force-detach when BPF link is active */
err = bpf_set_link_xdp_fd(IFINDEX_LO, -1, 0); err = bpf_set_link_xdp_fd(IFINDEX_LO, -1, 0);
if (CHECK(!err, "prog_detach_fail", "unexpected success\n")) if (!ASSERT_ERR(err, "prog_detach_fail"))
goto cleanup; goto cleanup;
/* BPF link is not allowed to replace another BPF link */ /* BPF link is not allowed to replace another BPF link */
...@@ -110,40 +110,34 @@ void serial_test_xdp_link(void) ...@@ -110,40 +110,34 @@ void serial_test_xdp_link(void)
skel2->links.xdp_handler = link; skel2->links.xdp_handler = link;
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0);
if (CHECK(err || id0 != id2, "id2_check", if (!ASSERT_OK(err, "id2_check_err") || !ASSERT_EQ(id0, id2, "id2_check_val"))
"loaded prog id %u != id2 %u, err %d", id0, id1, err))
goto cleanup; goto cleanup;
/* updating program under active BPF link works as expected */ /* updating program under active BPF link works as expected */
err = bpf_link__update_program(link, skel1->progs.xdp_handler); err = bpf_link__update_program(link, skel1->progs.xdp_handler);
if (CHECK(err, "link_upd", "failed: %d\n", err)) if (!ASSERT_OK(err, "link_upd"))
goto cleanup; goto cleanup;
memset(&link_info, 0, sizeof(link_info)); memset(&link_info, 0, sizeof(link_info));
err = bpf_obj_get_info_by_fd(bpf_link__fd(link), &link_info, &link_info_len); err = bpf_obj_get_info_by_fd(bpf_link__fd(link), &link_info, &link_info_len);
if (CHECK(err, "link_info", "failed: %d\n", err)) if (!ASSERT_OK(err, "link_info"))
goto cleanup; goto cleanup;
CHECK(link_info.type != BPF_LINK_TYPE_XDP, "link_type", ASSERT_EQ(link_info.type, BPF_LINK_TYPE_XDP, "link_type");
"got %u != exp %u\n", link_info.type, BPF_LINK_TYPE_XDP); ASSERT_EQ(link_info.prog_id, id1, "link_prog_id");
CHECK(link_info.prog_id != id1, "link_prog_id", ASSERT_EQ(link_info.xdp.ifindex, IFINDEX_LO, "link_ifindex");
"got %u != exp %u\n", link_info.prog_id, id1);
CHECK(link_info.xdp.ifindex != IFINDEX_LO, "link_ifindex",
"got %u != exp %u\n", link_info.xdp.ifindex, IFINDEX_LO);
err = bpf_link__detach(link); err = bpf_link__detach(link);
if (CHECK(err, "link_detach", "failed %d\n", err)) if (!ASSERT_OK(err, "link_detach"))
goto cleanup; goto cleanup;
memset(&link_info, 0, sizeof(link_info)); memset(&link_info, 0, sizeof(link_info));
err = bpf_obj_get_info_by_fd(bpf_link__fd(link), &link_info, &link_info_len); err = bpf_obj_get_info_by_fd(bpf_link__fd(link), &link_info, &link_info_len);
if (CHECK(err, "link_info", "failed: %d\n", err))
goto cleanup; ASSERT_OK(err, "link_info");
CHECK(link_info.prog_id != id1, "link_prog_id", ASSERT_EQ(link_info.prog_id, id1, "link_prog_id");
"got %u != exp %u\n", link_info.prog_id, id1);
/* ifindex should be zeroed out */ /* ifindex should be zeroed out */
CHECK(link_info.xdp.ifindex != 0, "link_ifindex", ASSERT_EQ(link_info.xdp.ifindex, 0, "link_ifindex");
"got %u != exp %u\n", link_info.xdp.ifindex, 0);
cleanup: cleanup:
test_xdp_link__destroy(skel1); test_xdp_link__destroy(skel1);
......
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