xref: /linux/tools/testing/selftests/bpf/bpf_experimental.h (revision 0c7c237b1c35011ef0b8d30c1d5c20bc6ae7b69b)
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 #endif
135