On Mon, Nov 19, 2018 at 10:42:21PM -0800, Nikita V. Shirokov wrote: > 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 > > inner_map_fd is ignored by any other maps aside from (hash|array) of > maps > > Signed-off-by: Nikita V. Shirokov <tehn...@tehnerd.com> > Acked-by: Yonghong Song <y...@fb.com> > --- > tools/lib/bpf/libbpf.c | 11 ++++++++++- > tools/lib/bpf/libbpf.h | 2 ++ > 2 files changed, 12 insertions(+), 1 deletion(-) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index a01eb9584e52..7e130e0c8fc9 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -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; > @@ -653,8 +654,10 @@ bpf_object__init_maps(struct bpf_object *obj, int flags) > * 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++) { > obj->maps[i].fd = -1; > + obj->maps[i].inner_map_fd = -1; > + } > > /* > * Fill obj->maps using data in "maps" section. > @@ -1146,6 +1149,7 @@ 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; > + 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 +2566,11 @@ void bpf_map__set_ifindex(struct bpf_map *map, __u32 > ifindex) > map->map_ifindex = ifindex; > } > > +void bpf_map__add_inner_map_fd(struct bpf_map *map, int fd) > +{ > + map->inner_map_fd = fd;
I think the name bpf_map__set_inner_map_fd() would be more appropriate and it should check that map->def->type == map-in-map && map->inner_map_fd == -1 before assigning new one. Also the behavior of bpf_object__create_maps() is not great. If nothing is set the function will be passing create_attr.inner_map_fd == -1 to the kernel. For regular maps that field is sadly ignored by kernel. Only for map-in-map the value of -1 will be triggering map_create error. Imo bpf_object__create_maps() should be doing: if (create_attr.map_type == map-in-map && map->inner_map_fd >= 0) create_attr.inner_map_fd = map->inner_map_fd; // otherwise keep it zero inited