bpf, selftests: Use single cgroup helpers for both test_sockmap/progs
authorJohn Fastabend <john.fastabend@gmail.com>
Fri, 31 Jul 2020 22:09:14 +0000 (15:09 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 2 Aug 2020 03:20:59 +0000 (20:20 -0700)
Nearly every user of cgroup helpers does the same sequence of API calls. So
push these into a single helper cgroup_setup_and_join. The cases that do
a bit of extra logic are test_progs which currently uses an env variable
to decide if it needs to setup the cgroup environment or can use an
existingi environment. And then tests that are doing cgroup tests
themselves. We skip these cases for now.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/159623335418.30208.15807461815525100199.stgit@john-XPS-13-9370
15 files changed:
tools/testing/selftests/bpf/cgroup_helpers.c
tools/testing/selftests/bpf/cgroup_helpers.h
tools/testing/selftests/bpf/get_cgroup_id_user.c
tools/testing/selftests/bpf/test_cgroup_storage.c
tools/testing/selftests/bpf/test_dev_cgroup.c
tools/testing/selftests/bpf/test_netcnt.c
tools/testing/selftests/bpf/test_skb_cgroup_id_user.c
tools/testing/selftests/bpf/test_sock.c
tools/testing/selftests/bpf/test_sock_addr.c
tools/testing/selftests/bpf/test_sock_fields.c
tools/testing/selftests/bpf/test_socket_cookie.c
tools/testing/selftests/bpf/test_sockmap.c
tools/testing/selftests/bpf/test_sysctl.c
tools/testing/selftests/bpf/test_tcpbpf_user.c
tools/testing/selftests/bpf/test_tcpnotify_user.c

index 0fb910d..0330517 100644 (file)
@@ -290,3 +290,26 @@ free_mem:
        free(fhp);
        return ret;
 }
+
+int cgroup_setup_and_join(const char *path) {
+       int cg_fd;
+
+       if (setup_cgroup_environment()) {
+               fprintf(stderr, "Failed to setup cgroup environment\n");
+               return -EINVAL;
+       }
+
+       cg_fd = create_and_get_cgroup(path);
+       if (cg_fd < 0) {
+               fprintf(stderr, "Failed to create test cgroup\n");
+               cleanup_cgroup_environment();
+               return cg_fd;
+       }
+
+       if (join_cgroup(path)) {
+               fprintf(stderr, "Failed to join cgroup\n");
+               cleanup_cgroup_environment();
+               return -EINVAL;
+       }
+       return cg_fd;
+}
index d64bb89..5fe3d88 100644 (file)
@@ -9,6 +9,7 @@
        __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
 
 
+int cgroup_setup_and_join(const char *path);
 int create_and_get_cgroup(const char *path);
 int join_cgroup(const char *path);
 int setup_cgroup_environment(void);
index e8da7b3..b8d6aef 100644 (file)
@@ -58,20 +58,10 @@ int main(int argc, char **argv)
        int exit_code = 1;
        char buf[256];
 
-       err = setup_cgroup_environment();
-       if (CHECK(err, "setup_cgroup_environment", "err %d errno %d\n", err,
-                 errno))
+       cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
+       if (CHECK(cgroup_fd < 0, "cgroup_setup_and_join", "err %d errno %d\n", cgroup_fd, errno))
                return 1;
 
-       cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
-       if (CHECK(cgroup_fd < 0, "create_and_get_cgroup", "err %d errno %d\n",
-                 cgroup_fd, errno))
-               goto cleanup_cgroup_env;
-
-       err = join_cgroup(TEST_CGROUP);
-       if (CHECK(err, "join_cgroup", "err %d errno %d\n", err, errno))
-               goto cleanup_cgroup_env;
-
        err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
        if (CHECK(err, "bpf_prog_load", "err %d errno %d\n", err, errno))
                goto cleanup_cgroup_env;
index 6557290..d946252 100644 (file)
@@ -74,22 +74,7 @@ int main(int argc, char **argv)
                goto out;
        }
 
