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: Stanislav Fomichev <sdf@google.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20240426231621.2716876-3-sdf@google.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
This commit is contained in:
Stanislav Fomichev 2024-04-26 16:16:19 -07:00 committed by Martin KaFai Lau
parent 543576ec15
commit d70b2660e7

View File

@ -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;
}
err = bpf_prog_attach(prog_fd, cgroup_fd, test->attach_type, 0);
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:
bpf_prog_detach2(prog_fd, cgroup_fd, test->attach_type);
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);
}