|
|
8def76 |
From e8386c4e1fa3b5486487fa4d6c350a0d5e300aaf Mon Sep 17 00:00:00 2001
|
|
|
8def76 |
From: Andrea Claudi <aclaudi@redhat.com>
|
|
|
8def76 |
Date: Thu, 13 Jun 2019 14:37:56 +0200
|
|
|
8def76 |
Subject: [PATCH] bpf: implement btf handling and map annotation
|
|
|
8def76 |
|
|
|
8def76 |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1716361
|
|
|
8def76 |
Upstream Status: iproute2.git commit f823f36012fb5
|
|
|
8def76 |
|
|
|
8def76 |
commit f823f36012fb5ab4ddfca6ed4ff56188730f281e
|
|
|
8def76 |
Author: Daniel Borkmann <daniel@iogearbox.net>
|
|
|
8def76 |
Date: Wed Jul 18 01:31:22 2018 +0200
|
|
|
8def76 |
|
|
|
8def76 |
bpf: implement btf handling and map annotation
|
|
|
8def76 |
|
|
|
8def76 |
Implement loading of .BTF section from object file and build up
|
|
|
8def76 |
internal table for retrieving key/value id related to maps in
|
|
|
8def76 |
the BPF program. Latter is done by setting up struct btf_type
|
|
|
8def76 |
table.
|
|
|
8def76 |
|
|
|
8def76 |
One of the issues is that there's a disconnect between the data
|
|
|
8def76 |
types used in the map and struct bpf_elf_map, meaning the underlying
|
|
|
8def76 |
types are unknown from the map description. One way to overcome
|
|
|
8def76 |
this is to add a annotation such that the loader will recognize
|
|
|
8def76 |
the relation to both. BPF_ANNOTATE_KV_PAIR(map_foo, struct key,
|
|
|
8def76 |
struct val); has been added to the API that programs can use.
|
|
|
8def76 |
|
|
|
8def76 |
The loader will then pick the corresponding key/value type ids and
|
|
|
8def76 |
attach it to the maps for creation. This can later on be dumped via
|
|
|
8def76 |
bpftool for introspection.
|
|
|
8def76 |
|
|
|
8def76 |
Example with test_xdp_noinline.o from kernel selftests:
|
|
|
8def76 |
|
|
|
8def76 |
[...]
|
|
|
8def76 |
|
|
|
8def76 |
struct ctl_value {
|
|
|
8def76 |
union {
|
|
|
8def76 |
__u64 value;
|
|
|
8def76 |
__u32 ifindex;
|
|
|
8def76 |
__u8 mac[6];
|
|
|
8def76 |
};
|
|
|
8def76 |
};
|
|
|
8def76 |
|
|
|
8def76 |
struct bpf_map_def __attribute__ ((section("maps"), used)) ctl_array = {
|
|
|
8def76 |
.type = BPF_MAP_TYPE_ARRAY,
|
|
|
8def76 |
.key_size = sizeof(__u32),
|
|
|
8def76 |
.value_size = sizeof(struct ctl_value),
|
|
|
8def76 |
.max_entries = 16,
|
|
|
8def76 |
.map_flags = 0,
|
|
|
8def76 |
};
|
|
|
8def76 |
BPF_ANNOTATE_KV_PAIR(ctl_array, __u32, struct ctl_value);
|
|
|
8def76 |
|
|
|
8def76 |
[...]
|
|
|
8def76 |
|
|
|
8def76 |
Above could also further be wrapped in a macro. Compiling through LLVM and
|
|
|
8def76 |
converting to BTF:
|
|
|
8def76 |
|
|
|
8def76 |
# llc --version
|
|
|
8def76 |
LLVM (http://llvm.org/):
|
|
|
8def76 |
LLVM version 7.0.0svn
|
|
|
8def76 |
Optimized build.
|
|
|
8def76 |
Default target: x86_64-unknown-linux-gnu
|
|
|
8def76 |
Host CPU: skylake
|
|
|
8def76 |
|
|
|
8def76 |
Registered Targets:
|
|
|
8def76 |
bpf - BPF (host endian)
|
|
|
8def76 |
bpfeb - BPF (big endian)
|
|
|
8def76 |
bpfel - BPF (little endian)
|
|
|
8def76 |
[...]
|
|
|
8def76 |
|
|
|
8def76 |
# clang [...] -O2 -target bpf -g -emit-llvm -c test_xdp_noinline.c -o - |
|
|
|
8def76 |
llc -march=bpf -mcpu=probe -mattr=dwarfris -filetype=obj -o test_xdp_noinline.o
|
|
|
8def76 |
# pahole -J test_xdp_noinline.o
|
|
|
8def76 |
|
|
|
8def76 |
Checking pahole dump of BPF object file:
|
|
|
8def76 |
|
|
|
8def76 |
# file test_xdp_noinline.o
|
|
|
8def76 |
test_xdp_noinline.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), with debug_info, not stripped
|
|
|
8def76 |
# pahole test_xdp_noinline.o
|
|
|
8def76 |
[...]
|
|
|
8def76 |
struct ctl_value {
|
|
|
8def76 |
union {
|
|
|
8def76 |
__u64 value; /* 0 8 */
|
|
|
8def76 |
__u32 ifindex; /* 0 4 */
|
|
|
8def76 |
__u8 mac[0]; /* 0 0 */
|
|
|
8def76 |
}; /* 0 8 */
|
|
|
8def76 |
|
|
|
8def76 |
/* size: 8, cachelines: 1, members: 1 */
|
|
|
8def76 |
/* last cacheline: 8 bytes */
|
|
|
8def76 |
};
|
|
|
8def76 |
|
|
|
8def76 |
Now loading into kernel and dumping the map via bpftool:
|
|
|
8def76 |
|
|
|
8def76 |
# ip -force link set dev lo xdp obj test_xdp_noinline.o sec xdp-test
|
|
|
8def76 |
# ip a
|
|
|
8def76 |
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 xdpgeneric/id:227 qdisc noqueue state UNKNOWN group default qlen 1000
|
|
|
8def76 |
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
|
|
|
8def76 |
inet 127.0.0.1/8 scope host lo
|
|
|
8def76 |
valid_lft forever preferred_lft forever
|
|
|
8def76 |
inet6 ::1/128 scope host
|
|
|
8def76 |
valid_lft forever preferred_lft forever
|
|
|
8def76 |
[...]
|
|
|
8def76 |
# bpftool prog show id 227
|
|
|
8def76 |
227: xdp tag a85e060c275c5616 gpl
|
|
|
8def76 |
loaded_at 2018-07-17T14:41:29+0000 uid 0
|
|
|
8def76 |
xlated 8152B not jited memlock 12288B map_ids 381,385,386,382,384,383
|
|
|
8def76 |
# bpftool map dump id 386
|
|
|
8def76 |
[{
|
|
|
8def76 |
"key": 0,
|
|
|
8def76 |
"value": {
|
|
|
8def76 |
"": {
|
|
|
8def76 |
"value": 0,
|
|
|
8def76 |
"ifindex": 0,
|
|
|
8def76 |
"mac": []
|
|
|
8def76 |
}
|
|
|
8def76 |
}
|
|
|
8def76 |
},{
|
|
|
8def76 |
"key": 1,
|
|
|
8def76 |
"value": {
|
|
|
8def76 |
"": {
|
|
|
8def76 |
"value": 0,
|
|
|
8def76 |
"ifindex": 0,
|
|
|
8def76 |
"mac": []
|
|
|
8def76 |
}
|
|
|
8def76 |
}
|
|
|
8def76 |
},{
|
|
|
8def76 |
[...]
|
|
|
8def76 |
|
|
|
8def76 |
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
|
|
|
8def76 |
Signed-off-by: David Ahern <dsahern@gmail.com>
|
|
|
8def76 |
---
|
|
|
8def76 |
include/bpf_elf.h | 9 ++
|
|
|
8def76 |
include/bpf_util.h | 1 +
|
|
|
8def76 |
lib/bpf.c | 332 ++++++++++++++++++++++++++++++++++++++++++++-
|
|
|
8def76 |
3 files changed, 338 insertions(+), 4 deletions(-)
|
|
|
8def76 |
|
|
|
8def76 |
diff --git a/include/bpf_elf.h b/include/bpf_elf.h
|
|
|
8def76 |
index a8e360f3bbb28..84e8ae00834c8 100644
|
|
|
8def76 |
--- a/include/bpf_elf.h
|
|
|
8def76 |
+++ b/include/bpf_elf.h
|
|
|
8def76 |
@@ -41,4 +41,13 @@ struct bpf_elf_map {
|
|
|
8def76 |
__u32 inner_idx;
|
|
|
8def76 |
};
|
|
|
8def76 |
|
|
|
8def76 |
+#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
|
|
|
8def76 |
+ struct ____btf_map_##name { \
|
|
|
8def76 |
+ type_key key; \
|
|
|
8def76 |
+ type_val value; \
|
|
|
8def76 |
+ }; \
|
|
|
8def76 |
+ struct ____btf_map_##name \
|
|
|
8def76 |
+ __attribute__ ((section(".maps." #name), used)) \
|
|
|
8def76 |
+ ____btf_map_##name = { }
|
|
|
8def76 |
+
|
|
|
8def76 |
#endif /* __BPF_ELF__ */
|
|
|
8def76 |
diff --git a/include/bpf_util.h b/include/bpf_util.h
|
|
|
8def76 |
index 219beb40cd253..63837a04e56fe 100644
|
|
|
8def76 |
--- a/include/bpf_util.h
|
|
|
8def76 |
+++ b/include/bpf_util.h
|
|
|
8def76 |
@@ -14,6 +14,7 @@
|
|
|
8def76 |
#define __BPF_UTIL__
|
|
|
8def76 |
|
|
|
8def76 |
#include <linux/bpf.h>
|
|
|
8def76 |
+#include <linux/btf.h>
|
|
|
8def76 |
#include <linux/filter.h>
|
|
|
8def76 |
#include <linux/magic.h>
|
|
|
8def76 |
#include <linux/elf-em.h>
|
|
|
8def76 |
diff --git a/lib/bpf.c b/lib/bpf.c
|
|
|
8def76 |
index 1b87490555050..d093d0bd86eae 100644
|
|
|
8def76 |
--- a/lib/bpf.c
|
|
|
8def76 |
+++ b/lib/bpf.c
|
|
|
8def76 |
@@ -393,6 +393,8 @@ struct bpf_prog_data {
|
|
|
8def76 |
|
|
|
8def76 |
struct bpf_map_ext {
|
|
|
8def76 |
struct bpf_prog_data owner;
|
|
|
8def76 |
+ unsigned int btf_id_key;
|
|
|
8def76 |
+ unsigned int btf_id_val;
|
|
|
8def76 |
};
|
|
|
8def76 |
|
|
|
8def76 |
static int bpf_derive_elf_map_from_fdinfo(int fd, struct bpf_elf_map *map,
|
|
|
8def76 |
@@ -1125,24 +1127,36 @@ struct bpf_config {
|
|
|
8def76 |
unsigned int jit_enabled;
|
|
|
8def76 |
};
|
|
|
8def76 |
|
|
|
8def76 |
+struct bpf_btf {
|
|
|
8def76 |
+ const struct btf_header *hdr;
|
|
|
8def76 |
+ const void *raw;
|
|
|
8def76 |
+ const char *strings;
|
|
|
8def76 |
+ const struct btf_type **types;
|
|
|
8def76 |
+ int types_num;
|
|
|
8def76 |
+};
|
|
|
8def76 |
+
|
|
|
8def76 |
struct bpf_elf_ctx {
|
|
|
8def76 |
struct bpf_config cfg;
|
|
|
8def76 |
Elf *elf_fd;
|
|
|
8def76 |
GElf_Ehdr elf_hdr;
|
|
|
8def76 |
Elf_Data *sym_tab;
|
|
|
8def76 |
Elf_Data *str_tab;
|
|
|
8def76 |
+ Elf_Data *btf_data;
|
|
|
8def76 |
char obj_uid[64];
|
|
|
8def76 |
int obj_fd;
|
|
|
8def76 |
+ int btf_fd;
|
|
|
8def76 |
int map_fds[ELF_MAX_MAPS];
|
|
|
8def76 |
struct bpf_elf_map maps[ELF_MAX_MAPS];
|
|
|
8def76 |
struct bpf_map_ext maps_ext[ELF_MAX_MAPS];
|
|
|
8def76 |
struct bpf_elf_prog prog_text;
|
|
|
8def76 |
+ struct bpf_btf btf;
|
|
|
8def76 |
int sym_num;
|
|
|
8def76 |
int map_num;
|
|
|
8def76 |
int map_len;
|
|
|
8def76 |
bool *sec_done;
|
|
|
8def76 |
int sec_maps;
|
|
|
8def76 |
int sec_text;
|
|
|
8def76 |
+ int sec_btf;
|
|
|
8def76 |
char license[ELF_MAX_LICENSE_LEN];
|
|
|
8def76 |
enum bpf_prog_type type;
|
|
|
8def76 |
__u32 ifindex;
|
|
|
8def76 |
@@ -1167,6 +1181,11 @@ struct bpf_map_data {
|
|
|
8def76 |
struct bpf_elf_map *ent;
|
|
|
8def76 |
};
|
|
|
8def76 |
|
|
|
8def76 |
+static bool bpf_log_has_data(struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ return ctx->log && ctx->log[0];
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
static __check_format_string(2, 3) void
|
|
|
8def76 |
bpf_dump_error(struct bpf_elf_ctx *ctx, const char *format, ...)
|
|
|
8def76 |
{
|
|
|
8def76 |
@@ -1176,7 +1195,7 @@ bpf_dump_error(struct bpf_elf_ctx *ctx, const char *format, ...)
|
|
|
8def76 |
vfprintf(stderr, format, vl);
|
|
|
8def76 |
va_end(vl);
|
|
|
8def76 |
|
|
|
8def76 |
- if (ctx->log && ctx->log[0]) {
|
|
|
8def76 |
+ if (bpf_log_has_data(ctx)) {
|
|
|
8def76 |
if (ctx->verbose) {
|
|
|
8def76 |
fprintf(stderr, "%s\n", ctx->log);
|
|
|
8def76 |
} else {
|
|
|
8def76 |
@@ -1223,7 +1242,9 @@ static int bpf_log_realloc(struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
|
|
|
8def76 |
static int bpf_map_create(enum bpf_map_type type, uint32_t size_key,
|
|
|
8def76 |
uint32_t size_value, uint32_t max_elem,
|
|
|
8def76 |
- uint32_t flags, int inner_fd, uint32_t ifindex)
|
|
|
8def76 |
+ uint32_t flags, int inner_fd, int btf_fd,
|
|
|
8def76 |
+ uint32_t ifindex, uint32_t btf_id_key,
|
|
|
8def76 |
+ uint32_t btf_id_val)
|
|
|
8def76 |
{
|
|
|
8def76 |
union bpf_attr attr = {};
|
|
|
8def76 |
|
|
|
8def76 |
@@ -1234,10 +1255,30 @@ static int bpf_map_create(enum bpf_map_type type, uint32_t size_key,
|
|
|
8def76 |
attr.map_flags = flags;
|
|
|
8def76 |
attr.inner_map_fd = inner_fd;
|
|
|
8def76 |
attr.map_ifindex = ifindex;
|
|
|
8def76 |
+ attr.btf_fd = btf_fd;
|
|
|
8def76 |
+ attr.btf_key_type_id = btf_id_key;
|
|
|
8def76 |
+ attr.btf_value_type_id = btf_id_val;
|
|
|
8def76 |
|
|
|
8def76 |
return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
+static int bpf_btf_load(void *btf, size_t size_btf,
|
|
|
8def76 |
+ char *log, size_t size_log)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ union bpf_attr attr = {};
|
|
|
8def76 |
+
|
|
|
8def76 |
+ attr.btf = bpf_ptr_to_u64(btf);
|
|
|
8def76 |
+ attr.btf_size = size_btf;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (size_log > 0) {
|
|
|
8def76 |
+ attr.btf_log_buf = bpf_ptr_to_u64(log);
|
|
|
8def76 |
+ attr.btf_log_size = size_log;
|
|
|
8def76 |
+ attr.btf_log_level = 1;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ return bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
static int bpf_obj_pin(int fd, const char *pathname)
|
|
|
8def76 |
{
|
|
|
8def76 |
union bpf_attr attr = {};
|
|
|
8def76 |
@@ -1613,7 +1654,8 @@ static int bpf_map_attach(const char *name, struct bpf_elf_ctx *ctx,
|
|
|
8def76 |
ifindex = bpf_map_offload_neutral(map->type) ? 0 : ctx->ifindex;
|
|
|
8def76 |
errno = 0;
|
|
|
8def76 |
fd = bpf_map_create(map->type, map->size_key, map->size_value,
|
|
|
8def76 |
- map->max_elem, map->flags, map_inner_fd, ifindex);
|
|
|
8def76 |
+ map->max_elem, map->flags, map_inner_fd, ctx->btf_fd,
|
|
|
8def76 |
+ ifindex, ext->btf_id_key, ext->btf_id_val);
|
|
|
8def76 |
|
|
|
8def76 |
if (fd < 0 || ctx->verbose) {
|
|
|
8def76 |
bpf_map_report(fd, name, map, ctx, map_inner_fd);
|
|
|
8def76 |
@@ -1638,8 +1680,80 @@ static const char *bpf_str_tab_name(const struct bpf_elf_ctx *ctx,
|
|
|
8def76 |
return ctx->str_tab->d_buf + sym->st_name;
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
+static int bpf_btf_find(struct bpf_elf_ctx *ctx, const char *name)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ const struct btf_type *type;
|
|
|
8def76 |
+ const char *res;
|
|
|
8def76 |
+ int id;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ for (id = 1; id < ctx->btf.types_num; id++) {
|
|
|
8def76 |
+ type = ctx->btf.types[id];
|
|
|
8def76 |
+ if (type->name_off >= ctx->btf.hdr->str_len)
|
|
|
8def76 |
+ continue;
|
|
|
8def76 |
+ res = &ctx->btf.strings[type->name_off];
|
|
|
8def76 |
+ if (!strcmp(res, name))
|
|
|
8def76 |
+ return id;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ return -ENOENT;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static int bpf_btf_find_kv(struct bpf_elf_ctx *ctx, const struct bpf_elf_map *map,
|
|
|
8def76 |
+ const char *name, uint32_t *id_key, uint32_t *id_val)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ const struct btf_member *key, *val;
|
|
|
8def76 |
+ const struct btf_type *type;
|
|
|
8def76 |
+ char btf_name[512];
|
|
|
8def76 |
+ const char *res;
|
|
|
8def76 |
+ int id;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ snprintf(btf_name, sizeof(btf_name), "____btf_map_%s", name);
|
|
|
8def76 |
+ id = bpf_btf_find(ctx, btf_name);
|
|
|
8def76 |
+ if (id < 0)
|
|
|
8def76 |
+ return id;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ type = ctx->btf.types[id];
|
|
|
8def76 |
+ if (BTF_INFO_KIND(type->info) != BTF_KIND_STRUCT)
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+ if (BTF_INFO_VLEN(type->info) != 2)
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ key = ((void *) type) + sizeof(*type);
|
|
|
8def76 |
+ val = key + 1;
|
|
|
8def76 |
+ if (!key->type || key->type >= ctx->btf.types_num ||
|
|
|
8def76 |
+ !val->type || val->type >= ctx->btf.types_num)
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (key->name_off >= ctx->btf.hdr->str_len ||
|
|
|
8def76 |
+ val->name_off >= ctx->btf.hdr->str_len)
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ res = &ctx->btf.strings[key->name_off];
|
|
|
8def76 |
+ if (strcmp(res, "key"))
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ res = &ctx->btf.strings[val->name_off];
|
|
|
8def76 |
+ if (strcmp(res, "value"))
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ *id_key = key->type;
|
|
|
8def76 |
+ *id_val = val->type;
|
|
|
8def76 |
+ return 0;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static void bpf_btf_annotate(struct bpf_elf_ctx *ctx, int which, const char *name)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ uint32_t id_key = 0, id_val = 0;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (!bpf_btf_find_kv(ctx, &ctx->maps[which], name, &id_key, &id_val)) {
|
|
|
8def76 |
+ ctx->maps_ext[which].btf_id_key = id_key;
|
|
|
8def76 |
+ ctx->maps_ext[which].btf_id_val = id_val;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
static const char *bpf_map_fetch_name(struct bpf_elf_ctx *ctx, int which)
|
|
|
8def76 |
{
|
|
|
8def76 |
+ const char *name;
|
|
|
8def76 |
GElf_Sym sym;
|
|
|
8def76 |
int i;
|
|
|
8def76 |
|
|
|
8def76 |
@@ -1653,7 +1767,9 @@ static const char *bpf_map_fetch_name(struct bpf_elf_ctx *ctx, int which)
|
|
|
8def76 |
sym.st_value / ctx->map_len != which)
|
|
|
8def76 |
continue;
|
|
|
8def76 |
|
|
|
8def76 |
- return bpf_str_tab_name(ctx, &sym);
|
|
|
8def76 |
+ name = bpf_str_tab_name(ctx, &sym);
|
|
|
8def76 |
+ bpf_btf_annotate(ctx, which, name);
|
|
|
8def76 |
+ return name;
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
return NULL;
|
|
|
8def76 |
@@ -1915,11 +2031,210 @@ static int bpf_fetch_text(struct bpf_elf_ctx *ctx, int section,
|
|
|
8def76 |
return 0;
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
+static void bpf_btf_report(int fd, struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ fprintf(stderr, "\nBTF debug data section \'.BTF\' %s%s (%d)!\n",
|
|
|
8def76 |
+ fd < 0 ? "rejected: " : "loaded",
|
|
|
8def76 |
+ fd < 0 ? strerror(errno) : "",
|
|
|
8def76 |
+ fd < 0 ? errno : fd);
|
|
|
8def76 |
+
|
|
|
8def76 |
+ fprintf(stderr, " - Length: %zu\n", ctx->btf_data->d_size);
|
|
|
8def76 |
+
|
|
|
8def76 |
+ bpf_dump_error(ctx, "Verifier analysis:\n\n");
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static int bpf_btf_attach(struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ int tries = 0, fd;
|
|
|
8def76 |
+retry:
|
|
|
8def76 |
+ errno = 0;
|
|
|
8def76 |
+ fd = bpf_btf_load(ctx->btf_data->d_buf, ctx->btf_data->d_size,
|
|
|
8def76 |
+ ctx->log, ctx->log_size);
|
|
|
8def76 |
+ if (fd < 0 || ctx->verbose) {
|
|
|
8def76 |
+ if (fd < 0 && (errno == ENOSPC || !ctx->log_size)) {
|
|
|
8def76 |
+ if (tries++ < 10 && !bpf_log_realloc(ctx))
|
|
|
8def76 |
+ goto retry;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ fprintf(stderr, "Log buffer too small to dump verifier log %zu bytes (%d tries)!\n",
|
|
|
8def76 |
+ ctx->log_size, tries);
|
|
|
8def76 |
+ return fd;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (bpf_log_has_data(ctx))
|
|
|
8def76 |
+ bpf_btf_report(fd, ctx);
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ return fd;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static int bpf_fetch_btf_begin(struct bpf_elf_ctx *ctx, int section,
|
|
|
8def76 |
+ struct bpf_elf_sec_data *data)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ ctx->btf_data = data->sec_data;
|
|
|
8def76 |
+ ctx->sec_btf = section;
|
|
|
8def76 |
+ ctx->sec_done[section] = true;
|
|
|
8def76 |
+ return 0;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static int bpf_btf_check_header(struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ const struct btf_header *hdr = ctx->btf_data->d_buf;
|
|
|
8def76 |
+ const char *str_start, *str_end;
|
|
|
8def76 |
+ unsigned int data_len;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (hdr->magic != BTF_MAGIC) {
|
|
|
8def76 |
+ fprintf(stderr, "Object has wrong BTF magic: %x, expected: %x!\n",
|
|
|
8def76 |
+ hdr->magic, BTF_MAGIC);
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (hdr->version != BTF_VERSION) {
|
|
|
8def76 |
+ fprintf(stderr, "Object has wrong BTF version: %u, expected: %u!\n",
|
|
|
8def76 |
+ hdr->version, BTF_VERSION);
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (hdr->flags) {
|
|
|
8def76 |
+ fprintf(stderr, "Object has unsupported BTF flags %x!\n",
|
|
|
8def76 |
+ hdr->flags);
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ data_len = ctx->btf_data->d_size - sizeof(*hdr);
|
|
|
8def76 |
+ if (data_len < hdr->type_off ||
|
|
|
8def76 |
+ data_len < hdr->str_off ||
|
|
|
8def76 |
+ data_len < hdr->type_len + hdr->str_len ||
|
|
|
8def76 |
+ hdr->type_off >= hdr->str_off ||
|
|
|
8def76 |
+ hdr->type_off + hdr->type_len != hdr->str_off ||
|
|
|
8def76 |
+ hdr->str_off + hdr->str_len != data_len ||
|
|
|
8def76 |
+ (hdr->type_off & (sizeof(uint32_t) - 1))) {
|
|
|
8def76 |
+ fprintf(stderr, "Object has malformed BTF data!\n");
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ ctx->btf.hdr = hdr;
|
|
|
8def76 |
+ ctx->btf.raw = hdr + 1;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ str_start = ctx->btf.raw + hdr->str_off;
|
|
|
8def76 |
+ str_end = str_start + hdr->str_len;
|
|
|
8def76 |
+ if (!hdr->str_len ||
|
|
|
8def76 |
+ hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
|
|
|
8def76 |
+ str_start[0] || str_end[-1]) {
|
|
|
8def76 |
+ fprintf(stderr, "Object has malformed BTF string data!\n");
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ ctx->btf.strings = str_start;
|
|
|
8def76 |
+ return 0;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static int bpf_btf_register_type(struct bpf_elf_ctx *ctx,
|
|
|
8def76 |
+ const struct btf_type *type)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ int cur = ctx->btf.types_num, num = cur + 1;
|
|
|
8def76 |
+ const struct btf_type **types;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ types = realloc(ctx->btf.types, num * sizeof(type));
|
|
|
8def76 |
+ if (!types) {
|
|
|
8def76 |
+ free(ctx->btf.types);
|
|
|
8def76 |
+ ctx->btf.types = NULL;
|
|
|
8def76 |
+ ctx->btf.types_num = 0;
|
|
|
8def76 |
+ return -ENOMEM;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ ctx->btf.types = types;
|
|
|
8def76 |
+ ctx->btf.types[cur] = type;
|
|
|
8def76 |
+ ctx->btf.types_num = num;
|
|
|
8def76 |
+ return 0;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static struct btf_type btf_type_void;
|
|
|
8def76 |
+
|
|
|
8def76 |
+static int bpf_btf_prep_type_data(struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ const void *type_cur = ctx->btf.raw + ctx->btf.hdr->type_off;
|
|
|
8def76 |
+ const void *type_end = ctx->btf.raw + ctx->btf.hdr->str_off;
|
|
|
8def76 |
+ const struct btf_type *type;
|
|
|
8def76 |
+ uint16_t var_len;
|
|
|
8def76 |
+ int ret, kind;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ ret = bpf_btf_register_type(ctx, &btf_type_void);
|
|
|
8def76 |
+ if (ret < 0)
|
|
|
8def76 |
+ return ret;
|
|
|
8def76 |
+
|
|
|
8def76 |
+ while (type_cur < type_end) {
|
|
|
8def76 |
+ type = type_cur;
|
|
|
8def76 |
+ type_cur += sizeof(*type);
|
|
|
8def76 |
+
|
|
|
8def76 |
+ var_len = BTF_INFO_VLEN(type->info);
|
|
|
8def76 |
+ kind = BTF_INFO_KIND(type->info);
|
|
|
8def76 |
+
|
|
|
8def76 |
+ switch (kind) {
|
|
|
8def76 |
+ case BTF_KIND_INT:
|
|
|
8def76 |
+ type_cur += sizeof(int);
|
|
|
8def76 |
+ break;
|
|
|
8def76 |
+ case BTF_KIND_ARRAY:
|
|
|
8def76 |
+ type_cur += sizeof(struct btf_array);
|
|
|
8def76 |
+ break;
|
|
|
8def76 |
+ case BTF_KIND_STRUCT:
|
|
|
8def76 |
+ case BTF_KIND_UNION:
|
|
|
8def76 |
+ type_cur += var_len * sizeof(struct btf_member);
|
|
|
8def76 |
+ break;
|
|
|
8def76 |
+ case BTF_KIND_ENUM:
|
|
|
8def76 |
+ type_cur += var_len * sizeof(struct btf_enum);
|
|
|
8def76 |
+ break;
|
|
|
8def76 |
+ case BTF_KIND_TYPEDEF:
|
|
|
8def76 |
+ case BTF_KIND_PTR:
|
|
|
8def76 |
+ case BTF_KIND_FWD:
|
|
|
8def76 |
+ case BTF_KIND_VOLATILE:
|
|
|
8def76 |
+ case BTF_KIND_CONST:
|
|
|
8def76 |
+ case BTF_KIND_RESTRICT:
|
|
|
8def76 |
+ break;
|
|
|
8def76 |
+ default:
|
|
|
8def76 |
+ fprintf(stderr, "Object has unknown BTF type: %u!\n", kind);
|
|
|
8def76 |
+ return -EINVAL;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ ret = bpf_btf_register_type(ctx, type);
|
|
|
8def76 |
+ if (ret < 0)
|
|
|
8def76 |
+ return ret;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+
|
|
|
8def76 |
+ return 0;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static int bpf_btf_prep_data(struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ int ret = bpf_btf_check_header(ctx);
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (!ret)
|
|
|
8def76 |
+ return bpf_btf_prep_type_data(ctx);
|
|
|
8def76 |
+ return ret;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
+static void bpf_fetch_btf_end(struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ int fd = bpf_btf_attach(ctx);
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (fd < 0)
|
|
|
8def76 |
+ return;
|
|
|
8def76 |
+ ctx->btf_fd = fd;
|
|
|
8def76 |
+ if (bpf_btf_prep_data(ctx) < 0) {
|
|
|
8def76 |
+ close(ctx->btf_fd);
|
|
|
8def76 |
+ ctx->btf_fd = 0;
|
|
|
8def76 |
+ }
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
static bool bpf_has_map_data(const struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
{
|
|
|
8def76 |
return ctx->sym_tab && ctx->str_tab && ctx->sec_maps;
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
+static bool bpf_has_btf_data(const struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
+{
|
|
|
8def76 |
+ return ctx->sec_btf;
|
|
|
8def76 |
+}
|
|
|
8def76 |
+
|
|
|
8def76 |
static bool bpf_has_call_data(const struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
{
|
|
|
8def76 |
return ctx->sec_text;
|
|
|
8def76 |
@@ -1952,6 +2267,9 @@ static int bpf_fetch_ancillary(struct bpf_elf_ctx *ctx, bool check_text_sec)
|
|
|
8def76 |
else if (data.sec_hdr.sh_type == SHT_STRTAB &&
|
|
|
8def76 |
!strcmp(data.sec_name, ".strtab"))
|
|
|
8def76 |
ret = bpf_fetch_strtab(ctx, i, &data);
|
|
|
8def76 |
+ else if (data.sec_hdr.sh_type == SHT_PROGBITS &&
|
|
|
8def76 |
+ !strcmp(data.sec_name, ".BTF"))
|
|
|
8def76 |
+ ret = bpf_fetch_btf_begin(ctx, i, &data);
|
|
|
8def76 |
if (ret < 0) {
|
|
|
8def76 |
fprintf(stderr, "Error parsing section %d! Perhaps check with readelf -a?\n",
|
|
|
8def76 |
i);
|
|
|
8def76 |
@@ -1959,6 +2277,8 @@ static int bpf_fetch_ancillary(struct bpf_elf_ctx *ctx, bool check_text_sec)
|
|
|
8def76 |
}
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
+ if (bpf_has_btf_data(ctx))
|
|
|
8def76 |
+ bpf_fetch_btf_end(ctx);
|
|
|
8def76 |
if (bpf_has_map_data(ctx)) {
|
|
|
8def76 |
ret = bpf_fetch_maps_end(ctx);
|
|
|
8def76 |
if (ret < 0) {
|
|
|
8def76 |
@@ -2596,6 +2916,10 @@ static void bpf_maps_teardown(struct bpf_elf_ctx *ctx)
|
|
|
8def76 |
if (ctx->map_fds[i])
|
|
|
8def76 |
close(ctx->map_fds[i]);
|
|
|
8def76 |
}
|
|
|
8def76 |
+
|
|
|
8def76 |
+ if (ctx->btf_fd)
|
|
|
8def76 |
+ close(ctx->btf_fd);
|
|
|
8def76 |
+ free(ctx->btf.types);
|
|
|
8def76 |
}
|
|
|
8def76 |
|
|
|
8def76 |
static void bpf_elf_ctx_destroy(struct bpf_elf_ctx *ctx, bool failure)
|
|
|
8def76 |
--
|
|
|
8def76 |
2.20.1
|
|
|
8def76 |
|