Skip to content

Commit d70b266

Browse files
fomichevMartin KaFai Lau
authored and
Martin KaFai Lau
committed
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 <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 543576e commit d70b266

File tree

1 file changed

+19
-6
lines changed
  • tools/testing/selftests/bpf/prog_tests

1 file changed

+19
-6
lines changed

tools/testing/selftests/bpf/prog_tests/sockopt.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,10 @@ static int call_getsockopt(bool use_io_uring, int fd, int level, int optname,
10361036
return getsockopt(fd, level, optname, optval, optlen);
10371037
}
10381038

1039-
static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
1039+
static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring,
1040+
bool use_link)
10401041
{
1041-
int sock_fd, err, prog_fd;
1042+
int sock_fd, err, prog_fd, link_fd = -1;
10421043
void *optval = NULL;
10431044
int ret = 0;
10441045

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

1054-
err = bpf_prog_attach(prog_fd, cgroup_fd, test->attach_type, 0);
1055+
if (use_link) {
1056+
err = bpf_link_create(prog_fd, cgroup_fd, test->attach_type, NULL);
1057+
link_fd = err;
1058+
} else {
1059+
err = bpf_prog_attach(prog_fd, cgroup_fd, test->attach_type, 0);
1060+
}
10551061
if (err < 0) {
10561062
if (test->error == DENY_ATTACH)
10571063
goto close_prog_fd;
@@ -1142,7 +1148,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
11421148
close_sock_fd:
11431149
close(sock_fd);
11441150
detach_prog:
1145-
bpf_prog_detach2(prog_fd, cgroup_fd, test->attach_type);
1151+
if (use_link) {
1152+
if (link_fd >= 0)
1153+
close(link_fd);
1154+
} else {
1155+
bpf_prog_detach2(prog_fd, cgroup_fd, test->attach_type);
1156+
}
11461157
close_prog_fd:
11471158
close(prog_fd);
11481159
return ret;
@@ -1160,10 +1171,12 @@ void test_sockopt(void)
11601171
if (!test__start_subtest(tests[i].descr))
11611172
continue;
11621173

1163-
ASSERT_OK(run_test(cgroup_fd, &tests[i], false),
1174+
ASSERT_OK(run_test(cgroup_fd, &tests[i], false, false),
1175+
tests[i].descr);
1176+
ASSERT_OK(run_test(cgroup_fd, &tests[i], false, true),
11641177
tests[i].descr);
11651178
if (tests[i].io_uring_support)
1166-
ASSERT_OK(run_test(cgroup_fd, &tests[i], true),
1179+
ASSERT_OK(run_test(cgroup_fd, &tests[i], true, false),
11671180
tests[i].descr);
11681181
}
11691182

0 commit comments

Comments
 (0)