DPDK 26.11.0-rc2
rte_bpf.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
3 */
4
5#ifndef _RTE_BPF_H_
6#define _RTE_BPF_H_
7
19#include <rte_common.h>
20#include <rte_mbuf.h>
21#include <rte_malloc.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/*
28 * eBPF instruction format
29 */
30struct rte_ebpf_insn {
31 uint8_t code;
32 uint8_t dst_reg:4;
33 uint8_t src_reg:4;
34 int16_t off;
35 int32_t imm;
36};
37
38/*
39 * eBPF allows functions with up to 5 arguments.
40 */
41#define RTE_BPF_FUNC_MAX_ARGS 5
42
43#define RTE_BPF_EXEC_FLAG_JIT RTE_BIT64(0)
46#define RTE_BPF_EXEC_FLAG_MASK RTE_BPF_EXEC_FLAG_JIT
47
48/* Format instructions as assembler. */
49#define RTE_BPF_FORMAT_FLAG_DISASSEMBLY 0
50/* Format instructions as hexadecimal. */
51#define RTE_BPF_FORMAT_FLAG_HEXADECIMAL RTE_BIT32(0)
52
53/* Only valid in disassembly mode. */
54/* Format jump offsets relative to the next instruction. */
55#define RTE_BPF_FORMAT_FLAG_RELATIVE_JUMPS 0
56/* Format jump targets relative to the start of the program. */
57#define RTE_BPF_FORMAT_FLAG_ABSOLUTE_JUMPS RTE_BIT32(1)
58
59/* Only valid in hexadecimal mode. */
60/* Format full hexadecimal representation of wide instructions. */
61#define RTE_BPF_FORMAT_FLAG_AUTO_WIDE 0
62/* Format as hexadecimal only first half of wide instructions. */
63#define RTE_BPF_FORMAT_FLAG_NEVER_WIDE RTE_BIT32(2)
64
74};
75
80 enum rte_bpf_arg_type type;
85 size_t size;
86 size_t buf_size;
88};
89
93#define RTE_BPF_ARG_PTR_TYPE(x) ((x) & RTE_BPF_ARG_PTR)
94
102
107 const char *name;
109 union {
110 struct {
111 uint64_t (*val)(uint64_t, uint64_t, uint64_t,
112 uint64_t, uint64_t);
113 uint32_t nb_args;
114 struct rte_bpf_arg args[RTE_BPF_FUNC_MAX_ARGS];
117 } func;
118 struct {
119 void *val;
121 } var;
122 };
123};
124
133};
134
135struct bpf_insn;
136struct rte_bpf_validate_debug;
137
144 size_t sz;
146 uint32_t flags;
151 union {
152 struct {
153 const struct rte_ebpf_insn *ins;
154 uint32_t nb_ins;
155 } raw;
156 struct {
157 const struct bpf_insn *ins;
158 uint32_t nb_ins;
159 } cbpf;
160 struct {
161 const char *path;
162 const char *section;
163 } elf_file;
164 struct {
165 const void *data;
166 size_t size;
167 const char *section;
168 } elf_memory;
169 };
170
171 const struct rte_bpf_xsym *xsym;
173 uint32_t nb_xsym;
175 struct rte_bpf_arg prog_arg[RTE_BPF_FUNC_MAX_ARGS];
176 uint32_t nb_prog_arg;
178 /* Validate debug instance. */
179 struct rte_bpf_validate_debug *debug;
180};
181
186 const struct rte_ebpf_insn *ins;
187 uint32_t nb_ins;
188 const struct rte_bpf_xsym *xsym;
190 uint32_t nb_xsym;
192};
193
198 uint64_t (*func)(void *);
199 size_t sz;
200};
201
202union rte_bpf_func_arg {
203 uint64_t u64;
204 void *ptr;
205};
206
207typedef uint64_t (*rte_bpf_jit_func0_t)(void);
208typedef uint64_t (*rte_bpf_jit_func1_t)(union rte_bpf_func_arg);
209typedef uint64_t (*rte_bpf_jit_func2_t)(union rte_bpf_func_arg, union rte_bpf_func_arg);
210typedef uint64_t (*rte_bpf_jit_func3_t)(union rte_bpf_func_arg, union rte_bpf_func_arg,
211 union rte_bpf_func_arg);
212typedef uint64_t (*rte_bpf_jit_func4_t)(union rte_bpf_func_arg, union rte_bpf_func_arg,
213 union rte_bpf_func_arg, union rte_bpf_func_arg);
214typedef uint64_t (*rte_bpf_jit_func5_t)(union rte_bpf_func_arg, union rte_bpf_func_arg,
215 union rte_bpf_func_arg, union rte_bpf_func_arg, union rte_bpf_func_arg);
216
221 union {
222 void *raw;
223 rte_bpf_jit_func0_t func0; /* nullary function */
224 rte_bpf_jit_func1_t func1; /* unary function */
225 rte_bpf_jit_func2_t func2; /* binary function */
226 rte_bpf_jit_func3_t func3; /* ternary function */
227 rte_bpf_jit_func4_t func4; /* quaternary function */
228 rte_bpf_jit_func5_t func5; /* quinary function */
229 };
230 size_t sz;
231};
232
233/* Tuple of eBPF program arguments. */
234struct rte_bpf_prog_ctx {
235 union rte_bpf_func_arg arg[RTE_BPF_FUNC_MAX_ARGS];
236};
237
238struct rte_bpf;
239
246void
247rte_bpf_destroy(struct rte_bpf *bpf);
248
270__rte_experimental
271struct rte_bpf *
274
287struct rte_bpf *
288rte_bpf_load(const struct rte_bpf_prm *prm)
290
311struct rte_bpf *
312rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
313 const char *sname)
315
326uint64_t
327rte_bpf_exec(const struct rte_bpf *bpf, void *ctx);
328
346__rte_experimental
347uint64_t
348rte_bpf_exec_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx,
349 uint64_t flags);
350
365uint32_t
366rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],
367 uint32_t num);
368
391__rte_experimental
392uint32_t
393rte_bpf_exec_burst_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx,
394 uint64_t rc[], uint32_t num, uint64_t flags);
395
408int
409rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit);
410
426__rte_experimental
427int
428rte_bpf_get_jit_ex(const struct rte_bpf *bpf, struct rte_bpf_jit_ex *jit);
429
436__rte_experimental
437bool
438rte_bpf_insn_is_wide(const struct rte_ebpf_insn *ins);
439
459__rte_experimental
460int
461rte_bpf_format(char *buffer, size_t bufsz, const struct rte_ebpf_insn *ins,
462 uint32_t pc, uint32_t flags);
463
474void
475rte_bpf_dump(FILE *f, const struct rte_ebpf_insn *buf, uint32_t len);
476
477struct bpf_program;
478
493struct rte_bpf_prm *
494rte_bpf_convert(const struct bpf_program *prog)
496
497#ifdef __cplusplus
498}
499#endif
500
501#endif /* _RTE_BPF_H_ */
rte_bpf_origin
Definition: rte_bpf.h:128
@ RTE_BPF_ORIGIN_RAW
Definition: rte_bpf.h:129
@ RTE_BPF_ORIGIN_CBPF
Definition: rte_bpf.h:130
@ RTE_BPF_ORIGIN_ELF_MEMORY
Definition: rte_bpf.h:132
@ RTE_BPF_ORIGIN_ELF_FILE
Definition: rte_bpf.h:131
__rte_experimental struct rte_bpf struct rte_bpf * rte_bpf_load(const struct rte_bpf_prm *prm) __rte_malloc __rte_dealloc(rte_bpf_destroy
int rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit)
void rte_bpf_dump(FILE *f, const struct rte_ebpf_insn *buf, uint32_t len)
struct rte_bpf_prm * rte_bpf_convert(const struct bpf_program *prog) __rte_malloc __rte_dealloc_free
__rte_experimental int rte_bpf_get_jit_ex(const struct rte_bpf *bpf, struct rte_bpf_jit_ex *jit)
rte_bpf_xtype
Definition: rte_bpf.h:98
@ RTE_BPF_XTYPE_VAR
Definition: rte_bpf.h:100
@ RTE_BPF_XTYPE_FUNC
Definition: rte_bpf.h:99
__rte_experimental struct rte_bpf struct rte_bpf struct rte_bpf * rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname, const char *sname) __rte_malloc __rte_dealloc(rte_bpf_destroy
__rte_experimental struct rte_bpf struct rte_bpf struct rte_bpf uint64_t rte_bpf_exec(const struct rte_bpf *bpf, void *ctx)
__rte_experimental uint64_t rte_bpf_exec_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx, uint64_t flags)
rte_bpf_arg_type
Definition: rte_bpf.h:68
@ RTE_BPF_ARG_UNDEF
Definition: rte_bpf.h:69
@ RTE_BPF_ARG_PTR_MBUF
Definition: rte_bpf.h:72
@ RTE_BPF_ARG_PTR
Definition: rte_bpf.h:71
@ RTE_BPF_ARG_RAW
Definition: rte_bpf.h:70
@ RTE_BPF_ARG_RESERVED
Definition: rte_bpf.h:73
__rte_experimental int rte_bpf_format(char *buffer, size_t bufsz, const struct rte_ebpf_insn *ins, uint32_t pc, uint32_t flags)
__rte_experimental struct rte_bpf * rte_bpf_load_ex(const struct rte_bpf_prm_ex *prm) __rte_malloc __rte_dealloc(rte_bpf_destroy
__rte_experimental bool rte_bpf_insn_is_wide(const struct rte_ebpf_insn *ins)
uint32_t rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[], uint32_t num)
__rte_experimental uint32_t rte_bpf_exec_burst_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx, uint64_t rc[], uint32_t num, uint64_t flags)
void rte_bpf_destroy(struct rte_bpf *bpf)
#define __rte_dealloc(dealloc, argno)
Definition: rte_common.h:367
#define __rte_malloc
Definition: rte_common.h:356
#define __rte_dealloc_free
Definition: rte_malloc.h:37
size_t size
Definition: rte_bpf.h:85
size_t buf_size
Definition: rte_bpf.h:86
uint64_t(* func)(void *)
Definition: rte_bpf.h:198
size_t sz
Definition: rte_bpf.h:199
const struct bpf_insn * ins
Definition: rte_bpf.h:157
const void * data
Definition: rte_bpf.h:165
struct rte_bpf_arg prog_arg[RTE_BPF_FUNC_MAX_ARGS]
Definition: rte_bpf.h:175
enum rte_bpf_origin origin
Definition: rte_bpf.h:148
const char * path
Definition: rte_bpf.h:161
const struct rte_ebpf_insn * ins
Definition: rte_bpf.h:153
const char * section
Definition: rte_bpf.h:162
uint32_t flags
Definition: rte_bpf.h:146
size_t size
Definition: rte_bpf.h:166
const struct rte_bpf_xsym * xsym
Definition: rte_bpf.h:171
uint32_t nb_prog_arg
Definition: rte_bpf.h:176
size_t sz
Definition: rte_bpf.h:144
uint32_t nb_ins
Definition: rte_bpf.h:154
uint32_t nb_xsym
Definition: rte_bpf.h:173
struct rte_bpf_arg prog_arg
Definition: rte_bpf.h:191
const struct rte_ebpf_insn * ins
Definition: rte_bpf.h:186
const struct rte_bpf_xsym * xsym
Definition: rte_bpf.h:188
uint32_t nb_ins
Definition: rte_bpf.h:187
uint32_t nb_xsym
Definition: rte_bpf.h:190
struct rte_bpf_xsym::@82::@85 var
struct rte_bpf_arg ret
Definition: rte_bpf.h:116
struct rte_bpf_arg args[RTE_BPF_FUNC_MAX_ARGS]
Definition: rte_bpf.h:114
const char * name
Definition: rte_bpf.h:107
enum rte_bpf_xtype type
Definition: rte_bpf.h:108
void * val
Definition: rte_bpf.h:119
struct rte_bpf_arg desc
Definition: rte_bpf.h:120