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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22169C54EE9 for ; Thu, 8 Sep 2022 18:40:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229822AbiIHSkN (ORCPT ); Thu, 8 Sep 2022 14:40:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229898AbiIHSkJ (ORCPT ); Thu, 8 Sep 2022 14:40:09 -0400 Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 432407E314 for ; Thu, 8 Sep 2022 11:40:08 -0700 (PDT) Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-1280590722dso13278131fac.1 for ; Thu, 08 Sep 2022 11:40:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=iTGfA/iMqnfd3k85Y1NHL8lddPFv4K9B34/OL+ENFuk=; b=SkxBMfsp5wAtlCovtLuMr1AVxuUWUg3DtsSV7o3P6CNBUxXKWEuOXhu/iiKX0a0+6r a/QXodyHXSrKOB6vrEoF+vQ3Aa/llnFfhwvEq1WcJMUvzPks1hpr/fFmIB+4uyAxVYU3 PLJikHFvceA7GyfAo7BBxifNw4XpGPtpDhFO5UcL8da2Fl5M0Y7csShGWXhR0HdusvmU 7Y668icsVolR0pxTIbA9WZpnX41q7XJCDYnQn7ody/gdcTon8O2SdSMTq+Fvgso/Ptst x13CDs5c7dIbxxoSlb3ovPzxzdQOCZXrzsM+XGfel43W2bUDFMHw2dbaVOXxpFSSIvMy UMLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=iTGfA/iMqnfd3k85Y1NHL8lddPFv4K9B34/OL+ENFuk=; b=nZvKjzjwAJXkoDGxYa7JVzr7tGGaC82s8pgUHS2nYXRWDlFJMHzgXDyaOs2dcFfXCV tgVV7G6bMJgtdu/yWqKptQkhXBr/yA7acmd+D6+KNvwKli/sc5ugHdnnlq35ID0lio01 xTpGWSJomlDR9fl4P9ryGyUD/nCiShBQDarpPTpAfgIWjIPRHH52p2tlCAFeNSJMz2Xf eHaXQ5dSzw6Vlllf3c0etZkdVlBx943QUzw71V4FvGJFrFeb4y5uWLnUk45c6a5VHvQL nKX9YnT66suAl36kmhrNiCpKD4r5fw7RQxhujOZRhGFM+WEmyS2hANH6bkBQfD+ZtvKl ovAw== X-Gm-Message-State: ACgBeo1TAdDpy3fGIEcNjTQq97ywMGunX6lHwoDu+EdQPSsDhxUfS+Sy gmRNLKGvyYKu7rx06K6v7PUYZRDgzME8Mw== X-Google-Smtp-Source: AA6agR6G6H4kYaYfOpI1HoI4NW2A/GwqMahoIFvCjNz6tfgGyRPyttt3tq8Q6ZLdDFSB5OKkkLJfGg== X-Received: by 2002:a05:6808:16aa:b0:34d:8758:34ef with SMTP id bb42-20020a05680816aa00b0034d875834efmr1016318oib.176.1662662407114; Thu, 08 Sep 2022 11:40:07 -0700 (PDT) Received: from localhost.localdomain (r74-192-156-22.gtwncmta01.grtntx.tl.dh.suddenlink.net. [74.192.156.22]) by smtp.googlemail.com with ESMTPSA id u18-20020a056870441200b00127a6357bd5sm5029946oah.49.2022.09.08.11.40.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 11:40:06 -0700 (PDT) From: Marcelo Juchem X-Google-Original-From: Marcelo Juchem To: bpf@vger.kernel.org Cc: Marcelo Juchem Subject: [PATCH] bpftool: output map/prog indices on `gen skeleton` Date: Thu, 8 Sep 2022 13:39:52 -0500 Message-Id: <20220908183952.3438815-1-mj@hunetr.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The skeleton generated by `bpftool` makes it easy to attach and load bpf objects as a whole. Some BPF programs are not directly portable across kernel versions, though, and require some cherry-picking on which programs to load/attach. The skeleton makes this cherry-picking possible, but not entirely friendly in some cases. For example, an useful feature is `attach_with_fallback` so that one program can be attempted, and fallback programs tried subsequently until one works (think `tcp_recvmsg` interface changing on kernel 5.19). Being able to represent a set of probes programatically in a way that is both descriptive, compile-time validated, runtime efficient and custom library friendly is quite desirable for application developers. A very simple way to represent a set of probes is with an array of indices. This patch creates a couple of enums under the `__cplusplus` section to represent the program and map indices inside the skeleton object, that can be used to refer to the proper program/map object. This is the code generated for the `__cplusplus` section of `profiler.skel.h`: ``` enum map_idxs: size_t { events = 0, fentry_readings = 1, accum_readings = 2, counts = 3, rodata = 4 }; enum prog_idxs: size_t { fentry_XXX = 0, fexit_XXX = 1 }; static inline struct profiler_bpf *open(const struct bpf_object_open_opts *opts = nullptr); static inline struct profiler_bpf *open_and_load(); static inline int load(struct profiler_bpf *skel); static inline int attach(struct profiler_bpf *skel); static inline void detach(struct profiler_bpf *skel); static inline void destroy(struct profiler_bpf *skel); static inline const void *elf_bytes(size_t *sz); ``` --- src/gen.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/gen.c b/src/gen.c index 7070dcf..7e28dc7 100644 --- a/src/gen.c +++ b/src/gen.c @@ -1086,6 +1086,38 @@ static int do_skeleton(int argc, char **argv) \n\ \n\ #ifdef __cplusplus \n\ + " + ); + + { + size_t i = 0; + printf("\tenum map_index: size_t {"); + bpf_object__for_each_map(map, obj) { + if (!get_map_ident(map, ident, sizeof(ident))) + continue; + if (i) { + printf(","); + } + printf("\n\t\t%s = %lu", ident, i); + ++i; + } + printf("\n\t};\n"); + } + { + size_t i = 0; + printf("\tenum prog_index: size_t {"); + bpf_object__for_each_program(prog, obj) { + if (i) { + printf(","); + } + printf("\n\t\t%s = %lu", bpf_program__name(prog), i); + ++i; + } + printf("\n\t};\n"); + } + + codegen("\ + \n\ static inline struct %1$s *open(const struct bpf_object_open_opts *opts = nullptr);\n\ static inline struct %1$s *open_and_load(); \n\ static inline int load(struct %1$s *skel); \n\ -- 2.37.2