* [PATCH v2 bpf-next 1/3] libbpf: Change bpf_object_skelecton data field to const void*
2021-09-01 19:44 [PATCH v2 bpf-next 0/3] Bpf skeleton helper method Matt Smith
@ 2021-09-01 19:44 ` Matt Smith
2021-09-01 19:44 ` [PATCH v2 bpf-next 2/3] bpftool: Provide a helper method for accessing bpf binary data Matt Smith
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Matt Smith @ 2021-09-01 19:44 UTC (permalink / raw)
To: bpf, ast, andriin, daniel, kernel-team; +Cc: Matt Smith
This change was necessary to enforce the implied contract
that bpf_object_skeleton->data should not be mutated. The data
will be cast to `void *` during assignment to handle the case
where a user is compiling with older libbpf headers to avoid
a compiler warning of `const void *` data being cast to `void *`
Signed-off-by: Matt Smith <alastorze@fb.com>
---
tools/lib/bpf/libbpf.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index f177d897c5f7..2f6f0e15d1e7 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -854,7 +854,7 @@ struct bpf_object_skeleton {
size_t sz; /* size of this struct, for forward/backward compatibility */
const char *name;
- void *data;
+ const void *data;
size_t data_sz;
struct bpf_object **obj;
--
2.30.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 bpf-next 2/3] bpftool: Provide a helper method for accessing bpf binary data
2021-09-01 19:44 [PATCH v2 bpf-next 0/3] Bpf skeleton helper method Matt Smith
2021-09-01 19:44 ` [PATCH v2 bpf-next 1/3] libbpf: Change bpf_object_skelecton data field to const void* Matt Smith
@ 2021-09-01 19:44 ` Matt Smith
2021-09-01 19:44 ` [PATCH v2 bpf-next 3/3] selftests/bpf: Add checks for X__elf_bytes skeleton helper Matt Smith
2021-09-08 1:02 ` [PATCH v2 bpf-next 0/3] Bpf skeleton helper method Andrii Nakryiko
3 siblings, 0 replies; 5+ messages in thread
From: Matt Smith @ 2021-09-01 19:44 UTC (permalink / raw)
To: bpf, ast, andriin, daniel, kernel-team; +Cc: Matt Smith
This adds a binary method X_elf_bytes() which returns the binary data of
the compiled bpf object. It additionally sets the size of the return
data to the provided size_t pointer argument.
The assignment to s->data is cast to void * to ensure no warning is
issued if compiled with a previous version of libbpf where the
bpf_object_skeleton field is void * instead of const void *
Signed-off-by: Matt Smith <alastorze@fb.com>
---
tools/bpf/bpftool/gen.c | 39 ++++++++++++++++++++++++---------------
1 file changed, 24 insertions(+), 15 deletions(-)
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index d40d92bbf0e4..f54dc4792fce 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -238,8 +238,8 @@ static void codegen(const char *template, ...)
} else if (c == '\n') {
break;
} else {
- p_err("unrecognized character at pos %td in template '%s'",
- src - template - 1, template);
+ p_err("unrecognized character at pos %td in template '%s': '%c'",
+ src - template - 1, template, c);
free(s);
exit(-1);
}
@@ -406,7 +406,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
}
bpf_object__for_each_map(map, obj) {
- const char * ident;
+ const char *ident;
ident = get_map_ident(map);
if (!ident)
@@ -787,6 +787,9 @@ static int do_skeleton(int argc, char **argv)
free(obj); \n\
} \n\
\n\
+ static inline const void * \n\
+ %1$s__elf_bytes(size_t *sz); \n\
+ \n\
static inline int \n\
%1$s__create_skeleton(struct %1$s *obj); \n\
\n\
@@ -943,25 +946,31 @@ static int do_skeleton(int argc, char **argv)
codegen("\
\n\
\n\
- s->data_sz = %d; \n\
- s->data = (void *)\"\\ \n\
- ",
- file_sz);
-
- /* embed contents of BPF object file */
- print_hex(obj_data, file_sz);
-
- codegen("\
+ s->data = (void *)%2$s__elf_bytes(&s->data_sz); \n\
\n\
- \"; \n\
\n\
return 0; \n\
err: \n\
bpf_object__destroy_skeleton(s); \n\
return -ENOMEM; \n\
} \n\
- \n\
- #endif /* %s */ \n\
+ \n\
+ static inline const void * \n\
+ %2$s__elf_bytes(size_t *sz) \n\
+ { \n\
+ *sz = %1$d; \n\
+ return (const void *)\"\\ \n\
+ "
+ , file_sz, obj_name);
+
+ /* embed contents of BPF object file */
+ print_hex(obj_data, file_sz);
+
+ codegen("\
+ \n\
+ \"; \n\
+ } \n\
+ #endif /* %s */ \n\
",
header_guard);
err = 0;
--
2.30.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 bpf-next 3/3] selftests/bpf: Add checks for X__elf_bytes skeleton helper
2021-09-01 19:44 [PATCH v2 bpf-next 0/3] Bpf skeleton helper method Matt Smith
2021-09-01 19:44 ` [PATCH v2 bpf-next 1/3] libbpf: Change bpf_object_skelecton data field to const void* Matt Smith
2021-09-01 19:44 ` [PATCH v2 bpf-next 2/3] bpftool: Provide a helper method for accessing bpf binary data Matt Smith
@ 2021-09-01 19:44 ` Matt Smith
2021-09-08 1:02 ` [PATCH v2 bpf-next 0/3] Bpf skeleton helper method Andrii Nakryiko
3 siblings, 0 replies; 5+ messages in thread
From: Matt Smith @ 2021-09-01 19:44 UTC (permalink / raw)
To: bpf, ast, andriin, daniel, kernel-team; +Cc: Matt Smith
This patch adds two checks for the X__elf_bytes bpf skeleton helper
method. The first asserts that the pointer returned from the helper
method is valid, the second asserts that the provided size pointer is
set.
Signed-off-by: Matt Smith <alastorze@fb.com>
---
tools/testing/selftests/bpf/prog_tests/skeleton.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/skeleton.c b/tools/testing/selftests/bpf/prog_tests/skeleton.c
index f6f130c99b8c..963a4f5f596d 100644
--- a/tools/testing/selftests/bpf/prog_tests/skeleton.c
+++ b/tools/testing/selftests/bpf/prog_tests/skeleton.c
@@ -18,6 +18,9 @@ void test_skeleton(void)
struct test_skeleton__data *data;
struct test_skeleton__rodata *rodata;
struct test_skeleton__kconfig *kcfg;
+ const void *eb;
+ size_t ebs_val = 0;
+ size_t *ebs = &ebs_val;
skel = test_skeleton__open();
if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
@@ -91,6 +94,10 @@ void test_skeleton(void)
CHECK(bss->kern_ver != kcfg->LINUX_KERNEL_VERSION, "ext2",
"got %d != exp %d\n", bss->kern_ver, kcfg->LINUX_KERNEL_VERSION);
+ eb = test_skeleton__elf_bytes(ebs);
+ ASSERT_OK_PTR(eb, "elf_bytes_not_null");
+ ASSERT_NEQ(*ebs, 0, "elf_bytes_size_not_zero");
+
cleanup:
test_skeleton__destroy(skel);
}
--
2.30.2
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 bpf-next 0/3] Bpf skeleton helper method
2021-09-01 19:44 [PATCH v2 bpf-next 0/3] Bpf skeleton helper method Matt Smith
` (2 preceding siblings ...)
2021-09-01 19:44 ` [PATCH v2 bpf-next 3/3] selftests/bpf: Add checks for X__elf_bytes skeleton helper Matt Smith
@ 2021-09-08 1:02 ` Andrii Nakryiko
3 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2021-09-08 1:02 UTC (permalink / raw)
To: Matt Smith; +Cc: bpf, Alexei Starovoitov, andriin, Daniel Borkmann, Kernel Team
On Wed, Sep 1, 2021 at 1:25 PM Matt Smith <alastorze@fb.com> wrote:
>
> This patch series changes the type of bpf_object_skeleton->data
> to const void * and provides a helper method X__elf_bytes(size_t *sz)
> for accessing the raw binary data of the compiled embedded BPF object.
>
> The type change enforces the previously implied behavior of immutability
> for this field while casting it to (void *) before assignment allows
> for compiling with previous versions of the libbpf headers without
> compiler warnings.
>
> The helper method allows easier access to the BPF binary object data
> and is leveraged to populate the skeleton field. The inclusion of
> this helper method will allow users to get access to the data without
> needing to populate an entire skeleton first.
>
> Checks are added in the third patch to validate the behavior of the
> added method
>
> Matt Smith (3):
> libbpf: Change bpf_object_skelecton data field to const void*
> bpftool: Provide a helper method for accessing bpf binary data
> selftests/bpf: Add checks for X__elf_bytes skeleton helper
>
> tools/bpf/bpftool/gen.c | 39 ++++++++++++-------
> tools/lib/bpf/libbpf.h | 2 +-
> .../selftests/bpf/prog_tests/skeleton.c | 7 ++++
> 3 files changed, 32 insertions(+), 16 deletions(-)
>
> --
> 2.30.2
>
Fixed \n\ alignment and made a few small tweaks. Applied to bpf-next, thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread