1 /* Copyright (C) 2019-2020 Free Software Foundation, Inc.
2 
3    This file is part of GCC.
4 
5    GCC is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3, or (at your option)
8    any later version.
9 
10    GCC is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    Under Section 7 of GPL version 3, you are granted additional
16    permissions described in the GCC Runtime Library Exception, version
17    3.1, as published by the Free Software Foundation.
18 
19    You should have received a copy of the GNU General Public License and
20    a copy of the GCC Runtime Library Exception along with this program;
21    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
22    <http://www.gnu.org/licenses/>.  */
23 
24 /* The purpose of this file is to provide a compatiblity layer with
25    the Linux kernel bpf_helpers.h header that is located in
26    linux/tools/testing/selftests/bpf/bpf_helpers.h.  That file is
27    currently llvm-specific.  */
28 
29 #ifndef __BPF_HELPERS_H
30 #define __BPF_HELPERS_H
31 
32 #define SEC(NAME) __attribute__((section(NAME), used))
33 
34 /* Flags used in some kernel helpers.  */
35 
36 #define BPF_ANY     0
37 #define BPF_NOEXIST 1
38 #define BPF_EXIST   2
39 
40 #define BPF_F_LOCK 4
41 #define BPF_F_NO_COMMON_LRU (1U << 1)
42 #define BPF_F_NUMA_NODE (1U << 2)
43 
44 /* Functions to call kernel helpers.  We provide the "standard" bpf_*
45    names as synonyms of the corresponding GCC builtins.  In some
46    cases, where non-void pointers are passed to the helper, inline
47    functions are used to achieve proper type checking.  */
48 
49 #ifndef KERNEL_VERSION
50 # define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
51 #endif
52 
53 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,0,0)
54 
55 #define bpf_map_lookup_elem	__builtin_bpf_helper_map_lookup_elem
56 #define bpf_map_update_elem	__builtin_bpf_helper_map_update_elem
57 #define bpf_map_delete_elem	__builtin_bpf_helper_map_delete_elem
58 
59 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,1,0)
60 
61 #define bpf_probe_read		__builtin_bpf_helper_probe_read
62 #define bpf_ktime_get_ns	__builtin_bpf_helper_ktime_get_ns
63 #define bpf_trace_printk	__builtin_bpf_helper_trace_printk
64 #define bpf_get_prandom_u32	__builtin_bpf_helper_get_prandom_u32
65 #define bpf_get_smp_processor_id __builtin_bpf_helper_get_smp_processor_id
66 #define bpf_skb_store_bytes	__builtin_bpf_helper_skb_store_bytes
67 #define bpf_l3_csum_replace	__builtin_bpf_helper_l3_csum_replace
68 #define bpf_l4_csum_replace	__builtin_bpf_helper_l4_csum_replace
69 
70 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,2,0)
71 
72 #define bpf_tail_call		__builtin_bpf_helper_tail_call
73 #define bpf_clone_redirect	__builtin_bpf_helper_clone_redirect
74 #define bpf_get_current_pid_tgid __builtin_bpf_helper_get_current_pid_tgid
75 #define bpf_get_current_uid_gid  __builtin_bpf_helper_get_current_uid_gid
76 #define bpf_get_current_comm	__builtin_bpf_helper_get_current_comm
77 
78 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,3,0)
79 
80 #define bpf_get_cgroup_classid	__builtin_bpf_helper_get_cgroup_classid
81 #define bpf_skb_vlan_push	__builtin_bpf_helper_skb_vlan_push
82 #define bpf_skb_vlan_pop	__builtin_bpf_helper_skb_vlan_pop
83 #define bpf_skb_get_tunnel_key	__builtin_bpf_helper_skb_get_tunnel_key
84 #define bpf_skb_set_tunnel_key	__builtin_bpf_helper_skb_set_tunnel_key
85 #define bpf_perf_event_read	__builtin_bpf_helper_perf_event_read
86 
87 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,4,0)
88 
89 #define bpf_redirect		__builtin_bpf_helper_redirect
90 #define bpf_get_route_realm	__builtin_bpf_helper_get_route_realm
91 #define bpf_perf_event_output	__builtin_bpf_helper_perf_event_output
92 
93 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,5,0)
94 
95 #define bpf_skb_load_bytes	__builtin_bpf_helper_skb_load_bytes
96 
97 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,6,0)
98 
99 #define bpf_get_stackid		__builtin_bpf_helper_get_stackid
100 #define bpf_csum_diff		__builtin_bpf_helper_csum_diff
101 #define bpf_skb_get_tunnel_opt	__builtin_bpf_helper_skb_get_tunnel_opt
102 #define bpf_skb_set_tunnel_opt	__builtin_bpf_helper_skb_set_tunnel_opt
103 
104 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,8,0)
105 
106 #define bpf_skb_change_proto	__builtin_bpf_helper_skb_change_proto
107 #define bpf_skb_change_type	__builtin_bpf_helper_skb_change_type
108 #define bpf_skb_under_cgroup	__builtin_bpf_helper_skb_under_cgroup
109 #define bpf_get_hash_recalc	__builtin_bpf_helper_get_hash_recalc
110 #define bpf_get_current_task	__builtin_bpf_helper_get_current_task
111 #define bpf_probe_write_user	__builtin_bpf_helper_probe_write_user
112 
113 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,9,0)
114 
115 #define bpf_current_task_under_cgroup __builtin_bpf_helper_current_task_under_cgroup
116 #define bpf_skb_change_tail	__builtin_bpf_helper_skb_change_tail
117 #define bpf_skb_pull_data	__builtin_bpf_helper_skb_pull_data
118 #define bpf_csum_update		__builtin_bpf_helper_csum_update
119 #define bpf_set_hash_invalid	__builtin_bpf_helper_set_hash_invalid
120 
121 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,10,0)
122 
123 #define bpf_get_numa_node_id	__builtin_bpf_helper_get_numa_node_id
124 #define bpf_skb_change_head	__builtin_bpf_helper_skb_change_head
125 #define bpf_xdp_adjust_head	__builtin_bpf_helper_xdp_adjust_head
126 
127 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,11,0)
128 
129 #define bpf_probe_read_str	__builtin_bpf_helper_probe_read_str
130 
131 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,12,0)
132 
133 #define bpf_get_socket_cookie	__builtin_bpf_helper_get_socket_cookie
134 #define bpf_get_socket_uid	__builtin_bpf_helper_get_socket_uid
135 
136 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,13,0)
137 
138 #define bpf_set_hash		__builtin_bpf_helper_set_hash
139 #define bpf_setsockopt		__builtin_bpf_helper_setsockopt
140 #define bpf_skb_adjust_room	__builtin_bpf_helper_skb_adjust_room
141 
142 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,14,0)
143 
144 #define bpf_redirect_map	__builtin_bpf_helper_redirect_map
145 #define bpf_sk_redirect_map	__builtin_bpf_helper_sk_redirect_map
146 #define bpf_sock_map_update	__builtin_bpf_helper_sock_map_update
147 
148 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,15,0)
149 
150 #define bpf_perf_event_read_value __builtin_bpf_helper_perf_event_read_value
151 #define bpf_perf_prog_read_value  __builtin_bpf_helper_perf_prog_read_value
152 #define bpf_getsockopt		  __builtin_bpf_helper_getsockopt
153 #define bpf_xdp_adjust_meta	__builtin_bpf_helper_xdp_adjust_meta
154 
155 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,16,0)
156 
157 #define bpf_override_return	__builtin_bpf_helper_override_return
158 #define bpf_sock_ops_cb_flags_set __builtin_bpf_helper_sock_ops_cb_flags_set
159 
160 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,17,0)
161 
162 #define bpf_msg_redirect_map	__builtin_bpf_helper_msg_redirect_map
163 #define bpf_msg_apply_bytes	__builtin_bpf_helper_msg_apply_bytes
164 #define bpf_msg_cork_bytes	__builtin_bpf_helper_msg_cork_bytes
165 #define bpf_pull_data		__builtin_bpf_helper_pull_data
166 #define bpf_bind		__builtin_bpf_helper_bpf_bind
167 
168 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,18,0)
169 
170 #define bpf_xdp_adjust_tail	__builtin_bpf_helper_xdp_adjust_tail
171 #define bpf_skb_get_xfrm_state	__builtin_bpf_helper_skb_get_xfrm_state
172 #define bpf_get_stack		__builtin_bpf_helper_get_stack
173 #define bpf_skb_load_bytes_relative __builtin_bpf_helper_skb_load_bytes_relative
174 #define bpf_sock_hash_update	__builtin_bpf_helper_sock_hash_update
175 #define bpf_msg_redirect_hash	__builtin_bpf_helper_msg_redirect_hash
176 #define bpf_sk_redirect_hash	__builtin_bpf_helper_sk_redirect_hash
177 #define bpf_lwt_push_encap		__builtin_bpf_helper_lwt_push_encap
178 #define bpf_lwt_seg6_store_bytes	__builtin_bpf_helper_lwt_seg6_store_bytes
179 #define bpf_lwt_seg6_adjust_srh		__builtin_bpf_helper_lwt_seg6_adjust_srh
180 #define bpf_lwt_seg6_action		__builtin_bpf_helper_lwt_seg6_action
181 #define bpf_rc_repeat			__builtin_bpf_helper_rc_repeat
182 #define bpf_rc_keydown			__builtin_bpf_helper_rc_keydown
183 #define bpf_skb_cgroup_id		__builtin_bpf_helper_skb_cgroup_id
184 #define bpf_get_current_cgroup_id	__builtin_bpf_helper_get_current_cgroup_id
185 
186 static inline int
bpf_fib_lookup(void * ctx,struct bpf_fib_lookup * param,int plen,unsigned int flags)187 bpf_fib_lookup (void *ctx, struct bpf_fib_lookup *param, int plen,
188 		unsigned int flags)
189 {
190   return __builtin_bpf_helper_fib_lookup (ctx, (void *) param, plen, flags);
191 }
192 
193 
194 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,19,0)
195 
196 #define bpf_get_local_storage	__builtin_bpf_helper_get_local_storage
197 #define bpf_sk_select_reuseport	__builtin_bpf_helper_sk_select_reuseport
198 #define bpf_skb_ancestor_cgroup_id	__builtin_bpf_helper_skb_ancestor_cgroup_id
199 
200 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,20,0)
201 
202 #define bpf_sk_release		__builtin_bpf_helper_sk_release
203 #define bpf_map_push_elem	__builtin_bpf_helper_map_push_elem
204 #define bpf_map_pop_elem	__builtin_bpf_helper_map_pop_elem
205 #define bpf_map_peek_elem	__builtin_bpf_helper_map_peek_elem
206 #define bpf_msg_push_data	__builtin_bpf_helper_msg_push_data
207 
208 static inline struct bpf_sock *
bpf_sk_lookup_tcp(void * ctx,struct bpf_sock_tuple * tuple,int size,unsigned long long netns_id,unsigned long long flags)209 bpf_sk_lookup_tcp (void *ctx, struct bpf_sock_tuple *tuple,
210 		   int size, unsigned long long netns_id,
211 		   unsigned long long flags)
212 {
213   return
214     (struct bpf_sock *) __builtin_bpf_helper_sk_lookup_tcp (ctx,
215 							    (void *) tuple,
216 							    size,
217 							    netns_id, flags);
218 }
219 
220 static inline struct bpf_sock *
bpf_sk_lookup_udp(void * ctx,struct bpf_sock_tuple * tuple,int size,unsigned long long netns_id,unsigned long long flags)221 bpf_sk_lookup_udp (void *ctx, struct bpf_sock_tuple *tuple,
222 		   int size, unsigned long long netns_id,
223 		   unsigned long long flags)
224 {
225   return
226     (struct bpf_sock *) __builtin_bpf_helper_sk_lookup_udp (ctx,
227 							    (void *) tuple,
228 							    size,
229 							    netns_id, flags);
230 }
231 
232 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (5,0,0)
233 
234 #define bpf_msg_pop_data	__builtin_bpf_helper_pop_data
235 #define bpf_rc_pointer_rel	__builtin_bpf_helper_rc_pointer_rel
236 
237 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (5,1,0)
238 
239 #define bpf_spin_lock		__builtin_bpf_helper_spin_lock
240 #define bpf_spin_unlock		__builtin_bpf_helper_spin_unlock
241 #define bpf_skb_ecn_set_ce	__builtin_bpf_helper_skb_ecn_set_ce
242 
243 static inline struct bpf_sock *
bpf_sk_fullsock(struct bpf_sock * sk)244 bpf_sk_fullsock (struct bpf_sock *sk)
245 {
246   return
247     (struct bpf_sock *) __builtin_bpf_helper_sk_fullsock ((void *) sk);
248 }
249 
250 static inline struct bpf_sock *
bpf_tcp_sock(struct bpf_sock * sk)251 bpf_tcp_sock (struct bpf_sock *sk)
252 {
253   return
254     (struct bpf_sock *) __builtin_bpf_helper_tcp_sock ((void *) sk);
255 }
256 
257 static inline struct bpf_sock *
bpf_get_listener_sock(struct bpf_sock * sk)258 bpf_get_listener_sock (struct bpf_sock *sk)
259 {
260   return
261     (struct bpf_sock *) __builtin_bpf_helper_get_listener_sock ((void *) sk);
262 }
263 
264 #if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (5,2,0)
265 
266 
267 #endif /* 5.2 */
268 #endif /* 5.1 */
269 #endif /* 5.0 */
270 #endif /* 4.20 */
271 #endif /* 4.19 */
272 #endif /* 4.18 */
273 #endif /* 4.17 */
274 #endif /* 4.16 */
275 #endif /* 4.15 */
276 #endif /* 4.14 */
277 #endif /* 4.13 */
278 #endif /* 4.12 */
279 #endif /* 4.11 */
280 #endif /* 4.10 */
281 #endif /* 4.9 */
282 #endif /* 4.8 */
283 #endif /* 4.6 */
284 #endif /* 4.5 */
285 #endif /* 4.4 */
286 #endif /* 4.3 */
287 #endif /* 4.2 */
288 #endif /* 4.1 */
289 #endif /* 4.0 */
290 
291 /* Functions to emit BPF_LD_ABS and BPF_LD_IND instructions.  We
292    provide the "standard" names as synonyms of the corresponding GCC
293    builtins.  Note how the SKB argument is ignored.  */
294 
295 static inline long long
load_byte(void * skb,unsigned long long off)296 load_byte (void *skb __attribute__ ((unused)),
297 	   unsigned long long off)
298 {
299   return __builtin_bpf_load_byte (off);
300 }
301 
302 static inline long long
load_half(void * skb,unsigned long long off)303 load_half (void *skb __attribute__ ((unused)),
304 	   unsigned long long off)
305 {
306   return __builtin_bpf_load_half (off);
307 }
308 
309 static inline long long
load_word(void * skb,unsigned long long off)310 load_word (void *skb __attribute__ ((unused)),
311 	   unsigned long long off)
312 {
313   return __builtin_bpf_load_word (off);
314 }
315 
316 struct bpf_map_def
317 {
318   unsigned int type;
319   unsigned int key_size;
320   unsigned int value_size;
321   unsigned int max_entries;
322   unsigned int map_flags;
323   unsigned int inner_map_idx;
324   unsigned int numa_node;
325 };
326 
327 #endif /* ! __BPF_HELPERS_H */
328