blob: b06cf5aea097febbf022462f8662cbc883ae6d2f [file] [log] [blame]
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -07001/* eBPF mini library */
2#ifndef __LIBBPF_H
3#define __LIBBPF_H
4
5struct bpf_insn;
6
7int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
Alexei Starovoitov89b97602016-03-07 21:57:20 -08008 int max_entries, int map_flags);
Alexei Starovoitovffb65f22014-11-13 17:36:48 -08009int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070010int bpf_lookup_elem(int fd, void *key, void *value);
11int bpf_delete_elem(int fd, void *key);
12int bpf_get_next_key(int fd, void *key, void *next_key);
13
14int bpf_prog_load(enum bpf_prog_type prog_type,
15 const struct bpf_insn *insns, int insn_len,
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070016 const char *license, int kern_version);
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070017
Alexei Starovoitov1ee2b4b2017-02-10 20:28:24 -080018int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type,
19 unsigned int flags);
Daniel Mackc00662a2016-11-23 16:52:30 +010020int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
21
Daniel Borkmann42984d72015-10-29 14:58:10 +010022int bpf_obj_pin(int fd, const char *pathname);
23int bpf_obj_get(const char *pathname);
24
Alexei Starovoitova8085782014-12-01 15:06:38 -080025#define LOG_BUF_SIZE 65536
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070026extern char bpf_log_buf[LOG_BUF_SIZE];
27
28/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
29
30#define BPF_ALU64_REG(OP, DST, SRC) \
31 ((struct bpf_insn) { \
32 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
33 .dst_reg = DST, \
34 .src_reg = SRC, \
35 .off = 0, \
36 .imm = 0 })
37
38#define BPF_ALU32_REG(OP, DST, SRC) \
39 ((struct bpf_insn) { \
40 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
41 .dst_reg = DST, \
42 .src_reg = SRC, \
43 .off = 0, \
44 .imm = 0 })
45
46/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
47
48#define BPF_ALU64_IMM(OP, DST, IMM) \
49 ((struct bpf_insn) { \
50 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
51 .dst_reg = DST, \
52 .src_reg = 0, \
53 .off = 0, \
54 .imm = IMM })
55
56#define BPF_ALU32_IMM(OP, DST, IMM) \
57 ((struct bpf_insn) { \
58 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
59 .dst_reg = DST, \
60 .src_reg = 0, \
61 .off = 0, \
62 .imm = IMM })
63
64/* Short form of mov, dst_reg = src_reg */
65
66#define BPF_MOV64_REG(DST, SRC) \
67 ((struct bpf_insn) { \
68 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
69 .dst_reg = DST, \
70 .src_reg = SRC, \
71 .off = 0, \
72 .imm = 0 })
73
Alexei Starovoitovbf508872015-10-07 22:23:23 -070074#define BPF_MOV32_REG(DST, SRC) \
75 ((struct bpf_insn) { \
76 .code = BPF_ALU | BPF_MOV | BPF_X, \
77 .dst_reg = DST, \
78 .src_reg = SRC, \
79 .off = 0, \
80 .imm = 0 })
81
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -070082/* Short form of mov, dst_reg = imm32 */
83
84#define BPF_MOV64_IMM(DST, IMM) \
85 ((struct bpf_insn) { \
86 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
87 .dst_reg = DST, \
88 .src_reg = 0, \
89 .off = 0, \
90 .imm = IMM })
91
Josef Bacik48461132016-09-28 10:54:32 -040092#define BPF_MOV32_IMM(DST, IMM) \
93 ((struct bpf_insn) { \
94 .code = BPF_ALU | BPF_MOV | BPF_K, \
95 .dst_reg = DST, \
96 .src_reg = 0, \
97 .off = 0, \
98 .imm = IMM })
99
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700100/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
101#define BPF_LD_IMM64(DST, IMM) \
102 BPF_LD_IMM64_RAW(DST, 0, IMM)
103
104#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
105 ((struct bpf_insn) { \
106 .code = BPF_LD | BPF_DW | BPF_IMM, \
107 .dst_reg = DST, \
108 .src_reg = SRC, \
109 .off = 0, \
110 .imm = (__u32) (IMM) }), \
111 ((struct bpf_insn) { \
112 .code = 0, /* zero is reserved opcode */ \
113 .dst_reg = 0, \
114 .src_reg = 0, \
115 .off = 0, \
116 .imm = ((__u64) (IMM)) >> 32 })
117
Daniel Borkmannf1a66f82015-03-01 12:31:43 +0100118#ifndef BPF_PSEUDO_MAP_FD
119# define BPF_PSEUDO_MAP_FD 1
120#endif
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700121
122/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
123#define BPF_LD_MAP_FD(DST, MAP_FD) \
124 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
125
126
Alexei Starovoitov03f47232014-12-01 15:06:36 -0800127/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
128
129#define BPF_LD_ABS(SIZE, IMM) \
130 ((struct bpf_insn) { \
131 .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
132 .dst_reg = 0, \
133 .src_reg = 0, \
134 .off = 0, \
135 .imm = IMM })
136
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700137/* Memory load, dst_reg = *(uint *) (src_reg + off16) */
138
139#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
140 ((struct bpf_insn) { \
141 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
142 .dst_reg = DST, \
143 .src_reg = SRC, \
144 .off = OFF, \
145 .imm = 0 })
146
147/* Memory store, *(uint *) (dst_reg + off16) = src_reg */
148
149#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
150 ((struct bpf_insn) { \
151 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
152 .dst_reg = DST, \
153 .src_reg = SRC, \
154 .off = OFF, \
155 .imm = 0 })
156
157/* Memory store, *(uint *) (dst_reg + off16) = imm32 */
158
159#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
160 ((struct bpf_insn) { \
161 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
162 .dst_reg = DST, \
163 .src_reg = 0, \
164 .off = OFF, \
165 .imm = IMM })
166
167/* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
168
169#define BPF_JMP_REG(OP, DST, SRC, OFF) \
170 ((struct bpf_insn) { \
171 .code = BPF_JMP | BPF_OP(OP) | BPF_X, \
172 .dst_reg = DST, \
173 .src_reg = SRC, \
174 .off = OFF, \
175 .imm = 0 })
176
177/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
178
179#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
180 ((struct bpf_insn) { \
181 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
182 .dst_reg = DST, \
183 .src_reg = 0, \
184 .off = OFF, \
185 .imm = IMM })
186
187/* Raw code statement block */
188
189#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
190 ((struct bpf_insn) { \
191 .code = CODE, \
192 .dst_reg = DST, \
193 .src_reg = SRC, \
194 .off = OFF, \
195 .imm = IMM })
196
197/* Program exit */
198
199#define BPF_EXIT_INSN() \
200 ((struct bpf_insn) { \
201 .code = BPF_JMP | BPF_EXIT, \
202 .dst_reg = 0, \
203 .src_reg = 0, \
204 .off = 0, \
205 .imm = 0 })
206
Alexei Starovoitov03f47232014-12-01 15:06:36 -0800207/* create RAW socket and bind to interface 'name' */
208int open_raw_sock(const char *name);
209
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700210struct perf_event_attr;
211int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
212 int group_fd, unsigned long flags);
Alexei Starovoitov3c731eb2014-09-26 00:17:07 -0700213#endif