tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c
Extension
.c
Size
4373 bytes
Lines
164
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2023 Yafang Shao <laoar.shao@gmail.com> */

#include <sys/types.h>
#include <unistd.h>
#include <test_progs.h>
#include "cgroup_helpers.h"
#include "test_cgroup1_hierarchy.skel.h"

static void bpf_cgroup1(struct test_cgroup1_hierarchy *skel)
{
	struct bpf_link *lsm_link, *fentry_link;
	int err;

	/* Attach LSM prog first */
	lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run);
	if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
		return;

	/* LSM prog will be triggered when attaching fentry */
	fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
	ASSERT_NULL(fentry_link, "fentry_attach_fail");

	err = bpf_link__destroy(lsm_link);
	ASSERT_OK(err, "destroy_lsm");
}

static void bpf_cgroup1_sleepable(struct test_cgroup1_hierarchy *skel)
{
	struct bpf_link *lsm_link, *fentry_link;
	int err;

	/* Attach LSM prog first */
	lsm_link = bpf_program__attach_lsm(skel->progs.lsm_s_run);
	if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
		return;

	/* LSM prog will be triggered when attaching fentry */
	fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
	ASSERT_NULL(fentry_link, "fentry_attach_fail");

	err = bpf_link__destroy(lsm_link);
	ASSERT_OK(err, "destroy_lsm");
}

static void bpf_cgroup1_invalid_id(struct test_cgroup1_hierarchy *skel)
{
	struct bpf_link *lsm_link, *fentry_link;
	int err;

	/* Attach LSM prog first */
	lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run);
	if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
		return;

	/* LSM prog will be triggered when attaching fentry */
	fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
	if (!ASSERT_OK_PTR(fentry_link, "fentry_attach_success"))
		goto cleanup;

	err = bpf_link__destroy(fentry_link);
	ASSERT_OK(err, "destroy_lsm");

cleanup:
	err = bpf_link__destroy(lsm_link);
	ASSERT_OK(err, "destroy_fentry");
}

void test_cgroup1_hierarchy(void)
{
	struct test_cgroup1_hierarchy *skel;
	__u64 current_cgid;
	int hid, err;

	skel = test_cgroup1_hierarchy__open();
	if (!ASSERT_OK_PTR(skel, "open"))
		return;

	skel->bss->target_pid = getpid();

	err = bpf_program__set_attach_target(skel->progs.fentry_run, 0, "bpf_fentry_test1");
	if (!ASSERT_OK(err, "fentry_set_target"))
		goto destroy;

	err = test_cgroup1_hierarchy__load(skel);
	if (!ASSERT_OK(err, "load"))
		goto destroy;

	/* Setup cgroup1 hierarchy */
	err = setup_cgroup_environment();

Annotation

Implementation Notes