Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <laoar.shao@gmail.com>
Link: https://lore.kernel.org/r/20231111090034.4248-5-laoar.shao@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Yafang Shao and committed by
Alexei Starovoitov
c1dcc050 f744d35e

+23 -6
+22 -6
tools/testing/selftests/bpf/cgroup_helpers.c
··· 422 422 } 423 423 424 424 /** 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 427 427 * 428 428 * On success, it returns the cgroup id. On failure it returns 0, 429 429 * which is an invalid cgroup id. 430 430 * If there is a failure, it prints the error to stderr. 431 431 */ 432 - unsigned long long get_cgroup_id(const char *relative_path) 432 + unsigned long long get_cgroup_id_from_path(const char *cgroup_workdir) 433 433 { 434 434 int dirfd, err, flags, mount_id, fhsize; 435 435 union { 436 436 unsigned long long cgid; 437 437 unsigned char raw_bytes[8]; 438 438 } id; 439 - char cgroup_workdir[PATH_MAX + 1]; 440 439 struct file_handle *fhp, *fhp2; 441 440 unsigned long long ret = 0; 442 - 443 - format_cgroup_path(cgroup_workdir, relative_path); 444 441 445 442 dirfd = AT_FDCWD; 446 443 flags = 0; ··· 472 475 free_mem: 473 476 free(fhp); 474 477 return ret; 478 + } 479 + 480 + unsigned long long get_cgroup_id(const char *relative_path) 481 + { 482 + char cgroup_workdir[PATH_MAX + 1]; 483 + 484 + format_cgroup_path(cgroup_workdir, relative_path); 485 + return get_cgroup_id_from_path(cgroup_workdir); 475 486 } 476 487 477 488 int cgroup_setup_and_join(const char *path) { ··· 625 620 format_classid_path(cgroup_workdir); 626 621 join_cgroup_from_top(NETCLS_MOUNT_PATH); 627 622 nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT); 623 + } 624 + 625 + /** 626 + * get_classid_cgroup_id - Get the cgroup id of a net_cls cgroup 627 + */ 628 + unsigned long long get_classid_cgroup_id(void) 629 + { 630 + char cgroup_workdir[PATH_MAX + 1]; 631 + 632 + format_classid_path(cgroup_workdir); 633 + return get_cgroup_id_from_path(cgroup_workdir); 628 634 }
+1
tools/testing/selftests/bpf/cgroup_helpers.h
··· 31 31 /* cgroupv1 related */ 32 32 int set_classid(void); 33 33 int join_classid(void); 34 + unsigned long long get_classid_cgroup_id(void); 34 35 35 36 int setup_classid_environment(void); 36 37 void cleanup_classid_environment(void);