xref: /linux/tools/testing/selftests/bpf/bpf_experimental.h (revision 4e94ddfe2aab72139acb8d5372fac9e6c3f3e383)
1 #ifndef __BPF_EXPERIMENTAL__
2 #define __BPF_EXPERIMENTAL__
3 
4 #include <vmlinux.h>
5 #include <bpf/bpf_tracing.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_core_read.h>
8 
9 #define __contains(name, node) __attribute__((btf_decl_tag("contains:" #name ":" #node)))
10 
11 /* Description
12  *	Allocates an object of the type represented by 'local_type_id' in
13  *	program BTF. User may use the bpf_core_type_id_local macro to pass the
14  *	type ID of a struct in program BTF.
15  *
16  *	The 'local_type_id' parameter must be a known constant.
17  *	The 'meta' parameter is rewritten by the verifier, no need for BPF
18  *	program to set it.
19  * Returns
20  *	A pointer to an object of the type corresponding to the passed in
21  *	'local_type_id', or NULL on failure.
22  */
23 extern void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
24 
25 /* Convenience macro to wrap over bpf_obj_new_impl */
26 #define bpf_obj_new(type) ((type *)bpf_obj_new_impl(bpf_core_type_id_local(type), NULL))
27 
28 /* Description
29  *	Free an allocated object. All fields of the object that require
30  *	destruction will be destructed before the storage is freed.
31  *
32  *	The 'meta' parameter is rewritten by the verifier, no need for BPF
33  *	program to set it.
34  * Returns
35  *	Void.
36  */
37 extern void bpf_obj_drop_impl(void *kptr, void *meta) __ksym;
38 
39 /* Convenience macro to wrap over bpf_obj_drop_impl */
40 #define bpf_obj_drop(kptr) bpf_obj_drop_impl(kptr, NULL)
41 
42 /* Description
43  *	Increment the refcount on a refcounted local kptr, turning the
44  *	non-owning reference input into an owning reference in the process.
45  *
46  *	The 'meta' parameter is rewritten by the verifier, no need for BPF
47  *	program to set it.
48  * Returns
49  *	An owning reference to the object pointed to by 'kptr'
50  */
51 extern void *bpf_refcount_acquire_impl(void *kptr, void *meta) __ksym;
52 
53 /* Convenience macro to wrap over bpf_refcount_acquire_impl */
54 #define bpf_refcount_acquire(kptr) bpf_refcount_acquire_impl(kptr, NULL)
55 
56 /* Description
57  *	Add a new entry to the beginning of the BPF linked list.
58  *
59  *	The 'meta' and 'off' parameters are rewritten by the verifier, no need
60  *	for BPF programs to set them
61  * Returns
62  *	0 if the node was successfully added
63  *	-EINVAL if the node wasn't added because it's already in a list
64  */
65 extern int bpf_list_push_front_impl(struct bpf_list_head *head,
66 				    struct bpf_list_node *node,
67 				    void *meta, __u64 off) __ksym;
68 
69 /* Convenience macro to wrap over bpf_list_push_front_impl */
70 #define bpf_list_push_front(head, node) bpf_list_push_front_impl(head, node, NULL, 0)
71 
72 /* Description
73  *	Add a new entry to the end of the BPF linked list.
74  *
75  *	The 'meta' and 'off' parameters are rewritten by the verifier, no need
76  *	for BPF programs to set them
77  * Returns
78  *	0 if the node was successfully added
79  *	-EINVAL if the node wasn't added because it's already in a list
80  */
81 extern int bpf_list_push_back_impl(struct bpf_list_head *head,
82 				   struct bpf_list_node *node,
83 				   void *meta, __u64 off) __ksym;
84 
85 /* Convenience macro to wrap over bpf_list_push_back_impl */
86 #define bpf_list_push_back(head, node) bpf_list_push_back_impl(head, node, NULL, 0)
87 
88 /* Description
89  *	Remove the entry at the beginning of the BPF linked list.
90  * Returns
91  *	Pointer to bpf_list_node of deleted entry, or NULL if list is empty.
92  */
93 extern struct bpf_list_node *bpf_list_pop_front(struct bpf_list_head *head) __ksym;
94 
95 /* Description
96  *	Remove the entry at the end of the BPF linked list.
97  * Returns
98  *	Pointer to bpf_list_node of deleted entry, or NULL if list is empty.
99  */
100 extern struct bpf_list_node *bpf_list_pop_back(struct bpf_list_head *head) __ksym;
101 
102 /* Description
103  *	Remove 'node' from rbtree with root 'root'
104  * Returns
105  * 	Pointer to the removed node, or NULL if 'root' didn't contain 'node'
106  */
107 extern struct bpf_rb_node *bpf_rbtree_remove(struct bpf_rb_root *root,
108 					     struct bpf_rb_node *node) __ksym;
109 
110 /* Description
111  *	Add 'node' to rbtree with root 'root' using comparator 'less'
112  *
113  *	The 'meta' and 'off' parameters are rewritten by the verifier, no need
114  *	for BPF programs to set them
115  * Returns
116  *	0 if the node was successfully added
117  *	-EINVAL if the node wasn't added because it's already in a tree
118  */
119 extern int bpf_rbtree_add_impl(struct bpf_rb_root *root, struct bpf_rb_node *node,
120 			       bool (less)(struct bpf_rb_node *a, const struct bpf_rb_node *b),
121 			       void *meta, __u64 off) __ksym;
122 
123 /* Convenience macro to wrap over bpf_rbtree_add_impl */
124 #define bpf_rbtree_add(head, node, less) bpf_rbtree_add_impl(head, node, less, NULL, 0)
125 
126 /* Description
127  *	Return the first (leftmost) node in input tree
128  * Returns
129  *	Pointer to the node, which is _not_ removed from the tree. If the tree
130  *	contains no nodes, returns NULL.
131  */
132 extern struct bpf_rb_node *bpf_rbtree_first(struct bpf_rb_root *root) __ksym;
133 
134 /* Description
135  *	Allocates a percpu object of the type represented by 'local_type_id' in
136  *	program BTF. User may use the bpf_core_type_id_local macro to pass the
137  *	type ID of a struct in program BTF.
138  *
139  *	The 'local_type_id' parameter must be a known constant.
140  *	The 'meta' parameter is rewritten by the verifier, no need for BPF
141  *	program to set it.
142  * Returns
143  *	A pointer to a percpu object of the type corresponding to the passed in
144  *	'local_type_id', or NULL on failure.
145  */
146 extern void *bpf_percpu_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
147 
148 /* Convenience macro to wrap over bpf_percpu_obj_new_impl */
149 #define bpf_percpu_obj_new(type) ((type __percpu_kptr *)bpf_percpu_obj_new_impl(bpf_core_type_id_local(type), NULL))
150 
151 /* Description
152  *	Free an allocated percpu object. All fields of the object that require
153  *	destruction will be destructed before the storage is freed.
154  *
155  *	The 'meta' parameter is rewritten by the verifier, no need for BPF
156  *	program to set it.
157  * Returns
158  *	Void.
159  */
160 extern void bpf_percpu_obj_drop_impl(void *kptr, void *meta) __ksym;
161 
162 struct bpf_iter_task_vma;
163 
164 extern int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,
165 				 struct task_struct *task,
166 				 unsigned long addr) __ksym;
167 extern struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it) __ksym;
168 extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym;
169 
170 /* Convenience macro to wrap over bpf_obj_drop_impl */
171 #define bpf_percpu_obj_drop(kptr) bpf_percpu_obj_drop_impl(kptr, NULL)
172 
173 /* Description
174  *	Throw a BPF exception from the program, immediately terminating its
175  *	execution and unwinding the stack. The supplied 'cookie' parameter
176  *	will be the return value of the program when an exception is thrown,
177  *	and the default exception callback is used. Otherwise, if an exception
178  *	callback is set using the '__exception_cb(callback)' declaration tag
179  *	on the main program, the 'cookie' parameter will be the callback's only
180  *	input argument.
181  *
182  *	Thus, in case of default exception callback, 'cookie' is subjected to
183  *	constraints on the program's return value (as with R0 on exit).
184  *	Otherwise, the return value of the marked exception callback will be
185  *	subjected to the same checks.
186  *
187  *	Note that throwing an exception with lingering resources (locks,
188  *	references, etc.) will lead to a verification error.
189  *
190  *	Note that callbacks *cannot* call this helper.
191  * Returns
192  *	Never.
193  * Throws
194  *	An exception with the specified 'cookie' value.
195  */
196 extern void bpf_throw(u64 cookie) __ksym;
197 
198 /* This macro must be used to mark the exception callback corresponding to the
199  * main program. For example:
200  *
201  * int exception_cb(u64 cookie) {
202  *	return cookie;
203  * }
204  *
205  * SEC("tc")
206  * __exception_cb(exception_cb)
207  * int main_prog(struct __sk_buff *ctx) {
208  *	...
209  *	return TC_ACT_OK;
210  * }
211  *
212  * Here, exception callback for the main program will be 'exception_cb'. Note
213  * that this attribute can only be used once, and multiple exception callbacks
214  * specified for the main program will lead to verification error.
215  */
216 #define __exception_cb(name) __attribute__((btf_decl_tag("exception_callback:" #name)))
217 
218 #define __bpf_assert_signed(x) _Generic((x), \
219     unsigned long: 0,       \
220     unsigned long long: 0,  \
221     signed long: 1,         \
222     signed long long: 1     \
223 )
224 
225 #define __bpf_assert_check(LHS, op, RHS)								 \
226 	_Static_assert(sizeof(&(LHS)), "1st argument must be an lvalue expression");			 \
227 	_Static_assert(sizeof(LHS) == 8, "Only 8-byte integers are supported\n");			 \
228 	_Static_assert(__builtin_constant_p(__bpf_assert_signed(LHS)), "internal static assert");	 \
229 	_Static_assert(__builtin_constant_p((RHS)), "2nd argument must be a constant expression")
230 
231 #define __bpf_assert(LHS, op, cons, RHS, VAL)							\
232 	({											\
233 		(void)bpf_throw;								\
234 		asm volatile ("if %[lhs] " op " %[rhs] goto +2; r1 = %[value]; call bpf_throw"	\
235 			       : : [lhs] "r"(LHS), [rhs] cons(RHS), [value] "ri"(VAL) : );	\
236 	})
237 
238 #define __bpf_assert_op_sign(LHS, op, cons, RHS, VAL, supp_sign)			\
239 	({										\
240 		__bpf_assert_check(LHS, op, RHS);					\
241 		if (__bpf_assert_signed(LHS) && !(supp_sign))				\
242 			__bpf_assert(LHS, "s" #op, cons, RHS, VAL);			\
243 		else									\
244 			__bpf_assert(LHS, #op, cons, RHS, VAL);				\
245 	 })
246 
247 #define __bpf_assert_op(LHS, op, RHS, VAL, supp_sign)					\
248 	({										\
249 		if (sizeof(typeof(RHS)) == 8) {						\
250 			const typeof(RHS) rhs_var = (RHS);				\
251 			__bpf_assert_op_sign(LHS, op, "r", rhs_var, VAL, supp_sign);	\
252 		} else {								\
253 			__bpf_assert_op_sign(LHS, op, "i", RHS, VAL, supp_sign);	\
254 		}									\
255 	 })
256 
257 /* Description
258  *	Assert that a conditional expression is true.
259  * Returns
260  *	Void.
261  * Throws
262  *	An exception with the value zero when the assertion fails.
263  */
264 #define bpf_assert(cond) if (!(cond)) bpf_throw(0);
265 
266 /* Description
267  *	Assert that a conditional expression is true.
268  * Returns
269  *	Void.
270  * Throws
271  *	An exception with the specified value when the assertion fails.
272  */
273 #define bpf_assert_with(cond, value) if (!(cond)) bpf_throw(value);
274 
275 /* Description
276  *	Assert that LHS is equal to RHS. This statement updates the known value
277  *	of LHS during verification. Note that RHS must be a constant value, and
278  *	must fit within the data type of LHS.
279  * Returns
280  *	Void.
281  * Throws
282  *	An exception with the value zero when the assertion fails.
283  */
284 #define bpf_assert_eq(LHS, RHS)						\
285 	({								\
286 		barrier_var(LHS);					\
287 		__bpf_assert_op(LHS, ==, RHS, 0, true);			\
288 	})
289 
290 /* Description
291  *	Assert that LHS is equal to RHS. This statement updates the known value
292  *	of LHS during verification. Note that RHS must be a constant value, and
293  *	must fit within the data type of LHS.
294  * Returns
295  *	Void.
296  * Throws
297  *	An exception with the specified value when the assertion fails.
298  */
299 #define bpf_assert_eq_with(LHS, RHS, value)				\
300 	({								\
301 		barrier_var(LHS);					\
302 		__bpf_assert_op(LHS, ==, RHS, value, true);		\
303 	})
304 
305 /* Description
306  *	Assert that LHS is less than RHS. This statement updates the known
307  *	bounds of LHS during verification. Note that RHS must be a constant
308  *	value, and must fit within the data type of LHS.
309  * Returns
310  *	Void.
311  * Throws
312  *	An exception with the value zero when the assertion fails.
313  */
314 #define bpf_assert_lt(LHS, RHS)						\
315 	({								\
316 		barrier_var(LHS);					\
317 		__bpf_assert_op(LHS, <, RHS, 0, false);			\
318 	})
319 
320 /* Description
321  *	Assert that LHS is less than RHS. This statement updates the known
322  *	bounds of LHS during verification. Note that RHS must be a constant
323  *	value, and must fit within the data type of LHS.
324  * Returns
325  *	Void.
326  * Throws
327  *	An exception with the specified value when the assertion fails.
328  */
329 #define bpf_assert_lt_with(LHS, RHS, value)				\
330 	({								\
331 		barrier_var(LHS);					\
332 		__bpf_assert_op(LHS, <, RHS, value, false);		\
333 	})
334 
335 /* Description
336  *	Assert that LHS is greater than RHS. This statement updates the known
337  *	bounds of LHS during verification. Note that RHS must be a constant
338  *	value, and must fit within the data type of LHS.
339  * Returns
340  *	Void.
341  * Throws
342  *	An exception with the value zero when the assertion fails.
343  */
344 #define bpf_assert_gt(LHS, RHS)						\
345 	({								\
346 		barrier_var(LHS);					\
347 		__bpf_assert_op(LHS, >, RHS, 0, false);			\
348 	})
349 
350 /* Description
351  *	Assert that LHS is greater than RHS. This statement updates the known
352  *	bounds of LHS during verification. Note that RHS must be a constant
353  *	value, and must fit within the data type of LHS.
354  * Returns
355  *	Void.
356  * Throws
357  *	An exception with the specified value when the assertion fails.
358  */
359 #define bpf_assert_gt_with(LHS, RHS, value)				\
360 	({								\
361 		barrier_var(LHS);					\
362 		__bpf_assert_op(LHS, >, RHS, value, false);		\
363 	})
364 
365 /* Description
366  *	Assert that LHS is less than or equal to RHS. This statement updates the
367  *	known bounds of LHS during verification. Note that RHS must be a
368  *	constant value, and must fit within the data type of LHS.
369  * Returns
370  *	Void.
371  * Throws
372  *	An exception with the value zero when the assertion fails.
373  */
374 #define bpf_assert_le(LHS, RHS)						\
375 	({								\
376 		barrier_var(LHS);					\
377 		__bpf_assert_op(LHS, <=, RHS, 0, false);		\
378 	})
379 
380 /* Description
381  *	Assert that LHS is less than or equal to RHS. This statement updates the
382  *	known bounds of LHS during verification. Note that RHS must be a
383  *	constant value, and must fit within the data type of LHS.
384  * Returns
385  *	Void.
386  * Throws
387  *	An exception with the specified value when the assertion fails.
388  */
389 #define bpf_assert_le_with(LHS, RHS, value)				\
390 	({								\
391 		barrier_var(LHS);					\
392 		__bpf_assert_op(LHS, <=, RHS, value, false);		\
393 	})
394 
395 /* Description
396  *	Assert that LHS is greater than or equal to RHS. This statement updates
397  *	the known bounds of LHS during verification. Note that RHS must be a
398  *	constant value, and must fit within the data type of LHS.
399  * Returns
400  *	Void.
401  * Throws
402  *	An exception with the value zero when the assertion fails.
403  */
404 #define bpf_assert_ge(LHS, RHS)						\
405 	({								\
406 		barrier_var(LHS);					\
407 		__bpf_assert_op(LHS, >=, RHS, 0, false);		\
408 	})
409 
410 /* Description
411  *	Assert that LHS is greater than or equal to RHS. This statement updates
412  *	the known bounds of LHS during verification. Note that RHS must be a
413  *	constant value, and must fit within the data type of LHS.
414  * Returns
415  *	Void.
416  * Throws
417  *	An exception with the specified value when the assertion fails.
418  */
419 #define bpf_assert_ge_with(LHS, RHS, value)				\
420 	({								\
421 		barrier_var(LHS);					\
422 		__bpf_assert_op(LHS, >=, RHS, value, false);		\
423 	})
424 
425 /* Description
426  *	Assert that LHS is in the range [BEG, END] (inclusive of both). This
427  *	statement updates the known bounds of LHS during verification. Note
428  *	that both BEG and END must be constant values, and must fit within the
429  *	data type of LHS.
430  * Returns
431  *	Void.
432  * Throws
433  *	An exception with the value zero when the assertion fails.
434  */
435 #define bpf_assert_range(LHS, BEG, END)					\
436 	({								\
437 		_Static_assert(BEG <= END, "BEG must be <= END");	\
438 		barrier_var(LHS);					\
439 		__bpf_assert_op(LHS, >=, BEG, 0, false);		\
440 		__bpf_assert_op(LHS, <=, END, 0, false);		\
441 	})
442 
443 /* Description
444  *	Assert that LHS is in the range [BEG, END] (inclusive of both). This
445  *	statement updates the known bounds of LHS during verification. Note
446  *	that both BEG and END must be constant values, and must fit within the
447  *	data type of LHS.
448  * Returns
449  *	Void.
450  * Throws
451  *	An exception with the specified value when the assertion fails.
452  */
453 #define bpf_assert_range_with(LHS, BEG, END, value)			\
454 	({								\
455 		_Static_assert(BEG <= END, "BEG must be <= END");	\
456 		barrier_var(LHS);					\
457 		__bpf_assert_op(LHS, >=, BEG, value, false);		\
458 		__bpf_assert_op(LHS, <=, END, value, false);		\
459 	})
460 
461 struct bpf_iter_css_task;
462 struct cgroup_subsys_state;
463 extern int bpf_iter_css_task_new(struct bpf_iter_css_task *it,
464 		struct cgroup_subsys_state *css, unsigned int flags) __weak __ksym;
465 extern struct task_struct *bpf_iter_css_task_next(struct bpf_iter_css_task *it) __weak __ksym;
466 extern void bpf_iter_css_task_destroy(struct bpf_iter_css_task *it) __weak __ksym;
467 
468 struct bpf_iter_task;
469 extern int bpf_iter_task_new(struct bpf_iter_task *it,
470 		struct task_struct *task, unsigned int flags) __weak __ksym;
471 extern struct task_struct *bpf_iter_task_next(struct bpf_iter_task *it) __weak __ksym;
472 extern void bpf_iter_task_destroy(struct bpf_iter_task *it) __weak __ksym;
473 
474 struct bpf_iter_css;
475 extern int bpf_iter_css_new(struct bpf_iter_css *it,
476 				struct cgroup_subsys_state *start, unsigned int flags) __weak __ksym;
477 extern struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *it) __weak __ksym;
478 extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;
479 
480 #endif
481