diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/linux/bpf.h | 20 | ||||
-rw-r--r-- | include/linux/btf.h | 48 | ||||
-rw-r--r-- | include/uapi/linux/bpf.h | 12 | ||||
-rw-r--r-- | include/uapi/linux/btf.h | 130 |
4 files changed, 207 insertions, 3 deletions
diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 95a7abd0ee92..ee5275e7d4df 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h | |||
@@ -22,6 +22,8 @@ struct perf_event; | |||
22 | struct bpf_prog; | 22 | struct bpf_prog; |
23 | struct bpf_map; | 23 | struct bpf_map; |
24 | struct sock; | 24 | struct sock; |
25 | struct seq_file; | ||
26 | struct btf; | ||
25 | 27 | ||
26 | /* map is generic key/value storage optionally accesible by eBPF programs */ | 28 | /* map is generic key/value storage optionally accesible by eBPF programs */ |
27 | struct bpf_map_ops { | 29 | struct bpf_map_ops { |
@@ -43,10 +45,14 @@ struct bpf_map_ops { | |||
43 | void (*map_fd_put_ptr)(void *ptr); | 45 | void (*map_fd_put_ptr)(void *ptr); |
44 | u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf); | 46 | u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf); |
45 | u32 (*map_fd_sys_lookup_elem)(void *ptr); | 47 | u32 (*map_fd_sys_lookup_elem)(void *ptr); |
48 | void (*map_seq_show_elem)(struct bpf_map *map, void *key, | ||
49 | struct seq_file *m); | ||
50 | int (*map_check_btf)(const struct bpf_map *map, const struct btf *btf, | ||
51 | u32 key_type_id, u32 value_type_id); | ||
46 | }; | 52 | }; |
47 | 53 | ||
48 | struct bpf_map { | 54 | struct bpf_map { |
49 | /* 1st cacheline with read-mostly members of which some | 55 | /* The first two cachelines with read-mostly members of which some |
50 | * are also accessed in fast-path (e.g. ops, max_entries). | 56 | * are also accessed in fast-path (e.g. ops, max_entries). |
51 | */ | 57 | */ |
52 | const struct bpf_map_ops *ops ____cacheline_aligned; | 58 | const struct bpf_map_ops *ops ____cacheline_aligned; |
@@ -62,10 +68,13 @@ struct bpf_map { | |||
62 | u32 pages; | 68 | u32 pages; |
63 | u32 id; | 69 | u32 id; |
64 | int numa_node; | 70 | int numa_node; |
71 | u32 btf_key_id; | ||
72 | u32 btf_value_id; | ||
73 | struct btf *btf; | ||
65 | bool unpriv_array; | 74 | bool unpriv_array; |
66 | /* 7 bytes hole */ | 75 | /* 55 bytes hole */ |
67 | 76 | ||
68 | /* 2nd cacheline with misc members to avoid false sharing | 77 | /* The 3rd and 4th cacheline with misc members to avoid false sharing |
69 | * particularly with refcounting. | 78 | * particularly with refcounting. |
70 | */ | 79 | */ |
71 | struct user_struct *user ____cacheline_aligned; | 80 | struct user_struct *user ____cacheline_aligned; |
@@ -100,6 +109,11 @@ static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map) | |||
100 | return container_of(map, struct bpf_offloaded_map, map); | 109 | return container_of(map, struct bpf_offloaded_map, map); |
101 | } | 110 | } |
102 | 111 | ||
112 | static inline bool bpf_map_support_seq_show(const struct bpf_map *map) | ||
113 | { | ||
114 | return map->ops->map_seq_show_elem && map->ops->map_check_btf; | ||
115 | } | ||
116 | |||
103 | extern const struct bpf_map_ops bpf_map_offload_ops; | 117 | extern const struct bpf_map_ops bpf_map_offload_ops; |
104 | 118 | ||
105 | /* function argument constraints */ | 119 | /* function argument constraints */ |
diff --git a/include/linux/btf.h b/include/linux/btf.h new file mode 100644 index 000000000000..a966dc6d61ee --- /dev/null +++ b/include/linux/btf.h | |||
@@ -0,0 +1,48 @@ | |||
1 | /* SPDX-License-Identifier: GPL-2.0 */ | ||
2 | /* Copyright (c) 2018 Facebook */ | ||
3 | |||
4 | #ifndef _LINUX_BTF_H | ||
5 | #define _LINUX_BTF_H 1 | ||
6 | |||
7 | #include <linux/types.h> | ||
8 | |||
9 | struct btf; | ||
10 | struct btf_type; | ||
11 | union bpf_attr; | ||
12 | |||
13 | extern const struct file_operations btf_fops; | ||
14 | |||
15 | void btf_put(struct btf *btf); | ||
16 | int btf_new_fd(const union bpf_attr *attr); | ||
17 | struct btf *btf_get_by_fd(int fd); | ||
18 | int btf_get_info_by_fd(const struct btf *btf, | ||
19 | const union bpf_attr *attr, | ||
20 | union bpf_attr __user *uattr); | ||
21 | /* Figure out the size of a type_id. If type_id is a modifier | ||
22 | * (e.g. const), it will be resolved to find out the type with size. | ||
23 | * | ||
24 | * For example: | ||
25 | * In describing "const void *", type_id is "const" and "const" | ||
26 | * refers to "void *". The return type will be "void *". | ||
27 | * | ||
28 | * If type_id is a simple "int", then return type will be "int". | ||
29 | * | ||
30 | * @btf: struct btf object | ||
31 | * @type_id: Find out the size of type_id. The type_id of the return | ||
32 | * type is set to *type_id. | ||
33 | * @ret_size: It can be NULL. If not NULL, the size of the return | ||
34 | * type is set to *ret_size. | ||
35 | * Return: The btf_type (resolved to another type with size info if needed). | ||
36 | * NULL is returned if type_id itself does not have size info | ||
37 | * (e.g. void) or it cannot be resolved to another type that | ||
38 | * has size info. | ||
39 | * *type_id and *ret_size will not be changed in the | ||
40 | * NULL return case. | ||
41 | */ | ||
42 | const struct btf_type *btf_type_id_size(const struct btf *btf, | ||
43 | u32 *type_id, | ||
44 | u32 *ret_size); | ||
45 | void btf_type_seq_show(const struct btf *btf, u32 type_id, void *obj, | ||
46 | struct seq_file *m); | ||
47 | |||
48 | #endif | ||
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 9a2d1a04eb24..c8383a289f7b 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h | |||
@@ -95,6 +95,7 @@ enum bpf_cmd { | |||
95 | BPF_OBJ_GET_INFO_BY_FD, | 95 | BPF_OBJ_GET_INFO_BY_FD, |
96 | BPF_PROG_QUERY, | 96 | BPF_PROG_QUERY, |
97 | BPF_RAW_TRACEPOINT_OPEN, | 97 | BPF_RAW_TRACEPOINT_OPEN, |
98 | BPF_BTF_LOAD, | ||
98 | }; | 99 | }; |
99 | 100 | ||
100 | enum bpf_map_type { | 101 | enum bpf_map_type { |
@@ -279,6 +280,9 @@ union bpf_attr { | |||
279 | */ | 280 | */ |
280 | char map_name[BPF_OBJ_NAME_LEN]; | 281 | char map_name[BPF_OBJ_NAME_LEN]; |
281 | __u32 map_ifindex; /* ifindex of netdev to create on */ | 282 | __u32 map_ifindex; /* ifindex of netdev to create on */ |
283 | __u32 btf_fd; /* fd pointing to a BTF type data */ | ||
284 | __u32 btf_key_id; /* BTF type_id of the key */ | ||
285 | __u32 btf_value_id; /* BTF type_id of the value */ | ||
282 | }; | 286 | }; |
283 | 287 | ||
284 | struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ | 288 | struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ |
@@ -363,6 +367,14 @@ union bpf_attr { | |||
363 | __u64 name; | 367 | __u64 name; |
364 | __u32 prog_fd; | 368 | __u32 prog_fd; |
365 | } raw_tracepoint; | 369 | } raw_tracepoint; |
370 | |||
371 | struct { /* anonymous struct for BPF_BTF_LOAD */ | ||
372 | __aligned_u64 btf; | ||
373 | __aligned_u64 btf_log_buf; | ||
374 | __u32 btf_size; | ||
375 | __u32 btf_log_size; | ||
376 | __u32 btf_log_level; | ||
377 | }; | ||
366 | } __attribute__((aligned(8))); | 378 | } __attribute__((aligned(8))); |
367 | 379 | ||
368 | /* BPF helper function descriptions: | 380 | /* BPF helper function descriptions: |
diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h new file mode 100644 index 000000000000..74a30b1090df --- /dev/null +++ b/include/uapi/linux/btf.h | |||
@@ -0,0 +1,130 @@ | |||
1 | /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ | ||
2 | /* Copyright (c) 2018 Facebook */ | ||
3 | #ifndef _UAPI__LINUX_BTF_H__ | ||
4 | #define _UAPI__LINUX_BTF_H__ | ||
5 | |||
6 | #include <linux/types.h> | ||
7 | |||
8 | #define BTF_MAGIC 0xeB9F | ||
9 | #define BTF_MAGIC_SWAP 0x9FeB | ||
10 | #define BTF_VERSION 1 | ||
11 | #define BTF_FLAGS_COMPR 0x01 | ||
12 | |||
13 | struct btf_header { | ||
14 | __u16 magic; | ||
15 | __u8 version; | ||
16 | __u8 flags; | ||
17 | |||
18 | __u32 parent_label; | ||
19 | __u32 parent_name; | ||
20 | |||
21 | /* All offsets are in bytes relative to the end of this header */ | ||
22 | __u32 label_off; /* offset of label section */ | ||
23 | __u32 object_off; /* offset of data object section*/ | ||
24 | __u32 func_off; /* offset of function section */ | ||
25 | __u32 type_off; /* offset of type section */ | ||
26 | __u32 str_off; /* offset of string section */ | ||
27 | __u32 str_len; /* length of string section */ | ||
28 | }; | ||
29 | |||
30 | /* Max # of type identifier */ | ||
31 | #define BTF_MAX_TYPE 0x7fffffff | ||
32 | /* Max offset into the string section */ | ||
33 | #define BTF_MAX_NAME_OFFSET 0x7fffffff | ||
34 | /* Max # of struct/union/enum members or func args */ | ||
35 | #define BTF_MAX_VLEN 0xffff | ||
36 | |||
37 | /* The type id is referring to a parent BTF */ | ||
38 | #define BTF_TYPE_PARENT(id) (((id) >> 31) & 0x1) | ||
39 | #define BTF_TYPE_ID(id) ((id) & BTF_MAX_TYPE) | ||
40 | |||
41 | /* String is in the ELF string section */ | ||
42 | #define BTF_STR_TBL_ELF_ID(ref) (((ref) >> 31) & 0x1) | ||
43 | #define BTF_STR_OFFSET(ref) ((ref) & BTF_MAX_NAME_OFFSET) | ||
44 | |||
45 | struct btf_type { | ||
46 | __u32 name; | ||
47 | /* "info" bits arrangement | ||
48 | * bits 0-15: vlen (e.g. # of struct's members) | ||
49 | * bits 16-23: unused | ||
50 | * bits 24-28: kind (e.g. int, ptr, array...etc) | ||
51 | * bits 29-30: unused | ||
52 | * bits 31: root | ||
53 | */ | ||
54 | __u32 info; | ||
55 | /* "size" is used by INT, ENUM, STRUCT and UNION. | ||
56 | * "size" tells the size of the type it is describing. | ||
57 | * | ||
58 | * "type" is used by PTR, TYPEDEF, VOLATILE, CONST and RESTRICT. | ||
59 | * "type" is a type_id referring to another type. | ||
60 | */ | ||
61 | union { | ||
62 | __u32 size; | ||
63 | __u32 type; | ||
64 | }; | ||
65 | }; | ||
66 | |||
67 | #define BTF_INFO_KIND(info) (((info) >> 24) & 0x1f) | ||
68 | #define BTF_INFO_ISROOT(info) (!!(((info) >> 24) & 0x80)) | ||
69 | #define BTF_INFO_VLEN(info) ((info) & 0xffff) | ||
70 | |||
71 | #define BTF_KIND_UNKN 0 /* Unknown */ | ||
72 | #define BTF_KIND_INT 1 /* Integer */ | ||
73 | #define BTF_KIND_PTR 2 /* Pointer */ | ||
74 | #define BTF_KIND_ARRAY 3 /* Array */ | ||
75 | #define BTF_KIND_STRUCT 4 /* Struct */ | ||
76 | #define BTF_KIND_UNION 5 /* Union */ | ||
77 | #define BTF_KIND_ENUM 6 /* Enumeration */ | ||
78 | #define BTF_KIND_FWD 7 /* Forward */ | ||
79 | #define BTF_KIND_TYPEDEF 8 /* Typedef */ | ||
80 | #define BTF_KIND_VOLATILE 9 /* Volatile */ | ||
81 | #define BTF_KIND_CONST 10 /* Const */ | ||
82 | #define BTF_KIND_RESTRICT 11 /* Restrict */ | ||
83 | #define BTF_KIND_MAX 11 | ||
84 | #define NR_BTF_KINDS 12 | ||
85 | |||
86 | /* For some specific BTF_KIND, "struct btf_type" is immediately | ||
87 | * followed by extra data. | ||
88 | */ | ||
89 | |||
90 | /* BTF_KIND_INT is followed by a u32 and the following | ||
91 | * is the 32 bits arrangement: | ||
92 | */ | ||
93 | #define BTF_INT_ENCODING(VAL) (((VAL) & 0xff000000) >> 24) | ||
94 | #define BTF_INT_OFFSET(VAL) (((VAL & 0x00ff0000)) >> 16) | ||
95 | #define BTF_INT_BITS(VAL) ((VAL) & 0x0000ffff) | ||
96 | |||
97 | /* Attributes stored in the BTF_INT_ENCODING */ | ||
98 | #define BTF_INT_SIGNED 0x1 | ||
99 | #define BTF_INT_CHAR 0x2 | ||
100 | #define BTF_INT_BOOL 0x4 | ||
101 | #define BTF_INT_VARARGS 0x8 | ||
102 | |||
103 | /* BTF_KIND_ENUM is followed by multiple "struct btf_enum". | ||
104 | * The exact number of btf_enum is stored in the vlen (of the | ||
105 | * info in "struct btf_type"). | ||
106 | */ | ||
107 | struct btf_enum { | ||
108 | __u32 name; | ||
109 | __s32 val; | ||
110 | }; | ||
111 | |||
112 | /* BTF_KIND_ARRAY is followed by one "struct btf_array" */ | ||
113 | struct btf_array { | ||
114 | __u32 type; | ||
115 | __u32 index_type; | ||
116 | __u32 nelems; | ||
117 | }; | ||
118 | |||
119 | /* BTF_KIND_STRUCT and BTF_KIND_UNION are followed | ||
120 | * by multiple "struct btf_member". The exact number | ||
121 | * of btf_member is stored in the vlen (of the info in | ||
122 | * "struct btf_type"). | ||
123 | */ | ||
124 | struct btf_member { | ||
125 | __u32 name; | ||
126 | __u32 type; | ||
127 | __u32 offset; /* offset in bits */ | ||
128 | }; | ||
129 | |||
130 | #endif /* _UAPI__LINUX_BTF_H__ */ | ||