Skip to content

Commit 806e971

Browse files
borkmannNobody
authored andcommitted
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]>
1 parent 85f7bb4 commit 806e971

File tree

2 files changed

+122
-12
lines changed

2 files changed

+122
-12
lines changed

tools/testing/selftests/bpf/cgroup_helpers.c

Lines changed: 109 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,35 @@
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 exists which should be the case for
27+
* the vast majority of users.
2728
*/
2829

2930
#define WALK_FD_LIMIT 16
31+
3032
#define CGROUP_MOUNT_PATH "/mnt"
33+
#define NETCLS_MOUNT_PATH "/sys/fs/cgroup/net_cls"
3134
#define CGROUP_WORK_DIR "/cgroup-test-work-dir"
35+
3236
#define format_cgroup_path(buf, path) \
3337
snprintf(buf, sizeof(buf), "%s%s%s", CGROUP_MOUNT_PATH, \
3438
CGROUP_WORK_DIR, path)
3539

40+
#define format_classid_path(buf) \
41+
snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH, \
42+
CGROUP_WORK_DIR)
43+
3644
/**
3745
* enable_all_controllers() - Enable all available cgroup v2 controllers
3846
*
@@ -139,8 +147,7 @@ static int nftwfunc(const char *filename, const struct stat *statptr,
139147
return 0;
140148
}
141149

142-
143-
static int join_cgroup_from_top(char *cgroup_path)
150+
static int join_cgroup_from_top(const char *cgroup_path)
144151
{
145152
char cgroup_procs_path[PATH_MAX + 1];
146153
pid_t pid = getpid();
@@ -313,3 +320,96 @@ int cgroup_setup_and_join(const char *path) {
313320
}
314321
return cg_fd;
315322
}
323+
324+
/**
325+
* setup_classid_environment() - Setup the cgroupv1 net_cls environment
326+
*
327+
* After calling this function, cleanup_classid_environment should be called
328+
* once testing is complete.
329+
*
330+
* This function will print an error to stderr and return 1 if it is unable
331+
* to setup the cgroup environment. If setup is successful, 0 is returned.
332+
*/
333+
int setup_classid_environment(void)
334+
{
335+
char cgroup_workdir[PATH_MAX + 1];
336+
337+
format_classid_path(cgroup_workdir);
338+
cleanup_classid_environment();
339+
340+
if (mkdir(cgroup_workdir, 0777) && errno != EEXIST) {
341+
log_err("mkdir cgroup work dir");
342+
return 1;
343+
}
344+
345+
return 0;
346+
}
347+
348+
/**
349+
* set_classid() - Set a cgroupv1 net_cls classid
350+
* @id: the numeric classid
351+
*
352+
* Writes the passed classid into the cgroup work dir's net_cls.classid
353+
* file in order to later on trigger socket tagging.
354+
*
355+
* On success, it returns 0, otherwise on failure it returns 1. If there
356+
* is a failure, it prints the error to stderr.
357+
*/
358+
int set_classid(unsigned int id)
359+
{
360+
char cgroup_workdir[PATH_MAX - 42];
361+
char cgroup_classid_path[PATH_MAX + 1];
362+
int fd, rc = 0;
363+
364+
format_classid_path(cgroup_workdir);
365+
snprintf(cgroup_classid_path, sizeof(cgroup_classid_path),
366+
"%s/net_cls.classid", cgroup_workdir);
367+
368+
fd = open(cgroup_classid_path, O_WRONLY);
369+
if (fd < 0) {
370+
log_err("Opening cgroup classid: %s", cgroup_classid_path);
371+
return 1;
372+
}
373+
374+
if (dprintf(fd, "%u\n", id) < 0) {
375+
log_err("Setting cgroup classid");
376+
rc = 1;
377+
}
378+
379+
close(fd);
380+
return rc;
381+
}
382+
383+
/**
384+
* join_classid() - Join a cgroupv1 net_cls classid
385+
*
386+
* This function expects the cgroup work dir to be already created, as we
387+
* join it here. This causes the process sockets to be tagged with the given
388+
* net_cls classid.
389+
*
390+
* On success, it returns 0, otherwise on failure it returns 1.
391+
*/
392+
int join_classid(void)
393+
{
394+
char cgroup_workdir[PATH_MAX + 1];
395+
396+
format_classid_path(cgroup_workdir);
397+
return join_cgroup_from_top(cgroup_workdir);
398+
}
399+
400+
/**
401+
* cleanup_classid_environment() - Cleanup the cgroupv1 net_cls environment
402+
*
403+
* At call time, it moves the calling process to the root cgroup, and then
404+
* runs the deletion process.
405+
*
406+
* On failure, it will print an error to stderr, and try to continue.
407+
*/
408+
void cleanup_classid_environment(void)
409+
{
410+
char cgroup_workdir[PATH_MAX + 1];
411+
412+
format_classid_path(cgroup_workdir);
413+
join_cgroup_from_top(NETCLS_MOUNT_PATH);
414+
nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT);
415+
}
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)