Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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