Commit 50325b17 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov
Browse files

bpftool: Expose attach_type-to-string array to non-cgroup code



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: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Reviewed-by: default avatarQuentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200429001614.1544-8-andriin@fb.com
parent 2c2837b0
Loading
Loading
Loading
Loading
+15 −33
Original line number Diff line number Diff line
@@ -31,42 +31,20 @@

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);
		if (attach_type < ARRAY_SIZE(attach_type_name))
			jsonw_string_field(json_wtr, "attach_type",
				   attach_type_str);
					   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;
+32 −0
Original line number Diff line number Diff line
@@ -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;