From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AB6EC433DF for ; Fri, 21 Aug 2020 18:44:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F29A320735 for ; Fri, 21 Aug 2020 18:44:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="e1/wF8nQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726599AbgHUSoc (ORCPT ); Fri, 21 Aug 2020 14:44:32 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:14818 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726466AbgHUSo0 (ORCPT ); Fri, 21 Aug 2020 14:44:26 -0400 Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 07LIeN2q006622 for ; Fri, 21 Aug 2020 11:44:24 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=8ZtHLbWQmLHyspFQ6lS5xxV6lHpJpOFj5s3PJW+mjSA=; b=e1/wF8nQ1f1c/0UwrpxV6VpIAZ7Y3TX+LoqBYTMeD8WLEaAgBPYsUIDzWeIRLxy4llGA 6IMzXjZWrS+QPL4IJ+hUyuybZfp9kNe2zXLb06etHpFOVFhMuGRahzckKeicoFZE/3PR JDJ88kHVaiVcdopiympaGhkAtgSFk4uDugQ= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 332ehfsw71-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 21 Aug 2020 11:44:24 -0700 Received: from intmgw002.03.ash8.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:21d::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Fri, 21 Aug 2020 11:44:22 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 061543702096; Fri, 21 Aug 2020 11:44:21 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Yonghong Song Smtp-Origin-Hostname: devbig003.ftw2.facebook.com To: , CC: Alexei Starovoitov , Daniel Borkmann , , Andrii Nakryiko Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next v4 3/3] bpftool: implement link_query for bpf iterators Date: Fri, 21 Aug 2020 11:44:20 -0700 Message-ID: <20200821184420.574430-1-yhs@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200821184418.574065-1-yhs@fb.com> References: <20200821184418.574065-1-yhs@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-08-21_08:2020-08-21,2020-08-21 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 lowpriorityscore=0 priorityscore=1501 spamscore=0 mlxscore=0 suspectscore=8 mlxlogscore=999 phishscore=0 malwarescore=0 impostorscore=0 clxscore=1015 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2008210176 X-FB-Internal: deliver Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The link query for bpf iterators is implemented. Besides being shown to the user what bpf iterator the link represents, the target_name is also used to filter out what additional information should be printed out, e.g., whether map_id should be shown or not. The following is an example of bpf_iter link dump, plain output or pretty output. $ bpftool link show 11: iter prog 59 target_name task pids test_progs(1749) 34: iter prog 173 target_name bpf_map_elem map_id 127 pids test_progs_1(1753) $ bpftool -p link show [{ "id": 11, "type": "iter", "prog_id": 59, "target_name": "task", "pids": [{ "pid": 1749, "comm": "test_progs" } ] },{ "id": 34, "type": "iter", "prog_id": 173, "target_name": "bpf_map_elem", "map_id": 127, "pids": [{ "pid": 1753, "comm": "test_progs_1" } ] } ] Acked-by: Andrii Nakryiko Signed-off-by: Yonghong Song --- tools/bpf/bpftool/link.c | 44 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index a89f09e3c848..e77e1525d20a 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -77,6 +77,22 @@ static void show_link_attach_type_json(__u32 attach_ty= pe, json_writer_t *wtr) jsonw_uint_field(wtr, "attach_type", attach_type); } =20 +static bool is_iter_map_target(const char *target_name) +{ + return strcmp(target_name, "bpf_map_elem") =3D=3D 0 || + strcmp(target_name, "bpf_sk_storage_map") =3D=3D 0; +} + +static void show_iter_json(struct bpf_link_info *info, json_writer_t *wt= r) +{ + const char *target_name =3D u64_to_ptr(info->iter.target_name); + + jsonw_string_field(wtr, "target_name", target_name); + + if (is_iter_map_target(target_name)) + jsonw_uint_field(wtr, "map_id", info->iter.map.map_id); +} + static int get_prog_info(int prog_id, struct bpf_prog_info *info) { __u32 len =3D sizeof(*info); @@ -128,6 +144,9 @@ static int show_link_close_json(int fd, struct bpf_li= nk_info *info) info->cgroup.cgroup_id); show_link_attach_type_json(info->cgroup.attach_type, json_wtr); break; + case BPF_LINK_TYPE_ITER: + show_iter_json(info, json_wtr); + break; case BPF_LINK_TYPE_NETNS: jsonw_uint_field(json_wtr, "netns_ino", info->netns.netns_ino); @@ -175,6 +194,16 @@ static void show_link_attach_type_plain(__u32 attach= _type) printf("attach_type %u ", attach_type); } =20 +static void show_iter_plain(struct bpf_link_info *info) +{ + const char *target_name =3D u64_to_ptr(info->iter.target_name); + + printf("target_name %s ", target_name); + + if (is_iter_map_target(target_name)) + printf("map_id %u ", info->iter.map.map_id); +} + static int show_link_close_plain(int fd, struct bpf_link_info *info) { struct bpf_prog_info prog_info; @@ -204,6 +233,9 @@ static int show_link_close_plain(int fd, struct bpf_l= ink_info *info) printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id); show_link_attach_type_plain(info->cgroup.attach_type); break; + case BPF_LINK_TYPE_ITER: + show_iter_plain(info); + break; case BPF_LINK_TYPE_NETNS: printf("\n\tnetns_ino %u ", info->netns.netns_ino); show_link_attach_type_plain(info->netns.attach_type); @@ -231,7 +263,7 @@ static int do_show_link(int fd) { struct bpf_link_info info; __u32 len =3D sizeof(info); - char raw_tp_name[256]; + char buf[256]; int err; =20 memset(&info, 0, sizeof(info)); @@ -245,8 +277,14 @@ static int do_show_link(int fd) } if (info.type =3D=3D BPF_LINK_TYPE_RAW_TRACEPOINT && !info.raw_tracepoint.tp_name) { - info.raw_tracepoint.tp_name =3D (unsigned long)&raw_tp_name; - info.raw_tracepoint.tp_name_len =3D sizeof(raw_tp_name); + info.raw_tracepoint.tp_name =3D (unsigned long)&buf; + info.raw_tracepoint.tp_name_len =3D sizeof(buf); + goto again; + } + if (info.type =3D=3D BPF_LINK_TYPE_ITER && + !info.iter.target_name) { + info.iter.target_name =3D (unsigned long)&buf; + info.iter.target_name_len =3D sizeof(buf); goto again; } =20 --=20 2.24.1