Commit d70b2660 authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Martin KaFai Lau
Browse files

selftests/bpf: Extend sockopt tests to use BPF_LINK_CREATE



Run all existing test cases with the attachment created via
BPF_LINK_CREATE. Next commit will add extra test cases to verify
link_create attach_type enforcement.

Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20240426231621.2716876-3-sdf@google.com


Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parent 543576ec
Loading
Loading
Loading
Loading
+19 −6
Original line number Diff line number Diff line
@@ -1036,9 +1036,10 @@ static int call_getsockopt(bool use_io_uring, int fd, int level, int optname,
	return getsockopt(fd, level, optname, optval, optlen);
}

static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring,
		    bool use_link)
{
	int sock_fd, err, prog_fd;
	int sock_fd, err, prog_fd, link_fd = -1;
	void *optval = NULL;
	int ret = 0;

@@ -1051,7 +1052,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
		return -1;
	}

	if (use_link) {
		err = bpf_link_create(prog_fd, cgroup_fd, test->attach_type, NULL);
		link_fd = err;
	} else {
		err = bpf_prog_attach(prog_fd, cgroup_fd, test->attach_type, 0);
	}
	if (err < 0) {
		if (test->error == DENY_ATTACH)
			goto close_prog_fd;
@@ -1142,7 +1148,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
close_sock_fd:
	close(sock_fd);
detach_prog:
	if (use_link) {
		if (link_fd >= 0)
			close(link_fd);
	} else {
		bpf_prog_detach2(prog_fd, cgroup_fd, test->attach_type);
	}
close_prog_fd:
	close(prog_fd);
	return ret;
@@ -1160,10 +1171,12 @@ void test_sockopt(void)
		if (!test__start_subtest(tests[i].descr))
			continue;

		ASSERT_OK(run_test(cgroup_fd, &tests[i], false),
		ASSERT_OK(run_test(cgroup_fd, &tests[i], false, false),
			  tests[i].descr);
		ASSERT_OK(run_test(cgroup_fd, &tests[i], false, true),
			  tests[i].descr);
		if (tests[i].io_uring_support)
			ASSERT_OK(run_test(cgroup_fd, &tests[i], true),
			ASSERT_OK(run_test(cgroup_fd, &tests[i], true, false),
				  tests[i].descr);
	}