Replace lookup in `attach_type_strings` with libbpf_attach_type_to_str helper for cgroup (cgroup.c) and non-cgroup (prog.c) attach types. Signed-off-by: Julia Kartseva <hex@xxxxxx> --- tools/bpf/bpftool/cgroup.c | 60 +++++++++++++++++++++----------------- tools/bpf/bpftool/prog.c | 20 +++++++------ 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/tools/bpf/bpftool/cgroup.c b/tools/bpf/bpftool/cgroup.c index 1ef45e55039e..1b53218b06e7 100644 --- a/tools/bpf/bpftool/cgroup.c +++ b/tools/bpf/bpftool/cgroup.c @@ -15,6 +15,7 @@ #include <unistd.h> #include <bpf.h> +#include <libbpf.h> #include "main.h" @@ -31,35 +32,37 @@ 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 const enum bpf_attach_type cgroup_attach_types[] = { + BPF_CGROUP_INET_INGRESS, + BPF_CGROUP_INET_EGRESS, + BPF_CGROUP_INET_SOCK_CREATE, + BPF_CGROUP_SOCK_OPS, + BPF_CGROUP_DEVICE, + BPF_CGROUP_INET4_BIND, + BPF_CGROUP_INET6_BIND, + BPF_CGROUP_INET4_CONNECT, + BPF_CGROUP_INET6_CONNECT, + BPF_CGROUP_INET4_POST_BIND, + BPF_CGROUP_INET6_POST_BIND, + BPF_CGROUP_UDP4_SENDMSG, + BPF_CGROUP_UDP6_SENDMSG, + BPF_CGROUP_SYSCTL, + BPF_CGROUP_UDP4_RECVMSG, + BPF_CGROUP_UDP6_RECVMSG, + BPF_CGROUP_GETSOCKOPT, + BPF_CGROUP_SETSOCKOPT, }; static enum bpf_attach_type parse_attach_type(const char *str) { enum bpf_attach_type type; + const char *atype_str; + unsigned int i; - for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) { - if (attach_type_strings[type] && - is_prefix(str, attach_type_strings[type])) + for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++) { + type = cgroup_attach_types[i]; + if (!libbpf_attach_type_to_str(type, &atype_str) && + is_prefix(str, atype_str)) return type; } @@ -120,7 +123,7 @@ static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type) static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type, int level) { - const char *attach_flags_str; + const char *attach_flags_str, *atype_str; __u32 prog_ids[1024] = {0}; __u32 prog_cnt, iter; __u32 attach_flags; @@ -136,6 +139,11 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type, if (prog_cnt == 0) return 0; + ret = libbpf_attach_type_to_str(type, &atype_str); + + if (ret) + return 0; + switch (attach_flags) { case BPF_F_ALLOW_MULTI: attach_flags_str = "multi"; @@ -152,8 +160,8 @@ 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], - attach_flags_str, level); + show_bpf_prog(prog_ids[iter], atype_str, attach_flags_str, + level); return 0; } diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 8bbb24339a52..4dfec67e22fa 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -25,21 +25,23 @@ #include "main.h" #include "xlated_dumper.h" -static const char * const attach_type_strings[] = { - [BPF_SK_SKB_STREAM_PARSER] = "stream_parser", - [BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict", - [BPF_SK_MSG_VERDICT] = "msg_verdict", - [BPF_FLOW_DISSECTOR] = "flow_dissector", - [__MAX_BPF_ATTACH_TYPE] = NULL, +static const enum bpf_attach_type attach_types[] = { + BPF_SK_SKB_STREAM_PARSER, + BPF_SK_SKB_STREAM_VERDICT, + BPF_SK_MSG_VERDICT, + BPF_FLOW_DISSECTOR, }; static enum bpf_attach_type parse_attach_type(const char *str) { enum bpf_attach_type type; + const char *atype_str; + unsigned int i; - for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) { - if (attach_type_strings[type] && - is_prefix(str, attach_type_strings[type])) + for (i = 0; type < ARRAY_SIZE(attach_types); i++) { + type = attach_types[i]; + if (!libbpf_attach_type_to_str(type, &atype_str) && + is_prefix(str, atype_str)) return type; } -- 2.17.1