xref: /dpdk/lib/bpf/rte_bpf.h (revision e2ca8ea4cd1cbcd52874c2350422323a250b8cac)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2018 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_BPF_H_
699a2dd95SBruce Richardson #define _RTE_BPF_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file rte_bpf.h
1099a2dd95SBruce Richardson  *
1199a2dd95SBruce Richardson  * RTE BPF support.
1299a2dd95SBruce Richardson  *
1399a2dd95SBruce Richardson  * librte_bpf provides a framework to load and execute eBPF bytecode
1499a2dd95SBruce Richardson  * inside user-space dpdk based applications.
1599a2dd95SBruce Richardson  * It supports basic set of features from eBPF spec
1699a2dd95SBruce Richardson  * (https://www.kernel.org/doc/Documentation/networking/filter.txt).
1799a2dd95SBruce Richardson  */
1899a2dd95SBruce Richardson 
1999a2dd95SBruce Richardson #include <rte_common.h>
2099a2dd95SBruce Richardson #include <rte_mbuf.h>
2199a2dd95SBruce Richardson #include <bpf_def.h>
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson #ifdef __cplusplus
2499a2dd95SBruce Richardson extern "C" {
2599a2dd95SBruce Richardson #endif
2699a2dd95SBruce Richardson 
2799a2dd95SBruce Richardson /**
2899a2dd95SBruce Richardson  * Possible types for function/BPF program arguments.
2999a2dd95SBruce Richardson  */
3099a2dd95SBruce Richardson enum rte_bpf_arg_type {
3199a2dd95SBruce Richardson 	RTE_BPF_ARG_UNDEF,      /**< undefined */
3299a2dd95SBruce Richardson 	RTE_BPF_ARG_RAW,        /**< scalar value */
3399a2dd95SBruce Richardson 	RTE_BPF_ARG_PTR = 0x10, /**< pointer to data buffer */
3499a2dd95SBruce Richardson 	RTE_BPF_ARG_PTR_MBUF,   /**< pointer to rte_mbuf */
3599a2dd95SBruce Richardson 	RTE_BPF_ARG_RESERVED    /**< reserved for internal use */
3699a2dd95SBruce Richardson };
3799a2dd95SBruce Richardson 
3899a2dd95SBruce Richardson /**
3999a2dd95SBruce Richardson  * function argument information
4099a2dd95SBruce Richardson  */
4199a2dd95SBruce Richardson struct rte_bpf_arg {
4299a2dd95SBruce Richardson 	enum rte_bpf_arg_type type;
4399a2dd95SBruce Richardson 	/**
4499a2dd95SBruce Richardson 	 * for ptr type - max size of data buffer it points to
4599a2dd95SBruce Richardson 	 * for raw type - the size (in bytes) of the value
4699a2dd95SBruce Richardson 	 */
4799a2dd95SBruce Richardson 	size_t size;
4899a2dd95SBruce Richardson 	size_t buf_size;
4999a2dd95SBruce Richardson 	/**< for mbuf ptr type, max size of rte_mbuf data buffer */
5099a2dd95SBruce Richardson };
5199a2dd95SBruce Richardson 
5299a2dd95SBruce Richardson /**
5399a2dd95SBruce Richardson  * determine is argument a pointer
5499a2dd95SBruce Richardson  */
5599a2dd95SBruce Richardson #define RTE_BPF_ARG_PTR_TYPE(x)	((x) & RTE_BPF_ARG_PTR)
5699a2dd95SBruce Richardson 
5799a2dd95SBruce Richardson /**
5899a2dd95SBruce Richardson  * Possible types for external symbols.
5999a2dd95SBruce Richardson  */
6099a2dd95SBruce Richardson enum rte_bpf_xtype {
6199a2dd95SBruce Richardson 	RTE_BPF_XTYPE_FUNC, /**< function */
6299a2dd95SBruce Richardson 	RTE_BPF_XTYPE_VAR   /**< variable */
6399a2dd95SBruce Richardson };
6499a2dd95SBruce Richardson 
6599a2dd95SBruce Richardson /**
6699a2dd95SBruce Richardson  * Definition for external symbols available in the BPF program.
6799a2dd95SBruce Richardson  */
6899a2dd95SBruce Richardson struct rte_bpf_xsym {
6999a2dd95SBruce Richardson 	const char *name;        /**< name */
7099a2dd95SBruce Richardson 	enum rte_bpf_xtype type; /**< type */
7199a2dd95SBruce Richardson 	union {
7299a2dd95SBruce Richardson 		struct {
7399a2dd95SBruce Richardson 			uint64_t (*val)(uint64_t, uint64_t, uint64_t,
7499a2dd95SBruce Richardson 				uint64_t, uint64_t);
7599a2dd95SBruce Richardson 			uint32_t nb_args;
7699a2dd95SBruce Richardson 			struct rte_bpf_arg args[EBPF_FUNC_MAX_ARGS];
7799a2dd95SBruce Richardson 			/**< Function arguments descriptions. */
7899a2dd95SBruce Richardson 			struct rte_bpf_arg ret; /**< function return value. */
7999a2dd95SBruce Richardson 		} func;
8099a2dd95SBruce Richardson 		struct {
8199a2dd95SBruce Richardson 			void *val; /**< actual memory location */
8299a2dd95SBruce Richardson 			struct rte_bpf_arg desc; /**< type, size, etc. */
8399a2dd95SBruce Richardson 		} var; /**< external variable */
8499a2dd95SBruce Richardson 	};
8599a2dd95SBruce Richardson };
8699a2dd95SBruce Richardson 
8799a2dd95SBruce Richardson /**
8899a2dd95SBruce Richardson  * Input parameters for loading eBPF code.
8999a2dd95SBruce Richardson  */
9099a2dd95SBruce Richardson struct rte_bpf_prm {
9199a2dd95SBruce Richardson 	const struct ebpf_insn *ins; /**< array of eBPF instructions */
9299a2dd95SBruce Richardson 	uint32_t nb_ins;            /**< number of instructions in ins */
9399a2dd95SBruce Richardson 	const struct rte_bpf_xsym *xsym;
9499a2dd95SBruce Richardson 	/**< array of external symbols that eBPF code is allowed to reference */
9599a2dd95SBruce Richardson 	uint32_t nb_xsym; /**< number of elements in xsym */
9699a2dd95SBruce Richardson 	struct rte_bpf_arg prog_arg; /**< eBPF program input arg description */
9799a2dd95SBruce Richardson };
9899a2dd95SBruce Richardson 
9999a2dd95SBruce Richardson /**
10099a2dd95SBruce Richardson  * Information about compiled into native ISA eBPF code.
10199a2dd95SBruce Richardson  */
10299a2dd95SBruce Richardson struct rte_bpf_jit {
10399a2dd95SBruce Richardson 	uint64_t (*func)(void *); /**< JIT-ed native code */
10499a2dd95SBruce Richardson 	size_t sz;                /**< size of JIT-ed code */
10599a2dd95SBruce Richardson };
10699a2dd95SBruce Richardson 
10799a2dd95SBruce Richardson struct rte_bpf;
10899a2dd95SBruce Richardson 
10999a2dd95SBruce Richardson /**
11099a2dd95SBruce Richardson  * De-allocate all memory used by this eBPF execution context.
11199a2dd95SBruce Richardson  *
11299a2dd95SBruce Richardson  * @param bpf
11399a2dd95SBruce Richardson  *   BPF handle to destroy.
11499a2dd95SBruce Richardson  */
11599a2dd95SBruce Richardson void
11699a2dd95SBruce Richardson rte_bpf_destroy(struct rte_bpf *bpf);
11799a2dd95SBruce Richardson 
11899a2dd95SBruce Richardson /**
11999a2dd95SBruce Richardson  * Create a new eBPF execution context and load given BPF code into it.
12099a2dd95SBruce Richardson  *
12199a2dd95SBruce Richardson  * @param prm
12299a2dd95SBruce Richardson  *  Parameters used to create and initialise the BPF execution context.
12399a2dd95SBruce Richardson  * @return
12499a2dd95SBruce Richardson  *   BPF handle that is used in future BPF operations,
12599a2dd95SBruce Richardson  *   or NULL on error, with error code set in rte_errno.
12699a2dd95SBruce Richardson  *   Possible rte_errno errors include:
12799a2dd95SBruce Richardson  *   - EINVAL - invalid parameter passed to function
12899a2dd95SBruce Richardson  *   - ENOMEM - can't reserve enough memory
12999a2dd95SBruce Richardson  */
13099a2dd95SBruce Richardson struct rte_bpf *
13199a2dd95SBruce Richardson rte_bpf_load(const struct rte_bpf_prm *prm);
13299a2dd95SBruce Richardson 
13399a2dd95SBruce Richardson /**
13499a2dd95SBruce Richardson  * Create a new eBPF execution context and load BPF code from given ELF
13599a2dd95SBruce Richardson  * file into it.
13699a2dd95SBruce Richardson  * Note that if the function will encounter EBPF_PSEUDO_CALL instruction
13799a2dd95SBruce Richardson  * that references external symbol, it will treat is as standard BPF_CALL
13899a2dd95SBruce Richardson  * to the external helper function.
13999a2dd95SBruce Richardson  *
14099a2dd95SBruce Richardson  * @param prm
14199a2dd95SBruce Richardson  *  Parameters used to create and initialise the BPF execution context.
14299a2dd95SBruce Richardson  * @param fname
14399a2dd95SBruce Richardson  *  Pathname for a ELF file.
14499a2dd95SBruce Richardson  * @param sname
14599a2dd95SBruce Richardson  *  Name of the executable section within the file to load.
14699a2dd95SBruce Richardson  * @return
14799a2dd95SBruce Richardson  *   BPF handle that is used in future BPF operations,
14899a2dd95SBruce Richardson  *   or NULL on error, with error code set in rte_errno.
14999a2dd95SBruce Richardson  *   Possible rte_errno errors include:
15099a2dd95SBruce Richardson  *   - EINVAL - invalid parameter passed to function
15199a2dd95SBruce Richardson  *   - ENOMEM - can't reserve enough memory
15299a2dd95SBruce Richardson  */
15399a2dd95SBruce Richardson struct rte_bpf *
15499a2dd95SBruce Richardson rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
15599a2dd95SBruce Richardson 		const char *sname);
15699a2dd95SBruce Richardson /**
15799a2dd95SBruce Richardson  * Execute given BPF bytecode.
15899a2dd95SBruce Richardson  *
15999a2dd95SBruce Richardson  * @param bpf
16099a2dd95SBruce Richardson  *   handle for the BPF code to execute.
16199a2dd95SBruce Richardson  * @param ctx
16299a2dd95SBruce Richardson  *   pointer to input context.
16399a2dd95SBruce Richardson  * @return
16499a2dd95SBruce Richardson  *   BPF execution return value.
16599a2dd95SBruce Richardson  */
16699a2dd95SBruce Richardson uint64_t
16799a2dd95SBruce Richardson rte_bpf_exec(const struct rte_bpf *bpf, void *ctx);
16899a2dd95SBruce Richardson 
16999a2dd95SBruce Richardson /**
17099a2dd95SBruce Richardson  * Execute given BPF bytecode over a set of input contexts.
17199a2dd95SBruce Richardson  *
17299a2dd95SBruce Richardson  * @param bpf
17399a2dd95SBruce Richardson  *   handle for the BPF code to execute.
17499a2dd95SBruce Richardson  * @param ctx
17599a2dd95SBruce Richardson  *   array of pointers to the input contexts.
17699a2dd95SBruce Richardson  * @param rc
17799a2dd95SBruce Richardson  *   array of return values (one per input).
17899a2dd95SBruce Richardson  * @param num
17999a2dd95SBruce Richardson  *   number of elements in ctx[] (and rc[]).
18099a2dd95SBruce Richardson  * @return
18199a2dd95SBruce Richardson  *   number of successfully processed inputs.
18299a2dd95SBruce Richardson  */
18399a2dd95SBruce Richardson uint32_t
18499a2dd95SBruce Richardson rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],
18599a2dd95SBruce Richardson 		uint32_t num);
18699a2dd95SBruce Richardson 
18799a2dd95SBruce Richardson /**
18899a2dd95SBruce Richardson  * Provide information about natively compiled code for given BPF handle.
18999a2dd95SBruce Richardson  *
19099a2dd95SBruce Richardson  * @param bpf
19199a2dd95SBruce Richardson  *   handle for the BPF code.
19299a2dd95SBruce Richardson  * @param jit
19399a2dd95SBruce Richardson  *   pointer to the rte_bpf_jit structure to be filled with related data.
19499a2dd95SBruce Richardson  * @return
19599a2dd95SBruce Richardson  *   - -EINVAL if the parameters are invalid.
19699a2dd95SBruce Richardson  *   - Zero if operation completed successfully.
19799a2dd95SBruce Richardson  */
19899a2dd95SBruce Richardson int
19999a2dd95SBruce Richardson rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit);
20099a2dd95SBruce Richardson 
201745b7587SStephen Hemminger /**
202745b7587SStephen Hemminger  * Dump epf instructions to a file.
203745b7587SStephen Hemminger  *
204745b7587SStephen Hemminger  * @param f
205745b7587SStephen Hemminger  *   A pointer to a file for output
206745b7587SStephen Hemminger  * @param buf
207745b7587SStephen Hemminger  *   A pointer to BPF instructions
208745b7587SStephen Hemminger  * @param len
209745b7587SStephen Hemminger  *   Number of BPF instructions to dump.
210745b7587SStephen Hemminger  */
211745b7587SStephen Hemminger void
212745b7587SStephen Hemminger rte_bpf_dump(FILE *f, const struct ebpf_insn *buf, uint32_t len);
213745b7587SStephen Hemminger 
2142eccf6afSStephen Hemminger struct bpf_program;
2152eccf6afSStephen Hemminger 
2162eccf6afSStephen Hemminger /**
2172eccf6afSStephen Hemminger  * Convert a Classic BPF program from libpcap into a DPDK BPF code.
2182eccf6afSStephen Hemminger  *
2192eccf6afSStephen Hemminger  * @param prog
2202eccf6afSStephen Hemminger  *  Classic BPF program from pcap_compile().
2212eccf6afSStephen Hemminger  * @return
2222eccf6afSStephen Hemminger  *   Pointer to BPF program (allocated with *rte_malloc*)
2232eccf6afSStephen Hemminger  *   that is used in future BPF operations,
2242eccf6afSStephen Hemminger  *   or NULL on error, with error code set in rte_errno.
2252eccf6afSStephen Hemminger  *   Possible rte_errno errors include:
2262eccf6afSStephen Hemminger  *   - EINVAL - invalid parameter passed to function
2272eccf6afSStephen Hemminger  *   - ENOMEM - can't reserve enough memory
228*65d9b7c6SKonstantin Ananyev  *   - ENOTSUP - operation not supported
2292eccf6afSStephen Hemminger  */
2302eccf6afSStephen Hemminger struct rte_bpf_prm *
2312eccf6afSStephen Hemminger rte_bpf_convert(const struct bpf_program *prog);
2322eccf6afSStephen Hemminger 
23399a2dd95SBruce Richardson #ifdef __cplusplus
23499a2dd95SBruce Richardson }
23599a2dd95SBruce Richardson #endif
23699a2dd95SBruce Richardson 
23799a2dd95SBruce Richardson #endif /* _RTE_BPF_H_ */
238