Skip to contentSkip to conversation

libbpf: auto-attach skeletons struct_ops

PATCH Applied Mykyta Yatsenko [email protected] 3 participants 3 messages Jun 5, 2024 – Jun 6, 2024

Subsystems (3)
lore
Share & tools
lore

Mykyta Yatsenko [email protected]

author
#0 lore raw

PATCH 1/1 libbpf: auto-attach skeletons struct_ops

From: Mykyta Yatsenko <[email protected]>

Similarly to bpf_program, support bpf_map automatic attachment in bpf_object__attach_skeleton. Currently only struct_ops maps could be attached.

Bpftool
Code-generate links in skeleton struct for struct_ops maps. Similarly to bpf_program_skeleton, set links in bpf_map_skeleton.

Libbpf
Extending bpf_map with new autoattach field to support enabling or disabling autoattach functionality, introducing getter/setter for this field.
Extending bpf_object__(attach|detach)_skeleton with attaching/detaching struct_ops maps.

---
 tools/bpf/bpftool/gen.c  | 36 +++++++++++++++++++---
 tools/lib/bpf/libbpf.c   | 65 ++++++++++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf.h   | 20 +++++++++++++
 tools/lib/bpf/libbpf.map |  2 ++
 4 files changed, 116 insertions(+), 7 deletions(-)
tools/bpf/bpftool/gen.c +32 -4
index b3979ddc0189..a8d4db0d8044 100644
@@ -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];
@@ -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++;
 	}
 }
@@ -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;
@@ -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) {
@@ -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)
@@ -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");
 	}
 
@@ -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("\
@@ -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("\
tools/lib/bpf/libbpf.c +62 -3

108 lines not on this page · raw message

tools/lib/bpf/libbpf.h +20 -0
index 26e4e35528c5..641d6ec876e2 100644
@@ -978,6 +978,25 @@ bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *map);
 LIBBPF_API int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate);
 LIBBPF_API bool bpf_map__autocreate(const struct bpf_map *map);
 
+/**
+ * @brief **bpf_map__set_autoattach()** sets whether libbpf has to auto-attach
+ * map during BPF skeleton attach phase.
+ * @param map the BPF map instance
+ * @param autoattach whether to attach map during BPF skeleton attach phase
+ * @return 0 on success; negative error code, otherwise
+ *
+ */
+LIBBPF_API int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach);
+
+/**
+ * @brief **bpf_map__autoattach()** returns whether BPF map is configured to
+ * auto-attach during BPF skeleton attach phase.
+ * @param map the BPF map instance
+ * @return true if map is set to auto-attach during skeleton attach phase; false otherwise
+ *
+ */
+LIBBPF_API bool bpf_map__autoattach(const struct bpf_map *map);
+
 /**
  * @brief **bpf_map__fd()** gets the file descriptor of the passed
  * BPF map
@@ -1672,6 +1691,7 @@ struct bpf_map_skeleton {
 	const char *name;
 	struct bpf_map **map;
 	void **mmaped;
+	struct bpf_link **link;
 };
 
 struct bpf_prog_skeleton {
tools/lib/bpf/libbpf.map +2 -0
index c1ce8aa3520b..40595233dc7f 100644
@@ -419,6 +419,8 @@ LIBBPF_1.4.0 {
 
 LIBBPF_1.5.0 {
 	global:
+		bpf_map__autoattach;
+		bpf_map__set_autoattach;
 		bpf_program__attach_sockmap;
 		ring__consume_n;
 		ring_buffer__consume_n;
Signature · 1 line
2.45.0
#1 lore raw
Quoted 18 lines · Mykyta Yatsenko

From: Mykyta Yatsenko <[email protected]>

Similarly to bpf_program, support bpf_map automatic attachment in bpf_object__attach_skeleton. Currently only struct_ops maps could be attached.

Bpftool
Code-generate links in skeleton struct for struct_ops maps. Similarly to bpf_program_skeleton, set links in bpf_map_skeleton.

Libbpf
Extending bpf_map with new autoattach field to support enabling or disabling autoattach functionality, introducing getter/setter for this field.
Extending bpf_object__(attach|detach)_skeleton with attaching/detaching struct_ops maps.

Quoted 8 lines
---
 tools/bpf/bpftool/gen.c  | 36 +++++++++++++++++++---
 tools/lib/bpf/libbpf.c   | 65 ++++++++++++++++++++++++++++++++++++++--
 tools/lib/bpf/libbpf.h   | 20 +++++++++++++
 tools/lib/bpf/libbpf.map |  2 ++
 4 files changed, 116 insertions(+), 7 deletions(-)

This looks great and finished (see minor nit below which I fixed up while applying). I know you had a selftest ready, why didn't you submit it? Please follow up with a selftest so we have this functionality executed in our selftests. Thanks!

[...]

tools/lib/bpf/libbpf.c int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) · line 13992

Quoted 11 lines
tools/lib/bpf/libbpf.c +11 -0
+               /* only struct_ops maps can be attached */
+               if (!bpf_map__is_struct_ops(map))
+                       continue;
+               *link = bpf_map__attach_struct_ops(map);
+
+               if (!*link) {
+                       const int errcode = errno;
+
+                       pr_warn("struct_ops %s: failed to auto-attach: %d\n", bpf_map__name(map),
+                               errcode);
+                       return libbpf_err(-errcode);

there is err variable already in this function, I switched to using it here

Quoted 6 lines
tools/lib/bpf/libbpf.c +3 -0
+               }
+       }
+
        return 0;
 }

[...]

#2 lore raw
In reply to Mykyta Yatsenko [email protected]
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 11 lines · On Wed, 5 Jun 2024 18:51:35 +0100 you wrote:

From: Mykyta Yatsenko <[email protected]>

Similarly to bpf_program, support bpf_map automatic attachment in bpf_object__attach_skeleton. Currently only struct_ops maps could be attached.

Bpftool
Code-generate links in skeleton struct for struct_ops maps. Similarly to bpf_program_skeleton, set links in bpf_map_skeleton.

[...]

Here is the summary with links:
- [bpf-next] libbpf: auto-attach skeletons struct_ops

    https://git.kernel.org/bpf/bpf-next/c/08ac454e258e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html