2018-10-06 06:40:00 +07:00
|
|
|
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
2018-04-19 05:56:05 +07:00
|
|
|
/* Copyright (c) 2018 Facebook */
|
|
|
|
|
2018-10-04 05:26:42 +07:00
|
|
|
#ifndef __LIBBPF_BTF_H
|
|
|
|
#define __LIBBPF_BTF_H
|
2018-04-19 05:56:05 +07:00
|
|
|
|
2019-05-25 01:59:03 +07:00
|
|
|
#include <stdarg.h>
|
2019-08-08 04:39:48 +07:00
|
|
|
#include <linux/btf.h>
|
2018-07-24 22:40:21 +07:00
|
|
|
#include <linux/types.h>
|
2018-04-19 05:56:05 +07:00
|
|
|
|
2018-11-22 00:29:44 +07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-10-16 12:50:34 +07:00
|
|
|
#ifndef LIBBPF_API
|
|
|
|
#define LIBBPF_API __attribute__((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2018-04-19 05:56:05 +07:00
|
|
|
#define BTF_ELF_SEC ".BTF"
|
2018-11-20 06:29:16 +07:00
|
|
|
#define BTF_EXT_ELF_SEC ".BTF.ext"
|
libbpf: allow specifying map definitions using BTF
This patch adds support for a new way to define BPF maps. It relies on
BTF to describe mandatory and optional attributes of a map, as well as
captures type information of key and value naturally. This eliminates
the need for BPF_ANNOTATE_KV_PAIR hack and ensures key/value sizes are
always in sync with the key/value type.
Relying on BTF, this approach allows for both forward and backward
compatibility w.r.t. extending supported map definition features. By
default, any unrecognized attributes are treated as an error, but it's
possible relax this using MAPS_RELAX_COMPAT flag. New attributes, added
in the future will need to be optional.
The outline of the new map definition (short, BTF-defined maps) is as follows:
1. All the maps should be defined in .maps ELF section. It's possible to
have both "legacy" map definitions in `maps` sections and BTF-defined
maps in .maps sections. Everything will still work transparently.
2. The map declaration and initialization is done through
a global/static variable of a struct type with few mandatory and
extra optional fields:
- type field is mandatory and specified type of BPF map;
- key/value fields are mandatory and capture key/value type/size information;
- max_entries attribute is optional; if max_entries is not specified or
initialized, it has to be provided in runtime through libbpf API
before loading bpf_object;
- map_flags is optional and if not defined, will be assumed to be 0.
3. Key/value fields should be **a pointer** to a type describing
key/value. The pointee type is assumed (and will be recorded as such
and used for size determination) to be a type describing key/value of
the map. This is done to save excessive amounts of space allocated in
corresponding ELF sections for key/value of big size.
4. As some maps disallow having BTF type ID associated with key/value,
it's possible to specify key/value size explicitly without
associating BTF type ID with it. Use key_size and value_size fields
to do that (see example below).
Here's an example of simple ARRAY map defintion:
struct my_value { int x, y, z; };
struct {
int type;
int max_entries;
int *key;
struct my_value *value;
} btf_map SEC(".maps") = {
.type = BPF_MAP_TYPE_ARRAY,
.max_entries = 16,
};
This will define BPF ARRAY map 'btf_map' with 16 elements. The key will
be of type int and thus key size will be 4 bytes. The value is struct
my_value of size 12 bytes. This map can be used from C code exactly the
same as with existing maps defined through struct bpf_map_def.
Here's an example of STACKMAP definition (which currently disallows BTF type
IDs for key/value):
struct {
__u32 type;
__u32 max_entries;
__u32 map_flags;
__u32 key_size;
__u32 value_size;
} stackmap SEC(".maps") = {
.type = BPF_MAP_TYPE_STACK_TRACE,
.max_entries = 128,
.map_flags = BPF_F_STACK_BUILD_ID,
.key_size = sizeof(__u32),
.value_size = PERF_MAX_STACK_DEPTH * sizeof(struct bpf_stack_build_id),
};
This approach is naturally extended to support map-in-map, by making a value
field to be another struct that describes inner map. This feature is not
implemented yet. It's also possible to incrementally add features like pinning
with full backwards and forward compatibility. Support for static
initialization of BPF_MAP_TYPE_PROG_ARRAY using pointers to BPF programs
is also on the roadmap.
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-06-18 02:26:56 +07:00
|
|
|
#define MAPS_ELF_SEC ".maps"
|
2018-04-19 05:56:05 +07:00
|
|
|
|
|
|
|
struct btf;
|
2018-11-20 06:29:16 +07:00
|
|
|
struct btf_ext;
|
2018-07-24 22:40:22 +07:00
|
|
|
struct btf_type;
|
2018-04-19 05:56:05 +07:00
|
|
|
|
2019-04-10 04:20:14 +07:00
|
|
|
struct bpf_object;
|
|
|
|
|
2018-11-20 06:29:16 +07:00
|
|
|
/*
|
|
|
|
* The .BTF.ext ELF section layout defined as
|
|
|
|
* struct btf_ext_header
|
|
|
|
* func_info subsection
|
|
|
|
*
|
|
|
|
* The func_info subsection layout:
|
|
|
|
* record size for struct bpf_func_info in the func_info subsection
|
|
|
|
* struct btf_sec_func_info for section #1
|
|
|
|
* a list of bpf_func_info records for section #1
|
|
|
|
* where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
|
|
|
|
* but may not be identical
|
|
|
|
* struct btf_sec_func_info for section #2
|
|
|
|
* a list of bpf_func_info records for section #2
|
|
|
|
* ......
|
|
|
|
*
|
|
|
|
* Note that the bpf_func_info record size in .BTF.ext may not
|
|
|
|
* be the same as the one defined in include/uapi/linux/bpf.h.
|
|
|
|
* The loader should ensure that record_size meets minimum
|
|
|
|
* requirement and pass the record as is to the kernel. The
|
|
|
|
* kernel will handle the func_info properly based on its contents.
|
|
|
|
*/
|
|
|
|
struct btf_ext_header {
|
|
|
|
__u16 magic;
|
|
|
|
__u8 version;
|
|
|
|
__u8 flags;
|
|
|
|
__u32 hdr_len;
|
|
|
|
|
|
|
|
/* All offsets are in bytes relative to the end of this header */
|
|
|
|
__u32 func_info_off;
|
|
|
|
__u32 func_info_len;
|
2018-12-08 07:42:31 +07:00
|
|
|
__u32 line_info_off;
|
|
|
|
__u32 line_info_len;
|
2019-08-08 04:39:50 +07:00
|
|
|
|
|
|
|
/* optional part of .BTF.ext header */
|
|
|
|
__u32 offset_reloc_off;
|
|
|
|
__u32 offset_reloc_len;
|
2018-11-20 06:29:16 +07:00
|
|
|
};
|
|
|
|
|
2018-10-16 12:50:34 +07:00
|
|
|
LIBBPF_API void btf__free(struct btf *btf);
|
2019-02-02 07:14:14 +07:00
|
|
|
LIBBPF_API struct btf *btf__new(__u8 *data, __u32 size);
|
2019-05-25 01:58:57 +07:00
|
|
|
LIBBPF_API struct btf *btf__parse_elf(const char *path,
|
|
|
|
struct btf_ext **btf_ext);
|
2019-04-10 04:20:14 +07:00
|
|
|
LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
|
2019-02-09 02:19:36 +07:00
|
|
|
LIBBPF_API int btf__load(struct btf *btf);
|
2018-10-16 12:50:34 +07:00
|
|
|
LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
|
|
|
|
const char *type_name);
|
2019-02-05 08:29:46 +07:00
|
|
|
LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
|
2018-10-16 12:50:34 +07:00
|
|
|
LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
|
|
|
|
__u32 id);
|
|
|
|
LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
|
|
|
|
LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
|
|
|
|
LIBBPF_API int btf__fd(const struct btf *btf);
|
2019-02-09 02:19:37 +07:00
|
|
|
LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
|
2018-10-16 12:50:34 +07:00
|
|
|
LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
|
2018-11-24 07:44:32 +07:00
|
|
|
LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
|
2019-02-06 02:48:22 +07:00
|
|
|
LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
|
2019-02-05 02:00:58 +07:00
|
|
|
__u32 expected_key_size,
|
|
|
|
__u32 expected_value_size,
|
|
|
|
__u32 *key_type_id, __u32 *value_type_id);
|
2018-04-19 05:56:05 +07:00
|
|
|
|
2019-02-05 02:00:57 +07:00
|
|
|
LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size);
|
|
|
|
LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
|
2019-03-01 06:31:22 +07:00
|
|
|
LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext,
|
2019-02-09 02:19:38 +07:00
|
|
|
__u32 *size);
|
2019-02-05 02:00:57 +07:00
|
|
|
LIBBPF_API int btf_ext__reloc_func_info(const struct btf *btf,
|
|
|
|
const struct btf_ext *btf_ext,
|
|
|
|
const char *sec_name, __u32 insns_cnt,
|
|
|
|
void **func_info, __u32 *cnt);
|
|
|
|
LIBBPF_API int btf_ext__reloc_line_info(const struct btf *btf,
|
|
|
|
const struct btf_ext *btf_ext,
|
|
|
|
const char *sec_name, __u32 insns_cnt,
|
|
|
|
void **line_info, __u32 *cnt);
|
|
|
|
LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
|
|
|
|
LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
|
2018-11-20 06:29:16 +07:00
|
|
|
|
2019-02-05 08:29:45 +07:00
|
|
|
struct btf_dedup_opts {
|
2019-03-01 06:31:23 +07:00
|
|
|
unsigned int dedup_table_size;
|
2019-02-05 08:29:45 +07:00
|
|
|
bool dont_resolve_fwds;
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
|
|
|
|
const struct btf_dedup_opts *opts);
|
|
|
|
|
2019-05-25 01:59:03 +07:00
|
|
|
struct btf_dump;
|
|
|
|
|
|
|
|
struct btf_dump_opts {
|
|
|
|
void *ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
|
|
|
|
|
|
|
|
LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
|
|
|
|
const struct btf_ext *btf_ext,
|
|
|
|
const struct btf_dump_opts *opts,
|
|
|
|
btf_dump_printf_fn_t printf_fn);
|
|
|
|
LIBBPF_API void btf_dump__free(struct btf_dump *d);
|
|
|
|
|
|
|
|
LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
|
|
|
|
|
2019-08-08 04:39:48 +07:00
|
|
|
/*
|
|
|
|
* A set of helpers for easier BTF types handling
|
|
|
|
*/
|
|
|
|
static inline __u16 btf_kind(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KIND(t->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline __u16 btf_vlen(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_VLEN(t->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_kflag(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INFO_KFLAG(t->info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_int(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_INT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_ptr(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_PTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_array(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_ARRAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_struct(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_STRUCT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_union(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_UNION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_composite(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
__u16 kind = btf_kind(t);
|
|
|
|
|
|
|
|
return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_enum(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_ENUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_fwd(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_FWD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_typedef(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_TYPEDEF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_volatile(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_VOLATILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_const(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_CONST;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_restrict(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_RESTRICT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_mod(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
__u16 kind = btf_kind(t);
|
|
|
|
|
|
|
|
return kind == BTF_KIND_VOLATILE ||
|
|
|
|
kind == BTF_KIND_CONST ||
|
|
|
|
kind == BTF_KIND_RESTRICT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_func(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_FUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_func_proto(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_FUNC_PROTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_var(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_VAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool btf_is_datasec(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return btf_kind(t) == BTF_KIND_DATASEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline __u8 btf_int_encoding(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INT_ENCODING(*(__u32 *)(t + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline __u8 btf_int_offset(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INT_OFFSET(*(__u32 *)(t + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline __u8 btf_int_bits(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return BTF_INT_BITS(*(__u32 *)(t + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btf_array *btf_array(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return (struct btf_array *)(t + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btf_enum *btf_enum(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return (struct btf_enum *)(t + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btf_member *btf_members(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return (struct btf_member *)(t + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get bit offset of a member with specified index. */
|
|
|
|
static inline __u32 btf_member_bit_offset(const struct btf_type *t,
|
|
|
|
__u32 member_idx)
|
|
|
|
{
|
|
|
|
const struct btf_member *m = btf_members(t) + member_idx;
|
|
|
|
bool kflag = btf_kflag(t);
|
|
|
|
|
|
|
|
return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or
|
|
|
|
* BTF_KIND_UNION. If member is not a bitfield, zero is returned.
|
|
|
|
*/
|
|
|
|
static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
|
|
|
|
__u32 member_idx)
|
|
|
|
{
|
|
|
|
const struct btf_member *m = btf_members(t) + member_idx;
|
|
|
|
bool kflag = btf_kflag(t);
|
|
|
|
|
|
|
|
return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btf_param *btf_params(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return (struct btf_param *)(t + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btf_var *btf_var(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return (struct btf_var *)(t + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btf_var_secinfo *
|
|
|
|
btf_var_secinfos(const struct btf_type *t)
|
|
|
|
{
|
|
|
|
return (struct btf_var_secinfo *)(t + 1);
|
|
|
|
}
|
|
|
|
|
2018-11-22 00:29:44 +07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
|
|
|
|
2018-10-04 05:26:42 +07:00
|
|
|
#endif /* __LIBBPF_BTF_H */
|