Various BPF improvements
Subsystems (4)
Daniel Borkmann
maintainercover Various BPF improvements
Main part of this set is to: i) avoid strict af_alg kernel dependency, ii) add loader support for bpf to bpf calls and iii) add btf loader support with an option to annotate maps. For details please see the individual patches. Thanks!
Daniel Borkmann (5):
bpf: import btf uapi kernel header
bpf: move bpf_elf_map fixup notification under verbose
bpf: remove strict dependency on af_alg
bpf: implement bpf to bpf calls support
bpf: implement btf handling and map annotation
include/bpf_elf.h | 9 + include/bpf_util.h | 1 + include/uapi/linux/btf.h | 113 +++++++++ lib/bpf.c | 645 +++++++++++++++++++++++++++++++++++++---------- 4 files changed, 639 insertions(+), 129 deletions(-) create mode 100644 include/uapi/linux/btf.h
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 2/5 bpf: move bpf_elf_map fixup notification under verbose
No need to spam the user with this if it can be fixed gracefully anyway. Therefore, move it under verbose option.
--- lib/bpf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
lib/bpf.c +3 -3
@@ -1893,9 +1893,9 @@ static int bpf_fetch_maps_end(struct bpf_elf_ctx *ctx) }memcpy (ctx->maps, fixup,sizeof (fixup)); - -printf ("Note: %zu bytes struct bpf_elf_map fixup performed due to size mismatch!\n" , -sizeof (struct bpf_elf_map ) - ctx->map_len); +if (ctx->verbose) +printf ("%zu bytes struct bpf_elf_map fixup performed due to size mismatch!\n" , +sizeof (struct bpf_elf_map ) - ctx->map_len);return 0 ; }
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 3/5 bpf: remove strict dependency on af_alg
In reply to Daniel BorkmannDo not bail out when AF_ALG is not supported by the kernel and only do so when a map is requested in object ns where we're calculating the hash. Otherwise, the loader can operate just fine, therefore lets not fail early when it's not needed.
--- lib/bpf.c | 74 ++++++++++++++++++++++++--------------------------------------- 1 file changed, 28 insertions(+), 46 deletions(-)
lib/bpf.c +28 -46
@@ -1125,6 +1125,7 @@ struct bpf_elf_ctx {GElf_Ehdr elf_hdr;Elf_Data *sym_tab;Elf_Data *str_tab; +char obj_uid[64 ];int obj_fd;int map_fds[ELF_MAX_MAPS ];struct bpf_elf_map maps[ELF_MAX_MAPS ];
@@ -1138,6 +1139,7 @@ struct bpf_elf_ctx {enum bpf_prog_type type;__u32 ifindex;bool verbose; +bool noafalg;struct bpf_elf_st stat;struct bpf_hash_entry *ht[256 ];char *log;
@@ -1253,22 +1255,15 @@ static int bpf_obj_hash(const char *object, uint8_t *out, size_t len)return -EINVAL ; cfd =socket (AF_ALG ,SOCK_SEQPACKET ,0 ); -if (cfd <0 ) { -fprintf (stderr,"Cannot get AF_ALG socket: %s\n" , -strerror (errno)); +if (cfd <0 )return cfd; - } ret =bind (cfd, (struct sockaddr *)&alg,sizeof (alg)); -if (ret <0 ) { -fprintf (stderr,"Error binding socket: %s\n" ,strerror (errno)); +if (ret <0 ) goto out_cfd; - } ofd =accept (cfd,NULL ,0 );if (ofd <0 ) { -fprintf (stderr,"Error accepting socket: %s\n" , -strerror (errno)); ret = ofd; goto out_cfd; }
@@ -1313,29 +1308,7 @@ out_cfd:return ret; } -static const char *bpf_get_obj_uid (const char *pathname) -{ -static bool bpf_uid_cached; -static char bpf_uid[64 ]; -uint8_t tmp[20 ]; -int ret; - -if (bpf_uid_cached) - goto done; - - ret =bpf_obj_hash (pathname, tmp,sizeof (tmp)); -if (ret) { -fprintf (stderr,"Object hashing failed!\n" ); -return NULL ; - } - -hexstring_n2a (tmp,sizeof (tmp), bpf_uid,sizeof (bpf_uid)); - bpf_uid_cached = true; -done: -return bpf_uid; -} - -static int bpf_init_env (const char *pathname) +static void bpf_init_env (void ) {struct rlimit limit = { .rlim_cur =RLIM_INFINITY ,
@@ -1345,15 +1318,8 @@ static int bpf_init_env(const char *pathname)/* Don't bother in case we fail! */ setrlimit (RLIMIT_MEMLOCK , &limit); -if (!bpf_get_work_dir (BPF_PROG_TYPE_UNSPEC )) { +if (!bpf_get_work_dir (BPF_PROG_TYPE_UNSPEC ))fprintf (stderr,"Continuing without mounted eBPF fs. Too old kernel?\n" ); -return 0 ; - } - -if (!bpf_get_obj_uid (pathname)) -return -1 ; - -return 0 ; } static const char *bpf_custom_pinning(const struct bpf_elf_ctx *ctx,
@@ -1389,7 +1355,7 @@ static void bpf_make_pathname(char *pathname, size_t len, const char *name,case PIN_OBJECT_NS :snprintf (pathname, len,"%s/%s/%s" ,bpf_get_work_dir (ctx->type), -bpf_get_obj_uid (NULL ), name); + ctx->obj_uid, name);break ;case PIN_GLOBAL_NS : snprintf(pathname, len, "%s/%s/%s",
@@ -1422,7 +1388,7 @@ static int bpf_make_obj_path(const struct bpf_elf_ctx *ctx)int ret;snprintf (tmp,sizeof (tmp),"%s/%s" ,bpf_get_work_dir (ctx->type), -bpf_get_obj_uid (NULL )); + ctx->obj_uid); ret =mkdir (tmp,S_IRWXU );if (ret && errno !=EEXIST ) {
@@ -1691,6 +1657,12 @@ static int bpf_maps_attach_all(struct bpf_elf_ctx *ctx)const char *map_name;for (i =0 ; i < ctx->map_num; i++) { +if (ctx->maps[i].pinning ==PIN_OBJECT_NS && + ctx->noafalg) { +fprintf (stderr,"Missing kernel AF_ALG support for PIN_OBJECT_NS!\n" ); +return -ENOTSUP ; + } + map_name =bpf_map_fetch_name (ctx, i);if (!map_name)return -EIO ;
@@ -2446,14 +2418,24 @@ static int bpf_elf_ctx_init(struct bpf_elf_ctx *ctx, const char *pathname, enum bpf_prog_type type, __u32 ifindex, bool verbose) { - int ret = -EINVAL; + uint8_t tmp[20]; +int ret; -if (elf_version (EV_CURRENT ) ==EV_NONE || -bpf_init_env (pathname)) -return ret; +if (elf_version (EV_CURRENT ) ==EV_NONE ) +return -EINVAL ; + +bpf_init_env ();memset (ctx,0 ,sizeof (*ctx));bpf_get_cfg (ctx); + + ret =bpf_obj_hash (pathname, tmp,sizeof (tmp)); +if (ret) + ctx->noafalg = true; +else +hexstring_n2a (tmp,sizeof (tmp), ctx->obj_uid, +sizeof (ctx->obj_uid)); + ctx->verbose = verbose; ctx->type = type; ctx->ifindex = ifindex;
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 1/5 bpf: import btf uapi kernel header
In reply to Daniel BorkmannNeeded later on for BTF loader.
--- include/uapi/linux/btf.h | 113 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 include/uapi/linux/btf.h
include/uapi/linux/btf.h new file +113 -0
@@ -0,0 +1,113 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* Copyright (c) 2018 Facebook */ +#ifndef _UAPI__LINUX_BTF_H__ +#define _UAPI__LINUX_BTF_H__ + +#include <linux/types.h> + +#define BTF_MAGIC 0xeB9F +#define BTF_VERSION 1 + +struct btf_header { +__u16 magic; +__u8 version; +__u8 flags; +__u32 hdr_len; + +/* All offsets are in bytes relative to the end of this header */ +__u32 type_off;/* offset of type section */ +__u32 type_len;/* length of type section */ +__u32 str_off;/* offset of string section */ +__u32 str_len;/* length of string section */ +}; + +/* Max # of type identifier */ +#define BTF_MAX_TYPE 0x0000ffff +/* Max offset into the string section */ +#define BTF_MAX_NAME_OFFSET 0x0000ffff +/* Max # of struct/union/enum members or func args */ +#define BTF_MAX_VLEN 0xffff + +struct btf_type { +__u32 name_off; +/* "info" bits arrangement +* bits 0-15: vlen (e.g. # of struct's members) +* bits 16-23: unused +* bits 24-27: kind (e.g. int, ptr, array...etc) +* bits 28-31: unused +*/ +__u32 info; +/* "size" is used by INT, ENUM, STRUCT and UNION. +* "size" tells the size of the type it is describing. +* +* "type" is used by PTR, TYPEDEF, VOLATILE, CONST and RESTRICT. +* "type" is a type_id referring to another type. +*/ +union { +__u32 size; +__u32 type; + }; +}; + +#define BTF_INFO_KIND (info) (((info) >> 24) & 0x0f) +#define BTF_INFO_VLEN (info) ((info) & 0xffff) + +#define BTF_KIND_UNKN 0/* Unknown */ +#define BTF_KIND_INT 1/* Integer */ +#define BTF_KIND_PTR 2/* Pointer */ +#define BTF_KIND_ARRAY 3/* Array */ +#define BTF_KIND_STRUCT 4/* Struct */ +#define BTF_KIND_UNION 5/* Union */ +#define BTF_KIND_ENUM 6/* Enumeration */ +#define BTF_KIND_FWD 7/* Forward */ +#define BTF_KIND_TYPEDEF 8/* Typedef */ +#define BTF_KIND_VOLATILE 9/* Volatile */ +#define BTF_KIND_CONST 10/* Const */ +#define BTF_KIND_RESTRICT 11/* Restrict */ +#define BTF_KIND_MAX 11 +#define NR_BTF_KINDS 12 + +/* For some specific BTF_KIND, "struct btf_type" is immediately +* followed by extra data. +*/ + +/* BTF_KIND_INT is followed by a u32 and the following +* is the 32 bits arrangement: +*/ +#define BTF_INT_ENCODING (VAL ) (((VAL) & 0x0f000000) >> 24) +#define BTF_INT_OFFSET (VAL ) (((VAL & 0x00ff0000)) >> 16) +#define BTF_INT_BITS (VAL ) ((VAL) & 0x0000ffff) + +/* Attributes stored in the BTF_INT_ENCODING */ +#define BTF_INT_SIGNED (1 << 0) +#define BTF_INT_CHAR (1 << 1) +#define BTF_INT_BOOL (1 << 2) + +/* BTF_KIND_ENUM is followed by multiple "struct btf_enum". +* The exact number of btf_enum is stored in the vlen (of the +* info in "struct btf_type"). +*/ +struct btf_enum { +__u32 name_off; +__s32 val; +}; + +/* BTF_KIND_ARRAY is followed by one "struct btf_array" */ +struct btf_array { +__u32 type; +__u32 index_type; +__u32 nelems; +}; + +/* BTF_KIND_STRUCT and BTF_KIND_UNION are followed +* by multiple "struct btf_member". The exact number +* of btf_member is stored in the vlen (of the info in +* "struct btf_type"). +*/ +struct btf_member { +__u32 name_off; +__u32 type; +__u32 offset;/* offset in bits */ +}; + +#endif /* _UAPI__LINUX_BTF_H__ */
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 5/5 bpf: implement btf handling and map annotation
In reply to Daniel BorkmannImplement loading of .BTF section from object file and build up internal table for retrieving key/value id related to maps in the BPF program. Latter is done by setting up struct btf_type table.
One of the issues is that there's a disconnect between the data types used in the map and struct bpf_elf_map, meaning the underlying types are unknown from the map description. One way to overcome this is to add a annotation such that the loader will recognize the relation to both. BPF_ANNOTATE_KV_PAIR(map_foo, struct key, struct val); has been added to the API that programs can use.
The loader will then pick the corresponding key/value type ids and attach it to the maps for creation. This can later on be dumped via bpftool for introspection.
Example with test_xdp_noinline.o from kernel selftests:
[...]
struct ctl_value {
union {
__u64 value;
__u32 ifindex;
__u8 mac[6];
};
};
struct bpf_map_def __attribute__ ((section("maps"), used)) ctl_array = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(struct ctl_value),
.max_entries = 16,
.map_flags = 0,
};
BPF_ANNOTATE_KV_PAIR(ctl_array, __u32, struct ctl_value);[...]
Above could also further be wrapped in a macro. Compiling through LLVM and converting to BTF:
# llc --version LLVM (http://llvm.org/): LLVM version 7.0.0svn Optimized build. Default target: x86_64-unknown-linux-gnu Host CPU: skylake
Registered Targets:
bpf - BPF (host endian)
bpfeb - BPF (big endian)
bpfel - BPF (little endian)
[...]
# clang [...] -O2 -target bpf -g -emit-llvm -c test_xdp_noinline.c -o - |
llc -march=bpf -mcpu=probe -mattr=dwarfris -filetype=obj -o test_xdp_noinline.o
# pahole -J test_xdp_noinline.oChecking pahole dump of BPF object file:
# file test_xdp_noinline.o
test_xdp_noinline.o: ELF 64-bit LSB relocatable, *unknown arch 0xf7* version 1 (SYSV), with debug_info, not stripped
# pahole test_xdp_noinline.o
[...]
struct ctl_value {
union {
__u64 value; /* 0 8 */
__u32 ifindex; /* 0 4 */
__u8 mac[0]; /* 0 0 */
}; /* 0 8 *//* size: 8, cachelines: 1, members: 1 */ /* last cacheline: 8 bytes */
};
Now loading into kernel and dumping the map via bpftool:
# ip -force link set dev lo xdp obj test_xdp_noinline.o sec xdp-test
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 xdpgeneric/id:227 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
[...]
# bpftool prog show id 227
227: xdp tag a85e060c275c5616 gpl
loaded_at 2018-07-17T14:41:29+0000 uid 0
xlated 8152B not jited memlock 12288B map_ids 381,385,386,382,384,383
# bpftool map dump id 386
[{
"key": 0,
"value": {
"": {
"value": 0,
"ifindex": 0,
"mac": []
}
}
},{
"key": 1,
"value": {
"": {
"value": 0,
"ifindex": 0,
"mac": []
}
}
},{
[...]--- include/bpf_elf.h | 9 ++ include/bpf_util.h | 1 + lib/bpf.c | 332 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 338 insertions(+), 4 deletions(-)
include/bpf_elf.h +9 -0
@@ -41,4 +41,13 @@ struct bpf_elf_map { __u32 inner_idx; }; +#define BPF_ANNOTATE_KV_PAIR (name, type_key, type_val) \ + struct ____btf_map_##name { \ + type_key key; \ + type_val value; \ + }; \ + struct ____btf_map_##name \ + __attribute__ ((section(".maps." #name), used)) \ + ____btf_map_##name = { } +#endif /* __BPF_ELF__ */
include/bpf_util.h +1 -0
@@ -14,6 +14,7 @@#define __BPF_UTIL__#include <linux/bpf.h> +#include <linux/btf.h> #include <linux/filter.h> #include <linux/magic.h> #include <linux/elf-em.h>
lib/bpf.c +328 -4
439 lines not on this page · raw message
Signature · 1 line
2.9.5
Daniel Borkmann
maintainerPATCH 4/5 bpf: implement bpf to bpf calls support
In reply to Daniel BorkmannImplement missing bpf to bpf calls support. The loader will recognize .text section and handle relocation entries that are emitted by LLVM.
First step is processing of map related relocation entries for .text section, and in a second step loader will copy .text section into program section and adjust call instruction offset accordingly.
Example with test_xdp_noinline.o from kernel selftests:
1) Every function as __attribute__ ((always_inline)), rest left unchanged:
# ip -force link set dev lo xdp obj test_xdp_noinline.o sec xdp-test
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 xdpgeneric/id:233 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
[...]
# bpftool prog dump xlated id 233
[...]
1669: (2d) if r3 > r2 goto pc+4
1670: (79) r2 = *(u64 *)(r10 -136)
1671: (61) r2 = *(u32 *)(r2 +0)
1672: (63) *(u32 *)(r1 +0) = r2
1673: (b7) r0 = 1
1674: (95) exit <-- 1674 insns total 2) Every function as __attribute__ ((noinline)), rest left unchanged:
# ip -force link set dev lo xdp obj test_xdp_noinline.o sec xdp-test
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 xdpgeneric/id:236 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
[...]
# bpftool prog dump xlated id 236
[...]
1000: (bf) r1 = r6
1001: (b7) r2 = 24
1002: (85) call pc+3 <-- pc-relative call insns
1003: (1f) r7 -= r0
1004: (bf) r0 = r7
1005: (95) exit
1006: (bf) r0 = r1
1007: (bf) r1 = r2
1008: (67) r1 <<= 32
1009: (77) r1 >>= 32
1010: (bf) r3 = r0
1011: (6f) r3 <<= r1
1012: (87) r2 = -r2
1013: (57) r2 &= 31
1014: (67) r0 <<= 32
1015: (77) r0 >>= 32
1016: (7f) r0 >>= r2
1017: (4f) r0 |= r3
1018: (95) exit <-- 1018 insns total--- lib/bpf.c | 233 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 157 insertions(+), 76 deletions(-)
lib/bpf.c +157 -76
@@ -1104,7 +1104,8 @@ int bpf_prog_load(enum bpf_prog_type type, const struct bpf_insn *insns,#ifdef HAVE_ELF struct bpf_elf_prog {enum bpf_prog_type type; -const struct bpf_insn *insns; +struct bpf_insn *insns; +unsigned int insns_num;size_t size;const char *license; };
@@ -1130,11 +1131,13 @@ struct bpf_elf_ctx {int map_fds[ELF_MAX_MAPS ];struct bpf_elf_map maps[ELF_MAX_MAPS ];struct bpf_map_ext maps_ext[ELF_MAX_MAPS ]; +struct bpf_elf_prog prog_text;int sym_num;int map_num;int map_len;bool *sec_done;int sec_maps; +int sec_text;char license[ELF_MAX_LICENSE_LEN ];enum bpf_prog_type type;__u32 ifindex;
@@ -1899,12 +1902,25 @@ static int bpf_fetch_strtab(struct bpf_elf_ctx *ctx, int section,return 0 ; } +static int bpf_fetch_text (struct bpf_elf_ctx *ctx,int section, +struct bpf_elf_sec_data *data) +{ + ctx->sec_text = section; + ctx->sec_done[section] = true; +return 0 ; +} +static bool bpf_has_map_data (const struct bpf_elf_ctx *ctx) {return ctx->sym_tab && ctx->str_tab && ctx->sec_maps; } -static int bpf_fetch_ancillary (struct bpf_elf_ctx *ctx) +static bool bpf_has_call_data (const struct bpf_elf_ctx *ctx) +{ +return ctx->sec_text; +} + +static int bpf_fetch_ancillary (struct bpf_elf_ctx *ctx,bool check_text_sec) {struct bpf_elf_sec_data data;int i, ret =-1 ;
@@ -1920,6 +1936,11 @@ static int bpf_fetch_ancillary(struct bpf_elf_ctx *ctx)else if (data.sec_hdr.sh_type ==SHT_PROGBITS && !strcmp (data.sec_name,ELF_SECTION_LICENSE )) ret =bpf_fetch_license (ctx, i, &data); +else if (data.sec_hdr.sh_type ==SHT_PROGBITS && + (data.sec_hdr.sh_flags &SHF_EXECINSTR ) && + !strcmp (data.sec_name,".text" ) && + check_text_sec) + ret =bpf_fetch_text (ctx, i, &data);else if (data.sec_hdr.sh_type ==SHT_SYMTAB && !strcmp (data.sec_name,".symtab" )) ret =bpf_fetch_symtab (ctx, i, &data);
@@ -1964,17 +1985,18 @@ static int bpf_fetch_prog(struct bpf_elf_ctx *ctx, const char *section, ret =bpf_fill_section_data (ctx, i, &data);if (ret <0 || !(data.sec_hdr.sh_type ==SHT_PROGBITS && - data.sec_hdr.sh_flags &SHF_EXECINSTR && + (data.sec_hdr.sh_flags &SHF_EXECINSTR ) && !strcmp (data.sec_name, section)))continue ; *sseen = true;memset (&prog,0 ,sizeof (prog)); - prog.type = ctx->type; - prog.insns = data.sec_data->d_buf; - prog.size = data.sec_data->d_size; - prog.license = ctx->license; + prog.type = ctx->type; + prog.license = ctx->license; + prog.size = data.sec_data->d_size; + prog.insns_num = prog.size /sizeof (struct bpf_insn ); + prog.insns = data.sec_data->d_buf; fd =bpf_prog_attach (section, &prog, ctx);if (fd <0 )
@@ -1987,84 +2009,120 @@ static int bpf_fetch_prog(struct bpf_elf_ctx *ctx, const char *section,return fd; } -struct bpf_tail_call_props { -unsigned int total; -unsigned int jited; +struct bpf_relo_props { +struct bpf_tail_call { +unsigned int total; +unsigned int jited; + } tc; +int main_num; }; +static int bpf_apply_relo_map (struct bpf_elf_ctx *ctx,struct bpf_elf_prog *prog, +GElf_Rel *relo,GElf_Sym *sym, +struct bpf_relo_props *props) +{ +unsigned int insn_off = relo->r_offset /sizeof (struct bpf_insn ); +unsigned int map_idx = sym->st_value / ctx->map_len; + +if (insn_off >= prog->insns_num) +return -EINVAL ; +if (prog->insns[insn_off].code != (BPF_LD |BPF_IMM |BPF_DW )) { +fprintf (stderr,"ELF contains relo data for non ld64 instruction at offset %u! Compiler bug?!\n" , + insn_off); +return -EINVAL ; + } + +if (map_idx >=ARRAY_SIZE (ctx->map_fds)) +return -EINVAL ; +if (!ctx->map_fds[map_idx]) +return -EINVAL ; +if (ctx->maps[map_idx].type ==BPF_MAP_TYPE_PROG_ARRAY ) { + props->tc.total++; +if (ctx->maps_ext[map_idx].owner.jited || + (ctx->maps_ext[map_idx].owner.type ==0 && + ctx->cfg.jit_enabled)) + props->tc.jited++; + } + + prog->insns[insn_off].src_reg =BPF_PSEUDO_MAP_FD ; + prog->insns[insn_off].imm = ctx->map_fds[map_idx]; +return 0 ; +} + +static int bpf_apply_relo_call (struct bpf_elf_ctx *ctx,struct bpf_elf_prog *prog, +GElf_Rel *relo,GElf_Sym *sym, +struct bpf_relo_props *props) +{ +unsigned int insn_off = relo->r_offset /sizeof (struct bpf_insn ); +struct bpf_elf_prog *prog_text = &ctx->prog_text; + +if (insn_off >= prog->insns_num) +return -EINVAL ; +if (prog->insns[insn_off].code != (BPF_JMP |BPF_CALL ) && + prog->insns[insn_off].src_reg !=BPF_PSEUDO_CALL ) { +fprintf (stderr,"ELF contains relo data for non call instruction at offset %u! Compiler bug?!\n" , + insn_off); +return -EINVAL ; + } + +if (!props->main_num) { +struct bpf_insn *insns =realloc (prog->insns, + prog->size + prog_text->size); +if (!insns) +return -ENOMEM ; + +memcpy (insns + prog->insns_num, prog_text->insns, + prog_text->size); + props->main_num = prog->insns_num; + prog->insns = insns; + prog->insns_num += prog_text->insns_num; + prog->size += prog_text->size; + } + + prog->insns[insn_off].imm += props->main_num - insn_off; +return 0 ; +} +static int bpf_apply_relo_data (struct bpf_elf_ctx *ctx,struct bpf_elf_sec_data *data_relo, -struct bpf_elf_sec_data *data_insn, -struct bpf_tail_call_props *props) +struct bpf_elf_prog *prog, +struct bpf_relo_props *props) { -Elf_Data *idata = data_insn->sec_data;GElf_Shdr *rhdr = &data_relo->sec_hdr;int relo_ent, relo_num = rhdr->sh_size / rhdr->sh_entsize; -struct bpf_insn *insns = idata->d_buf; -unsigned int num_insns = idata->d_size /sizeof (*insns);for (relo_ent =0 ; relo_ent < relo_num; relo_ent++) { -unsigned int ioff, rmap;GElf_Rel relo;GElf_Sym sym; +int ret = -EIO ;if (gelf_getrel (data_relo->sec_data, relo_ent, &relo) != &relo)return -EIO ; - - ioff = relo.r_offset /sizeof (struct bpf_insn ); -if (ioff >= num_insns || - insns[ioff].code != (BPF_LD |BPF_IMM |BPF_DW )) { -fprintf (stderr,"ELF contains relo data for non ld64 instruction at offset %u! Compiler bug?!\n" , - ioff); -fprintf (stderr," - Current section: %s\n" , data_relo->sec_name); -if (ioff < num_insns && - insns[ioff].code == (BPF_JMP |BPF_CALL )) -fprintf (stderr," - Try to annotate functions with always_inline attribute!\n" ); -return -EINVAL ; - } -if (gelf_getsym (ctx->sym_tab,GELF_R_SYM (relo.r_info), &sym) != &sym)return -EIO ; -if (sym.st_shndx != ctx->sec_maps) { -fprintf (stderr,"ELF contains non-map related relo data in entry %u pointing to section %u! Compiler bug?!\n" , - relo_ent, sym.st_shndx); -return -EIO ; - } - rmap = sym.st_value / ctx->map_len; -if (rmap >=ARRAY_SIZE (ctx->map_fds)) -return -EINVAL ; -if (!ctx->map_fds[rmap]) -return -EINVAL ; -if (ctx->maps[rmap].type ==BPF_MAP_TYPE_PROG_ARRAY ) { - props->total++; -if (ctx->maps_ext[rmap].owner.jited || - (ctx->maps_ext[rmap].owner.type ==0 && - ctx->cfg.jit_enabled)) - props->jited++; - } - -if (ctx->verbose) -fprintf (stderr,"Map \'%s\' (%d) injected into prog section \'%s\' at offset %u!\n" , -bpf_str_tab_name (ctx, &sym), ctx->map_fds[rmap], - data_insn->sec_name, ioff); - - insns[ioff].src_reg =BPF_PSEUDO_MAP_FD ; - insns[ioff].imm = ctx->map_fds[rmap]; +if (sym.st_shndx == ctx->sec_maps) + ret =bpf_apply_relo_map (ctx, prog, &relo, &sym, props); +else if (sym.st_shndx == ctx->sec_text) + ret =bpf_apply_relo_call (ctx, prog, &relo, &sym, props); +else +fprintf (stderr,"ELF contains non-{map,call} related relo data in entry %u pointing to section %u! Compiler bug?!\n" , + relo_ent, sym.st_shndx); +if (ret <0 ) +return ret; }return 0 ; }static int bpf_fetch_prog_relo (struct bpf_elf_ctx *ctx,const char *section, -bool *lderr,bool *sseen) +bool *lderr,bool *sseen,struct bpf_elf_prog *prog) {struct bpf_elf_sec_data data_relo, data_insn; -struct bpf_elf_prog prog;int ret, idx, i, fd =-1 ;for (i =1 ; i < ctx->elf_hdr.e_shnum; i++) { -struct bpf_tail_call_props props = {}; +struct bpf_relo_props props = {}; ret =bpf_fill_section_data (ctx, i, &data_relo);if (ret <0 || data_relo.sec_hdr.sh_type !=SHT_REL )
@@ -2075,40 +2133,54 @@ static int bpf_fetch_prog_relo(struct bpf_elf_ctx *ctx, const char *section, ret =bpf_fill_section_data (ctx, idx, &data_insn);if (ret <0 || !(data_insn.sec_hdr.sh_type ==SHT_PROGBITS && - data_insn.sec_hdr.sh_flags &SHF_EXECINSTR && + (data_insn.sec_hdr.sh_flags &SHF_EXECINSTR ) && !strcmp (data_insn.sec_name, section)))continue ; +if (sseen) + *sseen = true; + +memset (prog,0 ,sizeof (*prog)); + prog->type = ctx->type; + prog->license = ctx->license; + prog->size = data_insn.sec_data->d_size; + prog->insns_num = prog->size /sizeof (struct bpf_insn ); + prog->insns =malloc (prog->size); +if (!prog->insns) { + *lderr = true; +return -ENOMEM ; + } - *sseen = true; +memcpy (prog->insns, data_insn.sec_data->d_buf, prog->size); - ret =bpf_apply_relo_data (ctx, &data_relo, &data_insn, &props); + ret =bpf_apply_relo_data (ctx, &data_relo, prog, &props);if (ret <0 ) { *lderr = true; +if (ctx->sec_text != idx) +free (prog->insns);return ret; } +if (ctx->sec_text == idx) { + fd =0 ; + goto out; + } -memset (&prog,0 ,sizeof (prog)); - prog.type = ctx->type; - prog.insns = data_insn.sec_data->d_buf; - prog.size = data_insn.sec_data->d_size; - prog.license = ctx->license; - - fd =bpf_prog_attach (section, &prog, ctx); + fd =bpf_prog_attach (section, prog, ctx); +free (prog->insns);if (fd <0 ) { *lderr = true; -if (props.total) { +if (props.tc.total) {if (ctx->cfg.jit_enabled && - props.total != props.jited) + props.tc.total != props.tc.jited)fprintf (stderr,"JIT enabled, but only %u/%u tail call maps in the program have JITed owner!\n" , - props.jited, props.total); + props.tc.jited, props.tc.total);if (!ctx->cfg.jit_enabled && - props.jited) + props.tc.jited)fprintf (stderr,"JIT disabled, but %u/%u tail call maps in the program have JITed owner!\n" , - props.jited, props.total); + props.tc.jited, props.tc.total); }return fd; } - +out: ctx->sec_done[i] = true; ctx->sec_done[idx] = true;break ;
@@ -2120,10 +2192,18 @@ static int bpf_fetch_prog_relo(struct bpf_elf_ctx *ctx, const char *section,static int bpf_fetch_prog_sec (struct bpf_elf_ctx *ctx,const char *section) {bool lderr = false, sseen = false; +struct bpf_elf_prog prog;int ret =-1 ; -if (bpf_has_map_data (ctx)) - ret =bpf_fetch_prog_relo (ctx, section, &lderr, &sseen); +if (bpf_has_call_data (ctx)) { + ret =bpf_fetch_prog_relo (ctx,".text" , &lderr,NULL , + &ctx->prog_text); +if (ret <0 ) +return ret; + } + +if (bpf_has_map_data (ctx) ||bpf_has_call_data (ctx)) + ret =bpf_fetch_prog_relo (ctx, section, &lderr, &sseen, &prog);if (ret <0 && !lderr) ret =bpf_fetch_prog (ctx, section, &sseen);if (ret <0 && !sseen)
@@ -2520,6 +2600,7 @@ static void bpf_elf_ctx_destroy(struct bpf_elf_ctx *ctx, bool failure)bpf_hash_destroy (ctx); +free (ctx->prog_text.insns);free (ctx->sec_done);free (ctx->log);
@@ -2541,7 +2622,7 @@ static int bpf_obj_open(const char *pathname, enum bpf_prog_type type,return ret; } - ret =bpf_fetch_ancillary (ctx); + ret =bpf_fetch_ancillary (ctx,strcmp (section,".text" ));if (ret <0 ) {fprintf (stderr,"Error fetching ELF ancillary data!\n" ); goto out;
Signature · 1 line
2.9.5
Re: [PATCH iproute2 2/5] bpf: move bpf_elf_map fixup notification under verbose
In reply to Daniel BorkmannQuoted 13 lines · Daniel Borkmann
No need to spam the user with this if it can be fixed gracefully anyway. Therefore, move it under verbose option.
Signed-off-by Daniel Borkmann <[email protected]>--- lib/bpf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)lib/bpf.c +0
-0
lib/bpf.c static int bpf_fetch_maps_end(struct bpf_elf_ctx *ctx) · line 1898
Quoted 9 lines
lib/bpf.c +3
-3@@ -1893,9 +1893,9 @@ static int bpf_fetch_maps_end(struct bpf_elf_ctx *ctx) }memcpy (ctx->maps, fixup,sizeof (fixup)); - -printf ("Note: %zu bytes struct bpf_elf_map fixup performed due to size mismatch!\n" , -sizeof (struct bpf_elf_map ) - ctx->map_len); +if (ctx->verbose) +printf ("%zu bytes struct bpf_elf_map fixup performed due to size mismatch!\n" , +sizeof (struct bpf_elf_map ) - ctx->map_len);
Glad to see that :) FWIW you seem to use fprintf(stderr, ) in the btf patch for information messages, I think that's a good approach. Separating "results" from "info/error" messages. Helps JSON too. Probably doesn't matter much for this message.
Re: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
In reply to Daniel BorkmannQuoted 13 lines · Daniel Borkmann
# bpftool map dump id 386
[{
"key": 0,
"value": {
"": {
"value": 0,
"ifindex": 0,
"mac": []
}
}
},{
"key": 1,
"value": {Quoted 8 lines
"": {
"value": 0,
"ifindex": 0,
"mac": []
}
}
},{
[...]Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
FWIW all the patches look nice to me! Thanks for keeping the support for loading programs from the ".text" section :)
Quoted 12 lines · Daniel Borkmann
Main part of this set is to: i) avoid strict
af_algkernel dependency, ii) add loader support for bpf to bpf calls and iii) add btf loader support with an option to annotate maps. For details please see the individual patches. Thanks!Daniel Borkmann (5):
bpf: import btf uapi kernel header
bpf: movebpf_elf_mapfixup notification under verbose
bpf: remove strict dependency onaf_alg
bpf: implement bpf to bpf calls support
bpf: implement btf handling and map annotation
Quoted 7 lines
include/bpf_elf.h | 9 + include/bpf_util.h | 1 + include/uapi/linux/btf.h | 113 +++++++++ lib/bpf.c | 645 +++++++++++++++++++++++++++++++++++++---------- 4 files changed, 639 insertions(+), 129 deletions(-) create mode 100644 include/uapi/linux/btf.h
Applied 2-5 to iproute2-next. Pulled btf.h from the last header sync point for consistency.
Daniel Borkmann
maintainerRe: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
In reply to Jakub KicinskiQuoted 23 lines · Jakub Kicinski
Quoted 13 lines · Daniel Borkmann
# bpftool map dump id 386 [{ "key": 0, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ "key": 1, "value": {Quoted 8 lines
"": { "value": 0, "ifindex": 0, "mac": [] } } },{ [...]
Quoted 2 lines
Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
Yeah agree, I think it would be nice to see a more pahole style dump where we have types and member names along with the value as otherwise it might be a bit confusing.
Quoted 2 lines
FWIW all the patches look nice to me! Thanks for keeping the support for loading programs from the ".text" section :)
The test_offload.py from BPF kselftests helped a lot in that! :)
Thanks,
Daniel
Daniel Borkmann
maintainerRe: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
Quoted 24 lines · Daniel Borkmann
Quoted 23 lines · Jakub Kicinski
Quoted 21 lines · Daniel Borkmann
# bpftool map dump id 386 [{ "key": 0, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ "key": 1, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ [...]
Quoted 6 lines
Quoted 2 lines
Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
Yeah agree, I think it would be nice to see a more pahole style dump where we have types and member names along with the value as otherwise it might be a bit confusing.
Another feature that would be super useful imho would be in the /single/ map view e.g. 'bpftool map show id 123' to have a detailed BTF key+value type dump, so in addition to the basic map info we show pahole like info of the structs with length/offsets.
Quoted 8 lines
Quoted 2 lines
FWIW all the patches look nice to me! Thanks for keeping the support for loading programs from the ".text" section :)
The
test_offload.pyfrom BPF kselftests helped a lot in that! :)Thanks,
Daniel
Re: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
Quoted 28 lines · Daniel Borkmann
Quoted 27 lines · Daniel Borkmann
Quoted 25 lines · Jakub Kicinski
Quoted 13 lines · Daniel Borkmann
# bpftool map dump id 386 [{ "key": 0, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ "key": 1, "value": {Quoted 8 lines
"": { "value": 0, "ifindex": 0, "mac": [] } } },{ [...]Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
Quoted 8 lines
Quoted 3 lines
Yeah agree, I think it would be nice to see a more pahole style dump where we have types and member names along with the value as otherwise it might be a bit confusing.
Another feature that would be super useful imho would be in the /single/ map view e.g. 'bpftool map show id 123' to have a detailed BTF key+value type dump, so in addition to the basic map info we show pahole like info of the structs with length/offsets.
That sounds good! We could also consider adding a btf object and commands to interrogate BTF types in the kernel in general.. Perhaps then we could add something like bpftool btf describe map id 123.
Having the single map view show more information seems interesting, but I wonder if it could be surprising. Is there precedent for such behaviour?
Re: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
In reply to Jakub KicinskiQuoted 23 lines · Jakub Kicinski
Quoted 13 lines · Daniel Borkmann
# bpftool map dump id 386 [{ "key": 0, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ "key": 1, "value": {Quoted 8 lines
"": { "value": 0, "ifindex": 0, "mac": [] } } },{ [...]
Quoted 2 lines
Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
Note that the kernel's btf_verifier_log is using "(anon)" in this case. Not sure if it is a good idea for json.
Re: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
In reply to Jakub KicinskiQuoted 33 lines · Jakub Kicinski
Quoted 32 lines · Daniel Borkmann
Quoted 30 lines · Daniel Borkmann
Quoted 23 lines · Jakub Kicinski
Quoted 13 lines · Daniel Borkmann
# bpftool map dump id 386 [{ "key": 0, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ "key": 1, "value": {Quoted 8 lines
"": { "value": 0, "ifindex": 0, "mac": [] } } },{ [...]Quoted 2 lines
Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
Yeah agree, I think it would be nice to see a more pahole style dump where we have types and member names along with the value as otherwise it might be a bit confusing.
Quoted 8 lines
Quoted 4 lines
Another feature that would be super useful imho would be in the /single/ map view e.g. 'bpftool map show id 123' to have a detailed BTF key+value type dump, so in addition to the basic map info we show pahole like info of the structs with length/offsets.
That sounds good! We could also consider adding a btf object and commands to interrogate BTF types in the kernel in general.. Perhaps then we could add something like bpftool btf describe map id 123.
+1 on the btf subcommand.
Quoted 4 lines
Having the single map view show more information seems interesting, but I wonder if it could be surprising. Is there precedent for such behaviour?
Having everything in one page (map show id 123) could be interesting. One thing is the pahole-like output may be quite long? e.g. the member of a struct could itself be another struct.
Not sure how the pahole-like output may look like in json though.
Re: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
In reply to Martin KaFai LauQuoted 24 lines · Martin KaFai Lau
Quoted 23 lines · Jakub Kicinski
Quoted 21 lines · Daniel Borkmann
# bpftool map dump id 386 [{ "key": 0, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ "key": 1, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ [...]
Quoted 4 lines
Quoted 2 lines
Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
Note that the kernel's
btf_verifier_logis using "(anon)" in this case. Not sure if it is a good idea for json.
"(anon)" sounds good, we should probably add some id to it in JSON in case there are multiple.
Daniel Borkmann
maintainerRe: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
In reply to Martin KaFai LauQuoted 39 lines · Martin KaFai Lau
Quoted 38 lines · Jakub Kicinski
Quoted 36 lines · Daniel Borkmann
Quoted 24 lines · Daniel Borkmann
Quoted 23 lines · Jakub Kicinski
Quoted 21 lines · Daniel Borkmann
# bpftool map dump id 386 [{ "key": 0, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ "key": 1, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ [...]Quoted 6 lines
Quoted 2 lines
Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
Yeah agree, I think it would be nice to see a more pahole style dump where we have types and member names along with the value as otherwise it might be a bit confusing.
Another feature that would be super useful imho would be in the /single/ map view e.g. 'bpftool map show id 123' to have a detailed BTF key+value type dump, so in addition to the basic map info we show pahole like info of the structs with length/offsets.
Quoted 4 lines
Quoted 3 lines
That sounds good! We could also consider adding a btf object and commands to interrogate BTF types in the kernel in general.. Perhaps then we could add something like bpftool btf describe map id 123.
+1 on the btf subcommand.
That would also work, I think both might be useful to have. Former would all sit under a single command to show map details. With 'bpftool btf' you would also allow for a full BTF dump when a specific BTF obj id is provided?
Quoted 6 lines
Quoted 3 lines
Having the single map view show more information seems interesting, but I wonder if it could be surprising. Is there precedent for such behaviour?
Having everything in one page (map show id 123) could be interesting. One thing is the pahole-like output may be quite long? e.g. the member of a struct could itself be another struct.
Right, though probably fine when you want to see all information specific to one map. Of course the 'bpftool map' list view would need to hide this information.
Quoted 1 line
Not sure how the pahole-like output may look like in json though.
Would the existing map k/v dump have more or less the same 'issue'?
Re: [PATCH iproute2 5/5] bpf: implement btf handling and map annotation
Quoted 40 lines · Daniel Borkmann
Quoted 39 lines · Martin KaFai Lau
Quoted 38 lines · Jakub Kicinski
Quoted 36 lines · Daniel Borkmann
Quoted 24 lines · Daniel Borkmann
Quoted 23 lines · Jakub Kicinski
Quoted 21 lines · Daniel Borkmann
# bpftool map dump id 386 [{ "key": 0, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ "key": 1, "value": { "": { "value": 0, "ifindex": 0, "mac": [] } } },{ [...]Quoted 6 lines
Quoted 2 lines
Ugh, the empty keys ("") look worrying, we should probably improve handling of anonymous structs in bpftool :S
Yeah agree, I think it would be nice to see a more pahole style dump where we have types and member names along with the value as otherwise it might be a bit confusing.
Another feature that would be super useful imho would be in the /single/ map view e.g. 'bpftool map show id 123' to have a detailed BTF key+value type dump, so in addition to the basic map info we show pahole like info of the structs with length/offsets.
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
That sounds good! We could also consider adding a btf object and commands to interrogate BTF types in the kernel in general.. Perhaps then we could add something like bpftool btf describe map id 123.
+1 on the btf subcommand.
That would also work, I think both might be useful to have. Former would all sit under a single command to show map details.
Agree that both would be useful. btf command could address the whole BTF object which could include many maps/types while the map command is focusing on its own map info.
Quoted 2 lines
With 'bpftool btf' you
would also allow for a full BTF dump when a specific BTF obj id is provided?
Right, I think the BTF obj id (or file) is needed for the btf command. and then it should allow to do full dump or only show a particular map/type id.
A little forward thinking, map here is a C type. Hence, I think using the name "type" like "btftool btf id 1 show _type_ id 123" may be better when we later expand BTF usage beyond BPF program.
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Having the single map view show more information seems interesting, but I wonder if it could be surprising. Is there precedent for such behaviour?
Having everything in one page (map show id 123) could be interesting. One thing is the pahole-like output may be quite long? e.g. the member of a struct could itself be another struct.
Quoted 7 lines
Right, though probably fine when you want to see all information specific to one map. Of course the 'bpftool map' list view would need to hide this information.
Quoted 1 line
Not sure how the pahole-like output may look like in json though.
Would the existing map k/v dump have more or less the same 'issue'?
True, the existing map k/v dump of the map data is reusing the json {}/[]/""/number convention. I think that is ok and actually a pretty condensed way since people are used to this convention when reading "data" output.
For printing out C type, I think it is more natural to have it as close to C syntax as possible in order to have it parsable by human eyes. However, yes, we could reuse a similar fashion to print type in json as we do in printing data. Just curious, the json type output is more for script or mostly for people that can read everything from one json output.
For plaintext, we can just print like pahole.