summaryrefslogtreecommitdiff
path: root/gcc/config/bpf/bpf-helpers.h
blob: 1dd05c8ef6902db949289c88da3c11bb38a26b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* Copyright (C) 2019-2020 Free Software Foundation, Inc.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */

/* The purpose of this file is to provide a compatiblity layer with
   the Linux kernel bpf_helpers.h header that is located in
   linux/tools/testing/selftests/bpf/bpf_helpers.h.  That file is
   currently llvm-specific.  */

#ifndef __BPF_HELPERS_H
#define __BPF_HELPERS_H

#define SEC(NAME) __attribute__((section(NAME), used))

/* Flags used in some kernel helpers.  */

#define BPF_ANY     0
#define BPF_NOEXIST 1
#define BPF_EXIST   2

#define BPF_F_LOCK 4
#define BPF_F_NO_COMMON_LRU (1U << 1)
#define BPF_F_NUMA_NODE (1U << 2)

/* Functions to call kernel helpers.  We provide the "standard" bpf_*
   names as synonyms of the corresponding GCC builtins.  In some
   cases, where non-void pointers are passed to the helper, inline
   functions are used to achieve proper type checking.  */

#ifndef KERNEL_VERSION
# define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,0,0)

#define bpf_map_lookup_elem	__builtin_bpf_helper_map_lookup_elem
#define bpf_map_update_elem	__builtin_bpf_helper_map_update_elem
#define bpf_map_delete_elem	__builtin_bpf_helper_map_delete_elem

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,1,0)

#define bpf_probe_read		__builtin_bpf_helper_probe_read
#define bpf_ktime_get_ns	__builtin_bpf_helper_ktime_get_ns
#define bpf_trace_printk	__builtin_bpf_helper_trace_printk
#define bpf_get_prandom_u32	__builtin_bpf_helper_get_prandom_u32
#define bpf_get_smp_processor_id __builtin_bpf_helper_get_smp_processor_id
#define bpf_skb_store_bytes	__builtin_bpf_helper_skb_store_bytes
#define bpf_l3_csum_replace	__builtin_bpf_helper_l3_csum_replace
#define bpf_l4_csum_replace	__builtin_bpf_helper_l4_csum_replace

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,2,0)

#define bpf_tail_call		__builtin_bpf_helper_tail_call
#define bpf_clone_redirect	__builtin_bpf_helper_clone_redirect
#define bpf_get_current_pid_tgid __builtin_bpf_helper_get_current_pid_tgid
#define bpf_get_current_uid_gid  __builtin_bpf_helper_get_current_uid_gid
#define bpf_get_current_comm	__builtin_bpf_helper_get_current_comm

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,3,0)

#define bpf_get_cgroup_classid	__builtin_bpf_helper_get_cgroup_classid
#define bpf_skb_vlan_push	__builtin_bpf_helper_skb_vlan_push
#define bpf_skb_vlan_pop	__builtin_bpf_helper_skb_vlan_pop
#define bpf_skb_get_tunnel_key	__builtin_bpf_helper_skb_get_tunnel_key
#define bpf_skb_set_tunnel_key	__builtin_bpf_helper_skb_set_tunnel_key
#define bpf_perf_event_read	__builtin_bpf_helper_perf_event_read

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,4,0)

#define bpf_redirect		__builtin_bpf_helper_redirect
#define bpf_get_route_realm	__builtin_bpf_helper_get_route_realm
#define bpf_perf_event_output	__builtin_bpf_helper_perf_event_output

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,5,0)

#define bpf_skb_load_bytes	__builtin_bpf_helper_skb_load_bytes

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,6,0)

#define bpf_get_stackid		__builtin_bpf_helper_get_stackid
#define bpf_csum_diff		__builtin_bpf_helper_csum_diff
#define bpf_skb_get_tunnel_opt	__builtin_bpf_helper_skb_get_tunnel_opt
#define bpf_skb_set_tunnel_opt	__builtin_bpf_helper_skb_set_tunnel_opt

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,8,0)

