]>
Commit | Line | Data |
---|---|---|
daedfb22 AS |
1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
2 | * | |
3 | * This program is free software; you can redistribute it and/or | |
4 | * modify it under the terms of version 2 of the GNU General Public | |
5 | * License as published by the Free Software Foundation. | |
6 | */ | |
7 | #ifndef _UAPI__LINUX_BPF_H__ | |
8 | #define _UAPI__LINUX_BPF_H__ | |
9 | ||
10 | #include <linux/types.h> | |
c15952dc | 11 | #include <linux/bpf_common.h> |
daedfb22 AS |
12 | |
13 | /* Extended instruction set based on top of classic BPF */ | |
14 | ||
15 | /* instruction classes */ | |
16 | #define BPF_ALU64 0x07 /* alu mode in double word width */ | |
17 | ||
18 | /* ld/ldx fields */ | |
19 | #define BPF_DW 0x18 /* double word */ | |
20 | #define BPF_XADD 0xc0 /* exclusive add */ | |
21 | ||
22 | /* alu/jmp fields */ | |
23 | #define BPF_MOV 0xb0 /* mov reg to reg */ | |
24 | #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */ | |
25 | ||
26 | /* change endianness of a register */ | |
27 | #define BPF_END 0xd0 /* flags for endianness conversion: */ | |
28 | #define BPF_TO_LE 0x00 /* convert to little-endian */ | |
29 | #define BPF_TO_BE 0x08 /* convert to big-endian */ | |
30 | #define BPF_FROM_LE BPF_TO_LE | |
31 | #define BPF_FROM_BE BPF_TO_BE | |
32 | ||
33 | #define BPF_JNE 0x50 /* jump != */ | |
34 | #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */ | |
35 | #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */ | |
36 | #define BPF_CALL 0x80 /* function call */ | |
37 | #define BPF_EXIT 0x90 /* function return */ | |
38 | ||
39 | /* Register numbers */ | |
40 | enum { | |
41 | BPF_REG_0 = 0, | |
42 | BPF_REG_1, | |
43 | BPF_REG_2, | |
44 | BPF_REG_3, | |
45 | BPF_REG_4, | |
46 | BPF_REG_5, | |
47 | BPF_REG_6, | |
48 | BPF_REG_7, | |
49 | BPF_REG_8, | |
50 | BPF_REG_9, | |
51 | BPF_REG_10, | |
52 | __MAX_BPF_REG, | |
53 | }; | |
54 | ||
55 | /* BPF has 10 general purpose 64-bit registers and stack frame. */ | |
56 | #define MAX_BPF_REG __MAX_BPF_REG | |
57 | ||
58 | struct bpf_insn { | |
59 | __u8 code; /* opcode */ | |
60 | __u8 dst_reg:4; /* dest register */ | |
61 | __u8 src_reg:4; /* source register */ | |
62 | __s16 off; /* signed offset */ | |
63 | __s32 imm; /* signed immediate constant */ | |
64 | }; | |
65 | ||
b2197755 | 66 | /* BPF syscall commands, see bpf(2) man-page for details. */ |
99c55f7d | 67 | enum bpf_cmd { |
99c55f7d | 68 | BPF_MAP_CREATE, |
db20fd2b | 69 | BPF_MAP_LOOKUP_ELEM, |
db20fd2b | 70 | BPF_MAP_UPDATE_ELEM, |
db20fd2b | 71 | BPF_MAP_DELETE_ELEM, |
db20fd2b | 72 | BPF_MAP_GET_NEXT_KEY, |
09756af4 | 73 | BPF_PROG_LOAD, |
b2197755 DB |
74 | BPF_OBJ_PIN, |
75 | BPF_OBJ_GET, | |
99c55f7d AS |
76 | }; |
77 | ||
78 | enum bpf_map_type { | |
79 | BPF_MAP_TYPE_UNSPEC, | |
0f8e4bd8 | 80 | BPF_MAP_TYPE_HASH, |
28fbcfa0 | 81 | BPF_MAP_TYPE_ARRAY, |
04fd61ab | 82 | BPF_MAP_TYPE_PROG_ARRAY, |
ea317b26 | 83 | BPF_MAP_TYPE_PERF_EVENT_ARRAY, |
824bd0ce | 84 | BPF_MAP_TYPE_PERCPU_HASH, |
a10423b8 | 85 | BPF_MAP_TYPE_PERCPU_ARRAY, |
d5a3b1f6 | 86 | BPF_MAP_TYPE_STACK_TRACE, |
4ed8ec52 | 87 | BPF_MAP_TYPE_CGROUP_ARRAY, |
99c55f7d AS |
88 | }; |
89 | ||
09756af4 AS |
90 | enum bpf_prog_type { |
91 | BPF_PROG_TYPE_UNSPEC, | |
ddd872bc | 92 | BPF_PROG_TYPE_SOCKET_FILTER, |
2541517c | 93 | BPF_PROG_TYPE_KPROBE, |
96be4325 | 94 | BPF_PROG_TYPE_SCHED_CLS, |
94caee8c | 95 | BPF_PROG_TYPE_SCHED_ACT, |
98b5c2c6 | 96 | BPF_PROG_TYPE_TRACEPOINT, |
09756af4 AS |
97 | }; |
98 | ||
f1a66f85 DB |
99 | #define BPF_PSEUDO_MAP_FD 1 |
100 | ||
3274f520 AS |
101 | /* flags for BPF_MAP_UPDATE_ELEM command */ |
102 | #define BPF_ANY 0 /* create new element or update existing */ | |
103 | #define BPF_NOEXIST 1 /* create new element if it didn't exist */ | |
104 | #define BPF_EXIST 2 /* update existing element */ | |
105 | ||
6c905981 AS |
106 | #define BPF_F_NO_PREALLOC (1U << 0) |
107 | ||
99c55f7d AS |
108 | union bpf_attr { |
109 | struct { /* anonymous struct used by BPF_MAP_CREATE command */ | |
110 | __u32 map_type; /* one of enum bpf_map_type */ | |
111 | __u32 key_size; /* size of key in bytes */ | |
112 | __u32 value_size; /* size of value in bytes */ | |
113 | __u32 max_entries; /* max number of entries in a map */ | |
6c905981 | 114 | __u32 map_flags; /* prealloc or not */ |
99c55f7d | 115 | }; |
db20fd2b AS |
116 | |
117 | struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ | |
118 | __u32 map_fd; | |
119 | __aligned_u64 key; | |
120 | union { | |
121 | __aligned_u64 value; | |
122 | __aligned_u64 next_key; | |
123 | }; | |
3274f520 | 124 | __u64 flags; |
db20fd2b | 125 | }; |
09756af4 AS |
126 | |
127 | struct { /* anonymous struct used by BPF_PROG_LOAD command */ | |
128 | __u32 prog_type; /* one of enum bpf_prog_type */ | |
129 | __u32 insn_cnt; | |
130 | __aligned_u64 insns; | |
131 | __aligned_u64 license; | |
cbd35700 AS |
132 | __u32 log_level; /* verbosity level of verifier */ |
133 | __u32 log_size; /* size of user buffer */ | |
134 | __aligned_u64 log_buf; /* user supplied buffer */ | |
2541517c | 135 | __u32 kern_version; /* checked when prog_type=kprobe */ |
09756af4 | 136 | }; |
b2197755 DB |
137 | |
138 | struct { /* anonymous struct used by BPF_OBJ_* commands */ | |
139 | __aligned_u64 pathname; | |
140 | __u32 bpf_fd; | |
141 | }; | |
99c55f7d AS |
142 | } __attribute__((aligned(8))); |
143 | ||
09756af4 AS |
144 | /* integer value in 'imm' field of BPF_CALL instruction selects which helper |
145 | * function eBPF program intends to call | |
146 | */ | |
147 | enum bpf_func_id { | |
148 | BPF_FUNC_unspec, | |
d0003ec0 AS |
149 | BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */ |
150 | BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */ | |
151 | BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */ | |
2541517c | 152 | BPF_FUNC_probe_read, /* int bpf_probe_read(void *dst, int size, void *src) */ |
d9847d31 | 153 | BPF_FUNC_ktime_get_ns, /* u64 bpf_ktime_get_ns(void) */ |
9c959c86 | 154 | BPF_FUNC_trace_printk, /* int bpf_trace_printk(const char *fmt, int fmt_size, ...) */ |
03e69b50 | 155 | BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */ |
c04167ce | 156 | BPF_FUNC_get_smp_processor_id, /* u32 raw_smp_processor_id(void) */ |
91bc4822 AS |
157 | |
158 | /** | |
159 | * skb_store_bytes(skb, offset, from, len, flags) - store bytes into packet | |
160 | * @skb: pointer to skb | |
a166151c | 161 | * @offset: offset within packet from skb->mac_header |
91bc4822 AS |
162 | * @from: pointer where to copy bytes from |
163 | * @len: number of bytes to store into packet | |
164 | * @flags: bit 0 - if true, recompute skb->csum | |
165 | * other bits - reserved | |
166 | * Return: 0 on success | |
167 | */ | |
168 | BPF_FUNC_skb_store_bytes, | |
169 | ||
170 | /** | |
171 | * l3_csum_replace(skb, offset, from, to, flags) - recompute IP checksum | |
172 | * @skb: pointer to skb | |
173 | * @offset: offset within packet where IP checksum is located | |
174 | * @from: old value of header field | |
175 | * @to: new value of header field | |
176 | * @flags: bits 0-3 - size of header field | |
177 | * other bits - reserved | |
178 | * Return: 0 on success | |
179 | */ | |
180 | BPF_FUNC_l3_csum_replace, | |
181 | ||
182 | /** | |
183 | * l4_csum_replace(skb, offset, from, to, flags) - recompute TCP/UDP checksum | |
184 | * @skb: pointer to skb | |
185 | * @offset: offset within packet where TCP/UDP checksum is located | |
186 | * @from: old value of header field | |
187 | * @to: new value of header field | |
188 | * @flags: bits 0-3 - size of header field | |
189 | * bit 4 - is pseudo header | |
190 | * other bits - reserved | |
191 | * Return: 0 on success | |
192 | */ | |
193 | BPF_FUNC_l4_csum_replace, | |
04fd61ab AS |
194 | |
195 | /** | |
196 | * bpf_tail_call(ctx, prog_array_map, index) - jump into another BPF program | |
197 | * @ctx: context pointer passed to next program | |
198 | * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY | |
199 | * @index: index inside array that selects specific program to run | |
200 | * Return: 0 on success | |
201 | */ | |
202 | BPF_FUNC_tail_call, | |
3896d655 AS |
203 | |
204 | /** | |
205 | * bpf_clone_redirect(skb, ifindex, flags) - redirect to another netdev | |
206 | * @skb: pointer to skb | |
207 | * @ifindex: ifindex of the net device | |
208 | * @flags: bit 0 - if set, redirect to ingress instead of egress | |
209 | * other bits - reserved | |
210 | * Return: 0 on success | |
211 | */ | |
212 | BPF_FUNC_clone_redirect, | |
ffeedafb AS |
213 | |
214 | /** | |
215 | * u64 bpf_get_current_pid_tgid(void) | |
216 | * Return: current->tgid << 32 | current->pid | |
217 | */ | |
218 | BPF_FUNC_get_current_pid_tgid, | |
219 | ||
220 | /** | |
221 | * u64 bpf_get_current_uid_gid(void) | |
222 | * Return: current_gid << 32 | current_uid | |
223 | */ | |
224 | BPF_FUNC_get_current_uid_gid, | |
225 | ||
226 | /** | |
227 | * bpf_get_current_comm(char *buf, int size_of_buf) | |
228 | * stores current->comm into buf | |
229 | * Return: 0 on success | |
230 | */ | |
231 | BPF_FUNC_get_current_comm, | |
8d20aabe DB |
232 | |
233 | /** | |
234 | * bpf_get_cgroup_classid(skb) - retrieve a proc's classid | |
235 | * @skb: pointer to skb | |
236 | * Return: classid if != 0 | |
237 | */ | |
238 | BPF_FUNC_get_cgroup_classid, | |
4e10df9a AS |
239 | BPF_FUNC_skb_vlan_push, /* bpf_skb_vlan_push(skb, vlan_proto, vlan_tci) */ |
240 | BPF_FUNC_skb_vlan_pop, /* bpf_skb_vlan_pop(skb) */ | |
d3aa45ce AS |
241 | |
242 | /** | |
243 | * bpf_skb_[gs]et_tunnel_key(skb, key, size, flags) | |
244 | * retrieve or populate tunnel metadata | |
245 | * @skb: pointer to skb | |
246 | * @key: pointer to 'struct bpf_tunnel_key' | |
247 | * @size: size of 'struct bpf_tunnel_key' | |
248 | * @flags: room for future extensions | |
249 | * Retrun: 0 on success | |
250 | */ | |
251 | BPF_FUNC_skb_get_tunnel_key, | |
252 | BPF_FUNC_skb_set_tunnel_key, | |
35578d79 | 253 | BPF_FUNC_perf_event_read, /* u64 bpf_perf_event_read(&map, index) */ |
27b29f63 AS |
254 | /** |
255 | * bpf_redirect(ifindex, flags) - redirect to another netdev | |
256 | * @ifindex: ifindex of the net device | |
257 | * @flags: bit 0 - if set, redirect to ingress instead of egress | |
258 | * other bits - reserved | |
259 | * Return: TC_ACT_REDIRECT | |
260 | */ | |
261 | BPF_FUNC_redirect, | |
c46646d0 DB |
262 | |
263 | /** | |
264 | * bpf_get_route_realm(skb) - retrieve a dst's tclassid | |
265 | * @skb: pointer to skb | |
266 | * Return: realm if != 0 | |
267 | */ | |
268 | BPF_FUNC_get_route_realm, | |
a43eec30 AS |
269 | |
270 | /** | |
271 | * bpf_perf_event_output(ctx, map, index, data, size) - output perf raw sample | |
272 | * @ctx: struct pt_regs* | |
273 | * @map: pointer to perf_event_array map | |
274 | * @index: index of event in the map | |
275 | * @data: data on stack to be output as raw data | |
276 | * @size: size of data | |
277 | * Return: 0 on success | |
278 | */ | |
279 | BPF_FUNC_perf_event_output, | |
05c74e5e | 280 | BPF_FUNC_skb_load_bytes, |
d5a3b1f6 AS |
281 | |
282 | /** | |
283 | * bpf_get_stackid(ctx, map, flags) - walk user or kernel stack and return id | |
284 | * @ctx: struct pt_regs* | |
285 | * @map: pointer to stack_trace map | |
286 | * @flags: bits 0-7 - numer of stack frames to skip | |
287 | * bit 8 - collect user stack instead of kernel | |
288 | * bit 9 - compare stacks by hash only | |
289 | * bit 10 - if two different stacks hash into the same stackid | |
290 | * discard old | |
291 | * other bits - reserved | |
292 | * Return: >= 0 stackid on success or negative error | |
293 | */ | |
294 | BPF_FUNC_get_stackid, | |
7d672345 DB |
295 | |
296 | /** | |
297 | * bpf_csum_diff(from, from_size, to, to_size, seed) - calculate csum diff | |
298 | * @from: raw from buffer | |
299 | * @from_size: length of from buffer | |
300 | * @to: raw to buffer | |
301 | * @to_size: length of to buffer | |
302 | * @seed: optional seed | |
303 | * Return: csum result | |
304 | */ | |
305 | BPF_FUNC_csum_diff, | |
14ca0751 DB |
306 | |
307 | /** | |
308 | * bpf_skb_[gs]et_tunnel_opt(skb, opt, size) | |
309 | * retrieve or populate tunnel options metadata | |
310 | * @skb: pointer to skb | |
311 | * @opt: pointer to raw tunnel option data | |
312 | * @size: size of @opt | |
313 | * Return: 0 on success for set, option size for get | |
314 | */ | |
315 | BPF_FUNC_skb_get_tunnel_opt, | |
316 | BPF_FUNC_skb_set_tunnel_opt, | |
6578171a DB |
317 | |
318 | /** | |
319 | * bpf_skb_change_proto(skb, proto, flags) | |
320 | * Change protocol of the skb. Currently supported is | |
321 | * v4 -> v6, v6 -> v4 transitions. The helper will also | |
322 | * resize the skb. eBPF program is expected to fill the | |
323 | * new headers via skb_store_bytes and lX_csum_replace. | |
324 | * @skb: pointer to skb | |
325 | * @proto: new skb->protocol type | |
326 | * @flags: reserved | |
327 | * Return: 0 on success or negative error | |
328 | */ | |
329 | BPF_FUNC_skb_change_proto, | |
330 | ||
d2485c42 DB |
331 | /** |
332 | * bpf_skb_change_type(skb, type) | |
333 | * Change packet type of skb. | |
334 | * @skb: pointer to skb | |
335 | * @type: new skb->pkt_type type | |
336 | * Return: 0 on success or negative error | |
337 | */ | |
338 | BPF_FUNC_skb_change_type, | |
339 | ||
4a482f34 MKL |
340 | /** |
341 | * bpf_skb_in_cgroup(skb, map, index) - Check cgroup2 membership of skb | |
342 | * @skb: pointer to skb | |
343 | * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type | |
344 | * @index: index of the cgroup in the bpf_map | |
345 | * Return: | |
346 | * == 0 skb failed the cgroup2 descendant test | |
347 | * == 1 skb succeeded the cgroup2 descendant test | |
348 | * < 0 error | |
349 | */ | |
350 | BPF_FUNC_skb_in_cgroup, | |
13c5c240 DB |
351 | |
352 | /** | |
353 | * bpf_get_hash_recalc(skb) | |
354 | * Retrieve and possibly recalculate skb->hash. | |
355 | * @skb: pointer to skb | |
356 | * Return: hash | |
357 | */ | |
358 | BPF_FUNC_get_hash_recalc, | |
359 | ||
606274c5 AS |
360 | /** |
361 | * u64 bpf_get_current_task(void) | |
362 | * Returns current task_struct | |
363 | * Return: current | |
364 | */ | |
365 | BPF_FUNC_get_current_task, | |
366 | ||
09756af4 AS |
367 | __BPF_FUNC_MAX_ID, |
368 | }; | |
369 | ||
781c53bc DB |
370 | /* All flags used by eBPF helper functions, placed here. */ |
371 | ||
372 | /* BPF_FUNC_skb_store_bytes flags. */ | |
373 | #define BPF_F_RECOMPUTE_CSUM (1ULL << 0) | |
8afd54c8 | 374 | #define BPF_F_INVALIDATE_HASH (1ULL << 1) |
781c53bc DB |
375 | |
376 | /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags. | |
377 | * First 4 bits are for passing the header field size. | |
378 | */ | |
379 | #define BPF_F_HDR_FIELD_MASK 0xfULL | |
380 | ||
381 | /* BPF_FUNC_l4_csum_replace flags. */ | |
382 | #define BPF_F_PSEUDO_HDR (1ULL << 4) | |
2f72959a | 383 | #define BPF_F_MARK_MANGLED_0 (1ULL << 5) |
781c53bc DB |
384 | |
385 | /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */ | |
386 | #define BPF_F_INGRESS (1ULL << 0) | |
387 | ||
c6c33454 DB |
388 | /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */ |
389 | #define BPF_F_TUNINFO_IPV6 (1ULL << 0) | |
390 | ||
d5a3b1f6 AS |
391 | /* BPF_FUNC_get_stackid flags. */ |
392 | #define BPF_F_SKIP_FIELD_MASK 0xffULL | |
393 | #define BPF_F_USER_STACK (1ULL << 8) | |
394 | #define BPF_F_FAST_STACK_CMP (1ULL << 9) | |
395 | #define BPF_F_REUSE_STACKID (1ULL << 10) | |
396 | ||
2da897e5 DB |
397 | /* BPF_FUNC_skb_set_tunnel_key flags. */ |
398 | #define BPF_F_ZERO_CSUM_TX (1ULL << 1) | |
22080870 | 399 | #define BPF_F_DONT_FRAGMENT (1ULL << 2) |
2da897e5 | 400 | |
6816a7ff | 401 | /* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */ |
1e33759c DB |
402 | #define BPF_F_INDEX_MASK 0xffffffffULL |
403 | #define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK | |
555c8a86 DB |
404 | /* BPF_FUNC_perf_event_output for sk_buff input context. */ |
405 | #define BPF_F_CTXLEN_MASK (0xfffffULL << 32) | |
1e33759c | 406 | |
9bac3d6d AS |
407 | /* user accessible mirror of in-kernel sk_buff. |
408 | * new fields can only be added to the end of this structure | |
409 | */ | |
410 | struct __sk_buff { | |
411 | __u32 len; | |
412 | __u32 pkt_type; | |
413 | __u32 mark; | |
414 | __u32 queue_mapping; | |
c2497395 AS |
415 | __u32 protocol; |
416 | __u32 vlan_present; | |
417 | __u32 vlan_tci; | |
27cd5452 | 418 | __u32 vlan_proto; |
bcad5718 | 419 | __u32 priority; |
37e82c2f AS |
420 | __u32 ingress_ifindex; |
421 | __u32 ifindex; | |
d691f9e8 AS |
422 | __u32 tc_index; |
423 | __u32 cb[5]; | |
ba7591d8 | 424 | __u32 hash; |
045efa82 | 425 | __u32 tc_classid; |
969bf05e AS |
426 | __u32 data; |
427 | __u32 data_end; | |
9bac3d6d AS |
428 | }; |
429 | ||
d3aa45ce AS |
430 | struct bpf_tunnel_key { |
431 | __u32 tunnel_id; | |
c6c33454 DB |
432 | union { |
433 | __u32 remote_ipv4; | |
434 | __u32 remote_ipv6[4]; | |
435 | }; | |
436 | __u8 tunnel_tos; | |
437 | __u8 tunnel_ttl; | |
c0e760c9 | 438 | __u16 tunnel_ext; |
4018ab18 | 439 | __u32 tunnel_label; |
d3aa45ce AS |
440 | }; |
441 | ||
daedfb22 | 442 | #endif /* _UAPI__LINUX_BPF_H__ */ |