xref: /linux/tools/bpf/bpftool/gen.c (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
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 		/* Assume 32-bit architectures when generating data section
147 		 * struct memory layout. Given bpftool can't know which target
148 		 * host architecture it's emitting skeleton for, we need to be
149 		 * conservative and assume 32-bit one to ensure enough padding
150 		 * bytes are generated for pointer and long types. This will
151 		 * still work correctly for 64-bit architectures, because in
152 		 * the worst case we'll generate unnecessary padding field,
153 		 * which on 64-bit architectures is not strictly necessary and
154 		 * would be handled by natural 8-byte alignment. But it still
155 		 * will be a correct memory layout, based on recorded offsets
156 		 * in BTF.
157 		 */
158 		if (align > 4)
159 			align = 4;
160 
161 		align_off = (off + align - 1) / align * align;
162 		if (align_off != need_off) {
163 			printf("\t\tchar __pad%d[%d];\n",
164 			       pad_cnt, need_off - off);
165 			pad_cnt++;
166 		}
167 
168 		/* sanitize variable name, e.g., for static vars inside
169 		 * a function, it's name is '<function name>.<variable name>',
170 		 * which we'll turn into a '<function name>_<variable name>'
171 		 */
172 		var_ident[0] = '\0';
173 		strncat(var_ident, var_name, sizeof(var_ident) - 1);
174 		sanitize_identifier(var_ident);
175 
176 		printf("\t\t");
177 		err = btf_dump__emit_type_decl(d, var_type_id, &opts);
178 		if (err)
179 			return err;
180 		printf(";\n");
181 
182 		off = sec_var->offset + sec_var->size;
183 	}
184 	printf("	} *%s;\n", sec_ident);
185 	return 0;
186 }
187 
188 static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
189 {
190 	struct btf *btf = bpf_object__btf(obj);
191 	int n = btf__get_nr_types(btf);
192 	struct btf_dump *d;
193 	int i, err = 0;
194 
195 	d = btf_dump__new(btf, NULL, NULL, codegen_btf_dump_printf);
196 	if (IS_ERR(d))
197 		return PTR_ERR(d);
198 
199 	for (i = 1; i <= n; i++) {
200 		const struct btf_type *t = btf__type_by_id(btf, i);
201 
202 		if (!btf_is_datasec(t))
203 			continue;
204 
205 		err = codegen_datasec_def(obj, btf, d, t, obj_name);
206 		if (err)
207 			goto out;
208 	}
209 out:
210 	btf_dump__free(d);
211 	return err;
212 }
213 
214 static void codegen(const char *template, ...)
215 {
216 	const char *src, *end;
217 	int skip_tabs = 0, n;
218 	char *s, *dst;
219 	va_list args;
220 	char c;
221 
222 	n = strlen(template);
223 	s = malloc(n + 1);
224 	if (!s)
225 		exit(-1);
226 	src = template;
227 	dst = s;
228 
229 	/* find out "baseline" indentation to skip */
230 	while ((c = *src++)) {
231 		if (c == '\t') {
232 			skip_tabs++;
233 		} else if (c == '\n') {
234 			break;
235 		} else {
236 			p_err("unrecognized character at pos %td in template '%s'",
237 			      src - template - 1, template);
238 			free(s);
239 			exit(-1);
240 		}
241 	}
242 
243 	while (*src) {
244 		/* skip baseline indentation tabs */
245 		for (n = skip_tabs; n > 0; n--, src++) {
246 			if (*src != '\t') {
247 				p_err("not enough tabs at pos %td in template '%s'",
248 				      src - template - 1, template);
249 				free(s);
250 				exit(-1);
251 			}
252 		}
253 		/* trim trailing whitespace */
254 		end = strchrnul(src, '\n');
255 		for (n = end - src; n > 0 && isspace(src[n - 1]); n--)
256 			;
257 		memcpy(dst, src, n);
258 		dst += n;
259 		if (*end)
260 			*dst++ = '\n';
261 		src = *end ? end + 1 : end;
262 	}
263 	*dst++ = '\0';
264 
265 	/* print out using adjusted template */
266 	va_start(args, template);
267 	n = vprintf(s, args);
268 	va_end(args);
269 
270 	free(s);
271 }
272 
273 static int do_skeleton(int argc, char **argv)
274 {
275 	char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
276 	size_t i, map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz;
277 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
278 	char obj_name[MAX_OBJ_NAME_LEN], *obj_data;
279 	struct bpf_object *obj = NULL;
280 	const char *file, *ident;
281 	struct bpf_program *prog;
282 	int fd, len, err = -1;
283 	struct bpf_map *map;
284 	struct btf *btf;
285 	struct stat st;
286 
287 	if (!REQ_ARGS(1)) {
288 		usage();
289 		return -1;
290 	}
291 	file = GET_ARG();
292 
293 	if (argc) {
294 		p_err("extra unknown arguments");
295 		return -1;
296 	}
297 
298 	if (stat(file, &st)) {
299 		p_err("failed to stat() %s: %s", file, strerror(errno));
300 		return -1;
301 	}
302 	file_sz = st.st_size;
303 	mmap_sz = roundup(file_sz, sysconf(_SC_PAGE_SIZE));
304 	fd = open(file, O_RDONLY);
305 	if (fd < 0) {
306 		p_err("failed to open() %s: %s", file, strerror(errno));
307 		return -1;
308 	}
309 	obj_data = mmap(NULL, mmap_sz, PROT_READ, MAP_PRIVATE, fd, 0);
310 	if (obj_data == MAP_FAILED) {
311 		obj_data = NULL;
312 		p_err("failed to mmap() %s: %s", file, strerror(errno));
313 		goto out;
314 	}
315 	get_obj_name(obj_name, file);
316 	opts.object_name = obj_name;
317 	obj = bpf_object__open_mem(obj_data, file_sz, &opts);
318 	if (IS_ERR(obj)) {
319 		char err_buf[256];
320 
321 		libbpf_strerror(PTR_ERR(obj), err_buf, sizeof(err_buf));
322 		p_err("failed to open BPF object file: %s", err_buf);
323 		obj = NULL;
324 		goto out;
325 	}
326 
327 	bpf_object__for_each_map(map, obj) {
328 		ident = get_map_ident(map);
329 		if (!ident) {
330 			p_err("ignoring unrecognized internal map '%s'...",
331 			      bpf_map__name(map));
332 			continue;
333 		}
334 		map_cnt++;
335 	}
336 	bpf_object__for_each_program(prog, obj) {
337 		prog_cnt++;
338 	}
339 
340 	get_header_guard(header_guard, obj_name);
341 	codegen("\
342 		\n\
343 		/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */   \n\
344 									    \n\
345 		/* THIS FILE IS AUTOGENERATED! */			    \n\
346 		#ifndef %2$s						    \n\
347 		#define %2$s						    \n\
348 									    \n\
349 		#include <stdlib.h>					    \n\
350 		#include <bpf/libbpf.h>					    \n\
351 									    \n\
352 		struct %1$s {						    \n\
353 			struct bpf_object_skeleton *skeleton;		    \n\
354 			struct bpf_object *obj;				    \n\
355 		",
356 		obj_name, header_guard
357 	);
358 
359 	if (map_cnt) {
360 		printf("\tstruct {\n");
361 		bpf_object__for_each_map(map, obj) {
362 			ident = get_map_ident(map);
363 			if (!ident)
364 				continue;
365 			printf("\t\tstruct bpf_map *%s;\n", ident);
366 		}
367 		printf("\t} maps;\n");
368 	}
369 
370 	if (prog_cnt) {
371 		printf("\tstruct {\n");
372 		bpf_object__for_each_program(prog, obj) {
373 			printf("\t\tstruct bpf_program *%s;\n",
374 			       bpf_program__name(prog));
375 		}
376 		printf("\t} progs;\n");
377 		printf("\tstruct {\n");
378 		bpf_object__for_each_program(prog, obj) {
379 			printf("\t\tstruct bpf_link *%s;\n",
380 			       bpf_program__name(prog));
381 		}
382 		printf("\t} links;\n");
383 	}
384 
385 	btf = bpf_object__btf(obj);
386 	if (btf) {
387 		err = codegen_datasecs(obj, obj_name);
388 		if (err)
389 			goto out;
390 	}
391 
392 	codegen("\
393 		\n\
394 		};							    \n\
395 									    \n\
396 		static void						    \n\
397 		%1$s__destroy(struct %1$s *obj)				    \n\
398 		{							    \n\
399 			if (!obj)					    \n\
400 				return;					    \n\
401 			if (obj->skeleton)				    \n\
402 				bpf_object__destroy_skeleton(obj->skeleton);\n\
403 			free(obj);					    \n\
404 		}							    \n\
405 									    \n\
406 		static inline int					    \n\
407 		%1$s__create_skeleton(struct %1$s *obj);		    \n\
408 									    \n\
409 		static inline struct %1$s *				    \n\
410 		%1$s__open_opts(const struct bpf_object_open_opts *opts)    \n\
411 		{							    \n\
412 			struct %1$s *obj;				    \n\
413 									    \n\
414 			obj = (struct %1$s *)calloc(1, sizeof(*obj));	    \n\
415 			if (!obj)					    \n\
416 				return NULL;				    \n\
417 			if (%1$s__create_skeleton(obj))			    \n\
418 				goto err;				    \n\
419 			if (bpf_object__open_skeleton(obj->skeleton, opts)) \n\
420 				goto err;				    \n\
421 									    \n\
422 			return obj;					    \n\
423 		err:							    \n\
424 			%1$s__destroy(obj);				    \n\
425 			return NULL;					    \n\
426 		}							    \n\
427 									    \n\
428 		static inline struct %1$s *				    \n\
429 		%1$s__open(void)					    \n\
430 		{							    \n\
431 			return %1$s__open_opts(NULL);			    \n\
432 		}							    \n\
433 									    \n\
434 		static inline int					    \n\
435 		%1$s__load(struct %1$s *obj)				    \n\
436 		{							    \n\
437 			return bpf_object__load_skeleton(obj->skeleton);    \n\
438 		}							    \n\
439 									    \n\
440 		static inline struct %1$s *				    \n\
441 		%1$s__open_and_load(void)				    \n\
442 		{							    \n\
443 			struct %1$s *obj;				    \n\
444 									    \n\
445 			obj = %1$s__open();				    \n\
446 			if (!obj)					    \n\
447 				return NULL;				    \n\
448 			if (%1$s__load(obj)) {				    \n\
449 				%1$s__destroy(obj);			    \n\
450 				return NULL;				    \n\
451 			}						    \n\
452 			return obj;					    \n\
453 		}							    \n\
454 									    \n\
455 		static inline int					    \n\
456 		%1$s__attach(struct %1$s *obj)				    \n\
457 		{							    \n\
458 			return bpf_object__attach_skeleton(obj->skeleton);  \n\
459 		}							    \n\
460 									    \n\
461 		static inline void					    \n\
462 		%1$s__detach(struct %1$s *obj)				    \n\
463 		{							    \n\
464 			return bpf_object__detach_skeleton(obj->skeleton);  \n\
465 		}							    \n\
466 		",
467 		obj_name
468 	);
469 
470 	codegen("\
471 		\n\
472 									    \n\
473 		static inline int					    \n\
474 		%1$s__create_skeleton(struct %1$s *obj)			    \n\
475 		{							    \n\
476 			struct bpf_object_skeleton *s;			    \n\
477 									    \n\
478 			s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\
479 			if (!s)						    \n\
480 				return -1;				    \n\
481 			obj->skeleton = s;				    \n\
482 									    \n\
483 			s->sz = sizeof(*s);				    \n\
484 			s->name = \"%1$s\";				    \n\
485 			s->obj = &obj->obj;				    \n\
486 		",
487 		obj_name
488 	);
489 	if (map_cnt) {
490 		codegen("\
491 			\n\
492 									    \n\
493 				/* maps */				    \n\
494 				s->map_cnt = %zu;			    \n\
495 				s->map_skel_sz = sizeof(*s->maps);	    \n\
496 				s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt, s->map_skel_sz);\n\
497 				if (!s->maps)				    \n\
498 					goto err;			    \n\
499 			",
500 			map_cnt
501 		);
502 		i = 0;
503 		bpf_object__for_each_map(map, obj) {
504 			ident = get_map_ident(map);
505 
506 			if (!ident)
507 				continue;
508 
509 			codegen("\
510 				\n\
511 									    \n\
512 					s->maps[%zu].name = \"%s\";	    \n\
513 					s->maps[%zu].map = &obj->maps.%s;   \n\
514 				",
515 				i, bpf_map__name(map), i, ident);
516 			/* memory-mapped internal maps */
517 			if (bpf_map__is_internal(map) &&
518 			    (bpf_map__def(map)->map_flags & BPF_F_MMAPABLE)) {
519 				printf("\ts->maps[%zu].mmaped = (void **)&obj->%s;\n",
520 				       i, ident);
521 			}
522 			i++;
523 		}
524 	}
525 	if (prog_cnt) {
526 		codegen("\
527 			\n\
528 									    \n\
529 				/* programs */				    \n\
530 				s->prog_cnt = %zu;			    \n\
531 				s->prog_skel_sz = sizeof(*s->progs);	    \n\
532 				s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);\n\
533 				if (!s->progs)				    \n\
534 					goto err;			    \n\
535 			",
536 			prog_cnt
537 		);
538 		i = 0;
539 		bpf_object__for_each_program(prog, obj) {
540 			codegen("\
541 				\n\
542 									    \n\
543 					s->progs[%1$zu].name = \"%2$s\";    \n\
544 					s->progs[%1$zu].prog = &obj->progs.%2$s;\n\
545 					s->progs[%1$zu].link = &obj->links.%2$s;\n\
546 				",
547 				i, bpf_program__name(prog));
548 			i++;
549 		}
550 	}
551 	codegen("\
552 		\n\
553 									    \n\
554 			s->data_sz = %d;				    \n\
555 			s->data = (void *)\"\\				    \n\
556 		",
557 		file_sz);
558 
559 	/* embed contents of BPF object file */
560 	for (i = 0, len = 0; i < file_sz; i++) {
561 		int w = obj_data[i] ? 4 : 2;
562 
563 		len += w;
564 		if (len > 78) {
565 			printf("\\\n");
566 			len = w;
567 		}
568 		if (!obj_data[i])
569 			printf("\\0");
570 		else
571 			printf("\\x%02x", (unsigned char)obj_data[i]);
572 	}
573 
574 	codegen("\
575 		\n\
576 		\";							    \n\
577 									    \n\
578 			return 0;					    \n\
579 		err:							    \n\
580 			bpf_object__destroy_skeleton(s);		    \n\
581 			return -1;					    \n\
582 		}							    \n\
583 									    \n\
584 		#endif /* %s */						    \n\
585 		",
586 		header_guard);
587 	err = 0;
588 out:
589 	bpf_object__close(obj);
590 	if (obj_data)
591 		munmap(obj_data, mmap_sz);
592 	close(fd);
593 	return err;
594 }
595 
596 static int do_help(int argc, char **argv)
597 {
598 	if (json_output) {
599 		jsonw_null(json_wtr);
600 		return 0;
601 	}
602 
603 	fprintf(stderr,
604 		"Usage: %1$s %2$s skeleton FILE\n"
605 		"       %1$s %2$s help\n"
606 		"\n"
607 		"       " HELP_SPEC_OPTIONS "\n"
608 		"",
609 		bin_name, "gen");
610 
611 	return 0;
612 }
613 
614 static const struct cmd cmds[] = {
615 	{ "skeleton",	do_skeleton },
616 	{ "help",	do_help },
617 	{ 0 }
618 };
619 
620 int do_gen(int argc, char **argv)
621 {
622 	return cmd_select(cmds, argc, argv, do_help);
623 }
624