bpf: adding support for mapinmap in libbpf
Subsystems (6)
v4 cover bpf: adding support for mapinmap in libbpf
in this patch series i'm adding a helper for libbpf which would allow it to load map-in-map(BPF_MAP_TYPE_ARRAY_OF_MAPS and BPF_MAP_TYPE_HASH_OF_MAPS).
first patch contains new helper + explains proposed workflow second patch contains tests which also could be used as example of usage
v3->v4:
- renamed helper to set_inner_map_fd
- now we set this value only if it haven't been set before and only for (array|hash) of maps
v2->v3:
- fixing typo in patch description
- initializing inner_map_fd to -1 by default
v1->v2:
- addressing nits
- removing const identifier from fd in new helper
- starting to check return val for bpf_map_update_elem
Nikita V. Shirokov (2):
bpf: adding support for map in map in libbpf
bpf: adding tests for mapinmap helpber in libbpf
tools/lib/bpf/libbpf.c | 33 +++++++++--- tools/lib/bpf/libbpf.h | 2 + tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/test_mapinmap.c | 49 +++++++++++++++++ tools/testing/selftests/bpf/test_maps.c | 82 +++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_mapinmap.c
Signature · 1 line
2.15.1
PATCH v4 1/2 bpf: adding support for map in map in libbpf
idea is pretty simple. for specified map (pointed by struct bpf_map) we would provide descriptor of already loaded map, which is going to be used as a prototype for inner map. proposed workflow:
1) open bpf's object (bpf_object__open)
2) create bpf's map which is going to be used as a prototype
3) find (by name) map-in-map which you want to load and update w/ descriptor of inner map w/ a new helper from this patch
4) load bpf program w/ bpf_object__load
--- tools/lib/bpf/libbpf.c | 33 +++++++++++++++++++++++++++------ tools/lib/bpf/libbpf.h | 2 ++ 2 files changed, 29 insertions(+), 6 deletions(-)
tools/lib/bpf/libbpf.c +27 -6
@@ -163,6 +163,7 @@ struct bpf_map {char *name;size_t offset;int map_ifindex; +int inner_map_fd;struct bpf_map_def def;__u32 btf_key_type_id;__u32 btf_value_type_id;
@@ -585,6 +586,14 @@ static int compare_bpf_map(const void *_a, const void *_b)return a->offset - b->offset; } +static bool bpf_map_type__is_mapinmap (enum bpf_map_type type) +{ +if (type ==BPF_MAP_TYPE_ARRAY_OF_MAPS || + type ==BPF_MAP_TYPE_HASH_OF_MAPS ) +return true; +return false; +} +static int bpf_object__init_maps (struct bpf_object *obj,int flags) {
@@ -648,13 +657,15 @@ bpf_object__init_maps(struct bpf_object *obj, int flags) } obj->nr_maps = nr_maps; -/* -* fill all fd with -1 so won't close incorrect -* fd (fd=0 is stdin) when failure (zclose won't close -* negative fd)). -*/ -for (i =0 ; i < nr_maps; i++) +for (i =0 ; i < nr_maps; i++) { +/* +* fill all fd with -1 so won't close incorrect +* fd (fd=0 is stdin) when failure (zclose won't close +* negative fd)). +*/ obj->maps[i].fd =-1 ; + obj->maps[i].inner_map_fd =-1 ; + }/* * Fill obj->maps using data in "maps" section.
@@ -1146,6 +1157,9 @@ bpf_object__create_maps(struct bpf_object *obj) create_attr.btf_fd =0 ; create_attr.btf_key_type_id =0 ; create_attr.btf_value_type_id =0 ; +if (bpf_map_type__is_mapinmap (def->type) && + map->inner_map_fd >=0 ) + create_attr.inner_map_fd = map->inner_map_fd;if (obj->btf && !bpf_map_find_btf_info (map, obj->btf)) { create_attr.btf_fd =btf__fd (obj->btf);
@@ -2562,6 +2576,13 @@ void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) map->map_ifindex = ifindex; } +void bpf_map__set_inner_map_fd (struct bpf_map *map,int fd) +{ +if (bpf_map_type__is_mapinmap (map->def.type) && + map->inner_map_fd ==-1 ) + map->inner_map_fd = fd; +} +static struct bpf_map *__bpf_map__iter (struct bpf_map *m,struct bpf_object *obj,int i) {
tools/lib/bpf/libbpf.h +2 -0
@@ -293,6 +293,8 @@ LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path); +LIBBPF_API void bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); +LIBBPF_API long libbpf_get_error (const void *ptr);struct bpf_prog_load_attr {
Signature · 1 line
2.15.1
Re: [PATCH v4 bpf-next 1/2] bpf: adding support for map in map in libbpf
Quoted 29 lines · Nikita V. Shirokov
idea is pretty simple. for specified map (pointed by struct
bpf_map) we would provide descriptor of already loaded map, which is going to be used as a prototype for inner map. proposed workflow:
1) open bpf's object (bpf_object__open)
2) create bpf's map which is going to be used as a prototype
3) find (by name) map-in-map which you want to load and update w/ descriptor of inner map w/ a new helper from this patch
4) load bpf program w/bpf_object__loadSigned-off-by Nikita V. Shirokov <[email protected]>Acked-by Yonghong Song <[email protected]>--- tools/lib/bpf/libbpf.c | 33 +++++++++++++++++++++++++++------ tools/lib/bpf/libbpf.h | 2 ++ 2 files changed, 29 insertions(+), 6 deletions(-)tools/lib/bpf/libbpf.c +1
-0@@ -163,6 +163,7 @@ struct bpf_map {char *name;size_t offset;int map_ifindex; +int inner_map_fd;struct bpf_map_def def;__u32 btf_key_type_id;__u32 btf_value_type_id;
tools/lib/bpf/libbpf.c static int compare_bpf_map(const void *_a, const void *_b) · line 589
Quoted 4 lines
tools/lib/bpf/libbpf.c +1
-0@@ -585,6 +586,14 @@ static int compare_bpf_map(const void *_a, const void *_b)return a->offset - b->offset; } +static bool bpf_map_type__is_mapinmap (enum bpf_map_type type)
there is already public api bpf_create_map_in_map() Please use the existing naming convention.
Quoted 43 lines
tools/lib/bpf/libbpf.c +18
-6+{ +if (type ==BPF_MAP_TYPE_ARRAY_OF_MAPS || + type ==BPF_MAP_TYPE_HASH_OF_MAPS ) +return true; +return false; +} +static int bpf_object__init_maps (struct bpf_object *obj,int flags) {@@ -648,13 +657,15 @@ bpf_object__init_maps(struct bpf_object *obj, int flags) } obj->nr_maps = nr_maps; -/* -* fill all fd with -1 so won't close incorrect -* fd (fd=0 is stdin) when failure (zclose won't close -* negative fd)). -*/ -for (i =0 ; i < nr_maps; i++) +for (i =0 ; i < nr_maps; i++) { +/* +* fill all fd with -1 so won't close incorrect +* fd (fd=0 is stdin) when failure (zclose won't close +* negative fd)). +*/ obj->maps[i].fd =-1 ; + obj->maps[i].inner_map_fd =-1 ; + }/* * Fill obj->maps using data in "maps" section. @@ -1146,6 +1157,9 @@ bpf_object__create_maps(struct bpf_object *obj) create_attr.btf_fd =0 ; create_attr.btf_key_type_id =0 ; create_attr.btf_value_type_id =0 ; +if (bpf_map_type__is_mapinmap (def->type) && + map->inner_map_fd >=0 ) + create_attr.inner_map_fd = map->inner_map_fd;if (obj->btf && !bpf_map_find_btf_info (map, obj->btf)) { create_attr.btf_fd =btf__fd (obj->btf);
tools/lib/bpf/libbpf.c void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) · line 2583
Quoted 8 lines
tools/lib/bpf/libbpf.c +5
-0@@ -2562,6 +2576,13 @@ void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex) map->map_ifindex = ifindex; } +void bpf_map__set_inner_map_fd (struct bpf_map *map,int fd) +{ +if (bpf_map_type__is_mapinmap (map->def.type) && + map->inner_map_fd ==-1 ) + map->inner_map_fd = fd;
return an error?
Quoted 21 lines
tools/lib/bpf/libbpf.c +2
-0+} +static struct bpf_map *__bpf_map__iter (struct bpf_map *m,struct bpf_object *obj,int i) {tools/lib/bpf/libbpf.h +2
-0@@ -293,6 +293,8 @@ LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex); LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path); LIBBPF_API int bpf_map__unpin(struct bpf_map *map, const char *path); +LIBBPF_API void bpf_map__set_inner_map_fd(struct bpf_map *map, int fd); +LIBBPF_API long libbpf_get_error (const void *ptr);struct bpf_prog_load_attr {Signature · 1 line
2.15.1
PATCH v4 2/2 bpf: adding tests for mapinmap helpber in libbpf
In reply to Nikita V. Shirokovadding test/example of bpf_map__add_inner_map_fd usage
--- tools/testing/selftests/bpf/Makefile | 3 +- tools/testing/selftests/bpf/test_mapinmap.c | 49 +++++++++++++++++ tools/testing/selftests/bpf/test_maps.c | 82 +++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_mapinmap.c
tools/testing/selftests/bpf/Makefile +2 -1
@@ -38,7 +38,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \ get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \ test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \ - test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o + test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o \ + test_mapinmap.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/test_mapinmap.c new file +49 -0
49 lines not on this page · raw message
tools/testing/selftests/bpf/test_maps.c +82 -0
94 lines not on this page · raw message
Signature · 1 line
2.15.1