|
12 | 12 | #include <unistd.h>
|
13 | 13 | #include <ftw.h>
|
14 | 14 |
|
15 |
| - |
16 | 15 | #include "cgroup_helpers.h"
|
17 | 16 |
|
18 | 17 | /*
|
19 | 18 | * To avoid relying on the system setup, when setup_cgroup_env is called
|
20 |
| - * we create a new mount namespace, and cgroup namespace. The cgroup2 |
21 |
| - * root is mounted at CGROUP_MOUNT_PATH |
22 |
| - * |
23 |
| - * Unfortunately, most people don't have cgroupv2 enabled at this point in time. |
24 |
| - * It's easier to create our own mount namespace and manage it ourselves. |
| 19 | + * we create a new mount namespace, and cgroup namespace. The cgroupv2 |
| 20 | + * root is mounted at CGROUP_MOUNT_PATH. Unfortunately, most people don't |
| 21 | + * have cgroupv2 enabled at this point in time. It's easier to create our |
| 22 | + * own mount namespace and manage it ourselves. We assume /mnt exists. |
25 | 23 | *
|
26 |
| - * We assume /mnt exists. |
| 24 | + * Related cgroupv1 helpers are named *classid*(), since we only use the |
| 25 | + * net_cls controller for tagging net_cls.classid. We assume the default |
| 26 | + * mount under /sys/fs/cgroup/net_cls, which should be the case for the |
| 27 | + * vast majority of users. |
27 | 28 | */
|
28 | 29 |
|
29 | 30 | #define WALK_FD_LIMIT 16
|
| 31 | + |
30 | 32 | #define CGROUP_MOUNT_PATH "/mnt"
|
| 33 | +#define CGROUP_MOUNT_DFLT "/sys/fs/cgroup" |
| 34 | +#define NETCLS_MOUNT_PATH CGROUP_MOUNT_DFLT "/net_cls" |
31 | 35 | #define CGROUP_WORK_DIR "/cgroup-test-work-dir"
|
| 36 | + |
32 | 37 | #define format_cgroup_path(buf, path) \
|
33 | 38 | snprintf(buf, sizeof(buf), "%s%s%s", CGROUP_MOUNT_PATH, \
|
34 | 39 | CGROUP_WORK_DIR, path)
|
35 | 40 |
|
| 41 | +#define format_classid_path(buf) \ |
| 42 | + snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH, \ |
| 43 | + CGROUP_WORK_DIR) |
| 44 | + |
36 | 45 | /**
|
37 | 46 | * enable_all_controllers() - Enable all available cgroup v2 controllers
|
38 | 47 | *
|
@@ -139,8 +148,7 @@ static int nftwfunc(const char *filename, const struct stat *statptr,
|
139 | 148 | return 0;
|
140 | 149 | }
|
141 | 150 |
|
142 |
| - |
143 |
| -static int join_cgroup_from_top(char *cgroup_path) |
| 151 | +static int join_cgroup_from_top(const char *cgroup_path) |
144 | 152 | {
|
145 | 153 | char cgroup_procs_path[PATH_MAX + 1];
|
146 | 154 | pid_t pid = getpid();
|
@@ -313,3 +321,114 @@ int cgroup_setup_and_join(const char *path) {
|
313 | 321 | }
|
314 | 322 | return cg_fd;
|
315 | 323 | }
|
| 324 | + |
| 325 | +/** |
| 326 | + * setup_classid_environment() - Setup the cgroupv1 net_cls environment |
| 327 | + * |
| 328 | + * After calling this function, cleanup_classid_environment should be called |
| 329 | + * once testing is complete. |
| 330 | + * |
| 331 | + * This function will print an error to stderr and return 1 if it is unable |
| 332 | + * to setup the cgroup environment. If setup is successful, 0 is returned. |
| 333 | + */ |
| 334 | +int setup_classid_environment(void) |
| 335 | +{ |
| 336 | + char cgroup_workdir[PATH_MAX + 1]; |
| 337 | + |
| 338 | + format_classid_path(cgroup_workdir); |
| 339 | + |
| 340 | + if (mount("tmpfs", CGROUP_MOUNT_DFLT, "tmpfs", 0, NULL) && |
| 341 | + errno != EBUSY) { |
| 342 | + log_err("mount cgroup base"); |
| 343 | + return 1; |
| 344 | + } |
| 345 | + |
| 346 | + if (mkdir(NETCLS_MOUNT_PATH, 0777) && errno != EEXIST) { |
| 347 | + log_err("mkdir cgroup net_cls"); |
| 348 | + return 1; |
| 349 | + } |
| 350 | + |
| 351 | + if (mount("net_cls", NETCLS_MOUNT_PATH, "cgroup", 0, "net_cls") && |
| 352 | + errno != EBUSY) { |
| 353 | + log_err("mount cgroup net_cls"); |
| 354 | + return 1; |
| 355 | + } |
| 356 | + |
| 357 | + cleanup_classid_environment(); |
| 358 | + |
| 359 | + if (mkdir(cgroup_workdir, 0777) && errno != EEXIST) { |
| 360 | + log_err("mkdir cgroup work dir"); |
| 361 | + return 1; |
| 362 | + } |
| 363 | + |
| 364 | + return 0; |
| 365 | +} |
| 366 | + |
| 367 | +/** |
| 368 | + * set_classid() - Set a cgroupv1 net_cls classid |
| 369 | + * @id: the numeric classid |
| 370 | + * |
| 371 | + * Writes the passed classid into the cgroup work dir's net_cls.classid |
| 372 | + * file in order to later on trigger socket tagging. |
| 373 | + * |
| 374 | + * On success, it returns 0, otherwise on failure it returns 1. If there |
| 375 | + * is a failure, it prints the error to stderr. |
| 376 | + */ |
| 377 | +int set_classid(unsigned int id) |
| 378 | +{ |
| 379 | + char cgroup_workdir[PATH_MAX - 42]; |
| 380 | + char cgroup_classid_path[PATH_MAX + 1]; |
| 381 | + int fd, rc = 0; |
| 382 | + |
| 383 | + format_classid_path(cgroup_workdir); |
| 384 | + snprintf(cgroup_classid_path, sizeof(cgroup_classid_path), |
| 385 | + "%s/net_cls.classid", cgroup_workdir); |
| 386 | + |
| 387 | + fd = open(cgroup_classid_path, O_WRONLY); |
| 388 | + if (fd < 0) { |
| 389 | + log_err("Opening cgroup classid: %s", cgroup_classid_path); |
| 390 | + return 1; |
| 391 | + } |
| 392 | + |
| 393 | + if (dprintf(fd, "%u\n", id) < 0) { |
| 394 | + log_err("Setting cgroup classid"); |
| 395 | + rc = 1; |
| 396 | + } |
| 397 | + |
| 398 | + close(fd); |
| 399 | + return rc; |
| 400 | +} |
| 401 | + |
| 402 | +/** |
| 403 | + * join_classid() - Join a cgroupv1 net_cls classid |
| 404 | + * |
| 405 | + * This function expects the cgroup work dir to be already created, as we |
| 406 | + * join it here. This causes the process sockets to be tagged with the given |
| 407 | + * net_cls classid. |
| 408 | + * |
| 409 | + * On success, it returns 0, otherwise on failure it returns 1. |
| 410 | + */ |
| 411 | +int join_classid(void) |
| 412 | +{ |
| 413 | + char cgroup_workdir[PATH_MAX + 1]; |
| 414 | + |
| 415 | + format_classid_path(cgroup_workdir); |
| 416 | + return join_cgroup_from_top(cgroup_workdir); |
| 417 | +} |
| 418 | + |
| 419 | +/** |
| 420 | + * cleanup_classid_environment() - Cleanup the cgroupv1 net_cls environment |
| 421 | + * |
| 422 | + * At call time, it moves the calling process to the root cgroup, and then |
| 423 | + * runs the deletion process. |
| 424 | + * |
| 425 | + * On failure, it will print an error to stderr, and try to continue. |
| 426 | + */ |
| 427 | +void cleanup_classid_environment(void) |
| 428 | +{ |
| 429 | + char cgroup_workdir[PATH_MAX + 1]; |
| 430 | + |
| 431 | + format_classid_path(cgroup_workdir); |
| 432 | + join_cgroup_from_top(NETCLS_MOUNT_PATH); |
| 433 | + nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT); |
| 434 | +} |
0 commit comments