From patchwork Tue Jan 5 14:45:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Young X-Patchwork-Id: 11999173 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 X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable 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 CEF04C433E9 for ; Tue, 5 Jan 2021 14:46:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A492122B51 for ; Tue, 5 Jan 2021 14:46:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728183AbhAEOqU (ORCPT ); Tue, 5 Jan 2021 09:46:20 -0500 Received: from gofer.mess.org ([88.97.38.141]:46111 "EHLO gofer.mess.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728023AbhAEOqT (ORCPT ); Tue, 5 Jan 2021 09:46:19 -0500 Received: by gofer.mess.org (Postfix, from userid 1000) id 01ED6C63A5; Tue, 5 Jan 2021 14:45:34 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mess.org; s=2020; t=1609857935; bh=g86agJ+LPCw8ZkNJiHIjn0DJBtQq5JIr7Xq67deH28w=; h=From:To:Subject:Date:In-Reply-To:References:From; b=XHOybA/r4qqH1zaC2uKTFdjC2EijfmJkW3d1TIyxp0nrJ3ZBi+LQthgf5jiCUFEb5 zR+ZMU3J3k6BCc0FOnZuBWL5oVjbdlhIJpd7Hp7Mp5j1Sc4bF/vY84Kbfcc011OUpC LZLRppiXbiEV5ko1jHT4gPGaBJ2LbDbhTxkbHl0aouRai2FM98PG13HatJzHYbJX0C 6YRHdrobCpiPQwTp0K+OaV8gOwSNmuhhV97jW77PufdLz3CILYqcJduwwjsxKqtTl4 7gu5rHs/siDqlhWC+aa2QhOrIClNp8CagKEOoQiKJZcCTDh76JRPYUcJ0vryfv/N+P E4tn0ykEpX8gw== From: Sean Young To: Yonghong Song , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Nathan Chancellor , Nick Desaulniers , Quentin Monnet , =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8?= =?utf-8?q?rgensen?= , linux-doc@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Subject: [PATCH v3 3/4] bpftool: add support for ints larger than 128 bits Date: Tue, 5 Jan 2021 14:45:33 +0000 Message-Id: <67ffe6998af5cf88bdda6eaa1e6b085db1e093ed.1609855479.git.sean@mess.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net clang supports arbitrary length ints using the _ExtInt extension. This can be useful to hold very large values, e.g. 256 bit or 512 bit types. This requires the _ExtInt extension enabled in clang, which is under review. Link: https://clang.llvm.org/docs/LanguageExtensions.html#extended-integer-types Link: https://reviews.llvm.org/D93103 Signed-off-by: Sean Young --- tools/bpf/bpftool/btf_dumper.c | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c index 0e9310727281..8b5318ec5c26 100644 --- a/tools/bpf/bpftool/btf_dumper.c +++ b/tools/bpf/bpftool/btf_dumper.c @@ -271,6 +271,41 @@ static void btf_int128_print(json_writer_t *jw, const void *data, } } +static void btf_bigint_print(json_writer_t *jw, const void *data, int nr_bits, + bool is_plain_text) +{ + char buf[nr_bits / 4 + 1]; + int last_u64 = nr_bits / 64 - 1; + bool seen_nonzero = false; + int i; + + for (i = 0; i <= last_u64; i++) { +#ifdef __BIG_ENDIAN_BITFIELD + __u64 v = ((__u64 *)data)[i]; +#else + __u64 v = ((__u64 *)data)[last_u64 - i]; +#endif + + if (!seen_nonzero) { + if (!v && i != last_u64) + continue; + + snprintf(buf, sizeof(buf), "%llx", v); + + seen_nonzero = true; + } else { + size_t off = strlen(buf); + + snprintf(buf + off, sizeof(buf) - off, "%016llx", v); + } + } + + if (is_plain_text) + jsonw_printf(jw, "0x%s", buf); + else + jsonw_printf(jw, "\"0x%s\"", buf); +} + static void btf_int128_shift(__u64 *print_num, __u16 left_shift_bits, __u16 right_shift_bits) { @@ -373,6 +408,11 @@ static int btf_dumper_int(const struct btf_type *t, __u8 bit_offset, return 0; } + if (nr_bits > 128) { + btf_bigint_print(jw, data, nr_bits, is_plain_text); + return 0; + } + if (nr_bits == 128) { btf_int128_print(jw, data, is_plain_text); return 0;