selftests/bpf: switch to new libbpf XDP APIs

Switch to using new bpf_xdp_*() APIs across all selftests. Take
advantage of a more straightforward and user-friendly semantics of
old_prog_fd (0 means "don't care") in few places.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20220120061422.2710637-4-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Andrii Nakryiko 2022-01-19 22:14:21 -08:00 коммит произвёл Alexei Starovoitov
Родитель c86575ecca
Коммит 544356524d
7 изменённых файлов: 47 добавлений и 50 удалений

Просмотреть файл

@ -11,8 +11,7 @@ void serial_test_xdp_attach(void)
const char *file = "./test_xdp.o"; const char *file = "./test_xdp.o";
struct bpf_prog_info info = {}; struct bpf_prog_info info = {};
int err, fd1, fd2, fd3; int err, fd1, fd2, fd3;
DECLARE_LIBBPF_OPTS(bpf_xdp_set_link_opts, opts, LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
.old_fd = -1);
len = sizeof(info); len = sizeof(info);
@ -38,49 +37,47 @@ void serial_test_xdp_attach(void)
if (CHECK_FAIL(err)) if (CHECK_FAIL(err))
goto out_2; goto out_2;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, fd1, XDP_FLAGS_REPLACE, err = bpf_xdp_attach(IFINDEX_LO, fd1, XDP_FLAGS_REPLACE, &opts);
&opts);
if (CHECK(err, "load_ok", "initial load failed")) if (CHECK(err, "load_ok", "initial load failed"))
goto out_close; goto out_close;
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0);
if (CHECK(err || id0 != id1, "id1_check", if (CHECK(err || id0 != id1, "id1_check",
"loaded prog id %u != id1 %u, err %d", id0, id1, err)) "loaded prog id %u != id1 %u, err %d", id0, id1, err))
goto out_close; goto out_close;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, fd2, XDP_FLAGS_REPLACE, err = bpf_xdp_attach(IFINDEX_LO, fd2, XDP_FLAGS_REPLACE, &opts);
&opts);
if (CHECK(!err, "load_fail", "load with expected id didn't fail")) if (CHECK(!err, "load_fail", "load with expected id didn't fail"))
goto out; goto out;
opts.old_fd = fd1; opts.old_prog_fd = fd1;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, fd2, 0, &opts); err = bpf_xdp_attach(IFINDEX_LO, fd2, 0, &opts);
if (CHECK(err, "replace_ok", "replace valid old_fd failed")) if (CHECK(err, "replace_ok", "replace valid old_fd failed"))
goto out; goto out;
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0);
if (CHECK(err || id0 != id2, "id2_check", if (CHECK(err || id0 != id2, "id2_check",
"loaded prog id %u != id2 %u, err %d", id0, id2, err)) "loaded prog id %u != id2 %u, err %d", id0, id2, err))
goto out_close; goto out_close;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, fd3, 0, &opts); err = bpf_xdp_attach(IFINDEX_LO, fd3, 0, &opts);
if (CHECK(!err, "replace_fail", "replace invalid old_fd didn't fail")) if (CHECK(!err, "replace_fail", "replace invalid old_fd didn't fail"))
goto out; goto out;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, -1, 0, &opts); err = bpf_xdp_detach(IFINDEX_LO, 0, &opts);
if (CHECK(!err, "remove_fail", "remove invalid old_fd didn't fail")) if (CHECK(!err, "remove_fail", "remove invalid old_fd didn't fail"))
goto out; goto out;
opts.old_fd = fd2; opts.old_prog_fd = fd2;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, -1, 0, &opts); err = bpf_xdp_detach(IFINDEX_LO, 0, &opts);
if (CHECK(err, "remove_ok", "remove valid old_fd failed")) if (CHECK(err, "remove_ok", "remove valid old_fd failed"))
goto out; goto out;
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0);
if (CHECK(err || id0 != 0, "unload_check", if (CHECK(err || id0 != 0, "unload_check",
"loaded prog id %u != 0, err %d", id0, err)) "loaded prog id %u != 0, err %d", id0, err))
goto out_close; goto out_close;
out: out:
bpf_set_link_xdp_fd(IFINDEX_LO, -1, 0); bpf_xdp_detach(IFINDEX_LO, 0, NULL);
out_close: out_close:
bpf_object__close(obj3); bpf_object__close(obj3);
out_2: out_2:

Просмотреть файл

