-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Martin KaFai Lau says: ==================== This patch introduces BPF Type Format (BTF). BTF (BPF Type Format) is the meta data format which describes the data types of BPF program/map. Hence, it basically focus on the C programming language which the modern BPF is primary using. The first use case is to provide a generic pretty print capability for a BPF map. A modified pahole that can convert dwarf to BTF is here: https://github.com/iamkafai/pahole/tree/btf Please see individual patch for details. v5: - Remove BTF_KIND_FLOAT and BTF_KIND_FUNC which are not currently used. They can be added in the future. Some bpf_df_xxx() are removed together. - Add comment in patch 7 to clarify that the new bpffs_map_fops should not be extended further. v4: - Fix warning (remove unneeded semicolon) - Remove a redundant variable (nr_bytes) from btf_int_check_meta() in patch 1. Caught by W=1. v3: - Rebase to bpf-next - Fix sparse warning (by adding static) - Add BTF header logging: btf_verifier_log_hdr() - Fix the alignment test on btf->type_off - Add tests for the BTF header - Lower the max BTF size to 16MB. It should be enough for some time. We could raise it later if it would be needed. v2: - Use kvfree where needed in patch 1 and 2 - Also consider BTF_INT_OFFSET() in the btf_int_check_meta() in patch 1 - Fix an incorrect goto target in map_create() during the btf-error-path in patch 7 - re-org some local vars to keep the rev xmas tree in btf.c ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
- Loading branch information
Showing
22 changed files
with
5,076 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
/* Copyright (c) 2018 Facebook */ | ||
|
||
#ifndef _LINUX_BTF_H | ||
#define _LINUX_BTF_H 1 | ||
|
||
#include <linux/types.h> | ||
|
||
struct btf; | ||
struct btf_type; | ||
union bpf_attr; | ||
|
||
extern const struct file_operations btf_fops; | ||
|
||
void btf_put(struct btf *btf); | ||
int btf_new_fd(const union bpf_attr *attr); | ||
struct btf *btf_get_by_fd(int fd); | ||
int btf_get_info_by_fd(const struct btf *btf, | ||
const union bpf_attr *attr, | ||
union bpf_attr __user *uattr); | ||
/* Figure out the size of a type_id. If type_id is a modifier | ||
* (e.g. const), it will be resolved to find out the type with size. | ||
* | ||
* For example: | ||
* In describing "const void *", type_id is "const" and "const" | ||
* refers to "void *". The return type will be "void *". | ||
* | ||
* If type_id is a simple "int", then return type will be "int". | ||
* | ||
* @btf: struct btf object | ||
* @type_id: Find out the size of type_id. The type_id of the return | ||
* type is set to *type_id. | ||
* @ret_size: It can be NULL. If not NULL, the size of the return | ||
* type is set to *ret_size. | ||
* Return: The btf_type (resolved to another type with size info if needed). | ||
* NULL is returned if type_id itself does not have size info | ||
* (e.g. void) or it cannot be resolved to another type that | ||
* has size info. | ||
* *type_id and *ret_size will not be changed in the | ||
* NULL return case. | ||
*/ | ||
const struct btf_type *btf_type_id_size(const struct btf *btf, | ||
u32 *type_id, | ||
u32 *ret_size); | ||
void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, | ||
struct seq_file *m); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* 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_MAGIC_SWAP 0x9FeB | ||
#define BTF_VERSION 1 | ||
#define BTF_FLAGS_COMPR 0x01 | ||
|
||
struct btf_header { | ||
__u16 magic; | ||
__u8 version; | ||
__u8 flags; | ||
|
||
__u32 parent_label; | ||
__u32 parent_name; | ||
|
||
/* All offsets are in bytes relative to the end of this header */ | ||
__u32 label_off; /* offset of label section */ | ||
__u32 object_off; /* offset of data object section*/ | ||
__u32 func_off; /* offset of function section */ | ||
__u32 type_off; /* offset 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 0x7fffffff | ||
/* Max offset into the string section */ | ||
#define BTF_MAX_NAME_OFFSET 0x7fffffff | ||
/* Max # of struct/union/enum members or func args */ | ||
#define BTF_MAX_VLEN 0xffff | ||
|
||
/* The type id is referring to a parent BTF */ | ||
#define BTF_TYPE_PARENT(id) (((id) >> 31) & 0x1) | ||
#define BTF_TYPE_ID(id) ((id) & BTF_MAX_TYPE) | ||
|
||
/* String is in the ELF string section */ | ||
#define BTF_STR_TBL_ELF_ID(ref) (((ref) >> 31) & 0x1) | ||
#define BTF_STR_OFFSET(ref) ((ref) & BTF_MAX_NAME_OFFSET) | ||
|
||
struct btf_type { | ||
__u32 name; | ||
/* "info" bits arrangement | ||
* bits 0-15: vlen (e.g. # of struct's members) | ||
* bits 16-23: unused | ||
* bits 24-28: kind (e.g. int, ptr, array...etc) | ||
* bits 29-30: unused | ||
* bits 31: root | ||
*/ | ||
__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) & 0x1f) | ||
#define BTF_INFO_ISROOT(info) (!!(((info) >> 24) & 0x80)) | ||
#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) & 0xff000000) >> 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 0x1 | ||
#define BTF_INT_CHAR 0x2 | ||
#define BTF_INT_BOOL 0x4 | ||
#define BTF_INT_VARARGS 0x8 | ||
|
||
/* 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; | ||
__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; | ||
__u32 type; | ||
__u32 offset; /* offset in bits */ | ||
}; | ||
|
||
#endif /* _UAPI__LINUX_BTF_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.