1 /* eBPF mini library */
9 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
11 #define BPF_ALU64_REG(OP, DST, SRC) \
12 ((struct bpf_insn) { \
13 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
19 #define BPF_ALU32_REG(OP, DST, SRC) \
20 ((struct bpf_insn) { \
21 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
27 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
29 #define BPF_ALU64_IMM(OP, DST, IMM) \
30 ((struct bpf_insn) { \
31 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
37 #define BPF_ALU32_IMM(OP, DST, IMM) \
38 ((struct bpf_insn) { \
39 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
45 /* Short form of mov, dst_reg = src_reg */
47 #define BPF_MOV64_REG(DST, SRC) \
48 ((struct bpf_insn) { \
49 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
55 #define BPF_MOV32_REG(DST, SRC) \
56 ((struct bpf_insn) { \
57 .code = BPF_ALU | BPF_MOV | BPF_X, \
63 /* Short form of mov, dst_reg = imm32 */
65 #define BPF_MOV64_IMM(DST, IMM) \
66 ((struct bpf_insn) { \
67 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
73 #define BPF_MOV32_IMM(DST, IMM) \
74 ((struct bpf_insn) { \
75 .code = BPF_ALU | BPF_MOV | BPF_K, \
81 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
82 #define BPF_LD_IMM64(DST, IMM) \
83 BPF_LD_IMM64_RAW(DST, 0, IMM)
85 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
86 ((struct bpf_insn) { \
87 .code = BPF_LD | BPF_DW | BPF_IMM, \
91 .imm = (__u32) (IMM) }), \
92 ((struct bpf_insn) { \
93 .code = 0, /* zero is reserved opcode */ \
97 .imm = ((__u64) (IMM)) >> 32 })
99 #ifndef BPF_PSEUDO_MAP_FD
100 # define BPF_PSEUDO_MAP_FD 1
103 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
104 #define BPF_LD_MAP_FD(DST, MAP_FD) \
105 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
108 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
110 #define BPF_LD_ABS(SIZE, IMM) \
111 ((struct bpf_insn) { \
112 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
118 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
120 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
121 ((struct bpf_insn) { \
122 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
128 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
130 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
131 ((struct bpf_insn) { \
132 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
138 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
140 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
141 ((struct bpf_insn) { \
142 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
148 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
150 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
151 ((struct bpf_insn) { \
152 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
158 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
160 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
161 ((struct bpf_insn) { \
162 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
168 /* Raw code statement block */
170 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
171 ((struct bpf_insn) { \
180 #define BPF_EXIT_INSN() \
181 ((struct bpf_insn) { \
182 .code = BPF_JMP | BPF_EXIT, \