Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2119427imn; Mon, 1 Aug 2022 11:49:55 -0700 (PDT) X-Google-Smtp-Source: AGRyM1swbYknnriBd1t/5qMSTjdTB/7dMMRlv7mSRa+6GWKe/X+jloRHY88LXxE/+mwPwVU46tfY X-Received: by 2002:a65:5cc4:0:b0:41a:ae9f:5642 with SMTP id b4-20020a655cc4000000b0041aae9f5642mr14239365pgt.542.1659379795200; Mon, 01 Aug 2022 11:49:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659379795; cv=none; d=google.com; s=arc-20160816; b=0VkE+3jvlhzmLLgkxnaWurP4UGEGCOVBBNJN4IChpjVA8SsOoLe6e14p69eYJY2iTU pluWaP4RL323Z/Cd+ZWQX0eBVjPQi9WfaPZikgxZfj8GQOzLhKJy1PNs9UETGRf2enDF +1GjqxomXLkAxXNeIuFa884NhGeBIS77OTEsmzY1bMiKk2FgzSl6C3CvcPbzfnZofJv4 7DX2cE4BQI7yM5/sYmhpcRJoG8NIfpxhr1jdvt7P77e83rd8Kr2sUWxicPnfMoCGJVAR BNmfuvAjpdsHHeo1lJA/BpdHHXxw8YFnAkxqii7nvivG9wtSG/qgAKGoVTK+OcnM2hv3 2Mig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=rKRUKZDZz2O69RKll2RdfMllg8pu6Nivb2REULm2Y90=; b=H9nRL0BjrDTa30XIA0xPaRv8w8v+Nc3StJ5DcJNsmQGhbCC4IubT+a53m84P6AuFZa xsHCj9oM0u/pfN4wzGsMOfuqSRlqoqCl5ncd29vUoQ6dOc1yb4ooV2p79BSxDxrJXQmH W70qcSYOKGi65xvVPGztwfKDYxmh+H9T1RnA1Ln+jwfdeHQmRHh6csEjQ4M7LOIb7DYB 5VKUSXAPnyFP0EIMtdPuOHK349QcPv0d9uwiD0KOhFL88zuaArqIeC59p2kh1Q6XAF/8 GSB4C95s4gILE/NtcfMyd3gSEe4SBTfHCiyPXHU89mIbZxZ/lYLVTBAkxV2iSOOrlvgY 0Iow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=cV6bi04C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p14-20020a635b0e000000b0041a4a97d588si13063248pgb.588.2022.08.01.11.49.40; Mon, 01 Aug 2022 11:49:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=cV6bi04C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233935AbiHARzQ (ORCPT + 99 others); Mon, 1 Aug 2022 13:55:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234168AbiHARy6 (ORCPT ); Mon, 1 Aug 2022 13:54:58 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A2723A491 for ; Mon, 1 Aug 2022 10:54:47 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31cdce3ed04so98223017b3.13 for ; Mon, 01 Aug 2022 10:54:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=rKRUKZDZz2O69RKll2RdfMllg8pu6Nivb2REULm2Y90=; b=cV6bi04Cg/qmM5fTQ8Fbet1xlz67vIVUgKe7rhfQYe3qwZDGivlJ1PHVE7/nxfaD8O 7osgCPHSYG/6Bms62kjZYs4kZuEJOktcIfhWtXgpXB8HFfvZifxqB8xFklGd4sVrcqMd uHI9SMqzJ4UIz1IRt3gzdFQ9c3au22LhGNUatB74ENDA4lFz3aCHz/xz139w2wxnOFJ3 MxbMunmed0XYUtcVSpL07QVzzwifs4Bjy4kG5KpULT+b4rsfLB3lkbptcwrlQSDmfviO Sew0mXlFBI1d7xzqO0ufjndS34Cuhqm6u2Fl+atbliUZxK91XvW8pBgskyDuIfLnGzYP dicg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=rKRUKZDZz2O69RKll2RdfMllg8pu6Nivb2REULm2Y90=; b=s/zvZjQTvQdjB7TGP+jHRa9KVX/PQH42h7M81CHcqR9bnLW8nFsBR2xFsbUan9lcqa p1j/4exzkse42NaqHSHRB1eAnPuHo/zCpRlmM4F1z4WYfqpyeXJyQpSZeeFyiG3zRiTv 0llR8+Gg8KS+6bM86DOOtz+gPQDdWwXdk4pJXD7CK5ry32Hpzyvcgb9mW2lfaeXow9tt Airuxl0qWGvF5tXn1u8iLbPzVl713P33YgfNxvjhOoBstmXThb5j0CjuPOTWRVQZilwt 51FZJP7UzQMCl2E40g/sDN//zxl+K5KoUz7s1rzonLAwtCEQ07SmelpdaMPQoXQ5KFbK Nwrg== X-Gm-Message-State: ACgBeo2rtfzPcUgif9o+Gkt159u/5seUjRD1hWIjRDIQhFaaLd37SKOA XrJoCr6CyglPosKL5HniazaIp+oa+UFO+AmaVpwrSZnf7SijOi57sRgWL8llDPUL2w8ILveEh3w Hw8iw1WVOINiAGUGu/+72Z4a8hoQHGaDYnIhAdvO8vmaG7Pu8O7S22GQp/27+vPlPG1dwjQ== X-Received: from haoluo.svl.corp.google.com ([2620:15c:2d4:203:7c9:7b32:e73f:6716]) (user=haoluo job=sendgmr) by 2002:a25:3503:0:b0:672:adb4:a69f with SMTP id c3-20020a253503000000b00672adb4a69fmr11324876yba.41.1659376486720; Mon, 01 Aug 2022 10:54:46 -0700 (PDT) Date: Mon, 1 Aug 2022 10:54:04 -0700 In-Reply-To: <20220801175407.2647869-1-haoluo@google.com> Message-Id: <20220801175407.2647869-6-haoluo@google.com> Mime-Version: 1.0 References: <20220801175407.2647869-1-haoluo@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH bpf-next v6 5/8] selftests/bpf: Test cgroup_iter. From: Hao Luo To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, cgroups@vger.kernel.org, netdev@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Tejun Heo , Zefan Li , KP Singh , Johannes Weiner , Michal Hocko , Benjamin Tissoires , John Fastabend , Michal Koutny , Roman Gushchin , David Rientjes , Stanislav Fomichev , Shakeel Butt , Yosry Ahmed , Hao Luo Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a selftest for cgroup_iter. The selftest creates a mini cgroup tree of the following structure: ROOT (working cgroup) | PARENT / \ CHILD1 CHILD2 and tests the following scenarios: - invalid cgroup fd. - pre-order walk over descendants from PARENT. - post-order walk over descendants from PARENT. - walk of ancestors from PARENT. - early termination. Acked-by: Yonghong Song Signed-off-by: Hao Luo --- .../selftests/bpf/prog_tests/cgroup_iter.c | 193 ++++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 7 + .../testing/selftests/bpf/progs/cgroup_iter.c | 39 ++++ 3 files changed, 239 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter.c diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c b/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c new file mode 100644 index 000000000000..5dc843a3f507 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c @@ -0,0 +1,193 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ + +#include +#include +#include +#include "cgroup_iter.skel.h" +#include "cgroup_helpers.h" + +#define ROOT 0 +#define PARENT 1 +#define CHILD1 2 +#define CHILD2 3 +#define NUM_CGROUPS 4 + +#define PROLOGUE "prologue\n" +#define EPILOGUE "epilogue\n" + +#define format_expected_output1(cg_id1) \ + snprintf(expected_output, sizeof(expected_output), \ + PROLOGUE "%8llu\n" EPILOGUE, (cg_id1)) + +#define format_expected_output2(cg_id1, cg_id2) \ + snprintf(expected_output, sizeof(expected_output), \ + PROLOGUE "%8llu\n%8llu\n" EPILOGUE, \ + (cg_id1), (cg_id2)) + +#define format_expected_output3(cg_id1, cg_id2, cg_id3) \ + snprintf(expected_output, sizeof(expected_output), \ + PROLOGUE "%8llu\n%8llu\n%8llu\n" EPILOGUE, \ + (cg_id1), (cg_id2), (cg_id3)) + +const char *cg_path[] = { + "/", "/parent", "/parent/child1", "/parent/child2" +}; + +static int cg_fd[] = {-1, -1, -1, -1}; +static unsigned long long cg_id[] = {0, 0, 0, 0}; +static char expected_output[64]; + +int setup_cgroups(void) +{ + int fd, i = 0; + + for (i = 0; i < NUM_CGROUPS; i++) { + fd = create_and_get_cgroup(cg_path[i]); + if (fd < 0) + return fd; + + cg_fd[i] = fd; + cg_id[i] = get_cgroup_id(cg_path[i]); + } + return 0; +} + +void cleanup_cgroups(void) +{ + int i; + + for (i = 0; i < NUM_CGROUPS; i++) + close(cg_fd[i]); +} + +static void read_from_cgroup_iter(struct bpf_program *prog, int cgroup_fd, + int order, const char *testname) +{ + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); + union bpf_iter_link_info linfo; + struct bpf_link *link; + int len, iter_fd; + static char buf[64]; + + memset(&linfo, 0, sizeof(linfo)); + linfo.cgroup.cgroup_fd = cgroup_fd; + linfo.cgroup.traversal_order = order; + opts.link_info = &linfo; + opts.link_info_len = sizeof(linfo); + + link = bpf_program__attach_iter(prog, &opts); + if (!ASSERT_OK_PTR(link, "attach_iter")) + return; + + iter_fd = bpf_iter_create(bpf_link__fd(link)); + if (iter_fd < 0) + goto free_link; + + memset(buf, 0, sizeof(buf)); + while ((len = read(iter_fd, buf, sizeof(buf))) > 0) + ; + + ASSERT_STREQ(buf, expected_output, testname); + + /* read() after iter finishes should be ok. */ + if (len == 0) + ASSERT_OK(read(iter_fd, buf, sizeof(buf)), "second_read"); + + close(iter_fd); +free_link: + bpf_link__destroy(link); +} + +/* Invalid cgroup. */ +static void test_invalid_cgroup(struct cgroup_iter *skel) +{ + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); + union bpf_iter_link_info linfo; + struct bpf_link *link; + + memset(&linfo, 0, sizeof(linfo)); + linfo.cgroup.cgroup_fd = (__u32)-1; + opts.link_info = &linfo; + opts.link_info_len = sizeof(linfo); + + link = bpf_program__attach_iter(skel->progs.cgroup_id_printer, &opts); + if (!ASSERT_ERR_PTR(link, "attach_iter")) + bpf_link__destroy(link); +} + +/* Preorder walk prints parent and child in order. */ +static void test_walk_preorder(struct cgroup_iter *skel) +{ + format_expected_output3(cg_id[PARENT], cg_id[CHILD1], cg_id[CHILD2]); + + read_from_cgroup_iter(skel->progs.cgroup_id_printer, cg_fd[PARENT], + BPF_ITER_CGROUP_PRE, "preorder"); +} + +/* Postorder walk prints child and parent in order. */ +static void test_walk_postorder(struct cgroup_iter *skel) +{ + format_expected_output3(cg_id[CHILD1], cg_id[CHILD2], cg_id[PARENT]); + + read_from_cgroup_iter(skel->progs.cgroup_id_printer, cg_fd[PARENT], + BPF_ITER_CGROUP_POST, "postorder"); +} + +/* Walking parents prints parent and then root. */ +static void test_walk_parent_up(struct cgroup_iter *skel) +{ + /* terminate the walk when ROOT is met. */ + skel->bss->terminal_cgroup = cg_id[ROOT]; + + format_expected_output2(cg_id[PARENT], cg_id[ROOT]); + + read_from_cgroup_iter(skel->progs.cgroup_id_printer, cg_fd[PARENT], + BPF_ITER_CGROUP_PARENT_UP, "parent_up"); + + skel->bss->terminal_cgroup = 0; +} + +/* Early termination prints parent only. */ +static void test_early_termination(struct cgroup_iter *skel) +{ + /* terminate the walk after the first element is processed. */ + skel->bss->terminate_early = 1; + + format_expected_output1(cg_id[PARENT]); + + read_from_cgroup_iter(skel->progs.cgroup_id_printer, cg_fd[PARENT], + BPF_ITER_CGROUP_PRE, "early_termination"); + + skel->bss->terminate_early = 0; +} + +void test_cgroup_iter(void) +{ + struct cgroup_iter *skel = NULL; + + if (setup_cgroup_environment()) + return; + + if (setup_cgroups()) + goto out; + + skel = cgroup_iter__open_and_load(); + if (!ASSERT_OK_PTR(skel, "cgroup_iter__open_and_load")) + goto out; + + if (test__start_subtest("cgroup_iter__invalid_cgroup")) + test_invalid_cgroup(skel); + if (test__start_subtest("cgroup_iter__preorder")) + test_walk_preorder(skel); + if (test__start_subtest("cgroup_iter__postorder")) + test_walk_postorder(skel); + if (test__start_subtest("cgroup_iter__parent_up_walk")) + test_walk_parent_up(skel); + if (test__start_subtest("cgroup_iter__early_termination")) + test_early_termination(skel); +out: + cgroup_iter__destroy(skel); + cleanup_cgroups(); + cleanup_cgroup_environment(); +} diff --git a/tools/testing/selftests/bpf/progs/bpf_iter.h b/tools/testing/selftests/bpf/progs/bpf_iter.h index e9846606690d..c41ee80533ca 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter.h +++ b/tools/testing/selftests/bpf/progs/bpf_iter.h @@ -17,6 +17,7 @@ #define bpf_iter__bpf_sk_storage_map bpf_iter__bpf_sk_storage_map___not_used #define bpf_iter__sockmap bpf_iter__sockmap___not_used #define bpf_iter__bpf_link bpf_iter__bpf_link___not_used +#define bpf_iter__cgroup bpf_iter__cgroup___not_used #define btf_ptr btf_ptr___not_used #define BTF_F_COMPACT BTF_F_COMPACT___not_used #define BTF_F_NONAME BTF_F_NONAME___not_used @@ -40,6 +41,7 @@ #undef bpf_iter__bpf_sk_storage_map #undef bpf_iter__sockmap #undef bpf_iter__bpf_link +#undef bpf_iter__cgroup #undef btf_ptr #undef BTF_F_COMPACT #undef BTF_F_NONAME @@ -141,6 +143,11 @@ struct bpf_iter__bpf_link { struct bpf_link *link; }; +struct bpf_iter__cgroup { + struct bpf_iter_meta *meta; + struct cgroup *cgroup; +} __attribute__((preserve_access_index)); + struct btf_ptr { void *ptr; __u32 type_id; diff --git a/tools/testing/selftests/bpf/progs/cgroup_iter.c b/tools/testing/selftests/bpf/progs/cgroup_iter.c new file mode 100644 index 000000000000..2a34d146d6df --- /dev/null +++ b/tools/testing/selftests/bpf/progs/cgroup_iter.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Google */ + +#include "bpf_iter.h" +#include +#include + +char _license[] SEC("license") = "GPL"; +volatile int terminate_early = 0; +volatile u64 terminal_cgroup = 0; + +static inline u64 cgroup_id(struct cgroup *cgrp) +{ + return cgrp->kn->id; +} + +SEC("iter/cgroup") +int cgroup_id_printer(struct bpf_iter__cgroup *ctx) +{ + struct seq_file *seq = ctx->meta->seq; + struct cgroup *cgrp = ctx->cgroup; + + /* epilogue */ + if (cgrp == NULL) { + BPF_SEQ_PRINTF(seq, "epilogue\n"); + return 0; + } + + /* prologue */ + if (ctx->meta->seq_num == 0) + BPF_SEQ_PRINTF(seq, "prologue\n"); + + BPF_SEQ_PRINTF(seq, "%8llu\n", cgroup_id(cgrp)); + + if (terminal_cgroup == cgroup_id(cgrp)) + return 1; + + return terminate_early ? 1 : 0; +} -- 2.37.1.455.g008518b4e5-goog