blob: f70088b4c19b953a5a24c0d09aa3b058fbaae9b2 [file] [log] [blame]
Andrii Nakryiko985ead42019-12-13 17:43:37 -08001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2019 Facebook */
3
4#ifndef _GNU_SOURCE
5#define _GNU_SOURCE
6#endif
7#include <ctype.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <linux/err.h>
11#include <stdbool.h>
12#include <stdio.h>
13#include <string.h>
14#include <unistd.h>
15#include <bpf.h>
16#include <libbpf.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <unistd.h>
20
21#include "btf.h"
22#include "libbpf_internal.h"
23#include "json_writer.h"
24#include "main.h"
25
26
27#define MAX_OBJ_NAME_LEN 64
28
29static void sanitize_identifier(char *name)
30{
31 int i;
32
33 for (i = 0; name[i]; i++)
34 if (!isalnum(name[i]) && name[i] != '_')
35 name[i] = '_';
36}
37
38static bool str_has_suffix(const char *str, const char *suffix)
39{
40 size_t i, n1 = strlen(str), n2 = strlen(suffix);
41
42 if (n1 < n2)
43 return false;
44
45 for (i = 0; i < n2; i++) {
46 if (str[n1 - i - 1] != suffix[n2 - i - 1])
47 return false;
48 }
49
50 return true;
51}
52
53static void get_obj_name(char *name, const char *file)
54{
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);
61}
62
63static void get_header_guard(char *guard, const char *obj_name)
64{
65 int i;
66
67 sprintf(guard, "__%s_SKEL_H__", obj_name);
68 for (i = 0; guard[i]; i++)
69 guard[i] = toupper(guard[i]);
70}
71
72static const char *get_map_ident(const struct bpf_map *map)
73{
74 const char *name = bpf_map__name(map);
75
76 if (!bpf_map__is_internal(map))
77 return name;
78
79 if (str_has_suffix(name, ".data"))
80 return "data";
81 else if (str_has_suffix(name, ".rodata"))
82 return "rodata";
83 else if (str_has_suffix(name, ".bss"))
84 return "bss";
Andrii Nakryiko2ad97d42019-12-13 17:47:09 -080085 else if (str_has_suffix(name, ".extern"))
86 return "externs"; /* extern is a C keyword */
Andrii Nakryiko985ead42019-12-13 17:43:37 -080087 else
88 return NULL;
89}
90
91static void codegen_btf_dump_printf(void *ct, const char *fmt, va_list args)
92{
93 vprintf(fmt, args);
94}
95
96static int codegen_datasec_def(struct bpf_object *obj,
97 struct btf *btf,
98 struct btf_dump *d,
99 const struct btf_type *sec,
100 const char *obj_name)
101{
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;
106 char var_ident[256];
107
108 if (strcmp(sec_name, ".data") == 0)
109 sec_ident = "data";
110 else if (strcmp(sec_name, ".bss") == 0)
111 sec_ident = "bss";
112 else if (strcmp(sec_name, ".rodata") == 0)
113 sec_ident = "rodata";
Andrii Nakryiko2ad97d42019-12-13 17:47:09 -0800114 else if (strcmp(sec_name, ".extern") == 0)
115 sec_ident = "externs"; /* extern is a C keyword */
Andrii Nakryiko985ead42019-12-13 17:43:37 -0800116 else
117 return 0;
118
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,
125 .indent_level = 2,
126 );
127 int need_off = sec_var->offset, align_off, align;
128 __u32 var_type_id = var->type;
129 const struct btf_type *t;
130
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);
135 }
136
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);
140 return -EINVAL;
141 }
142
143 align = btf__align_of(btf, var->type);
144 if (align <= 0) {
145 p_err("Failed to determine alignment of variable '%s': %d",
146 var_name, align);
147 return -EINVAL;
148 }
149
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);
154 pad_cnt++;
155 }
156
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>'
160 */
161 var_ident[0] = '\0';
162 strncat(var_ident, var_name, sizeof(var_ident) - 1);
163 sanitize_identifier(var_ident);
164
165 printf("\t\t");
166 err = btf_dump__emit_type_decl(d, var_type_id, &opts);
167 if (err)
168 return err;
169 printf(";\n");
170
171 off = sec_var->offset + sec_var->size;
172 }
173 printf(" } *%s;\n", sec_ident);
174 return 0;
175}
176
177static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
178{
179 struct btf *btf = bpf_object__btf(obj);
180 int n = btf__get_nr_types(btf);
181 struct btf_dump *d;
182 int i, err = 0;
183
184 d = btf_dump__new(btf, NULL, NULL, codegen_btf_dump_printf);
185 if (IS_ERR(d))
186 return PTR_ERR(d);
187
188 for (i = 1; i <= n; i++) {
189 const struct btf_type *t = btf__type_by_id(btf, i);
190
191 if (!btf_is_datasec(t))
192 continue;
193
194 err = codegen_datasec_def(obj, btf, d, t, obj_name);
195 if (err)
196 goto out;
197 }
198out:
199 btf_dump__free(d);
200 return err;
201}
202
203static int codegen(const char *template, ...)
204{
205 const char *src, *end;
206 int skip_tabs = 0, n;
207 char *s, *dst;
208 va_list args;
209 char c;
210
211 n = strlen(template);
212 s = malloc(n + 1);
213 if (!s)
214 return -ENOMEM;
215 src = template;
216 dst = s;
217
218 /* find out "baseline" indentation to skip */
219 while ((c = *src++)) {
220 if (c == '\t') {
221 skip_tabs++;
222 } else if (c == '\n') {
223 break;
224 } else {
225 p_err("unrecognized character at pos %td in template '%s'",
226 src - template - 1, template);
227 return -EINVAL;
228 }
229 }
230
231 while (*src) {
232 /* skip baseline indentation tabs */
233 for (n = skip_tabs; n > 0; n--, src++) {
234 if (*src != '\t') {
235 p_err("not enough tabs at pos %td in template '%s'",
236 src - template - 1, template);
237 return -EINVAL;
238 }
239 }
240 /* trim trailing whitespace */
241 end = strchrnul(src, '\n');
242 for (n = end - src; n > 0 && isspace(src[n - 1]); n--)
243 ;
244 memcpy(dst, src, n);
245 dst += n;
246 if (*end)
247 *dst++ = '\n';
248 src = *end ? end + 1 : end;
249 }
250 *dst++ = '\0';
251
252 /* print out using adjusted template */
253 va_start(args, template);
254 n = vprintf(s, args);
255 va_end(args);
256
257 free(s);
258 return n;
259}
260
261static int do_skeleton(int argc, char **argv)
262{
263 char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
264 size_t i, map_cnt = 0, prog_cnt = 0;
265 char obj_name[MAX_OBJ_NAME_LEN];
266 const char *file, *ident;
267 struct bpf_program *prog;
268 struct bpf_object *obj;
269 struct bpf_map *map;
270 struct btf *btf;
271 int err = -1;
272
273 if (!REQ_ARGS(1)) {
274 usage();
275 return -1;
276 }
277 file = GET_ARG();
278
279 if (argc) {
280 p_err("extra unknown arguments");
281 return -1;
282 }
283
284 obj = bpf_object__open_file(file, NULL);
285 if (IS_ERR(obj)) {
286 p_err("failed to open BPF object file: %ld", PTR_ERR(obj));
287 return -1;
288 }
289
290 get_obj_name(obj_name, file);
291 get_header_guard(header_guard, obj_name);
292
293 bpf_object__for_each_map(map, obj) {
294 ident = get_map_ident(map);
295 if (!ident) {
296 p_err("ignoring unrecognized internal map '%s'...",
297 bpf_map__name(map));
298 continue;
299 }
300 map_cnt++;
301 }
302 bpf_object__for_each_program(prog, obj) {
303 prog_cnt++;
304 }
305
306 codegen("\
307 \n\
308 /* THIS FILE IS AUTOGENERATED! */ \n\
309 #ifndef %2$s \n\
310 #define %2$s \n\
311 \n\
312 #include <stdlib.h> \n\
313 #include <libbpf.h> \n\
314 \n\
315 struct %1$s { \n\
316 struct bpf_object_skeleton *skeleton; \n\
317 struct bpf_object *obj; \n\
318 ",
319 obj_name, header_guard
320 );
321
322 if (map_cnt) {
323 printf("\tstruct {\n");
324 bpf_object__for_each_map(map, obj) {
325 ident = get_map_ident(map);
326 if (!ident)
327 continue;
328 printf("\t\tstruct bpf_map *%s;\n", ident);
329 }
330 printf("\t} maps;\n");
331 }
332
333 if (prog_cnt) {
334 printf("\tstruct {\n");
335 bpf_object__for_each_program(prog, obj) {
336 printf("\t\tstruct bpf_program *%s;\n",
337 bpf_program__name(prog));
338 }
339 printf("\t} progs;\n");
340 printf("\tstruct {\n");
341 bpf_object__for_each_program(prog, obj) {
342 printf("\t\tstruct bpf_link *%s;\n",
343 bpf_program__name(prog));
344 }
345 printf("\t} links;\n");
346 }
347
348 btf = bpf_object__btf(obj);
349 if (btf) {
350 err = codegen_datasecs(obj, obj_name);
351 if (err)
352 goto out;
353 }
354
355 codegen("\
356 \n\
357 }; \n\
358 \n\
359 static inline struct bpf_object_skeleton * \n\
360 %1$s__create_skeleton(struct %1$s *obj, struct bpf_embed_data *embed)\n\
361 { \n\
362 struct bpf_object_skeleton *s; \n\
363 \n\
364 s = calloc(1, sizeof(*s)); \n\
365 if (!s) \n\
366 return NULL; \n\
367 \n\
368 s->sz = sizeof(*s); \n\
369 s->name = \"%1$s\"; \n\
370 s->data = embed->data; \n\
371 s->data_sz = embed->size; \n\
372 s->obj = &obj->obj; \n\
373 ",
374 obj_name
375 );
376 if (map_cnt) {
377 codegen("\
378 \n\
379 \n\
380 /* maps */ \n\
381 s->map_cnt = %zu; \n\
382 s->map_skel_sz = sizeof(*s->maps); \n\
383 s->maps = calloc(s->map_cnt, s->map_skel_sz);\n\
384 if (!s->maps) \n\
385 goto err; \n\
386 ",
387 map_cnt
388 );
389 i = 0;
390 bpf_object__for_each_map(map, obj) {
Paul Chaignon159ecc002019-12-16 12:27:33 +0100391 ident = get_map_ident(map);
Andrii Nakryiko985ead42019-12-13 17:43:37 -0800392
393 if (!ident)
394 continue;
395
396 codegen("\
397 \n\
398 \n\
399 s->maps[%zu].name = \"%s\"; \n\
400 s->maps[%zu].map = &obj->maps.%s; \n\
401 ",
402 i, bpf_map__name(map), i, ident);
403 /* memory-mapped internal maps */
404 if (bpf_map__is_internal(map) &&
405 (bpf_map__def(map)->map_flags & BPF_F_MMAPABLE)) {
406 printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n",
407 i, ident);
408 }
409 i++;
410 }
411 }
412 if (prog_cnt) {
413 codegen("\
414 \n\
415 \n\
416 /* programs */ \n\
417 s->prog_cnt = %zu; \n\
418 s->prog_skel_sz = sizeof(*s->progs); \n\
419 s->progs = calloc(s->prog_cnt, s->prog_skel_sz);\n\
420 if (!s->progs) \n\
421 goto err; \n\
422 ",
423 prog_cnt
424 );
425 i = 0;
426 bpf_object__for_each_program(prog, obj) {
427 codegen("\
428 \n\
429 \n\
430 s->progs[%1$zu].name = \"%2$s\"; \n\
431 s->progs[%1$zu].prog = &obj->progs.%2$s;\n\
432 s->progs[%1$zu].link = &obj->links.%2$s;\n\
433 ",
434 i, bpf_program__name(prog));
435 i++;
436 }
437 }
438 codegen("\
439 \n\
440 \n\
441 return s; \n\
442 err: \n\
443 bpf_object__destroy_skeleton(s); \n\
444 return NULL; \n\
445 } \n\
446 \n\
447 static void \n\
448 %1$s__destroy(struct %1$s *obj) \n\
449 { \n\
450 if (!obj) \n\
451 return; \n\
452 if (obj->skeleton) \n\
453 bpf_object__destroy_skeleton(obj->skeleton);\n\
454 free(obj); \n\
455 } \n\
456 \n\
457 static inline struct %1$s * \n\
458 %1$s__open_opts(struct bpf_embed_data *embed, const struct bpf_object_open_opts *opts)\n\
459 { \n\
460 struct %1$s *obj; \n\
461 \n\
462 obj = calloc(1, sizeof(*obj)); \n\
463 if (!obj) \n\
464 return NULL; \n\
465 \n\
466 obj->skeleton = %1$s__create_skeleton(obj, embed); \n\
467 if (!obj->skeleton) \n\
468 goto err; \n\
469 \n\
470 if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
471 goto err; \n\
472 \n\
473 return obj; \n\
474 err: \n\
475 %1$s__destroy(obj); \n\
476 return NULL; \n\
477 } \n\
478 \n\
479 static inline struct %1$s * \n\
480 %1$s__open(struct bpf_embed_data *embed) \n\
481 { \n\
482 return %1$s__open_opts(embed, NULL); \n\
483 } \n\
484 \n\
485 static inline int \n\
486 %1$s__load(struct %1$s *obj) \n\
487 { \n\
488 return bpf_object__load_skeleton(obj->skeleton); \n\
489 } \n\
490 \n\
491 static inline struct %1$s * \n\
492 %1$s__open_and_load(struct bpf_embed_data *embed) \n\
493 { \n\
494 struct %1$s *obj; \n\
495 \n\
496 obj = %1$s__open(embed); \n\
497 if (!obj) \n\
498 return NULL; \n\
499 if (%1$s__load(obj)) { \n\
500 %1$s__destroy(obj); \n\
501 return NULL; \n\
502 } \n\
503 return obj; \n\
504 } \n\
505 \n\
506 static inline int \n\
507 %1$s__attach(struct %1$s *obj) \n\
508 { \n\
509 return bpf_object__attach_skeleton(obj->skeleton); \n\
510 } \n\
511 \n\
512 static inline void \n\
513 %1$s__detach(struct %1$s *obj) \n\
514 { \n\
515 return bpf_object__detach_skeleton(obj->skeleton); \n\
516 } \n\
517 \n\
518 #endif /* %2$s */ \n\
519 ",
520 obj_name, header_guard
521 );
522 err = 0;
523out:
524 bpf_object__close(obj);
525 return err;
526}
527
528static int do_help(int argc, char **argv)
529{
530 if (json_output) {
531 jsonw_null(json_wtr);
532 return 0;
533 }
534
535 fprintf(stderr,
536 "Usage: %1$s gen skeleton FILE\n"
537 " %1$s gen help\n"
538 "\n"
539 " " HELP_SPEC_OPTIONS "\n"
540 "",
541 bin_name);
542
543 return 0;
544}
545
546static const struct cmd cmds[] = {
547 { "skeleton", do_skeleton },
548 { "help", do_help },
549 { 0 }
550};
551
552int do_gen(int argc, char **argv)
553{
554 return cmd_select(cmds, argc, argv, do_help);
555}