Commit 707ee8ac authored by Andrii Nakryiko's avatar Andrii Nakryiko
Browse files

Merge branch 'migrate from bpf_prog_test_run{,_xattr}'



Delyan Kratunov says:

====================

Fairly straight-forward mechanical transformation from bpf_prog_test_run
and bpf_prog_test_run_xattr to the bpf_prog_test_run_opts goodness.

I did a fair amount of drive-by CHECK/CHECK_ATTR cleanups as well, though
certainly not everything possible. Primarily, I did not want to just change
arguments to CHECK calls, though I had to do a bit more than that
in some cases (overall, -119 CHECK calls and all CHECK_ATTR calls).

v2 -> v3:
Don't introduce CHECK_OPTS, replace CHECK/CHECK_ATTR usages we need to touch
with ASSERT_* calls instead.
Don't be prescriptive about the opts var name and keep old names where that would
minimize unnecessary code churn.
Drop _xattr-specific checks in prog_run_xattr and rename accordingly.

v1 -> v2:
Split selftests/bpf changes into two commits to appease the mailing list.
====================

Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents dd5152ab 3e1ab843
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -1272,12 +1272,12 @@ static int do_run(int argc, char **argv)
{
	char *data_fname_in = NULL, *data_fname_out = NULL;
	char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
	struct bpf_prog_test_run_attr test_attr = {0};
	const unsigned int default_size = SZ_32K;
	void *data_in = NULL, *data_out = NULL;
	void *ctx_in = NULL, *ctx_out = NULL;
	unsigned int repeat = 1;
	int fd, err;
	LIBBPF_OPTS(bpf_test_run_opts, test_attr);

	if (!REQ_ARGS(4))
		return -1;
@@ -1395,14 +1395,13 @@ static int do_run(int argc, char **argv)
			goto free_ctx_in;
	}

	test_attr.prog_fd	= fd;
	test_attr.repeat	= repeat;
	test_attr.data_in	= data_in;
	test_attr.data_out	= data_out;
	test_attr.ctx_in	= ctx_in;
	test_attr.ctx_out	= ctx_out;

	err = bpf_prog_test_run_xattr(&test_attr);
	err = bpf_prog_test_run_opts(fd, &test_attr);
	if (err) {
		p_err("failed to run program: %s", strerror(errno));
		goto free_ctx_out;
+3 −1
Original line number Diff line number Diff line
@@ -453,12 +453,14 @@ struct bpf_prog_test_run_attr {
			     * out: length of cxt_out */
};

LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_test_run_opts() instead")
LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);

/*
 * bpf_prog_test_run does not check that data_out is large enough. Consider
 * using bpf_prog_test_run_xattr instead.
 * using bpf_prog_test_run_opts instead.
 */
LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_test_run_opts() instead")
LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
				 __u32 size, void *data_out, __u32 *size_out,
				 __u32 *retval, __u32 *duration);
+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;

+13 −27
Original line number Diff line number Diff line
@@ -79,28 +79,21 @@ static void test_check_mtu_run_xdp(struct test_check_mtu *skel,
				   struct bpf_program *prog,
				   __u32 mtu_expect)
{
	const char *prog_name = bpf_program__name(prog);
	int retval_expect = XDP_PASS;
	__u32 mtu_result = 0;
	char buf[256] = {};
	int err;
	struct bpf_prog_test_run_attr tattr = {
	int err, prog_fd = bpf_program__fd(prog);
	LIBBPF_OPTS(bpf_test_run_opts, topts,
		.repeat = 1,
		.data_in = &pkt_v4,
		.data_size_in = sizeof(pkt_v4),
		.data_out = buf,
		.data_size_out = sizeof(buf),
		.prog_fd = bpf_program__fd(prog),
	};

	err = bpf_prog_test_run_xattr(&tattr);
	CHECK_ATTR(err != 0, "bpf_prog_test_run",
		   "prog_name:%s (err %d errno %d retval %d)\n",
		   prog_name, err, errno, tattr.retval);
	);

	CHECK(tattr.retval != retval_expect, "retval",
	      "progname:%s unexpected retval=%d expected=%d\n",
	      prog_name, tattr.retval, retval_expect);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "test_run");
	ASSERT_EQ(topts.retval, retval_expect, "retval");

	/* Extract MTU that BPF-prog got */
	mtu_result = skel->bss->global_bpf_mtu_xdp;
@@ -139,28 +132,21 @@ static void test_check_mtu_run_tc(struct test_check_mtu *skel,
				  struct bpf_program *prog,
				  __u32 mtu_expect)
{
	const char *prog_name = bpf_program__name(prog);
	int retval_expect = BPF_OK;
	__u32 mtu_result = 0;
	char buf[256] = {};
	int err;
	struct bpf_prog_test_run_attr tattr = {
		.repeat = 1,
	int err, prog_fd = bpf_program__fd(prog);
	LIBBPF_OPTS(bpf_test_run_opts, topts,
		.data_in = &pkt_v4,
		.data_size_in = sizeof(pkt_v4),
		.data_out = buf,
		.data_size_out = sizeof(buf),
		.prog_fd = bpf_program__fd(prog),
	};

	err = bpf_prog_test_run_xattr(&tattr);
	CHECK_ATTR(err != 0, "bpf_prog_test_run",
		   "prog_name:%s (err %d errno %d retval %d)\n",
		   prog_name, err, errno, tattr.retval);
		.repeat = 1,
	);

	CHECK(tattr.retval != retval_expect, "retval",
	      "progname:%s unexpected retval=%d expected=%d\n",
	      prog_name, tattr.retval, retval_expect);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "test_run");
	ASSERT_EQ(topts.retval, retval_expect, "retval");

	/* Extract MTU that BPF-prog got */
	mtu_result = skel->bss->global_bpf_mtu_tc;
Loading