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