~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /* Copyright (C) 2023 Yafang Shao <laoar.shao@gmail.com> */
  3 
  4 #include <sys/types.h>
  5 #include <unistd.h>
  6 #include <test_progs.h>
  7 #include "cgroup_helpers.h"
  8 #include "test_cgroup1_hierarchy.skel.h"
  9 
 10 static void bpf_cgroup1(struct test_cgroup1_hierarchy *skel)
 11 {
 12         struct bpf_link *lsm_link, *fentry_link;
 13         int err;
 14 
 15         /* Attach LSM prog first */
 16         lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run);
 17         if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
 18                 return;
 19 
 20         /* LSM prog will be triggered when attaching fentry */
 21         fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
 22         ASSERT_NULL(fentry_link, "fentry_attach_fail");
 23 
 24         err = bpf_link__destroy(lsm_link);
 25         ASSERT_OK(err, "destroy_lsm");
 26 }
 27 
 28 static void bpf_cgroup1_sleepable(struct test_cgroup1_hierarchy *skel)
 29 {
 30         struct bpf_link *lsm_link, *fentry_link;
 31         int err;
 32 
 33         /* Attach LSM prog first */
 34         lsm_link = bpf_program__attach_lsm(skel->progs.lsm_s_run);
 35         if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
 36                 return;
 37 
 38         /* LSM prog will be triggered when attaching fentry */
 39         fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
 40         ASSERT_NULL(fentry_link, "fentry_attach_fail");
 41 
 42         err = bpf_link__destroy(lsm_link);
 43         ASSERT_OK(err, "destroy_lsm");
 44 }
 45 
 46 static void bpf_cgroup1_invalid_id(struct test_cgroup1_hierarchy *skel)
 47 {
 48         struct bpf_link *lsm_link, *fentry_link;
 49         int err;
 50 
 51         /* Attach LSM prog first */
 52         lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run);
 53         if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
 54                 return;
 55 
 56         /* LSM prog will be triggered when attaching fentry */
 57         fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
 58         if (!ASSERT_OK_PTR(fentry_link, "fentry_attach_success"))
 59                 goto cleanup;
 60 
 61         err = bpf_link__destroy(fentry_link);
 62         ASSERT_OK(err, "destroy_lsm");
 63 
 64 cleanup:
 65         err = bpf_link__destroy(lsm_link);
 66         ASSERT_OK(err, "destroy_fentry");
 67 }
 68 
 69 void test_cgroup1_hierarchy(void)
 70 {
 71         struct test_cgroup1_hierarchy *skel;
 72         __u64 current_cgid;
 73         int hid, err;
 74 
 75         skel = test_cgroup1_hierarchy__open();
 76         if (!ASSERT_OK_PTR(skel, "open"))
 77                 return;
 78 
 79         skel->bss->target_pid = getpid();
 80 
 81         err = bpf_program__set_attach_target(skel->progs.fentry_run, 0, "bpf_fentry_test1");
 82         if (!ASSERT_OK(err, "fentry_set_target"))
 83                 goto destroy;
 84 
 85         err = test_cgroup1_hierarchy__load(skel);
 86         if (!ASSERT_OK(err, "load"))
 87                 goto destroy;
 88 
 89         /* Setup cgroup1 hierarchy */
 90         err = setup_cgroup_environment();
 91         if (!ASSERT_OK(err, "setup_cgroup_environment"))
 92                 goto destroy;
 93         err = setup_classid_environment();
 94         if (!ASSERT_OK(err, "setup_classid_environment"))
 95                 goto cleanup_cgroup;
 96 
 97         err = join_classid();
 98         if (!ASSERT_OK(err, "join_cgroup1"))
 99                 goto cleanup;
100 
101         current_cgid = get_classid_cgroup_id();
102         if (!ASSERT_GE(current_cgid, 0, "cgroup1 id"))
103                 goto cleanup;
104 
105         hid = get_cgroup1_hierarchy_id("net_cls");
106         if (!ASSERT_GE(hid, 0, "cgroup1 id"))
107                 goto cleanup;
108         skel->bss->target_hid = hid;
109 
110         if (test__start_subtest("test_cgroup1_hierarchy")) {
111                 skel->bss->target_ancestor_cgid = current_cgid;
112                 bpf_cgroup1(skel);
113         }
114 
115         if (test__start_subtest("test_root_cgid")) {
116                 skel->bss->target_ancestor_cgid = 1;
117                 skel->bss->target_ancestor_level = 0;
118                 bpf_cgroup1(skel);
119         }
120 
121         if (test__start_subtest("test_invalid_level")) {
122                 skel->bss->target_ancestor_cgid = 1;
123                 skel->bss->target_ancestor_level = 1;
124                 bpf_cgroup1_invalid_id(skel);
125         }
126 
127         if (test__start_subtest("test_invalid_cgid")) {
128                 skel->bss->target_ancestor_cgid = 0;
129                 bpf_cgroup1_invalid_id(skel);
130         }
131 
132         if (test__start_subtest("test_invalid_hid")) {
133                 skel->bss->target_ancestor_cgid = 1;
134                 skel->bss->target_ancestor_level = 0;
135                 skel->bss->target_hid = -1;
136                 bpf_cgroup1_invalid_id(skel);
137         }
138 
139         if (test__start_subtest("test_invalid_cgrp_name")) {
140                 skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cl");
141                 skel->bss->target_ancestor_cgid = current_cgid;
142                 bpf_cgroup1_invalid_id(skel);
143         }
144 
145         if (test__start_subtest("test_invalid_cgrp_name2")) {
146                 skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cls,");
147                 skel->bss->target_ancestor_cgid = current_cgid;
148                 bpf_cgroup1_invalid_id(skel);
149         }
150 
151         if (test__start_subtest("test_sleepable_prog")) {
152                 skel->bss->target_hid = hid;
153                 skel->bss->target_ancestor_cgid = current_cgid;
154                 bpf_cgroup1_sleepable(skel);
155         }
156 
157 cleanup:
158         cleanup_classid_environment();
159 cleanup_cgroup:
160         cleanup_cgroup_environment();
161 destroy:
162         test_cgroup1_hierarchy__destroy(skel);
163 }
164 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php