Skip to content

Commit

Permalink
libbpf: Introduce libbpf_bpf_attach_type_str
Browse files Browse the repository at this point in the history
This change introduces a new function, libbpf_bpf_attach_type_str, to
the public libbpf API. The function allows users to get a string
representation for a bpf_attach_type variant.

Signed-off-by: Daniel Müller <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Acked-by: Quentin Monnet <[email protected]>
Acked-by: Yonghong Song <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
d-e-s-o authored and anakryiko committed Jun 2, 2022
1 parent 2e98964 commit ccde576
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,52 @@
static struct bpf_map *bpf_object__add_map(struct bpf_object *obj);
static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog);

static const char * const attach_type_name[] = {
[BPF_CGROUP_INET_INGRESS] = "cgroup_inet_ingress",
[BPF_CGROUP_INET_EGRESS] = "cgroup_inet_egress",
[BPF_CGROUP_INET_SOCK_CREATE] = "cgroup_inet_sock_create",
[BPF_CGROUP_INET_SOCK_RELEASE] = "cgroup_inet_sock_release",
[BPF_CGROUP_SOCK_OPS] = "cgroup_sock_ops",
[BPF_CGROUP_DEVICE] = "cgroup_device",
[BPF_CGROUP_INET4_BIND] = "cgroup_inet4_bind",
[BPF_CGROUP_INET6_BIND] = "cgroup_inet6_bind",
[BPF_CGROUP_INET4_CONNECT] = "cgroup_inet4_connect",
[BPF_CGROUP_INET6_CONNECT] = "cgroup_inet6_connect",
[BPF_CGROUP_INET4_POST_BIND] = "cgroup_inet4_post_bind",
[BPF_CGROUP_INET6_POST_BIND] = "cgroup_inet6_post_bind",
[BPF_CGROUP_INET4_GETPEERNAME] = "cgroup_inet4_getpeername",
[BPF_CGROUP_INET6_GETPEERNAME] = "cgroup_inet6_getpeername",
[BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname",
[BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname",
[BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg",
[BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg",
[BPF_CGROUP_SYSCTL] = "cgroup_sysctl",
[BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg",
[BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg",
[BPF_CGROUP_GETSOCKOPT] = "cgroup_getsockopt",
[BPF_CGROUP_SETSOCKOPT] = "cgroup_setsockopt",
[BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser",
[BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict",
[BPF_SK_SKB_VERDICT] = "sk_skb_verdict",
[BPF_SK_MSG_VERDICT] = "sk_msg_verdict",
[BPF_LIRC_MODE2] = "lirc_mode2",
[BPF_FLOW_DISSECTOR] = "flow_dissector",
[BPF_TRACE_RAW_TP] = "trace_raw_tp",
[BPF_TRACE_FENTRY] = "trace_fentry",
[BPF_TRACE_FEXIT] = "trace_fexit",
[BPF_MODIFY_RETURN] = "modify_return",
[BPF_LSM_MAC] = "lsm_mac",
[BPF_SK_LOOKUP] = "sk_lookup",
[BPF_TRACE_ITER] = "trace_iter",
[BPF_XDP_DEVMAP] = "xdp_devmap",
[BPF_XDP_CPUMAP] = "xdp_cpumap",
[BPF_XDP] = "xdp",
[BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select",
[BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate",
[BPF_PERF_EVENT] = "perf_event",
[BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi",
};

static const char * const map_type_name[] = {
[BPF_MAP_TYPE_UNSPEC] = "unspec",
[BPF_MAP_TYPE_HASH] = "hash",
Expand Down Expand Up @@ -9369,6 +9415,14 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
return libbpf_err(-ESRCH);
}

const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t)
{
if (t < 0 || t >= ARRAY_SIZE(attach_type_name))
return NULL;

return attach_type_name[t];
}

const char *libbpf_bpf_map_type_str(enum bpf_map_type t)
{
if (t < 0 || t >= ARRAY_SIZE(map_type_name))
Expand Down
9 changes: 9 additions & 0 deletions tools/lib/bpf/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ enum libbpf_errno {

LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);

/**
* @brief **libbpf_bpf_attach_type_str()** converts the provided attach type
* value into a textual representation.
* @param t The attach type.
* @return Pointer to a static string identifying the attach type. NULL is
* returned for unknown **bpf_attach_type** values.
*/
LIBBPF_API const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t);

/**
* @brief **libbpf_bpf_map_type_str()** converts the provided map type value
* into a textual representation.
Expand Down
1 change: 1 addition & 0 deletions tools/lib/bpf/libbpf.map
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ LIBBPF_0.8.0 {

LIBBPF_1.0.0 {
global:
libbpf_bpf_attach_type_str;
libbpf_bpf_map_type_str;
libbpf_bpf_prog_type_str;

Expand Down

0 comments on commit ccde576

Please sign in to comment.