Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2021 Microsoft Corporation
3 : : *
4 : : * Based on bpf_convert_filter() in the Linux kernel sources
5 : : * and filter2xdp.
6 : : *
7 : : * Licensed as BSD with permission original authors.
8 : : * Copyright (C) 2017 Tobias Klauser
9 : : * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
10 : : */
11 : :
12 : : #include "bpf_impl.h"
13 : : #include <eal_export.h>
14 : : #include <rte_errno.h>
15 : :
16 : : #ifdef RTE_HAS_LIBPCAP
17 : :
18 : : #include <assert.h>
19 : : #include <errno.h>
20 : : #include <stdbool.h>
21 : : #include <stddef.h>
22 : : #include <stdint.h>
23 : : #include <stdlib.h>
24 : : #include <string.h>
25 : :
26 : : #include <rte_common.h>
27 : : #include <rte_bpf.h>
28 : : #include <rte_log.h>
29 : : #include <rte_malloc.h>
30 : :
31 : : #include <pcap/pcap.h>
32 : : #include <pcap/bpf.h>
33 : :
34 : : #include "bpf_def.h"
35 : :
36 : : #ifndef BPF_MAXINSNS
37 : : #define BPF_MAXINSNS 4096
38 : : #endif
39 : :
40 : : /*
41 : : * Linux socket filter uses negative absolute offsets to
42 : : * reference ancillary data.
43 : : */
44 : : #define SKF_AD_OFF (-0x1000)
45 : : #define SKF_AD_PROTOCOL 0
46 : : #define SKF_AD_PKTTYPE 4
47 : : #define SKF_AD_IFINDEX 8
48 : : #define SKF_AD_NLATTR 12
49 : : #define SKF_AD_NLATTR_NEST 16
50 : : #define SKF_AD_MARK 20
51 : : #define SKF_AD_QUEUE 24
52 : : #define SKF_AD_HATYPE 28
53 : : #define SKF_AD_RXHASH 32
54 : : #define SKF_AD_CPU 36
55 : : #define SKF_AD_ALU_XOR_X 40
56 : : #define SKF_AD_VLAN_TAG 44
57 : : #define SKF_AD_VLAN_TAG_PRESENT 48
58 : : #define SKF_AD_PAY_OFFSET 52
59 : : #define SKF_AD_RANDOM 56
60 : : #define SKF_AD_VLAN_TPID 60
61 : : #define SKF_AD_MAX 64
62 : :
63 : : /* ArgX, context and stack frame pointer register positions. Note,
64 : : * Arg1, Arg2, Arg3, etc are used as argument mappings of function
65 : : * calls in BPF_CALL instruction.
66 : : */
67 : : #define BPF_REG_ARG1 EBPF_REG_1
68 : : #define BPF_REG_ARG2 EBPF_REG_2
69 : : #define BPF_REG_ARG3 EBPF_REG_3
70 : : #define BPF_REG_ARG4 EBPF_REG_4
71 : : #define BPF_REG_ARG5 EBPF_REG_5
72 : : #define BPF_REG_CTX EBPF_REG_6
73 : : #define BPF_REG_FP EBPF_REG_10
74 : :
75 : : /* Additional register mappings for converted user programs. */
76 : : #define BPF_REG_A EBPF_REG_0
77 : : #define BPF_REG_X EBPF_REG_7
78 : : #define BPF_REG_TMP EBPF_REG_8
79 : :
80 : : /* Helper macros for filter block array initializers. */
81 : :
82 : : /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
83 : :
84 : : #define EBPF_ALU64_REG(OP, DST, SRC) \
85 : : ((struct rte_ebpf_insn) { \
86 : : .code = EBPF_ALU64 | BPF_OP(OP) | BPF_X, \
87 : : .dst_reg = DST, \
88 : : .src_reg = SRC, \
89 : : .off = 0, \
90 : : .imm = 0 })
91 : :
92 : : #define BPF_ALU32_REG(OP, DST, SRC) \
93 : : ((struct rte_ebpf_insn) { \
94 : : .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
95 : : .dst_reg = DST, \
96 : : .src_reg = SRC, \
97 : : .off = 0, \
98 : : .imm = 0 })
99 : :
100 : : /* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
101 : :
102 : : #define BPF_ALU32_IMM(OP, DST, IMM) \
103 : : ((struct rte_ebpf_insn) { \
104 : : .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
105 : : .dst_reg = DST, \
106 : : .src_reg = 0, \
107 : : .off = 0, \
108 : : .imm = IMM })
109 : :
110 : : /* Short form of mov, dst_reg = src_reg */
111 : :
112 : : #define BPF_MOV64_REG(DST, SRC) \
113 : : ((struct rte_ebpf_insn) { \
114 : : .code = EBPF_ALU64 | EBPF_MOV | BPF_X, \
115 : : .dst_reg = DST, \
116 : : .src_reg = SRC, \
117 : : .off = 0, \
118 : : .imm = 0 })
119 : :
120 : : #define BPF_MOV32_REG(DST, SRC) \
121 : : ((struct rte_ebpf_insn) { \
122 : : .code = BPF_ALU | EBPF_MOV | BPF_X, \
123 : : .dst_reg = DST, \
124 : : .src_reg = SRC, \
125 : : .off = 0, \
126 : : .imm = 0 })
127 : :
128 : : /* Short form of mov, dst_reg = imm32 */
129 : :
130 : : #define BPF_MOV32_IMM(DST, IMM) \
131 : : ((struct rte_ebpf_insn) { \
132 : : .code = BPF_ALU | EBPF_MOV | BPF_K, \
133 : : .dst_reg = DST, \
134 : : .src_reg = 0, \
135 : : .off = 0, \
136 : : .imm = IMM })
137 : :
138 : : /* Short form of mov based on type, BPF_X: dst_reg = src_reg, BPF_K: dst_reg = imm32 */
139 : :
140 : : #define BPF_MOV32_RAW(TYPE, DST, SRC, IMM) \
141 : : ((struct rte_ebpf_insn) { \
142 : : .code = BPF_ALU | EBPF_MOV | BPF_SRC(TYPE), \
143 : : .dst_reg = DST, \
144 : : .src_reg = SRC, \
145 : : .off = 0, \
146 : : .imm = IMM })
147 : :
148 : : /* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
149 : :
150 : : #define BPF_LD_ABS(SIZE, IMM) \
151 : : ((struct rte_ebpf_insn) { \
152 : : .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
153 : : .dst_reg = 0, \
154 : : .src_reg = 0, \
155 : : .off = 0, \
156 : : .imm = IMM })
157 : :
158 : : /* Memory load, dst_reg = *(uint *) (src_reg + off16) */
159 : :
160 : : #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
161 : : ((struct rte_ebpf_insn) { \
162 : : .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
163 : : .dst_reg = DST, \
164 : : .src_reg = SRC, \
165 : : .off = OFF, \
166 : : .imm = 0 })
167 : :
168 : : /* Memory store, *(uint *) (dst_reg + off16) = src_reg */
169 : :
170 : : #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
171 : : ((struct rte_ebpf_insn) { \
172 : : .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
173 : : .dst_reg = DST, \
174 : : .src_reg = SRC, \
175 : : .off = OFF, \
176 : : .imm = 0 })
177 : :
178 : : /* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
179 : :
180 : : #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
181 : : ((struct rte_ebpf_insn) { \
182 : : .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
183 : : .dst_reg = DST, \
184 : : .src_reg = 0, \
185 : : .off = OFF, \
186 : : .imm = IMM })
187 : :
188 : : /* Raw code statement block */
189 : :
190 : : #define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
191 : : ((struct rte_ebpf_insn) { \
192 : : .code = CODE, \
193 : : .dst_reg = DST, \
194 : : .src_reg = SRC, \
195 : : .off = OFF, \
196 : : .imm = IMM })
197 : :
198 : : /* Program exit */
199 : :
200 : : #define BPF_EXIT_INSN() \
201 : : ((struct rte_ebpf_insn) { \
202 : : .code = BPF_JMP | EBPF_EXIT, \
203 : : .dst_reg = 0, \
204 : : .src_reg = 0, \
205 : : .off = 0, \
206 : : .imm = 0 })
207 : :
208 : : /*
209 : : * Placeholder to convert BPF extensions like length and VLAN tag
210 : : * If and when DPDK BPF supports them.
211 : : */
212 : 1128 : static bool convert_bpf_load(const struct bpf_insn *fp,
213 : : struct rte_ebpf_insn **new_insnp __rte_unused)
214 : : {
215 [ - + ]: 1128 : switch (fp->k) {
216 : 0 : case SKF_AD_OFF + SKF_AD_PROTOCOL:
217 : : case SKF_AD_OFF + SKF_AD_PKTTYPE:
218 : : case SKF_AD_OFF + SKF_AD_IFINDEX:
219 : : case SKF_AD_OFF + SKF_AD_HATYPE:
220 : : case SKF_AD_OFF + SKF_AD_MARK:
221 : : case SKF_AD_OFF + SKF_AD_RXHASH:
222 : : case SKF_AD_OFF + SKF_AD_QUEUE:
223 : : case SKF_AD_OFF + SKF_AD_VLAN_TAG:
224 : : case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
225 : : case SKF_AD_OFF + SKF_AD_VLAN_TPID:
226 : : case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
227 : : case SKF_AD_OFF + SKF_AD_NLATTR:
228 : : case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
229 : : case SKF_AD_OFF + SKF_AD_CPU:
230 : : case SKF_AD_OFF + SKF_AD_RANDOM:
231 : : case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
232 : : /* Linux has special negative offsets to access meta-data. */
233 : 0 : RTE_BPF_LOG_LINE(ERR,
234 : : "rte_bpf_convert: socket offset %d not supported",
235 : : fp->k - SKF_AD_OFF);
236 : 0 : return true;
237 : : default:
238 : : return false;
239 : : }
240 : : }
241 : :
242 : 108 : static int bpf_convert_filter(const struct bpf_insn *prog, size_t len,
243 : : struct rte_ebpf_insn *new_prog, uint32_t *new_len)
244 : : {
245 : : unsigned int pass = 0;
246 : : size_t new_flen = 0, target, i;
247 : : struct rte_ebpf_insn *new_insn;
248 : : const struct bpf_insn *fp;
249 : : int *addrs = NULL;
250 : : uint8_t bpf_src;
251 : :
252 [ - + ]: 108 : if (len > BPF_MAXINSNS) {
253 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "cBPF program too long (%zu insns)",
254 : : len);
255 : 0 : return -EINVAL;
256 : : }
257 : :
258 : : /* On second pass, allocate the new program */
259 [ + + ]: 108 : if (new_prog) {
260 : 54 : addrs = calloc(len, sizeof(*addrs));
261 [ + - ]: 54 : if (addrs == NULL)
262 : : return -ENOMEM;
263 : : }
264 : :
265 : 108 : do_pass:
266 : : new_insn = new_prog;
267 : : fp = prog;
268 : :
269 : : /* Classic BPF related prologue emission. */
270 [ + + ]: 162 : if (new_insn) {
271 : : /* Classic BPF expects A and X to be reset first. These need
272 : : * to be guaranteed to be the first two instructions.
273 : : */
274 : 108 : *new_insn++ = EBPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
275 : 108 : *new_insn++ = EBPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
276 : :
277 : : /* All programs must keep CTX in callee saved BPF_REG_CTX.
278 : : * In eBPF case it's done by the compiler, here we need to
279 : : * do this ourself. Initial CTX is present in BPF_REG_ARG1.
280 : : */
281 : 108 : *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
282 : : } else {
283 : : new_insn += 3;
284 : : }
285 : :
286 [ + + ]: 3696 : for (i = 0; i < len; fp++, i++) {
287 : 3534 : struct rte_ebpf_insn tmp_insns[6] = { };
288 : 3534 : struct rte_ebpf_insn *insn = tmp_insns;
289 : :
290 [ + + ]: 3534 : if (addrs)
291 : 2356 : addrs[i] = new_insn - new_prog;
292 : :
293 [ + + - + : 3534 : switch (fp->code) {
- + + + +
+ + + - +
- ]
294 : : /* Absolute loads are how classic BPF accesses skb */
295 : 1128 : case BPF_LD | BPF_ABS | BPF_W:
296 : : case BPF_LD | BPF_ABS | BPF_H:
297 : : case BPF_LD | BPF_ABS | BPF_B:
298 [ - + ]: 1128 : if (convert_bpf_load(fp, &insn))
299 : 0 : goto err;
300 : :
301 : 1128 : *insn = BPF_RAW_INSN(fp->code, 0, 0, 0, fp->k);
302 : 1128 : break;
303 : :
304 : : /* Indirect loads index the packet with X: A = P[X + k] */
305 : 150 : case BPF_LD | BPF_IND | BPF_W:
306 : : case BPF_LD | BPF_IND | BPF_H:
307 : : case BPF_LD | BPF_IND | BPF_B:
308 : 150 : *insn = BPF_RAW_INSN(fp->code, 0, BPF_REG_X, 0, fp->k);
309 : 150 : break;
310 : :
311 : 0 : case BPF_ALU | BPF_DIV | BPF_X:
312 : : case BPF_ALU | BPF_MOD | BPF_X:
313 : : /* For cBPF, don't cause floating point exception */
314 : 0 : *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
315 : 0 : *insn++ = BPF_JMP_IMM(EBPF_JNE, BPF_REG_X, 0, 2);
316 : 0 : *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
317 : 0 : *insn++ = BPF_EXIT_INSN();
318 : : /* fallthrough */
319 : 222 : case BPF_ALU | BPF_ADD | BPF_X:
320 : : case BPF_ALU | BPF_ADD | BPF_K:
321 : : case BPF_ALU | BPF_SUB | BPF_X:
322 : : case BPF_ALU | BPF_SUB | BPF_K:
323 : : case BPF_ALU | BPF_AND | BPF_X:
324 : : case BPF_ALU | BPF_AND | BPF_K:
325 : : case BPF_ALU | BPF_OR | BPF_X:
326 : : case BPF_ALU | BPF_OR | BPF_K:
327 : : case BPF_ALU | BPF_LSH | BPF_X:
328 : : case BPF_ALU | BPF_LSH | BPF_K:
329 : : case BPF_ALU | BPF_RSH | BPF_X:
330 : : case BPF_ALU | BPF_RSH | BPF_K:
331 : : case BPF_ALU | BPF_XOR | BPF_X:
332 : : case BPF_ALU | BPF_XOR | BPF_K:
333 : : case BPF_ALU | BPF_MUL | BPF_X:
334 : : case BPF_ALU | BPF_MUL | BPF_K:
335 : : case BPF_ALU | BPF_DIV | BPF_K:
336 : : case BPF_ALU | BPF_MOD | BPF_K:
337 : : case BPF_ALU | BPF_NEG:
338 : : /* All arithmetic insns map as-is. */
339 : 222 : insn->code = fp->code;
340 : 222 : insn->dst_reg = BPF_REG_A;
341 : 222 : bpf_src = BPF_SRC(fp->code);
342 [ + + ]: 222 : insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
343 : 222 : insn->off = 0;
344 : 222 : insn->imm = fp->k;
345 : 222 : break;
346 : :
347 : : /* Jump transformation cannot use BPF block macros
348 : : * everywhere as offset calculation and target updates
349 : : * require a bit more work than the rest, i.e. jump
350 : : * opcodes map as-is, but offsets need adjustment.
351 : : */
352 : :
353 : : #define BPF_EMIT_JMP \
354 : : do { \
355 : : if (target >= len) \
356 : : goto err; \
357 : : insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
358 : : /* Adjust pc relative offset for 2nd or 3rd insn. */ \
359 : : insn->off -= insn - tmp_insns; \
360 : : } while (0)
361 : :
362 : 0 : case BPF_JMP | BPF_JA:
363 : 0 : target = i + fp->k + 1;
364 : 0 : insn->code = fp->code;
365 [ # # # # ]: 0 : BPF_EMIT_JMP;
366 : 0 : break;
367 : :
368 : 1542 : case BPF_JMP | BPF_JEQ | BPF_K:
369 : : case BPF_JMP | BPF_JEQ | BPF_X:
370 : : case BPF_JMP | BPF_JSET | BPF_K:
371 : : case BPF_JMP | BPF_JSET | BPF_X:
372 : : case BPF_JMP | BPF_JGT | BPF_K:
373 : : case BPF_JMP | BPF_JGT | BPF_X:
374 : : case BPF_JMP | BPF_JGE | BPF_K:
375 : : case BPF_JMP | BPF_JGE | BPF_X:
376 [ + - + + ]: 1542 : if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
377 : : /* BPF immediates are signed, zero extend
378 : : * immediate into tmp register and use it
379 : : * in compare insn.
380 : : */
381 : 210 : *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
382 : :
383 : : insn->dst_reg = BPF_REG_A;
384 : 210 : insn->src_reg = BPF_REG_TMP;
385 : : bpf_src = BPF_X;
386 : : } else {
387 : : insn->dst_reg = BPF_REG_A;
388 : 1332 : insn->imm = fp->k;
389 : 1332 : bpf_src = BPF_SRC(fp->code);
390 [ + - ]: 2664 : insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
391 : : }
392 : :
393 : : /* Common case where 'jump_false' is next insn. */
394 [ + + ]: 1542 : if (fp->jf == 0) {
395 : 528 : insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
396 : 528 : target = i + fp->jt + 1;
397 [ - + + + ]: 528 : BPF_EMIT_JMP;
398 : 528 : break;
399 : : }
400 : :
401 : : /* Convert JEQ into JNE when 'jump_true' is next insn. */
402 [ + + + + ]: 1014 : if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
403 : 906 : insn->code = BPF_JMP | EBPF_JNE | bpf_src;
404 : 906 : target = i + fp->jf + 1;
405 [ - + + + ]: 906 : BPF_EMIT_JMP;
406 : 906 : break;
407 : : }
408 : :
409 : : /* Other jumps are mapped into two insns: Jxx and JA. */
410 : 108 : target = i + fp->jt + 1;
411 : 108 : insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
412 [ - + + + ]: 108 : BPF_EMIT_JMP;
413 : 108 : insn++;
414 : :
415 : 108 : insn->code = BPF_JMP | BPF_JA;
416 : 108 : target = i + fp->jf + 1;
417 [ - + + + ]: 108 : BPF_EMIT_JMP;
418 : 108 : break;
419 : :
420 : : /* ldxb 4 * ([14] & 0xf) is remapped into 6 insns. */
421 : 78 : case BPF_LDX | BPF_MSH | BPF_B:
422 : : /* tmp = A */
423 : 78 : *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
424 : : /* A = BPF_R0 = *(u8 *) (skb->data + K) */
425 : 78 : *insn++ = BPF_LD_ABS(BPF_B, fp->k);
426 : : /* A &= 0xf */
427 : 78 : *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
428 : : /* A <<= 2 */
429 : 78 : *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
430 : : /* X = A */
431 : 78 : *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
432 : : /* A = tmp */
433 : 78 : *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
434 : 78 : break;
435 : :
436 : : /* RET_K is remapped into 2 insns. RET_A case doesn't need an
437 : : * extra mov as EBPF_REG_0 is already mapped into BPF_REG_A.
438 : : */
439 : 324 : case BPF_RET | BPF_A:
440 : : case BPF_RET | BPF_K:
441 [ + - ]: 324 : if (BPF_RVAL(fp->code) == BPF_K) {
442 : 324 : *insn++ = BPF_MOV32_RAW(BPF_K, EBPF_REG_0,
443 : : 0, fp->k);
444 : : }
445 : 324 : *insn = BPF_EXIT_INSN();
446 : 324 : break;
447 : :
448 : : /* Store to stack. */
449 : 12 : case BPF_ST:
450 : : case BPF_STX:
451 [ - + ]: 12 : *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
452 : : BPF_ST ? BPF_REG_A : BPF_REG_X,
453 : : -(BPF_MEMWORDS - fp->k) * 4);
454 : 12 : break;
455 : :
456 : : /* Load from stack. */
457 : 12 : case BPF_LD | BPF_MEM:
458 : : case BPF_LDX | BPF_MEM:
459 [ - + ]: 12 : *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
460 : : BPF_REG_A : BPF_REG_X, BPF_REG_FP,
461 : : -(BPF_MEMWORDS - fp->k) * 4);
462 : 12 : break;
463 : :
464 : : /* A = K or X = K */
465 : 12 : case BPF_LD | BPF_IMM:
466 : : case BPF_LDX | BPF_IMM:
467 [ - + ]: 12 : *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
468 : : BPF_REG_A : BPF_REG_X, fp->k);
469 : 12 : break;
470 : :
471 : : /* X = A */
472 : 48 : case BPF_MISC | BPF_TAX:
473 : 48 : *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
474 : 48 : break;
475 : :
476 : : /* A = X */
477 : 0 : case BPF_MISC | BPF_TXA:
478 : 0 : *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
479 : 0 : break;
480 : :
481 : : /* A = mbuf->len or X = mbuf->len */
482 : 6 : case BPF_LD | BPF_W | BPF_LEN:
483 : : case BPF_LDX | BPF_W | BPF_LEN:
484 : : /* BPF_ABS/BPF_IND implicitly expect mbuf ptr in R6 */
485 : :
486 [ - + ]: 6 : *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
487 : : BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
488 : : offsetof(struct rte_mbuf, pkt_len));
489 : 6 : break;
490 : :
491 : : /* Unknown instruction. */
492 : 0 : default:
493 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "Unknown instruction!: %#x",
494 : : fp->code);
495 : 0 : goto err;
496 : : }
497 : :
498 : 3534 : insn++;
499 [ + + ]: 3534 : if (new_prog)
500 : 2356 : memcpy(new_insn, tmp_insns,
501 : 2356 : sizeof(*insn) * (insn - tmp_insns));
502 : 3534 : new_insn += insn - tmp_insns;
503 : : }
504 : :
505 [ + + ]: 162 : if (!new_prog) {
506 : : /* Only calculating new length. */
507 : 54 : *new_len = new_insn - new_prog;
508 : 54 : return 0;
509 : : }
510 : :
511 : 108 : pass++;
512 [ + + ]: 108 : if ((ptrdiff_t)new_flen != new_insn - new_prog) {
513 : 54 : new_flen = new_insn - new_prog;
514 [ - + ]: 54 : if (pass > 2)
515 : 0 : goto err;
516 : 54 : goto do_pass;
517 : : }
518 : :
519 : 54 : free(addrs);
520 [ - + ]: 54 : assert(*new_len == new_flen);
521 : :
522 : : return 0;
523 : 0 : err:
524 : 0 : free(addrs);
525 : 0 : return -1;
526 : : }
527 : :
528 : : RTE_EXPORT_SYMBOL(rte_bpf_convert)
529 : : struct rte_bpf_prm *
530 : 27 : rte_bpf_convert(const struct bpf_program *prog)
531 : : {
532 : : struct rte_bpf_prm *prm = NULL;
533 : : struct rte_ebpf_insn *ebpf = NULL;
534 : 27 : uint32_t ebpf_len = 0;
535 : : int ret;
536 : :
537 [ - + ]: 27 : if (prog == NULL) {
538 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "NULL program");
539 : 0 : rte_errno = EINVAL;
540 : 0 : return NULL;
541 : : }
542 : :
543 : : /* 1st pass: calculate the eBPF program length */
544 : 27 : ret = bpf_convert_filter(prog->bf_insns, prog->bf_len, NULL, &ebpf_len);
545 [ - + ]: 27 : if (ret < 0) {
546 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "cannot get eBPF length");
547 : 0 : rte_errno = -ret;
548 : 0 : return NULL;
549 : : }
550 : :
551 : 27 : RTE_BPF_LOG_FUNC_LINE(DEBUG, "prog len cBPF=%u -> eBPF=%u",
552 : : prog->bf_len, ebpf_len);
553 : :
554 : 27 : prm = rte_zmalloc("bpf_filter",
555 : 27 : sizeof(*prm) + ebpf_len * sizeof(*ebpf), 0);
556 [ - + ]: 27 : if (prm == NULL) {
557 : 0 : rte_errno = ENOMEM;
558 : 0 : return NULL;
559 : : }
560 : :
561 : : /* The EPBF instructions in this case are right after the header */
562 : 27 : ebpf = (void *)(prm + 1);
563 : :
564 : : /* 2nd pass: remap cBPF to eBPF instructions */
565 : 27 : ret = bpf_convert_filter(prog->bf_insns, prog->bf_len, ebpf, &ebpf_len);
566 [ - + ]: 27 : if (ret < 0) {
567 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "cannot convert cBPF to eBPF");
568 : 0 : rte_free(prm);
569 : 0 : rte_errno = -ret;
570 : 0 : return NULL;
571 : : }
572 : :
573 : 27 : prm->ins = ebpf;
574 : 27 : prm->nb_ins = ebpf_len;
575 : :
576 : : /* Classic BPF programs use mbufs */
577 : 27 : prm->prog_arg.type = RTE_BPF_ARG_PTR_MBUF;
578 : 27 : prm->prog_arg.size = sizeof(struct rte_mbuf);
579 : :
580 : 27 : return prm;
581 : : }
582 : :
583 : : void
584 : 845 : __rte_bpf_convert_cleanup(struct __rte_bpf_load *load)
585 : : {
586 : 845 : free(load->ins);
587 : 845 : }
588 : :
589 : : int
590 : 27 : __rte_bpf_convert(struct __rte_bpf_load *load)
591 : : {
592 : : struct rte_bpf_prm_ex *const prm = &load->prm;
593 : 27 : uint32_t nb_ins = 0;
594 : : int ret;
595 : :
596 : : RTE_ASSERT(prm->origin == RTE_BPF_ORIGIN_CBPF);
597 : :
598 [ + - + - ]: 27 : if (prm->cbpf.ins == NULL || prm->cbpf.nb_ins == 0)
599 : : return -EINVAL;
600 : :
601 : : /* 1st pass: calculate the eBPF program length */
602 : 27 : ret = bpf_convert_filter(prm->cbpf.ins, prm->cbpf.nb_ins, NULL, &nb_ins);
603 [ - + ]: 27 : if (ret < 0) {
604 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "cannot get eBPF length");
605 : 0 : return ret;
606 : : }
607 : :
608 : : RTE_ASSERT(load->ins == NULL);
609 : 27 : load->ins = malloc(nb_ins * sizeof(load->ins[0]));
610 [ + - ]: 27 : if (load->ins == NULL)
611 : : return -ENOMEM;
612 : :
613 : : /* 2nd pass: remap cBPF to eBPF instructions */
614 : 27 : ret = bpf_convert_filter(prm->cbpf.ins, prm->cbpf.nb_ins, load->ins, &nb_ins);
615 [ - + ]: 27 : if (ret < 0) {
616 : 0 : RTE_BPF_LOG_FUNC_LINE(ERR, "cannot convert cBPF to eBPF");
617 : 0 : return ret;
618 : : }
619 : :
620 : 27 : prm->origin = RTE_BPF_ORIGIN_RAW;
621 : 27 : prm->raw.ins = load->ins;
622 : 27 : prm->raw.nb_ins = nb_ins;
623 : :
624 : 27 : return 0;
625 : : }
626 : :
627 : : #else /* RTE_HAS_LIBPCAP */
628 : :
629 : : RTE_EXPORT_SYMBOL(rte_bpf_convert)
630 : : struct rte_bpf_prm *
631 : : rte_bpf_convert(const struct bpf_program *prog)
632 : : {
633 : : RTE_SET_USED(prog);
634 : : RTE_BPF_LOG_FUNC_LINE(ERR, "not supported, rebuild with libpcap installed");
635 : : rte_errno = ENOTSUP;
636 : : return NULL;
637 : : }
638 : :
639 : : void
640 : : __rte_bpf_convert_cleanup(struct __rte_bpf_load *load)
641 : : {
642 : : RTE_ASSERT(load->ins == NULL);
643 : : }
644 : :
645 : : int
646 : : __rte_bpf_convert(struct __rte_bpf_load *load)
647 : : {
648 : : RTE_SET_USED(load);
649 : : RTE_BPF_LOG_FUNC_LINE(ERR, "not supported, rebuild with libpcap installed");
650 : : return -ENOTSUP;
651 : : }
652 : :
653 : : #endif /* RTE_HAS_LIBPCAP */
|