Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp3150072rwb; Wed, 30 Nov 2022 16:19:21 -0800 (PST) X-Google-Smtp-Source: AA0mqf4hr6LY+NkW1Ckaqq0UlpX5ucPhh3AGucG43V77cy3NKDBRsTlbZOSVH6buYsS1EzaFXp8S X-Received: by 2002:a17:906:fcd0:b0:7ad:b8c0:3057 with SMTP id qx16-20020a170906fcd000b007adb8c03057mr54532292ejb.440.1669853961292; Wed, 30 Nov 2022 16:19:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669853961; cv=none; d=google.com; s=arc-20160816; b=EGzoLNGn5JICizEpY3N+2q/8hMWztJdOsRNteBwepdea2oe0hzxcCulCLb8TbUIi++ dXRMWnafuj8z+p6j+5itemclJ52AsrXJEtlfJyGvduPE43kpllB/tXC9Zn67S/p8pknv 0H7vSbrsvN4o6xaYq8vga1D+i6fp54tiIeLD4vuialDTWxokFpZH8H8QgR1rTZv4PH2G hCcoyv2RCV0/nZhmKZe8qvzd0ux8EmHyflWCQKfEfMhQ1yGlglvZDsd2N3aMn2etAoRJ JrMsbhn1cmykn4VnHuKBMdeWLeKwJRHRJXV0YHYrN5wcMEDXdAVLoKrhBU4kPUDV2YZL mHow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=UQlapujHjKw7zEIFDE0bIVyHKZ7ZDtpnlk0TFhcybwA=; b=Lb997IEUFmEvu3QS5vul0PcWT1dAnTWS9SPl9JIuxUbTmSwSqjLaEPFyUGx8l0jkUX i5MMXoH8YyA3eAVcoJVJO0n9Bdl//EpuF/eysASoGCMI/4WOjHy9EptDaem+XQHTwv0I rnstKAA/OGPia2Seyak4MCt6pJWNTCa8ivv5FSMX682Z1BRkdPQdISdCVpUfqPU46MHj SbmsCtll1ppvxiSJ2pXA6mVQRfKuxzpr+I7Kq4m6tiu+z7QqSZw7tNCC+9bKPncGLUuC TZ7+EG/Zuy9T5jZUjjgY/R7ACxUuIr/9ISQrqvd2EqA9tnn8yMuJocIYuZU4xXE/GJaS 7jpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="Ysm8TV/w"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y17-20020a056402271100b004612505ece4si2648491edd.483.2022.11.30.16.19.01; Wed, 30 Nov 2022 16:19:21 -0800 (PST) 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=@gmail.com header.s=20210112 header.b="Ysm8TV/w"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229605AbiK3XGN (ORCPT + 83 others); Wed, 30 Nov 2022 18:06:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229586AbiK3XGL (ORCPT ); Wed, 30 Nov 2022 18:06:11 -0500 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B28D397033; Wed, 30 Nov 2022 15:06:07 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id s5so76819edc.12; Wed, 30 Nov 2022 15:06:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=UQlapujHjKw7zEIFDE0bIVyHKZ7ZDtpnlk0TFhcybwA=; b=Ysm8TV/wU765nO4TfDrEonAPGjEFGm6E5iiVUYJOpUTVRLXFO3g8PSVucc0QBO77qc RotMShY3b1/V8LPZ+LTzTvOwYtDXl1whxa23u4QnDrou4mw86hzWbB2eCC69gybYw7TW smQ5ZQXFdzRLVJu1tRka0CNIHqBmT/wN6ONRqZOsbUTSP9z0IdRTPONmCLEg8YTZND9D KmvHnVx4MzMB4nGmQMVWwjCW9Tt/5g8kUHJEpPsSx6OdWTbHV0/fS8i0NaJWBnhCCTEJ jppNDTC7lFN5JUGJCU/NNBRqRydvrYiGDi+FAvFuu7z25o5hk8qSlAm27IylwMNvc2k/ K9WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UQlapujHjKw7zEIFDE0bIVyHKZ7ZDtpnlk0TFhcybwA=; b=0oF7Pz2wmfcwxj93D4G47KqsMaJxgUTSEmbxJMeNa30l4U30ihwuvMaNvroOCLGBs3 pCjillY+bSJN+RHdli34+TUTR9wdQe9i5v2uC9YjPdUFUYORQVwKCEk5hr7b0h18zUB6 8gdWzgeMG3jZXKnL99jWgxwobui0AhzYfMaQZ+nQT3bl48USzzqpXClO+n4mJTeGyMlH Kfi1L/ciG7gkLZ853QjxtKYlEFDMvCltajBaNPNLSuQqVDFzWWliuwtt0TTU4X9yjyp6 AY2PHtXAyv0Q116O1n5tVicLpqtxsy9DTnDwL2WbjY4vCWZg28uGs+Yj8GwhRBdiI8Bu GAbQ== X-Gm-Message-State: ANoB5pnqQXRdiCXUr6jtmc8poYzQ68Qjp3ExJJc0S/da3IlIxESx7eKL HvDW4Ip/WNTqHjw0Y3NNHcuVCQQQcBpcsWw4ql/ePrCl X-Received: by 2002:a50:ed90:0:b0:46a:e6e3:b3cf with SMTP id h16-20020a50ed90000000b0046ae6e3b3cfmr21089221edr.333.1669849566068; Wed, 30 Nov 2022 15:06:06 -0800 (PST) MIME-Version: 1.0 References: <20221124151603.807536-1-benjamin.tissoires@redhat.com> <20221124151603.807536-2-benjamin.tissoires@redhat.com> In-Reply-To: <20221124151603.807536-2-benjamin.tissoires@redhat.com> From: Andrii Nakryiko Date: Wed, 30 Nov 2022 15:05:53 -0800 Message-ID: Subject: Re: [RFC hid v1 01/10] bpftool: generate json output of skeletons To: Benjamin Tissoires Cc: Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Dmitry Torokhov , Tero Kristo , linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 On Thu, Nov 24, 2022 at 7:16 AM Benjamin Tissoires wrote: > > So we can then build light skeletons with loaders in any language. > It would be useful to include an example generated JSON. Other than that the overall idea makes sense. Kind of machine-friendly "BPF object schema" to allow automation. > Signed-off-by: Benjamin Tissoires > --- > tools/bpf/bpftool/gen.c | 95 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 95 insertions(+) > > diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c > index cf8b4e525c88..818a5209b3ac 100644 > --- a/tools/bpf/bpftool/gen.c > +++ b/tools/bpf/bpftool/gen.c > @@ -904,6 +904,96 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li > } > } > > +static int gen_json(struct bpf_object *obj, const char *obj_name, size_t file_sz, uint8_t *obj_data) > +{ > + struct bpf_program *prog; > + struct bpf_map *map; > + char ident[256]; > + > + jsonw_start_object(json_wtr); /* root object */ > + > + jsonw_string_field(json_wtr, "name", obj_name); > + > + jsonw_bool_field(json_wtr, "use_loader", use_loader); > + > + /* print all maps */ > + jsonw_name(json_wtr, "maps"); > + jsonw_start_array(json_wtr); > + bpf_object__for_each_map(map, obj) { > + if (!get_map_ident(map, ident, sizeof(ident))) { > + p_err("ignoring unrecognized internal map '%s'...", > + bpf_map__name(map)); > + continue; > + } > + > + jsonw_start_object(json_wtr); /* map object */ > + jsonw_string_field(json_wtr, "ident", ident); > + jsonw_string_field(json_wtr, "name", bpf_map__name(map)); > + > + /* print mmap data value */ > + if (is_internal_mmapable_map(map, ident, sizeof(ident))) { > + const void *mmap_data = NULL; > + size_t mmap_size = 0; > + > + mmap_data = bpf_map__initial_value(map, &mmap_size); > + > + jsonw_uint_field(json_wtr, "size", mmap_size); > + jsonw_uint_field(json_wtr, "mmap_sz", bpf_map_mmap_sz(map)); > + jsonw_name(json_wtr, "data"); > + print_hex_data_json((uint8_t *)mmap_data, mmap_size); > + > + } > + jsonw_end_object(json_wtr); /* map object */ > + } > + jsonw_end_array(json_wtr); > + > + /* print all progs */ > + jsonw_name(json_wtr, "progs"); > + jsonw_start_array(json_wtr); > + bpf_object__for_each_program(prog, obj) { > + jsonw_start_object(json_wtr); /* prog object */ > + jsonw_string_field(json_wtr, "name", bpf_program__name(prog)); > + jsonw_string_field(json_wtr, "sec", bpf_program__section_name(prog)); > + jsonw_end_object(json_wtr); /* prog object */ > + } > + jsonw_end_array(json_wtr); > + > + /* print object data */ > + if (use_loader) { > + DECLARE_LIBBPF_OPTS(gen_loader_opts, opts); > + int err = 0; > + > + err = bpf_object__gen_loader(obj, &opts); > + if (err) > + return err; > + > + err = bpf_object__load(obj); > + if (err) { > + p_err("failed to load object file"); > + return err; > + } > + /* If there was no error during load then gen_loader_opts > + * are populated with the loader program. > + */ > + > + jsonw_uint_field(json_wtr, "data_sz", opts.data_sz); > + jsonw_name(json_wtr, "data"); > + print_hex_data_json((uint8_t *)opts.data, opts.data_sz); > + > + jsonw_uint_field(json_wtr, "insns_sz", opts.insns_sz); > + jsonw_name(json_wtr, "insns"); > + print_hex_data_json((uint8_t *)opts.insns, opts.insns_sz); > + > + } else { > + jsonw_name(json_wtr, "data"); > + print_hex_data_json(obj_data, file_sz); > + } > + > + jsonw_end_object(json_wtr); /* root object */ > + > + return 0; > +} > + > static int do_skeleton(int argc, char **argv) > { > char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")]; > @@ -986,6 +1076,11 @@ static int do_skeleton(int argc, char **argv) > goto out; > } > > + if (json_output) { > + err = gen_json(obj, obj_name, file_sz, (uint8_t *)obj_data); > + goto out; > + } > + > bpf_object__for_each_map(map, obj) { > if (!get_map_ident(map, ident, sizeof(ident))) { > p_err("ignoring unrecognized internal map '%s'...", > -- > 2.38.1 >