1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/limits.h>
10 #include <linux/sched.h>
15 #include "cgroup_helpers.h"
19 * To avoid relying on the system setup, when setup_cgroup_env is called
20 * we create a new mount namespace, and cgroup namespace. The cgroupv2
21 * root is mounted at CGROUP_MOUNT_PATH. Unfortunately, most people don't
22 * have cgroupv2 enabled at this point in time. It's easier to create our
23 * own mount namespace and manage it ourselves. We assume /mnt exists.
25 * Related cgroupv1 helpers are named *classid*(), since we only use the
26 * net_cls controller for tagging net_cls.classid. We assume the default
27 * mount under /sys/fs/cgroup/net_cls, which should be the case for the
28 * vast majority of users.
31 #define WALK_FD_LIMIT 16
33 #define CGROUP_MOUNT_PATH "/mnt"
34 #define CGROUP_MOUNT_DFLT "/sys/fs/cgroup"
35 #define NETCLS_MOUNT_PATH CGROUP_MOUNT_DFLT "/net_cls"
36 #define CGROUP_WORK_DIR "/cgroup-test-work-dir"
38 #define format_cgroup_path_pid(buf, path, pid) \
39 snprintf(buf, sizeof(buf), "%s%s%d%s", CGROUP_MOUNT_PATH, \
40 CGROUP_WORK_DIR, pid, path)
42 #define format_cgroup_path(buf, path) \
43 format_cgroup_path_pid(buf, path, getpid())
45 #define format_parent_cgroup_path(buf, path) \
46 format_cgroup_path_pid(buf, path, getppid())
48 #define format_classid_path(buf) \
49 snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH, \
52 static int __enable_controllers(const char *cgroup_path, const char *controllers)
54 char path[PATH_MAX + 1];
55 char enable[PATH_MAX + 1];
60 /* If not controllers are passed, enable all available controllers */
62 snprintf(path, sizeof(path), "%s/cgroup.controllers",
64 fd = open(path, O_RDONLY);
66 log_err("Opening cgroup.controllers: %s", path);
69 len = read(fd, enable, sizeof(enable) - 1);
72 log_err("Reading cgroup.controllers: %s", path);
74 } else if (len == 0) { /* No controllers to enable */
81 bpf_strlcpy(enable, controllers, sizeof(enable));
84 snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
85 cfd = open(path, O_RDWR);
87 log_err("Opening cgroup.subtree_control: %s", path);
91 for (c = strtok_r(enable, " ", &c2); c; c = strtok_r(NULL, " ", &c2)) {
92 if (dprintf(cfd, "+%s\n", c) <= 0) {
93 log_err("Enabling controller %s: %s", c, path);
103 * enable_controllers() - Enable cgroup v2 controllers
104 * @relative_path: The cgroup path, relative to the workdir
105 * @controllers: List of controllers to enable in cgroup.controllers format
108 * Enable given cgroup v2 controllers, if @controllers is NULL, enable all
109 * available controllers.
111 * If successful, 0 is returned.
113 int enable_controllers(const char *relative_path, const char *controllers)
115 char cgroup_path[PATH_MAX + 1];
117 format_cgroup_path(cgroup_path, relative_path);
118 return __enable_controllers(cgroup_path, controllers);
121 static int __write_cgroup_file(const char *cgroup_path, const char *file,
124 char file_path[PATH_MAX + 1];
127 snprintf(file_path, sizeof(file_path), "%s/%s", cgroup_path, file);
128 fd = open(file_path, O_RDWR);
130 log_err("Opening %s", file_path);
134 if (dprintf(fd, "%s", buf) <= 0) {
135 log_err("Writing to %s", file_path);
144 * write_cgroup_file() - Write to a cgroup file
145 * @relative_path: The cgroup path, relative to the workdir
146 * @file: The name of the file in cgroupfs to write to
147 * @buf: Buffer to write to the file
149 * Write to a file in the given cgroup's directory.
151 * If successful, 0 is returned.
153 int write_cgroup_file(const char *relative_path, const char *file,
156 char cgroup_path[PATH_MAX - 24];
158 format_cgroup_path(cgroup_path, relative_path);
159 return __write_cgroup_file(cgroup_path, file, buf);
163 * write_cgroup_file_parent() - Write to a cgroup file in the parent process
165 * @relative_path: The cgroup path, relative to the parent process workdir
166 * @file: The name of the file in cgroupfs to write to
167 * @buf: Buffer to write to the file
169 * Write to a file in the given cgroup's directory under the parent process
172 * If successful, 0 is returned.
174 int write_cgroup_file_parent(const char *relative_path, const char *file,
177 char cgroup_path[PATH_MAX - 24];
179 format_parent_cgroup_path(cgroup_path, relative_path);
180 return __write_cgroup_file(cgroup_path, file, buf);
184 * setup_cgroup_environment() - Setup the cgroup environment
186 * After calling this function, cleanup_cgroup_environment should be called
187 * once testing is complete.
189 * This function will print an error to stderr and return 1 if it is unable
190 * to setup the cgroup environment. If setup is successful, 0 is returned.
192 int setup_cgroup_environment(void)
194 char cgroup_workdir[PATH_MAX - 24];
196 format_cgroup_path(cgroup_workdir, "");
198 if (unshare(CLONE_NEWNS)) {
203 if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
204 log_err("mount fakeroot");
208 if (mount("none", CGROUP_MOUNT_PATH, "cgroup2", 0, NULL) && errno != EBUSY) {
209 log_err("mount cgroup2");
213 /* Cleanup existing failed runs, now that the environment is setup */
214 cleanup_cgroup_environment();
216 if (mkdir(cgroup_workdir, 0777) && errno != EEXIST) {
217 log_err("mkdir cgroup work dir");
221 /* Enable all available controllers to increase test coverage */
222 if (__enable_controllers(CGROUP_MOUNT_PATH, NULL) ||
223 __enable_controllers(cgroup_workdir, NULL))
229 static int nftwfunc(const char *filename, const struct stat *statptr,
230 int fileflags, struct FTW *pfwt)
232 if ((fileflags & FTW_D) && rmdir(filename))
233 log_err("Removing cgroup: %s", filename);
237 static int join_cgroup_from_top(const char *cgroup_path)
239 char cgroup_procs_path[PATH_MAX + 1];
240 pid_t pid = getpid();
243 snprintf(cgroup_procs_path, sizeof(cgroup_procs_path),
244 "%s/cgroup.procs", cgroup_path);
246 fd = open(cgroup_procs_path, O_WRONLY);
248 log_err("Opening Cgroup Procs: %s", cgroup_procs_path);
252 if (dprintf(fd, "%d\n", pid) < 0) {
253 log_err("Joining Cgroup");
262 * join_cgroup() - Join a cgroup
263 * @relative_path: The cgroup path, relative to the workdir, to join
265 * This function expects a cgroup to already be created, relative to the cgroup
266 * work dir, and it joins it. For example, passing "/my-cgroup" as the path
267 * would actually put the calling process into the cgroup
268 * "/cgroup-test-work-dir/my-cgroup"
270 * On success, it returns 0, otherwise on failure it returns 1.
272 int join_cgroup(const char *relative_path)
274 char cgroup_path[PATH_MAX + 1];
276 format_cgroup_path(cgroup_path, relative_path);
277 return join_cgroup_from_top(cgroup_path);
281 * join_parent_cgroup() - Join a cgroup in the parent process workdir
282 * @relative_path: The cgroup path, relative to parent process workdir, to join
286 * On success, it returns 0, otherwise on failure it returns 1.
288 int join_parent_cgroup(const char *relative_path)
290 char cgroup_path[PATH_MAX + 1];
292 format_parent_cgroup_path(cgroup_path, relative_path);
293 return join_cgroup_from_top(cgroup_path);
297 * cleanup_cgroup_environment() - Cleanup Cgroup Testing Environment
299 * This is an idempotent function to delete all temporary cgroups that
300 * have been created during the test, including the cgroup testing work
303 * At call time, it moves the calling process to the root cgroup, and then
304 * runs the deletion process. It is idempotent, and should not fail, unless
305 * a process is lingering.
307 * On failure, it will print an error to stderr, and try to continue.
309 void cleanup_cgroup_environment(void)
311 char cgroup_workdir[PATH_MAX + 1];
313 format_cgroup_path(cgroup_workdir, "");
314 join_cgroup_from_top(CGROUP_MOUNT_PATH);
315 nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT);
319 * get_root_cgroup() - Get the FD of the root cgroup
321 * On success, it returns the file descriptor. On failure, it returns -1.
322 * If there is a failure, it prints the error to stderr.
324 int get_root_cgroup(void)
328 fd = open(CGROUP_MOUNT_PATH, O_RDONLY);
330 log_err("Opening root cgroup");
337 * create_and_get_cgroup() - Create a cgroup, relative to workdir, and get the FD
338 * @relative_path: The cgroup path, relative to the workdir, to join
340 * This function creates a cgroup under the top level workdir and returns the
341 * file descriptor. It is idempotent.
343 * On success, it returns the file descriptor. On failure it returns -1.
344 * If there is a failure, it prints the error to stderr.
346 int create_and_get_cgroup(const char *relative_path)
348 char cgroup_path[PATH_MAX + 1];
351 format_cgroup_path(cgroup_path, relative_path);
352 if (mkdir(cgroup_path, 0777) && errno != EEXIST) {
353 log_err("mkdiring cgroup %s .. %s", relative_path, cgroup_path);
357 fd = open(cgroup_path, O_RDONLY);
359 log_err("Opening Cgroup");
367 * get_cgroup_id() - Get cgroup id for a particular cgroup path
368 * @relative_path: The cgroup path, relative to the workdir, to join
370 * On success, it returns the cgroup id. On failure it returns 0,
371 * which is an invalid cgroup id.
372 * If there is a failure, it prints the error to stderr.
374 unsigned long long get_cgroup_id(const char *relative_path)
376 int dirfd, err, flags, mount_id, fhsize;
378 unsigned long long cgid;
379 unsigned char raw_bytes[8];
381 char cgroup_workdir[PATH_MAX + 1];
382 struct file_handle *fhp, *fhp2;
383 unsigned long long ret = 0;
385 format_cgroup_path(cgroup_workdir, relative_path);
389 fhsize = sizeof(*fhp);
390 fhp = calloc(1, fhsize);
395 err = name_to_handle_at(dirfd, cgroup_workdir, fhp, &mount_id, flags);
396 if (err >= 0 || fhp->handle_bytes != 8) {
397 log_err("name_to_handle_at");
401 fhsize = sizeof(struct file_handle) + fhp->handle_bytes;
402 fhp2 = realloc(fhp, fhsize);
407 err = name_to_handle_at(dirfd, cgroup_workdir, fhp2, &mount_id, flags);
410 log_err("name_to_handle_at");
414 memcpy(id.raw_bytes, fhp->f_handle, 8);
422 int cgroup_setup_and_join(const char *path) {
425 if (setup_cgroup_environment()) {
426 fprintf(stderr, "Failed to setup cgroup environment\n");
430 cg_fd = create_and_get_cgroup(path);
432 fprintf(stderr, "Failed to create test cgroup\n");
433 cleanup_cgroup_environment();
437 if (join_cgroup(path)) {
438 fprintf(stderr, "Failed to join cgroup\n");
439 cleanup_cgroup_environment();
446 * setup_classid_environment() - Setup the cgroupv1 net_cls environment
448 * After calling this function, cleanup_classid_environment should be called
449 * once testing is complete.
451 * This function will print an error to stderr and return 1 if it is unable
452 * to setup the cgroup environment. If setup is successful, 0 is returned.
454 int setup_classid_environment(void)
456 char cgroup_workdir[PATH_MAX + 1];
458 format_classid_path(cgroup_workdir);
460 if (mount("tmpfs", CGROUP_MOUNT_DFLT, "tmpfs", 0, NULL) &&
462 log_err("mount cgroup base");
466 if (mkdir(NETCLS_MOUNT_PATH, 0777) && errno != EEXIST) {
467 log_err("mkdir cgroup net_cls");
471 if (mount("net_cls", NETCLS_MOUNT_PATH, "cgroup", 0, "net_cls") &&
473 log_err("mount cgroup net_cls");
477 cleanup_classid_environment();
479 if (mkdir(cgroup_workdir, 0777) && errno != EEXIST) {
480 log_err("mkdir cgroup work dir");
488 * set_classid() - Set a cgroupv1 net_cls classid
489 * @id: the numeric classid
491 * Writes the passed classid into the cgroup work dir's net_cls.classid
492 * file in order to later on trigger socket tagging.
494 * On success, it returns 0, otherwise on failure it returns 1. If there
495 * is a failure, it prints the error to stderr.
497 int set_classid(unsigned int id)
499 char cgroup_workdir[PATH_MAX - 42];
500 char cgroup_classid_path[PATH_MAX + 1];
503 format_classid_path(cgroup_workdir);
504 snprintf(cgroup_classid_path, sizeof(cgroup_classid_path),
505 "%s/net_cls.classid", cgroup_workdir);
507 fd = open(cgroup_classid_path, O_WRONLY);
509 log_err("Opening cgroup classid: %s", cgroup_classid_path);
513 if (dprintf(fd, "%u\n", id) < 0) {
514 log_err("Setting cgroup classid");
523 * join_classid() - Join a cgroupv1 net_cls classid
525 * This function expects the cgroup work dir to be already created, as we
526 * join it here. This causes the process sockets to be tagged with the given
529 * On success, it returns 0, otherwise on failure it returns 1.
531 int join_classid(void)
533 char cgroup_workdir[PATH_MAX + 1];
535 format_classid_path(cgroup_workdir);
536 return join_cgroup_from_top(cgroup_workdir);
540 * cleanup_classid_environment() - Cleanup the cgroupv1 net_cls environment
542 * At call time, it moves the calling process to the root cgroup, and then
543 * runs the deletion process.
545 * On failure, it will print an error to stderr, and try to continue.
547 void cleanup_classid_environment(void)
549 char cgroup_workdir[PATH_MAX + 1];
551 format_classid_path(cgroup_workdir);
552 join_cgroup_from_top(NETCLS_MOUNT_PATH);
553 nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT);