Similar to prog_type to string mapping, standardize string representation of map types by putting commonly used names to libbpf. The map_type to string mapping is taken from bpftool: tools/bpf/bpftool/map.c Signed-off-by: Julia Kartseva <hex@xxxxxx> --- tools/lib/bpf/libbpf.c | 51 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 4 ++++ 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 946a4d41f223..9c531256888b 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -325,6 +325,35 @@ static const char *const prog_type_strs[] = { [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt", }; +static const char *const map_type_strs[] = { + [BPF_MAP_TYPE_UNSPEC] = "unspec", + [BPF_MAP_TYPE_HASH] = "hash", + [BPF_MAP_TYPE_ARRAY] = "array", + [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array", + [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array", + [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash", + [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array", + [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace", + [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array", + [BPF_MAP_TYPE_LRU_HASH] = "lru_hash", + [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash", + [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie", + [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps", + [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps", + [BPF_MAP_TYPE_DEVMAP] = "devmap", + [BPF_MAP_TYPE_SOCKMAP] = "sockmap", + [BPF_MAP_TYPE_CPUMAP] = "cpumap", + [BPF_MAP_TYPE_XSKMAP] = "xskmap", + [BPF_MAP_TYPE_SOCKHASH] = "sockhash", + [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage", + [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray", + [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage", + [BPF_MAP_TYPE_QUEUE] = "queue", + [BPF_MAP_TYPE_STACK] = "stack", + [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage", + [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash" +}; + void bpf_program__unload(struct bpf_program *prog) { int i; @@ -4683,6 +4712,28 @@ int libbpf_prog_type_to_str(enum bpf_prog_type type, const char **str) return 0; } +int libbpf_map_type_from_str(const char *str, enum bpf_map_type *type) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(map_type_strs); i++) + if (map_type_strs[i] && strcmp(map_type_strs[i], str) == 0) { + *type = i; + return 0; + } + + return -EINVAL; +} + +int libbpf_map_type_to_str(enum bpf_map_type type, const char **str) +{ + if (type < BPF_MAP_TYPE_UNSPEC || type >= ARRAY_SIZE(map_type_strs)) + return -EINVAL; + + *str = map_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 6846c488d8a2..90daeb2cdefb 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -135,6 +135,10 @@ LIBBPF_API int libbpf_prog_type_from_str(const char *str, enum bpf_prog_type *type); LIBBPF_API int libbpf_prog_type_to_str(enum bpf_prog_type type, const char **str); +/* String representation of map 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); /* Accessors of bpf_program */ struct bpf_program; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 2ea7c99f1579..e4ecf5414bb7 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -190,4 +190,6 @@ LIBBPF_0.0.5 { bpf_btf_get_next_id; libbpf_prog_type_from_str; libbpf_prog_type_to_str; + libbpf_map_type_from_str; + libbpf_map_type_to_str; } LIBBPF_0.0.4; -- 2.17.1