Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp693178rwb; Thu, 1 Dec 2022 07:17:16 -0800 (PST) X-Google-Smtp-Source: AA0mqf5+0+ccFVPsTGgBCeEOdKDdn/uV7EwNI2P30dpSJdW3TM+O9VqD3zoK8gohfde6XVOruiuI X-Received: by 2002:a17:906:89a3:b0:78d:408a:4a18 with SMTP id gg35-20020a17090689a300b0078d408a4a18mr56892833ejc.261.1669907836599; Thu, 01 Dec 2022 07:17:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669907836; cv=none; d=google.com; s=arc-20160816; b=ZfC9xo5JI03pnld+qxaXTHDVw2igIitnDL3JoIcfC2beDucdqMFL+cM6RRDa0dhQbz MA6U3IAMtqD3OSzZVOpSeVu1Gv7N8+cEmCEdaA6iqGjcREocydD6Vra3qDmFv1V/xnp2 DmXIg5xcVlTay2t8Ec4pXlWnHqZm4hJvIsLjyvcr55g3vtZ+/sxA8tdmg60X8RJUGlA0 Ury6aazuHjXDA1ZKUbYIo4JvO1HDNbPAereffR3WVhG76zLVmENGHwM6vvbLP9c3d+pL 9V1ZSOLnYEKnU620NlEKMbF2fRqMlEnmbQl7bt8zieNOkYQqs2tzbqmhxO22c28Jlk1s XpBw== 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=SR3PJdSecGTlAfxSaOagL/vi51rfippLgIgBQ/FaDaE=; b=lI+SqiDrXkus0po4BnhS7JA3uM7r4dASEktrf9hRmCDFQKNIiZltADRReaww+nFTfz oyFZk0OsQ6ftnW6Fn9oaXiaWpH1z5YARePyB03y5xGnRwNAj8u7it2PZweS8OWITy43z fRnZ3s5i1viQKd3aFbK12jwzx22Qn41sUY73p+Ru3z3RyHkR2XxmZUQg8pVxknVbYTqS vQXgt9jIzK8TrxRYn363xzm2IgIr3cdeC9hnX3wkamEfHHWT2+8DJ79qSajh4Lw/bJG0 PVwZEAIFstBCDiaIi4kLCdIMIIACHMNBWrkxf6xSBSDrMPDSESe1g9A90IjcHaJETUbl 66xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=TdRUtLzJ; 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=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h12-20020a05640250cc00b004692dec65dbsi3998885edb.259.2022.12.01.07.16.56; Thu, 01 Dec 2022 07:17:16 -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=@redhat.com header.s=mimecast20190719 header.b=TdRUtLzJ; 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=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231193AbiLAOYN (ORCPT + 82 others); Thu, 1 Dec 2022 09:24:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231521AbiLAOYA (ORCPT ); Thu, 1 Dec 2022 09:24:00 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DF919E44B for ; Thu, 1 Dec 2022 06:23:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669904582; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=SR3PJdSecGTlAfxSaOagL/vi51rfippLgIgBQ/FaDaE=; b=TdRUtLzJ8kmn9uV9XrY1kWPSxLBm8vITm1qRgwKEUdbUH35Vz+TD+TIH0uhtUu6u9n42Vu j65NdaDHB+o8LElSl2gHdUX1RaCu5uy7iArPEdlPjkw5JiBUztzCffLmWgP8vBJFJkW527 NnxjSpV+BMgQnWCQTfnSaZ1sUb6RL0g= Received: from mail-il1-f199.google.com (mail-il1-f199.google.com [209.85.166.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-392-Ax789COcMUaXq0sSQtoX2Q-1; Thu, 01 Dec 2022 09:23:01 -0500 X-MC-Unique: Ax789COcMUaXq0sSQtoX2Q-1 Received: by mail-il1-f199.google.com with SMTP id h20-20020a056e021d9400b00300581edaa5so2074696ila.12 for ; Thu, 01 Dec 2022 06:23:01 -0800 (PST) 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=SR3PJdSecGTlAfxSaOagL/vi51rfippLgIgBQ/FaDaE=; b=w8cjILbh0KyqXp3jfrDCrZjjsUwGt9hEcmMS5Wib+38QUR3UXc4Ria3HL7aJ0YKYLe emvw1QRdPDTHeOibpJr1VN2LG3ZguHYbI11x2zLR4FnOh2ES3Gzyas3kag/XrrBGDKra ZjWvGMKEJsUWvFfKkg0Mt2U+QdfrKLIbho5ePBv2wYDD0iTxb4RrUNWxx8na/y+cetkh 6cGnPNyXsf5nCXAnF2N8cuwwMz6WUo4w6+07dZOftb5kVwCvDwSMgnNE59xdl4rZpzL+ 1ZAYuyptD4GuvLg4UaYxKriA37tWK2Aoo8AhNnQwcXyT+TjJmScvGOtttLbE+gZv9TF7 qa9g== X-Gm-Message-State: ANoB5pkJXXbMM086LM8fG0KodtLC0TVgvLNbdFXFhB5PzB7nHGqguBrB /T/u5/Xyjr1Ftn0isSVhh3aEfsOLl9eXGtO4JgkpYNRDE0F21zQFvRbC+/79JdXgZaN9M4UkoC2 3Mp6yvoUAmPsPm3lJeJBsnoC6D/qG76G/RgQhxSlB X-Received: by 2002:a5e:a50c:0:b0:6d6:3d7e:1592 with SMTP id 12-20020a5ea50c000000b006d63d7e1592mr23083916iog.37.1669904580885; Thu, 01 Dec 2022 06:23:00 -0800 (PST) X-Received: by 2002:a5e:a50c:0:b0:6d6:3d7e:1592 with SMTP id 12-20020a5ea50c000000b006d63d7e1592mr23083910iog.37.1669904580642; Thu, 01 Dec 2022 06:23:00 -0800 (PST) MIME-Version: 1.0 References: <20221124151603.807536-1-benjamin.tissoires@redhat.com> <20221124151603.807536-2-benjamin.tissoires@redhat.com> In-Reply-To: From: Benjamin Tissoires Date: Thu, 1 Dec 2022 15:22:49 +0100 Message-ID: Subject: Re: [RFC hid v1 01/10] bpftool: generate json output of skeletons To: Andrii Nakryiko 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,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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, Dec 1, 2022 at 12:06 AM Andrii Nakryiko wrote: > > 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. > Great :) I'll add the generated example in v2 then. However, I have problems figuring out how I can hit the "codegen_datasecs()" path. I think that's the only code path I am not duplicating from the do_skeleton() function, but I don't know what kind of .bpf.c program will trigger that part. Also, should I add some tests for it in tools/testing/selftests/bpf? Cheers, Benjamin > > 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 > > >