Standardize string representation of attach types by putting commonly used
names to libbpf.
The attach_type to string mapping is taken from bpftool:
tools/bpf/bpftool/cgroup.c
tools/bpf/bpftool/prog.c

Signed-off-by: Julia Kartseva <[email protected]>
---
 tools/lib/bpf/libbpf.c   | 50 ++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  5 ++++
 tools/lib/bpf/libbpf.map |  2 ++
 3 files changed, 57 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 9c531256888b..b5b07493655f 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -354,6 +354,32 @@ static const char *const map_type_strs[] = {
        [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash"
 };
 
+static const char *const attach_type_strs[] = {
+       [BPF_CGROUP_INET_INGRESS] = "ingress",
+       [BPF_CGROUP_INET_EGRESS] = "egress",
+       [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
+       [BPF_CGROUP_SOCK_OPS] = "sock_ops",
+       [BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
+       [BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
+       [BPF_CGROUP_DEVICE] = "device",
+       [BPF_SK_MSG_VERDICT] = "msg_verdict",
+       [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_LIRC_MODE2] = "lirc_mode2",
+       [BPF_FLOW_DISSECTOR] = "flow_dissector",
+       [BPF_CGROUP_SYSCTL] = "sysctl",
+       [BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4",
+       [BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6",
+       [BPF_CGROUP_GETSOCKOPT] = "getsockopt",
+       [BPF_CGROUP_SETSOCKOPT] = "setsockopt"
+};
+
 void bpf_program__unload(struct bpf_program *prog)
 {
        int i;
@@ -4734,6 +4760,30 @@ int libbpf_map_type_to_str(enum bpf_map_type type, const 
char **str)
        return 0;
 }
 
+int libbpf_attach_type_from_str(const char *str, enum bpf_attach_type *type)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(attach_type_strs); i++)
+               if (attach_type_strs[i] &&
+                   strcmp(attach_type_strs[i], str) == 0) {
+                       *type = i;
+                       return 0;
+               }
+
+       return -EINVAL;
+}
+
+int libbpf_attach_type_to_str(enum bpf_attach_type type, const char **str)
+{
+       if (type < BPF_CGROUP_INET_INGRESS ||
+           type >= ARRAY_SIZE(attach_type_strs))
+               return -EINVAL;
+
+       *str = attach_type_strs[type];
+       return 0;
+}
+
 static int
 bpf_program__identify_section(struct bpf_program *prog,
                              enum bpf_prog_type *prog_type,
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 90daeb2cdefb..0ad941951b0d 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -139,6 +139,11 @@ LIBBPF_API int libbpf_prog_type_to_str(enum bpf_prog_type 
type,
 LIBBPF_API int libbpf_map_type_from_str(const char *str,
                                        enum bpf_map_type *type);
 LIBBPF_API int libbpf_map_type_to_str(enum bpf_map_type type, const char 
**str);
+/* String representation of attach type */
+LIBBPF_API int libbpf_attach_type_from_str(const char *str,
+                                          enum bpf_attach_type *type);
+LIBBPF_API int libbpf_attach_type_to_str(enum bpf_attach_type type,
+                                        const char **str);
 
 /* Accessors of bpf_program */
 struct bpf_program;
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index e4ecf5414bb7..d87a6dc8a71f 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -192,4 +192,6 @@ LIBBPF_0.0.5 {
                libbpf_prog_type_to_str;
                libbpf_map_type_from_str;
                libbpf_map_type_to_str;
+               libbpf_attach_type_from_str;
+               libbpf_attach_type_to_str;
 } LIBBPF_0.0.4;
-- 
2.17.1

Reply via email to