#define bpf_skb_change_proto	__builtin_bpf_helper_skb_change_proto
#define bpf_skb_change_type	__builtin_bpf_helper_skb_change_type
#define bpf_skb_under_cgroup	__builtin_bpf_helper_skb_under_cgroup
#define bpf_get_hash_recalc	__builtin_bpf_helper_get_hash_recalc
#define bpf_get_current_task	__builtin_bpf_helper_get_current_task
#define bpf_probe_write_user	__builtin_bpf_helper_probe_write_user

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,9,0)

#define bpf_current_task_under_cgroup __builtin_bpf_helper_current_task_under_cgroup
#define bpf_skb_change_tail	__builtin_bpf_helper_skb_change_tail
#define bpf_skb_pull_data	__builtin_bpf_helper_skb_pull_data
#define bpf_csum_update		__builtin_bpf_helper_csum_update
#define bpf_set_hash_invalid	__builtin_bpf_helper_set_hash_invalid

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,10,0)

#define bpf_get_numa_node_id	__builtin_bpf_helper_get_numa_node_id
#define bpf_skb_change_head	__builtin_bpf_helper_skb_change_head
#define bpf_xdp_adjust_head	__builtin_bpf_helper_xdp_adjust_head

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,11,0)

#define bpf_probe_read_str	__builtin_bpf_helper_probe_read_str

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,12,0)

#define bpf_get_socket_cookie	__builtin_bpf_helper_get_socket_cookie
#define bpf_get_socket_uid	__builtin_bpf_helper_get_socket_uid

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,13,0)

#define bpf_set_hash		__builtin_bpf_helper_set_hash
#define bpf_setsockopt		__builtin_bpf_helper_setsockopt
#define bpf_skb_adjust_room	__builtin_bpf_helper_skb_adjust_room

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,14,0)

#define bpf_redirect_map	__builtin_bpf_helper_redirect_map
#define bpf_sk_redirect_map	__builtin_bpf_helper_sk_redirect_map
#define bpf_sock_map_update	__builtin_bpf_helper_sock_map_update

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,15,0)

#define bpf_perf_event_read_value __builtin_bpf_helper_perf_event_read_value
#define bpf_perf_prog_read_value  __builtin_bpf_helper_perf_prog_read_value
#define bpf_getsockopt		  __builtin_bpf_helper_getsockopt
#define bpf_xdp_adjust_meta	__builtin_bpf_helper_xdp_adjust_meta

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,16,0)

#define bpf_override_return	__builtin_bpf_helper_override_return
#define bpf_sock_ops_cb_flags_set __builtin_bpf_helper_sock_ops_cb_flags_set

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,17,0)

#define bpf_msg_redirect_map	__builtin_bpf_helper_msg_redirect_map
#define bpf_msg_apply_bytes	__builtin_bpf_helper_msg_apply_bytes
#define bpf_msg_cork_bytes	__builtin_bpf_helper_msg_cork_bytes
#define bpf_pull_data		__builtin_bpf_helper_pull_data
#define bpf_bind		__builtin_bpf_helper_bpf_bind

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,18,0)

#define bpf_xdp_adjust_tail	__builtin_bpf_helper_xdp_adjust_tail
#define bpf_skb_get_xfrm_state	__builtin_bpf_helper_skb_get_xfrm_state
#define bpf_get_stack		__builtin_bpf_helper_get_stack
#define bpf_skb_load_bytes_relative __builtin_bpf_helper_skb_load_bytes_relative
#define bpf_sock_hash_update	__builtin_bpf_helper_sock_hash_update
#define bpf_msg_redirect_hash	__builtin_bpf_helper_msg_redirect_hash
#define bpf_sk_redirect_hash	__builtin_bpf_helper_sk_redirect_hash
#define bpf_lwt_push_encap		__builtin_bpf_helper_lwt_push_encap
#define bpf_lwt_seg6_store_bytes	__builtin_bpf_helper_lwt_seg6_store_bytes
#define bpf_lwt_seg6_adjust_srh		__builtin_bpf_helper_lwt_seg6_adjust_srh
#define bpf_lwt_seg6_action		__builtin_bpf_helper_lwt_seg6_action
#define bpf_rc_repeat			__builtin_bpf_helper_rc_repeat
#define bpf_rc_keydown			__builtin_bpf_helper_rc_keydown
#define bpf_skb_cgroup_id		__builtin_bpf_helper_skb_cgroup_id
#define bpf_get_current_cgroup_id	__builtin_bpf_helper_get_current_cgroup_id

