提交 67234743 编写于 作者: A Alexei Starovoitov 提交者: Daniel Borkmann

libbpf: Generate loader program out of BPF ELF file.

The BPF program loading process performed by libbpf is quite complex
and consists of the following steps:
"open" phase:
- parse elf file and remember relocations, sections
- collect externs and ksyms including their btf_ids in prog's BTF
- patch BTF datasec (since llvm couldn't do it)
- init maps (old style map_def, BTF based, global data map, kconfig map)
- collect relocations against progs and maps
"load" phase:
- probe kernel features
- load vmlinux BTF
- resolve externs (kconfig and ksym)
- load program BTF
- init struct_ops
- create maps
- apply CO-RE relocations
- patch ld_imm64 insns with src_reg=PSEUDO_MAP, PSEUDO_MAP_VALUE, PSEUDO_BTF_ID
- reposition subprograms and adjust call insns
- sanitize and load progs

During this process libbpf does sys_bpf() calls to load BTF, create maps,
populate maps and finally load programs.
Instead of actually doing the syscalls generate a trace of what libbpf
would have done and represent it as the "loader program".
The "loader program" consists of single map with:
- union bpf_attr(s)
- BTF bytes
- map value bytes
- insns bytes
and single bpf program that passes bpf_attr(s) and data into bpf_sys_bpf() helper.
Executing such "loader program" via bpf_prog_test_run() command will
replay the sequence of syscalls that libbpf would have done which will result
the same maps created and programs loaded as specified in the elf file.
The "loader program" removes libelf and majority of libbpf dependency from
program loading process.

kconfig, typeless ksym, struct_ops and CO-RE are not supported yet.

The order of relocate_data and relocate_calls had to change, so that
bpf_gen__prog_load() can see all relocations for a given program with
correct insn_idx-es.
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
Acked-by: NAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210514003623.28033-15-alexei.starovoitov@gmail.com
上级 e2fa0156
libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \ libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \
netlink.o bpf_prog_linfo.o libbpf_probes.o xsk.o hashmap.o \ netlink.o bpf_prog_linfo.o libbpf_probes.o xsk.o hashmap.o \
btf_dump.o ringbuf.o strset.o linker.o btf_dump.o ringbuf.o strset.o linker.o gen_loader.o
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/* Copyright (c) 2021 Facebook */
#ifndef __BPF_GEN_INTERNAL_H
#define __BPF_GEN_INTERNAL_H
struct ksym_relo_desc {
const char *name;
int kind;
int insn_idx;
};
struct bpf_gen {
struct gen_loader_opts *opts;
void *data_start;
void *data_cur;
void *insn_start;
void *insn_cur;
__u32 nr_progs;
__u32 nr_maps;
int log_level;
int error;
struct ksym_relo_desc *relos;
int relo_cnt;
char attach_target[128];
int attach_kind;
};
void bpf_gen__init(struct bpf_gen *gen, int log_level);
int bpf_gen__finish(struct bpf_gen *gen);
void bpf_gen__free(struct bpf_gen *gen);
void bpf_gen__load_btf(struct bpf_gen *gen, const void *raw_data, __u32 raw_size);
void bpf_gen__map_create(struct bpf_gen *gen, struct bpf_create_map_attr *map_attr, int map_idx);
struct bpf_prog_load_params;
void bpf_gen__prog_load(struct bpf_gen *gen, struct bpf_prog_load_params *load_attr, int prog_idx);
void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u32 value_size);
void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx);
void bpf_gen__record_attach_target(struct bpf_gen *gen, const char *name, enum bpf_attach_type type);
void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, int kind, int insn_idx);
#endif
此差异已折叠。
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
#include "str_error.h" #include "str_error.h"
#include "libbpf_internal.h" #include "libbpf_internal.h"
#include "hashmap.h" #include "hashmap.h"
#include "bpf_gen_internal.h"
#ifndef BPF_FS_MAGIC #ifndef BPF_FS_MAGIC
#define BPF_FS_MAGIC 0xcafe4a11 #define BPF_FS_MAGIC 0xcafe4a11
...@@ -412,8 +413,6 @@ struct module_btf { ...@@ -412,8 +413,6 @@ struct module_btf {
int fd; int fd;
}; };
struct bpf_gen;
struct bpf_object { struct bpf_object {
char name[BPF_OBJ_NAME_LEN]; char name[BPF_OBJ_NAME_LEN];
char license[64]; char license[64];
...@@ -2661,7 +2660,7 @@ static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force) ...@@ -2661,7 +2660,7 @@ static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
int err; int err;
/* btf_vmlinux could be loaded earlier */ /* btf_vmlinux could be loaded earlier */
if (obj->btf_vmlinux) if (obj->btf_vmlinux || obj->gen_loader)
return 0; return 0;
if (!force && !obj_needs_vmlinux_btf(obj)) if (!force && !obj_needs_vmlinux_btf(obj))
...@@ -2743,7 +2742,20 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj) ...@@ -2743,7 +2742,20 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
bpf_object__sanitize_btf(obj, kern_btf); bpf_object__sanitize_btf(obj, kern_btf);
} }
err = btf__load(kern_btf); if (obj->gen_loader) {
__u32 raw_size = 0;
const void *raw_data = btf__get_raw_data(kern_btf, &raw_size);
if (!raw_data)
return -ENOMEM;
bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size);
/* Pretend to have valid FD to pass various fd >= 0 checks.
* This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
*/
btf__set_fd(kern_btf, 0);
} else {
err = btf__load(kern_btf);
}
if (sanitize) { if (sanitize) {
if (!err) { if (!err) {
/* move fd to libbpf's BTF */ /* move fd to libbpf's BTF */
...@@ -4319,6 +4331,12 @@ static bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id f ...@@ -4319,6 +4331,12 @@ static bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id f
struct kern_feature_desc *feat = &feature_probes[feat_id]; struct kern_feature_desc *feat = &feature_probes[feat_id];
int ret; int ret;
if (obj->gen_loader)
/* To generate loader program assume the latest kernel
* to avoid doing extra prog_load, map_create syscalls.
*/
return true;
if (READ_ONCE(feat->res) == FEAT_UNKNOWN) { if (READ_ONCE(feat->res) == FEAT_UNKNOWN) {
ret = feat->probe(); ret = feat->probe();
if (ret > 0) { if (ret > 0) {
...@@ -4401,6 +4419,13 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) ...@@ -4401,6 +4419,13 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
char *cp, errmsg[STRERR_BUFSIZE]; char *cp, errmsg[STRERR_BUFSIZE];
int err, zero = 0; int err, zero = 0;
if (obj->gen_loader) {
bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps,
map->mmaped, map->def.value_size);
if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG)
bpf_gen__map_freeze(obj->gen_loader, map - obj->maps);
return 0;
}
err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0); err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
if (err) { if (err) {
err = -errno; err = -errno;
...@@ -4426,7 +4451,7 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map) ...@@ -4426,7 +4451,7 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
static void bpf_map__destroy(struct bpf_map *map); static void bpf_map__destroy(struct bpf_map *map);
static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map) static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
{ {
struct bpf_create_map_attr create_attr; struct bpf_create_map_attr create_attr;
struct bpf_map_def *def = &map->def; struct bpf_map_def *def = &map->def;
...@@ -4474,7 +4499,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map) ...@@ -4474,7 +4499,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map)
if (map->inner_map) { if (map->inner_map) {
int err; int err;
err = bpf_object__create_map(obj, map->inner_map); err = bpf_object__create_map(obj, map->inner_map, true);
if (err) { if (err) {
pr_warn("map '%s': failed to create inner map: %d\n", pr_warn("map '%s': failed to create inner map: %d\n",
map->name, err); map->name, err);
...@@ -4486,7 +4511,15 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map) ...@@ -4486,7 +4511,15 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map)
create_attr.inner_map_fd = map->inner_map_fd; create_attr.inner_map_fd = map->inner_map_fd;
} }
map->fd = bpf_create_map_xattr(&create_attr); if (obj->gen_loader) {
bpf_gen__map_create(obj->gen_loader, &create_attr, is_inner ? -1 : map - obj->maps);
/* Pretend to have valid FD to pass various fd >= 0 checks.
* This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
*/
map->fd = 0;
} else {
map->fd = bpf_create_map_xattr(&create_attr);
}
if (map->fd < 0 && (create_attr.btf_key_type_id || if (map->fd < 0 && (create_attr.btf_key_type_id ||
create_attr.btf_value_type_id)) { create_attr.btf_value_type_id)) {
char *cp, errmsg[STRERR_BUFSIZE]; char *cp, errmsg[STRERR_BUFSIZE];
...@@ -4507,6 +4540,8 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map) ...@@ -4507,6 +4540,8 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map)
return -errno; return -errno;
if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) { if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
if (obj->gen_loader)
map->inner_map->fd = -1;
bpf_map__destroy(map->inner_map); bpf_map__destroy(map->inner_map);
zfree(&map->inner_map); zfree(&map->inner_map);
} }
...@@ -4514,11 +4549,11 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map) ...@@ -4514,11 +4549,11 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map)
return 0; return 0;
} }
static int init_map_slots(struct bpf_map *map) static int init_map_slots(struct bpf_object *obj, struct bpf_map *map)
{ {
const struct bpf_map *targ_map; const struct bpf_map *targ_map;
unsigned int i; unsigned int i;
int fd, err; int fd, err = 0;
for (i = 0; i < map->init_slots_sz; i++) { for (i = 0; i < map->init_slots_sz; i++) {
if (!map->init_slots[i]) if (!map->init_slots[i])
...@@ -4526,7 +4561,13 @@ static int init_map_slots(struct bpf_map *map) ...@@ -4526,7 +4561,13 @@ static int init_map_slots(struct bpf_map *map)
targ_map = map->init_slots[i]; targ_map = map->init_slots[i];
fd = bpf_map__fd(targ_map); fd = bpf_map__fd(targ_map);
err = bpf_map_update_elem(map->fd, &i, &fd, 0); if (obj->gen_loader) {
pr_warn("// TODO map_update_elem: idx %ld key %d value==map_idx %ld\n",
map - obj->maps, i, targ_map - obj->maps);
return -ENOTSUP;
} else {
err = bpf_map_update_elem(map->fd, &i, &fd, 0);
}
if (err) { if (err) {
err = -errno; err = -errno;
pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n", pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
...@@ -4568,7 +4609,7 @@ bpf_object__create_maps(struct bpf_object *obj) ...@@ -4568,7 +4609,7 @@ bpf_object__create_maps(struct bpf_object *obj)
pr_debug("map '%s': skipping creation (preset fd=%d)\n", pr_debug("map '%s': skipping creation (preset fd=%d)\n",
map->name, map->fd); map->name, map->fd);
} else { } else {
err = bpf_object__create_map(obj, map); err = bpf_object__create_map(obj, map, false);
if (err) if (err)
goto err_out; goto err_out;
...@@ -4584,7 +4625,7 @@ bpf_object__create_maps(struct bpf_object *obj) ...@@ -4584,7 +4625,7 @@ bpf_object__create_maps(struct bpf_object *obj)
} }
if (map->init_slots_sz) { if (map->init_slots_sz) {
err = init_map_slots(map); err = init_map_slots(obj, map);
if (err < 0) { if (err < 0) {
zclose(map->fd); zclose(map->fd);
goto err_out; goto err_out;
...@@ -4994,6 +5035,9 @@ static int load_module_btfs(struct bpf_object *obj) ...@@ -4994,6 +5035,9 @@ static int load_module_btfs(struct bpf_object *obj)
if (obj->btf_modules_loaded) if (obj->btf_modules_loaded)
return 0; return 0;
if (obj->gen_loader)
return 0;
/* don't do this again, even if we find no module BTFs */ /* don't do this again, even if we find no module BTFs */
obj->btf_modules_loaded = true; obj->btf_modules_loaded = true;
...@@ -6141,6 +6185,12 @@ static int bpf_core_apply_relo(struct bpf_program *prog, ...@@ -6141,6 +6185,12 @@ static int bpf_core_apply_relo(struct bpf_program *prog,
if (str_is_empty(spec_str)) if (str_is_empty(spec_str))
return -EINVAL; return -EINVAL;
if (prog->obj->gen_loader) {
pr_warn("// TODO core_relo: prog %ld insn[%d] %s %s kind %d\n",
prog - prog->obj->programs, relo->insn_off / 8,
local_name, spec_str, relo->kind);
return -ENOTSUP;
}
err = bpf_core_parse_spec(local_btf, local_id, spec_str, relo->kind, &local_spec); err = bpf_core_parse_spec(local_btf, local_id, spec_str, relo->kind, &local_spec);
if (err) { if (err) {
pr_warn("prog '%s': relo #%d: parsing [%d] %s %s + %s failed: %d\n", pr_warn("prog '%s': relo #%d: parsing [%d] %s %s + %s failed: %d\n",
...@@ -6871,6 +6921,20 @@ bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog) ...@@ -6871,6 +6921,20 @@ bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog)
return 0; return 0;
} }
static void
bpf_object__free_relocs(struct bpf_object *obj)
{
struct bpf_program *prog;
int i;
/* free up relocation descriptors */
for (i = 0; i < obj->nr_programs; i++) {
prog = &obj->programs[i];
zfree(&prog->reloc_desc);
prog->nr_reloc = 0;
}
}
static int static int
bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
{ {
...@@ -6940,12 +7004,8 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path) ...@@ -6940,12 +7004,8 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
return err; return err;
} }
} }
/* free up relocation descriptors */ if (!obj->gen_loader)
for (i = 0; i < obj->nr_programs; i++) { bpf_object__free_relocs(obj);
prog = &obj->programs[i];
zfree(&prog->reloc_desc);
prog->nr_reloc = 0;
}
return 0; return 0;
} }
...@@ -7134,6 +7194,9 @@ static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program ...@@ -7134,6 +7194,9 @@ static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program
enum bpf_func_id func_id; enum bpf_func_id func_id;
int i; int i;
if (obj->gen_loader)
return 0;
for (i = 0; i < prog->insns_cnt; i++, insn++) { for (i = 0; i < prog->insns_cnt; i++, insn++) {
if (!insn_is_helper_call(insn, &func_id)) if (!insn_is_helper_call(insn, &func_id))
continue; continue;
...@@ -7218,6 +7281,12 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -7218,6 +7281,12 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
load_attr.log_level = prog->log_level; load_attr.log_level = prog->log_level;
load_attr.prog_flags = prog->prog_flags; load_attr.prog_flags = prog->prog_flags;
if (prog->obj->gen_loader) {
bpf_gen__prog_load(prog->obj->gen_loader, &load_attr,
prog - prog->obj->programs);
*pfd = -1;
return 0;
}
retry_load: retry_load:
if (log_buf_size) { if (log_buf_size) {
log_buf = malloc(log_buf_size); log_buf = malloc(log_buf_size);
...@@ -7295,6 +7364,38 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -7295,6 +7364,38 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
return ret; return ret;
} }
static int bpf_program__record_externs(struct bpf_program *prog)
{
struct bpf_object *obj = prog->obj;
int i;
for (i = 0; i < prog->nr_reloc; i++) {
struct reloc_desc *relo = &prog->reloc_desc[i];
struct extern_desc *ext = &obj->externs[relo->sym_off];
switch (relo->type) {
case RELO_EXTERN_VAR:
if (ext->type != EXT_KSYM)
continue;
if (!ext->ksym.type_id) {
pr_warn("typeless ksym %s is not supported yet\n",
ext->name);
return -ENOTSUP;
}
bpf_gen__record_extern(obj->gen_loader, ext->name, BTF_KIND_VAR,
relo->insn_idx);
break;
case RELO_EXTERN_FUNC:
bpf_gen__record_extern(obj->gen_loader, ext->name, BTF_KIND_FUNC,
relo->insn_idx);
break;
default:
continue;
}
}
return 0;
}
static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd, int *btf_type_id); static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd, int *btf_type_id);
int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
...@@ -7340,6 +7441,8 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver) ...@@ -7340,6 +7441,8 @@ int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
pr_warn("prog '%s': inconsistent nr(%d) != 1\n", pr_warn("prog '%s': inconsistent nr(%d) != 1\n",
prog->name, prog->instances.nr); prog->name, prog->instances.nr);
} }
if (prog->obj->gen_loader)
bpf_program__record_externs(prog);
err = load_program(prog, prog->insns, prog->insns_cnt, err = load_program(prog, prog->insns, prog->insns_cnt,
license, kern_ver, &fd); license, kern_ver, &fd);
if (!err) if (!err)
...@@ -7416,6 +7519,8 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level) ...@@ -7416,6 +7519,8 @@ bpf_object__load_progs(struct bpf_object *obj, int log_level)
if (err) if (err)
return err; return err;
} }
if (obj->gen_loader)
bpf_object__free_relocs(obj);
return 0; return 0;
} }
...@@ -7796,6 +7901,12 @@ static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj) ...@@ -7796,6 +7901,12 @@ static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj)
if (ext->type != EXT_KSYM || !ext->ksym.type_id) if (ext->type != EXT_KSYM || !ext->ksym.type_id)
continue; continue;
if (obj->gen_loader) {
ext->is_set = true;
ext->ksym.kernel_btf_obj_fd = 0;
ext->ksym.kernel_btf_id = 0;
continue;
}
t = btf__type_by_id(obj->btf, ext->btf_id); t = btf__type_by_id(obj->btf, ext->btf_id);
if (btf_is_var(t)) if (btf_is_var(t))
err = bpf_object__resolve_ksym_var_btf_id(obj, ext); err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
...@@ -7910,6 +8021,9 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) ...@@ -7910,6 +8021,9 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
return -EINVAL; return -EINVAL;
} }
if (obj->gen_loader)
bpf_gen__init(obj->gen_loader, attr->log_level);
err = bpf_object__probe_loading(obj); err = bpf_object__probe_loading(obj);
err = err ? : bpf_object__load_vmlinux_btf(obj, false); err = err ? : bpf_object__load_vmlinux_btf(obj, false);
err = err ? : bpf_object__resolve_externs(obj, obj->kconfig); err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
...@@ -7920,6 +8034,15 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) ...@@ -7920,6 +8034,15 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
err = err ? : bpf_object__relocate(obj, attr->target_btf_path); err = err ? : bpf_object__relocate(obj, attr->target_btf_path);
err = err ? : bpf_object__load_progs(obj, attr->log_level); err = err ? : bpf_object__load_progs(obj, attr->log_level);
if (obj->gen_loader) {
/* reset FDs */
btf__set_fd(obj->btf, -1);
for (i = 0; i < obj->nr_maps; i++)
obj->maps[i].fd = -1;
if (!err)
err = bpf_gen__finish(obj->gen_loader);
}
/* clean up module BTFs */ /* clean up module BTFs */
for (i = 0; i < obj->btf_module_cnt; i++) { for (i = 0; i < obj->btf_module_cnt; i++) {
close(obj->btf_modules[i].fd); close(obj->btf_modules[i].fd);
...@@ -8545,6 +8668,7 @@ void bpf_object__close(struct bpf_object *obj) ...@@ -8545,6 +8668,7 @@ void bpf_object__close(struct bpf_object *obj)
if (obj->clear_priv) if (obj->clear_priv)
obj->clear_priv(obj, obj->priv); obj->clear_priv(obj, obj->priv);
bpf_gen__free(obj->gen_loader);
bpf_object__elf_finish(obj); bpf_object__elf_finish(obj);
bpf_object__unload(obj); bpf_object__unload(obj);
btf__free(obj->btf); btf__free(obj->btf);
...@@ -8635,6 +8759,22 @@ void *bpf_object__priv(const struct bpf_object *obj) ...@@ -8635,6 +8759,22 @@ void *bpf_object__priv(const struct bpf_object *obj)
return obj ? obj->priv : ERR_PTR(-EINVAL); return obj ? obj->priv : ERR_PTR(-EINVAL);
} }
int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
{
struct bpf_gen *gen;
if (!opts)
return -EFAULT;
if (!OPTS_VALID(opts, gen_loader_opts))
return -EINVAL;
gen = calloc(sizeof(*gen), 1);
if (!gen)
return -ENOMEM;
gen->opts = opts;
obj->gen_loader = gen;
return 0;
}
static struct bpf_program * static struct bpf_program *
__bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj, __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
bool forward) bool forward)
...@@ -9272,6 +9412,28 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, ...@@ -9272,6 +9412,28 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
#define BTF_ITER_PREFIX "bpf_iter_" #define BTF_ITER_PREFIX "bpf_iter_"
#define BTF_MAX_NAME_SIZE 128 #define BTF_MAX_NAME_SIZE 128
void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
const char **prefix, int *kind)
{
switch (attach_type) {
case BPF_TRACE_RAW_TP:
*prefix = BTF_TRACE_PREFIX;
*kind = BTF_KIND_TYPEDEF;
break;
case BPF_LSM_MAC:
*prefix = BTF_LSM_PREFIX;
*kind = BTF_KIND_FUNC;
break;
case BPF_TRACE_ITER:
*prefix = BTF_ITER_PREFIX;
*kind = BTF_KIND_FUNC;
break;
default:
*prefix = "";
*kind = BTF_KIND_FUNC;
}
}
static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
const char *name, __u32 kind) const char *name, __u32 kind)
{ {
...@@ -9292,21 +9454,11 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix, ...@@ -9292,21 +9454,11 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
static inline int find_attach_btf_id(struct btf *btf, const char *name, static inline int find_attach_btf_id(struct btf *btf, const char *name,
enum bpf_attach_type attach_type) enum bpf_attach_type attach_type)
{ {
int err; const char *prefix;
int kind;
if (attach_type == BPF_TRACE_RAW_TP)
err = find_btf_by_prefix_kind(btf, BTF_TRACE_PREFIX, name,
BTF_KIND_TYPEDEF);
else if (attach_type == BPF_LSM_MAC)
err = find_btf_by_prefix_kind(btf, BTF_LSM_PREFIX, name,
BTF_KIND_FUNC);
else if (attach_type == BPF_TRACE_ITER)
err = find_btf_by_prefix_kind(btf, BTF_ITER_PREFIX, name,
BTF_KIND_FUNC);
else
err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
return err; btf_get_kernel_prefix_kind(attach_type, &prefix, &kind);
return find_btf_by_prefix_kind(btf, prefix, name, kind);
} }
int libbpf_find_vmlinux_btf_id(const char *name, int libbpf_find_vmlinux_btf_id(const char *name,
...@@ -9405,7 +9557,7 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd, ...@@ -9405,7 +9557,7 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd,
__u32 attach_prog_fd = prog->attach_prog_fd; __u32 attach_prog_fd = prog->attach_prog_fd;
const char *name = prog->sec_name, *attach_name; const char *name = prog->sec_name, *attach_name;
const struct bpf_sec_def *sec = NULL; const struct bpf_sec_def *sec = NULL;
int i, err; int i, err = 0;
if (!name) if (!name)
return -EINVAL; return -EINVAL;
...@@ -9440,7 +9592,13 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd, ...@@ -9440,7 +9592,13 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd,
} }
/* kernel/module BTF ID */ /* kernel/module BTF ID */
err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id); if (prog->obj->gen_loader) {
bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type);
*btf_obj_fd = 0;
*btf_type_id = 1;
} else {
err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id);
}
if (err) { if (err) {
pr_warn("failed to find kernel BTF type ID of '%s': %d\n", attach_name, err); pr_warn("failed to find kernel BTF type ID of '%s': %d\n", attach_name, err);
return err; return err;
......
...@@ -800,6 +800,18 @@ LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s); ...@@ -800,6 +800,18 @@ LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s);
LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s); LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s);
LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s); LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s);
struct gen_loader_opts {
size_t sz; /* size of this struct, for forward/backward compatiblity */
const char *data;
const char *insns;
__u32 data_sz;
__u32 insns_sz;
};
#define gen_loader_opts__last_field insns_sz
LIBBPF_API int bpf_object__gen_loader(struct bpf_object *obj,
struct gen_loader_opts *opts);
enum libbpf_tristate { enum libbpf_tristate {
TRI_NO = 0, TRI_NO = 0,
TRI_YES = 1, TRI_YES = 1,
......
...@@ -360,6 +360,7 @@ LIBBPF_0.4.0 { ...@@ -360,6 +360,7 @@ LIBBPF_0.4.0 {
bpf_linker__free; bpf_linker__free;
bpf_linker__new; bpf_linker__new;
bpf_map__inner_map; bpf_map__inner_map;
bpf_object__gen_loader;
bpf_object__set_kversion; bpf_object__set_kversion;
bpf_tc_attach; bpf_tc_attach;
bpf_tc_detach; bpf_tc_detach;
......
...@@ -258,6 +258,8 @@ int bpf_object__section_size(const struct bpf_object *obj, const char *name, ...@@ -258,6 +258,8 @@ int bpf_object__section_size(const struct bpf_object *obj, const char *name,
int bpf_object__variable_offset(const struct bpf_object *obj, const char *name, int bpf_object__variable_offset(const struct bpf_object *obj, const char *name,
__u32 *off); __u32 *off);
struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf); struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf);
void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
const char **prefix, int *kind);
struct btf_ext_info { struct btf_ext_info {
/* /*
......
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/* Copyright (c) 2021 Facebook */
#ifndef __SKEL_INTERNAL_H
#define __SKEL_INTERNAL_H
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/mman.h>
/* This file is a base header for auto-generated *.lskel.h files.
* Its contents will change and may become part of auto-generation in the future.
*
* The layout of bpf_[map|prog]_desc and bpf_loader_ctx is feature dependent
* and will change from one version of libbpf to another and features
* requested during loader program generation.
*/
struct bpf_map_desc {
union {
/* input for the loader prog */
struct {
__aligned_u64 initial_value;
__u32 max_entries;
};
/* output of the loader prog */
struct {
int map_fd;
};
};
};
struct bpf_prog_desc {
int prog_fd;
};
struct bpf_loader_ctx {
size_t sz;
__u32 log_level;
__u32 log_size;
__u64 log_buf;
};
struct bpf_load_and_run_opts {
struct bpf_loader_ctx *ctx;
const void *data;
const void *insns;
__u32 data_sz;
__u32 insns_sz;
const char *errstr;
};
static inline int skel_sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
static inline int skel_closenz(int fd)
{
if (fd > 0)
return close(fd);
return -EINVAL;
}
static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts)
{
int map_fd = -1, prog_fd = -1, key = 0, err;
union bpf_attr attr;
map_fd = bpf_create_map_name(BPF_MAP_TYPE_ARRAY, "__loader.map", 4,
opts->data_sz, 1, 0);
if (map_fd < 0) {
opts->errstr = "failed to create loader map";
err = -errno;
goto out;
}
err = bpf_map_update_elem(map_fd, &key, opts->data, 0);
if (err < 0) {
opts->errstr = "failed to update loader map";
err = -errno;
goto out;
}
memset(&attr, 0, sizeof(attr));
attr.prog_type = BPF_PROG_TYPE_SYSCALL;
attr.insns = (long) opts->insns;
attr.insn_cnt = opts->insns_sz / sizeof(struct bpf_insn);
attr.license = (long) "Dual BSD/GPL";
memcpy(attr.prog_name, "__loader.prog", sizeof("__loader.prog"));
attr.fd_array = (long) &map_fd;
attr.log_level = opts->ctx->log_level;
attr.log_size = opts->ctx->log_size;
attr.log_buf = opts->ctx->log_buf;
attr.prog_flags = BPF_F_SLEEPABLE;
prog_fd = skel_sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
if (prog_fd < 0) {
opts->errstr = "failed to load loader prog";
err = -errno;
goto out;
}
memset(&attr, 0, sizeof(attr));
attr.test.prog_fd = prog_fd;
attr.test.ctx_in = (long) opts->ctx;
attr.test.ctx_size_in = opts->ctx->sz;
err = skel_sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
if (err < 0 || (int)attr.test.retval < 0) {
opts->errstr = "failed to execute loader prog";
if (err < 0)
err = -errno;
else
err = (int)attr.test.retval;
goto out;
}
err = 0;
out:
if (map_fd >= 0)
close(map_fd);
if (prog_fd >= 0)
close(prog_fd);
return err;
}
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册