1 // 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/bpf.h> 16 #include <bpf/libbpf.h> 17 #include <sys/types.h> 18 #include <sys/stat.h> 19 #include <sys/mman.h> 20 #include <bpf/btf.h> 21 22 #include "bpf/libbpf_internal.h" 23 #include "json_writer.h" 24 #include "main.h" 25 26 27 #define MAX_OBJ_NAME_LEN 64 28 29 static 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 38 static 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 53 static 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 63 static 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 72 static 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"; 85 else if (str_has_suffix(name, ".kconfig")) 86 return "kconfig"; 87 else 88 return NULL; 89 } 90 91 static void codegen_btf_dump_printf(void *ctx, const char *fmt, va_list args) 92 { 93 vprintf(fmt, args); 94 } 95 96 static 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 bool strip_mods = false; 108 109 if (strcmp(sec_name, ".data") == 0) { 110 sec_ident = "data"; 111 } else if (strcmp(sec_name, ".bss") == 0) { 112 sec_ident = "bss"; 113 } else if (strcmp(sec_name, ".rodata") == 0) { 114 sec_ident = "rodata"; 115 strip_mods = true; 116 } else if (strcmp(sec_name, ".kconfig") == 0) { 117 sec_ident = "kconfig"; 118 } else { 119 return 0; 120 } 121 122 printf(" struct %s__%s {\n", obj_name, sec_ident); 123 for (i = 0; i < vlen; i++, sec_var++) { 124 const struct btf_type *var = btf__type_by_id(btf, sec_var->type); 125 const char *var_name = btf__name_by_offset(btf, var->name_off); 126 DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts, 127 .field_name = var_ident, 128 .indent_level = 2, 129 .strip_mods = strip_mods, 130 ); 131 int need_off = sec_var->offset, align_off, align; 132 __u32 var_type_id = var->type; 133 134 if (off > need_off) { 135 p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n", 136 sec_name, i, need_off, off); 137 return -EINVAL; 138 } 139 140 align = btf__align_of(btf, var->type); 141 if (align <= 0) { 142 p_err("Failed to determine alignment of variable '%s': %d", 143 var_name, align); 144 return -EINVAL; 145 } 146 147 align_off = (off + align - 1) / align * align; 148 if (align_off != need_off) { 149 printf("\t\tchar __pad%d[%d];\n", 150 pad_cnt, need_off - off); 151 pad_cnt++; 152 } 153 154 /* sanitize variable name, e.g., for static vars inside 155 * a function, it's name is '<function name>.<variable name>', 156 * which we'll turn into a '<function name>_<variable name>' 157 */ 158 var_ident[0] = '\0'; 159 strncat(var_ident, var_name, sizeof(var_ident) - 1); 160 sanitize_identifier(var_ident); 161 162 printf("\t\t"); 163 err = btf_dump__emit_type_decl(d, var_type_id, &opts); 164 if (err) 165 return err; 166 printf(";\n"); 167 168 off = sec_var->offset + sec_var->size; 169 } 170 printf(" } *%s;\n", sec_ident); 171 return 0; 172 } 173 174 static int codegen_datasecs(struct bpf_object *obj, const char *obj_name) 175 { 176 struct btf *btf = bpf_object__btf(obj); 177 int n = btf__get_nr_types(btf); 178 struct btf_dump *d; 179 int i, err = 0; 180 181 d = btf_dump__new(btf, NULL, NULL, codegen_btf_dump_printf); 182 if (IS_ERR(d)) 183 return PTR_ERR(d); 184 185 for (i = 1; i <= n; i++) { 186 const struct btf_type *t = btf__type_by_id(btf, i); 187 188 if (!btf_is_datasec(t)) 189 continue; 190 191 err = codegen_datasec_def(obj, btf, d, t, obj_name); 192 if (err) 193 goto out; 194 } 195 out: 196 btf_dump__free(d); 197 return err; 198 } 199 200 static void codegen(const char *template, ...) 201 { 202 const char *src, *end; 203 int skip_tabs = 0, n; 204 char *s, *dst; 205 va_list args; 206 char c; 207 208 n = strlen(template); 209 s = malloc(n + 1); 210 if (!s) 211 exit(-1); 212 src = template; 213 dst = s; 214 215 /* find out "baseline" indentation to skip */ 216 while ((c = *src++)) { 217 if (c == '\t') { 218 skip_tabs++; 219 } else if (c == '\n') { 220 break; 221 } else { 222 p_err("unrecognized character at pos %td in template '%s'", 223 src - template - 1, template); 224 free(s); 225 exit(-1); 226 } 227 } 228 229 while (*src) { 230 /* skip baseline indentation tabs */ 231 for (n = skip_tabs; n > 0; n--, src++) { 232 if (*src != '\t') { 233 p_err("not enough tabs at pos %td in template '%s'", 234 src - template - 1, template); 235 free(s); 236 exit(-1); 237 } 238 } 239 /* trim trailing whitespace */ 240 end = strchrnul(src, '\n'); 241 for (n = end - src; n > 0 && isspace(src[n - 1]); n--) 242 ; 243 memcpy(dst, src, n); 244 dst += n; 245 if (*end) 246 *dst++ = '\n'; 247 src = *end ? end + 1 : end; 248 } 249 *dst++ = '\0'; 250 251 /* print out using adjusted template */ 252 va_start(args, template); 253 n = vprintf(s, args); 254 va_end(args); 255 256 free(s); 257 } 258 259 static int do_skeleton(int argc, char **argv) 260 { 261 char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")]; 262 size_t i, map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz; 263 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); 264 char obj_name[MAX_OBJ_NAME_LEN], *obj_data; 265 struct bpf_object *obj = NULL; 266 const char *file, *ident; 267 struct bpf_program *prog; 268 int fd, len, err = -1; 269 struct bpf_map *map; 270 struct btf *btf; 271 struct stat st; 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 if (stat(file, &st)) { 285 p_err("failed to stat() %s: %s", file, strerror(errno)); 286 return -1; 287 } 288 file_sz = st.st_size; 289 mmap_sz = roundup(file_sz, sysconf(_SC_PAGE_SIZE)); 290 fd = open(file, O_RDONLY); 291 if (fd < 0) { 292 p_err("failed to open() %s: %s", file, strerror(errno)); 293 return -1; 294 } 295 obj_data = mmap(NULL, mmap_sz, PROT_READ, MAP_PRIVATE, fd, 0); 296 if (obj_data == MAP_FAILED) { 297 obj_data = NULL; 298 p_err("failed to mmap() %s: %s", file, strerror(errno)); 299 goto out; 300 } 301 get_obj_name(obj_name, file); 302 opts.object_name = obj_name; 303 obj = bpf_object__open_mem(obj_data, file_sz, &opts); 304 if (IS_ERR(obj)) { 305 obj = NULL; 306 p_err("failed to open BPF object file: %ld", PTR_ERR(obj)); 307 goto out; 308 } 309 310 bpf_object__for_each_map(map, obj) { 311 ident = get_map_ident(map); 312 if (!ident) { 313 p_err("ignoring unrecognized internal map '%s'...", 314 bpf_map__name(map)); 315 continue; 316 } 317 map_cnt++; 318 } 319 bpf_object__for_each_program(prog, obj) { 320 prog_cnt++; 321 } 322 323 get_header_guard(header_guard, obj_name); 324 codegen("\ 325 \n\ 326 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ \n\ 327 \n\ 328 /* THIS FILE IS AUTOGENERATED! */ \n\ 329 #ifndef %2$s \n\ 330 #define %2$s \n\ 331 \n\ 332 #include <stdlib.h> \n\ 333 #include <bpf/libbpf.h> \n\ 334 \n\ 335 struct %1$s { \n\ 336 struct bpf_object_skeleton *skeleton; \n\ 337 struct bpf_object *obj; \n\ 338 ", 339 obj_name, header_guard 340 ); 341 342 if (map_cnt) { 343 printf("\tstruct {\n"); 344 bpf_object__for_each_map(map, obj) { 345 ident = get_map_ident(map); 346 if (!ident) 347 continue; 348 printf("\t\tstruct bpf_map *%s;\n", ident); 349 } 350 printf("\t} maps;\n"); 351 } 352 353 if (prog_cnt) { 354 printf("\tstruct {\n"); 355 bpf_object__for_each_program(prog, obj) { 356 printf("\t\tstruct bpf_program *%s;\n", 357 bpf_program__name(prog)); 358 } 359 printf("\t} progs;\n"); 360 printf("\tstruct {\n"); 361 bpf_object__for_each_program(prog, obj) { 362 printf("\t\tstruct bpf_link *%s;\n", 363 bpf_program__name(prog)); 364 } 365 printf("\t} links;\n"); 366 } 367 368 btf = bpf_object__btf(obj); 369 if (btf) { 370 err = codegen_datasecs(obj, obj_name); 371 if (err) 372 goto out; 373 } 374 375 codegen("\ 376 \n\ 377 }; \n\ 378 \n\ 379 static void \n\ 380 %1$s__destroy(struct %1$s *obj) \n\ 381 { \n\ 382 if (!obj) \n\ 383 return; \n\ 384 if (obj->skeleton) \n\ 385 bpf_object__destroy_skeleton(obj->skeleton);\n\ 386 free(obj); \n\ 387 } \n\ 388 \n\ 389 static inline int \n\ 390 %1$s__create_skeleton(struct %1$s *obj); \n\ 391 \n\ 392 static inline struct %1$s * \n\ 393 %1$s__open_opts(const struct bpf_object_open_opts *opts) \n\ 394 { \n\ 395 struct %1$s *obj; \n\ 396 \n\ 397 obj = (typeof(obj))calloc(1, sizeof(*obj)); \n\ 398 if (!obj) \n\ 399 return NULL; \n\ 400 if (%1$s__create_skeleton(obj)) \n\ 401 goto err; \n\ 402 if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\ 403 goto err; \n\ 404 \n\ 405 return obj; \n\ 406 err: \n\ 407 %1$s__destroy(obj); \n\ 408 return NULL; \n\ 409 } \n\ 410 \n\ 411 static inline struct %1$s * \n\ 412 %1$s__open(void) \n\ 413 { \n\ 414 return %1$s__open_opts(NULL); \n\ 415 } \n\ 416 \n\ 417 static inline int \n\ 418 %1$s__load(struct %1$s *obj) \n\ 419 { \n\ 420 return bpf_object__load_skeleton(obj->skeleton); \n\ 421 } \n\ 422 \n\ 423 static inline struct %1$s * \n\ 424 %1$s__open_and_load(void) \n\ 425 { \n\ 426 struct %1$s *obj; \n\ 427 \n\ 428 obj = %1$s__open(); \n\ 429 if (!obj) \n\ 430 return NULL; \n\ 431 if (%1$s__load(obj)) { \n\ 432 %1$s__destroy(obj); \n\ 433 return NULL; \n\ 434 } \n\ 435 return obj; \n\ 436 } \n\ 437 \n\ 438 static inline int \n\ 439 %1$s__attach(struct %1$s *obj) \n\ 440 { \n\ 441 return bpf_object__attach_skeleton(obj->skeleton); \n\ 442 } \n\ 443 \n\ 444 static inline void \n\ 445 %1$s__detach(struct %1$s *obj) \n\ 446 { \n\ 447 return bpf_object__detach_skeleton(obj->skeleton); \n\ 448 } \n\ 449 ", 450 obj_name 451 ); 452 453 codegen("\ 454 \n\ 455 \n\ 456 static inline int \n\ 457 %1$s__create_skeleton(struct %1$s *obj) \n\ 458 { \n\ 459 struct bpf_object_skeleton *s; \n\ 460 \n\ 461 s = (typeof(s))calloc(1, sizeof(*s)); \n\ 462 if (!s) \n\ 463 return -1; \n\ 464 obj->skeleton = s; \n\ 465 \n\ 466 s->sz = sizeof(*s); \n\ 467 s->name = \"%1$s\"; \n\ 468 s->obj = &obj->obj; \n\ 469 ", 470 obj_name 471 ); 472 if (map_cnt) { 473 codegen("\ 474 \n\ 475 \n\ 476 /* maps */ \n\ 477 s->map_cnt = %zu; \n\ 478 s->map_skel_sz = sizeof(*s->maps); \n\ 479 s->maps = (typeof(s->maps))calloc(s->map_cnt, s->map_skel_sz);\n\ 480 if (!s->maps) \n\ 481 goto err; \n\ 482 ", 483 map_cnt 484 ); 485 i = 0; 486 bpf_object__for_each_map(map, obj) { 487 ident = get_map_ident(map); 488 489 if (!ident) 490 continue; 491 492 codegen("\ 493 \n\ 494 \n\ 495 s->maps[%zu].name = \"%s\"; \n\ 496 s->maps[%zu].map = &obj->maps.%s; \n\ 497 ", 498 i, bpf_map__name(map), i, ident); 499 /* memory-mapped internal maps */ 500 if (bpf_map__is_internal(map) && 501 (bpf_map__def(map)->map_flags & BPF_F_MMAPABLE)) { 502 printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n", 503 i, ident); 504 } 505 i++; 506 } 507 } 508 if (prog_cnt) { 509 codegen("\ 510 \n\ 511 \n\ 512 /* programs */ \n\ 513 s->prog_cnt = %zu; \n\ 514 s->prog_skel_sz = sizeof(*s->progs); \n\ 515 s->progs = (typeof(s->progs))calloc(s->prog_cnt, s->prog_skel_sz);\n\ 516 if (!s->progs) \n\ 517 goto err; \n\ 518 ", 519 prog_cnt 520 ); 521 i = 0; 522 bpf_object__for_each_program(prog, obj) { 523 codegen("\ 524 \n\ 525 \n\ 526 s->progs[%1$zu].name = \"%2$s\"; \n\ 527 s->progs[%1$zu].prog = &obj->progs.%2$s;\n\ 528 s->progs[%1$zu].link = &obj->links.%2$s;\n\ 529 ", 530 i, bpf_program__name(prog)); 531 i++; 532 } 533 } 534 codegen("\ 535 \n\ 536 \n\ 537 s->data_sz = %d; \n\ 538 s->data = (void *)\"\\ \n\ 539 ", 540 file_sz); 541 542 /* embed contents of BPF object file */ 543 for (i = 0, len = 0; i < file_sz; i++) { 544 int w = obj_data[i] ? 4 : 2; 545 546 len += w; 547 if (len > 78) { 548 printf("\\\n"); 549 len = w; 550 } 551 if (!obj_data[i]) 552 printf("\\0"); 553 else 554 printf("\\x%02x", (unsigned char)obj_data[i]); 555 } 556 557 codegen("\ 558 \n\ 559 \"; \n\ 560 \n\ 561 return 0; \n\ 562 err: \n\ 563 bpf_object__destroy_skeleton(s); \n\ 564 return -1; \n\ 565 } \n\ 566 \n\ 567 #endif /* %s */ \n\ 568 ", 569 header_guard); 570 err = 0; 571 out: 572 bpf_object__close(obj); 573 if (obj_data) 574 munmap(obj_data, mmap_sz); 575 close(fd); 576 return err; 577 } 578 579 static int do_help(int argc, char **argv) 580 { 581 if (json_output) { 582 jsonw_null(json_wtr); 583 return 0; 584 } 585 586 fprintf(stderr, 587 "Usage: %1$s %2$s skeleton FILE\n" 588 " %1$s %2$s help\n" 589 "\n" 590 " " HELP_SPEC_OPTIONS "\n" 591 "", 592 bin_name, "gen"); 593 594 return 0; 595 } 596 597 static const struct cmd cmds[] = { 598 { "skeleton", do_skeleton }, 599 { "help", do_help }, 600 { 0 } 601 }; 602 603 int do_gen(int argc, char **argv) 604 { 605 return cmd_select(cmds, argc, argv, do_help); 606 } 607