@ -23,11 +23,11 @@ void serial_test_xdp_cpumap_attach(void)
return; return;
prog_fd = bpf_program__fd(skel->progs.xdp_redir_prog); prog_fd = bpf_program__fd(skel->progs.xdp_redir_prog);
err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE); err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
if (!ASSERT_OK(err, "Generic attach of program with 8-byte CPUMAP")) if (!ASSERT_OK(err, "Generic attach of program with 8-byte CPUMAP"))
goto out_close; goto out_close;
err = bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_SKB_MODE); err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
ASSERT_OK(err, "XDP program detach"); ASSERT_OK(err, "XDP program detach");
prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm); prog_fd = bpf_program__fd(skel->progs.xdp_dummy_cm);
@ -45,9 +45,9 @@ void serial_test_xdp_cpumap_attach(void)
ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to cpumap entry prog_id"); ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to cpumap entry prog_id");
/* can not attach BPF_XDP_CPUMAP program to a device */ /* can not attach BPF_XDP_CPUMAP program to a device */
err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE); err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_CPUMAP program")) if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_CPUMAP program"))
bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_SKB_MODE); bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
val.qsize = 192; val.qsize = 192;
val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog); val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);

Просмотреть файл

@ -25,11 +25,11 @@ static void test_xdp_with_devmap_helpers(void)
return; return;
dm_fd = bpf_program__fd(skel->progs.xdp_redir_prog); dm_fd = bpf_program__fd(skel->progs.xdp_redir_prog);
err = bpf_set_link_xdp_fd(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE); err = bpf_xdp_attach(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE, NULL);
if (!ASSERT_OK(err, "Generic attach of program with 8-byte devmap")) if (!ASSERT_OK(err, "Generic attach of program with 8-byte devmap"))
goto out_close; goto out_close;
err = bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_SKB_MODE); err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
ASSERT_OK(err, "XDP program detach"); ASSERT_OK(err, "XDP program detach");
dm_fd = bpf_program__fd(skel->progs.xdp_dummy_dm); dm_fd = bpf_program__fd(skel->progs.xdp_dummy_dm);
@ -47,9 +47,9 @@ static void test_xdp_with_devmap_helpers(void)
ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to devmap entry prog_id"); ASSERT_EQ(info.id, val.bpf_prog.id, "Match program id to devmap entry prog_id");
/* can not attach BPF_XDP_DEVMAP program to a device */ /* can not attach BPF_XDP_DEVMAP program to a device */
err = bpf_set_link_xdp_fd(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE); err = bpf_xdp_attach(IFINDEX_LO, dm_fd, XDP_FLAGS_SKB_MODE, NULL);
if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_DEVMAP program")) if (!ASSERT_NEQ(err, 0, "Attach of BPF_XDP_DEVMAP program"))
bpf_set_link_xdp_fd(IFINDEX_LO, -1, XDP_FLAGS_SKB_MODE); bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_SKB_MODE, NULL);
val.ifindex = 1; val.ifindex = 1;
val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog); val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);

Просмотреть файл

