1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Facebook */
10 #include <linux/err.h>
16 #include <bpf/libbpf.h>
17 #include <sys/types.h>
23 #include "bpf/libbpf_internal.h"
24 #include "json_writer.h"
28 #define MAX_OBJ_NAME_LEN 64
30 static void sanitize_identifier(char *name
)
34 for (i
= 0; name
[i
]; i
++)
35 if (!isalnum(name
[i
]) && name
[i
] != '_')
39 static bool str_has_suffix(const char *str
, const char *suffix
)
41 size_t i
, n1
= strlen(str
), n2
= strlen(suffix
);
46 for (i
= 0; i
< n2
; i
++) {
47 if (str
[n1
- i
- 1] != suffix
[n2
- i
- 1])
54 static void get_obj_name(char *name
, const char *file
)
56 /* Using basename() GNU version which doesn't modify arg. */
57 strncpy(name
, basename(file
), MAX_OBJ_NAME_LEN
- 1);
58 name
[MAX_OBJ_NAME_LEN
- 1] = '\0';
59 if (str_has_suffix(name
, ".o"))
60 name
[strlen(name
) - 2] = '\0';
61 sanitize_identifier(name
);
64 static void get_header_guard(char *guard
, const char *obj_name
)
68 sprintf(guard
, "__%s_SKEL_H__", obj_name
);
69 for (i
= 0; guard
[i
]; i
++)
70 guard
[i
] = toupper(guard
[i
]);
73 static const char *get_map_ident(const struct bpf_map
*map
)
75 const char *name
= bpf_map__name(map
);
77 if (!bpf_map__is_internal(map
))
80 if (str_has_suffix(name
, ".data"))
82 else if (str_has_suffix(name
, ".rodata"))
84 else if (str_has_suffix(name
, ".bss"))
86 else if (str_has_suffix(name
, ".kconfig"))
92 static void codegen_btf_dump_printf(void *ct
, const char *fmt
, va_list args
)
97 static int codegen_datasec_def(struct bpf_object
*obj
,
100 const struct btf_type
*sec
,
101 const char *obj_name
)
103 const char *sec_name
= btf__name_by_offset(btf
, sec
->name_off
);
104 const struct btf_var_secinfo
*sec_var
= btf_var_secinfos(sec
);
105 int i
, err
, off
= 0, pad_cnt
= 0, vlen
= btf_vlen(sec
);
106 const char *sec_ident
;
109 if (strcmp(sec_name
, ".data") == 0)
111 else if (strcmp(sec_name
, ".bss") == 0)
113 else if (strcmp(sec_name
, ".rodata") == 0)
114 sec_ident
= "rodata";
115 else if (strcmp(sec_name
, ".kconfig") == 0)
116 sec_ident
= "kconfig";
120 printf(" struct %s__%s {\n", obj_name
, sec_ident
);
121 for (i
= 0; i
< vlen
; i
++, sec_var
++) {
122 const struct btf_type
*var
= btf__type_by_id(btf
, sec_var
->type
);
123 const char *var_name
= btf__name_by_offset(btf
, var
->name_off
);
124 DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts
, opts
,
125 .field_name
= var_ident
,
128 int need_off
= sec_var
->offset
, align_off
, align
;
129 __u32 var_type_id
= var
->type
;
130 const struct btf_type
*t
;
132 t
= btf__type_by_id(btf
, var_type_id
);
133 while (btf_is_mod(t
)) {
134 var_type_id
= t
->type
;
135 t
= btf__type_by_id(btf
, var_type_id
);
138 if (off
> need_off
) {
139 p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n",
140 sec_name
, i
, need_off
, off
);
144 align
= btf__align_of(btf
, var
->type
);
146 p_err("Failed to determine alignment of variable '%s': %d",
151 align_off
= (off
+ align
- 1) / align
* align
;
152 if (align_off
!= need_off
) {
153 printf("\t\tchar __pad%d[%d];\n",
154 pad_cnt
, need_off
- off
);
158 /* sanitize variable name, e.g., for static vars inside
159 * a function, it's name is '<function name>.<variable name>',
160 * which we'll turn into a '<function name>_<variable name>'
163 strncat(var_ident
, var_name
, sizeof(var_ident
) - 1);
164 sanitize_identifier(var_ident
);
167 err
= btf_dump__emit_type_decl(d
, var_type_id
, &opts
);
172 off
= sec_var
->offset
+ sec_var
->size
;
174 printf(" } *%s;\n", sec_ident
);
178 static int codegen_datasecs(struct bpf_object
*obj
, const char *obj_name
)
180 struct btf
*btf
= bpf_object__btf(obj
);
181 int n
= btf__get_nr_types(btf
);
185 d
= btf_dump__new(btf
, NULL
, NULL
, codegen_btf_dump_printf
);
189 for (i
= 1; i
<= n
; i
++) {
190 const struct btf_type
*t
= btf__type_by_id(btf
, i
);
192 if (!btf_is_datasec(t
))
195 err
= codegen_datasec_def(obj
, btf
, d
, t
, obj_name
);
204 static int codegen(const char *template, ...)
206 const char *src
, *end
;
207 int skip_tabs
= 0, n
;
212 n
= strlen(template);
219 /* find out "baseline" indentation to skip */
220 while ((c
= *src
++)) {
223 } else if (c
== '\n') {
226 p_err("unrecognized character at pos %td in template '%s'",
227 src
- template - 1, template);
234 /* skip baseline indentation tabs */
235 for (n
= skip_tabs
; n
> 0; n
--, src
++) {
237 p_err("not enough tabs at pos %td in template '%s'",
238 src
- template - 1, template);
243 /* trim trailing whitespace */
244 end
= strchrnul(src
, '\n');
245 for (n
= end
- src
; n
> 0 && isspace(src
[n
- 1]); n
--)
251 src
= *end
? end
+ 1 : end
;
255 /* print out using adjusted template */
256 va_start(args
, template);
257 n
= vprintf(s
, args
);
264 static int do_skeleton(int argc
, char **argv
)
266 char header_guard
[MAX_OBJ_NAME_LEN
+ sizeof("__SKEL_H__")];
267 size_t i
, map_cnt
= 0, prog_cnt
= 0, file_sz
, mmap_sz
;
268 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, opts
);
269 char obj_name
[MAX_OBJ_NAME_LEN
], *obj_data
;
270 struct bpf_object
*obj
= NULL
;
271 const char *file
, *ident
;
272 struct bpf_program
*prog
;
273 int fd
, len
, err
= -1;
285 p_err("extra unknown arguments");
289 if (stat(file
, &st
)) {
290 p_err("failed to stat() %s: %s", file
, strerror(errno
));
293 file_sz
= st
.st_size
;
294 mmap_sz
= roundup(file_sz
, sysconf(_SC_PAGE_SIZE
));
295 fd
= open(file
, O_RDONLY
);
297 p_err("failed to open() %s: %s", file
, strerror(errno
));
300 obj_data
= mmap(NULL
, mmap_sz
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
301 if (obj_data
== MAP_FAILED
) {
303 p_err("failed to mmap() %s: %s", file
, strerror(errno
));
306 get_obj_name(obj_name
, file
);
307 opts
.object_name
= obj_name
;
308 obj
= bpf_object__open_mem(obj_data
, file_sz
, &opts
);
311 p_err("failed to open BPF object file: %ld", PTR_ERR(obj
));
315 bpf_object__for_each_map(map
, obj
) {
316 ident
= get_map_ident(map
);
318 p_err("ignoring unrecognized internal map '%s'...",
324 bpf_object__for_each_program(prog
, obj
) {
328 get_header_guard(header_guard
, obj_name
);
331 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ \n\
333 /* THIS FILE IS AUTOGENERATED! */ \n\
337 #include <stdlib.h> \n\
338 #include <bpf/libbpf.h> \n\
341 struct bpf_object_skeleton *skeleton; \n\
342 struct bpf_object *obj; \n\
344 obj_name
, header_guard
348 printf("\tstruct {\n");
349 bpf_object__for_each_map(map
, obj
) {
350 ident
= get_map_ident(map
);
353 printf("\t\tstruct bpf_map *%s;\n", ident
);
355 printf("\t} maps;\n");
359 printf("\tstruct {\n");
360 bpf_object__for_each_program(prog
, obj
) {
361 printf("\t\tstruct bpf_program *%s;\n",
362 bpf_program__name(prog
));
364 printf("\t} progs;\n");
365 printf("\tstruct {\n");
366 bpf_object__for_each_program(prog
, obj
) {
367 printf("\t\tstruct bpf_link *%s;\n",
368 bpf_program__name(prog
));
370 printf("\t} links;\n");
373 btf
= bpf_object__btf(obj
);
375 err
= codegen_datasecs(obj
, obj_name
);
385 %1$s__destroy(struct %1$s *obj) \n\
389 if (obj->skeleton) \n\
390 bpf_object__destroy_skeleton(obj->skeleton);\n\
394 static inline int \n\
395 %1$s__create_skeleton(struct %1$s *obj); \n\
397 static inline struct %1$s * \n\
398 %1$s__open_opts(const struct bpf_object_open_opts *opts) \n\
400 struct %1$s *obj; \n\
402 obj = (typeof(obj))calloc(1, sizeof(*obj)); \n\
405 if (%1$s__create_skeleton(obj)) \n\
407 if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
412 %1$s__destroy(obj); \n\
416 static inline struct %1$s * \n\
419 return %1$s__open_opts(NULL); \n\
422 static inline int \n\
423 %1$s__load(struct %1$s *obj) \n\
425 return bpf_object__load_skeleton(obj->skeleton); \n\
428 static inline struct %1$s * \n\
429 %1$s__open_and_load(void) \n\
431 struct %1$s *obj; \n\
433 obj = %1$s__open(); \n\
436 if (%1$s__load(obj)) { \n\
437 %1$s__destroy(obj); \n\
443 static inline int \n\
444 %1$s__attach(struct %1$s *obj) \n\
446 return bpf_object__attach_skeleton(obj->skeleton); \n\
449 static inline void \n\
450 %1$s__detach(struct %1$s *obj) \n\
452 return bpf_object__detach_skeleton(obj->skeleton); \n\
461 static inline int \n\
462 %1$s__create_skeleton(struct %1$s *obj) \n\
464 struct bpf_object_skeleton *s; \n\
466 s = (typeof(s))calloc(1, sizeof(*s)); \n\
469 obj->skeleton = s; \n\
471 s->sz = sizeof(*s); \n\
472 s->name = \"%1$s\"; \n\
473 s->obj = &obj->obj; \n\
482 s->map_cnt = %zu; \n\
483 s->map_skel_sz = sizeof(*s->maps); \n\
484 s->maps = (typeof(s->maps))calloc(s->map_cnt, s->map_skel_sz);\n\
491 bpf_object__for_each_map(map
, obj
) {
492 ident
= get_map_ident(map
);
500 s->maps[%zu].name = \"%s\"; \n\
501 s->maps[%zu].map = &obj->maps.%s; \n\
503 i
, bpf_map__name(map
), i
, ident
);
504 /* memory-mapped internal maps */
505 if (bpf_map__is_internal(map
) &&
506 (bpf_map__def(map
)->map_flags
& BPF_F_MMAPABLE
)) {
507 printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n",
518 s->prog_cnt = %zu; \n\
519 s->prog_skel_sz = sizeof(*s->progs); \n\
520 s->progs = (typeof(s->progs))calloc(s->prog_cnt, s->prog_skel_sz);\n\
527 bpf_object__for_each_program(prog
, obj
) {
531 s->progs[%1$zu].name = \"%2$s\"; \n\
532 s->progs[%1$zu].prog = &obj->progs.%2$s;\n\
533 s->progs[%1$zu].link = &obj->links.%2$s;\n\
535 i
, bpf_program__name(prog
));
543 s->data = (void *)\"\\ \n\
547 /* embed contents of BPF object file */
548 for (i
= 0, len
= 0; i
< file_sz
; i
++) {
549 int w
= obj_data
[i
] ? 4 : 2;
559 printf("\\x%02x", (unsigned char)obj_data
[i
]);
568 bpf_object__destroy_skeleton(s); \n\
577 bpf_object__close(obj
);
579 munmap(obj_data
, mmap_sz
);
584 static int do_help(int argc
, char **argv
)
587 jsonw_null(json_wtr
);
592 "Usage: %1$s gen skeleton FILE\n"
595 " " HELP_SPEC_OPTIONS
"\n"
602 static const struct cmd cmds
[] = {
603 { "skeleton", do_skeleton
},
608 int do_gen(int argc
, char **argv
)
610 return cmd_select(cmds
, argc
, argv
, do_help
);