1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
15 * All functions in this file may be changed or removed without prior notice.
17 * librte_bpf provides a framework to load and execute eBPF bytecode
18 * inside user-space dpdk based applications.
19 * It supports basic set of features from eBPF spec
20 * (https://www.kernel.org/doc/Documentation/networking/filter.txt).
23 #include <rte_common.h>
32 * Possible types for function/BPF program arguments.
34 enum rte_bpf_arg_type {
35 RTE_BPF_ARG_UNDEF, /**< undefined */
36 RTE_BPF_ARG_RAW, /**< scalar value */
37 RTE_BPF_ARG_PTR = 0x10, /**< pointer to data buffer */
38 RTE_BPF_ARG_PTR_MBUF, /**< pointer to rte_mbuf */
39 RTE_BPF_ARG_RESERVED, /**< reserved for internal use */
43 * function argument information
46 enum rte_bpf_arg_type type;
48 * for ptr type - max size of data buffer it points to
49 * for raw type - the size (in bytes) of the value
53 /**< for mbuf ptr type, max size of rte_mbuf data buffer */
57 * determine is argument a pointer
59 #define RTE_BPF_ARG_PTR_TYPE(x) ((x) & RTE_BPF_ARG_PTR)
62 * Possible types for external symbols.
65 RTE_BPF_XTYPE_FUNC, /**< function */
66 RTE_BPF_XTYPE_VAR, /**< variable */
71 * Definition for external symbols available in the BPF program.
74 const char *name; /**< name */
75 enum rte_bpf_xtype type; /**< type */
78 uint64_t (*val)(uint64_t, uint64_t, uint64_t,
81 struct rte_bpf_arg args[EBPF_FUNC_MAX_ARGS];
82 /**< Function arguments descriptions. */
83 struct rte_bpf_arg ret; /**< function return value. */
86 void *val; /**< actual memory location */
87 struct rte_bpf_arg desc; /**< type, size, etc. */
88 } var; /**< external variable */
93 * Input parameters for loading eBPF code.
96 const struct ebpf_insn *ins; /**< array of eBPF instructions */
97 uint32_t nb_ins; /**< number of instructions in ins */
98 const struct rte_bpf_xsym *xsym;
99 /**< array of external symbols that eBPF code is allowed to reference */
100 uint32_t nb_xsym; /**< number of elements in xsym */
101 struct rte_bpf_arg prog_arg; /**< eBPF program input arg description */
105 * Information about compiled into native ISA eBPF code.
108 uint64_t (*func)(void *); /**< JIT-ed native code */
109 size_t sz; /**< size of JIT-ed code */
115 * De-allocate all memory used by this eBPF execution context.
118 * BPF handle to destroy.
122 rte_bpf_destroy(struct rte_bpf *bpf);
125 * Create a new eBPF execution context and load given BPF code into it.
128 * Parameters used to create and initialise the BPF execution context.
130 * BPF handle that is used in future BPF operations,
131 * or NULL on error, with error code set in rte_errno.
132 * Possible rte_errno errors include:
133 * - EINVAL - invalid parameter passed to function
134 * - ENOMEM - can't reserve enough memory
138 rte_bpf_load(const struct rte_bpf_prm *prm);
141 * Create a new eBPF execution context and load BPF code from given ELF
143 * Note that if the function will encounter EBPF_PSEUDO_CALL instruction
144 * that references external symbol, it will treat is as standard BPF_CALL
145 * to the external helper function.
148 * Parameters used to create and initialise the BPF execution context.
150 * Pathname for a ELF file.
152 * Name of the executable section within the file to load.
154 * BPF handle that is used in future BPF operations,
155 * or NULL on error, with error code set in rte_errno.
156 * Possible rte_errno errors include:
157 * - EINVAL - invalid parameter passed to function
158 * - ENOMEM - can't reserve enough memory
162 rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
165 * Execute given BPF bytecode.
168 * handle for the BPF code to execute.
170 * pointer to input context.
172 * BPF execution return value.
176 rte_bpf_exec(const struct rte_bpf *bpf, void *ctx);
179 * Execute given BPF bytecode over a set of input contexts.
182 * handle for the BPF code to execute.
184 * array of pointers to the input contexts.
186 * array of return values (one per input).
188 * number of elements in ctx[] (and rc[]).
190 * number of successfully processed inputs.
194 rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],
198 * Provide information about natively compiled code for given BPF handle.
201 * handle for the BPF code.
203 * pointer to the rte_bpf_jit structure to be filled with related data.
205 * - -EINVAL if the parameters are invalid.
206 * - Zero if operation completed successfully.
210 rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit);
216 #endif /* _RTE_BPF_H_ */