-       if (setup_cgroup_environment()) {
-               printf("Failed to setup cgroup environment\n");
-               goto err;
-       }
-
-       /* Create a cgroup, get fd, and join it */
-       cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
-       if (cgroup_fd < 0) {
-               printf("Failed to create test cgroup\n");
-               goto err;
-       }
-
-       if (join_cgroup(TEST_CGROUP)) {
-               printf("Failed to join cgroup\n");
-               goto err;
-       }
+       cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
 
        /* Attach the bpf program */
        if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) {
index d850fb9..804dddd 100644 (file)
@@ -33,21 +33,10 @@ int main(int argc, char **argv)
                goto out;
        }
 
-       if (setup_cgroup_environment()) {
-               printf("Failed to load DEV_CGROUP program\n");
-               goto err;
-       }
-
-       /* Create a cgroup, get fd, and join it */
-       cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
+       cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
        if (cgroup_fd < 0) {
                printf("Failed to create test cgroup\n");
-               goto err;
-       }
-
-       if (join_cgroup(TEST_CGROUP)) {
-               printf("Failed to join cgroup\n");
-               goto err;
+               goto out;
        }
 
        /* Attach bpf program */
index 7a68c90..a7b9a69 100644 (file)
@@ -58,22 +58,9 @@ int main(int argc, char **argv)
                goto out;
        }
 
-       if (setup_cgroup_environment()) {
-               printf("Failed to load bpf program\n");
-               goto err;
-       }
-
-       /* Create a cgroup, get fd, and join it */
-       cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
-       if (cgroup_fd < 0) {
-               printf("Failed to create test cgroup\n");
+       cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
+       if (cgroup_fd < 0)
                goto err;
-       }
-
-       if (join_cgroup(TEST_CGROUP)) {
-               printf("Failed to join cgroup\n");
-               goto err;
-       }
 
        /* Attach bpf program */
        if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) {
index 356351c..4a64306 100644 (file)
@@ -160,16 +160,10 @@ int main(int argc, char **argv)
                exit(EXIT_FAILURE);
        }
 
-       if (setup_cgroup_environment())
-               goto err;
-
-       cgfd = create_and_get_cgroup(CGROUP_PATH);
+       cgfd = cgroup_setup_and_join(CGROUP_PATH);
        if (cgfd < 0)
                goto err;
 
-       if (join_cgroup(CGROUP_PATH))
-               goto err;
-
        if (send_packet(argv[1]))
                goto err;
 
index 52bf149..9613f75 100644 (file)
@@ -464,16 +464,10 @@ int main(int argc, char **argv)
        int cgfd = -1;
        int err = 0;
 
-       if (setup_cgroup_environment())
-               goto err;
-
-       cgfd = create_and_get_cgroup(CG_PATH);
+       cgfd = cgroup_setup_and_join(CG_PATH);
        if (cgfd < 0)
                goto err;
 
-       if (join_cgroup(CG_PATH))
-               goto err;
-
        if (run_tests(cgfd))
                goto err;
 
index 0358814..b8c72c1 100644 (file)
@@ -1638,16 +1638,10 @@ int main(int argc, char **argv)
                exit(err);
        }
 
-       if (setup_cgroup_environment())
-               goto err;
-
-       cgfd = create_and_get_cgroup(CG_PATH);
+       cgfd = cgroup_setup_and_join(CG_PATH);
        if (cgfd < 0)
                goto err;
 
-       if (join_cgroup(CG_PATH))
-               goto err;
-
        if (run_tests(cgfd))
                goto err;
 
index f0fc103..6c9f269 100644 (file)
@@ -421,19 +421,11 @@ int main(int argc, char **argv)
        struct bpf_object *obj;
        struct bpf_map *map;
 
-       err = setup_cgroup_environment();
-       CHECK(err, "setup_cgroup_environment()", "err:%d errno:%d",
-             err, errno);
-
-       atexit(cleanup_cgroup_environment);
-
        /* Create a cgroup, get fd, and join it */
