Add BTF-defined map-in-map support to libbpf
Subsystems (6)
v2 cover Add BTF-defined map-in-map support to libbpf
This patch set teaches libbpf how to declare and initialize ARRAY_OF_MAPS and HASH_OF_MAPS maps. See patch #3 for all the details.
Patch #1 refactors parsing BTF definition of map to re-use it cleanly for inner map definition parsing.
Patch #2 refactors map creation and destruction logic for reuse. It also fixes existing bug with not closing successfully created maps when bpf_object map creation overall fails.
Patch #3 adds support for an extension of BTF-defined map syntax, as well as parsing, recording, and use of relocations to allow declaratively initialize outer maps with references to inner maps.
Andrii Nakryiko (3):
libbpf: refactor BTF-defined map definition parsing logic
libbpf: refactor map creation logic and fix cleanup leak
libbpf: add BTF-defined map-in-map support
tools/lib/bpf/bpf_helpers.h | 1 + tools/lib/bpf/libbpf.c | 698 ++++++++++++------ .../selftests/bpf/prog_tests/btf_map_in_map.c | 49 ++ .../selftests/bpf/progs/test_btf_map_in_map.c | 76 ++ 4 files changed, 606 insertions(+), 218 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_map_in_map.c create mode 100644 tools/testing/selftests/bpf/progs/test_btf_map_in_map.c
Signature · 1 line
2.24.1
PATCH v2 1/3 libbpf: refactor BTF-defined map definition parsing logic
Factor out BTF map definition logic into stand-alone routine for easier reuse for map-in-map case.
--- tools/lib/bpf/libbpf.c | 195 ++++++++++++++++++++++------------------- 1 file changed, 103 insertions(+), 92 deletions(-)
tools/lib/bpf/libbpf.c +103 -92
@@ -1914,109 +1914,54 @@ static int build_map_pin_path(struct bpf_map *map, const char *path)return 0 ; } -static int bpf_object__init_user_btf_map (struct bpf_object *obj, -const struct btf_type *sec, -int var_idx,int sec_idx, -const Elf_Data *data,bool strict, -const char *pin_root_path) + +static int parse_btf_map_def (struct bpf_object *obj, +struct bpf_map *map, +const struct btf_type *def, +bool strict, +const char *pin_root_path) { -const struct btf_type *var, *def, *t; -const struct btf_var_secinfo *vi; -const struct btf_var *var_extra; +const struct btf_type *t;const struct btf_member *m; -const char *map_name; -struct bpf_map *map;int vlen, i; - vi =btf_var_secinfos (sec) + var_idx; - var =btf__type_by_id (obj->btf, vi->type); - var_extra =btf_var (var); - map_name =btf__name_by_offset (obj->btf, var->name_off); - vlen =btf_vlen (var); - -if (map_name ==NULL || map_name[0 ] =='\0' ) { -pr_warn ("map #%d: empty name.\n" , var_idx); -return -EINVAL ; - } -if ((__u64 )vi->offset + vi->size > data->d_size) { -pr_warn ("map '%s' BTF data is corrupted.\n" , map_name); -return -EINVAL ; - } -if (!btf_is_var (var)) { -pr_warn ("map '%s': unexpected var kind %u.\n" , - map_name,btf_kind (var)); -return -EINVAL ; - } -if (var_extra->linkage !=BTF_VAR_GLOBAL_ALLOCATED && - var_extra->linkage !=BTF_VAR_STATIC ) { -pr_warn ("map '%s': unsupported var linkage %u.\n" , - map_name, var_extra->linkage); -return -EOPNOTSUPP ; - } - - def =skip_mods_and_typedefs (obj->btf, var->type,NULL ); -if (!btf_is_struct (def)) { -pr_warn ("map '%s': unexpected def kind %u.\n" , - map_name,btf_kind (var)); -return -EINVAL ; - } -if (def->size > vi->size) { -pr_warn ("map '%s': invalid def size.\n" , map_name); -return -EINVAL ; - } - - map =bpf_object__add_map (obj); -if (IS_ERR (map)) -return PTR_ERR (map); - map->name =strdup (map_name); -if (!map->name) { -pr_warn ("map '%s': failed to alloc map name.\n" , map_name); -return -ENOMEM ; - } - map->libbpf_type =LIBBPF_MAP_UNSPEC ; - map->def.type =BPF_MAP_TYPE_UNSPEC ; - map->sec_idx = sec_idx; - map->sec_offset = vi->offset; -pr_debug ("map '%s': at sec_idx %d, offset %zu.\n" , - map_name, map->sec_idx, map->sec_offset); - vlen =btf_vlen (def); m =btf_members (def);for (i =0 ; i < vlen; i++, m++) {const char *name =btf__name_by_offset (obj->btf, m->name_off);if (!name) { -pr_warn ("map '%s': invalid field #%d.\n" , map_name, i); +pr_warn ("map '%s': invalid field #%d.\n" , map->name, i);return -EINVAL ; }if (strcmp (name,"type" ) ==0 ) { -if (!get_map_field_int (map_name, obj->btf, m, +if (!get_map_field_int (map->name, obj->btf, m, &map->def.type))return -EINVAL ;pr_debug ("map '%s': found type = %u.\n" , - map_name, map->def.type); + map->name, map->def.type); }else if (strcmp (name,"max_entries" ) ==0 ) { -if (!get_map_field_int (map_name, obj->btf, m, +if (!get_map_field_int (map->name, obj->btf, m, &map->def.max_entries))return -EINVAL ;pr_debug ("map '%s': found max_entries = %u.\n" , - map_name, map->def.max_entries); + map->name, map->def.max_entries); }else if (strcmp (name,"map_flags" ) ==0 ) { -if (!get_map_field_int (map_name, obj->btf, m, +if (!get_map_field_int (map->name, obj->btf, m, &map->def.map_flags))return -EINVAL ;pr_debug ("map '%s': found map_flags = %u.\n" , - map_name, map->def.map_flags); + map->name, map->def.map_flags); }else if (strcmp (name,"key_size" ) ==0 ) {__u32 sz; -if (!get_map_field_int (map_name, obj->btf, m, &sz)) +if (!get_map_field_int (map->name, obj->btf, m, &sz))return -EINVAL ;pr_debug ("map '%s': found key_size = %u.\n" , - map_name, sz); + map->name, sz);if (map->def.key_size && map->def.key_size != sz) {pr_warn ("map '%s': conflicting key size %u != %u.\n" , - map_name, map->def.key_size, sz); + map->name, map->def.key_size, sz);return -EINVAL ; } map->def.key_size = sz;
@@ -2026,25 +1971,25 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, t =btf__type_by_id (obj->btf, m->type);if (!t) {pr_warn ("map '%s': key type [%d] not found.\n" , - map_name, m->type); + map->name, m->type);return -EINVAL ; }if (!btf_is_ptr (t)) {pr_warn ("map '%s': key spec is not PTR: %u.\n" , - map_name,btf_kind (t)); + map->name,btf_kind (t));return -EINVAL ; } sz =btf__resolve_size (obj->btf, t->type);if (sz <0 ) {pr_warn ("map '%s': can't determine key size for type [%u]: %zd.\n" , - map_name, t->type, (ssize_t )sz); + map->name, t->type, (ssize_t )sz);return sz; }pr_debug ("map '%s': found key [%u], sz = %zd.\n" , - map_name, t->type, (ssize_t )sz); + map->name, t->type, (ssize_t )sz);if (map->def.key_size && map->def.key_size != sz) {pr_warn ("map '%s': conflicting key size %u != %zd.\n" , - map_name, map->def.key_size, (ssize_t )sz); + map->name, map->def.key_size, (ssize_t )sz);return -EINVAL ; } map->def.key_size = sz;
@@ -2052,13 +1997,13 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, }else if (strcmp (name,"value_size" ) ==0 ) {__u32 sz; -if (!get_map_field_int (map_name, obj->btf, m, &sz)) +if (!get_map_field_int (map->name, obj->btf, m, &sz))return -EINVAL ;pr_debug ("map '%s': found value_size = %u.\n" , - map_name, sz); + map->name, sz);if (map->def.value_size && map->def.value_size != sz) {pr_warn ("map '%s': conflicting value size %u != %u.\n" , - map_name, map->def.value_size, sz); + map->name, map->def.value_size, sz);return -EINVAL ; } map->def.value_size = sz;
@@ -2068,25 +2013,25 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj, t =btf__type_by_id (obj->btf, m->type);if (!t) {pr_warn ("map '%s': value type [%d] not found.\n" , - map_name, m->type); + map->name, m->type);return -EINVAL ; }if (!btf_is_ptr (t)) {pr_warn ("map '%s': value spec is not PTR: %u.\n" , - map_name,btf_kind (t)); + map->name,btf_kind (t));return -EINVAL ; } sz =btf__resolve_size (obj->btf, t->type);if (sz <0 ) {pr_warn ("map '%s': can't determine value size for type [%u]: %zd.\n" , - map_name, t->type, (ssize_t )sz); + map->name, t->type, (ssize_t )sz);return sz; }pr_debug ("map '%s': found value [%u], sz = %zd.\n" , - map_name, t->type, (ssize_t )sz); + map->name, t->type, (ssize_t )sz);if (map->def.value_size && map->def.value_size != sz) {pr_warn ("map '%s': conflicting value size %u != %zd.\n" , - map_name, map->def.value_size, (ssize_t )sz); + map->name, map->def.value_size, (ssize_t )sz);return -EINVAL ; } map->def.value_size = sz;
@@ -2095,44 +2040,110 @@ static int bpf_object__init_user_btf_map(struct bpf_object *obj,__u32 val;int err; -if (!get_map_field_int (map_name, obj->btf, m, &val)) +if (!get_map_field_int (map->name, obj->btf, m, &val))return -EINVAL ;pr_debug ("map '%s': found pinning = %u.\n" , - map_name, val); + map->name, val);if (val !=LIBBPF_PIN_NONE && val !=LIBBPF_PIN_BY_NAME ) {pr_warn ("map '%s': invalid pinning value %u.\n" , - map_name, val); + map->name, val);return -EINVAL ; }if (val ==LIBBPF_PIN_BY_NAME ) { err =build_map_pin_path (map, pin_root_path);if (err) {pr_warn ("map '%s': couldn't build pin path.\n" , - map_name); + map->name);return err; } } }else {if (strict) {pr_warn ("map '%s': unknown field '%s'.\n" , - map_name, name); + map->name, name);return -ENOTSUP ; }pr_debug ("map '%s': ignoring unknown field '%s'.\n" , - map_name, name); + map->name, name); } }if (map->def.type ==BPF_MAP_TYPE_UNSPEC ) { -pr_warn ("map '%s': map type isn't specified.\n" , map_name); +pr_warn ("map '%s': map type isn't specified.\n" , map->name);return -EINVAL ; }return 0 ; } +static int bpf_object__init_user_btf_map (struct bpf_object *obj, +const struct btf_type *sec, +int var_idx,int sec_idx, +const Elf_Data *data,bool strict, +const char *pin_root_path) +{ +const struct btf_type *var, *def; +const struct btf_var_secinfo *vi; +const struct btf_var *var_extra; +const char *map_name; +struct bpf_map *map; + + vi =btf_var_secinfos (sec) + var_idx; + var =btf__type_by_id (obj->btf, vi->type); + var_extra =btf_var (var); + map_name =btf__name_by_offset (obj->btf, var->name_off); + +if (map_name ==NULL || map_name[0 ] =='\0' ) { +pr_warn ("map #%d: empty name.\n" , var_idx); +return -EINVAL ; + } +if ((__u64 )vi->offset + vi->size > data->d_size) { +pr_warn ("map '%s' BTF data is corrupted.\n" , map_name); +return -EINVAL ; + } +if (!btf_is_var (var)) { +pr_warn ("map '%s': unexpected var kind %u.\n" , + map_name,btf_kind (var)); +return -EINVAL ; + } +if (var_extra->linkage !=BTF_VAR_GLOBAL_ALLOCATED && + var_extra->linkage !=BTF_VAR_STATIC ) { +pr_warn ("map '%s': unsupported var linkage %u.\n" , + map_name, var_extra->linkage); +return -EOPNOTSUPP ; + } + + def =skip_mods_and_typedefs (obj->btf, var->type,NULL ); +if (!btf_is_struct (def)) { +pr_warn ("map '%s': unexpected def kind %u.\n" , + map_name,btf_kind (var)); +return -EINVAL ; + } +if (def->size > vi->size) { +pr_warn ("map '%s': invalid def size.\n" , map_name); +return -EINVAL ; + } + + map =bpf_object__add_map (obj); +if (IS_ERR (map)) +return PTR_ERR (map); + map->name =strdup (map_name); +if (!map->name) { +pr_warn ("map '%s': failed to alloc map name.\n" , map_name); +return -ENOMEM ; + } + map->libbpf_type =LIBBPF_MAP_UNSPEC ; + map->def.type =BPF_MAP_TYPE_UNSPEC ; + map->sec_idx = sec_idx; + map->sec_offset = vi->offset; +pr_debug ("map '%s': at sec_idx %d, offset %zu.\n" , + map_name, map->sec_idx, map->sec_offset); + +return parse_btf_map_def (obj, map, def, strict, pin_root_path); +} +static int bpf_object__init_user_btf_maps (struct bpf_object *obj,bool strict,const char *pin_root_path) {
Signature · 1 line
2.24.1
PATCH v2 2/3 libbpf: refactor map creation logic and fix cleanup leak
In reply to Andrii NakryikoFactor out map creation and destruction logic to simplify code and especially error handling. Also fix map FD leak in case of partially successful map creation during bpf_object load operation.
--- tools/lib/bpf/libbpf.c | 226 ++++++++++++++++++++++------------------- 1 file changed, 121 insertions(+), 105 deletions(-)
tools/lib/bpf/libbpf.c +121 -105
@@ -3493,107 +3493,111 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)return 0 ; } +static void bpf_map__destroy (struct bpf_map *map); + +static int bpf_object__create_map (struct bpf_object *obj,struct bpf_map *map) +{ +struct bpf_create_map_attr create_attr; +struct bpf_map_def *def = &map->def; + +memset (&create_attr,0 ,sizeof (create_attr)); + +if (obj->caps.name) + create_attr.name = map->name; + create_attr.map_ifindex = map->map_ifindex; + create_attr.map_type = def->type; + create_attr.map_flags = def->map_flags; + create_attr.key_size = def->key_size; + create_attr.value_size = def->value_size; + +if (def->type ==BPF_MAP_TYPE_PERF_EVENT_ARRAY && !def->max_entries) { +int nr_cpus; + + nr_cpus =libbpf_num_possible_cpus (); +if (nr_cpus <0 ) { +pr_warn ("map '%s': failed to determine number of system CPUs: %d\n" , + map->name, nr_cpus); +return nr_cpus; + } +pr_debug ("map '%s': setting size to %d\n" , map->name, nr_cpus); + create_attr.max_entries = nr_cpus; + }else { + create_attr.max_entries = def->max_entries; + } + +if (bpf_map__is_struct_ops (map)) + create_attr.btf_vmlinux_value_type_id = + map->btf_vmlinux_value_type_id; + + create_attr.btf_fd =0 ; + create_attr.btf_key_type_id =0 ; + create_attr.btf_value_type_id =0 ; +if (obj->btf && !bpf_map_find_btf_info (obj, map)) { + create_attr.btf_fd =btf__fd (obj->btf); + create_attr.btf_key_type_id = map->btf_key_type_id; + create_attr.btf_value_type_id = map->btf_value_type_id; + } + + map->fd =bpf_create_map_xattr (&create_attr); +if (map->fd <0 && (create_attr.btf_key_type_id || + create_attr.btf_value_type_id)) { +char *cp, errmsg[STRERR_BUFSIZE ]; +int err = -errno; + + cp =libbpf_strerror_r (err, errmsg,sizeof (errmsg)); +pr_warn ("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n" , + map->name, cp, err); + create_attr.btf_fd =0 ; + create_attr.btf_key_type_id =0 ; + create_attr.btf_value_type_id =0 ; + map->btf_key_type_id =0 ; + map->btf_value_type_id =0 ; + map->fd =bpf_create_map_xattr (&create_attr); + } + +if (map->fd <0 ) +return -errno; + +return 0 ; +} +static int bpf_object__create_maps (struct bpf_object *obj) { -struct bpf_create_map_attr create_attr = {}; -int nr_cpus =0 ; -unsigned int i; +struct bpf_map *map; +char *cp, errmsg[STRERR_BUFSIZE ]; +unsigned int i, j;int err;for (i =0 ; i < obj->nr_maps; i++) { -struct bpf_map *map = &obj->maps[i]; -struct bpf_map_def *def = &map->def; -char *cp, errmsg[STRERR_BUFSIZE ]; -int *pfd = &map->fd; + map = &obj->maps[i];if (map->pin_path) { err =bpf_object__reuse_map (map);if (err) { -pr_warn ("error reusing pinned map %s\n" , +pr_warn ("map '%s': error reusing pinned map\n" , map->name); -return err; + goto err_out; } }if (map->fd >=0 ) { -pr_debug ("skip map create (preset) %s: fd=%d\n" , +pr_debug ("map '%s': skipping creation (preset fd=%d)\n" , map->name, map->fd);continue ; } -if (obj->caps.name) - create_attr.name = map->name; - create_attr.map_ifindex = map->map_ifindex; - create_attr.map_type = def->type; - create_attr.map_flags = def->map_flags; - create_attr.key_size = def->key_size; - create_attr.value_size = def->value_size; -if (def->type ==BPF_MAP_TYPE_PERF_EVENT_ARRAY && - !def->max_entries) { -if (!nr_cpus) - nr_cpus =libbpf_num_possible_cpus (); -if (nr_cpus <0 ) { -pr_warn ("failed to determine number of system CPUs: %d\n" , - nr_cpus); - err = nr_cpus; - goto err_out; - } -pr_debug ("map '%s': setting size to %d\n" , - map->name, nr_cpus); - create_attr.max_entries = nr_cpus; - }else { - create_attr.max_entries = def->max_entries; - } - create_attr.btf_fd =0 ; - create_attr.btf_key_type_id =0 ; - create_attr.btf_value_type_id =0 ; -if (bpf_map_type__is_map_in_map (def->type) && - map->inner_map_fd >=0 ) - create_attr.inner_map_fd = map->inner_map_fd; -if (bpf_map__is_struct_ops (map)) - create_attr.btf_vmlinux_value_type_id = - map->btf_vmlinux_value_type_id; - -if (obj->btf && !bpf_map_find_btf_info (obj, map)) { - create_attr.btf_fd =btf__fd (obj->btf); - create_attr.btf_key_type_id = map->btf_key_type_id; - create_attr.btf_value_type_id = map->btf_value_type_id; - } - - *pfd =bpf_create_map_xattr (&create_attr); -if (*pfd <0 && (create_attr.btf_key_type_id || - create_attr.btf_value_type_id)) { - err = -errno; - cp =libbpf_strerror_r (err, errmsg,sizeof (errmsg)); -pr_warn ("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n" , - map->name, cp, err); - create_attr.btf_fd =0 ; - create_attr.btf_key_type_id =0 ; - create_attr.btf_value_type_id =0 ; - map->btf_key_type_id =0 ; - map->btf_value_type_id =0 ; - *pfd =bpf_create_map_xattr (&create_attr); - } - -if (*pfd <0 ) { -size_t j; + err =bpf_object__create_map (obj, map); +if (err) + goto err_out; - err = -errno; -err_out: - cp =libbpf_strerror_r (err, errmsg,sizeof (errmsg)); -pr_warn ("failed to create map (name: '%s'): %s(%d)\n" , - map->name, cp, err); -pr_perm_msg (err); -for (j =0 ; j < i; j++) -zclose (obj->maps[j].fd); -return err; - } +pr_debug ("map '%s': created successfully, fd=%d\n" , map->name, + map->fd);if (bpf_map__is_internal (map)) { err =bpf_object__populate_internal_map (obj, map);if (err <0 ) { -zclose (*pfd); +zclose (map->fd); goto err_out; } }
@@ -3601,16 +3605,23 @@ bpf_object__create_maps(struct bpf_object *obj)if (map->pin_path && !map->pinned) { err =bpf_map__pin (map,NULL );if (err) { -pr_warn ("failed to auto-pin map name '%s' at '%s'\n" , - map->name, map->pin_path); -return err; +pr_warn ("map '%s': failed to auto-pin at '%s': %d\n" , + map->name, map->pin_path, err); +zclose (map->fd); + goto err_out; } } - -pr_debug ("created map %s: fd=%d\n" , map->name, *pfd); }return 0 ; + +err_out: + cp =libbpf_strerror_r (err, errmsg,sizeof (errmsg)); +pr_warn ("map '%s': failed to create: %s(%d)\n" , map->name, cp, err); +pr_perm_msg (err); +for (j =0 ; j < i; j++) +zclose (obj->maps[j].fd); +return err; } static int
@@ -5966,6 +5977,32 @@ int bpf_object__pin(struct bpf_object *obj, const char *path)return 0 ; } +static void bpf_map__destroy (struct bpf_map *map) +{ +if (map->clear_priv) + map->clear_priv (map, map->priv); + map->priv =NULL ; + map->clear_priv =NULL ; + +if (map->mmaped) { +munmap (map->mmaped,bpf_map_mmap_sz (map)); + map->mmaped =NULL ; + } + +if (map->st_ops) { +zfree (&map->st_ops->data); +zfree (&map->st_ops->progs); +zfree (&map->st_ops->kern_func_off); +zfree (&map->st_ops); + } + +zfree (&map->name); +zfree (&map->pin_path); + +if (map->fd >=0 ) +zclose (map->fd); +} +void bpf_object__close (struct bpf_object *obj) {size_t i;
@@ -5981,29 +6018,8 @@ void bpf_object__close(struct bpf_object *obj)btf__free (obj->btf);btf_ext__free (obj->btf_ext); -for (i =0 ; i < obj->nr_maps; i++) { -struct bpf_map *map = &obj->maps[i]; - -if (map->clear_priv) - map->clear_priv (map, map->priv); - map->priv =NULL ; - map->clear_priv =NULL ; - -if (map->mmaped) { -munmap (map->mmaped,bpf_map_mmap_sz (map)); - map->mmaped =NULL ; - } - -if (map->st_ops) { -zfree (&map->st_ops->data); -zfree (&map->st_ops->progs); -zfree (&map->st_ops->kern_func_off); -zfree (&map->st_ops); - } - -zfree (&map->name); -zfree (&map->pin_path); - } +for (i =0 ; i < obj->nr_maps; i++) +bpf_map__destroy (&obj->maps[i]);zfree (&obj->kconfig);zfree (&obj->externs);
Signature · 1 line
2.24.1
Re: [PATCH v2 bpf-next 2/3] libbpf: refactor map creation logic and fix cleanup leak
Quoted 7 lines · Andrii Nakryiko
Factor out map creation and destruction logic to simplify code and especially error handling. Also fix map FD leak in case of partially successful map creation during
bpf_objectload operation.Cc Toke Høiland-Jørgensen <[email protected]>Fixes 57a00f41644f ("libbpf: Add auto-pinning of maps when loading BPF objects")Signed-off-by Andrii Nakryiko <[email protected]>
Good catch on the fd leak!
PATCH v2 3/3 libbpf: add BTF-defined map-in-map support
In reply to Andrii NakryikoAs discussed at LPC 2019 ([0]), this patch brings (a quite belated) support for declarative BTF-defined map-in-map support in libbpf. It allows to define ARRAY_OF_MAPS and HASH_OF_MAPS BPF maps without any user-space initialization code involved.
Additionally, it allows to initialize outer map's slots with references to respective inner maps at load time, also completely declaratively.
Despite a weak type system of C, the way BTF-defined map-in-map definition works, it's actually quite hard to accidentally initialize outer map with incompatible inner maps. This being C, of course, it's still possible, but even that would be caught at load time and error returned with helpful debug log pointing exactly to the slot that failed to be initialized.
As an example, here's a rather advanced HASH_OF_MAPS declaration and initialization example, filling slots #0 and #4 with two inner maps:
#include <bpf/bpf_helpers.h>
struct inner_map {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, int);
__type(value, int);
} inner_map1 SEC(".maps"),
inner_map2 SEC(".maps");
struct outer_hash {
__uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
__uint(max_entries, 5);
__uint(key_size, sizeof(int));
__inner(values, struct inner_map);
} outer_hash SEC(".maps") = {
.values = {
[0] = &inner_map2,
[4] = &inner_map1,
},
};Here's the relevant part of libbpf debug log showing pretty clearly of what's going on with map-in-map initialization:
libbpf: .maps relo #0: for 6 value 0 rel.r_offset 96 name 260 ('inner_map1')
libbpf: .maps relo #0: map 'outer_arr' slot [0] points to map 'inner_map1'
libbpf: .maps relo #1: for 7 value 32 rel.r_offset 112 name 249 ('inner_map2')
libbpf: .maps relo #1: map 'outer_arr' slot [2] points to map 'inner_map2'
libbpf: .maps relo #2: for 7 value 32 rel.r_offset 144 name 249 ('inner_map2')
libbpf: .maps relo #2: map 'outer_hash' slot [0] points to map 'inner_map2'
libbpf: .maps relo #3: for 6 value 0 rel.r_offset 176 name 260 ('inner_map1')
libbpf: .maps relo #3: map 'outer_hash' slot [4] points to map 'inner_map1'
libbpf: map 'inner_map1': created successfully, fd=4
libbpf: map 'inner_map2': created successfully, fd=5
libbpf: map 'outer_hash': created successfully, fd=7
libbpf: map 'outer_hash': slot [0] set to map 'inner_map2' fd=5
libbpf: map 'outer_hash': slot [4] set to map 'inner_map1' fd=4
Notice from the log above that fd=6 (not logged explicitly) is used for inner "prototype" map, necessary for creation of outer map. It is destroyed immediately after outer map is created.
See also included selftest with some extra comments explaining extra details of usage. Additionally, similar initialization syntax and libbpf functionality can be used to do initialization of BPF_PROG_ARRAY with references to BPF sub-programs. This can be done in follow up patches, if there will be a demand for this.
[0] https://linuxplumbersconf.org/event/4/contributions/448/
--- tools/lib/bpf/bpf_helpers.h | 1 + tools/lib/bpf/libbpf.c | 281 ++++++++++++++++-- .../selftests/bpf/prog_tests/btf_map_in_map.c | 49 +++ .../selftests/bpf/progs/test_btf_map_in_map.c | 76 +++++ 4 files changed, 384 insertions(+), 23 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_map_in_map.c create mode 100644 tools/testing/selftests/bpf/progs/test_btf_map_in_map.c
tools/lib/bpf/bpf_helpers.h +1 -0
@@ -12,6 +12,7 @@#define __uint (name, val) int (*name)[val]#define __type (name, val) typeof(val) *name +#define __inner (name, val) typeof(val) *name[]/* Helper macro to print out debug messages */ #define bpf_printk (fmt, ...) \
tools/lib/bpf/libbpf.c +258 -23
379 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/btf_map_in_map.c new file +49 -0
49 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_btf_map_in_map.c new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
2.24.1
Re: [PATCH v2 bpf-next 3/3] libbpf: add BTF-defined map-in-map support
Quoted 30 lines · Andrii Nakryiko
As discussed at LPC 2019 ([0]), this patch brings (a quite belated) support for declarative BTF-defined map-in-map support in libbpf. It allows to define
ARRAY_OF_MAPSandHASH_OF_MAPSBPF maps without any user-space initialization code involved.Additionally, it allows to initialize outer map's slots with references to respective inner maps at load time, also completely declaratively.
Despite a weak type system of C, the way BTF-defined map-in-map definition works, it's actually quite hard to accidentally initialize outer map with incompatible inner maps. This being C, of course, it's still possible, but even that would be caught at load time and error returned with helpful debug log pointing exactly to the slot that failed to be initialized.
As an example, here's a rather advanced
HASH_OF_MAPSdeclaration and initialization example, filling slots #0 and #4 with two inner maps:#include <bpf/bpf_helpers.h> struct inner_map { __uint(type, BPF_MAP_TYPE_ARRAY); __uint(max_entries, 1); __type(key, int); __type(value, int); } inner_map1 SEC(".maps"), inner_map2 SEC(".maps"); struct outer_hash { __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS); __uint(max_entries, 5);
Quoted 8 lines
__uint(key_size, sizeof(int));
__inner(values, struct inner_map);
} outer_hash SEC(".maps") = {
.values = {
[0] = &inner_map2,
[4] = &inner_map1,
},
};I like the syntax (well, to the extent you can 'like' C syntax and its esoteric (ab)uses), and am only mildly horrified at what it takes to achieve it ;)
Re: [PATCH v2 bpf-next 3/3] libbpf: add BTF-defined map-in-map support
In reply to Andrii NakryikoQuoted 9 lines · Andrii Nakryiko
tools/lib/bpf/bpf_helpers.h +1
-0@@ -12,6 +12,7 @@#define __uint (name, val) int (*name)[val]#define __type (name, val) typeof(val) *name +#define __inner (name, val) typeof(val) *name[]
...
Quoted 19 lines
+++
b/tools/testing/selftests/bpf/progs/test_btf_map_in_map.ctools/lib/bpf/bpf_helpers.h +17
-0@@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2020 Facebook */ +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +struct inner_map { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, int); +} inner_map1 SEC(".maps"), + inner_map2 SEC(".maps"); + +struct outer_arr { + __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS); + __uint(max_entries, 3); + __uint(key_size, sizeof(int));
tools/lib/bpf/bpf_helpers.h line 27
Quoted 10 lines
tools/lib/bpf/bpf_helpers.h +10
-0+__uint (value_size,sizeof (int )); +/* it's possible to use anonymous struct as inner map definition here */ + __inner(values, struct { +__uint (type,BPF_MAP_TYPE_ARRAY ); +/* changing max_entries to 2 will fail during load +* due to incompatibility with inner_map definition */ +__uint (max_entries,1 ); +__type (key, int); +__type (value, int); + });
How about renaming it s/__inner/__array/ ?
Because that's what it defines from BTF pov.
Re: [PATCH v2 bpf-next 3/3] libbpf: add BTF-defined map-in-map support
Quoted 36 lines · Alexei Starovoitov
Quoted 9 lines · Andrii Nakryiko
tools/lib/bpf/bpf_helpers.h +1
-0@@ -12,6 +12,7 @@#define __uint (name, val) int (*name)[val]#define __type (name, val) typeof(val) *name +#define __inner (name, val) typeof(val) *name[]...
Quoted 24 lines
+++
b/tools/testing/selftests/bpf/progs/test_btf_map_in_map.ctools/lib/bpf/bpf_helpers.h +22
-0@@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2020 Facebook */ +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +struct inner_map { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, int); +} inner_map1 SEC(".maps"), + inner_map2 SEC(".maps"); + +struct outer_arr { + __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS); + __uint(max_entries, 3); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); +/* it's possible to use anonymous struct as inner map definition here */ + __inner(values, struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + /* changing max_entries to 2 will fail during load
Quoted 8 lines
tools/lib/bpf/bpf_helpers.h line 27
Quoted 5 lines
tools/lib/bpf/bpf_helpers.h +5
-0+* due to incompatibility with inner_map definition */ +__uint (max_entries,1 ); +__type (key, int); +__type (value, int); + });How about renaming it s/__inner/__array/ ?
Because that's what it defines from BTF pov.
Sounds good, will update.