1 /* eBPF mini library */
9 int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
10 int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
12 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
14 #define BPF_ALU64_REG(OP, DST, SRC) \
15 ((struct bpf_insn) { \
16 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
22 #define BPF_ALU32_REG(OP, DST, SRC) \
23 ((struct bpf_insn) { \
24 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
30 /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
32 #define BPF_ALU64_IMM(OP, DST, IMM) \
33 ((struct bpf_insn) { \
34 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
40 #define BPF_ALU32_IMM(OP, DST, IMM) \
41 ((struct bpf_insn) { \
42 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
48 /* Short form of mov, dst_reg = src_reg */
50 #define BPF_MOV64_REG(DST, SRC) \
51 ((struct bpf_insn) { \
52 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
58 #define BPF_MOV32_REG(DST, SRC) \
59 ((struct bpf_insn) { \
60 .code = BPF_ALU | BPF_MOV | BPF_X, \
66 /* Short form of mov, dst_reg = imm32 */
68 #define BPF_MOV64_IMM(DST, IMM) \
69 ((struct bpf_insn) { \
70 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
76 #define BPF_MOV32_IMM(DST, IMM) \
77 ((struct bpf_insn) { \
78 .code = BPF_ALU | BPF_MOV | BPF_K, \
84 /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
85 #define BPF_LD_IMM64(DST, IMM) \
86 BPF_LD_IMM64_RAW(DST, 0, IMM)
88 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
89 ((struct bpf_insn) { \
90 .code = BPF_LD | BPF_DW | BPF_IMM, \
94 .imm = (__u32) (IMM) }), \
95 ((struct bpf_insn) { \
96 .code = 0, /* zero is reserved opcode */ \
100 .imm = ((__u64) (IMM)) >> 32 })
102 #ifndef BPF_PSEUDO_MAP_FD
103 # define BPF_PSEUDO_MAP_FD 1
106 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
107 #define BPF_LD_MAP_FD(DST, MAP_FD) \
108 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
111 /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
113 #define BPF_LD_ABS(SIZE, IMM) \
114 ((struct bpf_insn) { \
115 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
121 /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
123 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
124 ((struct bpf_insn) { \
125 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
131 /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
133 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
134 ((struct bpf_insn) { \
135 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
141 /* Memory store, *(uint *) (dst_reg + off16) = imm32 */
143 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
144 ((struct bpf_insn) { \
145 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
151 /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
153 #define BPF_JMP_REG(OP, DST, SRC, OFF) \
154 ((struct bpf_insn) { \
155 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
161 /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
163 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
164 ((struct bpf_insn) { \
165 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
171 /* Raw code statement block */
173 #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
174 ((struct bpf_insn) { \
183 #define BPF_EXIT_INSN() \
184 ((struct bpf_insn) { \
185 .code = BPF_JMP | BPF_EXIT, \
191 /* create RAW socket and bind to interface 'name' */
192 int open_raw_sock(const char *name);
194 struct perf_event_attr;
195 int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
196 int group_fd, unsigned long flags);