bpftool: Use libbpf_bpf_link_type_str
authorDaniel Müller <deso@posteo.net>
Mon, 23 May 2022 23:04:28 +0000 (23:04 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 2 Jun 2022 23:26:38 +0000 (16:26 -0700)
This change switches bpftool over to using the recently introduced
libbpf_bpf_link_type_str function instead of maintaining its own string
representation for the bpf_link_type enum.

Signed-off-by: Daniel Müller <deso@posteo.net>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20220523230428.3077108-13-deso@posteo.net
tools/bpf/bpftool/link.c

index 66a2545..7a20931 100644 (file)
 #include "json_writer.h"
 #include "main.h"
 
-static const char * const link_type_name[] = {
-       [BPF_LINK_TYPE_UNSPEC]                  = "unspec",
-       [BPF_LINK_TYPE_RAW_TRACEPOINT]          = "raw_tracepoint",
-       [BPF_LINK_TYPE_TRACING]                 = "tracing",
-       [BPF_LINK_TYPE_CGROUP]                  = "cgroup",
-       [BPF_LINK_TYPE_ITER]                    = "iter",
-       [BPF_LINK_TYPE_NETNS]                   = "netns",
-       [BPF_LINK_TYPE_XDP]                     = "xdp",
-       [BPF_LINK_TYPE_PERF_EVENT]              = "perf_event",
-       [BPF_LINK_TYPE_KPROBE_MULTI]            = "kprobe_multi",
-       [BPF_LINK_TYPE_STRUCT_OPS]               = "struct_ops",
-};
-
 static struct hashmap *link_table;
 
 static int link_parse_fd(int *argc, char ***argv)
@@ -67,9 +54,12 @@ static int link_parse_fd(int *argc, char ***argv)
 static void
 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
 {
+       const char *link_type_str;
+
        jsonw_uint_field(wtr, "id", info->id);
-       if (info->type < ARRAY_SIZE(link_type_name))
-               jsonw_string_field(wtr, "type", link_type_name[info->type]);
+       link_type_str = libbpf_bpf_link_type_str(info->type);
+       if (link_type_str)
+               jsonw_string_field(wtr, "type", link_type_str);
        else
                jsonw_uint_field(wtr, "type", info->type);
 
@@ -187,9 +177,12 @@ static int show_link_close_json(int fd, struct bpf_link_info *info)
 
 static void show_link_header_plain(struct bpf_link_info *info)
 {
+       const char *link_type_str;
+
        printf("%u: ", info->id);
-       if (info->type < ARRAY_SIZE(link_type_name))
-               printf("%s  ", link_type_name[info->type]);
+       link_type_str = libbpf_bpf_link_type_str(info->type);
+       if (link_type_str)
+               printf("%s  ", link_type_str);
        else
                printf("type %u  ", info->type);