1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
12 * librte_bpf provides a framework to load and execute eBPF bytecode
13 * inside user-space dpdk based applications.
14 * It supports basic set of features from eBPF spec
15 * (https://www.kernel.org/doc/Documentation/networking/filter.txt).
18 #include <rte_common.h>
27 * Possible types for function/BPF program arguments.
29 enum rte_bpf_arg_type {
30 RTE_BPF_ARG_UNDEF, /**< undefined */
31 RTE_BPF_ARG_RAW, /**< scalar value */
32 RTE_BPF_ARG_PTR = 0x10, /**< pointer to data buffer */
33 RTE_BPF_ARG_PTR_MBUF, /**< pointer to rte_mbuf */
34 RTE_BPF_ARG_PTR_STACK,
38 * function argument information
41 enum rte_bpf_arg_type type;
42 size_t size; /**< for pointer types, size of data it points to */
44 /**< for mbuf ptr type, max size of rte_mbuf data buffer */
48 * determine is argument a pointer
50 #define RTE_BPF_ARG_PTR_TYPE(x) ((x) & RTE_BPF_ARG_PTR)
53 * Possible types for external symbols.
56 RTE_BPF_XTYPE_FUNC, /**< function */
57 RTE_BPF_XTYPE_VAR, /**< variable */
62 * Definition for external symbols available in the BPF program.
65 const char *name; /**< name */
66 enum rte_bpf_xtype type; /**< type */
68 uint64_t (*func)(uint64_t, uint64_t, uint64_t,
75 * Input parameters for loading eBPF code.
78 const struct ebpf_insn *ins; /**< array of eBPF instructions */
79 uint32_t nb_ins; /**< number of instructions in ins */
80 const struct rte_bpf_xsym *xsym;
81 /**< array of external symbols that eBPF code is allowed to reference */
82 uint32_t nb_xsym; /**< number of elements in xsym */
83 struct rte_bpf_arg prog_arg; /**< eBPF program input arg description */
87 * Information about compiled into native ISA eBPF code.
90 uint64_t (*func)(void *); /**< JIT-ed native code */
91 size_t sz; /**< size of JIT-ed code */
97 * De-allocate all memory used by this eBPF execution context.
100 * BPF handle to destroy.
102 void rte_bpf_destroy(struct rte_bpf *bpf);
105 * Create a new eBPF execution context and load given BPF code into it.
108 * Parameters used to create and initialise the BPF exeution context.
110 * BPF handle that is used in future BPF operations,
111 * or NULL on error, with error code set in rte_errno.
112 * Possible rte_errno errors include:
113 * - EINVAL - invalid parameter passed to function
114 * - ENOMEM - can't reserve enough memory
116 struct rte_bpf *rte_bpf_load(const struct rte_bpf_prm *prm);
119 * Create a new eBPF execution context and load BPF code from given ELF
123 * Parameters used to create and initialise the BPF exeution context.
125 * Pathname for a ELF file.
127 * Name of the executable section within the file to load.
129 * BPF handle that is used in future BPF operations,
130 * or NULL on error, with error code set in rte_errno.
131 * Possible rte_errno errors include:
132 * - EINVAL - invalid parameter passed to function
133 * - ENOMEM - can't reserve enough memory
135 struct rte_bpf *rte_bpf_elf_load(const struct rte_bpf_prm *prm,
136 const char *fname, const char *sname);
139 * Execute given BPF bytecode.
142 * handle for the BPF code to execute.
144 * pointer to input context.
146 * BPF execution return value.
148 uint64_t rte_bpf_exec(const struct rte_bpf *bpf, void *ctx);
151 * Execute given BPF bytecode over a set of input contexts.
154 * handle for the BPF code to execute.
156 * array of pointers to the input contexts.
158 * array of return values (one per input).
160 * number of elements in ctx[] (and rc[]).
162 * number of successfully processed inputs.
164 uint32_t rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[],
165 uint64_t rc[], uint32_t num);
168 * Provide information about natively compield code for given BPF handle.
171 * handle for the BPF code.
173 * pointer to the rte_bpf_jit structure to be filled with related data.
175 * - -EINVAL if the parameters are invalid.
176 * - Zero if operation completed successfully.
178 int rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit);
184 #endif /* _RTE_BPF_H_ */