bpf: Set map_btf_{name, id} for all map types
authorAndrey Ignatov <rdna@fb.com>
Fri, 19 Jun 2020 21:11:44 +0000 (14:11 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 22 Jun 2020 20:22:58 +0000 (22:22 +0200)
Set map_btf_name and map_btf_id for all map types so that map fields can
be accessed by bpf programs.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/a825f808f22af52b018dbe82f1c7d29dab5fc978.1592600985.git.rdna@fb.com
14 files changed:
kernel/bpf/arraymap.c
kernel/bpf/bpf_struct_ops.c
kernel/bpf/cpumap.c
kernel/bpf/devmap.c
kernel/bpf/hashtab.c
kernel/bpf/local_storage.c
kernel/bpf/lpm_trie.c
kernel/bpf/queue_stack_maps.c
kernel/bpf/reuseport_array.c
kernel/bpf/ringbuf.c
kernel/bpf/stackmap.c
net/core/bpf_sk_storage.c
net/core/sock_map.c
net/xdp/xskmap.c

index e7caa48..ec5cd11 100644 (file)
@@ -515,6 +515,7 @@ const struct bpf_map_ops array_map_ops = {
        .map_btf_id = &array_map_btf_id,
 };
 
+static int percpu_array_map_btf_id;
 const struct bpf_map_ops percpu_array_map_ops = {
        .map_alloc_check = array_map_alloc_check,
        .map_alloc = array_map_alloc,
@@ -525,6 +526,8 @@ const struct bpf_map_ops percpu_array_map_ops = {
        .map_delete_elem = array_map_delete_elem,
        .map_seq_show_elem = percpu_array_map_seq_show_elem,
        .map_check_btf = array_map_check_btf,
+       .map_btf_name = "bpf_array",
+       .map_btf_id = &percpu_array_map_btf_id,
 };
 
 static int fd_array_map_alloc_check(union bpf_attr *attr)
@@ -871,6 +874,7 @@ static void prog_array_map_free(struct bpf_map *map)
        fd_array_map_free(map);
 }
 
+static int prog_array_map_btf_id;
 const struct bpf_map_ops prog_array_map_ops = {
        .map_alloc_check = fd_array_map_alloc_check,
        .map_alloc = prog_array_map_alloc,
@@ -886,6 +890,8 @@ const struct bpf_map_ops prog_array_map_ops = {
        .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
        .map_release_uref = prog_array_map_clear,
        .map_seq_show_elem = prog_array_map_seq_show_elem,
+       .map_btf_name = "bpf_array",
+       .map_btf_id = &prog_array_map_btf_id,
 };
 
 static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
@@ -964,6 +970,7 @@ static void perf_event_fd_array_release(struct bpf_map *map,
        rcu_read_unlock();
 }
 
+static int perf_event_array_map_btf_id;
 const struct bpf_map_ops perf_event_array_map_ops = {
        .map_alloc_check = fd_array_map_alloc_check,
        .map_alloc = array_map_alloc,
@@ -975,6 +982,8 @@ const struct bpf_map_ops perf_event_array_map_ops = {
        .map_fd_put_ptr = perf_event_fd_array_put_ptr,
        .map_release = perf_event_fd_array_release,
        .map_check_btf = map_check_no_btf,
+       .map_btf_name = "bpf_array",
+       .map_btf_id = &perf_event_array_map_btf_id,
 };
 
 #ifdef CONFIG_CGROUPS
@@ -997,6 +1006,7 @@ static void cgroup_fd_array_free(struct bpf_map *map)
        fd_array_map_free(map);
 }
 
+static int cgroup_array_map_btf_id;
 const struct bpf_map_ops cgroup_array_map_ops = {
        .map_alloc_check = fd_array_map_alloc_check,
        .map_alloc = array_map_alloc,
@@ -1007,6 +1017,8 @@ const struct bpf_map_ops cgroup_array_map_ops = {
        .map_fd_get_ptr = cgroup_fd_array_get_ptr,
        .map_fd_put_ptr = cgroup_fd_array_put_ptr,
        .map_check_btf = map_check_no_btf,
+       .map_btf_name = "bpf_array",
+       .map_btf_id = &cgroup_array_map_btf_id,
 };
 #endif
 
@@ -1080,6 +1092,7 @@ static u32 array_of_map_gen_lookup(struct bpf_map *map,
        return insn - insn_buf;
 }
 
+static int array_of_maps_map_btf_id;
 const struct bpf_map_ops array_of_maps_map_ops = {
        .map_alloc_check = fd_array_map_alloc_check,
        .map_alloc = array_of_map_alloc,
@@ -1092,4 +1105,6 @@ const struct bpf_map_ops array_of_maps_map_ops = {
        .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
        .map_gen_lookup = array_of_map_gen_lookup,
        .map_check_btf = map_check_no_btf,
+       .map_btf_name = "bpf_array",
+       .map_btf_id = &array_of_maps_map_btf_id,
 };
index c6b0dec..969c5d4 100644 (file)
@@ -611,6 +611,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
        return map;
 }
 
+static int bpf_struct_ops_map_btf_id;
 const struct bpf_map_ops bpf_struct_ops_map_ops = {
        .map_alloc_check = bpf_struct_ops_map_alloc_check,
        .map_alloc = bpf_struct_ops_map_alloc,
@@ -620,6 +621,8 @@ const struct bpf_map_ops bpf_struct_ops_map_ops = {
        .map_delete_elem = bpf_struct_ops_map_delete_elem,
        .map_update_elem = bpf_struct_ops_map_update_elem,
        .map_seq_show_elem = bpf_struct_ops_map_seq_show_elem,
+       .map_btf_name = "bpf_struct_ops_map",
+       .map_btf_id = &bpf_struct_ops_map_btf_id,
 };
 
 /* "const void *" because some subsystem is
index 27595fc..bd86580 100644 (file)
@@ -543,6 +543,7 @@ static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
        return 0;
 }
 
+static int cpu_map_btf_id;
 const struct bpf_map_ops cpu_map_ops = {
        .map_alloc              = cpu_map_alloc,
        .map_free               = cpu_map_free,
@@ -551,6 +552,8 @@ const struct bpf_map_ops cpu_map_ops = {
        .map_lookup_elem        = cpu_map_lookup_elem,
        .map_get_next_key       = cpu_map_get_next_key,
        .map_check_btf          = map_check_no_btf,
+       .map_btf_name           = "bpf_cpu_map",
+       .map_btf_id             = &cpu_map_btf_id,
 };
 
 static int bq_flush_to_queue(struct xdp_bulk_queue *bq)
index 0cbb72c..58acc46 100644 (file)
@@ -747,6 +747,7 @@ static int dev_map_hash_update_elem(struct bpf_map *map, void *key, void *value,
                                         map, key, value, map_flags);
 }
 
+static int dev_map_btf_id;
 const struct bpf_map_ops dev_map_ops = {
        .map_alloc = dev_map_alloc,
        .map_free = dev_map_free,
@@ -755,8 +756,11 @@ const struct bpf_map_ops dev_map_ops = {
        .map_update_elem = dev_map_update_elem,
        .map_delete_elem = dev_map_delete_elem,
        .map_check_btf = map_check_no_btf,
+       .map_btf_name = "bpf_dtab",
+       .map_btf_id = &dev_map_btf_id,
 };
 
+static int dev_map_hash_map_btf_id;
 const struct bpf_map_ops dev_map_hash_ops = {
        .map_alloc = dev_map_alloc,
        .map_free = dev_map_free,
@@ -765,6 +769,8 @@ const struct bpf_map_ops dev_map_hash_ops = {
        .map_update_elem = dev_map_hash_update_elem,
        .map_delete_elem = dev_map_hash_delete_elem,
        .map_check_btf = map_check_no_btf,
+       .map_btf_name = "bpf_dtab",
+       .map_btf_id = &dev_map_hash_map_btf_id,
 };
 
 static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
index 2c5999e..acd0608 100644 (file)
@@ -1630,6 +1630,7 @@ const struct bpf_map_ops htab_map_ops = {
        .map_btf_id = &htab_map_btf_id,
 };
 
+static int htab_lru_map_btf_id;
 const struct bpf_map_ops htab_lru_map_ops = {
        .map_alloc_check = htab_map_alloc_check,
        .map_alloc = htab_map_alloc,
@@ -1642,6 +1643,8 @@ const struct bpf_map_ops htab_lru_map_ops = {
        .map_gen_lookup = htab_lru_map_gen_lookup,
        .map_seq_show_elem = htab_map_seq_show_elem,
        BATCH_OPS(htab_lru),
+       .map_btf_name = "bpf_htab",
+       .map_btf_id = &htab_lru_map_btf_id,
 };
 
 /* Called from eBPF program */
@@ -1746,6 +1749,7 @@ static void htab_percpu_map_seq_show_elem(struct bpf_map *map, void *key,
        rcu_read_unlock();
 }
 
+static int htab_percpu_map_btf_id;
 const struct bpf_map_ops htab_percpu_map_ops = {
        .map_alloc_check = htab_map_alloc_check,
        .map_alloc = htab_map_alloc,
@@ -1756,8 +1760,11 @@ const struct bpf_map_ops htab_percpu_map_ops = {
        .map_delete_elem = htab_map_delete_elem,
        .map_seq_show_elem = htab_percpu_map_seq_show_elem,
        BATCH_OPS(htab_percpu),
+       .map_btf_name = "bpf_htab",
+       .map_btf_id = &htab_percpu_map_btf_id,
 };
 
+static int htab_lru_percpu_map_btf_id;
 const struct bpf_map_ops htab_lru_percpu_map_ops = {
        .map_alloc_check = htab_map_alloc_check,
        .map_alloc = htab_map_alloc,
@@ -1768,6 +1775,8 @@ const struct bpf_map_ops htab_lru_percpu_map_ops = {
        .map_delete_elem = htab_lru_map_delete_elem,
        .map_seq_show_elem = htab_percpu_map_seq_show_elem,
        BATCH_OPS(htab_lru_percpu),
+       .map_btf_name = "bpf_htab",
+       .map_btf_id = &htab_lru_percpu_map_btf_id,
 };
 
 static int fd_htab_map_alloc_check(union bpf_attr *attr)
@@ -1890,6 +1899,7 @@ static void htab_of_map_free(struct bpf_map *map)
        fd_htab_map_free(map);
 }
 
+static int htab_of_maps_map_btf_id;
 const struct bpf_map_ops htab_of_maps_map_ops = {
        .map_alloc_check = fd_htab_map_alloc_check,
        .map_alloc = htab_of_map_alloc,
@@ -1902,4 +1912,6 @@ const struct bpf_map_ops htab_of_maps_map_ops = {
        .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
        .map_gen_lookup = htab_of_map_gen_lookup,
        .map_check_btf = map_check_no_btf,
+       .map_btf_name = "bpf_htab",
+       .map_btf_id = &htab_of_maps_map_btf_id,
 };
index 33d0186..51bd5a8 100644 (file)
@@ -409,6 +409,7 @@ static void cgroup_storage_seq_show_elem(struct bpf_map *map, void *_key,
        rcu_read_unlock();
 }
 
+static int cgroup_storage_map_btf_id;
 const struct bpf_map_ops cgroup_storage_map_ops = {
        .map_alloc = cgroup_storage_map_alloc,
        .map_free = cgroup_storage_map_free,
@@ -418,6 +419,8 @@ const struct bpf_map_ops cgroup_storage_map_ops = {
        .map_delete_elem = cgroup_storage_delete_elem,
        .map_check_btf = cgroup_storage_check_btf,
        .map_seq_show_elem = cgroup_storage_seq_show_elem,
+       .map_btf_name = "bpf_cgroup_storage_map",
+       .map_btf_id = &cgroup_storage_map_btf_id,
 };
 
 int bpf_cgroup_storage_assign(struct bpf_prog_aux *aux, struct bpf_map *_map)
index c8cc4e4..1abd4e3 100644 (file)
@@ -735,6 +735,7 @@ static int trie_check_btf(const struct bpf_map *map,
               -EINVAL : 0;
 }
 
+static int trie_map_btf_id;
 const struct bpf_map_ops trie_map_ops = {
        .map_alloc = trie_alloc,
        .map_free = trie_free,
@@ -743,4 +744,6 @@ const struct bpf_map_ops trie_map_ops = {
        .map_update_elem = trie_update_elem,
        .map_delete_elem = trie_delete_elem,
        .map_check_btf = trie_check_btf,
+       .map_btf_name = "lpm_trie",
+       .map_btf_id = &trie_map_btf_id,
 };
index 05c8e04..80c66a6 100644 (file)
@@ -262,6 +262,7 @@ static int queue_stack_map_get_next_key(struct bpf_map *map, void *key,
        return -EINVAL;
 }
 
+static int queue_map_btf_id;
 const struct bpf_map_ops queue_map_ops = {
        .map_alloc_check = queue_stack_map_alloc_check,
        .map_alloc = queue_stack_map_alloc,
@@ -273,8 +274,11 @@ const struct bpf_map_ops queue_map_ops = {
        .map_pop_elem = queue_map_pop_elem,
        .map_peek_elem = queue_map_peek_elem,
        .map_get_next_key = queue_stack_map_get_next_key,
+       .map_btf_name = "bpf_queue_stack",
+       .map_btf_id = &queue_map_btf_id,
 };
 
+static int stack_map_btf_id;
 const struct bpf_map_ops stack_map_ops = {
        .map_alloc_check = queue_stack_map_alloc_check,
        .map_alloc = queue_stack_map_alloc,
@@ -286,4 +290,6 @@ const struct bpf_map_ops stack_map_ops = {
        .map_pop_elem = stack_map_pop_elem,
        .map_peek_elem = stack_map_peek_elem,
        .map_get_next_key = queue_stack_map_get_next_key,
+       .map_btf_name = "bpf_queue_stack",
+       .map_btf_id = &stack_map_btf_id,
 };
index 21cde24..a09922f 100644 (file)
@@ -345,6 +345,7 @@ static int reuseport_array_get_next_key(struct bpf_map *map, void *key,
        return 0;
 }
 
+static int reuseport_array_map_btf_id;
 const struct bpf_map_ops reuseport_array_ops = {
        .map_alloc_check = reuseport_array_alloc_check,
        .map_alloc = reuseport_array_alloc,
@@ -352,4 +353,6 @@ const struct bpf_map_ops reuseport_array_ops = {
        .map_lookup_elem = reuseport_array_lookup_elem,
        .map_get_next_key = reuseport_array_get_next_key,
        .map_delete_elem = reuseport_array_delete_elem,
+       .map_btf_name = "reuseport_array",
+       .map_btf_id = &reuseport_array_map_btf_id,
 };
index 180414b..dbf37af 100644 (file)
@@ -294,6 +294,7 @@ static __poll_t ringbuf_map_poll(struct bpf_map *map, struct file *filp,
        return 0;
 }
 
+static int ringbuf_map_btf_id;
 const struct bpf_map_ops ringbuf_map_ops = {
        .map_alloc = ringbuf_map_alloc,
        .map_free = ringbuf_map_free,
@@ -303,6 +304,8 @@ const struct bpf_map_ops ringbuf_map_ops = {
        .map_update_elem = ringbuf_map_update_elem,
        .map_delete_elem = ringbuf_map_delete_elem,
        .map_get_next_key = ringbuf_map_get_next_key,
+       .map_btf_name = "bpf_ringbuf_map",
+       .map_btf_id = &ringbuf_map_btf_id,
 };
 
 /* Given pointer to ring buffer record metadata and struct bpf_ringbuf itself,
index 599488f..27dc9b1 100644 (file)
@@ -613,6 +613,7 @@ static void stack_map_free(struct bpf_map *map)
        put_callchain_buffers();
 }
 
+static int stack_trace_map_btf_id;
 const struct bpf_map_ops stack_trace_map_ops = {
        .map_alloc = stack_map_alloc,
        .map_free = stack_map_free,
@@ -621,6 +622,8 @@ const struct bpf_map_ops stack_trace_map_ops = {
        .map_update_elem = stack_map_update_elem,
        .map_delete_elem = stack_map_delete_elem,
        .map_check_btf = map_check_no_btf,
+       .map_btf_name = "bpf_stack_map",
+       .map_btf_id = &stack_trace_map_btf_id,
 };
 
 static int __init stack_map_init(void)
index 1dae4b5..6f921c4 100644 (file)
@@ -919,6 +919,7 @@ BPF_CALL_2(bpf_sk_storage_delete, struct bpf_map *, map, struct sock *, sk)
        return -ENOENT;
 }
 
+static int sk_storage_map_btf_id;
 const struct bpf_map_ops sk_storage_map_ops = {
        .map_alloc_check = bpf_sk_storage_map_alloc_check,
        .map_alloc = bpf_sk_storage_map_alloc,
@@ -928,6 +929,8 @@ const struct bpf_map_ops sk_storage_map_ops = {
        .map_update_elem = bpf_fd_sk_storage_update_elem,
        .map_delete_elem = bpf_fd_sk_storage_delete_elem,
        .map_check_btf = bpf_sk_storage_map_check_btf,
+       .map_btf_name = "bpf_sk_storage_map",
+       .map_btf_id = &sk_storage_map_btf_id,
 };
 
 const struct bpf_func_proto bpf_sk_storage_get_proto = {
index 2b884f2..4c1123c 100644 (file)
@@ -643,6 +643,7 @@ const struct bpf_func_proto bpf_msg_redirect_map_proto = {
        .arg4_type      = ARG_ANYTHING,
 };
 
+static int sock_map_btf_id;
 const struct bpf_map_ops sock_map_ops = {
        .map_alloc              = sock_map_alloc,
        .map_free               = sock_map_free,
@@ -653,6 +654,8 @@ const struct bpf_map_ops sock_map_ops = {
        .map_lookup_elem        = sock_map_lookup,
        .map_release_uref       = sock_map_release_progs,
        .map_check_btf          = map_check_no_btf,
+       .map_btf_name           = "bpf_stab",
+       .map_btf_id             = &sock_map_btf_id,
 };
 
 struct bpf_shtab_elem {
@@ -1176,6 +1179,7 @@ const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
        .arg4_type      = ARG_ANYTHING,
 };
 
+static int sock_hash_map_btf_id;
 const struct bpf_map_ops sock_hash_ops = {
        .map_alloc              = sock_hash_alloc,
        .map_free               = sock_hash_free,
@@ -1186,6 +1190,8 @@ const struct bpf_map_ops sock_hash_ops = {
        .map_lookup_elem_sys_only = sock_hash_lookup_sys,
        .map_release_uref       = sock_hash_release_progs,
        .map_check_btf          = map_check_no_btf,
+       .map_btf_name           = "bpf_shtab",
+       .map_btf_id             = &sock_hash_map_btf_id,
 };
 
 static struct sk_psock_progs *sock_map_progs(struct bpf_map *map)
index 1dc7208..8367adb 100644 (file)
@@ -254,6 +254,7 @@ void xsk_map_try_sock_delete(struct xsk_map *map, struct xdp_sock *xs,
        spin_unlock_bh(&map->lock);
 }
 
+static int xsk_map_btf_id;
 const struct bpf_map_ops xsk_map_ops = {
        .map_alloc = xsk_map_alloc,
        .map_free = xsk_map_free,
@@ -264,4 +265,6 @@ const struct bpf_map_ops xsk_map_ops = {
        .map_update_elem = xsk_map_update_elem,
        .map_delete_elem = xsk_map_delete_elem,
        .map_check_btf = map_check_no_btf,
+       .map_btf_name = "xsk_map",
+       .map_btf_id = &xsk_map_btf_id,
 };