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>
22 #include "bpf/libbpf_internal.h"
23 #include "json_writer.h"
27 #define MAX_OBJ_NAME_LEN 64
29 static void sanitize_identifier(char *name)
33 for (i = 0; name[i]; i++)
34 if (!isalnum(name[i]) && name[i] != '_')
38 static bool str_has_suffix(const char *str, const char *suffix)
40 size_t i, n1 = strlen(str), n2 = strlen(suffix);
45 for (i = 0; i < n2; i++) {
46 if (str[n1 - i - 1] != suffix[n2 - i - 1])
53 static void get_obj_name(char *name, const char *file)
55 /* Using basename() GNU version which doesn't modify arg. */
56 strncpy(name, basename(file), MAX_OBJ_NAME_LEN - 1);
57 name[MAX_OBJ_NAME_LEN - 1] = '\0';
58 if (str_has_suffix(name, ".o"))
59 name[strlen(name) - 2] = '\0';
60 sanitize_identifier(name);
63 static void get_header_guard(char *guard, const char *obj_name)
67 sprintf(guard, "__%s_SKEL_H__", obj_name);
68 for (i = 0; guard[i]; i++)
69 guard[i] = toupper(guard[i]);
72 static const char *get_map_ident(const struct bpf_map *map)
74 const char *name = bpf_map__name(map);
76 if (!bpf_map__is_internal(map))
79 if (str_has_suffix(name, ".data"))
81 else if (str_has_suffix(name, ".rodata"))
83 else if (str_has_suffix(name, ".bss"))
85 else if (str_has_suffix(name, ".kconfig"))
91 static void codegen_btf_dump_printf(void *ct, const char *fmt, va_list args)
96 static int codegen_datasec_def(struct bpf_object *obj,
99 const struct btf_type *sec,
100 const char *obj_name)
102 const char *sec_name = btf__name_by_offset(btf, sec->name_off);
103 const struct btf_var_secinfo *sec_var = btf_var_secinfos(sec);
104 int i, err, off = 0, pad_cnt = 0, vlen = btf_vlen(sec);
105 const char *sec_ident;
108 if (strcmp(sec_name, ".data") == 0)
110 else if (strcmp(sec_name, ".bss") == 0)
112 else if (strcmp(sec_name, ".rodata") == 0)
113 sec_ident = "rodata";
114 else if (strcmp(sec_name, ".kconfig") == 0)
115 sec_ident = "kconfig";
119 printf(" struct %s__%s {\n", obj_name, sec_ident);
120 for (i = 0; i < vlen; i++, sec_var++) {
121 const struct btf_type *var = btf__type_by_id(btf, sec_var->type);
122 const char *var_name = btf__name_by_offset(btf, var->name_off);
123 DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts,
124 .field_name = var_ident,
127 int need_off = sec_var->offset, align_off, align;
128 __u32 var_type_id = var->type;
129 const struct btf_type *t;
131 t = btf__type_by_id(btf, var_type_id);
132 while (btf_is_mod(t)) {
133 var_type_id = t->type;
134 t = btf__type_by_id(btf, var_type_id);
137 if (off > need_off) {
138 p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n",
139 sec_name, i, need_off, off);
143 align = btf__align_of(btf, var->type);
145 p_err("Failed to determine alignment of variable '%s': %d",
150 align_off = (off + align - 1) / align * align;
151 if (align_off != need_off) {
152 printf("\t\tchar __pad%d[%d];\n",
153 pad_cnt, need_off - off);
157 /* sanitize variable name, e.g., for static vars inside
158 * a function, it's name is '<function name>.<variable name>',
159 * which we'll turn into a '<function name>_<variable name>'
162 strncat(var_ident, var_name, sizeof(var_ident) - 1);
163 sanitize_identifier(var_ident);
166 err = btf_dump__emit_type_decl(d, var_type_id, &opts);
171 off = sec_var->offset + sec_var->size;
173 printf(" } *%s;\n", sec_ident);
177 static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
179 struct btf *btf = bpf_object__btf(obj);
180 int n = btf__get_nr_types(btf);
184 d = btf_dump__new(btf, NULL, NULL, codegen_btf_dump_printf);
188 for (i = 1; i <= n; i++) {
189 const struct btf_type *t = btf__type_by_id(btf, i);
191 if (!btf_is_datasec(t))
194 err = codegen_datasec_def(obj, btf, d, t, obj_name);
203 static int codegen(const char *template, ...)
205 const char *src, *end;
206 int skip_tabs = 0, n;
211 n = strlen(template);
218 /* find out "baseline" indentation to skip */
219 while ((c = *src++)) {
222 } else if (c == '\n') {
225 p_err("unrecognized character at pos %td in template '%s'",
226 src - template - 1, template);
233 /* skip baseline indentation tabs */
234 for (n = skip_tabs; n > 0; n--, src++) {
236 p_err("not enough tabs at pos %td in template '%s'",
237 src - template - 1, template);
242 /* trim trailing whitespace */
243 end = strchrnul(src, '\n');
244 for (n = end - src; n > 0 && isspace(src[n - 1]); n--)
250 src = *end ? end + 1 : end;
254 /* print out using adjusted template */
255 va_start(args, template);
256 n = vprintf(s, args);
263 static int do_skeleton(int argc, char **argv)
265 char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
266 size_t i, map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz;
267 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
268 char obj_name[MAX_OBJ_NAME_LEN], *obj_data;
269 struct bpf_object *obj = NULL;
270 const char *file, *ident;
271 struct bpf_program *prog;
272 int fd, len, err = -1;
284 p_err("extra unknown arguments");
288 if (stat(file, &st)) {
289 p_err("failed to stat() %s: %s", file, strerror(errno));
292 file_sz = st.st_size;
293 mmap_sz = roundup(file_sz, sysconf(_SC_PAGE_SIZE));
294 fd = open(file, O_RDONLY);
296 p_err("failed to open() %s: %s", file, strerror(errno));
299 obj_data = mmap(NULL, mmap_sz, PROT_READ, MAP_PRIVATE, fd, 0);
300 if (obj_data == MAP_FAILED) {
302 p_err("failed to mmap() %s: %s", file, strerror(errno));
305 get_obj_name(obj_name, file);
306 opts.object_name = obj_name;
307 obj = bpf_object__open_mem(obj_data, file_sz, &opts);
310 p_err("failed to open BPF object file: %ld", PTR_ERR(obj));
314 bpf_object__for_each_map(map, obj) {
315 ident = get_map_ident(map);
317 p_err("ignoring unrecognized internal map '%s'...",
323 bpf_object__for_each_program(prog, obj) {
327 get_header_guard(header_guard, obj_name);
330 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ \n\
332 /* THIS FILE IS AUTOGENERATED! */ \n\
336 #include <stdlib.h> \n\
337 #include <bpf/libbpf.h> \n\
340 struct bpf_object_skeleton *skeleton; \n\
341 struct bpf_object *obj; \n\
343 obj_name, header_guard
347 printf("\tstruct {\n");
348 bpf_object__for_each_map(map, obj) {
349 ident = get_map_ident(map);
352 printf("\t\tstruct bpf_map *%s;\n", ident);
354 printf("\t} maps;\n");
358 printf("\tstruct {\n");
359 bpf_object__for_each_program(prog, obj) {
360 printf("\t\tstruct bpf_program *%s;\n",
361 bpf_program__name(prog));
363 printf("\t} progs;\n");
364 printf("\tstruct {\n");
365 bpf_object__for_each_program(prog, obj) {
366 printf("\t\tstruct bpf_link *%s;\n",
367 bpf_program__name(prog));
369 printf("\t} links;\n");
372 btf = bpf_object__btf(obj);
374 err = codegen_datasecs(obj, obj_name);
384 %1$s__destroy(struct %1$s *obj) \n\
388 if (obj->skeleton) \n\
389 bpf_object__destroy_skeleton(obj->skeleton);\n\
393 static inline int \n\
394 %1$s__create_skeleton(struct %1$s *obj); \n\
396 static inline struct %1$s * \n\
397 %1$s__open_opts(const struct bpf_object_open_opts *opts) \n\
399 struct %1$s *obj; \n\
401 obj = (typeof(obj))calloc(1, sizeof(*obj)); \n\
404 if (%1$s__create_skeleton(obj)) \n\
406 if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
411 %1$s__destroy(obj); \n\
415 static inline struct %1$s * \n\
418 return %1$s__open_opts(NULL); \n\
421 static inline int \n\
422 %1$s__load(struct %1$s *obj) \n\
424 return bpf_object__load_skeleton(obj->skeleton); \n\
427 static inline struct %1$s * \n\
428 %1$s__open_and_load(void) \n\
430 struct %1$s *obj; \n\
432 obj = %1$s__open(); \n\
435 if (%1$s__load(obj)) { \n\
436 %1$s__destroy(obj); \n\
442 static inline int \n\
443 %1$s__attach(struct %1$s *obj) \n\
445 return bpf_object__attach_skeleton(obj->skeleton); \n\
448 static inline void \n\
449 %1$s__detach(struct %1$s *obj) \n\
451 return bpf_object__detach_skeleton(obj->skeleton); \n\
460 static inline int \n\
461 %1$s__create_skeleton(struct %1$s *obj) \n\
463 struct bpf_object_skeleton *s; \n\
465 s = (typeof(s))calloc(1, sizeof(*s)); \n\
468 obj->skeleton = s; \n\
470 s->sz = sizeof(*s); \n\
471 s->name = \"%1$s\"; \n\
472 s->obj = &obj->obj; \n\
481 s->map_cnt = %zu; \n\
482 s->map_skel_sz = sizeof(*s->maps); \n\
483 s->maps = (typeof(s->maps))calloc(s->map_cnt, s->map_skel_sz);\n\
490 bpf_object__for_each_map(map, obj) {
491 ident = get_map_ident(map);
499 s->maps[%zu].name = \"%s\"; \n\
500 s->maps[%zu].map = &obj->maps.%s; \n\
502 i, bpf_map__name(map), i, ident);
503 /* memory-mapped internal maps */
504 if (bpf_map__is_internal(map) &&
505 (bpf_map__def(map)->map_flags & BPF_F_MMAPABLE)) {
506 printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n",
517 s->prog_cnt = %zu; \n\
518 s->prog_skel_sz = sizeof(*s->progs); \n\
519 s->progs = (typeof(s->progs))calloc(s->prog_cnt, s->prog_skel_sz);\n\
526 bpf_object__for_each_program(prog, obj) {
530 s->progs[%1$zu].name = \"%2$s\"; \n\
531 s->progs[%1$zu].prog = &obj->progs.%2$s;\n\
532 s->progs[%1$zu].link = &obj->links.%2$s;\n\
534 i, bpf_program__name(prog));
542 s->data = (void *)\"\\ \n\
546 /* embed contents of BPF object file */
547 for (i = 0, len = 0; i < file_sz; i++) {
548 int w = obj_data[i] ? 4 : 2;
558 printf("\\x%02x", (unsigned char)obj_data[i]);
567 bpf_object__destroy_skeleton(s); \n\
576 bpf_object__close(obj);
578 munmap(obj_data, mmap_sz);
583 static int do_help(int argc, char **argv)
586 jsonw_null(json_wtr);
591 "Usage: %1$s %2$s skeleton FILE\n"
594 " " HELP_SPEC_OPTIONS "\n"
601 static const struct cmd cmds[] = {
602 { "skeleton", do_skeleton },
607 int do_gen(int argc, char **argv)
609 return cmd_select(cmds, argc, argv, do_help);