blob: cf7d2386d1f9bf0b9f38eb9b62f3f735c0c2fa42 [file] [log] [blame]
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -07001/* eBPF mini library */
2#ifndef __LIBBPF_H
3#define __LIBBPF_H
4
Joe Stringer43371c82016-12-14 14:43:39 -08005#include <bpf/bpf.h>
6
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -07007struct bpf_insn;
8
Daniel Mackd8c5b172016-11-23 16:52:30 +01009int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
10int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
11
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070012/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
13
14#define BPF_ALU64_REG(OP, DST, SRC) \
15 ((struct bpf_insn) { \
16 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
17 .dst_reg = DST, \
18 .src_reg = SRC, \
19 .off = 0, \
20 .imm = 0 })
21
22#define BPF_ALU32_REG(OP, DST, SRC) \
23 ((struct bpf_insn) { \
24 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
25 .dst_reg = DST, \
26 .src_reg = SRC, \
27 .off = 0, \
28 .imm = 0 })
29
30/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
31
32#define BPF_ALU64_IMM(OP, DST, IMM) \
33 ((struct bpf_insn) { \
34 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
35 .dst_reg = DST, \
36 .src_reg = 0, \
37 .off = 0, \
38 .imm = IMM })
39
40#define BPF_ALU32_IMM(OP, DST, IMM) \
41 ((struct bpf_insn) { \
42 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
43 .dst_reg = DST, \
44 .src_reg = 0, \
45 .off = 0, \
46 .imm = IMM })
47
48/* Short form of mov, dst_reg = src_reg */
49
50#define BPF_MOV64_REG(DST, SRC) \
51 ((struct bpf_insn) { \
52 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
53 .dst_reg = DST, \
54 .src_reg = SRC, \
55 .off = 0, \
56 .imm = 0 })
57
Alexei Starovoitovbf508872015-10-07 22:23:23 -070058#define BPF_MOV32_REG(DST, SRC) \
59 ((struct bpf_insn) { \
60 .code = BPF_ALU | BPF_MOV | BPF_X, \
61 .dst_reg = DST, \
62 .src_reg = SRC, \
63 .off = 0, \
64 .imm = 0 })
65
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070066/* Short form of mov, dst_reg = imm32 */
67
68#define BPF_MOV64_IMM(DST, IMM) \
69 ((struct bpf_insn) { \
70 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
71 .dst_reg = DST, \
72 .src_reg = 0, \
73 .off = 0, \
74 .imm = IMM })
75
Josef Bacik48461132016-09-28 10:54:32 -040076#define BPF_MOV32_IMM(DST, IMM) \
77 ((struct bpf_insn) { \
78 .code = BPF_ALU | BPF_MOV | BPF_K, \
79 .dst_reg = DST, \
80 .src_reg = 0, \
81 .off = 0, \
82 .imm = IMM })
83
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070084/* 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)
87
88#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
89 ((struct bpf_insn) { \
90 .code = BPF_LD | BPF_DW | BPF_IMM, \
91 .dst_reg = DST, \
92 .src_reg = SRC, \
93 .off = 0, \
94 .imm = (__u32) (IMM) }), \
95 ((struct bpf_insn) { \
96 .code = 0, /* zero is reserved opcode */ \
97 .dst_reg = 0, \
98 .src_reg = 0, \
99 .off = 0, \
100 .imm = ((__u64) (IMM)) >> 32 })
101
Daniel Borkmannf1a66f82015-03-01 12:31:43 +0100102#ifndef BPF_PSEUDO_MAP_FD
103# define BPF_PSEUDO_MAP_FD 1
104#endif
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700105
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)
109
110
Alexei Starovoitov03f47232014-12-01 15:06:36 -0800111/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
112
113#define BPF_LD_ABS(SIZE, IMM) \
114 ((struct bpf_insn) { \
115 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
116 .dst_reg = 0, \
117 .src_reg = 0, \
118 .off = 0, \
119 .imm = IMM })
120
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700121/* Memory load, dst_reg = *(uint *) (src_reg + off16) */
122
123#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
124 ((struct bpf_insn) { \
125 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
126 .dst_reg = DST, \
127 .src_reg = SRC, \
128 .off = OFF, \
129 .imm = 0 })
130
131/* Memory store, *(uint *) (dst_reg + off16) = src_reg */
132
133#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
134 ((struct bpf_insn) { \
135 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
136 .dst_reg = DST, \
137 .src_reg = SRC, \
138 .off = OFF, \
139 .imm = 0 })
140
141/* Memory store, *(uint *) (dst_reg + off16) = imm32 */
142
143#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
144 ((struct bpf_insn) { \
145 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
146 .dst_reg = DST, \
147 .src_reg = 0, \
148 .off = OFF, \
149 .imm = IMM })
150
151/* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
152
153#define BPF_JMP_REG(OP, DST, SRC, OFF) \
154 ((struct bpf_insn) { \
155 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
156 .dst_reg = DST, \
157 .src_reg = SRC, \
158 .off = OFF, \
159 .imm = 0 })
160
161/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
162
163#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
164 ((struct bpf_insn) { \
165 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
166 .dst_reg = DST, \
167 .src_reg = 0, \
168 .off = OFF, \
169 .imm = IMM })
170
171/* Raw code statement block */
172
173#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
174 ((struct bpf_insn) { \
175 .code = CODE, \
176 .dst_reg = DST, \
177 .src_reg = SRC, \
178 .off = OFF, \
179 .imm = IMM })
180
181/* Program exit */
182
183#define BPF_EXIT_INSN() \
184 ((struct bpf_insn) { \
185 .code = BPF_JMP | BPF_EXIT, \
186 .dst_reg = 0, \
187 .src_reg = 0, \
188 .off = 0, \
189 .imm = 0 })
190
Alexei Starovoitov03f47232014-12-01 15:06:36 -0800191/* create RAW socket and bind to interface 'name' */
192int open_raw_sock(const char *name);
193
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700194struct perf_event_attr;
195int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
196 int group_fd, unsigned long flags);
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700197#endif