提交 eab7fafe 编写于 作者: A Andrii Nakryiko 提交者: Zheng Zengkai

bpftool: Support multiple .rodata/.data internal maps in skeleton

mainline inclusion
from mainline-5.16-rc1
commit 8654b4d3
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8654b4d35e6c915ef456c14320ec8720383e81a7

-------------------------------------------------

Remove the assumption about only single instance of each of .rodata and
.data internal maps. Nothing changes for '.rodata' and '.data' maps, but new
'.rodata.something' map will get 'rodata_something' section in BPF
skeleton for them (as well as having struct bpf_map * field in maps
section with the same field name).
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Acked-by: NSong Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20211021014404.2635234-6-andrii@kernel.org
(cherry picked from commit 8654b4d3)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 0b38d6da
...@@ -33,6 +33,11 @@ static void sanitize_identifier(char *name) ...@@ -33,6 +33,11 @@ static void sanitize_identifier(char *name)
name[i] = '_'; name[i] = '_';
} }
static bool str_has_prefix(const char *str, const char *prefix)
{
return strncmp(str, prefix, strlen(prefix)) == 0;
}
static bool str_has_suffix(const char *str, const char *suffix) static bool str_has_suffix(const char *str, const char *suffix)
{ {
size_t i, n1 = strlen(str), n2 = strlen(suffix); size_t i, n1 = strlen(str), n2 = strlen(suffix);
...@@ -67,23 +72,47 @@ static void get_header_guard(char *guard, const char *obj_name) ...@@ -67,23 +72,47 @@ static void get_header_guard(char *guard, const char *obj_name)
guard[i] = toupper(guard[i]); guard[i] = toupper(guard[i]);
} }
static const char *get_map_ident(const struct bpf_map *map) static bool get_map_ident(const struct bpf_map *map, char *buf, size_t buf_sz)
{ {
static const char *sfxs[] = { ".data", ".rodata", ".bss", ".kconfig" };
const char *name = bpf_map__name(map); const char *name = bpf_map__name(map);
int i, n;
if (!bpf_map__is_internal(map)) {
snprintf(buf, buf_sz, "%s", name);
return true;
}
for (i = 0, n = ARRAY_SIZE(sfxs); i < n; i++) {
const char *sfx = sfxs[i], *p;
p = strstr(name, sfx);
if (p) {
snprintf(buf, buf_sz, "%s", p + 1);
sanitize_identifier(buf);
return true;
}
}
if (!bpf_map__is_internal(map)) return false;
return name; }
if (str_has_suffix(name, ".data")) static bool get_datasec_ident(const char *sec_name, char *buf, size_t buf_sz)
return "data"; {
else if (str_has_suffix(name, ".rodata")) static const char *pfxs[] = { ".data", ".rodata", ".bss", ".kconfig" };
return "rodata"; int i, n;
else if (str_has_suffix(name, ".bss"))
return "bss"; for (i = 0, n = ARRAY_SIZE(pfxs); i < n; i++) {
else if (str_has_suffix(name, ".kconfig")) const char *pfx = pfxs[i];
return "kconfig";
else if (str_has_prefix(sec_name, pfx)) {
return NULL; snprintf(buf, buf_sz, "%s", sec_name + 1);
sanitize_identifier(buf);
return true;
}
}
return false;
} }
static void codegen_btf_dump_printf(void *ctx, const char *fmt, va_list args) static void codegen_btf_dump_printf(void *ctx, const char *fmt, va_list args)
...@@ -100,24 +129,14 @@ static int codegen_datasec_def(struct bpf_object *obj, ...@@ -100,24 +129,14 @@ static int codegen_datasec_def(struct bpf_object *obj,
const char *sec_name = btf__name_by_offset(btf, sec->name_off); const char *sec_name = btf__name_by_offset(btf, sec->name_off);
const struct btf_var_secinfo *sec_var = btf_var_secinfos(sec); const struct btf_var_secinfo *sec_var = btf_var_secinfos(sec);
int i, err, off = 0, pad_cnt = 0, vlen = btf_vlen(sec); int i, err, off = 0, pad_cnt = 0, vlen = btf_vlen(sec);
const char *sec_ident; char var_ident[256], sec_ident[256];
char var_ident[256];
bool strip_mods = false; bool strip_mods = false;
if (strcmp(sec_name, ".data") == 0) { if (!get_datasec_ident(sec_name, sec_ident, sizeof(sec_ident)))
sec_ident = "data";
strip_mods = true;
} else if (strcmp(sec_name, ".bss") == 0) {
sec_ident = "bss";
strip_mods = true;
} else if (strcmp(sec_name, ".rodata") == 0) {
sec_ident = "rodata";
strip_mods = true;
} else if (strcmp(sec_name, ".kconfig") == 0) {
sec_ident = "kconfig";
} else {
return 0; return 0;
}
if (strcmp(sec_name, ".kconfig") != 0)
strip_mods = true;
printf(" struct %s__%s {\n", obj_name, sec_ident); printf(" struct %s__%s {\n", obj_name, sec_ident);
for (i = 0; i < vlen; i++, sec_var++) { for (i = 0; i < vlen; i++, sec_var++) {
...@@ -385,6 +404,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name) ...@@ -385,6 +404,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
{ {
struct bpf_program *prog; struct bpf_program *prog;
struct bpf_map *map; struct bpf_map *map;
char ident[256];
codegen("\ codegen("\
\n\ \n\
...@@ -405,10 +425,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name) ...@@ -405,10 +425,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
} }
bpf_object__for_each_map(map, obj) { bpf_object__for_each_map(map, obj) {
const char *ident; if (!get_map_ident(map, ident, sizeof(ident)))
ident = get_map_ident(map);
if (!ident)
continue; continue;
if (bpf_map__is_internal(map) && if (bpf_map__is_internal(map) &&
(bpf_map__def(map)->map_flags & BPF_F_MMAPABLE)) (bpf_map__def(map)->map_flags & BPF_F_MMAPABLE))
...@@ -432,6 +449,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h ...@@ -432,6 +449,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
struct bpf_object_load_attr load_attr = {}; struct bpf_object_load_attr load_attr = {};
DECLARE_LIBBPF_OPTS(gen_loader_opts, opts); DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
struct bpf_map *map; struct bpf_map *map;
char ident[256];
int err = 0; int err = 0;
err = bpf_object__gen_loader(obj, &opts); err = bpf_object__gen_loader(obj, &opts);
...@@ -477,12 +495,10 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h ...@@ -477,12 +495,10 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
", ",
obj_name, opts.data_sz); obj_name, opts.data_sz);
bpf_object__for_each_map(map, obj) { bpf_object__for_each_map(map, obj) {
const char *ident;
const void *mmap_data = NULL; const void *mmap_data = NULL;
size_t mmap_size = 0; size_t mmap_size = 0;
ident = get_map_ident(map); if (!get_map_ident(map, ident, sizeof(ident)))
if (!ident)
continue; continue;
if (!bpf_map__is_internal(map) || if (!bpf_map__is_internal(map) ||
...@@ -544,15 +560,15 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h ...@@ -544,15 +560,15 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
return err; \n\ return err; \n\
", obj_name); ", obj_name);
bpf_object__for_each_map(map, obj) { bpf_object__for_each_map(map, obj) {
const char *ident, *mmap_flags; const char *mmap_flags;
ident = get_map_ident(map); if (!get_map_ident(map, ident, sizeof(ident)))
if (!ident)
continue; continue;
if (!bpf_map__is_internal(map) || if (!bpf_map__is_internal(map) ||
!(bpf_map__def(map)->map_flags & BPF_F_MMAPABLE)) !(bpf_map__def(map)->map_flags & BPF_F_MMAPABLE))
continue; continue;
if (bpf_map__def(map)->map_flags & BPF_F_RDONLY_PROG) if (bpf_map__def(map)->map_flags & BPF_F_RDONLY_PROG)
mmap_flags = "PROT_READ"; mmap_flags = "PROT_READ";
else else
...@@ -602,7 +618,8 @@ static int do_skeleton(int argc, char **argv) ...@@ -602,7 +618,8 @@ static int do_skeleton(int argc, char **argv)
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts); DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data; char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data;
struct bpf_object *obj = NULL; struct bpf_object *obj = NULL;
const char *file, *ident; const char *file;
char ident[256];
struct bpf_program *prog; struct bpf_program *prog;
int fd, err = -1; int fd, err = -1;
struct bpf_map *map; struct bpf_map *map;
...@@ -673,8 +690,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -673,8 +690,7 @@ static int do_skeleton(int argc, char **argv)
} }
bpf_object__for_each_map(map, obj) { bpf_object__for_each_map(map, obj) {
ident = get_map_ident(map); if (!get_map_ident(map, ident, sizeof(ident))) {
if (!ident) {
p_err("ignoring unrecognized internal map '%s'...", p_err("ignoring unrecognized internal map '%s'...",
bpf_map__name(map)); bpf_map__name(map));
continue; continue;
...@@ -727,8 +743,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -727,8 +743,7 @@ static int do_skeleton(int argc, char **argv)
if (map_cnt) { if (map_cnt) {
printf("\tstruct {\n"); printf("\tstruct {\n");
bpf_object__for_each_map(map, obj) { bpf_object__for_each_map(map, obj) {
ident = get_map_ident(map); if (!get_map_ident(map, ident, sizeof(ident)))
if (!ident)
continue; continue;
if (use_loader) if (use_loader)
printf("\t\tstruct bpf_map_desc %s;\n", ident); printf("\t\tstruct bpf_map_desc %s;\n", ident);
...@@ -894,9 +909,7 @@ static int do_skeleton(int argc, char **argv) ...@@ -894,9 +909,7 @@ static int do_skeleton(int argc, char **argv)
); );
i = 0; i = 0;
bpf_object__for_each_map(map, obj) { bpf_object__for_each_map(map, obj) {
ident = get_map_ident(map); if (!get_map_ident(map, ident, sizeof(ident)))
if (!ident)
continue; continue;
codegen("\ codegen("\
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册