Skip to content

Commit cb3ac6b

Browse files
laoarKernel Patches Daemon
authored andcommitted
selftests/bpf: Add parallel support for classid
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>
1 parent 42820ca commit cb3ac6b

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

tools/testing/selftests/bpf/cgroup_helpers.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@
4545
#define format_parent_cgroup_path(buf, path) \
4646
format_cgroup_path_pid(buf, path, getppid())
4747

48-
#define format_classid_path(buf) \
49-
snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH, \
50-
CGROUP_WORK_DIR)
48+
#define format_classid_path_pid(buf, pid) \
49+
snprintf(buf, sizeof(buf), "%s%s%d", NETCLS_MOUNT_PATH, \
50+
CGROUP_WORK_DIR, pid)
51+
52+
#define format_classid_path(buf) \
53+
format_classid_path_pid(buf, getpid())
5154

5255
static __thread bool cgroup_workdir_mounted;
5356

@@ -551,15 +554,16 @@ int setup_classid_environment(void)
551554

552555
/**
553556
* set_classid() - Set a cgroupv1 net_cls classid
554-
* @id: the numeric classid
555557
*
556-
* Writes the passed classid into the cgroup work dir's net_cls.classid
558+
* Writes the classid into the cgroup work dir's net_cls.classid
557559
* file in order to later on trigger socket tagging.
558560
*
561+
* We leverage the current pid as the classid, ensuring unique identification.
562+
*
559563
* On success, it returns 0, otherwise on failure it returns 1. If there
560564
* is a failure, it prints the error to stderr.
561565
*/
562-
int set_classid(unsigned int id)
566+
int set_classid(void)
563567
{
564568
char cgroup_workdir[PATH_MAX - 42];
565569
char cgroup_classid_path[PATH_MAX + 1];
@@ -575,7 +579,7 @@ int set_classid(unsigned int id)
575579
return 1;
576580
}
577581

578-
if (dprintf(fd, "%u\n", id) < 0) {
582+
if (dprintf(fd, "%u\n", getpid()) < 0) {
579583
log_err("Setting cgroup classid");
580584
rc = 1;
581585
}

tools/testing/selftests/bpf/cgroup_helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int setup_cgroup_environment(void);
2929
void cleanup_cgroup_environment(void);
3030

3131
/* cgroupv1 related */
32-
int set_classid(unsigned int id);
32+
int set_classid(void);
3333
int join_classid(void);
3434

3535
int setup_classid_environment(void);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void test_cgroup_v1v2(void)
7171
}
7272
ASSERT_OK(run_test(cgroup_fd, server_fd, false), "cgroup-v2-only");
7373
setup_classid_environment();
74-
set_classid(42);
74+
set_classid();
7575
ASSERT_OK(run_test(cgroup_fd, server_fd, true), "cgroup-v1v2");
7676
cleanup_classid_environment();
7777
close(server_fd);

0 commit comments

Comments
 (0)