From patchwork Fri Apr 21 10:11:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xueming Feng X-Patchwork-Id: 13219771 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B32D8C77B75 for ; Fri, 21 Apr 2023 10:23:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230134AbjDUKW6 (ORCPT ); Fri, 21 Apr 2023 06:22:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229572AbjDUKW5 (ORCPT ); Fri, 21 Apr 2023 06:22:57 -0400 X-Greylist: delayed 588 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 21 Apr 2023 03:22:15 PDT Received: from pv50p00im-ztdg10011301.me.com (pv50p00im-ztdg10011301.me.com [17.58.6.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0836C16B for ; Fri, 21 Apr 2023 03:22:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kuroa.me; s=sig1; t=1682071923; bh=9LouxiBlmrmKuY4D4L114DxgCvo/TJdTAEmNocqna70=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=I7oHR8CUHrXpp00K5C88NdBdNLJvY/3rRPV2L72jQGXvRDpfRnOiaS45iy3QGPHzH xQXs68VeBtcIgg6C57mS/JbpMmOTIW924gBAwRruZZckAzGMxYNxC5YXU5uHwGhidj H4z+5bMizkzElg4vMfNRRVFMsSvpuK5hUNEf48DQkj7Waq5KLl5Mv6z/9iQ6/QL+To 6aeWIum9Z8yHnr6rblJcVt/8Znt64xPK0uN32J1YAfSRqHo+CppuNUROvFD+VSAca8 rTTsS/OvrnroQklZPb+xp98mM6utnWRlJmjDC9Np1FLBYNh6iDtBmmag3wSy5Wlqzg iUikfYHNQRzQA== Received: from localhost.localdomain (pv50p00im-dlb-asmtp-mailmevip.me.com [17.56.9.10]) by pv50p00im-ztdg10011301.me.com (Postfix) with ESMTPSA id 71B741801D9; Fri, 21 Apr 2023 10:11:59 +0000 (UTC) From: Xueming Feng To: Quentin Monnet Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Xueming Feng Subject: [PATCH] Dump map id instead of value for map_of_maps types Date: Fri, 21 Apr 2023 18:11:54 +0800 Message-Id: <20230421101154.23690-1-kuro@kuroa.me> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) MIME-Version: 1.0 X-Proofpoint-GUID: wVywTRvN0KikguucsLYVn11XDAQRkNGT X-Proofpoint-ORIG-GUID: wVywTRvN0KikguucsLYVn11XDAQRkNGT X-Proofpoint-Virus-Version: =?utf-8?q?vendor=3Dfsecure_engine=3D1=2E1=2E170-?= =?utf-8?q?22c6f66c430a71ce266a39bfe25bc2903e8d5c8f=3A6=2E0=2E517=2C18=2E0?= =?utf-8?q?=2E572=2C17=2E11=2E64=2E514=2E0000000_definitions=3D2022-06-21=5F?= =?utf-8?q?01=3A2022-06-21=5F01=2C2020-02-14=5F11=2C2022-02-23=5F01_signatur?= =?utf-8?q?es=3D0?= X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxlogscore=862 adultscore=0 bulkscore=0 spamscore=0 phishscore=0 malwarescore=0 mlxscore=0 clxscore=1030 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2304210088 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net When using `bpftool map dump` in plain format, it is usually more convenient to show the inner map id instead of raw value. Changing this behavior would help with quick debugging with `bpftool`, without disruption scripted behavior. Since user could dump the inner map with id, but need to convert value. Signed-off-by: Xueming Feng --- tools/bpf/bpftool/main.c | 16 ++++++++++++++++ tools/bpf/bpftool/main.h | 1 + tools/bpf/bpftool/map.c | 9 +++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index 08d0ac543c67..d297200c91f7 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c @@ -251,6 +251,22 @@ int detect_common_prefix(const char *arg, ...) return 0; } +void fprint_uint(FILE *f, void *arg, unsigned int n) +{ + unsigned char *data = arg; + unsigned int data_uint = 0; + + for (unsigned int i = 0; i < n && i < 4; i++) { + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + data_uint |= data[i] << (i * 8); + #else + data_uint |= data[i] << ((n - i - 1) * 8); + #endif + } + + fprintf(f, "%d", data_uint); +} + void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep) { unsigned char *data = arg; diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index 0ef373cef4c7..7488ef38e7a9 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h @@ -90,6 +90,7 @@ void __printf(1, 2) p_info(const char *fmt, ...); bool is_prefix(const char *pfx, const char *str); int detect_common_prefix(const char *arg, ...); +void fprint_uint(FILE *f, void *arg, unsigned int n); void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep); void usage(void) __noreturn; diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index aaeb8939e137..638bd8de8135 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -259,8 +259,13 @@ static void print_entry_plain(struct bpf_map_info *info, unsigned char *key, } if (info->value_size) { - printf("value:%c", break_names ? '\n' : ' '); - fprint_hex(stdout, value, info->value_size, " "); + if (map_is_map_of_maps(info->type)) { + printf("id:%c", break_names ? '\n' : ' '); + fprint_uint(stdout, value, info->value_size); + } else { + printf("value:%c", break_names ? '\n' : ' '); + fprint_hex(stdout, value, info->value_size, " "); + } } printf("\n");