On Mon, Jul 6, 2020 at 1:54 PM YiFei Zhu <zhuyifei1999@xxxxxxxxx> wrote: > > From: YiFei Zhu <zhuyifei@xxxxxxxxxx> > > This test creates a parent cgroup, and a child of that cgroup. > It attaches a cgroup_skb/egress program that simply counts packets, > to a global variable (ARRAY map), and to a CGROUP_STORAGE map. > The program is first attached to the parent cgroup only, then to > parent and child. > > The test cases sends a message within the child cgroup, and because > the program is inherited across parent / child cgroups, it will > trigger the egress program for both the parent and child, if they > exist. The program, when looking up a CGROUP_STORAGE map, uses the > cgroup and attach type of the attachment parameters; therefore, > both attaches uses different cgroup storages. > > We assert that all packet counts returns what we expects. > > Signed-off-by: YiFei Zhu <zhuyifei@xxxxxxxxxx> > --- > .../bpf/prog_tests/cg_storage_multi.c | 154 ++++++++++++++++++ > .../bpf/progs/cg_storage_multi_egress_only.c | 30 ++++ > 2 files changed, 184 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/cg_storage_multi.c > create mode 100644 tools/testing/selftests/bpf/progs/cg_storage_multi_egress_only.c > [...] > + > +static bool assert_storage(struct bpf_map *map, const char *cgroup_path, > + __u32 expected) > +{ > + struct bpf_cgroup_storage_key key = {0}; > + __u32 value; > + int map_fd; > + > + map_fd = bpf_map__fd(map); > + > + key.cgroup_inode_id = get_cgroup_id(cgroup_path); > + key.attach_type = BPF_CGROUP_INET_EGRESS; > + if (CHECK_FAIL(bpf_map_lookup_elem(map_fd, &key, &value) < 0)) please don't use CHECK_FAIL, use CHECK instead > + return true; > + if (CHECK_FAIL(value != expected)) > + return true; > + > + return false; > +} > + [...] > + > +static void test_egress_only(int parent_cgroup_fd, int child_cgroup_fd) > +{ > + struct cg_storage_multi_egress_only *obj; > + int err; > + > + if (!test__start_subtest("egress_only")) > + return; subtest check should be done in test_cg_storage_multi, otherwise it's not even clear that this test has subtests > + > + obj = cg_storage_multi_egress_only__open_and_load(); > + if (CHECK_FAIL(!obj)) > + return; > + > + /* Attach to parent cgroup, trigger packet from child. > + * Assert that there is only one run and in that run the storage is > + * parent cgroup's storage. > + * Also assert that child cgroup's storage does not exist > + */ > + err = bpf_prog_attach(bpf_program__fd(obj->progs.egress), > + parent_cgroup_fd, > + BPF_CGROUP_INET_EGRESS, BPF_F_ALLOW_MULTI); please use bpf_program__attach_cgroup() instead > + if (CHECK_FAIL(err)) > + goto close_bpf_object; > + err = connect_send(CHILD_CGROUP); > + if (CHECK_FAIL(err)) > + goto close_bpf_object; > + if (CHECK_FAIL(obj->bss->invocations != 1)) > + goto close_bpf_object; > + if (CHECK_FAIL(assert_storage(obj->maps.cgroup_storage, > + PARENT_CGROUP, 1))) > + goto close_bpf_object; > + if (CHECK_FAIL(assert_storage_noexist(obj->maps.cgroup_storage, > + CHILD_CGROUP))) > + goto close_bpf_object; > + > + /* Attach to parent and child cgroup, trigger packet from child. > + * Assert that there are two additional runs, one that run with parent > + * cgroup's storage and one with child cgroup's storage. > + */ > + err = bpf_prog_attach(bpf_program__fd(obj->progs.egress), > + child_cgroup_fd, > + BPF_CGROUP_INET_EGRESS, BPF_F_ALLOW_MULTI); Here as well. [...]