Commit 28d743f6 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Daniel Borkmann

bpftool: Generalize light skeleton generation.

Generealize light skeleton by hiding mmap details in skel_internal.h
In this form generated lskel.h is usable both by user space and by the kernel.

Note that previously #include <bpf/bpf.h> was in *.lskel.h file.
To avoid #ifdef-s in a generated lskel.h the include of bpf.h is moved
to skel_internal.h, but skel_internal.h is also used by gen_loader.c
which is part of libbpf. Therefore skel_internal.h does #include "bpf.h"
in case of user space, so gen_loader.c and lskel.h have necessary definitions.
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220209232001.27490-4-alexei.starovoitov@gmail.com
parent 6fe65f1b
...@@ -472,7 +472,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name) ...@@ -472,7 +472,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
continue; continue;
if (bpf_map__is_internal(map) && if (bpf_map__is_internal(map) &&
(bpf_map__map_flags(map) & BPF_F_MMAPABLE)) (bpf_map__map_flags(map) & BPF_F_MMAPABLE))
printf("\tmunmap(skel->%1$s, %2$zd);\n", printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
ident, bpf_map_mmap_sz(map)); ident, bpf_map_mmap_sz(map));
codegen("\ codegen("\
\n\ \n\
...@@ -481,7 +481,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name) ...@@ -481,7 +481,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
} }
codegen("\ codegen("\
\n\ \n\
free(skel); \n\ skel_free(skel); \n\
} \n\ } \n\
", ",
obj_name); obj_name);
...@@ -525,7 +525,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h ...@@ -525,7 +525,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
{ \n\ { \n\
struct %1$s *skel; \n\ struct %1$s *skel; \n\
\n\ \n\
skel = calloc(sizeof(*skel), 1); \n\ skel = skel_alloc(sizeof(*skel)); \n\
if (!skel) \n\ if (!skel) \n\
goto cleanup; \n\ goto cleanup; \n\
skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\ skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\
...@@ -543,19 +543,18 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h ...@@ -543,19 +543,18 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
continue; continue;
codegen("\ codegen("\
\n\ \n\
skel->%1$s = \n\ skel->%1$s = skel_prep_map_data((void *)\"\\ \n\
mmap(NULL, %2$zd, PROT_READ | PROT_WRITE,\n\ ", ident);
MAP_SHARED | MAP_ANONYMOUS, -1, 0); \n\
if (skel->%1$s == (void *) -1) \n\
goto cleanup; \n\
memcpy(skel->%1$s, (void *)\"\\ \n\
", ident, bpf_map_mmap_sz(map));
mmap_data = bpf_map__initial_value(map, &mmap_size); mmap_data = bpf_map__initial_value(map, &mmap_size);
print_hex(mmap_data, mmap_size); print_hex(mmap_data, mmap_size);
printf("\", %2$zd);\n" codegen("\
"\tskel->maps.%1$s.initial_value = (__u64)(long)skel->%1$s;\n", \n\
ident, mmap_size); \", %1$zd, %2$zd); \n\
if (!skel->%3$s) \n\
goto cleanup; \n\
skel->maps.%3$s.initial_value = (__u64) (long) skel->%3$s;\n\
", bpf_map_mmap_sz(map), mmap_size, ident);
} }
codegen("\ codegen("\
\n\ \n\
...@@ -611,9 +610,13 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h ...@@ -611,9 +610,13 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
else else
mmap_flags = "PROT_READ | PROT_WRITE"; mmap_flags = "PROT_READ | PROT_WRITE";
printf("\tskel->%1$s =\n" codegen("\
"\t\tmmap(skel->%1$s, %2$zd, %3$s, MAP_SHARED | MAP_FIXED,\n" \n\
"\t\t\tskel->maps.%1$s.map_fd, 0);\n", skel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value, \n\
%2$zd, %3$s, skel->maps.%1$s.map_fd);\n\
if (!skel->%1$s) \n\
return -ENOMEM; \n\
",
ident, bpf_map_mmap_sz(map), mmap_flags); ident, bpf_map_mmap_sz(map), mmap_flags);
} }
codegen("\ codegen("\
...@@ -751,8 +754,6 @@ static int do_skeleton(int argc, char **argv) ...@@ -751,8 +754,6 @@ static int do_skeleton(int argc, char **argv)
#ifndef %2$s \n\ #ifndef %2$s \n\
#define %2$s \n\ #define %2$s \n\
\n\ \n\
#include <stdlib.h> \n\
#include <bpf/bpf.h> \n\
#include <bpf/skel_internal.h> \n\ #include <bpf/skel_internal.h> \n\
\n\ \n\
struct %1$s { \n\ struct %1$s { \n\
......
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