Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp2083198imm; Fri, 6 Jul 2018 11:32:01 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdp25UTpeqAx7NDHj+AzucE1+QPBX4LTfXDVHVgNoiH6XYxBixeexhm6Ja5g5HKmyPWfHHQ X-Received: by 2002:a63:6ecb:: with SMTP id j194-v6mr4487197pgc.158.1530901921847; Fri, 06 Jul 2018 11:32:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530901921; cv=none; d=google.com; s=arc-20160816; b=KLm/tBdkxYcsUGdNTS0+4wC6dJkT6Ic/KkCE2EwOw2Q3KllNZHWpJBwwkt9AwLeyI0 X6c5KHs5DoZWjmxK61QuB9JMQLf0+rL1T7FOnrAJatcNjO+a17ZCVwjusE+YxdDTCSpg xsnqJhNBg7mrnHvmgLbu03+pTLeTNEqeoUEF3XEmhwcqi5fAgR+a2OgkzqKXBdFWMSn7 HYyaGlokG+53+BEdE2kCs7LpB2brGTqE+XvrpQCz+RlL0m5YrilBUUM9Y8WED6vbwe/B qvdX2W0LSV7yQJ4UqRkLUK3AgISsKIT/3bKL22rIZ5m6TWtcmjC5uuIjHowxvYGAnKSB zCbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:message-id:date:subject:cc:to :from:dkim-signature:dkim-signature:arc-authentication-results; bh=vcNVh7vClOknJEGBSPYAjavAen0HXo/WJ1qrZTNsg1M=; b=OtbDh16neFnuaj718AT+sexoxg0mlSrrlQeTVoVjcISvXMHYZEPXgyb/JMFxMHD66W qHzTF6a6m3/IagVGgQuqJSDD8GXwF/UQaxyZrzM9uzqkVAcKbYIa0OewkXikM59dZCCu JPp5TwTLNTnZ3hINk5Bq+NLGGfGjszu1dkn1RzZmWjAK5MZw3PZknxhl4P4BRxbq/dbS 9HfCKAXCLckWnPAHZVs60PG+WBqslsgHdvseoIY1V7Ac9Xud2M/aBi9PK/v9mNDrGrHK 2LDdmWKyTAUiMB7QVpKnhCoPvH2dtKMUijDs3yLDM/fqz2buvks6TwQnIMCAFg0dNrLo iHMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=Pr27+LDS; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=MpoefdUH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x69-v6si8601007pgd.635.2018.07.06.11.31.47; Fri, 06 Jul 2018 11:32:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=Pr27+LDS; dkim=pass header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=MpoefdUH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934428AbeGFSbC (ORCPT + 99 others); Fri, 6 Jul 2018 14:31:02 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:53758 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S934298AbeGFSa6 (ORCPT ); Fri, 6 Jul 2018 14:30:58 -0400 Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.0.22/8.16.0.22) with SMTP id w66ITCqh018568; Fri, 6 Jul 2018 11:30:35 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=vcNVh7vClOknJEGBSPYAjavAen0HXo/WJ1qrZTNsg1M=; b=Pr27+LDSSUb9FTsM16j2MuH9e8GtdTCq7ZHmOSZ0q/YZruKbugdRn9U3xXDIfvRAqfYU rm5P3jRqcRQn/QmHsM/33Yq0Xu1Gk++6m5ahfxYvckCsm8ShfTaMg4VSQIf7sAYv4Lmr qbxJT7sJvY+ifIlgWDllTqsxcoKlCL6elb4= Received: from mail.thefacebook.com ([199.201.64.23]) by m0089730.ppops.net with ESMTP id 2k2bq4gemq-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 06 Jul 2018 11:30:35 -0700 Received: from NAM03-BY2-obe.outbound.protection.outlook.com (192.168.54.28) by o365-in.thefacebook.com (192.168.16.23) with Microsoft SMTP Server (TLS) id 14.3.361.1; Fri, 6 Jul 2018 11:30:33 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector1-fb-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vcNVh7vClOknJEGBSPYAjavAen0HXo/WJ1qrZTNsg1M=; b=MpoefdUHPKOW6SRrJIO80oJ1Hc/VWjaCbJ2PtOIcVYG2SRwsmBpCrwkw0rkS76o1gVPle+6hxnR6KAj0mxtkZyMeTB3dF2ByxjfA7kA/rwTeqMVyMVHwn5xGDRb3IW0mZaLSa6nOXORP/5MmTmoCFd1DqSfROL2dY1L4EGs3AZ8= Received: from castle.thefacebook.com (2620:10d:c090:200::6:17d7) by SN1PR15MB0176.namprd15.prod.outlook.com (2a01:111:e400:58cf::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.930.19; Fri, 6 Jul 2018 18:30:28 +0000 From: Roman Gushchin To: CC: , , Jakub Kicinski , Roman Gushchin , Quentin Monnet , Daniel Borkmann , Alexei Starovoitov Subject: [PATCH v2 bpf-next 1/3] bpftool: introduce cgroup tree command Date: Fri, 6 Jul 2018 11:30:10 -0700 Message-ID: <20180706183012.6475-1-guro@fb.com> X-Mailer: git-send-email 2.14.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c090:200::6:17d7] X-ClientProxiedBy: MWHPR21CA0067.namprd21.prod.outlook.com (2603:10b6:300:db::29) To SN1PR15MB0176.namprd15.prod.outlook.com (2a01:111:e400:58cf::15) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e13dd7a2-f844-45e3-e497-08d5e36e8d77 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652040)(8989117)(5600053)(711020)(4534165)(4627221)(201703031133081)(201702281549075)(8990107)(2017052603328)(7153060)(7193020);SRVR:SN1PR15MB0176; X-Microsoft-Exchange-Diagnostics: 1;SN1PR15MB0176;3:K1CgVAffB8442dRfGeW8UQxzRROxwT1LGmH2Jzdwpk+lAgwo0JAGrE/lhk+smHu9Xcd//jnuVXBTjS1rAVjVbfAUrmcbQ8ULUMUegX4Z4igxakmg0BiZPZVpg0Vjz6zGWnFiBrsepi0QUX0BAbrm3XD6dp1PqryKw6uaajWI9rUQBOcGGyGrP3cwAv1DMhuzYmQ6ki7s11ccpURXEbHKzg/z1ttBK+Sz5mK/+JI1wGYDlNVzXCFfj/orRaiyPGNl;25:gsMnH9oeB6ZFFOc6WbeQ4/Pl14kOkYatxUfBypu3cTcPBo8xwrGeKd1BNymkNXwaAnY4QH/grAQfHnqvllgSvAwAl3CF8UxAwPeeAxOrM0Aq2zzAt94k/BY7uooTvie4DukLRK6xivtGlrLrwlkPXl3ihXAOws3uvnNHM3+xrndXodiw9kwtBh9ytCiP5/cidfpPpsLDH1zUrBtpb6RTNWQAoW0ixlIIFvkSFzaUAN65QiId3rK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FjRLwMUbN3Wk2i7SB7Xa/2aL028kJrIKm3Z7benDZZwkoQ2pspplKIL+3A+e0lsYb/3DZxg1HoxnO+edyPTHfgoB15Jl6kfPz7HHRD9aaAExkyaeYQOQjH/+nXcFciic/pa/Jm6cBhAceQKigDMYUqjHBwPIn9Dbf/dUwegYoO/Rv/7bRbLGGMC7CMq7dvJ7R9Vxpqr4j4ZJJEq2XyQhmzh1IZOWw= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(67672495146484)(81227570615382); X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(93006095)(93001095)(3002001)(10201501046)(3231254)(11241501184)(944501410)(52105095)(149027)(150027)(6041310)(20161123562045)(20161123564045)(20161123560045)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011)(7699016);SRVR:SN1PR15MB0176;BCL:0;PCL:0;RULEID:;SRVR:SN1PR15MB0176; X-Forefront-PRVS: 0725D9E8D0 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(136003)(396003)(39860400002)(346002)(366004)(376002)(189003)(199004)(476003)(53416004)(316002)(186003)(2361001)(2616005)(46003)(386003)(6506007)(8936002)(5660300001)(16526019)(68736007)(1076002)(7736002)(81156014)(48376002)(69596002)(97736004)(8676002)(478600001)(50466002)(51416003)(486006)(52396003)(52116002)(81166006)(6916009)(2906002)(6116002)(106356001)(105586002)(6486002)(50226002)(47776003)(86362001)(6666003)(2351001)(36756003)(54906003)(6512007)(16586007)(25786009)(4326008)(53936002)(5024004)(39060400002)(305945005)(14444005)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:SN1PR15MB0176;H:castle.thefacebook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; Received-SPF: None (protection.outlook.com: fb.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics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EC1FN84CctPkRBJavu4vslL74/?= =?us-ascii?Q?3lBKKnOCY7mbXB1ZDOyQoFGyfA6LVrVv20LUdlqcwN4+miIpsHKuOqxEn0ZR?= =?us-ascii?Q?ViwcaHSMkU4Euqkcg/nyK+T1FRF0rE4Yle85Hyshv0n8f/xa1D3unTBKCdls?= =?us-ascii?Q?cH5X7NFRfUrDt4miXdIHMZ34pgCFYuWJUqaTe2mXTnnnRBqd2saf4eGLmNru?= =?us-ascii?Q?fa7KKSMu6noCCxFRZurZC4rdYj1ozh3ljSHvWZnNdA8yghC2LQc9aAzu5moc?= =?us-ascii?Q?mzDZFJdOxMiOMo4K95eXxKlnc7TsUXcr+Yj7GnMtvATV2gtor+/ekqCoZVrN?= =?us-ascii?Q?zmJoQCSp//JlvPu+m2Ok1Yon9CH90ZWyXVnJNJ/FnERmNXCp6R2gM29tMfM9?= =?us-ascii?Q?D//QCRRdVNnkW6H2V+vZXIjox85IvGxHmpFZVX6vKjvdvrTWL1rIIMkzKen9?= =?us-ascii?B?Zz09?= X-Microsoft-Antispam-Message-Info: pId3My1ouyd0dAATlKrsSy4w3gm8rCkB9ieXHjIp/MJ4hJWa5XTJBISr07THEt3L4NBG9IVR+DLz4JWOatNyElhOvkDeaOV9Dwp/gy84U2qljo9mbGoWEJB1RS0xFAaIt0uAeshw7mG9dKH5yfP+HE6t+9+xFUmXK1Bu7q/DTpSglpyfHu/uaCRKGXw7iPROnazdHPen25jFjDbr3mbriXN+Jg942/fYly6taaZUmTQSCyW2P91H4CH4XHxRI0xxJ4oXU+ziC1hsiBa95KohyedwBEn0pGnk0v2Av+I/1H/EXF8rLk79Zzb08nGXJTYC/a8IIlNsAMfDArPPrAih/KRoLUhS+yDsySpfKhdt+aI= X-Microsoft-Exchange-Diagnostics: 1;SN1PR15MB0176;6:60kej1KJ/nn/vtpTxhAnFPvee1YTDVDv7MgRyCvPTaBSvYKDSFh4WPPLPX/UM5PEkH6mc8R2dOllFYLpbYIkD+Qa5L8k9AfIIl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NG0vwSsp1HcP5Payaw2QYUTCcvqEEs2QhDSPfl7b5Zd3ETWDDNotuU470YMaR3M9uzFh/OQz0rmyP/kkYF6JhdRQ= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jul 2018 18:30:28.7912 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e13dd7a2-f844-45e3-e497-08d5e36e8d77 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR15MB0176 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-07-06_05:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This commit introduces a new bpftool command: cgroup tree. The idea is to iterate over the whole cgroup tree and print all attached programs. I was debugging a bpf/systemd issue, and found, that there is no simple way to listen all bpf programs attached to cgroups. I did master something in bash, but after some time got tired of it, and decided, that adding a dedicated bpftool command could be a better idea. So, here it is: $ sudo ./bpftool cgroup tree CgroupPath ID AttachType AttachFlags Name /sys/fs/cgroup/system.slice/systemd-machined.service 18 ingress 17 egress /sys/fs/cgroup/system.slice/systemd-logind.service 20 ingress 19 egress /sys/fs/cgroup/system.slice/systemd-udevd.service 16 ingress 15 egress /sys/fs/cgroup/system.slice/systemd-journald.service 14 ingress 13 egress Signed-off-by: Roman Gushchin Cc: Jakub Kicinski Cc: Quentin Monnet Cc: Daniel Borkmann Cc: Alexei Starovoitov --- tools/bpf/bpftool/cgroup.c | 170 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 165 insertions(+), 5 deletions(-) diff --git a/tools/bpf/bpftool/cgroup.c b/tools/bpf/bpftool/cgroup.c index 16bee011e16c..ee7a9765c6b3 100644 --- a/tools/bpf/bpftool/cgroup.c +++ b/tools/bpf/bpftool/cgroup.c @@ -2,7 +2,12 @@ // Copyright (C) 2017 Facebook // Author: Roman Gushchin +#define _XOPEN_SOURCE 500 +#include #include +#include +#include +#include #include #include #include @@ -53,7 +58,8 @@ static enum bpf_attach_type parse_attach_type(const char *str) } static int show_bpf_prog(int id, const char *attach_type_str, - const char *attach_flags_str) + const char *attach_flags_str, + int level) { struct bpf_prog_info info = {}; __u32 info_len = sizeof(info); @@ -78,7 +84,8 @@ static int show_bpf_prog(int id, const char *attach_type_str, jsonw_string_field(json_wtr, "name", info.name); jsonw_end_object(json_wtr); } else { - printf("%-8u %-15s %-15s %-15s\n", info.id, + printf("%s%-8u %-15s %-15s %-15s\n", level ? " " : "", + info.id, attach_type_str, attach_flags_str, info.name); @@ -88,7 +95,20 @@ static int show_bpf_prog(int id, const char *attach_type_str, return 0; } -static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type) +static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type) +{ + __u32 prog_cnt = 0; + int ret; + + ret = bpf_prog_query(cgroup_fd, type, 0, NULL, NULL, &prog_cnt); + if (ret) + return -1; + + return prog_cnt; +} + +static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type, + int level) { __u32 prog_ids[1024] = {0}; char *attach_flags_str; @@ -123,7 +143,7 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type) for (iter = 0; iter < prog_cnt; iter++) show_bpf_prog(prog_ids[iter], attach_type_strings[type], - attach_flags_str); + attach_flags_str, level); return 0; } @@ -161,7 +181,7 @@ static int do_show(int argc, char **argv) * If we were able to get the show for at least one * attach type, let's return 0. */ - if (show_attached_bpf_progs(cgroup_fd, type) == 0) + if (show_attached_bpf_progs(cgroup_fd, type, 0) == 0) ret = 0; } @@ -173,6 +193,143 @@ static int do_show(int argc, char **argv) return ret; } +/* + * To distinguish nftw() errors and do_show_tree_fn() errors + * and avoid duplicating error messages, let's return -2 + * from do_show_tree_fn() in case of error. + */ +#define NFTW_ERR -1 +#define SHOW_TREE_FN_ERR -2 +static int do_show_tree_fn(const char *fpath, const struct stat *sb, + int typeflag, struct FTW *ftw) +{ + enum bpf_attach_type type; + bool skip = true; + int cgroup_fd; + + if (typeflag != FTW_D) + return 0; + + cgroup_fd = open(fpath, O_RDONLY); + if (cgroup_fd < 0) { + p_err("can't open cgroup %s: %s", fpath, strerror(errno)); + return SHOW_TREE_FN_ERR; + } + + for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) { + int count = count_attached_bpf_progs(cgroup_fd, type); + + if (count < 0 && errno != EINVAL) { + p_err("can't query bpf programs attached to %s: %s", + fpath, strerror(errno)); + close(cgroup_fd); + return SHOW_TREE_FN_ERR; + } + if (count > 0) { + skip = false; + break; + } + } + + if (skip) { + close(cgroup_fd); + return 0; + } + + if (json_output) { + jsonw_start_object(json_wtr); + jsonw_string_field(json_wtr, "cgroup", fpath); + jsonw_name(json_wtr, "programs"); + jsonw_start_array(json_wtr); + } else { + printf("%s\n", fpath); + } + + for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) + show_attached_bpf_progs(cgroup_fd, type, ftw->level); + + if (json_output) { + jsonw_end_array(json_wtr); + jsonw_end_object(json_wtr); + } + + close(cgroup_fd); + + return 0; +} + +static char *find_cgroup_root(void) +{ + struct mntent *mnt; + FILE *f; + + f = fopen("/proc/mounts", "r"); + if (f == NULL) + return NULL; + + while ((mnt = getmntent(f))) { + if (strcmp(mnt->mnt_type, "cgroup2") == 0) { + fclose(f); + return strdup(mnt->mnt_dir); + } + } + + fclose(f); + return NULL; +} + +static int do_show_tree(int argc, char **argv) +{ + char *cgroup_root; + int ret; + + switch (argc) { + case 0: + cgroup_root = find_cgroup_root(); + if (!cgroup_root) { + p_err("cgroup v2 isn't mounted"); + return -1; + } + break; + case 1: + cgroup_root = argv[0]; + break; + default: + p_err("too many parameters for cgroup tree"); + return -1; + } + + + if (json_output) + jsonw_start_array(json_wtr); + else + printf("%s\n" + "%-8s %-15s %-15s %-15s\n", + "CgroupPath", + "ID", "AttachType", "AttachFlags", "Name"); + + switch (nftw(cgroup_root, do_show_tree_fn, 1024, FTW_MOUNT)) { + case NFTW_ERR: + p_err("can't iterate over %s: %s", cgroup_root, + strerror(errno)); + ret = -1; + break; + case SHOW_TREE_FN_ERR: + ret = -1; + break; + default: + ret = 0; + } + + if (json_output) + jsonw_end_array(json_wtr); + + if (argc == 0) + free(cgroup_root); + + return ret; +} + static int do_attach(int argc, char **argv) { enum bpf_attach_type attach_type; @@ -289,6 +446,7 @@ static int do_help(int argc, char **argv) fprintf(stderr, "Usage: %s %s { show | list } CGROUP\n" + " %s %s tree [CGROUP_ROOT]\n" " %s %s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n" " %s %s detach CGROUP ATTACH_TYPE PROG\n" " %s %s help\n" @@ -298,6 +456,7 @@ static int do_help(int argc, char **argv) " " HELP_SPEC_PROGRAM "\n" " " HELP_SPEC_OPTIONS "\n" "", + bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]); @@ -307,6 +466,7 @@ static int do_help(int argc, char **argv) static const struct cmd cmds[] = { { "show", do_show }, { "list", do_show }, + { "tree", do_show_tree }, { "attach", do_attach }, { "detach", do_detach }, { "help", do_help }, -- 2.14.4