selftests/bpf: Extend sockopt tests to use BPF_LINK_CREATE
authorStanislav Fomichev <sdf@google.com>
Fri, 26 Apr 2024 23:16:19 +0000 (16:16 -0700)
committerMartin KaFai Lau <martin.lau@kernel.org>
Tue, 30 Apr 2024 17:43:37 +0000 (10:43 -0700)
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>
tools/testing/selftests/bpf/prog_tests/sockopt.c

index 5a4491d4edfe4730b95f4bbc2236bb39f2bb607c..dea340996e974d763cc894f8e3d175f28bf405fd 100644 (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 @@ free_optval:
 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);
        }