]>
Commit | Line | Data |
---|---|---|
3c731eba AS |
1 | /* eBPF mini library */ |
2 | #ifndef __LIBBPF_H | |
3 | #define __LIBBPF_H | |
4 | ||
5 | struct bpf_insn; | |
6 | ||
7 | int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, | |
89b97607 | 8 | int max_entries, int map_flags); |
d40fc181 JS |
9 | int bpf_map_update_elem(int fd, void *key, void *value, unsigned long long flags); |
10 | int bpf_map_lookup_elem(int fd, void *key, void *value); | |
11 | int bpf_map_delete_elem(int fd, void *key); | |
12 | int bpf_map_get_next_key(int fd, void *key, void *next_key); | |
3c731eba | 13 | |
d40fc181 JS |
14 | int bpf_load_program(enum bpf_prog_type prog_type, |
15 | const struct bpf_insn *insns, int insn_len, | |
16 | const char *license, int kern_version, | |
17 | char *log_buf, size_t log_buf_sz); | |
3c731eba | 18 | |
d8c5b17f DM |
19 | int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type); |
20 | int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); | |
21 | ||
42984d7c DB |
22 | int bpf_obj_pin(int fd, const char *pathname); |
23 | int bpf_obj_get(const char *pathname); | |
24 | ||
d40fc181 | 25 | #define BPF_LOG_BUF_SIZE (256 * 1024) |
3c731eba AS |
26 | |
27 | /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ | |
28 | ||
29 | #define BPF_ALU64_REG(OP, DST, SRC) \ | |
30 | ((struct bpf_insn) { \ | |
31 | .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ | |
32 | .dst_reg = DST, \ | |
33 | .src_reg = SRC, \ | |
34 | .off = 0, \ | |
35 | .imm = 0 }) | |
36 | ||
37 | #define BPF_ALU32_REG(OP, DST, SRC) \ | |
38 | ((struct bpf_insn) { \ | |
39 | .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ | |
40 | .dst_reg = DST, \ | |
41 | .src_reg = SRC, \ | |
42 | .off = 0, \ | |
43 | .imm = 0 }) | |
44 | ||
45 | /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ | |
46 | ||
47 | #define BPF_ALU64_IMM(OP, DST, IMM) \ | |
48 | ((struct bpf_insn) { \ | |
49 | .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ | |
50 | .dst_reg = DST, \ | |
51 | .src_reg = 0, \ | |
52 | .off = 0, \ | |
53 | .imm = IMM }) | |
54 | ||
55 | #define BPF_ALU32_IMM(OP, DST, IMM) \ | |
56 | ((struct bpf_insn) { \ | |
57 | .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ | |
58 | .dst_reg = DST, \ | |
59 | .src_reg = 0, \ | |
60 | .off = 0, \ | |
61 | .imm = IMM }) | |
62 | ||
63 | /* Short form of mov, dst_reg = src_reg */ | |
64 | ||
65 | #define BPF_MOV64_REG(DST, SRC) \ | |
66 | ((struct bpf_insn) { \ | |
67 | .code = BPF_ALU64 | BPF_MOV | BPF_X, \ | |
68 | .dst_reg = DST, \ | |
69 | .src_reg = SRC, \ | |
70 | .off = 0, \ | |
71 | .imm = 0 }) | |
72 | ||
bf508877 AS |
73 | #define BPF_MOV32_REG(DST, SRC) \ |
74 | ((struct bpf_insn) { \ | |
75 | .code = BPF_ALU | BPF_MOV | BPF_X, \ | |
76 | .dst_reg = DST, \ | |
77 | .src_reg = SRC, \ | |
78 | .off = 0, \ | |
79 | .imm = 0 }) | |
80 | ||
3c731eba AS |
81 | /* Short form of mov, dst_reg = imm32 */ |
82 | ||
83 | #define BPF_MOV64_IMM(DST, IMM) \ | |
84 | ((struct bpf_insn) { \ | |
85 | .code = BPF_ALU64 | BPF_MOV | BPF_K, \ | |
86 | .dst_reg = DST, \ | |
87 | .src_reg = 0, \ | |
88 | .off = 0, \ | |
48461135 JB |
89 | .imm = IMM }) |
90 | ||
91 | #define BPF_MOV32_IMM(DST, IMM) \ | |
92 | ((struct bpf_insn) { \ | |
93 | .code = BPF_ALU | BPF_MOV | BPF_K, \ | |
94 | .dst_reg = DST, \ | |
95 | .src_reg = 0, \ | |
96 | .off = 0, \ | |
3c731eba AS |
97 | .imm = IMM }) |
98 | ||
99 | /* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */ | |
100 | #define BPF_LD_IMM64(DST, IMM) \ | |
101 | BPF_LD_IMM64_RAW(DST, 0, IMM) | |
102 | ||
103 | #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \ | |
104 | ((struct bpf_insn) { \ | |
105 | .code = BPF_LD | BPF_DW | BPF_IMM, \ | |
106 | .dst_reg = DST, \ | |
107 | .src_reg = SRC, \ | |
108 | .off = 0, \ | |
109 | .imm = (__u32) (IMM) }), \ | |
110 | ((struct bpf_insn) { \ | |
111 | .code = 0, /* zero is reserved opcode */ \ | |
112 | .dst_reg = 0, \ | |
113 | .src_reg = 0, \ | |
114 | .off = 0, \ | |
115 | .imm = ((__u64) (IMM)) >> 32 }) | |
116 | ||
f1a66f85 DB |
117 | #ifndef BPF_PSEUDO_MAP_FD |
118 | # define BPF_PSEUDO_MAP_FD 1 | |
119 | #endif | |
3c731eba AS |
120 | |
121 | /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */ | |
122 | #define BPF_LD_MAP_FD(DST, MAP_FD) \ | |
123 | BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD) | |
124 | ||
125 | ||
03f4723e AS |
126 | /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ |
127 | ||
128 | #define BPF_LD_ABS(SIZE, IMM) \ | |
129 | ((struct bpf_insn) { \ | |
130 | .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ | |
131 | .dst_reg = 0, \ | |
132 | .src_reg = 0, \ | |
133 | .off = 0, \ | |
134 | .imm = IMM }) | |
135 | ||
3c731eba AS |
136 | /* Memory load, dst_reg = *(uint *) (src_reg + off16) */ |
137 | ||
138 | #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \ | |
139 | ((struct bpf_insn) { \ | |
140 | .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ | |
141 | .dst_reg = DST, \ | |
142 | .src_reg = SRC, \ | |
143 | .off = OFF, \ | |
144 | .imm = 0 }) | |
145 | ||
146 | /* Memory store, *(uint *) (dst_reg + off16) = src_reg */ | |
147 | ||
148 | #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \ | |
149 | ((struct bpf_insn) { \ | |
150 | .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ | |
151 | .dst_reg = DST, \ | |
152 | .src_reg = SRC, \ | |
153 | .off = OFF, \ | |
154 | .imm = 0 }) | |
155 | ||
156 | /* Memory store, *(uint *) (dst_reg + off16) = imm32 */ | |
157 | ||
158 | #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \ | |
159 | ((struct bpf_insn) { \ | |
160 | .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \ | |
161 | .dst_reg = DST, \ | |
162 | .src_reg = 0, \ | |
163 | .off = OFF, \ | |
164 | .imm = IMM }) | |
165 | ||
166 | /* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */ | |
167 | ||
168 | #define BPF_JMP_REG(OP, DST, SRC, OFF) \ | |
169 | ((struct bpf_insn) { \ | |
170 | .code = BPF_JMP | BPF_OP(OP) | BPF_X, \ | |
171 | .dst_reg = DST, \ | |
172 | .src_reg = SRC, \ | |
173 | .off = OFF, \ | |
174 | .imm = 0 }) | |
175 | ||
176 | /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ | |
177 | ||
178 | #define BPF_JMP_IMM(OP, DST, IMM, OFF) \ | |
179 | ((struct bpf_insn) { \ | |
180 | .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ | |
181 | .dst_reg = DST, \ | |
182 | .src_reg = 0, \ | |
183 | .off = OFF, \ | |
184 | .imm = IMM }) | |
185 | ||
186 | /* Raw code statement block */ | |
187 | ||
188 | #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \ | |
189 | ((struct bpf_insn) { \ | |
190 | .code = CODE, \ | |
191 | .dst_reg = DST, \ | |
192 | .src_reg = SRC, \ | |
193 | .off = OFF, \ | |
194 | .imm = IMM }) | |
195 | ||
196 | /* Program exit */ | |
197 | ||
198 | #define BPF_EXIT_INSN() \ | |
199 | ((struct bpf_insn) { \ | |
200 | .code = BPF_JMP | BPF_EXIT, \ | |
201 | .dst_reg = 0, \ | |
202 | .src_reg = 0, \ | |
203 | .off = 0, \ | |
204 | .imm = 0 }) | |
205 | ||
03f4723e AS |
206 | /* create RAW socket and bind to interface 'name' */ |
207 | int open_raw_sock(const char *name); | |
208 | ||
b896c4f9 AS |
209 | struct perf_event_attr; |
210 | int perf_event_open(struct perf_event_attr *attr, int pid, int cpu, | |
211 | int group_fd, unsigned long flags); | |
3c731eba | 212 | #endif |