bpf: add BPF loading and execution framework
[dpdk.git] / lib / librte_bpf / rte_bpf.h
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
8 /**
9  * @file
10  *
11  * RTE BPF support.
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).
16  */
17
18 #include <rte_common.h>
19 #include <rte_mbuf.h>
20 #include <bpf_def.h>
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 /**
27  * Possible types for function/BPF program arguments.
28  */
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,
35 };
36
37 /**
38  * function argument information
39  */
40 struct rte_bpf_arg {
41         enum rte_bpf_arg_type type;
42         size_t size;     /**< for pointer types, size of data it points to */
43         size_t buf_size;
44         /**< for mbuf ptr type, max size of rte_mbuf data buffer */
45 };
46
47 /**
48  * determine is argument a pointer
49  */
50 #define RTE_BPF_ARG_PTR_TYPE(x) ((x) & RTE_BPF_ARG_PTR)
51
52 /**
53  * Possible types for external symbols.
54  */
55 enum rte_bpf_xtype {
56         RTE_BPF_XTYPE_FUNC, /**< function */
57         RTE_BPF_XTYPE_VAR,  /**< variable */
58         RTE_BPF_XTYPE_NUM
59 };
60
61 /**
62  * Definition for external symbols available in the BPF program.
63  */
64 struct rte_bpf_xsym {
65         const char *name;        /**< name */
66         enum rte_bpf_xtype type; /**< type */
67         union {
68                 uint64_t (*func)(uint64_t, uint64_t, uint64_t,
69                                 uint64_t, uint64_t);
70                 void *var;
71         }; /**< value */
72 };
73
74 /**
75  * Input parameters for loading eBPF code.
76  */
77 struct rte_bpf_prm {
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 */
84 };
85
86 /**
87  * Information about compiled into native ISA eBPF code.
88  */
89 struct rte_bpf_jit {
90         uint64_t (*func)(void *); /**< JIT-ed native code */
91         size_t sz;                /**< size of JIT-ed code */
92 };
93
94 struct rte_bpf;
95
96 /**
97  * De-allocate all memory used by this eBPF execution context.
98  *
99  * @param bpf
100  *   BPF handle to destroy.
101  */
102 void rte_bpf_destroy(struct rte_bpf *bpf);
103
104 /**
105  * Create a new eBPF execution context and load given BPF code into it.
106  *
107  * @param prm
108  *  Parameters used to create and initialise the BPF exeution context.
109  * @return
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
115  */
116 struct rte_bpf *rte_bpf_load(const struct rte_bpf_prm *prm);
117
118 /**
119  * Execute given BPF bytecode.
120  *
121  * @param bpf
122  *   handle for the BPF code to execute.
123  * @param ctx
124  *   pointer to input context.
125  * @return
126  *   BPF execution return value.
127  */
128 uint64_t rte_bpf_exec(const struct rte_bpf *bpf, void *ctx);
129
130 /**
131  * Execute given BPF bytecode over a set of input contexts.
132  *
133  * @param bpf
134  *   handle for the BPF code to execute.
135  * @param ctx
136  *   array of pointers to the input contexts.
137  * @param rc
138  *   array of return values (one per input).
139  * @param num
140  *   number of elements in ctx[] (and rc[]).
141  * @return
142  *   number of successfully processed inputs.
143  */
144 uint32_t rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[],
145         uint64_t rc[], uint32_t num);
146
147 /**
148  * Provide information about natively compield code for given BPF handle.
149  *
150  * @param bpf
151  *   handle for the BPF code.
152  * @param jit
153  *   pointer to the rte_bpf_jit structure to be filled with related data.
154  * @return
155  *   - -EINVAL if the parameters are invalid.
156  *   - Zero if operation completed successfully.
157  */
158 int rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit);
159
160 #ifdef __cplusplus
161 }
162 #endif
163
164 #endif /* _RTE_BPF_H_ */