[RFC PATCH bpf-next 5/5] selftests/bpf: Add selftests for current_under_cgroupv1v2

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add selftests for bpf_current_task_under_cgroup() on both cgroup1 and
cgroup2. The result as follows,

  $ tools/testing/selftests/bpf/test_progs --name=current_under_cgroupv1v2
  #62/1    current_under_cgroupv1v2/test_current_under_cgroup2:OK
  #62/2    current_under_cgroupv1v2/test_current_under_cgroup1:OK
  #62      current_under_cgroupv1v2:OK
  Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Yafang Shao <laoar.shao@xxxxxxxxx>
---
 .../bpf/prog_tests/current_under_cgroupv1v2.c      | 76 ++++++++++++++++++++++
 .../bpf/progs/test_current_under_cgroupv1v2.c      | 31 +++++++++
 2 files changed, 107 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/current_under_cgroupv1v2.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_current_under_cgroupv1v2.c

diff --git a/tools/testing/selftests/bpf/prog_tests/current_under_cgroupv1v2.c b/tools/testing/selftests/bpf/prog_tests/current_under_cgroupv1v2.c
new file mode 100644
index 0000000..62efca3
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/current_under_cgroupv1v2.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "cgroup_helpers.h"
+#include "test_current_under_cgroupv1v2.skel.h"
+
+#define CGROUP2_DIR "/current_under_cgroup2"
+
+static void attach_progs(int cgrp_fd)
+{
+	struct test_current_under_cgroupv1v2 *skel;
+	int cgrp_map_fd, ret, idx = 0;
+
+	skel = test_current_under_cgroupv1v2__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_current_under_cgroupv1v2__open"))
+		return;
+
+	cgrp_map_fd = bpf_map__fd(skel->maps.cgrp_map);
+	ret = bpf_map_update_elem(cgrp_map_fd, &idx, &cgrp_fd, BPF_ANY);
+	if (!ASSERT_OK(ret, "update_cgrp_map"))
+		goto cleanup;
+
+	/* Attach LSM prog first */
+	skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run);
+	if (!ASSERT_OK_PTR(skel->links.lsm_run, "lsm_attach"))
+		goto cleanup;
+
+	/* LSM prog will be triggered when attaching fentry */
+	skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run);
+	ASSERT_NULL(skel->links.fentry_run, "fentry_attach");
+
+cleanup:
+	test_current_under_cgroupv1v2__destroy(skel);
+}
+
+static void current_under_cgroup1(void)
+{
+	int cgrp_fd, ret;
+
+	/* Setup cgroup1 hierarchy */
+	ret = setup_classid_environment();
+	if (!ASSERT_OK(ret, "setup_classid_environment"))
+		return;
+
+	ret = join_classid();
+	if (!ASSERT_OK(ret, "join_cgroup1"))
+		goto cleanup;
+
+	cgrp_fd = open_classid();
+	attach_progs(cgrp_fd);
+	close(cgrp_fd);
+
+cleanup:
+	/* Cleanup cgroup1 hierarchy */
+	cleanup_classid_environment();
+}
+
+static void current_under_cgroup2(void)
+{
+	int cgrp_fd;
+
+	cgrp_fd = test__join_cgroup(CGROUP2_DIR);
+	if (!ASSERT_GE(cgrp_fd, 0, "cgroup_join_cgroup2"))
+		return;
+
+	attach_progs(cgrp_fd);
+	close(cgrp_fd);
+}
+
+void test_current_under_cgroupv1v2(void)
+{
+	if (test__start_subtest("test_current_under_cgroup2"))
+		current_under_cgroup2();
+	if (test__start_subtest("test_current_under_cgroup1"))
+		current_under_cgroup1();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_current_under_cgroupv1v2.c b/tools/testing/selftests/bpf/progs/test_current_under_cgroupv1v2.c
new file mode 100644
index 0000000..9f0af0b
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_current_under_cgroupv1v2.c
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
+	__uint(key_size, sizeof(u32));
+	__uint(value_size, sizeof(u32));
+	__uint(max_entries, 1);
+} cgrp_map SEC(".maps");
+
+SEC("lsm/bpf")
+int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
+{
+	if (cmd != BPF_LINK_CREATE)
+		return 0;
+
+	if (bpf_current_task_under_cgroup(&cgrp_map, 0 /* map index */))
+		return -1;
+	return 0;
+}
+
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(fentry_run)
+{
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
1.8.3.1




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]     [Monitors]

  Powered by Linux