Skip to content

Commit c1dcc05

Browse files
laoarAlexei Starovoitov
authored andcommitted
selftests/bpf: Add a new cgroup helper get_classid_cgroup_id()
Introduce a new helper function to retrieve the cgroup ID from a net_cls cgroup directory. Signed-off-by: Yafang Shao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f744d35 commit c1dcc05

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

tools/testing/selftests/bpf/cgroup_helpers.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,26 +422,23 @@ int create_and_get_cgroup(const char *relative_path)
422422
}
423423

424424
/**
425-
* get_cgroup_id() - Get cgroup id for a particular cgroup path
426-
* @relative_path: The cgroup path, relative to the workdir, to join
425+
* get_cgroup_id_from_path - Get cgroup id for a particular cgroup path
426+
* @cgroup_workdir: The absolute cgroup path
427427
*
428428
* On success, it returns the cgroup id. On failure it returns 0,
429429
* which is an invalid cgroup id.
430430
* If there is a failure, it prints the error to stderr.
431431
*/
432-
unsigned long long get_cgroup_id(const char *relative_path)
432+
unsigned long long get_cgroup_id_from_path(const char *cgroup_workdir)
433433
{
434434
int dirfd, err, flags, mount_id, fhsize;
435435
union {
436436
unsigned long long cgid;
437437
unsigned char raw_bytes[8];
438438
} id;
439-
char cgroup_workdir[PATH_MAX + 1];
440439
struct file_handle *fhp, *fhp2;
441440
unsigned long long ret = 0;
442441

443-
format_cgroup_path(cgroup_workdir, relative_path);
444-
445442
dirfd = AT_FDCWD;
446443
flags = 0;
447444
fhsize = sizeof(*fhp);
@@ -477,6 +474,14 @@ unsigned long long get_cgroup_id(const char *relative_path)
477474
return ret;
478475
}
479476

477+
unsigned long long get_cgroup_id(const char *relative_path)
478+
{
479+
char cgroup_workdir[PATH_MAX + 1];
480+
481+
format_cgroup_path(cgroup_workdir, relative_path);
482+
return get_cgroup_id_from_path(cgroup_workdir);
483+
}
484+
480485
int cgroup_setup_and_join(const char *path) {
481486
int cg_fd;
482487

@@ -621,3 +626,14 @@ void cleanup_classid_environment(void)
621626
join_cgroup_from_top(NETCLS_MOUNT_PATH);
622627
nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT);
623628
}
629+
630+
/**
631+
* get_classid_cgroup_id - Get the cgroup id of a net_cls cgroup
632+
*/
633+
unsigned long long get_classid_cgroup_id(void)
634+
{
635+
char cgroup_workdir[PATH_MAX + 1];
636+
637+
format_classid_path(cgroup_workdir);
638+
return get_cgroup_id_from_path(cgroup_workdir);
639+
}

tools/testing/selftests/bpf/cgroup_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void cleanup_cgroup_environment(void);
3131
/* cgroupv1 related */
3232
int set_classid(void);
3333
int join_classid(void);
34+
unsigned long long get_classid_cgroup_id(void);
3435

3536
int setup_classid_environment(void);
3637
void cleanup_classid_environment(void);

0 commit comments

Comments
 (0)