static inline int
bpf_fib_lookup (void *ctx, struct bpf_fib_lookup *param, int plen,
		unsigned int flags)
{
  return __builtin_bpf_helper_fib_lookup (ctx, (void *) param, plen, flags);
}


#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,19,0)

#define bpf_get_local_storage	__builtin_bpf_helper_get_local_storage
#define bpf_sk_select_reuseport	__builtin_bpf_helper_sk_select_reuseport
#define bpf_skb_ancestor_cgroup_id	__builtin_bpf_helper_skb_ancestor_cgroup_id

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (4,20,0)

#define bpf_sk_release		__builtin_bpf_helper_sk_release
#define bpf_map_push_elem	__builtin_bpf_helper_map_push_elem
#define bpf_map_pop_elem	__builtin_bpf_helper_map_pop_elem
#define bpf_map_peek_elem	__builtin_bpf_helper_map_peek_elem
#define bpf_msg_push_data	__builtin_bpf_helper_msg_push_data

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)
{
  return
    (struct bpf_sock *) __builtin_bpf_helper_sk_lookup_tcp (ctx,
							    (void *) tuple,
							    size,
							    netns_id, flags);
}

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)
{
  return
    (struct bpf_sock *) __builtin_bpf_helper_sk_lookup_udp (ctx,
							    (void *) tuple,
							    size,
							    netns_id, flags);
}

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (5,0,0)

#define bpf_msg_pop_data	__builtin_bpf_helper_pop_data
#define bpf_rc_pointer_rel	__builtin_bpf_helper_rc_pointer_rel

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (5,1,0)

#define bpf_spin_lock		__builtin_bpf_helper_spin_lock
#define bpf_spin_unlock		__builtin_bpf_helper_spin_unlock
#define bpf_skb_ecn_set_ce	__builtin_bpf_helper_skb_ecn_set_ce

static inline struct bpf_sock *
bpf_sk_fullsock (struct bpf_sock *sk)
{
  return
    (struct bpf_sock *) __builtin_bpf_helper_sk_fullsock ((void *) sk);
}

static inline struct bpf_sock *
bpf_tcp_sock (struct bpf_sock *sk)
{
  return
    (struct bpf_sock *) __builtin_bpf_helper_tcp_sock ((void *) sk);
}

static inline struct bpf_sock *
bpf_get_listener_sock (struct bpf_sock *sk)
{
  return
    (struct bpf_sock *) __builtin_bpf_helper_get_listener_sock ((void *) sk);
}

#if __BPF_KERNEL_VERSION_CODE__ >= KERNEL_VERSION (5,2,0)


#endif /* 5.2 */
#endif /* 5.1 */
#endif /* 5.0 */
#endif /* 4.20 */
#endif /* 4.19 */
#endif /* 4.18 */
#endif /* 4.17 */
#endif /* 4.16 */
#endif /* 4.15 */
#endif /* 4.14 */
#endif /* 4.13 */
#endif /* 4.12 */
#endif /* 4.11 */
#endif /* 4.10 */
#endif /* 4.9 */
#endif /* 4.8 */
#endif /* 4.6 */
#endif /* 4.5 */
#endif /* 4.4 */
#endif /* 4.3 */
#endif /* 4.2 */
#endif /* 4.1 */
#endif /* 4.0 */

/* Functions to emit BPF_LD_ABS and BPF_LD_IND instructions.  We
   provide the "standard" names as synonyms of the corresponding GCC
   builtins.  Note how the SKB argument is ignored.  */

static inline long long
load_byte (void *skb __attribute__ ((unused)),
	   unsigned long long off)
{
  return __builtin_bpf_load_byte (off);
}

static inline long long
load_half (void *skb __attribute__ ((unused)),
	   unsigned long long off)
{
  return __builtin_bpf_load_half (off);
}

static inline long long
load_word (void *skb __attribute__ ((unused)),
	   unsigned long long off)
{
  return __builtin_bpf_load_word (off);
}

struct bpf_map_def
{
  unsigned int type;
  unsigned int key_size;
  unsigned int value_size;
  unsigned int max_entries;
  unsigned int map_flags;
  unsigned int inner_map_idx;
  unsigned int numa_node;
};

#endif /* ! __BPF_HELPERS_H */