Skip to content

Commit

Permalink
libbpf: Auto-attach struct_ops BPF maps in BPF skeleton
Browse files Browse the repository at this point in the history
Similarly to `bpf_program`, support `bpf_map` automatic attachment in
`bpf_object__attach_skeleton`. Currently only struct_ops maps could be
attached.

On bpftool side, code-generate links in skeleton struct for struct_ops maps.
Similarly to `bpf_program_skeleton`, set links in `bpf_map_skeleton`.

On libbpf side, extend `bpf_map` with new `autoattach` field to support
enabling or disabling autoattach functionality, introducing
getter/setter for this field.

`bpf_object__(attach|detach)_skeleton` is extended with
attaching/detaching struct_ops maps logic.

Signed-off-by: Mykyta Yatsenko <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
mykyta5 authored and qmonnet committed Jun 27, 2024
1 parent 9204347 commit 5f63dbb
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
}

static void
codegen_maps_skeleton(struct bpf_object *obj, size_t map_cnt, bool mmaped)
codegen_maps_skeleton(struct bpf_object *obj, size_t map_cnt, bool mmaped, bool populate_links)
{
struct bpf_map *map;
char ident[256];
Expand Down Expand Up @@ -888,6 +888,14 @@ codegen_maps_skeleton(struct bpf_object *obj, size_t map_cnt, bool mmaped)
printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n",
i, ident);
}

if (populate_links && bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS) {
codegen("\
\n\
s->maps[%zu].link = &obj->links.%s;\n\
",
i, ident);
}
i++;
}
}
Expand Down Expand Up @@ -1141,7 +1149,7 @@ static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj)
static int do_skeleton(int argc, char **argv)
{
char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
size_t map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz;
size_t map_cnt = 0, prog_cnt = 0, attach_map_cnt = 0, file_sz, mmap_sz;
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data;
struct bpf_object *obj = NULL;
Expand Down Expand Up @@ -1225,6 +1233,10 @@ static int do_skeleton(int argc, char **argv)
bpf_map__name(map));
continue;
}

if (bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS)
attach_map_cnt++;

map_cnt++;
}
bpf_object__for_each_program(prog, obj) {
Expand Down Expand Up @@ -1297,6 +1309,9 @@ static int do_skeleton(int argc, char **argv)
bpf_program__name(prog));
}
printf("\t} progs;\n");
}

if (prog_cnt + attach_map_cnt) {
printf("\tstruct {\n");
bpf_object__for_each_program(prog, obj) {
if (use_loader)
Expand All @@ -1306,6 +1321,19 @@ static int do_skeleton(int argc, char **argv)
printf("\t\tstruct bpf_link *%s;\n",
bpf_program__name(prog));
}

bpf_object__for_each_map(map, obj) {
if (!get_map_ident(map, ident, sizeof(ident)))
continue;
if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
continue;

if (use_loader)
printf("t\tint %s_fd;\n", ident);
else
printf("\t\tstruct bpf_link *%s;\n", ident);
}

printf("\t} links;\n");
}

Expand Down Expand Up @@ -1448,7 +1476,7 @@ static int do_skeleton(int argc, char **argv)
obj_name
);

codegen_maps_skeleton(obj, map_cnt, true /*mmaped*/);
codegen_maps_skeleton(obj, map_cnt, true /*mmaped*/, true /*links*/);
codegen_progs_skeleton(obj, prog_cnt, true /*populate_links*/);

codegen("\
Expand Down Expand Up @@ -1786,7 +1814,7 @@ static int do_subskeleton(int argc, char **argv)
}
}

codegen_maps_skeleton(obj, map_cnt, false /*mmaped*/);
codegen_maps_skeleton(obj, map_cnt, false /*mmaped*/, false /*links*/);
codegen_progs_skeleton(obj, prog_cnt, false /*links*/);

codegen("\
Expand Down

0 comments on commit 5f63dbb

Please sign in to comment.