@ -14,13 +14,13 @@ void serial_test_xdp_info(void)
/* Get prog_id for XDP_ATTACHED_NONE mode */ /* Get prog_id for XDP_ATTACHED_NONE mode */
err = bpf_get_link_xdp_id(IFINDEX_LO, &prog_id, 0); err = bpf_xdp_query_id(IFINDEX_LO, 0, &prog_id);
if (CHECK(err, "get_xdp_none", "errno=%d\n", errno)) if (CHECK(err, "get_xdp_none", "errno=%d\n", errno))
return; return;
if (CHECK(prog_id, "prog_id_none", "unexpected prog_id=%u\n", prog_id)) if (CHECK(prog_id, "prog_id_none", "unexpected prog_id=%u\n", prog_id))
return; return;
err = bpf_get_link_xdp_id(IFINDEX_LO, &prog_id, XDP_FLAGS_SKB_MODE); err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_SKB_MODE, &prog_id);
if (CHECK(err, "get_xdp_none_skb", "errno=%d\n", errno)) if (CHECK(err, "get_xdp_none_skb", "errno=%d\n", errno))
return; return;
if (CHECK(prog_id, "prog_id_none_skb", "unexpected prog_id=%u\n", if (CHECK(prog_id, "prog_id_none_skb", "unexpected prog_id=%u\n",
@ -37,32 +37,32 @@ void serial_test_xdp_info(void)
if (CHECK(err, "get_prog_info", "errno=%d\n", errno)) if (CHECK(err, "get_prog_info", "errno=%d\n", errno))
goto out_close; goto out_close;
err = bpf_set_link_xdp_fd(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE); err = bpf_xdp_attach(IFINDEX_LO, prog_fd, XDP_FLAGS_SKB_MODE, NULL);
if (CHECK(err, "set_xdp_skb", "errno=%d\n", errno)) if (CHECK(err, "set_xdp_skb", "errno=%d\n", errno))
goto out_close; goto out_close;
/* Get prog_id for single prog mode */ /* Get prog_id for single prog mode */
err = bpf_get_link_xdp_id(IFINDEX_LO, &prog_id, 0); err = bpf_xdp_query_id(IFINDEX_LO, 0, &prog_id);
if (CHECK(err, "get_xdp", "errno=%d\n", errno)) if (CHECK(err, "get_xdp", "errno=%d\n", errno))
goto out; goto out;
if (CHECK(prog_id != info.id, "prog_id", "prog_id not available\n")) if (CHECK(prog_id != info.id, "prog_id", "prog_id not available\n"))
goto out; goto out;
err = bpf_get_link_xdp_id(IFINDEX_LO, &prog_id, XDP_FLAGS_SKB_MODE); err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_SKB_MODE, &prog_id);
if (CHECK(err, "get_xdp_skb", "errno=%d\n", errno)) if (CHECK(err, "get_xdp_skb", "errno=%d\n", errno))
goto out; goto out;
if (CHECK(prog_id != info.id, "prog_id_skb", "prog_id not available\n")) if (CHECK(prog_id != info.id, "prog_id_skb", "prog_id not available\n"))
goto out; goto out;
err = bpf_get_link_xdp_id(IFINDEX_LO, &prog_id, XDP_FLAGS_DRV_MODE); err = bpf_xdp_query_id(IFINDEX_LO, XDP_FLAGS_DRV_MODE, &prog_id);
if (CHECK(err, "get_xdp_drv", "errno=%d\n", errno)) if (CHECK(err, "get_xdp_drv", "errno=%d\n", errno))
goto out; goto out;
if (CHECK(prog_id, "prog_id_drv", "unexpected prog_id=%u\n", prog_id)) if (CHECK(prog_id, "prog_id_drv", "unexpected prog_id=%u\n", prog_id))
goto out; goto out;
out: out:
bpf_set_link_xdp_fd(IFINDEX_LO, -1, 0); bpf_xdp_detach(IFINDEX_LO, 0, NULL);
out_close: out_close:
bpf_object__close(obj); bpf_object__close(obj);
} }

Просмотреть файл

@ -9,8 +9,8 @@
void serial_test_xdp_link(void) void serial_test_xdp_link(void)
{ {
__u32 duration = 0, id1, id2, id0 = 0, prog_fd1, prog_fd2, err; __u32 duration = 0, id1, id2, id0 = 0, prog_fd1, prog_fd2, err;
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;
LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
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;
@ -40,12 +40,12 @@ void serial_test_xdp_link(void)
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_xdp_attach(IFINDEX_LO, prog_fd1, XDP_FLAGS_REPLACE, &opts);
if (CHECK(err, "fd_attach", "initial prog attach failed: %d\n", err)) if (CHECK(err, "fd_attach", "initial prog attach failed: %d\n", err))
goto cleanup; goto cleanup;
/* validate prog ID */ /* validate prog ID */
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0);
CHECK(err || id0 != id1, "id1_check", CHECK(err || id0 != id1, "id1_check",
"loaded prog id %u != id1 %u, err %d", id0, id1, err); "loaded prog id %u != id1 %u, err %d", id0, id1, err);
@ -54,14 +54,14 @@ void serial_test_xdp_link(void)
if (!ASSERT_ERR_PTR(link, "link_attach_should_fail")) { if (!ASSERT_ERR_PTR(link, "link_attach_should_fail")) {
bpf_link__destroy(link); bpf_link__destroy(link);
/* best-effort detach prog */ /* best-effort detach prog */
opts.old_fd = prog_fd1; opts.old_prog_fd = prog_fd1;
bpf_set_link_xdp_fd_opts(IFINDEX_LO, -1, XDP_FLAGS_REPLACE, &opts); bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_REPLACE, &opts);
goto cleanup; goto cleanup;
} }
/* detach BPF program */ /* detach BPF program */
opts.old_fd = prog_fd1; opts.old_prog_fd = prog_fd1;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, -1, XDP_FLAGS_REPLACE, &opts); err = bpf_xdp_detach(IFINDEX_LO, XDP_FLAGS_REPLACE, &opts);
if (CHECK(err, "prog_detach", "failed %d\n", err)) if (CHECK(err, "prog_detach", "failed %d\n", err))
goto cleanup; goto cleanup;
@ -72,24 +72,24 @@ void serial_test_xdp_link(void)
skel1->links.xdp_handler = link; skel1->links.xdp_handler = link;
/* validate prog ID */ /* validate prog ID */
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0);
if (CHECK(err || id0 != id1, "id1_check", if (CHECK(err || id0 != id1, "id1_check",
"loaded prog id %u != id1 %u, err %d", id0, id1, err)) "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_prog_fd = prog_fd1;
err = bpf_set_link_xdp_fd_opts(IFINDEX_LO, prog_fd2, XDP_FLAGS_REPLACE, &opts); err = bpf_xdp_attach(IFINDEX_LO, prog_fd2, XDP_FLAGS_REPLACE, &opts);
if (CHECK(!err, "prog_attach_fail", "unexpected success\n")) if (CHECK(!err, "prog_attach_fail", "unexpected success\n"))
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_xdp_attach(IFINDEX_LO, prog_fd2, 0, NULL);
if (CHECK(!err, "prog_update_fail", "unexpected success\n")) if (CHECK(!err, "prog_update_fail", "unexpected success\n"))
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_xdp_detach(IFINDEX_LO, 0, NULL);
if (CHECK(!err, "prog_detach_fail", "unexpected success\n")) if (CHECK(!err, "prog_detach_fail", "unexpected success\n"))
goto cleanup; goto cleanup;
@ -109,7 +109,7 @@ void serial_test_xdp_link(void)
goto cleanup; goto cleanup;
skel2->links.xdp_handler = link; skel2->links.xdp_handler = link;
err = bpf_get_link_xdp_id(IFINDEX_LO, &id0, 0); err = bpf_xdp_query_id(IFINDEX_LO, 0, &id0);
if (CHECK(err || id0 != id2, "id2_check", if (CHECK(err || id0 != id2, "id2_check",
"loaded prog id %u != id2 %u, err %d", id0, id1, err)) "loaded prog id %u != id2 %u, err %d", id0, id1, err))
goto cleanup; goto cleanup;

Просмотреть файл

@ -32,12 +32,12 @@ static void int_exit(int sig)
int i; int i;
for (i = 0; ifaces[i] > 0; i++) { for (i = 0; ifaces[i] > 0; i++) {
if (bpf_get_link_xdp_id(ifaces[i], &prog_id, xdp_flags)) { if (bpf_xdp_query_id(ifaces[i], xdp_flags, &prog_id)) {
printf("bpf_get_link_xdp_id failed\n"); printf("bpf_xdp_query_id failed\n");
exit(1); exit(1);
} }
if (prog_id) if (prog_id)
bpf_set_link_xdp_fd(ifaces[i], -1, xdp_flags); bpf_xdp_detach(ifaces[i], xdp_flags, NULL);
} }
exit(0); exit(0);
@ -210,7 +210,7 @@ int main(int argc, char **argv)
} }
/* bind prog_fd to each interface */ /* bind prog_fd to each interface */
ret = bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags); ret = bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL);
if (ret) { if (ret) {
printf("Set xdp fd failed on %d\n", ifindex); printf("Set xdp fd failed on %d\n", ifindex);
goto err_out; goto err_out;

Просмотреть файл

@ -29,7 +29,7 @@ static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
static void cleanup(int sig) static void cleanup(int sig)
{ {
bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); bpf_xdp_detach(ifindex, xdp_flags, NULL);
if (sig) if (sig)
exit(1); exit(1);
} }
@ -203,7 +203,7 @@ int main(int argc, char **argv)
printf("XDP setup disrupts network connectivity, hit Ctrl+C to quit\n"); printf("XDP setup disrupts network connectivity, hit Ctrl+C to quit\n");
if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) { if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
fprintf(stderr, "Link set xdp fd failed for %s\n", ifname); fprintf(stderr, "Link set xdp fd failed for %s\n", ifname);
goto done; goto done;
} }