libbpf: Introduce libbpf_bpf_map_type_str
authorDaniel Müller <deso@posteo.net>
Mon, 23 May 2022 23:04:20 +0000 (23:04 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 2 Jun 2022 23:26:20 +0000 (16:26 -0700)
This change introduces a new function, libbpf_bpf_map_type_str, to the
public libbpf API. The function allows users to get a string
representation for a bpf_map_type enum variant.

Signed-off-by: Daniel Müller <deso@posteo.net>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20220523230428.3077108-5-deso@posteo.net
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h
tools/lib/bpf/libbpf.map

index 01421af..a345bc1 100644 (file)
 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 map_type_name[] = {
+       [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_DEVMAP_HASH]              = "devmap_hash",
+       [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_STRUCT_OPS]               = "struct_ops",
+       [BPF_MAP_TYPE_RINGBUF]                  = "ringbuf",
+       [BPF_MAP_TYPE_INODE_STORAGE]            = "inode_storage",
+       [BPF_MAP_TYPE_TASK_STORAGE]             = "task_storage",
+       [BPF_MAP_TYPE_BLOOM_FILTER]             = "bloom_filter",
+};
+
 static const char * const prog_type_name[] = {
        [BPF_PROG_TYPE_UNSPEC]                  = "unspec",
        [BPF_PROG_TYPE_SOCKET_FILTER]           = "socket_filter",
@@ -9335,6 +9369,14 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
        return libbpf_err(-ESRCH);
 }
 
+const char *libbpf_bpf_map_type_str(enum bpf_map_type t)
+{
+       if (t < 0 || t >= ARRAY_SIZE(map_type_name))
+               return NULL;
+
+       return map_type_name[t];
+}
+
 const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t)
 {
        if (t < 0 || t >= ARRAY_SIZE(prog_type_name))
index 11b5f8c..6c5b077 100644 (file)
@@ -52,6 +52,15 @@ enum libbpf_errno {
 LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
 
 /**
+ * @brief **libbpf_bpf_map_type_str()** converts the provided map type value
+ * into a textual representation.
+ * @param t The map type.
+ * @return Pointer to a static string identifying the map type. NULL is
+ * returned for unknown **bpf_map_type** values.
+ */
+LIBBPF_API const char *libbpf_bpf_map_type_str(enum bpf_map_type t);
+
+/**
  * @brief **libbpf_bpf_prog_type_str()** converts the provided program type
  * value into a textual representation.
  * @param t The program type.
index b12d290..16065ac 100644 (file)
@@ -462,6 +462,7 @@ LIBBPF_0.8.0 {
 
 LIBBPF_1.0.0 {
        global:
+               libbpf_bpf_map_type_str;
                libbpf_bpf_prog_type_str;
 
        local: *;