Commit d6be5947 authored by Andrii Nakryiko's avatar Andrii Nakryiko

Merge branch 'Bpf skeleton helper method'

Matt Smith says:

====================

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
====================
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents 03e601f4 980a1a4c
...@@ -238,8 +238,8 @@ static void codegen(const char *template, ...) ...@@ -238,8 +238,8 @@ static void codegen(const char *template, ...)
} else if (c == '\n') { } else if (c == '\n') {
break; break;
} else { } else {
p_err("unrecognized character at pos %td in template '%s'", p_err("unrecognized character at pos %td in template '%s': '%c'",
src - template - 1, template); src - template - 1, template, c);
free(s); free(s);
exit(-1); exit(-1);
} }
...@@ -406,7 +406,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name) ...@@ -406,7 +406,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
} }
bpf_object__for_each_map(map, obj) { bpf_object__for_each_map(map, obj) {
const char * ident; const char *ident;
ident = get_map_ident(map); ident = get_map_ident(map);
if (!ident) if (!ident)
...@@ -862,6 +862,8 @@ static int do_skeleton(int argc, char **argv) ...@@ -862,6 +862,8 @@ static int do_skeleton(int argc, char **argv)
codegen("\ codegen("\
\n\ \n\
\n\ \n\
static inline const void *%1$s__elf_bytes(size_t *sz); \n\
\n\
static inline int \n\ static inline int \n\
%1$s__create_skeleton(struct %1$s *obj) \n\ %1$s__create_skeleton(struct %1$s *obj) \n\
{ \n\ { \n\
...@@ -943,10 +945,20 @@ static int do_skeleton(int argc, char **argv) ...@@ -943,10 +945,20 @@ static int do_skeleton(int argc, char **argv)
codegen("\ codegen("\
\n\ \n\
\n\ \n\
s->data_sz = %d; \n\ s->data = (void *)%2$s__elf_bytes(&s->data_sz); \n\
s->data = (void *)\"\\ \n\ \n\
", return 0; \n\
file_sz); err: \n\
bpf_object__destroy_skeleton(s); \n\
return -ENOMEM; \n\
} \n\
\n\
static inline const void *%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 */ /* embed contents of BPF object file */
print_hex(obj_data, file_sz); print_hex(obj_data, file_sz);
...@@ -954,11 +966,6 @@ static int do_skeleton(int argc, char **argv) ...@@ -954,11 +966,6 @@ static int do_skeleton(int argc, char **argv)
codegen("\ codegen("\
\n\ \n\
\"; \n\ \"; \n\
\n\
return 0; \n\
err: \n\
bpf_object__destroy_skeleton(s); \n\
return -ENOMEM; \n\
} \n\ } \n\
\n\ \n\
#endif /* %s */ \n\ #endif /* %s */ \n\
......
...@@ -854,7 +854,7 @@ struct bpf_object_skeleton { ...@@ -854,7 +854,7 @@ struct bpf_object_skeleton {
size_t sz; /* size of this struct, for forward/backward compatibility */ size_t sz; /* size of this struct, for forward/backward compatibility */
const char *name; const char *name;
void *data; const void *data;
size_t data_sz; size_t data_sz;
struct bpf_object **obj; struct bpf_object **obj;
......
...@@ -18,6 +18,8 @@ void test_skeleton(void) ...@@ -18,6 +18,8 @@ void test_skeleton(void)
struct test_skeleton__data *data; struct test_skeleton__data *data;
struct test_skeleton__rodata *rodata; struct test_skeleton__rodata *rodata;
struct test_skeleton__kconfig *kcfg; struct test_skeleton__kconfig *kcfg;
const void *elf_bytes;
size_t elf_bytes_sz = 0;
skel = test_skeleton__open(); skel = test_skeleton__open();
if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
...@@ -91,6 +93,10 @@ void test_skeleton(void) ...@@ -91,6 +93,10 @@ void test_skeleton(void)
CHECK(bss->kern_ver != kcfg->LINUX_KERNEL_VERSION, "ext2", CHECK(bss->kern_ver != kcfg->LINUX_KERNEL_VERSION, "ext2",
"got %d != exp %d\n", bss->kern_ver, kcfg->LINUX_KERNEL_VERSION); "got %d != exp %d\n", bss->kern_ver, kcfg->LINUX_KERNEL_VERSION);
elf_bytes = test_skeleton__elf_bytes(&elf_bytes_sz);
ASSERT_OK_PTR(elf_bytes, "elf_bytes");
ASSERT_GE(elf_bytes_sz, 0, "elf_bytes_sz");
cleanup: cleanup:
test_skeleton__destroy(skel); test_skeleton__destroy(skel);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment