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 0A608C433EF for ; Wed, 29 Jun 2022 20:36:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230501AbiF2Ugp (ORCPT ); Wed, 29 Jun 2022 16:36:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230408AbiF2Ugo (ORCPT ); Wed, 29 Jun 2022 16:36:44 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 918213A705 for ; Wed, 29 Jun 2022 13:36:43 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id b26so11780659wrc.2 for ; Wed, 29 Jun 2022 13:36:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ttD/svoirIxXxLwqSgiy25UwXzGhj+wZ4JgpAPoFz9E=; b=KgDiNniiYWBisspGSMN9xQ63GID+vC+UlaWOOZmi45qZRlDgwS0luI+CYOQFT0blfV dDLZLJMwk4I27vn+KYMEPVeCb+b4yJ5hIFV9ZxgKaOuUvNJCjSYQC3KD/DjwYCWeQSD7 JUx0H/lzWCe5vuEBBW5/MgKMG40WKh5DsKET1jOMPU1FhEVze4iIL0nHJ62zxQrkadnF QVBDlMONIjC/m91AhARsjG4ChRpdGyOaI9xXcZtVg42bmV3DTfZ7gvVutm7VkPS71MbN n9bWRbiHW8/r5KiZw2+5hg7gAn3mEwzLFXy+MVps2uvPPTUe2sL273shLvW/7FUe7PCL /jfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ttD/svoirIxXxLwqSgiy25UwXzGhj+wZ4JgpAPoFz9E=; b=wQjc9hALa14qTSelCc8J//HOCIul82Z0LOJFv8CQvXtUS0TMtrf661+BZlWgcoby/Z ISGz9cYsTgSDtMYkHqvt5kaRTNc4fEu42JbkFYAeQPb5sWkAhzhpmsDUSs1ZRxY0z9Gu fRqcND2nfeG7IwcFkv3q3T8zqxP7rvF1/aUQl5wABFKgfu99rPCzr6rt9krWCdHvKwhL F/fq0IHItMXEhZhlgMpzMQL4bqEwxfNpM5zQvo7Mt+xR7IOckeRkHQfs4uPSjR+hhOVH P8QIWeWupyBQg8s2P3AFNW08p3n7o4ykjuclOkHc9MUZV/UB0zVbHRP81QJAy2I+w+YQ 0+5Q== X-Gm-Message-State: AJIora9h5z+t1FKWZyiVEZuzNL26TUXS+S3A3qqyvxXgtizl2RNGzXrm /Fk9PuMbF6pYYc1Dib0lO4PAvA== X-Google-Smtp-Source: AGRyM1tQKG+Jh3QEDjiSJ9ky+EG7wHmknbGdBqxfMG+ZkeEUK3jTMDqKqcMMtPM+MfMhjw5IF0qp4Q== X-Received: by 2002:a05:6000:1888:b0:21d:151c:92a0 with SMTP id a8-20020a056000188800b0021d151c92a0mr4813311wri.609.1656535002161; Wed, 29 Jun 2022 13:36:42 -0700 (PDT) Received: from harfang.fritz.box ([51.155.200.13]) by smtp.gmail.com with ESMTPSA id l21-20020a05600c4f1500b003a02b9c47e4sm246986wmq.27.2022.06.29.13.36.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jun 2022 13:36:41 -0700 (PDT) From: Quentin Monnet To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , bpf@vger.kernel.org, netdev@vger.kernel.org, Quentin Monnet , =?UTF-8?q?Daniel=20M=C3=BCller?= Subject: [PATCH bpf-next v2 1/2] bpftool: Add feature list (prog/map/link/attach types, helpers) Date: Wed, 29 Jun 2022 21:36:36 +0100 Message-Id: <20220629203637.138944-2-quentin@isovalent.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220629203637.138944-1-quentin@isovalent.com> References: <20220629203637.138944-1-quentin@isovalent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Add a "bpftool feature list" subcommand to list BPF "features". Contrarily to "bpftool feature probe", this is not about the features available on the system. Instead, it lists all features known to bpftool from compilation time; in other words, all program, map, attach, link types known to the libbpf version in use, and all helpers found in the UAPI BPF header. The first use case for this feature is bash completion: running the command provides a list of types that can be used to produce the list of candidate map types, for example. Now that bpftool uses "standard" names provided by libbpf for the program, map, link, and attach types, having the ability to list these types and helpers could also be useful in scripts to loop over existing items. Sample output: # bpftool feature list prog_types | grep -vw unspec | head -n 6 socket_filter kprobe sched_cls sched_act tracepoint xdp # bpftool -p feature list map_types | jq '.[1]' "hash" # bpftool feature list attach_types | grep '^cgroup_' cgroup_inet_ingress cgroup_inet_egress [...] cgroup_inet_sock_release # bpftool feature list helpers | grep -vw bpf_unspec | wc -l 207 The "unspec" types and helpers are not filtered out by bpftool, so as to remain closer to the enums, and to preserve the indices in the JSON arrays (e.g. "hash" at index 1 == BPF_MAP_TYPE_HASH in map types list). v2: Add missing "link_types" to the list of GROUPs in man page. Acked-by: Daniel Müller Signed-off-by: Quentin Monnet --- .../bpftool/Documentation/bpftool-feature.rst | 12 ++++ tools/bpf/bpftool/bash-completion/bpftool | 7 ++- tools/bpf/bpftool/feature.c | 55 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/Documentation/bpftool-feature.rst b/tools/bpf/bpftool/Documentation/bpftool-feature.rst index 4ce9a77bc1e0..c08064628d39 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-feature.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-feature.rst @@ -24,9 +24,11 @@ FEATURE COMMANDS ================ | **bpftool** **feature probe** [*COMPONENT*] [**full**] [**unprivileged**] [**macros** [**prefix** *PREFIX*]] +| **bpftool** **feature list** *GROUP* | **bpftool** **feature help** | | *COMPONENT* := { **kernel** | **dev** *NAME* } +| *GROUP* := { **prog_types** | **map_types** | **attach_types** | **link_types** | **helpers** } DESCRIPTION =========== @@ -70,6 +72,16 @@ DESCRIPTION The keywords **full**, **macros** and **prefix** have the same role as when probing the kernel. + **bpftool feature list** *GROUP* + List items known to bpftool. These can be BPF program types + (**prog_types**), BPF map types (**map_types**), attach types + (**attach_types**), link types (**link_types**), or BPF helper + functions (**helpers**). The command does not probe the system, but + simply lists the elements that bpftool knows from compilation time, + as provided from libbpf (for all object types) or from the BPF UAPI + header (list of helpers). This can be used in scripts to iterate over + BPF types or helpers. + **bpftool feature help** Print short help message. diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool index 91f89a9a5b36..9cef6516320b 100644 --- a/tools/bpf/bpftool/bash-completion/bpftool +++ b/tools/bpf/bpftool/bash-completion/bpftool @@ -1175,9 +1175,14 @@ _bpftool() _bpftool_once_attr 'full unprivileged' return 0 ;; + list) + [[ $prev != "$command" ]] && return 0 + COMPREPLY=( $( compgen -W 'prog_types map_types \ + attach_types link_types helpers' -- "$cur" ) ) + ;; *) [[ $prev == $object ]] && \ - COMPREPLY=( $( compgen -W 'help probe' -- "$cur" ) ) + COMPREPLY=( $( compgen -W 'help list probe' -- "$cur" ) ) ;; esac ;; diff --git a/tools/bpf/bpftool/feature.c b/tools/bpf/bpftool/feature.c index bac4ef428a02..576cc6b90c6a 100644 --- a/tools/bpf/bpftool/feature.c +++ b/tools/bpf/bpftool/feature.c @@ -1258,6 +1258,58 @@ static int do_probe(int argc, char **argv) return 0; } +static const char *get_helper_name(unsigned int id) +{ + if (id >= ARRAY_SIZE(helper_name)) + return NULL; + + return helper_name[id]; +} + +static int do_list(int argc, char **argv) +{ + const char *(*get_name)(unsigned int id); + unsigned int id = 0; + + if (argc < 1) + usage(); + + if (is_prefix(*argv, "prog_types")) { + get_name = (const char *(*)(unsigned int))libbpf_bpf_prog_type_str; + } else if (is_prefix(*argv, "map_types")) { + get_name = (const char *(*)(unsigned int))libbpf_bpf_map_type_str; + } else if (is_prefix(*argv, "attach_types")) { + get_name = (const char *(*)(unsigned int))libbpf_bpf_attach_type_str; + } else if (is_prefix(*argv, "link_types")) { + get_name = (const char *(*)(unsigned int))libbpf_bpf_link_type_str; + } else if (is_prefix(*argv, "helpers")) { + get_name = get_helper_name; + } else { + p_err("expected 'prog_types', 'map_types', 'attach_types', 'link_types' or 'helpers', got: %s", *argv); + return -1; + } + + if (json_output) + jsonw_start_array(json_wtr); /* root array */ + + while (true) { + const char *name; + + name = get_name(id++); + if (!name) + break; + if (json_output) + jsonw_string(json_wtr, name); + else + printf("%s\n", name); + } + + if (json_output) + jsonw_end_array(json_wtr); /* root array */ + + return 0; +} + static int do_help(int argc, char **argv) { if (json_output) { @@ -1267,9 +1319,11 @@ static int do_help(int argc, char **argv) fprintf(stderr, "Usage: %1$s %2$s probe [COMPONENT] [full] [unprivileged] [macros [prefix PREFIX]]\n" + " %1$s %2$s list GROUP\n" " %1$s %2$s help\n" "\n" " COMPONENT := { kernel | dev NAME }\n" + " GROUP := { prog_types | map_types | attach_types | link_types | helpers }\n" " " HELP_SPEC_OPTIONS " }\n" "", bin_name, argv[-2]); @@ -1279,6 +1333,7 @@ static int do_help(int argc, char **argv) static const struct cmd cmds[] = { { "probe", do_probe }, + { "list", do_list }, { "help", do_help }, { 0 } }; -- 2.34.1