Commit 04fcb5f9 authored by Delyan Kratunov's avatar Delyan Kratunov Committed by Andrii Nakryiko
Browse files

selftests/bpf: Migrate from bpf_prog_test_run



bpf_prog_test_run is being deprecated in favor of the OPTS-based
bpf_prog_test_run_opts.
We end up unable to use CHECK in most cases, so replace usages with
ASSERT_* calls.

Signed-off-by: default avatarDelyan Kratunov <delyank@fb.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220202235423.1097270-2-delyank@fb.com
parent dd5152ab
Loading
Loading
Loading
Loading
+35 −37
Original line number Diff line number Diff line
@@ -7,18 +7,18 @@
static void test_add(struct atomics_lskel *skel)
{
	int err, prog_fd;
	__u32 duration = 0, retval;
	int link_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	link_fd = atomics_lskel__add__attach(skel);
	if (!ASSERT_GT(link_fd, 0, "attach(add)"))
		return;

	prog_fd = skel->progs.add.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	if (CHECK(err || retval, "test_run add",
		  "err %d errno %d retval %d duration %d\n", err, errno, retval, duration))
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		goto cleanup;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		goto cleanup;

	ASSERT_EQ(skel->data->add64_value, 3, "add64_value");
@@ -39,19 +39,18 @@ static void test_add(struct atomics_lskel *skel)
static void test_sub(struct atomics_lskel *skel)
{
	int err, prog_fd;
	__u32 duration = 0, retval;
	int link_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	link_fd = atomics_lskel__sub__attach(skel);
	if (!ASSERT_GT(link_fd, 0, "attach(sub)"))
		return;

	prog_fd = skel->progs.sub.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	if (CHECK(err || retval, "test_run sub",
		  "err %d errno %d retval %d duration %d\n",
		  err, errno, retval, duration))
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		goto cleanup;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		goto cleanup;

	ASSERT_EQ(skel->data->sub64_value, -1, "sub64_value");
@@ -72,18 +71,18 @@ static void test_sub(struct atomics_lskel *skel)
static void test_and(struct atomics_lskel *skel)
{
	int err, prog_fd;
	__u32 duration = 0, retval;
	int link_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	link_fd = atomics_lskel__and__attach(skel);
	if (!ASSERT_GT(link_fd, 0, "attach(and)"))
		return;

	prog_fd = skel->progs.and.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	if (CHECK(err || retval, "test_run and",
		  "err %d errno %d retval %d duration %d\n", err, errno, retval, duration))
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		goto cleanup;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		goto cleanup;

	ASSERT_EQ(skel->data->and64_value, 0x010ull << 32, "and64_value");
@@ -100,19 +99,18 @@ static void test_and(struct atomics_lskel *skel)
static void test_or(struct atomics_lskel *skel)
{
	int err, prog_fd;
	__u32 duration = 0, retval;
	int link_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	link_fd = atomics_lskel__or__attach(skel);
	if (!ASSERT_GT(link_fd, 0, "attach(or)"))
		return;

	prog_fd = skel->progs.or.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	if (CHECK(err || retval, "test_run or",
		  "err %d errno %d retval %d duration %d\n",
		  err, errno, retval, duration))
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		goto cleanup;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		goto cleanup;

	ASSERT_EQ(skel->data->or64_value, 0x111ull << 32, "or64_value");
@@ -129,18 +127,18 @@ static void test_or(struct atomics_lskel *skel)
static void test_xor(struct atomics_lskel *skel)
{
	int err, prog_fd;
	__u32 duration = 0, retval;
	int link_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	link_fd = atomics_lskel__xor__attach(skel);
	if (!ASSERT_GT(link_fd, 0, "attach(xor)"))
		return;

	prog_fd = skel->progs.xor.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	if (CHECK(err || retval, "test_run xor",
		  "err %d errno %d retval %d duration %d\n", err, errno, retval, duration))
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		goto cleanup;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		goto cleanup;

	ASSERT_EQ(skel->data->xor64_value, 0x101ull << 32, "xor64_value");
@@ -157,18 +155,18 @@ static void test_xor(struct atomics_lskel *skel)
static void test_cmpxchg(struct atomics_lskel *skel)
{
	int err, prog_fd;
	__u32 duration = 0, retval;
	int link_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	link_fd = atomics_lskel__cmpxchg__attach(skel);
	if (!ASSERT_GT(link_fd, 0, "attach(cmpxchg)"))
		return;

	prog_fd = skel->progs.cmpxchg.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	if (CHECK(err || retval, "test_run cmpxchg",
		  "err %d errno %d retval %d duration %d\n", err, errno, retval, duration))
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		goto cleanup;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		goto cleanup;

	ASSERT_EQ(skel->data->cmpxchg64_value, 2, "cmpxchg64_value");
@@ -186,18 +184,18 @@ static void test_cmpxchg(struct atomics_lskel *skel)
static void test_xchg(struct atomics_lskel *skel)
{
	int err, prog_fd;
	__u32 duration = 0, retval;
	int link_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	link_fd = atomics_lskel__xchg__attach(skel);
	if (!ASSERT_GT(link_fd, 0, "attach(xchg)"))
		return;

	prog_fd = skel->progs.xchg.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	if (CHECK(err || retval, "test_run xchg",
		  "err %d errno %d retval %d duration %d\n", err, errno, retval, duration))
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "test_run_opts err"))
		goto cleanup;
	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
		goto cleanup;

	ASSERT_EQ(skel->data->xchg64_value, 2, "xchg64_value");
+7 −3
Original line number Diff line number Diff line
@@ -11,7 +11,12 @@ enum {
void test_bpf_nf_ct(int mode)
{
	struct test_bpf_nf *skel;
	int prog_fd, err, retval;
	int prog_fd, err;
	LIBBPF_OPTS(bpf_test_run_opts, topts,
		.data_in = &pkt_v4,
		.data_size_in = sizeof(pkt_v4),
		.repeat = 1,
	);

	skel = test_bpf_nf__open_and_load();
	if (!ASSERT_OK_PTR(skel, "test_bpf_nf__open_and_load"))
@@ -22,8 +27,7 @@ void test_bpf_nf_ct(int mode)
	else
		prog_fd = bpf_program__fd(skel->progs.nf_skb_ct_test);

	err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
				(__u32 *)&retval, NULL);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	if (!ASSERT_OK(err, "bpf_prog_test_run"))
		goto end;

+10 −14
Original line number Diff line number Diff line
@@ -9,38 +9,34 @@ void test_fentry_fexit(void)
	struct fentry_test_lskel *fentry_skel = NULL;
	struct fexit_test_lskel *fexit_skel = NULL;
	__u64 *fentry_res, *fexit_res;
	__u32 duration = 0, retval;
	int err, prog_fd, i;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	fentry_skel = fentry_test_lskel__open_and_load();
	if (CHECK(!fentry_skel, "fentry_skel_load", "fentry skeleton failed\n"))
	if (!ASSERT_OK_PTR(fentry_skel, "fentry_skel_load"))
		goto close_prog;
	fexit_skel = fexit_test_lskel__open_and_load();
	if (CHECK(!fexit_skel, "fexit_skel_load", "fexit skeleton failed\n"))
	if (!ASSERT_OK_PTR(fexit_skel, "fexit_skel_load"))
		goto close_prog;

	err = fentry_test_lskel__attach(fentry_skel);
	if (CHECK(err, "fentry_attach", "fentry attach failed: %d\n", err))
	if (!ASSERT_OK(err, "fentry_attach"))
		goto close_prog;
	err = fexit_test_lskel__attach(fexit_skel);
	if (CHECK(err, "fexit_attach", "fexit attach failed: %d\n", err))
	if (!ASSERT_OK(err, "fexit_attach"))
		goto close_prog;

	prog_fd = fexit_skel->progs.test1.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	CHECK(err || retval, "ipv6",
	      "err %d errno %d retval %d duration %d\n",
	      err, errno, retval, duration);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "ipv6 test_run");
	ASSERT_OK(topts.retval, "ipv6 test retval");

	fentry_res = (__u64 *)fentry_skel->bss;
	fexit_res = (__u64 *)fexit_skel->bss;
	printf("%lld\n", fentry_skel->bss->test1_result);
	for (i = 0; i < 8; i++) {
		CHECK(fentry_res[i] != 1, "result",
		      "fentry_test%d failed err %lld\n", i + 1, fentry_res[i]);
		CHECK(fexit_res[i] != 1, "result",
		      "fexit_test%d failed err %lld\n", i + 1, fexit_res[i]);
		ASSERT_EQ(fentry_res[i], 1, "fentry result");
		ASSERT_EQ(fexit_res[i], 1, "fexit result");
	}

close_prog:
+3 −4
Original line number Diff line number Diff line
@@ -6,9 +6,9 @@
static int fentry_test(struct fentry_test_lskel *fentry_skel)
{
	int err, prog_fd, i;
	__u32 duration = 0, retval;
	int link_fd;
	__u64 *result;
	LIBBPF_OPTS(bpf_test_run_opts, topts);

	err = fentry_test_lskel__attach(fentry_skel);
	if (!ASSERT_OK(err, "fentry_attach"))
@@ -20,10 +20,9 @@ static int fentry_test(struct fentry_test_lskel *fentry_skel)
		return -1;

	prog_fd = fentry_skel->progs.test1.prog_fd;
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "test_run");
	ASSERT_EQ(retval, 0, "test_run");
	ASSERT_EQ(topts.retval, 0, "test_run");

	result = (__u64 *)fentry_skel->bss;
	for (i = 0; i < sizeof(*fentry_skel->bss) / sizeof(__u64); i++) {
+19 −13
Original line number Diff line number Diff line
@@ -58,12 +58,17 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
				      test_cb cb)
{
	struct bpf_object *obj = NULL, *tgt_obj;
	__u32 retval, tgt_prog_id, info_len;
	__u32 tgt_prog_id, info_len;
	struct bpf_prog_info prog_info = {};
	struct bpf_program **prog = NULL, *p;
	struct bpf_link **link = NULL;
	int err, tgt_fd, i;
	struct btf *btf;
	LIBBPF_OPTS(bpf_test_run_opts, topts,
		.data_in = &pkt_v6,
		.data_size_in = sizeof(pkt_v6),
		.repeat = 1,
	);

	err = bpf_prog_test_load(target_obj_file, BPF_PROG_TYPE_UNSPEC,
			    &tgt_obj, &tgt_fd);
@@ -147,10 +152,9 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
	if (!run_prog)
		goto close_prog;

	err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6),
				NULL, NULL, &retval, NULL);
	err = bpf_prog_test_run_opts(tgt_fd, &topts);
	ASSERT_OK(err, "prog_run");
	ASSERT_EQ(retval, 0, "prog_run_ret");
	ASSERT_EQ(topts.retval, 0, "prog_run_ret");

	if (check_data_map(obj, prog_cnt, false))
		goto close_prog;
@@ -225,29 +229,31 @@ static int test_second_attach(struct bpf_object *obj)
	const char *tgt_obj_file = "./test_pkt_access.o";
	struct bpf_program *prog = NULL;
	struct bpf_object *tgt_obj;
	__u32 duration = 0, retval;
	struct bpf_link *link;
	int err = 0, tgt_fd;
	LIBBPF_OPTS(bpf_test_run_opts, topts,
		.data_in = &pkt_v6,
		.data_size_in = sizeof(pkt_v6),
		.repeat = 1,
	);

	prog = bpf_object__find_program_by_name(obj, prog_name);
	if (CHECK(!prog, "find_prog", "prog %s not found\n", prog_name))
	if (!ASSERT_OK_PTR(prog, "find_prog"))
		return -ENOENT;

	err = bpf_prog_test_load(tgt_obj_file, BPF_PROG_TYPE_UNSPEC,
			    &tgt_obj, &tgt_fd);
	if (CHECK(err, "second_prog_load", "file %s err %d errno %d\n",
		  tgt_obj_file, err, errno))
	if (!ASSERT_OK(err, "second_prog_load"))
		return err;

	link = bpf_program__attach_freplace(prog, tgt_fd, tgt_name);
	if (!ASSERT_OK_PTR(link, "second_link"))
		goto out;

	err = bpf_prog_test_run(tgt_fd, 1, &pkt_v6, sizeof(pkt_v6),
				NULL, NULL, &retval, &duration);
	if (CHECK(err || retval, "ipv6",
		  "err %d errno %d retval %d duration %d\n",
		  err, errno, retval, duration))
	err = bpf_prog_test_run_opts(tgt_fd, &topts);
	if (!ASSERT_OK(err, "ipv6 test_run"))
		goto out;
	if (!ASSERT_OK(topts.retval, "ipv6 retval"))
		goto out;

	err = check_data_map(obj, 1, true);
Loading