selftests/bpf: Add parallel support for classid
authorYafang Shao <laoar.shao@gmail.com>
Sat, 11 Nov 2023 09:00:31 +0000 (09:00 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 14 Nov 2023 16:56:56 +0000 (08:56 -0800)
Include the current pid in the classid cgroup path. This way, different
testers relying on classid-based configurations will have distinct classid
cgroup directories, enabling them to run concurrently. Additionally, we
leverage the current pid as the classid, ensuring unique identification.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Link: https://lore.kernel.org/r/20231111090034.4248-4-laoar.shao@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/cgroup_helpers.c
tools/testing/selftests/bpf/cgroup_helpers.h
tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c

index 10b5f42e65e78e66ed93e0776fc66b167031fe90..f18649a79d6464214fa1141744148b562b89c0a6 100644 (file)
 #define format_parent_cgroup_path(buf, path) \
        format_cgroup_path_pid(buf, path, getppid())
 
-#define format_classid_path(buf)                               \
-       snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH,   \
-                CGROUP_WORK_DIR)
+#define format_classid_path_pid(buf, pid)                              \
+       snprintf(buf, sizeof(buf), "%s%s%d", NETCLS_MOUNT_PATH, \
+                CGROUP_WORK_DIR, pid)
+
+#define format_classid_path(buf)       \
+       format_classid_path_pid(buf, getpid())
 
 static __thread bool cgroup_workdir_mounted;
 
@@ -551,15 +554,16 @@ int setup_classid_environment(void)
 
 /**
  * set_classid() - Set a cgroupv1 net_cls classid
- * @id: the numeric classid
  *
- * Writes the passed classid into the cgroup work dir's net_cls.classid
+ * Writes the classid into the cgroup work dir's net_cls.classid
  * file in order to later on trigger socket tagging.
  *
+ * We leverage the current pid as the classid, ensuring unique identification.
+ *
  * On success, it returns 0, otherwise on failure it returns 1. If there
  * is a failure, it prints the error to stderr.
  */
-int set_classid(unsigned int id)
+int set_classid(void)
 {
        char cgroup_workdir[PATH_MAX - 42];
        char cgroup_classid_path[PATH_MAX + 1];
@@ -575,7 +579,7 @@ int set_classid(unsigned int id)
                return 1;
        }
 
-       if (dprintf(fd, "%u\n", id) < 0) {
+       if (dprintf(fd, "%u\n", getpid()) < 0) {
                log_err("Setting cgroup classid");
                rc = 1;
        }
index 5c2cb9c8b5463f46b9c1b0b6afa811bccfee98e9..92fc41daf4a4f59eda07d449959a8bbe1436ab1f 100644 (file)
@@ -29,7 +29,7 @@ int setup_cgroup_environment(void);
 void cleanup_cgroup_environment(void);
 
 /* cgroupv1 related */
-int set_classid(unsigned int id);
+int set_classid(void);
 int join_classid(void);
 
 int setup_classid_environment(void);
index 9026b42914d31b0f97fd0cf12e25ae79507c2774..addf720428f7d5ca5ca91345552f72e02caac45a 100644 (file)
@@ -71,7 +71,7 @@ void test_cgroup_v1v2(void)
        }
        ASSERT_OK(run_test(cgroup_fd, server_fd, false), "cgroup-v2-only");
        setup_classid_environment();
-       set_classid(42);
+       set_classid();
        ASSERT_OK(run_test(cgroup_fd, server_fd, true), "cgroup-v1v2");
        cleanup_classid_environment();
        close(server_fd);