DPDK  26.07.0
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 #include <bpf_def.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define RTE_BPF_EXEC_FLAG_JIT RTE_BIT64(0)
31 #define RTE_BPF_EXEC_FLAG_MASK RTE_BPF_EXEC_FLAG_JIT
32 
33 /* Format instructions as assembler. */
34 #define RTE_BPF_FORMAT_FLAG_DISASSEMBLY 0
35 /* Format instructions as hexadecimal. */
36 #define RTE_BPF_FORMAT_FLAG_HEXADECIMAL RTE_BIT32(0)
37 
38 /* Only valid in disassembly mode. */
39 /* Format jump offsets relative to the next instruction. */
40 #define RTE_BPF_FORMAT_FLAG_RELATIVE_JUMPS 0
41 /* Format jump targets relative to the start of the program. */
42 #define RTE_BPF_FORMAT_FLAG_ABSOLUTE_JUMPS RTE_BIT32(1)
43 
44 /* Only valid in hexadecimal mode. */
45 /* Format full hexadecimal representation of wide instructions. */
46 #define RTE_BPF_FORMAT_FLAG_AUTO_WIDE 0
47 /* Format as hexadecimal only first half of wide instructions. */
48 #define RTE_BPF_FORMAT_FLAG_NEVER_WIDE RTE_BIT32(2)
49 
56  RTE_BPF_ARG_PTR = 0x10,
59 };
60 
64 struct rte_bpf_arg {
65  enum rte_bpf_arg_type type;
70  size_t size;
71  size_t buf_size;
73 };
74 
78 #define RTE_BPF_ARG_PTR_TYPE(x) ((x) & RTE_BPF_ARG_PTR)
79 
86 };
87 
91 struct rte_bpf_xsym {
92  const char *name;
94  union {
95  struct {
96  uint64_t (*val)(uint64_t, uint64_t, uint64_t,
97  uint64_t, uint64_t);
98  uint32_t nb_args;
99  struct rte_bpf_arg args[EBPF_FUNC_MAX_ARGS];
101  struct rte_bpf_arg ret;
102  } func;
103  struct {
104  void *val;
105  struct rte_bpf_arg desc;
106  } var;
107  };
108 };
109 
118 };
119 
120 struct bpf_insn;
121 struct rte_bpf_validate_debug;
122 
129  size_t sz;
131  uint32_t flags;
136  union {
137  struct {
138  const struct ebpf_insn *ins;
139  uint32_t nb_ins;
140  } raw;
141  struct {
142  const struct bpf_insn *ins;
143  uint32_t nb_ins;
144  } cbpf;
145  struct {
146  const char *path;
147  const char *section;
148  } elf_file;
149  struct {
150  const void *data;
151  size_t size;
152  const char *section;
153  } elf_memory;
154  };
155 
156  const struct rte_bpf_xsym *xsym;
158  uint32_t nb_xsym;
160  struct rte_bpf_arg prog_arg[EBPF_FUNC_MAX_ARGS];
161  uint32_t nb_prog_arg;
163  /* Validate debug instance. */
164  struct rte_bpf_validate_debug *debug;
165 };
166 
170 struct rte_bpf_prm {
171  const struct ebpf_insn *ins;
172  uint32_t nb_ins;
173  const struct rte_bpf_xsym *xsym;
175  uint32_t nb_xsym;
177 };
178 
182 struct rte_bpf_jit {
183  uint64_t (*func)(void *);
184  size_t sz;
185 };
186 
187 union rte_bpf_func_arg {
188  uint64_t u64;
189  void *ptr;
190 };
191 
192 typedef uint64_t (*rte_bpf_jit_func0_t)(void);
193 typedef uint64_t (*rte_bpf_jit_func1_t)(union rte_bpf_func_arg);
194 typedef uint64_t (*rte_bpf_jit_func2_t)(union rte_bpf_func_arg, union rte_bpf_func_arg);
195 typedef uint64_t (*rte_bpf_jit_func3_t)(union rte_bpf_func_arg, union rte_bpf_func_arg,
196  union rte_bpf_func_arg);
197 typedef uint64_t (*rte_bpf_jit_func4_t)(union rte_bpf_func_arg, union rte_bpf_func_arg,
198  union rte_bpf_func_arg, union rte_bpf_func_arg);
199 typedef uint64_t (*rte_bpf_jit_func5_t)(union rte_bpf_func_arg, union rte_bpf_func_arg,
200  union rte_bpf_func_arg, union rte_bpf_func_arg, union rte_bpf_func_arg);
201 
206  union {
207  void *raw;
208  rte_bpf_jit_func0_t func0; /* nullary function */
209  rte_bpf_jit_func1_t func1; /* unary function */
210  rte_bpf_jit_func2_t func2; /* binary function */
211  rte_bpf_jit_func3_t func3; /* ternary function */
212  rte_bpf_jit_func4_t func4; /* quaternary function */
213  rte_bpf_jit_func5_t func5; /* quinary function */
214  };
215  size_t sz;
216 };
217 
218 /* Tuple of eBPF program arguments. */
219 struct rte_bpf_prog_ctx {
220  union rte_bpf_func_arg arg[EBPF_FUNC_MAX_ARGS];
221 };
222 
223 struct rte_bpf;
224 
231 void
232 rte_bpf_destroy(struct rte_bpf *bpf);
233 
255 __rte_experimental
256 struct rte_bpf *
257 rte_bpf_load_ex(const struct rte_bpf_prm_ex *prm)
259 
272 struct rte_bpf *
273 rte_bpf_load(const struct rte_bpf_prm *prm)
275 
296 struct rte_bpf *
297 rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
298  const char *sname)
300 
311 uint64_t
312 rte_bpf_exec(const struct rte_bpf *bpf, void *ctx);
313 
331 __rte_experimental
332 uint64_t
333 rte_bpf_exec_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx,
334  uint64_t flags);
335 
350 uint32_t
351 rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],
352  uint32_t num);
353 
376 __rte_experimental
377 uint32_t
378 rte_bpf_exec_burst_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx,
379  uint64_t rc[], uint32_t num, uint64_t flags);
380 
393 int
394 rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit);
395 
411 __rte_experimental
412 int
413 rte_bpf_get_jit_ex(const struct rte_bpf *bpf, struct rte_bpf_jit_ex *jit);
414 
421 __rte_experimental
422 bool
423 rte_bpf_insn_is_wide(const struct ebpf_insn *ins);
424 
444 __rte_experimental
445 int
446 rte_bpf_format(char *buffer, size_t bufsz, const struct ebpf_insn *ins,
447  uint32_t pc, uint32_t flags);
448 
459 void
460 rte_bpf_dump(FILE *f, const struct ebpf_insn *buf, uint32_t len);
461 
462 struct bpf_program;
463 
478 struct rte_bpf_prm *
479 rte_bpf_convert(const struct bpf_program *prog)
481 
482 #ifdef __cplusplus
483 }
484 #endif
485 
486 #endif /* _RTE_BPF_H_ */
size_t buf_size
Definition: rte_bpf.h:71
__rte_experimental int rte_bpf_format(char *buffer, size_t bufsz, const struct ebpf_insn *ins, uint32_t pc, uint32_t flags)
rte_bpf_origin
Definition: rte_bpf.h:113
struct rte_bpf_arg desc
Definition: rte_bpf.h:105
rte_bpf_arg_type
Definition: rte_bpf.h:53
#define __rte_dealloc_free
Definition: rte_malloc.h:37
uint32_t nb_prog_arg
Definition: rte_bpf.h:161
__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 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)
uint32_t nb_ins
Definition: rte_bpf.h:172
rte_bpf_xtype
Definition: rte_bpf.h:83
struct rte_bpf_arg args[EBPF_FUNC_MAX_ARGS]
Definition: rte_bpf.h:99
const char * section
Definition: rte_bpf.h:147
size_t size
Definition: rte_bpf.h:151
struct rte_bpf_xsym::@70::@73 var
const struct ebpf_insn * ins
Definition: rte_bpf.h:138
void rte_bpf_dump(FILE *f, const struct ebpf_insn *buf, uint32_t len)
const struct bpf_insn * ins
Definition: rte_bpf.h:142
const struct ebpf_insn * ins
Definition: rte_bpf.h:171
uint32_t flags
Definition: rte_bpf.h:131
int rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit)
size_t sz
Definition: rte_bpf.h:129
size_t sz
Definition: rte_bpf.h:184
const struct rte_bpf_xsym * xsym
Definition: rte_bpf.h:156
#define __rte_dealloc(dealloc, argno)
Definition: rte_common.h:339
struct rte_bpf_arg ret
Definition: rte_bpf.h:101
const char * name
Definition: rte_bpf.h:92
__rte_experimental uint64_t rte_bpf_exec_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx, uint64_t flags)
#define __rte_malloc
Definition: rte_common.h:328
size_t size
Definition: rte_bpf.h:70
struct rte_bpf_prm * rte_bpf_convert(const struct bpf_program *prog) __rte_malloc __rte_dealloc_free
__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 ebpf_insn *ins)
uint32_t nb_ins
Definition: rte_bpf.h:139
enum rte_bpf_xtype type
Definition: rte_bpf.h:93
uint32_t nb_xsym
Definition: rte_bpf.h:175
uint32_t rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[], uint32_t num)
uint64_t(* func)(void *)
Definition: rte_bpf.h:183
const void * data
Definition: rte_bpf.h:150
const char * path
Definition: rte_bpf.h:146
__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 int rte_bpf_get_jit_ex(const struct rte_bpf *bpf, struct rte_bpf_jit_ex *jit)
struct rte_bpf_arg prog_arg
Definition: rte_bpf.h:176
void rte_bpf_destroy(struct rte_bpf *bpf)
const struct rte_bpf_xsym * xsym
Definition: rte_bpf.h:173
void * val
Definition: rte_bpf.h:104
uint32_t nb_xsym
Definition: rte_bpf.h:158
__rte_experimental struct rte_bpf struct rte_bpf * rte_bpf_load(const struct rte_bpf_prm *prm) __rte_malloc __rte_dealloc(rte_bpf_destroy
enum rte_bpf_origin origin
Definition: rte_bpf.h:133
struct rte_bpf_arg prog_arg[EBPF_FUNC_MAX_ARGS]
Definition: rte_bpf.h:160