1 /*
2  * Copyright (c) 2015-2018 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef STRACE_BPF_ATTR_H
29 #define STRACE_BPF_ATTR_H
30 
31 /*
32  * The policy is that all fields of type uint64_t in this header file
33  * must have ATTRIBUTE_ALIGNED(8).
34  *
35  * This should not cause any contradictions with <linux/bpf.h>
36  * unless the latter is buggy.
37  *
38  * By word "buggy" I mean containing such changes as Linux kernel commit
39  * v4.16-rc1~123^2~109^2~5^2~4.
40  */
41 
42 #ifndef BPF_OBJ_NAME_LEN
43 # define BPF_OBJ_NAME_LEN 16U
44 #else
45 # if BPF_OBJ_NAME_LEN != 16U
46 #  error "Unexpected value of BPF_OBJ_NAME_LEN"
47 # endif
48 #endif
49 
50 #ifndef BPF_TAG_SIZE
51 # define BPF_TAG_SIZE 8
52 #else
53 # if BPF_TAG_SIZE != 8
54 #  error "Unexpected value of BPF_TAG_SIZE"
55 # endif
56 #endif
57 
58 struct BPF_MAP_CREATE_struct {
59 	uint32_t map_type;
60 	uint32_t key_size;
61 	uint32_t value_size;
62 	uint32_t max_entries;
63 	uint32_t map_flags;
64 	uint32_t inner_map_fd;
65 	uint32_t numa_node;
66 	char     map_name[BPF_OBJ_NAME_LEN];
67 	uint32_t map_ifindex;
68 };
69 
70 #define BPF_MAP_CREATE_struct_size \
71 	sizeof(struct BPF_MAP_CREATE_struct)
72 #define expected_BPF_MAP_CREATE_struct_size 48
73 
74 struct BPF_MAP_LOOKUP_ELEM_struct {
75 	uint32_t map_fd;
76 	uint64_t ATTRIBUTE_ALIGNED(8) key;
77 	uint64_t ATTRIBUTE_ALIGNED(8) value;
78 };
79 
80 #define BPF_MAP_LOOKUP_ELEM_struct_size \
81 	sizeof(struct BPF_MAP_LOOKUP_ELEM_struct)
82 #define expected_BPF_MAP_LOOKUP_ELEM_struct_size 24
83 
84 struct BPF_MAP_UPDATE_ELEM_struct {
85 	uint32_t map_fd;
86 	uint64_t ATTRIBUTE_ALIGNED(8) key;
87 	uint64_t ATTRIBUTE_ALIGNED(8) value;
88 	uint64_t ATTRIBUTE_ALIGNED(8) flags;
89 };
90 
91 #define BPF_MAP_UPDATE_ELEM_struct_size \
92 	sizeof(struct BPF_MAP_UPDATE_ELEM_struct)
93 #define expected_BPF_MAP_UPDATE_ELEM_struct_size 32
94 
95 struct BPF_MAP_DELETE_ELEM_struct {
96 	uint32_t map_fd;
97 	uint64_t ATTRIBUTE_ALIGNED(8) key;
98 };
99 
100 #define BPF_MAP_DELETE_ELEM_struct_size \
101 	sizeof(struct BPF_MAP_DELETE_ELEM_struct)
102 #define expected_BPF_MAP_DELETE_ELEM_struct_size 16
103 
104 struct BPF_MAP_GET_NEXT_KEY_struct {
105 	uint32_t map_fd;
106 	uint64_t ATTRIBUTE_ALIGNED(8) key;
107 	uint64_t ATTRIBUTE_ALIGNED(8) next_key;
108 };
109 
110 #define BPF_MAP_GET_NEXT_KEY_struct_size \
111 	sizeof(struct BPF_MAP_GET_NEXT_KEY_struct)
112 #define expected_BPF_MAP_GET_NEXT_KEY_struct_size 24
113 
114 struct BPF_PROG_LOAD_struct {
115 	uint32_t prog_type;
116 	uint32_t insn_cnt;
117 	uint64_t ATTRIBUTE_ALIGNED(8) insns;
118 	uint64_t ATTRIBUTE_ALIGNED(8) license;
119 	uint32_t log_level;
120 	uint32_t log_size;
121 	uint64_t ATTRIBUTE_ALIGNED(8) log_buf;
122 	uint32_t kern_version;
123 	uint32_t prog_flags;
124 	char     prog_name[BPF_OBJ_NAME_LEN];
125 	uint32_t prog_ifindex;
126 	uint32_t expected_attach_type;
127 };
128 
129 #define BPF_PROG_LOAD_struct_size \
130 	offsetofend(struct BPF_PROG_LOAD_struct, expected_attach_type)
131 #define expected_BPF_PROG_LOAD_struct_size 72
132 
133 struct BPF_OBJ_PIN_struct {
134 	uint64_t ATTRIBUTE_ALIGNED(8) pathname;
135 	uint32_t bpf_fd;
136 	uint32_t file_flags;
137 };
138 
139 #define BPF_OBJ_PIN_struct_size \
140 	sizeof(struct BPF_OBJ_PIN_struct)
141 #define expected_BPF_OBJ_PIN_struct_size 16
142 
143 #define BPF_OBJ_GET_struct BPF_OBJ_PIN_struct
144 #define BPF_OBJ_GET_struct_size BPF_OBJ_PIN_struct_size
145 
146 struct BPF_PROG_ATTACH_struct {
147 	uint32_t target_fd;
148 	uint32_t attach_bpf_fd;
149 	uint32_t attach_type;
150 	uint32_t attach_flags;
151 };
152 
153 #define BPF_PROG_ATTACH_struct_size \
154 	sizeof(struct BPF_PROG_ATTACH_struct)
155 #define expected_BPF_PROG_ATTACH_struct_size 16
156 
157 struct BPF_PROG_DETACH_struct {
158 	uint32_t target_fd;
159 	uint32_t dummy;
160 	uint32_t attach_type;
161 };
162 
163 #define BPF_PROG_DETACH_struct_size \
164 	sizeof(struct BPF_PROG_DETACH_struct)
165 #define expected_BPF_PROG_DETACH_struct_size 12
166 
167 struct BPF_PROG_TEST_RUN_struct /* test */ {
168 	uint32_t prog_fd;
169 	uint32_t retval;
170 	uint32_t data_size_in;
171 	uint32_t data_size_out;
172 	uint64_t ATTRIBUTE_ALIGNED(8) data_in;
173 	uint64_t ATTRIBUTE_ALIGNED(8) data_out;
174 	uint32_t repeat;
175 	uint32_t duration;
176 };
177 
178 #define BPF_PROG_TEST_RUN_struct_size \
179 	sizeof(struct BPF_PROG_TEST_RUN_struct)
180 #define expected_BPF_PROG_TEST_RUN_struct_size 40
181 
182 struct BPF_PROG_GET_NEXT_ID_struct {
183 	uint32_t start_id;
184 	uint32_t next_id;
185 	uint32_t open_flags;
186 };
187 
188 #define BPF_PROG_GET_NEXT_ID_struct_size \
189 	sizeof(struct BPF_PROG_GET_NEXT_ID_struct)
190 #define expected_BPF_PROG_GET_NEXT_ID_struct_size 12
191 
192 #define BPF_MAP_GET_NEXT_ID_struct BPF_PROG_GET_NEXT_ID_struct
193 #define BPF_MAP_GET_NEXT_ID_struct_size BPF_PROG_GET_NEXT_ID_struct_size
194 
195 struct BPF_PROG_GET_FD_BY_ID_struct {
196 	uint32_t prog_id;
197 	uint32_t next_id;
198 	uint32_t open_flags;
199 };
200 
201 #define BPF_PROG_GET_FD_BY_ID_struct_size \
202 	sizeof(struct BPF_PROG_GET_FD_BY_ID_struct)
203 #define expected_BPF_PROG_GET_FD_BY_ID_struct_size 12
204 
205 struct BPF_MAP_GET_FD_BY_ID_struct {
206 	uint32_t map_id;
207 	uint32_t next_id;
208 	uint32_t open_flags;
209 };
210 
211 #define BPF_MAP_GET_FD_BY_ID_struct_size \
212 	sizeof(struct BPF_MAP_GET_FD_BY_ID_struct)
213 #define expected_BPF_MAP_GET_FD_BY_ID_struct_size 12
214 
215 struct BPF_OBJ_GET_INFO_BY_FD_struct /* info */ {
216 	uint32_t bpf_fd;
217 	uint32_t info_len;
218 	uint64_t ATTRIBUTE_ALIGNED(8) info;
219 };
220 
221 #define BPF_OBJ_GET_INFO_BY_FD_struct_size \
222 	sizeof(struct BPF_OBJ_GET_INFO_BY_FD_struct)
223 #define expected_BPF_OBJ_GET_INFO_BY_FD_struct_size 16
224 
225 struct BPF_PROG_QUERY_struct /* query */ {
226 	uint32_t target_fd;
227 	uint32_t attach_type;
228 	uint32_t query_flags;
229 	uint32_t attach_flags;
230 	uint64_t ATTRIBUTE_ALIGNED(8) prog_ids;
231 	uint32_t prog_cnt;
232 };
233 
234 #define BPF_PROG_QUERY_struct_size \
235 	offsetofend(struct BPF_PROG_QUERY_struct, prog_cnt)
236 #define expected_BPF_PROG_QUERY_struct_size 28
237 
238 struct BPF_RAW_TRACEPOINT_OPEN_struct /* raw_tracepoint */ {
239 	uint64_t ATTRIBUTE_ALIGNED(8) name;
240 	uint32_t prog_fd;
241 };
242 
243 #define BPF_RAW_TRACEPOINT_OPEN_struct_size \
244 	offsetofend(struct BPF_RAW_TRACEPOINT_OPEN_struct, prog_fd)
245 #define expected_BPF_RAW_TRACEPOINT_OPEN_struct_size 12
246 
247 struct bpf_map_info_struct {
248 	uint32_t type;
249 	uint32_t id;
250 	uint32_t key_size;
251 	uint32_t value_size;
252 	uint32_t max_entries;
253 	uint32_t map_flags;
254 	char     name[BPF_OBJ_NAME_LEN];
255 	uint32_t ifindex;
256 	/*
257 	 * The kernel UAPI is broken by Linux commit
258 	 * v4.16-rc1~123^2~109^2~5^2~4 .
259 	 */
260 	uint64_t ATTRIBUTE_ALIGNED(8) netns_dev; /* skip check */
261 	uint64_t ATTRIBUTE_ALIGNED(8) netns_ino; /* skip check */
262 };
263 
264 #define bpf_map_info_struct_size \
265 	sizeof(struct bpf_map_info_struct)
266 #define expected_bpf_map_info_struct_size 64
267 
268 struct bpf_prog_info_struct {
269 	uint32_t type;
270 	uint32_t id;
271 	uint8_t  tag[BPF_TAG_SIZE];
272 	uint32_t jited_prog_len;
273 	uint32_t xlated_prog_len;
274 	uint64_t ATTRIBUTE_ALIGNED(8) jited_prog_insns;
275 	uint64_t ATTRIBUTE_ALIGNED(8) xlated_prog_insns;
276 	uint64_t ATTRIBUTE_ALIGNED(8) load_time;
277 	uint32_t created_by_uid;
278 	uint32_t nr_map_ids;
279 	uint64_t ATTRIBUTE_ALIGNED(8) map_ids;
280 	char     name[BPF_OBJ_NAME_LEN];
281 	uint32_t ifindex;
282 	/*
283 	 * The kernel UAPI is broken by Linux commit
284 	 * v4.16-rc1~123^2~227^2~5^2~2 .
285 	 */
286 	uint64_t ATTRIBUTE_ALIGNED(8) netns_dev; /* skip check */
287 	uint64_t ATTRIBUTE_ALIGNED(8) netns_ino; /* skip check */
288 };
289 
290 #define bpf_prog_info_struct_size \
291 	sizeof(struct bpf_prog_info_struct)
292 #define expected_bpf_prog_info_struct_size 104
293 
294 #endif /* !STRACE_BPF_ATTR_H */
295