2 * Linux Socket Filter Data Structures
4 #ifndef __TOOLS_LINUX_FILTER_H
5 #define __TOOLS_LINUX_FILTER_H
9 /* ArgX, context and stack frame pointer register positions. Note,
10 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
11 * calls in BPF_CALL instruction.
13 #define BPF_REG_ARG1 BPF_REG_1
14 #define BPF_REG_ARG2 BPF_REG_2
15 #define BPF_REG_ARG3 BPF_REG_3
16 #define BPF_REG_ARG4 BPF_REG_4
17 #define BPF_REG_ARG5 BPF_REG_5
18 #define BPF_REG_CTX BPF_REG_6
19 #define BPF_REG_FP BPF_REG_10
21 /* Additional register mappings for converted user programs. */
22 #define BPF_REG_A BPF_REG_0
23 #define BPF_REG_X BPF_REG_7
24 #define BPF_REG_TMP BPF_REG_8
26 /* BPF program can access up to 512 bytes of stack space. */
27 #define MAX_BPF_STACK 512
29 /* Helper macros for filter block array initializers. */
31 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
33 #define BPF_ALU64_REG(OP, DST, SRC) \
34 ((struct bpf_insn) { \
35 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
41 #define BPF_ALU32_REG(OP, DST, SRC) \
42 ((struct bpf_insn) { \
43 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
49 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
51 #define BPF_ALU64_IMM(OP, DST, IMM) \
52 ((struct bpf_insn) { \
53 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
59 #define BPF_ALU32_IMM(OP, DST, IMM) \
60 ((struct bpf_insn) { \
61 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
67 /* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */
69 #define BPF_ENDIAN(TYPE, DST, LEN) \
70 ((struct bpf_insn) { \
71 .code = BPF_ALU | BPF_END | BPF_SRC(TYPE), \
77 /* Short form of mov, dst_reg = src_reg */
79 #define BPF_MOV64_REG(DST, SRC) \
80 ((struct bpf_insn) { \
81 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
87 #define BPF_MOV32_REG(DST, SRC) \
88 ((struct bpf_insn) { \
89 .code = BPF_ALU | BPF_MOV | BPF_X, \
95 /* Short form of mov, dst_reg = imm32 */
97 #define BPF_MOV64_IMM(DST, IMM) \
98 ((struct bpf_insn) { \
99 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
105 #define BPF_MOV32_IMM(DST, IMM) \
106 ((struct bpf_insn) { \
107 .code = BPF_ALU | BPF_MOV | BPF_K, \
113 /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
115 #define BPF_MOV64_RAW(TYPE, DST, SRC, IMM) \
116 ((struct bpf_insn) { \
117 .code = BPF_ALU64 | BPF_MOV | BPF_SRC(TYPE), \
123 #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
124 ((struct bpf_insn) { \
125 .code = BPF_ALU | BPF_MOV | BPF_SRC(TYPE), \
131 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
133 #define BPF_LD_ABS(SIZE, IMM) \
134 ((struct bpf_insn) { \
135 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
141 /* Indirect packet access, R0 = *(uint *) (skb->data + src_reg + imm32) */
143 #define BPF_LD_IND(SIZE, SRC, IMM) \
144 ((struct bpf_insn) { \
145 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_IND, \
151 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
153 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
154 ((struct bpf_insn) { \
155 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
161 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
163 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
164 ((struct bpf_insn) { \
165 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
171 /* Atomic memory add, *(uint *)(dst_reg + off16) += src_reg */
173 #define BPF_STX_XADD(SIZE, DST, SRC, OFF) \
174 ((struct bpf_insn) { \
175 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_XADD, \
181 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
183 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
184 ((struct bpf_insn) { \
185 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
191 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
193 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
194 ((struct bpf_insn) { \
195 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
201 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
203 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
204 ((struct bpf_insn) { \
205 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
211 /* Unconditional jumps, goto pc + off16 */
213 #define BPF_JMP_A(OFF) \
214 ((struct bpf_insn) { \
215 .code = BPF_JMP | BPF_JA, \
223 #define BPF_EMIT_CALL(FUNC) \
224 ((struct bpf_insn) { \
225 .code = BPF_JMP | BPF_CALL, \
229 .imm = ((FUNC) - BPF_FUNC_unspec) })
231 /* Raw code statement block */
233 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
234 ((struct bpf_insn) { \
241 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
243 #define BPF_LD_IMM64(DST, IMM) \
244 BPF_LD_IMM64_RAW(DST, 0, IMM)
246 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
247 ((struct bpf_insn) { \
248 .code = BPF_LD | BPF_DW | BPF_IMM, \
252 .imm = (__u32) (IMM) }), \
253 ((struct bpf_insn) { \
254 .code = 0, /* zero is reserved opcode */ \
258 .imm = ((__u64) (IMM)) >> 32 })
260 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
262 #define BPF_LD_MAP_FD(DST, MAP_FD) \
263 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
267 #define BPF_EXIT_INSN() \
268 ((struct bpf_insn) { \
269 .code = BPF_JMP | BPF_EXIT, \
275 #endif /* __TOOLS_LINUX_FILTER_H */