-       cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
-       CHECK(cgroup_fd == -1, "create_and_get_cgroup()",
+       cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
+       CHECK(cgroup_fd < 0, "cgroup_setup_and_join()",
              "cgroup_fd:%d errno:%d", cgroup_fd, errno);
-
-       err = join_cgroup(TEST_CGROUP);
-       CHECK(err, "join_cgroup", "err:%d errno:%d", err, errno);
+       atexit(cleanup_cgroup_environment);
 
        err = bpf_prog_load_xattr(&attr, &obj, &egress_fd);
        CHECK(err, "bpf_prog_load_xattr()", "err:%d", err);
index 15653b0..154a8fd 100644 (file)
@@ -191,16 +191,10 @@ int main(int argc, char **argv)
        int cgfd = -1;
        int err = 0;
 
-       if (setup_cgroup_environment())
-               goto err;
-
-       cgfd = create_and_get_cgroup(CG_PATH);
+       cgfd = cgroup_setup_and_join(CG_PATH);
        if (cgfd < 0)
                goto err;
 
-       if (join_cgroup(CG_PATH))
-               goto err;
-
        if (run_test(cgfd))
                goto err;
 
index 78789b2..9b6fb00 100644 (file)
@@ -1963,23 +1963,9 @@ int main(int argc, char **argv)
        }
 
        if (!cg_fd) {
-               if (setup_cgroup_environment()) {
-                       fprintf(stderr, "ERROR: cgroup env failed\n");
-                       return -EINVAL;
-               }
-
-               cg_fd = create_and_get_cgroup(CG_PATH);
-               if (cg_fd < 0) {
-                       fprintf(stderr,
-                               "ERROR: (%i) open cg path failed: %s\n",
-                               cg_fd, strerror(errno));
+               cg_fd = cgroup_setup_and_join(CG_PATH);
+               if (cg_fd < 0)
                        return cg_fd;
-               }
-
-               if (join_cgroup(CG_PATH)) {
-                       fprintf(stderr, "ERROR: failed to join cgroup\n");
-                       return -EINVAL;
-               }
                cg_created = 1;
        }
 
index d196e2a..a20a919 100644 (file)
@@ -1619,16 +1619,10 @@ int main(int argc, char **argv)
        int cgfd = -1;
        int err = 0;
 
-       if (setup_cgroup_environment())
-               goto err;
-
-       cgfd = create_and_get_cgroup(CG_PATH);
+       cgfd = cgroup_setup_and_join(CG_PATH);
        if (cgfd < 0)
                goto err;
 
-       if (join_cgroup(CG_PATH))
-               goto err;
-
        if (run_tests(cgfd))
                goto err;
 
index 3ae1276..74a9e49 100644 (file)
@@ -102,16 +102,10 @@ int main(int argc, char **argv)
        __u32 key = 0;
        int rv;
 
-       if (setup_cgroup_environment())
-               goto err;
-
-       cg_fd = create_and_get_cgroup(cg_path);
+       cg_fd = cgroup_setup_and_join(cg_path);
        if (cg_fd < 0)
                goto err;
 
-       if (join_cgroup(cg_path))
-               goto err;
-
        if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
                printf("FAILED: load_bpf_file failed for: %s\n", file);
                goto err;
index f9765dd..8549b31 100644 (file)
@@ -86,16 +86,10 @@ int main(int argc, char **argv)
        CPU_SET(0, &cpuset);
        pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
 
-       if (setup_cgroup_environment())
-               goto err;
-
-       cg_fd = create_and_get_cgroup(cg_path);
+       cg_fd = cgroup_setup_and_join(cg_path);
        if (cg_fd < 0)
                goto err;
 
-       if (join_cgroup(cg_path))
-               goto err;
-
        if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
                printf("FAILED: load_bpf_file failed for: %s\n", file);
                goto err;