1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
10 * @b EXPERIMENTAL: this API may change without prior notice
13 * librte_bpf provides a framework to load and execute eBPF bytecode
14 * inside user-space dpdk based applications.
15 * It supports basic set of features from eBPF spec
16 * (https://www.kernel.org/doc/Documentation/networking/filter.txt).
19 #include <rte_common.h>
28 * Possible types for function/BPF program arguments.
30 enum rte_bpf_arg_type {
31 RTE_BPF_ARG_UNDEF, /**< undefined */
32 RTE_BPF_ARG_RAW, /**< scalar value */
33 RTE_BPF_ARG_PTR = 0x10, /**< pointer to data buffer */
34 RTE_BPF_ARG_PTR_MBUF, /**< pointer to rte_mbuf */
35 RTE_BPF_ARG_PTR_STACK,
39 * function argument information
42 enum rte_bpf_arg_type type;
44 * for ptr type - max size of data buffer it points to
45 * for raw type - the size (in bytes) of the value
49 /**< for mbuf ptr type, max size of rte_mbuf data buffer */
53 * determine is argument a pointer
55 #define RTE_BPF_ARG_PTR_TYPE(x) ((x) & RTE_BPF_ARG_PTR)
58 * Possible types for external symbols.
61 RTE_BPF_XTYPE_FUNC, /**< function */
62 RTE_BPF_XTYPE_VAR, /**< variable */
67 * Definition for external symbols available in the BPF program.
70 const char *name; /**< name */
71 enum rte_bpf_xtype type; /**< type */
74 uint64_t (*val)(uint64_t, uint64_t, uint64_t,
77 struct rte_bpf_arg args[EBPF_FUNC_MAX_ARGS];
78 /**< Function arguments descriptions. */
79 struct rte_bpf_arg ret; /**< function return value. */
82 void *val; /**< actual memory location */
83 struct rte_bpf_arg desc; /**< type, size, etc. */
84 } var; /**< external variable */
89 * Input parameters for loading eBPF code.
92 const struct ebpf_insn *ins; /**< array of eBPF instructions */
93 uint32_t nb_ins; /**< number of instructions in ins */
94 const struct rte_bpf_xsym *xsym;
95 /**< array of external symbols that eBPF code is allowed to reference */
96 uint32_t nb_xsym; /**< number of elements in xsym */
97 struct rte_bpf_arg prog_arg; /**< eBPF program input arg description */
101 * Information about compiled into native ISA eBPF code.
104 uint64_t (*func)(void *); /**< JIT-ed native code */
105 size_t sz; /**< size of JIT-ed code */
111 * De-allocate all memory used by this eBPF execution context.
114 * BPF handle to destroy.
116 void __rte_experimental
117 rte_bpf_destroy(struct rte_bpf *bpf);
120 * Create a new eBPF execution context and load given BPF code into it.
123 * Parameters used to create and initialise the BPF execution context.
125 * BPF handle that is used in future BPF operations,
126 * or NULL on error, with error code set in rte_errno.
127 * Possible rte_errno errors include:
128 * - EINVAL - invalid parameter passed to function
129 * - ENOMEM - can't reserve enough memory
131 struct rte_bpf * __rte_experimental
132 rte_bpf_load(const struct rte_bpf_prm *prm);
135 * Create a new eBPF execution context and load BPF code from given ELF
137 * Note that if the function will encounter EBPF_PSEUDO_CALL instruction
138 * that references external symbol, it will treat is as standard BPF_CALL
139 * to the external helper function.
142 * Parameters used to create and initialise the BPF execution context.
144 * Pathname for a ELF file.
146 * Name of the executable section within the file to load.
148 * BPF handle that is used in future BPF operations,
149 * or NULL on error, with error code set in rte_errno.
150 * Possible rte_errno errors include:
151 * - EINVAL - invalid parameter passed to function
152 * - ENOMEM - can't reserve enough memory
154 struct rte_bpf * __rte_experimental
155 rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
158 * Execute given BPF bytecode.
161 * handle for the BPF code to execute.
163 * pointer to input context.
165 * BPF execution return value.
167 uint64_t __rte_experimental
168 rte_bpf_exec(const struct rte_bpf *bpf, void *ctx);
171 * Execute given BPF bytecode over a set of input contexts.
174 * handle for the BPF code to execute.
176 * array of pointers to the input contexts.
178 * array of return values (one per input).
180 * number of elements in ctx[] (and rc[]).
182 * number of successfully processed inputs.
184 uint32_t __rte_experimental
185 rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],
189 * Provide information about natively compiled code for given BPF handle.
192 * handle for the BPF code.
194 * pointer to the rte_bpf_jit structure to be filled with related data.
196 * - -EINVAL if the parameters are invalid.
197 * - Zero if operation completed successfully.
199 int __rte_experimental
200 rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit);
206 #endif /* _RTE_BPF_H_ */