bpftool: Expose attach_type-to-string array to non-cgroup code
authorAndrii Nakryiko <andriin@fb.com>
Wed, 29 Apr 2020 00:16:11 +0000 (17:16 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 29 Apr 2020 00:27:08 +0000 (17:27 -0700)
Move attach_type_strings into main.h for access in non-cgroup code.
bpf_attach_type is used for non-cgroup attach types quite widely now. So also
complete missing string translations for non-cgroup attach types.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200429001614.1544-8-andriin@fb.com
tools/bpf/bpftool/cgroup.c
tools/bpf/bpftool/main.h

index 62c6a1d..1693c80 100644 (file)
 
 static unsigned int query_flags;
 
-static const char * const attach_type_strings[] = {
-       [BPF_CGROUP_INET_INGRESS] = "ingress",
-       [BPF_CGROUP_INET_EGRESS] = "egress",
-       [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
-       [BPF_CGROUP_SOCK_OPS] = "sock_ops",
-       [BPF_CGROUP_DEVICE] = "device",
-       [BPF_CGROUP_INET4_BIND] = "bind4",
-       [BPF_CGROUP_INET6_BIND] = "bind6",
-       [BPF_CGROUP_INET4_CONNECT] = "connect4",
-       [BPF_CGROUP_INET6_CONNECT] = "connect6",
-       [BPF_CGROUP_INET4_POST_BIND] = "post_bind4",
-       [BPF_CGROUP_INET6_POST_BIND] = "post_bind6",
-       [BPF_CGROUP_UDP4_SENDMSG] = "sendmsg4",
-       [BPF_CGROUP_UDP6_SENDMSG] = "sendmsg6",
-       [BPF_CGROUP_SYSCTL] = "sysctl",
-       [BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4",
-       [BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6",
-       [BPF_CGROUP_GETSOCKOPT] = "getsockopt",
-       [BPF_CGROUP_SETSOCKOPT] = "setsockopt",
-       [__MAX_BPF_ATTACH_TYPE] = NULL,
-};
-
 static enum bpf_attach_type parse_attach_type(const char *str)
 {
        enum bpf_attach_type type;
 
        for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
-               if (attach_type_strings[type] &&
-                   is_prefix(str, attach_type_strings[type]))
+               if (attach_type_name[type] &&
+                   is_prefix(str, attach_type_name[type]))
                        return type;
        }
 
        return __MAX_BPF_ATTACH_TYPE;
 }
 
-static int show_bpf_prog(int id, const char *attach_type_str,
+static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
                         const char *attach_flags_str,
                         int level)
 {
@@ -86,18 +64,22 @@ static int show_bpf_prog(int id, const char *attach_type_str,
        if (json_output) {
                jsonw_start_object(json_wtr);
                jsonw_uint_field(json_wtr, "id", info.id);
-               jsonw_string_field(json_wtr, "attach_type",
-                                  attach_type_str);
+               if (attach_type < ARRAY_SIZE(attach_type_name))
+                       jsonw_string_field(json_wtr, "attach_type",
+                                          attach_type_name[attach_type]);
+               else
+                       jsonw_uint_field(json_wtr, "attach_type", attach_type);
                jsonw_string_field(json_wtr, "attach_flags",
                                   attach_flags_str);
                jsonw_string_field(json_wtr, "name", info.name);
                jsonw_end_object(json_wtr);
        } else {
-               printf("%s%-8u %-15s %-15s %-15s\n", level ? "    " : "",
-                      info.id,
-                      attach_type_str,
-                      attach_flags_str,
-                      info.name);
+               printf("%s%-8u ", level ? "    " : "", info.id);
+               if (attach_type < ARRAY_SIZE(attach_type_name))
+                       printf("%-15s", attach_type_name[attach_type]);
+               else
+                       printf("type %-10u", attach_type);
+               printf(" %-15s %-15s\n", attach_flags_str, info.name);
        }
 
        close(prog_fd);
@@ -171,7 +153,7 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
        }
 
        for (iter = 0; iter < prog_cnt; iter++)
-               show_bpf_prog(prog_ids[iter], attach_type_strings[type],
+               show_bpf_prog(prog_ids[iter], type,
                              attach_flags_str, level);
 
        return 0;
index 86f14ce..99d84bd 100644 (file)
@@ -83,6 +83,38 @@ static const char * const prog_type_name[] = {
        [BPF_PROG_TYPE_EXT]                     = "ext",
 };
 
+static const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = {
+       [BPF_CGROUP_INET_INGRESS] = "ingress",
+       [BPF_CGROUP_INET_EGRESS] = "egress",
+       [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
+       [BPF_CGROUP_SOCK_OPS] = "sock_ops",
+       [BPF_CGROUP_DEVICE] = "device",
+       [BPF_CGROUP_INET4_BIND] = "bind4",
+       [BPF_CGROUP_INET6_BIND] = "bind6",
+       [BPF_CGROUP_INET4_CONNECT] = "connect4",
+       [BPF_CGROUP_INET6_CONNECT] = "connect6",
+       [BPF_CGROUP_INET4_POST_BIND] = "post_bind4",
+       [BPF_CGROUP_INET6_POST_BIND] = "post_bind6",
+       [BPF_CGROUP_UDP4_SENDMSG] = "sendmsg4",
+       [BPF_CGROUP_UDP6_SENDMSG] = "sendmsg6",
+       [BPF_CGROUP_SYSCTL] = "sysctl",
+       [BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4",
+       [BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6",
+       [BPF_CGROUP_GETSOCKOPT] = "getsockopt",
+       [BPF_CGROUP_SETSOCKOPT] = "setsockopt",
+
+       [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser",
+       [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict",
+       [BPF_SK_MSG_VERDICT] = "sk_msg_verdict",
+       [BPF_LIRC_MODE2] = "lirc_mode2",
+       [BPF_FLOW_DISSECTOR] = "flow_dissector",
+       [BPF_TRACE_RAW_TP] = "raw_tp",
+       [BPF_TRACE_FENTRY] = "fentry",
+       [BPF_TRACE_FEXIT] = "fexit",
+       [BPF_MODIFY_RETURN] = "mod_ret",
+       [BPF_LSM_MAC] = "lsm_mac",
+};
+
 extern const char * const map_type_name[];
 extern const size_t map_type_name_size;