* [PATCH bpf-next v6 1/5] libbpf: set btf_value_type_id of struct bpf_map for struct_ops.
2024-02-29 6:45 [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons Kui-Feng Lee
@ 2024-02-29 6:45 ` Kui-Feng Lee
2024-02-29 6:45 ` [PATCH bpf-next v6 2/5] libbpf: Convert st_ops->data to shadow type Kui-Feng Lee
` (4 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Kui-Feng Lee @ 2024-02-29 6:45 UTC (permalink / raw)
To: bpf, ast, martin.lau, song, kernel-team, andrii, quentin
Cc: sinquersw, kuifeng, Kui-Feng Lee
For a struct_ops map, btf_value_type_id is the type ID of it's struct
type. This value is required by bpftool to generate skeleton including
pointers of shadow types. The code generator gets the type ID from
bpf_map__btf_value_type_id() in order to get the type information of the
struct type of a map.
Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
---
tools/lib/bpf/libbpf.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 01f407591a92..4c322dd65e10 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1229,6 +1229,7 @@ static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
map->name = strdup(var_name);
if (!map->name)
return -ENOMEM;
+ map->btf_value_type_id = type_id;
map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
map->def.key_size = sizeof(int);
@@ -4857,6 +4858,12 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
create_attr.btf_value_type_id = 0;
map->btf_key_type_id = 0;
map->btf_value_type_id = 0;
+ break;
+
+ case BPF_MAP_TYPE_STRUCT_OPS:
+ create_attr.btf_value_type_id = 0;
+ break;
+
default:
break;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH bpf-next v6 2/5] libbpf: Convert st_ops->data to shadow type.
2024-02-29 6:45 [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons Kui-Feng Lee
2024-02-29 6:45 ` [PATCH bpf-next v6 1/5] libbpf: set btf_value_type_id of struct bpf_map for struct_ops Kui-Feng Lee
@ 2024-02-29 6:45 ` Kui-Feng Lee
2024-02-29 6:45 ` [PATCH bpf-next v6 3/5] bpftool: generated shadow variables for struct_ops maps Kui-Feng Lee
` (3 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Kui-Feng Lee @ 2024-02-29 6:45 UTC (permalink / raw)
To: bpf, ast, martin.lau, song, kernel-team, andrii, quentin
Cc: sinquersw, kuifeng, Kui-Feng Lee
Convert st_ops->data to the shadow type of the struct_ops map. The shadow
type of a struct_ops type is a variant of the original struct type
providing a way to access/change the values in the maps of the struct_ops
type.
bpf_map__initial_value() will return st_ops->data for struct_ops types. The
skeleton is going to use it as the pointer to the shadow type of the
original struct type.
One of the main differences between the original struct type and the shadow
type is that all function pointers of the shadow type are converted to
pointers of struct bpf_program. Users can replace these bpf_program
pointers with other BPF programs. The st_ops->progs[] will be updated
before updating the value of a map to reflect the changes made by users.
Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
---
tools/lib/bpf/libbpf.c | 43 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 41 insertions(+), 2 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 4c322dd65e10..65d23a9d3e50 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1014,6 +1014,22 @@ static bool bpf_map__is_struct_ops(const struct bpf_map *map)
return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
}
+static bool is_valid_st_ops_program(struct bpf_object *obj,
+ const struct bpf_program *prog)
+{
+ int i;
+
+ if (!obj->nr_programs)
+ return false;
+
+ for (i = 0; i < obj->nr_programs; i++) {
+ if (&obj->programs[i] == prog)
+ return prog->type == BPF_PROG_TYPE_STRUCT_OPS;
+ }
+
+ return false;
+}
+
/* Init the map's fields that depend on kern_btf */
static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
{
@@ -1102,9 +1118,16 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
if (btf_is_ptr(mtype)) {
struct bpf_program *prog;
- prog = st_ops->progs[i];
+ /* Update the value from the shadow type */
+ prog = *(void **)mdata;
+ st_ops->progs[i] = prog;
if (!prog)
continue;
+ if (!is_valid_st_ops_program(obj, prog)) {
+ pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n",
+ map->name, mname);
+ return -ENOTSUP;
+ }
kern_mtype = skip_mods_and_typedefs(kern_btf,
kern_mtype->type,
@@ -9312,7 +9335,9 @@ static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
return NULL;
}
-/* Collect the reloc from ELF and populate the st_ops->progs[] */
+/* Collect the reloc from ELF, populate the st_ops->progs[], and update
+ * st_ops->data for shadow type.
+ */
static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
Elf64_Shdr *shdr, Elf_Data *data)
{
@@ -9426,6 +9451,14 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
}
st_ops->progs[member_idx] = prog;
+
+ /* st_ops->data will be exposed to users, being returned by
+ * bpf_map__initial_value() as a pointer to the shadow
+ * type. All function pointers in the original struct type
+ * should be converted to a pointer to struct bpf_program
+ * in the shadow type.
+ */
+ *((struct bpf_program **)(st_ops->data + moff)) = prog;
}
return 0;
@@ -9884,6 +9917,12 @@ int bpf_map__set_initial_value(struct bpf_map *map,
void *bpf_map__initial_value(struct bpf_map *map, size_t *psize)
{
+ if (bpf_map__is_struct_ops(map)) {
+ if (psize)
+ *psize = map->def.value_size;
+ return map->st_ops->data;
+ }
+
if (!map->mmaped)
return NULL;
*psize = map->def.value_size;
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH bpf-next v6 3/5] bpftool: generated shadow variables for struct_ops maps.
2024-02-29 6:45 [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons Kui-Feng Lee
2024-02-29 6:45 ` [PATCH bpf-next v6 1/5] libbpf: set btf_value_type_id of struct bpf_map for struct_ops Kui-Feng Lee
2024-02-29 6:45 ` [PATCH bpf-next v6 2/5] libbpf: Convert st_ops->data to shadow type Kui-Feng Lee
@ 2024-02-29 6:45 ` Kui-Feng Lee
2024-02-29 22:25 ` Andrii Nakryiko
2024-02-29 6:45 ` [PATCH bpf-next v6 4/5] bpftool: Add an example for struct_ops map and shadow type Kui-Feng Lee
` (2 subsequent siblings)
5 siblings, 1 reply; 9+ messages in thread
From: Kui-Feng Lee @ 2024-02-29 6:45 UTC (permalink / raw)
To: bpf, ast, martin.lau, song, kernel-team, andrii, quentin
Cc: sinquersw, kuifeng, Kui-Feng Lee
Declares and defines a pointer of the shadow type for each struct_ops map.
The code generator will create an anonymous struct type as the shadow type
for each struct_ops map. The shadow type is translated from the original
struct type of the map. The user of the skeleton use pointers of them to
access the values of struct_ops maps.
However, shadow types only supports certain types of fields, including
scalar types and function pointers. Any fields of unsupported types are
translated into an array of characters to occupy the space of the original
field. Function pointers are translated into pointers of the struct
bpf_program. Additionally, padding fields are generated to occupy the space
between two consecutive fields.
The pointers of shadow types of struct_osp maps are initialized when
*__open_opts() in skeletons are called. For a map called FOO, the user can
access it through the pointer at skel->struct_ops.FOO.
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
---
tools/bpf/bpftool/gen.c | 237 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 236 insertions(+), 1 deletion(-)
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index a9334c57e859..da8ae75eb6cb 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -55,6 +55,20 @@ static bool str_has_suffix(const char *str, const char *suffix)
return true;
}
+static const struct btf_type *
+resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
+{
+ const struct btf_type *t;
+
+ t = skip_mods_and_typedefs(btf, id, NULL);
+ if (!btf_is_ptr(t))
+ return NULL;
+
+ t = skip_mods_and_typedefs(btf, t->type, res_id);
+
+ return btf_is_func_proto(t) ? t : NULL;
+}
+
static void get_obj_name(char *name, const char *file)
{
char file_copy[PATH_MAX];
@@ -909,6 +923,206 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li
}
}
+static int walk_st_ops_shadow_vars(struct btf *btf, const char *ident,
+ const struct btf_type *map_type, __u32 map_type_id)
+{
+ LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts, .indent_level = 3);
+ const struct btf_type *member_type;
+ __u32 offset, next_offset = 0;
+ const struct btf_member *m;
+ struct btf_dump *d = NULL;
+ const char *member_name;
+ __u32 member_type_id;
+ int i, err = 0, n;
+ int size;
+
+ d = btf_dump__new(btf, codegen_btf_dump_printf, NULL, NULL);
+ if (!d)
+ return -errno;
+
+ n = btf_vlen(map_type);
+ for (i = 0, m = btf_members(map_type); i < n; i++, m++) {
+ member_type = skip_mods_and_typedefs(btf, m->type, &member_type_id);
+ member_name = btf__name_by_offset(btf, m->name_off);
+
+ offset = m->offset / 8;
+ if (next_offset < offset)
+ printf("\t\t\tchar __padding_%d[%d];\n", i, offset - next_offset);
+
+ switch (btf_kind(member_type)) {
+ case BTF_KIND_INT:
+ case BTF_KIND_FLOAT:
+ case BTF_KIND_ENUM:
+ case BTF_KIND_ENUM64:
+ /* scalar type */
+ printf("\t\t\t");
+ opts.field_name = member_name;
+ err = btf_dump__emit_type_decl(d, member_type_id, &opts);
+ if (err) {
+ p_err("Failed to emit type declaration for %s: %d", member_name, err);
+ goto out;
+ }
+ printf(";\n");
+
+ size = btf__resolve_size(btf, member_type_id);
+ if (size < 0) {
+ p_err("Failed to resolve size of %s: %d\n", member_name, size);
+ err = size;
+ goto out;
+ }
+
+ next_offset = offset + size;
+ break;
+
+ case BTF_KIND_PTR:
+ if (resolve_func_ptr(btf, m->type, NULL)) {
+ /* Function pointer */
+ printf("\t\t\tstruct bpf_program *%s;\n", member_name);
+
+ next_offset = offset + sizeof(void *);
+ break;
+ }
+ /* All pointer types are unsupported except for
+ * function pointers.
+ */
+ fallthrough;
+
+ default:
+ /* Unsupported types
+ *
+ * Types other than scalar types and function
+ * pointers are currently not supported in order to
+ * prevent conflicts in the generated code caused
+ * by multiple definitions. For instance, if the
+ * struct type FOO is used in a struct_ops map,
+ * bpftool has to generate definitions for FOO,
+ * which may result in conflicts if FOO is defined
+ * in different skeleton files.
+ */
+ size = btf__resolve_size(btf, member_type_id);
+ if (size < 0) {
+ p_err("Failed to resolve size of %s: %d\n", member_name, size);
+ err = size;
+ goto out;
+ }
+ printf("\t\t\tchar __unsupported_%d[%d];\n", i, size);
+
+ next_offset = offset + size;
+ break;
+ }
+ }
+
+ /* Can not fail since it must be a struct type */
+ size = btf__resolve_size(btf, map_type_id);
+ if (next_offset < (__u32)size)
+ printf("\t\t\tchar __padding_end[%d];\n", size - next_offset);
+
+out:
+ btf_dump__free(d);
+
+ return err;
+}
+
+/* Generate the pointer of the shadow type for a struct_ops map.
+ *
+ * This function adds a pointer of the shadow type for a struct_ops map.
+ * The members of a struct_ops map can be exported through a pointer to a
+ * shadow type. The user can access these members through the pointer.
+ *
+ * A shadow type includes not all members, only members of some types.
+ * They are scalar types and function pointers. The function pointers are
+ * translated to the pointer of the struct bpf_program. The scalar types
+ * are translated to the original type without any modifiers.
+ *
+ * Unsupported types will be translated to a char array to occupy the same
+ * space as the original field, being renamed as __unsupported_*. The user
+ * should treat these fields as opaque data.
+ */
+static int gen_st_ops_shadow_type(const char *obj_name, struct btf *btf, const char *ident,
+ const struct bpf_map *map)
+{
+ const struct btf_type *map_type;
+ const char *type_name;
+ __u32 map_type_id;
+ int err;
+
+ map_type_id = bpf_map__btf_value_type_id(map);
+ if (map_type_id == 0)
+ return -EINVAL;
+ map_type = btf__type_by_id(btf, map_type_id);
+ if (!map_type)
+ return -EINVAL;
+
+ type_name = btf__name_by_offset(btf, map_type->name_off);
+
+ printf("\t\tstruct %s_%s_%s {\n", obj_name, ident, type_name);
+
+ err = walk_st_ops_shadow_vars(btf, ident, map_type, map_type_id);
+ if (err)
+ return err;
+
+ printf("\t\t} *%s;\n", ident);
+
+ return 0;
+}
+
+static int gen_st_ops_shadow(const char *obj_name, struct btf *btf, struct bpf_object *obj)
+{
+ int err, st_ops_cnt = 0;
+ struct bpf_map *map;
+ char ident[256];
+
+ if (!btf)
+ return 0;
+
+ /* Generate the pointers to shadow types of
+ * struct_ops maps.
+ */
+ bpf_object__for_each_map(map, obj) {
+ if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
+ continue;
+ if (!get_map_ident(map, ident, sizeof(ident)))
+ continue;
+
+ if (!st_ops_cnt++)
+ printf("\tstruct {\n");
+
+ err = gen_st_ops_shadow_type(obj_name, btf, ident, map);
+ if (err)
+ return err;
+ }
+
+ if (st_ops_cnt)
+ printf("\t} struct_ops;\n");
+
+ return 0;
+}
+
+/* Generate the code to initialize the pointers of shadow types. */
+static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj)
+{
+ struct bpf_map *map;
+ char ident[256];
+
+ if (!btf)
+ return;
+
+ /* Initialize the pointers to_ops shadow types of
+ * struct_ops maps.
+ */
+ bpf_object__for_each_map(map, obj) {
+ if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
+ continue;
+ if (!get_map_ident(map, ident, sizeof(ident)))
+ continue;
+ codegen("\
+ \n\
+ obj->struct_ops.%1$s = bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
+ \n\
+ ", ident);
+ }
+}
+
static int do_skeleton(int argc, char **argv)
{
char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
@@ -1039,6 +1253,8 @@ static int do_skeleton(int argc, char **argv)
);
}
+ btf = bpf_object__btf(obj);
+
if (map_cnt) {
printf("\tstruct {\n");
bpf_object__for_each_map(map, obj) {
@@ -1052,6 +1268,10 @@ static int do_skeleton(int argc, char **argv)
printf("\t} maps;\n");
}
+ err = gen_st_ops_shadow(obj_name, btf, obj);
+ if (err)
+ goto out;
+
if (prog_cnt) {
printf("\tstruct {\n");
bpf_object__for_each_program(prog, obj) {
@@ -1075,7 +1295,6 @@ static int do_skeleton(int argc, char **argv)
printf("\t} links;\n");
}
- btf = bpf_object__btf(obj);
if (btf) {
err = codegen_datasecs(obj, obj_name);
if (err)
@@ -1133,6 +1352,12 @@ static int do_skeleton(int argc, char **argv)
if (err) \n\
goto err_out; \n\
\n\
+ ", obj_name);
+
+ gen_st_ops_shadow_init(btf, obj);
+
+ codegen("\
+ \n\
return obj; \n\
err_out: \n\
%1$s__destroy(obj); \n\
@@ -1442,6 +1667,10 @@ static int do_subskeleton(int argc, char **argv)
printf("\t} maps;\n");
}
+ err = gen_st_ops_shadow(obj_name, btf, obj);
+ if (err)
+ goto out;
+
if (prog_cnt) {
printf("\tstruct {\n");
bpf_object__for_each_program(prog, obj) {
@@ -1553,6 +1782,12 @@ static int do_subskeleton(int argc, char **argv)
if (err) \n\
goto err; \n\
\n\
+ ");
+
+ gen_st_ops_shadow_init(btf, obj);
+
+ codegen("\
+ \n\
return obj; \n\
err: \n\
%1$s__destroy(obj); \n\
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH bpf-next v6 3/5] bpftool: generated shadow variables for struct_ops maps.
2024-02-29 6:45 ` [PATCH bpf-next v6 3/5] bpftool: generated shadow variables for struct_ops maps Kui-Feng Lee
@ 2024-02-29 22:25 ` Andrii Nakryiko
2024-03-01 18:04 ` Kui-Feng Lee
0 siblings, 1 reply; 9+ messages in thread
From: Andrii Nakryiko @ 2024-02-29 22:25 UTC (permalink / raw)
To: Kui-Feng Lee
Cc: bpf, ast, martin.lau, song, kernel-team, andrii, quentin,
sinquersw, kuifeng
On Wed, Feb 28, 2024 at 10:45 PM Kui-Feng Lee <thinker.li@gmail.com> wrote:
>
> Declares and defines a pointer of the shadow type for each struct_ops map.
>
> The code generator will create an anonymous struct type as the shadow type
> for each struct_ops map. The shadow type is translated from the original
> struct type of the map. The user of the skeleton use pointers of them to
> access the values of struct_ops maps.
>
> However, shadow types only supports certain types of fields, including
> scalar types and function pointers. Any fields of unsupported types are
> translated into an array of characters to occupy the space of the original
> field. Function pointers are translated into pointers of the struct
> bpf_program. Additionally, padding fields are generated to occupy the space
> between two consecutive fields.
>
> The pointers of shadow types of struct_osp maps are initialized when
> *__open_opts() in skeletons are called. For a map called FOO, the user can
> access it through the pointer at skel->struct_ops.FOO.
>
> Reviewed-by: Quentin Monnet <quentin@isovalent.com>
> Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
> ---
> tools/bpf/bpftool/gen.c | 237 +++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 236 insertions(+), 1 deletion(-)
>
[...]
> +/* Generate the pointer of the shadow type for a struct_ops map.
> + *
> + * This function adds a pointer of the shadow type for a struct_ops map.
> + * The members of a struct_ops map can be exported through a pointer to a
> + * shadow type. The user can access these members through the pointer.
> + *
> + * A shadow type includes not all members, only members of some types.
> + * They are scalar types and function pointers. The function pointers are
> + * translated to the pointer of the struct bpf_program. The scalar types
> + * are translated to the original type without any modifiers.
> + *
> + * Unsupported types will be translated to a char array to occupy the same
> + * space as the original field, being renamed as __unsupported_*. The user
> + * should treat these fields as opaque data.
> + */
> +static int gen_st_ops_shadow_type(const char *obj_name, struct btf *btf, const char *ident,
> + const struct bpf_map *map)
> +{
> + const struct btf_type *map_type;
> + const char *type_name;
> + __u32 map_type_id;
> + int err;
> +
> + map_type_id = bpf_map__btf_value_type_id(map);
> + if (map_type_id == 0)
> + return -EINVAL;
> + map_type = btf__type_by_id(btf, map_type_id);
> + if (!map_type)
> + return -EINVAL;
> +
> + type_name = btf__name_by_offset(btf, map_type->name_off);
> +
> + printf("\t\tstruct %s_%s_%s {\n", obj_name, ident, type_name);
it should be %s__%s__%s, I fixed it up
> +
> + err = walk_st_ops_shadow_vars(btf, ident, map_type, map_type_id);
> + if (err)
> + return err;
> +
> + printf("\t\t} *%s;\n", ident);
> +
> + return 0;
> +}
> +
> +static int gen_st_ops_shadow(const char *obj_name, struct btf *btf, struct bpf_object *obj)
> +{
> + int err, st_ops_cnt = 0;
> + struct bpf_map *map;
> + char ident[256];
> +
> + if (!btf)
> + return 0;
> +
> + /* Generate the pointers to shadow types of
> + * struct_ops maps.
> + */
> + bpf_object__for_each_map(map, obj) {
> + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
> + continue;
> + if (!get_map_ident(map, ident, sizeof(ident)))
> + continue;
> +
> + if (!st_ops_cnt++)
goodness, too much operator precedence knowledge assumed, I simplified
this to what I can follow myself:
if (st_ops_cnt == 0)
printf(...);
st_ops_cnt++;
I don't think saving one line of code is worth it.
> + printf("\tstruct {\n");
> +
> + err = gen_st_ops_shadow_type(obj_name, btf, ident, map);
> + if (err)
> + return err;
> + }
> +
> + if (st_ops_cnt)
> + printf("\t} struct_ops;\n");
> +
> + return 0;
> +}
> +
[...]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH bpf-next v6 3/5] bpftool: generated shadow variables for struct_ops maps.
2024-02-29 22:25 ` Andrii Nakryiko
@ 2024-03-01 18:04 ` Kui-Feng Lee
0 siblings, 0 replies; 9+ messages in thread
From: Kui-Feng Lee @ 2024-03-01 18:04 UTC (permalink / raw)
To: Andrii Nakryiko, Kui-Feng Lee
Cc: bpf, ast, martin.lau, song, kernel-team, andrii, quentin, kuifeng
On 2/29/24 14:25, Andrii Nakryiko wrote:
> On Wed, Feb 28, 2024 at 10:45 PM Kui-Feng Lee <thinker.li@gmail.com> wrote:
>>
... skipped ...
>> +static int gen_st_ops_shadow(const char *obj_name, struct btf *btf, struct bpf_object *obj)
>> +{
>> + int err, st_ops_cnt = 0;
>> + struct bpf_map *map;
>> + char ident[256];
>> +
>> + if (!btf)
>> + return 0;
>> +
>> + /* Generate the pointers to shadow types of
>> + * struct_ops maps.
>> + */
>> + bpf_object__for_each_map(map, obj) {
>> + if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
>> + continue;
>> + if (!get_map_ident(map, ident, sizeof(ident)))
>> + continue;
>> +
>> + if (!st_ops_cnt++)
>
> goodness, too much operator precedence knowledge assumed, I simplified
> this to what I can follow myself:
>
> if (st_ops_cnt == 0)
> printf(...);
> st_ops_cnt++;
>
> I don't think saving one line of code is worth it.
It is totally reasonable.
Thanks for review!
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH bpf-next v6 4/5] bpftool: Add an example for struct_ops map and shadow type.
2024-02-29 6:45 [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons Kui-Feng Lee
` (2 preceding siblings ...)
2024-02-29 6:45 ` [PATCH bpf-next v6 3/5] bpftool: generated shadow variables for struct_ops maps Kui-Feng Lee
@ 2024-02-29 6:45 ` Kui-Feng Lee
2024-02-29 6:45 ` [PATCH bpf-next v6 5/5] selftests/bpf: Test if shadow types work correctly Kui-Feng Lee
2024-02-29 22:30 ` [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons patchwork-bot+netdevbpf
5 siblings, 0 replies; 9+ messages in thread
From: Kui-Feng Lee @ 2024-02-29 6:45 UTC (permalink / raw)
To: bpf, ast, martin.lau, song, kernel-team, andrii, quentin
Cc: sinquersw, kuifeng, Kui-Feng Lee
The example in bpftool-gen.8 explains how to use the pointer of the shadow
type to change the value of a field of a struct_ops map.
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
---
.../bpf/bpftool/Documentation/bpftool-gen.rst | 58 +++++++++++++++++--
1 file changed, 52 insertions(+), 6 deletions(-)
diff --git a/tools/bpf/bpftool/Documentation/bpftool-gen.rst b/tools/bpf/bpftool/Documentation/bpftool-gen.rst
index 5006e724d1bc..62572f5beed9 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-gen.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-gen.rst
@@ -257,18 +257,48 @@ EXAMPLES
return 0;
}
-This is example BPF application with two BPF programs and a mix of BPF maps
-and global variables. Source code is split across two source code files.
+**$ cat example3.bpf.c**
+
+::
+
+ #include <linux/ptrace.h>
+ #include <linux/bpf.h>
+ #include <bpf/bpf_helpers.h>
+ /* This header file is provided by the bpf_testmod module. */
+ #include "bpf_testmod.h"
+
+ int test_2_result = 0;
+
+ /* bpf_Testmod.ko calls this function, passing a "4"
+ * and testmod_map->data.
+ */
+ SEC("struct_ops/test_2")
+ void BPF_PROG(test_2, int a, int b)
+ {
+ test_2_result = a + b;
+ }
+
+ SEC(".struct_ops")
+ struct bpf_testmod_ops testmod_map = {
+ .test_2 = (void *)test_2,
+ .data = 0x1,
+ };
+
+This is example BPF application with three BPF programs and a mix of BPF
+maps and global variables. Source code is split across three source code
+files.
**$ clang --target=bpf -g example1.bpf.c -o example1.bpf.o**
**$ clang --target=bpf -g example2.bpf.c -o example2.bpf.o**
-**$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o**
+**$ clang --target=bpf -g example3.bpf.c -o example3.bpf.o**
+
+**$ bpftool gen object example.bpf.o example1.bpf.o example2.bpf.o example3.bpf.o**
-This set of commands compiles *example1.bpf.c* and *example2.bpf.c*
-individually and then statically links respective object files into the final
-BPF ELF object file *example.bpf.o*.
+This set of commands compiles *example1.bpf.c*, *example2.bpf.c* and
+*example3.bpf.c* individually and then statically links respective object
+files into the final BPF ELF object file *example.bpf.o*.
**$ bpftool gen skeleton example.bpf.o name example | tee example.skel.h**
@@ -291,7 +321,15 @@ BPF ELF object file *example.bpf.o*.
struct bpf_map *data;
struct bpf_map *bss;
struct bpf_map *my_map;
+ struct bpf_map *testmod_map;
} maps;
+ struct {
+ struct {
+ const struct bpf_program *test_1;
+ const struct bpf_program *test_2;
+ int data;
+ } *testmod_map;
+ } struct_ops;
struct {
struct bpf_program *handle_sys_enter;
struct bpf_program *handle_sys_exit;
@@ -304,6 +342,7 @@ BPF ELF object file *example.bpf.o*.
struct {
int x;
} data;
+ int test_2_result;
} *bss;
struct example__data {
_Bool global_flag;
@@ -342,10 +381,16 @@ BPF ELF object file *example.bpf.o*.
skel->rodata->param1 = 128;
+ /* Change the value through the pointer of shadow type */
+ skel->struct_ops.testmod_map->data = 13;
+
err = example__load(skel);
if (err)
goto cleanup;
+ /* The result of the function test_2() */
+ printf("test_2_result: %d\n", skel->bss->test_2_result);
+
err = example__attach(skel);
if (err)
goto cleanup;
@@ -372,6 +417,7 @@ BPF ELF object file *example.bpf.o*.
::
+ test_2_result: 17
my_map name: my_map
sys_enter prog FD: 8
my_static_var: 7
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH bpf-next v6 5/5] selftests/bpf: Test if shadow types work correctly.
2024-02-29 6:45 [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons Kui-Feng Lee
` (3 preceding siblings ...)
2024-02-29 6:45 ` [PATCH bpf-next v6 4/5] bpftool: Add an example for struct_ops map and shadow type Kui-Feng Lee
@ 2024-02-29 6:45 ` Kui-Feng Lee
2024-02-29 22:30 ` [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons patchwork-bot+netdevbpf
5 siblings, 0 replies; 9+ messages in thread
From: Kui-Feng Lee @ 2024-02-29 6:45 UTC (permalink / raw)
To: bpf, ast, martin.lau, song, kernel-team, andrii, quentin
Cc: sinquersw, kuifeng, Kui-Feng Lee
Change the values of fields, including scalar types and function pointers,
and check if the struct_ops map works as expected.
The test changes the field "test_2" of "testmod_1" from the pointer to
test_2() to pointer to test_3() and the field "data" to 13. The function
test_2() and test_3() both compute a new value for "test_2_result", but in
different way. By checking the value of "test_2_result", it ensures the
struct_ops map works as expected with changes through shadow types.
Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
---
.../selftests/bpf/bpf_testmod/bpf_testmod.c | 11 ++++++++++-
.../selftests/bpf/bpf_testmod/bpf_testmod.h | 8 ++++++++
.../bpf/prog_tests/test_struct_ops_module.c | 19 +++++++++++++++----
.../selftests/bpf/progs/struct_ops_module.c | 8 ++++++++
4 files changed, 41 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
index 66787e99ba1b..098ddd067224 100644
--- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
@@ -539,6 +539,15 @@ static int bpf_testmod_ops_init_member(const struct btf_type *t,
const struct btf_member *member,
void *kdata, const void *udata)
{
+ if (member->offset == offsetof(struct bpf_testmod_ops, data) * 8) {
+ /* For data fields, this function has to copy it and return
+ * 1 to indicate that the data has been handled by the
+ * struct_ops type, or the verifier will reject the map if
+ * the value of the data field is not zero.
+ */
+ ((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data;
+ return 1;
+ }
return 0;
}
@@ -559,7 +568,7 @@ static int bpf_dummy_reg(void *kdata)
* initialized, so we need to check for NULL.
*/
if (ops->test_2)
- ops->test_2(4, 3);
+ ops->test_2(4, ops->data);
return 0;
}
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
index c3b0cf788f9f..971458acfac3 100644
--- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
+++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
@@ -35,6 +35,14 @@ struct bpf_testmod_ops {
void (*test_2)(int a, int b);
/* Used to test nullable arguments. */
int (*test_maybe_null)(int dummy, struct task_struct *task);
+
+ /* The following fields are used to test shadow copies. */
+ char onebyte;
+ struct {
+ int a;
+ int b;
+ } unsupported;
+ int data;
};
#endif /* _BPF_TESTMOD_H */
diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
index 8d833f0c7580..7d6facf46ebb 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
@@ -32,17 +32,23 @@ static void check_map_info(struct bpf_map_info *info)
static void test_struct_ops_load(void)
{
- DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
struct struct_ops_module *skel;
struct bpf_map_info info = {};
struct bpf_link *link;
int err;
u32 len;
- skel = struct_ops_module__open_opts(&opts);
+ skel = struct_ops_module__open();
if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
return;
+ skel->struct_ops.testmod_1->data = 13;
+ skel->struct_ops.testmod_1->test_2 = skel->progs.test_3;
+ /* Since test_2() is not being used, it should be disabled from
+ * auto-loading, or it will fail to load.
+ */
+ bpf_program__set_autoload(skel->progs.test_2, false);
+
err = struct_ops_module__load(skel);
if (!ASSERT_OK(err, "struct_ops_module_load"))
goto cleanup;
@@ -56,8 +62,13 @@ static void test_struct_ops_load(void)
link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
ASSERT_OK_PTR(link, "attach_test_mod_1");
- /* test_2() will be called from bpf_dummy_reg() in bpf_testmod.c */
- ASSERT_EQ(skel->bss->test_2_result, 7, "test_2_result");
+ /* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c
+ *
+ * In bpf_testmod.c it will pass 4 and 13 (the value of data) to
+ * .test_2. So, the value of test_2_result should be 20 (4 + 13 +
+ * 3).
+ */
+ ASSERT_EQ(skel->bss->test_2_result, 20, "check_shadow_variables");
bpf_link__destroy(link);
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_module.c b/tools/testing/selftests/bpf/progs/struct_ops_module.c
index b78746b3cef3..25952fa09348 100644
--- a/tools/testing/selftests/bpf/progs/struct_ops_module.c
+++ b/tools/testing/selftests/bpf/progs/struct_ops_module.c
@@ -21,9 +21,17 @@ void BPF_PROG(test_2, int a, int b)
test_2_result = a + b;
}
+SEC("struct_ops/test_3")
+int BPF_PROG(test_3, int a, int b)
+{
+ test_2_result = a + b + 3;
+ return a + b + 3;
+}
+
SEC(".struct_ops.link")
struct bpf_testmod_ops testmod_1 = {
.test_1 = (void *)test_1,
.test_2 = (void *)test_2,
+ .data = 0x1,
};
--
2.34.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons
2024-02-29 6:45 [PATCH bpf-next v6 0/5] Create shadow types for struct_ops maps in skeletons Kui-Feng Lee
` (4 preceding siblings ...)
2024-02-29 6:45 ` [PATCH bpf-next v6 5/5] selftests/bpf: Test if shadow types work correctly Kui-Feng Lee
@ 2024-02-29 22:30 ` patchwork-bot+netdevbpf
5 siblings, 0 replies; 9+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-02-29 22:30 UTC (permalink / raw)
To: Kui-Feng Lee
Cc: bpf, ast, martin.lau, song, kernel-team, andrii, quentin,
sinquersw, kuifeng
Hello:
This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:
On Wed, 28 Feb 2024 22:45:18 -0800 you wrote:
> This patchset allows skeleton users to change the values of the fields
> in struct_ops maps at runtime. It will create a shadow type pointer in
> a skeleton for each struct_ops map, allowing users to access the
> values of fields through these pointers. For instance, if there is an
> integer field named "FOO" in a struct_ops map called "testmap", you
> can access the value of "FOO" in this way.
>
> [...]
Here is the summary with links:
- [bpf-next,v6,1/5] libbpf: set btf_value_type_id of struct bpf_map for struct_ops.
https://git.kernel.org/bpf/bpf-next/c/3644d285462a
- [bpf-next,v6,2/5] libbpf: Convert st_ops->data to shadow type.
https://git.kernel.org/bpf/bpf-next/c/69e4a9d2b3f5
- [bpf-next,v6,3/5] bpftool: generated shadow variables for struct_ops maps.
https://git.kernel.org/bpf/bpf-next/c/a7b0fa352eaf
- [bpf-next,v6,4/5] bpftool: Add an example for struct_ops map and shadow type.
https://git.kernel.org/bpf/bpf-next/c/f2e81192e07e
- [bpf-next,v6,5/5] selftests/bpf: Test if shadow types work correctly.
https://git.kernel.org/bpf/bpf-next/c/0623e7331794
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 9+ messages in thread