Skip to content

Commit 26736eb

Browse files
Jakub Kicinskiborkmann
authored andcommitted
tools: libbpf: allow map reuse
More advanced applications may want to only replace programs without destroying associated maps. Allow libbpf users to achieve that. Instead of always creating all of the maps at load time, expose to users an API to reconstruct the map object from already existing map. The map parameters are read from the kernel and replace the parameters of the ELF map. libbpf does not restrict the map replacement, i.e. the reused map does not have to be compatible with the ELF map definition. We relay on the verifier for checking the compatibility between maps and programs. The ELF map definition is completely overwritten by the information read from the kernel, to make sure libbpf's view of map object corresponds to the actual map. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Acked-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 531b014 commit 26736eb

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎tools/lib/bpf/libbpf.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,53 @@ static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf)
10351035
return 0;
10361036
}
10371037

1038+
int bpf_map__reuse_fd(struct bpf_map *map, int fd)
1039+
{
1040+
struct bpf_map_info info = {};
1041+
__u32 len = sizeof(info);
1042+
int new_fd, err;
1043+
char *new_name;
1044+
1045+
err = bpf_obj_get_info_by_fd(fd, &info, &len);
1046+
if (err)
1047+
return err;
1048+
1049+
new_name = strdup(info.name);
1050+
if (!new_name)
1051+
return -errno;
1052+
1053+
new_fd = open("/", O_RDONLY | O_CLOEXEC);
1054+
if (new_fd < 0)
1055+
goto err_free_new_name;
1056+
1057+
new_fd = dup3(fd, new_fd, O_CLOEXEC);
1058+
if (new_fd < 0)
1059+
goto err_close_new_fd;
1060+
1061+
err = zclose(map->fd);
1062+
if (err)
1063+
goto err_close_new_fd;
1064+
free(map->name);
1065+
1066+
map->fd = new_fd;
1067+
map->name = new_name;
1068+
map->def.type = info.type;
1069+
map->def.key_size = info.key_size;
1070+
map->def.value_size = info.value_size;
1071+
map->def.max_entries = info.max_entries;
1072+
map->def.map_flags = info.map_flags;
1073+
map->btf_key_type_id = info.btf_key_type_id;
1074+
map->btf_value_type_id = info.btf_value_type_id;
1075+
1076+
return 0;
1077+
1078+
err_close_new_fd:
1079+
close(new_fd);
1080+
err_free_new_name:
1081+
free(new_name);
1082+
return -errno;
1083+
}
1084+
10381085
static int
10391086
bpf_object__create_maps(struct bpf_object *obj)
10401087
{
@@ -1047,6 +1094,12 @@ bpf_object__create_maps(struct bpf_object *obj)
10471094
struct bpf_map_def *def = &map->def;
10481095
int *pfd = &map->fd;
10491096

1097+
if (map->fd >= 0) {
1098+
pr_debug("skip map create (preset) %s: fd=%d\n",
1099+
map->name, map->fd);
1100+
continue;
1101+
}
1102+
10501103
create_attr.name = map->name;
10511104
create_attr.map_ifindex = map->map_ifindex;
10521105
create_attr.map_type = def->type;

‎tools/lib/bpf/libbpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
261261
int bpf_map__set_priv(struct bpf_map *map, void *priv,
262262
bpf_map_clear_priv_t clear_priv);
263263
void *bpf_map__priv(struct bpf_map *map);
264+
int bpf_map__reuse_fd(struct bpf_map *map, int fd);
264265
bool bpf_map__is_offload_neutral(struct bpf_map *map);
265266
void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
266267
int bpf_map__pin(struct bpf_map *map, const char *path);

0 commit comments

Comments
 (0)