Skip to content

Commit d8079d8

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf, selftests: Add cgroup v1 net_cls classid helpers
Minimal set of helpers for net_cls classid cgroupv1 management in order to set an id, join from a process, initiate setup and teardown. cgroupv2 helpers are left as-is, but reused where possible. Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8520e22 commit d8079d8

File tree

2 files changed

+141
-12
lines changed

2 files changed

+141
-12
lines changed

tools/testing/selftests/bpf/cgroup_helpers.c

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,36 @@
1212
#include <unistd.h>
1313
#include <ftw.h>
1414

15-
1615
#include "cgroup_helpers.h"
1716

1817
/*
1918
* 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.
2523
*
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.
2728
*/
2829

2930
#define WALK_FD_LIMIT 16
31+
3032
#define CGROUP_MOUNT_PATH "/mnt"
33+
#define CGROUP_MOUNT_DFLT "/sys/fs/cgroup"
34+
#define NETCLS_MOUNT_PATH CGROUP_MOUNT_DFLT "/net_cls"
3135
#define CGROUP_WORK_DIR "/cgroup-test-work-dir"
36+
3237
#define format_cgroup_path(buf, path) \
3338
snprintf(buf, sizeof(buf), "%s%s%s", CGROUP_MOUNT_PATH, \
3439
CGROUP_WORK_DIR, path)
3540

41+
#define format_classid_path(buf) \
42+
snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH, \
43+
CGROUP_WORK_DIR)
44+
3645
/**
3746
* enable_all_controllers() - Enable all available cgroup v2 controllers
3847
*
@@ -139,8 +148,7 @@ static int nftwfunc(const char *filename, const struct stat *statptr,
139148
return 0;
140149
}
141150

142-
143-
static int join_cgroup_from_top(char *cgroup_path)
151+
static int join_cgroup_from_top(const char *cgroup_path)
144152
{
145153
char cgroup_procs_path[PATH_MAX + 1];
146154
pid_t pid = getpid();
@@ -313,3 +321,114 @@ int cgroup_setup_and_join(const char *path) {
313321
}
314322
return cg_fd;
315323
}
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+
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#ifndef __CGROUP_HELPERS_H
33
#define __CGROUP_HELPERS_H
4+
45
#include <errno.h>
56
#include <string.h>
67

78
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
89
#define log_err(MSG, ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
910
__FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
1011

11-
12+
/* cgroupv2 related */
1213
int cgroup_setup_and_join(const char *path);
1314
int create_and_get_cgroup(const char *path);
15+
unsigned long long get_cgroup_id(const char *path);
16+
1417
int join_cgroup(const char *path);
18+
1519
int setup_cgroup_environment(void);
1620
void cleanup_cgroup_environment(void);
17-
unsigned long long get_cgroup_id(const char *path);
1821

19-
#endif
22+
/* cgroupv1 related */
23+
int set_classid(unsigned int id);
24+
int join_classid(void);
25+
26+
int setup_classid_environment(void);
27+
void cleanup_classid_environment(void);
28+
29+
#endif /* __CGROUP_HELPERS_H */

0 commit comments

Comments
 (0)