Skip to content

Commit 28d743f

Browse files
Alexei Starovoitovborkmann
authored andcommitted
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: Alexei Starovoitov <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6fe65f1 commit 28d743f

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

tools/bpf/bpftool/gen.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
472472
continue;
473473
if (bpf_map__is_internal(map) &&
474474
(bpf_map__map_flags(map) & BPF_F_MMAPABLE))
475-
printf("\tmunmap(skel->%1$s, %2$zd);\n",
475+
printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n",
476476
ident, bpf_map_mmap_sz(map));
477477
codegen("\
478478
\n\
@@ -481,7 +481,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
481481
}
482482
codegen("\
483483
\n\
484-
free(skel); \n\
484+
skel_free(skel); \n\
485485
} \n\
486486
",
487487
obj_name);
@@ -525,7 +525,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
525525
{ \n\
526526
struct %1$s *skel; \n\
527527
\n\
528-
skel = calloc(sizeof(*skel), 1); \n\
528+
skel = skel_alloc(sizeof(*skel)); \n\
529529
if (!skel) \n\
530530
goto cleanup; \n\
531531
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
543543
continue;
544544

545545
codegen("\
546-
\n\
547-
skel->%1$s = \n\
548-
mmap(NULL, %2$zd, PROT_READ | PROT_WRITE,\n\
549-
MAP_SHARED | MAP_ANONYMOUS, -1, 0); \n\
550-
if (skel->%1$s == (void *) -1) \n\
551-
goto cleanup; \n\
552-
memcpy(skel->%1$s, (void *)\"\\ \n\
553-
", ident, bpf_map_mmap_sz(map));
546+
\n\
547+
skel->%1$s = skel_prep_map_data((void *)\"\\ \n\
548+
", ident);
554549
mmap_data = bpf_map__initial_value(map, &mmap_size);
555550
print_hex(mmap_data, mmap_size);
556-
printf("\", %2$zd);\n"
557-
"\tskel->maps.%1$s.initial_value = (__u64)(long)skel->%1$s;\n",
558-
ident, mmap_size);
551+
codegen("\
552+
\n\
553+
\", %1$zd, %2$zd); \n\
554+
if (!skel->%3$s) \n\
555+
goto cleanup; \n\
556+
skel->maps.%3$s.initial_value = (__u64) (long) skel->%3$s;\n\
557+
", bpf_map_mmap_sz(map), mmap_size, ident);
559558
}
560559
codegen("\
561560
\n\
@@ -611,9 +610,13 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
611610
else
612611
mmap_flags = "PROT_READ | PROT_WRITE";
613612

614-
printf("\tskel->%1$s =\n"
615-
"\t\tmmap(skel->%1$s, %2$zd, %3$s, MAP_SHARED | MAP_FIXED,\n"
616-
"\t\t\tskel->maps.%1$s.map_fd, 0);\n",
613+
codegen("\
614+
\n\
615+
skel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value, \n\
616+
%2$zd, %3$s, skel->maps.%1$s.map_fd);\n\
617+
if (!skel->%1$s) \n\
618+
return -ENOMEM; \n\
619+
",
617620
ident, bpf_map_mmap_sz(map), mmap_flags);
618621
}
619622
codegen("\
@@ -751,8 +754,6 @@ static int do_skeleton(int argc, char **argv)
751754
#ifndef %2$s \n\
752755
#define %2$s \n\
753756
\n\
754-
#include <stdlib.h> \n\
755-
#include <bpf/bpf.h> \n\
756757
#include <bpf/skel_internal.h> \n\
757758
\n\
758759
struct %1$s { \n\

0 commit comments

Comments
 (0)