GNU Linux-libre 5.13.14-gnu1
[releases.git] / tools / bpf / bpftool / gen.c
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 "json_writer.h"
23 #include "main.h"
24
25 #define MAX_OBJ_NAME_LEN 64
26
27 static void sanitize_identifier(char *name)
28 {
29         int i;
30
31         for (i = 0; name[i]; i++)
32                 if (!isalnum(name[i]) && name[i] != '_')
33                         name[i] = '_';
34 }
35
36 static bool str_has_suffix(const char *str, const char *suffix)
37 {
38         size_t i, n1 = strlen(str), n2 = strlen(suffix);
39
40         if (n1 < n2)
41                 return false;
42
43         for (i = 0; i < n2; i++) {
44                 if (str[n1 - i - 1] != suffix[n2 - i - 1])
45                         return false;
46         }
47
48         return true;
49 }
50
51 static void get_obj_name(char *name, const char *file)
52 {
53         /* Using basename() GNU version which doesn't modify arg. */
54         strncpy(name, basename(file), MAX_OBJ_NAME_LEN - 1);
55         name[MAX_OBJ_NAME_LEN - 1] = '\0';
56         if (str_has_suffix(name, ".o"))
57                 name[strlen(name) - 2] = '\0';
58         sanitize_identifier(name);
59 }
60
61 static void get_header_guard(char *guard, const char *obj_name)
62 {
63         int i;
64
65         sprintf(guard, "__%s_SKEL_H__", obj_name);
66         for (i = 0; guard[i]; i++)
67                 guard[i] = toupper(guard[i]);
68 }
69
70 static const char *get_map_ident(const struct bpf_map *map)
71 {
72         const char *name = bpf_map__name(map);
73
74         if (!bpf_map__is_internal(map))
75                 return name;
76
77         if (str_has_suffix(name, ".data"))
78                 return "data";
79         else if (str_has_suffix(name, ".rodata"))
80                 return "rodata";
81         else if (str_has_suffix(name, ".bss"))
82                 return "bss";
83         else if (str_has_suffix(name, ".kconfig"))
84                 return "kconfig";
85         else
86                 return NULL;
87 }
88
89 static void codegen_btf_dump_printf(void *ctx, const char *fmt, va_list args)
90 {
91         vprintf(fmt, args);
92 }
93
94 static int codegen_datasec_def(struct bpf_object *obj,
95                                struct btf *btf,
96                                struct btf_dump *d,
97                                const struct btf_type *sec,
98                                const char *obj_name)
99 {
100         const char *sec_name = btf__name_by_offset(btf, sec->name_off);
101         const struct btf_var_secinfo *sec_var = btf_var_secinfos(sec);
102         int i, err, off = 0, pad_cnt = 0, vlen = btf_vlen(sec);
103         const char *sec_ident;
104         char var_ident[256];
105         bool strip_mods = false;
106
107         if (strcmp(sec_name, ".data") == 0) {
108                 sec_ident = "data";
109         } else if (strcmp(sec_name, ".bss") == 0) {
110                 sec_ident = "bss";
111         } else if (strcmp(sec_name, ".rodata") == 0) {
112                 sec_ident = "rodata";
113                 strip_mods = true;
114         } else if (strcmp(sec_name, ".kconfig") == 0) {
115                 sec_ident = "kconfig";
116         } else {
117                 return 0;
118         }
119
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,
126                         .indent_level = 2,
127                         .strip_mods = strip_mods,
128                 );
129                 int need_off = sec_var->offset, align_off, align;
130                 __u32 var_type_id = var->type;
131
132                 if (off > need_off) {
133                         p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n",
134                               sec_name, i, need_off, off);
135                         return -EINVAL;
136                 }
137
138                 align = btf__align_of(btf, var->type);
139                 if (align <= 0) {
140                         p_err("Failed to determine alignment of variable '%s': %d",
141                               var_name, align);
142                         return -EINVAL;
143                 }
144                 /* Assume 32-bit architectures when generating data section
145                  * struct memory layout. Given bpftool can't know which target
146                  * host architecture it's emitting skeleton for, we need to be
147                  * conservative and assume 32-bit one to ensure enough padding
148                  * bytes are generated for pointer and long types. This will
149                  * still work correctly for 64-bit architectures, because in
150                  * the worst case we'll generate unnecessary padding field,
151                  * which on 64-bit architectures is not strictly necessary and
152                  * would be handled by natural 8-byte alignment. But it still
153                  * will be a correct memory layout, based on recorded offsets
154                  * in BTF.
155                  */
156                 if (align > 4)
157                         align = 4;
158
159                 align_off = (off + align - 1) / align * align;
160                 if (align_off != need_off) {
161                         printf("\t\tchar __pad%d[%d];\n",
162                                pad_cnt, need_off - off);
163                         pad_cnt++;
164                 }
165
166                 /* sanitize variable name, e.g., for static vars inside
167                  * a function, it's name is '<function name>.<variable name>',
168                  * which we'll turn into a '<function name>_<variable name>'
169                  */
170                 var_ident[0] = '\0';
171                 strncat(var_ident, var_name, sizeof(var_ident) - 1);
172                 sanitize_identifier(var_ident);
173
174                 printf("\t\t");
175                 err = btf_dump__emit_type_decl(d, var_type_id, &opts);
176                 if (err)
177                         return err;
178                 printf(";\n");
179
180                 off = sec_var->offset + sec_var->size;
181         }
182         printf("        } *%s;\n", sec_ident);
183         return 0;
184 }
185
186 static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
187 {
188         struct btf *btf = bpf_object__btf(obj);
189         int n = btf__get_nr_types(btf);
190         struct btf_dump *d;
191         int i, err = 0;
192
193         d = btf_dump__new(btf, NULL, NULL, codegen_btf_dump_printf);
194         if (IS_ERR(d))
195                 return PTR_ERR(d);
196
197         for (i = 1; i <= n; i++) {
198                 const struct btf_type *t = btf__type_by_id(btf, i);
199
200                 if (!btf_is_datasec(t))
201                         continue;
202
203                 err = codegen_datasec_def(obj, btf, d, t, obj_name);
204                 if (err)
205                         goto out;
206         }
207 out:
208         btf_dump__free(d);
209         return err;
210 }
211
212 static void codegen(const char *template, ...)
213 {
214         const char *src, *end;
215         int skip_tabs = 0, n;
216         char *s, *dst;
217         va_list args;
218         char c;
219
220         n = strlen(template);
221         s = malloc(n + 1);
222         if (!s)
223                 exit(-1);
224         src = template;
225         dst = s;
226
227         /* find out "baseline" indentation to skip */
228         while ((c = *src++)) {
229                 if (c == '\t') {
230                         skip_tabs++;
231                 } else if (c == '\n') {
232                         break;
233                 } else {
234                         p_err("unrecognized character at pos %td in template '%s'",
235                               src - template - 1, template);
236                         free(s);
237                         exit(-1);
238                 }
239         }
240
241         while (*src) {
242                 /* skip baseline indentation tabs */
243                 for (n = skip_tabs; n > 0; n--, src++) {
244                         if (*src != '\t') {
245                                 p_err("not enough tabs at pos %td in template '%s'",
246                                       src - template - 1, template);
247                                 free(s);
248                                 exit(-1);
249                         }
250                 }
251                 /* trim trailing whitespace */
252                 end = strchrnul(src, '\n');
253                 for (n = end - src; n > 0 && isspace(src[n - 1]); n--)
254                         ;
255                 memcpy(dst, src, n);
256                 dst += n;
257                 if (*end)
258                         *dst++ = '\n';
259                 src = *end ? end + 1 : end;
260         }
261         *dst++ = '\0';
262
263         /* print out using adjusted template */
264         va_start(args, template);
265         n = vprintf(s, args);
266         va_end(args);
267
268         free(s);
269 }
270
271 static int do_skeleton(int argc, char **argv)
272 {
273         char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
274         size_t i, map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz;
275         DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
276         char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data;
277         struct bpf_object *obj = NULL;
278         const char *file, *ident;
279         struct bpf_program *prog;
280         int fd, len, err = -1;
281         struct bpf_map *map;
282         struct btf *btf;
283         struct stat st;
284
285         if (!REQ_ARGS(1)) {
286                 usage();
287                 return -1;
288         }
289         file = GET_ARG();
290
291         while (argc) {
292                 if (!REQ_ARGS(2))
293                         return -1;
294
295                 if (is_prefix(*argv, "name")) {
296                         NEXT_ARG();
297
298                         if (obj_name[0] != '\0') {
299                                 p_err("object name already specified");
300                                 return -1;
301                         }
302
303                         strncpy(obj_name, *argv, MAX_OBJ_NAME_LEN - 1);
304                         obj_name[MAX_OBJ_NAME_LEN - 1] = '\0';
305                 } else {
306                         p_err("unknown arg %s", *argv);
307                         return -1;
308                 }
309
310                 NEXT_ARG();
311         }
312
313         if (argc) {
314                 p_err("extra unknown arguments");
315                 return -1;
316         }
317
318         if (stat(file, &st)) {
319                 p_err("failed to stat() %s: %s", file, strerror(errno));
320                 return -1;
321         }
322         file_sz = st.st_size;
323         mmap_sz = roundup(file_sz, sysconf(_SC_PAGE_SIZE));
324         fd = open(file, O_RDONLY);
325         if (fd < 0) {
326                 p_err("failed to open() %s: %s", file, strerror(errno));
327                 return -1;
328         }
329         obj_data = mmap(NULL, mmap_sz, PROT_READ, MAP_PRIVATE, fd, 0);
330         if (obj_data == MAP_FAILED) {
331                 obj_data = NULL;
332                 p_err("failed to mmap() %s: %s", file, strerror(errno));
333                 goto out;
334         }
335         if (obj_name[0] == '\0')
336                 get_obj_name(obj_name, file);
337         opts.object_name = obj_name;
338         obj = bpf_object__open_mem(obj_data, file_sz, &opts);
339         if (IS_ERR(obj)) {
340                 char err_buf[256];
341
342                 libbpf_strerror(PTR_ERR(obj), err_buf, sizeof(err_buf));
343                 p_err("failed to open BPF object file: %s", err_buf);
344                 obj = NULL;
345                 goto out;
346         }
347
348         bpf_object__for_each_map(map, obj) {
349                 ident = get_map_ident(map);
350                 if (!ident) {
351                         p_err("ignoring unrecognized internal map '%s'...",
352                               bpf_map__name(map));
353                         continue;
354                 }
355                 map_cnt++;
356         }
357         bpf_object__for_each_program(prog, obj) {
358                 prog_cnt++;
359         }
360
361         get_header_guard(header_guard, obj_name);
362         codegen("\
363                 \n\
364                 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */   \n\
365                                                                             \n\
366                 /* THIS FILE IS AUTOGENERATED! */                           \n\
367                 #ifndef %2$s                                                \n\
368                 #define %2$s                                                \n\
369                                                                             \n\
370                 #include <stdlib.h>                                         \n\
371                 #include <bpf/libbpf.h>                                     \n\
372                                                                             \n\
373                 struct %1$s {                                               \n\
374                         struct bpf_object_skeleton *skeleton;               \n\
375                         struct bpf_object *obj;                             \n\
376                 ",
377                 obj_name, header_guard
378         );
379
380         if (map_cnt) {
381                 printf("\tstruct {\n");
382                 bpf_object__for_each_map(map, obj) {
383                         ident = get_map_ident(map);
384                         if (!ident)
385                                 continue;
386                         printf("\t\tstruct bpf_map *%s;\n", ident);
387                 }
388                 printf("\t} maps;\n");
389         }
390
391         if (prog_cnt) {
392                 printf("\tstruct {\n");
393                 bpf_object__for_each_program(prog, obj) {
394                         printf("\t\tstruct bpf_program *%s;\n",
395                                bpf_program__name(prog));
396                 }
397                 printf("\t} progs;\n");
398                 printf("\tstruct {\n");
399                 bpf_object__for_each_program(prog, obj) {
400                         printf("\t\tstruct bpf_link *%s;\n",
401                                bpf_program__name(prog));
402                 }
403                 printf("\t} links;\n");
404         }
405
406         btf = bpf_object__btf(obj);
407         if (btf) {
408                 err = codegen_datasecs(obj, obj_name);
409                 if (err)
410                         goto out;
411         }
412
413         codegen("\
414                 \n\
415                 };                                                          \n\
416                                                                             \n\
417                 static void                                                 \n\
418                 %1$s__destroy(struct %1$s *obj)                             \n\
419                 {                                                           \n\
420                         if (!obj)                                           \n\
421                                 return;                                     \n\
422                         if (obj->skeleton)                                  \n\
423                                 bpf_object__destroy_skeleton(obj->skeleton);\n\
424                         free(obj);                                          \n\
425                 }                                                           \n\
426                                                                             \n\
427                 static inline int                                           \n\
428                 %1$s__create_skeleton(struct %1$s *obj);                    \n\
429                                                                             \n\
430                 static inline struct %1$s *                                 \n\
431                 %1$s__open_opts(const struct bpf_object_open_opts *opts)    \n\
432                 {                                                           \n\
433                         struct %1$s *obj;                                   \n\
434                                                                             \n\
435                         obj = (struct %1$s *)calloc(1, sizeof(*obj));       \n\
436                         if (!obj)                                           \n\
437                                 return NULL;                                \n\
438                         if (%1$s__create_skeleton(obj))                     \n\
439                                 goto err;                                   \n\
440                         if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
441                                 goto err;                                   \n\
442                                                                             \n\
443                         return obj;                                         \n\
444                 err:                                                        \n\
445                         %1$s__destroy(obj);                                 \n\
446                         return NULL;                                        \n\
447                 }                                                           \n\
448                                                                             \n\
449                 static inline struct %1$s *                                 \n\
450                 %1$s__open(void)                                            \n\
451                 {                                                           \n\
452                         return %1$s__open_opts(NULL);                       \n\
453                 }                                                           \n\
454                                                                             \n\
455                 static inline int                                           \n\
456                 %1$s__load(struct %1$s *obj)                                \n\
457                 {                                                           \n\
458                         return bpf_object__load_skeleton(obj->skeleton);    \n\
459                 }                                                           \n\
460                                                                             \n\
461                 static inline struct %1$s *                                 \n\
462                 %1$s__open_and_load(void)                                   \n\
463                 {                                                           \n\
464                         struct %1$s *obj;                                   \n\
465                                                                             \n\
466                         obj = %1$s__open();                                 \n\
467                         if (!obj)                                           \n\
468                                 return NULL;                                \n\
469                         if (%1$s__load(obj)) {                              \n\
470                                 %1$s__destroy(obj);                         \n\
471                                 return NULL;                                \n\
472                         }                                                   \n\
473                         return obj;                                         \n\
474                 }                                                           \n\
475                                                                             \n\
476                 static inline int                                           \n\
477                 %1$s__attach(struct %1$s *obj)                              \n\
478                 {                                                           \n\
479                         return bpf_object__attach_skeleton(obj->skeleton);  \n\
480                 }                                                           \n\
481                                                                             \n\
482                 static inline void                                          \n\
483                 %1$s__detach(struct %1$s *obj)                              \n\
484                 {                                                           \n\
485                         return bpf_object__detach_skeleton(obj->skeleton);  \n\
486                 }                                                           \n\
487                 ",
488                 obj_name
489         );
490
491         codegen("\
492                 \n\
493                                                                             \n\
494                 static inline int                                           \n\
495                 %1$s__create_skeleton(struct %1$s *obj)                     \n\
496                 {                                                           \n\
497                         struct bpf_object_skeleton *s;                      \n\
498                                                                             \n\
499                         s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
500                         if (!s)                                             \n\
501                                 return -1;                                  \n\
502                         obj->skeleton = s;                                  \n\
503                                                                             \n\
504                         s->sz = sizeof(*s);                                 \n\
505                         s->name = \"%1$s\";                                 \n\
506                         s->obj = &obj->obj;                                 \n\
507                 ",
508                 obj_name
509         );
510         if (map_cnt) {
511                 codegen("\
512                         \n\
513                                                                             \n\
514                                 /* maps */                                  \n\
515                                 s->map_cnt = %zu;                           \n\
516                                 s->map_skel_sz = sizeof(*s->maps);          \n\
517                                 s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt, s->map_skel_sz);\n\
518                                 if (!s->maps)                               \n\
519                                         goto err;                           \n\
520                         ",
521                         map_cnt
522                 );
523                 i = 0;
524                 bpf_object__for_each_map(map, obj) {
525                         ident = get_map_ident(map);
526
527                         if (!ident)
528                                 continue;
529
530                         codegen("\
531                                 \n\
532                                                                             \n\
533                                         s->maps[%zu].name = \"%s\";         \n\
534                                         s->maps[%zu].map = &obj->maps.%s;   \n\
535                                 ",
536                                 i, bpf_map__name(map), i, ident);
537                         /* memory-mapped internal maps */
538                         if (bpf_map__is_internal(map) &&
539                             (bpf_map__def(map)->map_flags & BPF_F_MMAPABLE)) {
540                                 printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n",
541                                        i, ident);
542                         }
543                         i++;
544                 }
545         }
546         if (prog_cnt) {
547                 codegen("\
548                         \n\
549                                                                             \n\
550                                 /* programs */                              \n\
551                                 s->prog_cnt = %zu;                          \n\
552                                 s->prog_skel_sz = sizeof(*s->progs);        \n\
553                                 s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);\n\
554                                 if (!s->progs)                              \n\
555                                         goto err;                           \n\
556                         ",
557                         prog_cnt
558                 );
559                 i = 0;
560                 bpf_object__for_each_program(prog, obj) {
561                         codegen("\
562                                 \n\
563                                                                             \n\
564                                         s->progs[%1$zu].name = \"%2$s\";    \n\
565                                         s->progs[%1$zu].prog = &obj->progs.%2$s;\n\
566                                         s->progs[%1$zu].link = &obj->links.%2$s;\n\
567                                 ",
568                                 i, bpf_program__name(prog));
569                         i++;
570                 }
571         }
572         codegen("\
573                 \n\
574                                                                             \n\
575                         s->data_sz = %d;                                    \n\
576                         s->data = (void *)\"\\                              \n\
577                 ",
578                 file_sz);
579
580         /* embed contents of BPF object file */
581         for (i = 0, len = 0; i < file_sz; i++) {
582                 int w = obj_data[i] ? 4 : 2;
583
584                 len += w;
585                 if (len > 78) {
586                         printf("\\\n");
587                         len = w;
588                 }
589                 if (!obj_data[i])
590                         printf("\\0");
591                 else
592                         printf("\\x%02x", (unsigned char)obj_data[i]);
593         }
594
595         codegen("\
596                 \n\
597                 \";                                                         \n\
598                                                                             \n\
599                         return 0;                                           \n\
600                 err:                                                        \n\
601                         bpf_object__destroy_skeleton(s);                    \n\
602                         return -1;                                          \n\
603                 }                                                           \n\
604                                                                             \n\
605                 #endif /* %s */                                             \n\
606                 ",
607                 header_guard);
608         err = 0;
609 out:
610         bpf_object__close(obj);
611         if (obj_data)
612                 munmap(obj_data, mmap_sz);
613         close(fd);
614         return err;
615 }
616
617 static int do_object(int argc, char **argv)
618 {
619         struct bpf_linker *linker;
620         const char *output_file, *file;
621         int err = 0;
622
623         if (!REQ_ARGS(2)) {
624                 usage();
625                 return -1;
626         }
627
628         output_file = GET_ARG();
629
630         linker = bpf_linker__new(output_file, NULL);
631         if (!linker) {
632                 p_err("failed to create BPF linker instance");
633                 return -1;
634         }
635
636         while (argc) {
637                 file = GET_ARG();
638
639                 err = bpf_linker__add_file(linker, file);
640                 if (err) {
641                         p_err("failed to link '%s': %s (%d)", file, strerror(err), err);
642                         goto out;
643                 }
644         }
645
646         err = bpf_linker__finalize(linker);
647         if (err) {
648                 p_err("failed to finalize ELF file: %s (%d)", strerror(err), err);
649                 goto out;
650         }
651
652         err = 0;
653 out:
654         bpf_linker__free(linker);
655         return err;
656 }
657
658 static int do_help(int argc, char **argv)
659 {
660         if (json_output) {
661                 jsonw_null(json_wtr);
662                 return 0;
663         }
664
665         fprintf(stderr,
666                 "Usage: %1$s %2$s object OUTPUT_FILE INPUT_FILE [INPUT_FILE...]\n"
667                 "       %1$s %2$s skeleton FILE [name OBJECT_NAME]\n"
668                 "       %1$s %2$s help\n"
669                 "\n"
670                 "       " HELP_SPEC_OPTIONS "\n"
671                 "",
672                 bin_name, "gen");
673
674         return 0;
675 }
676
677 static const struct cmd cmds[] = {
678         { "object",     do_object },
679         { "skeleton",   do_skeleton },
680         { "help",       do_help },
681         { 0 }
682 };
683
684 int do_gen(int argc, char **argv)
685 {
686         return cmd_select(cmds, argc, argv, do_help);
687 }