xref: /dpdk/lib/ipsec/rte_ipsec.h (revision aae98b8c6690ccc49d7a1536a1b1ee1264de49a7)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2018-2020 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _RTE_IPSEC_H_
699a2dd95SBruce Richardson #define _RTE_IPSEC_H_
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson /**
999a2dd95SBruce Richardson  * @file rte_ipsec.h
1099a2dd95SBruce Richardson  *
1199a2dd95SBruce Richardson  * RTE IPsec support.
1299a2dd95SBruce Richardson  *
1399a2dd95SBruce Richardson  * librte_ipsec provides a framework for data-path IPsec protocol
1499a2dd95SBruce Richardson  * processing (ESP/AH).
1599a2dd95SBruce Richardson  */
1699a2dd95SBruce Richardson 
1799a2dd95SBruce Richardson #include <rte_ipsec_sa.h>
1899a2dd95SBruce Richardson #include <rte_mbuf.h>
1999a2dd95SBruce Richardson 
2099a2dd95SBruce Richardson struct rte_ipsec_session;
2199a2dd95SBruce Richardson 
2299a2dd95SBruce Richardson /**
23*aae98b8cSAakash Sasidharan  * IPsec state for stateless processing of a batch of IPsec packets.
24*aae98b8cSAakash Sasidharan  */
25*aae98b8cSAakash Sasidharan struct rte_ipsec_state {
26*aae98b8cSAakash Sasidharan 	/**
27*aae98b8cSAakash Sasidharan 	 * 64 bit sequence number to be used for the first packet of the
28*aae98b8cSAakash Sasidharan 	 * batch of packets.
29*aae98b8cSAakash Sasidharan 	 */
30*aae98b8cSAakash Sasidharan 	uint64_t sqn;
31*aae98b8cSAakash Sasidharan };
32*aae98b8cSAakash Sasidharan 
33*aae98b8cSAakash Sasidharan /**
3499a2dd95SBruce Richardson  * IPsec session specific functions that will be used to:
3599a2dd95SBruce Richardson  * - prepare - for input mbufs and given IPsec session prepare crypto ops
3699a2dd95SBruce Richardson  *   that can be enqueued into the cryptodev associated with given session
3799a2dd95SBruce Richardson  *   (see *rte_ipsec_pkt_crypto_prepare* below for more details).
38*aae98b8cSAakash Sasidharan  * - prepare_stateless - similar to prepare, but further processing is done
39*aae98b8cSAakash Sasidharan  *   based on IPsec state provided by the 'state' parameter.
4099a2dd95SBruce Richardson  * - process - finalize processing of packets after crypto-dev finished
4199a2dd95SBruce Richardson  *   with them or process packets that are subjects to inline IPsec offload
4299a2dd95SBruce Richardson  *   (see rte_ipsec_pkt_process for more details).
4399a2dd95SBruce Richardson  */
4499a2dd95SBruce Richardson struct rte_ipsec_sa_pkt_func {
4599a2dd95SBruce Richardson 	union {
4699a2dd95SBruce Richardson 		uint16_t (*async)(const struct rte_ipsec_session *ss,
4799a2dd95SBruce Richardson 				struct rte_mbuf *mb[],
4899a2dd95SBruce Richardson 				struct rte_crypto_op *cop[],
4999a2dd95SBruce Richardson 				uint16_t num);
5099a2dd95SBruce Richardson 		uint16_t (*sync)(const struct rte_ipsec_session *ss,
5199a2dd95SBruce Richardson 				struct rte_mbuf *mb[],
5299a2dd95SBruce Richardson 				uint16_t num);
5399a2dd95SBruce Richardson 	} prepare;
54*aae98b8cSAakash Sasidharan 	union {
55*aae98b8cSAakash Sasidharan 		uint16_t (*async)(const struct rte_ipsec_session *ss,
56*aae98b8cSAakash Sasidharan 				struct rte_mbuf *mb[],
57*aae98b8cSAakash Sasidharan 				struct rte_crypto_op *cop[],
58*aae98b8cSAakash Sasidharan 				uint16_t num,
59*aae98b8cSAakash Sasidharan 				struct rte_ipsec_state *state);
60*aae98b8cSAakash Sasidharan 		uint16_t (*sync)(const struct rte_ipsec_session *ss,
61*aae98b8cSAakash Sasidharan 				struct rte_mbuf *mb[],
62*aae98b8cSAakash Sasidharan 				uint16_t num,
63*aae98b8cSAakash Sasidharan 				struct rte_ipsec_state *state);
64*aae98b8cSAakash Sasidharan 	} prepare_stateless;
6599a2dd95SBruce Richardson 	uint16_t (*process)(const struct rte_ipsec_session *ss,
6699a2dd95SBruce Richardson 				struct rte_mbuf *mb[],
6799a2dd95SBruce Richardson 				uint16_t num);
6899a2dd95SBruce Richardson };
6999a2dd95SBruce Richardson 
7099a2dd95SBruce Richardson /**
7199a2dd95SBruce Richardson  * rte_ipsec_session is an aggregate structure that defines particular
7299a2dd95SBruce Richardson  * IPsec Security Association IPsec (SA) on given security/crypto device:
7399a2dd95SBruce Richardson  * - pointer to the SA object
7499a2dd95SBruce Richardson  * - security session action type
7599a2dd95SBruce Richardson  * - pointer to security/crypto session, plus other related data
7699a2dd95SBruce Richardson  * - session/device specific functions to prepare/process IPsec packets.
7799a2dd95SBruce Richardson  */
78c6552d9aSTyler Retzlaff struct __rte_cache_aligned rte_ipsec_session {
7999a2dd95SBruce Richardson 	/**
8099a2dd95SBruce Richardson 	 * SA that session belongs to.
8199a2dd95SBruce Richardson 	 * Note that multiple sessions can belong to the same SA.
8299a2dd95SBruce Richardson 	 */
8399a2dd95SBruce Richardson 	struct rte_ipsec_sa *sa;
8499a2dd95SBruce Richardson 	/** session action type */
8599a2dd95SBruce Richardson 	enum rte_security_session_action_type type;
8699a2dd95SBruce Richardson 	/** session and related data */
8799a2dd95SBruce Richardson 	union {
8899a2dd95SBruce Richardson 		struct {
8999a2dd95SBruce Richardson 			struct rte_cryptodev_sym_session *ses;
9099a2dd95SBruce Richardson 			uint8_t dev_id;
9199a2dd95SBruce Richardson 		} crypto;
9299a2dd95SBruce Richardson 		struct {
9399a2dd95SBruce Richardson 			struct rte_security_session *ses;
9499a2dd95SBruce Richardson 			struct rte_security_ctx *ctx;
9599a2dd95SBruce Richardson 			uint32_t ol_flags;
9699a2dd95SBruce Richardson 		} security;
9799a2dd95SBruce Richardson 	};
9899a2dd95SBruce Richardson 	/** functions to prepare/process IPsec packets */
9999a2dd95SBruce Richardson 	struct rte_ipsec_sa_pkt_func pkt_func;
100c6552d9aSTyler Retzlaff };
10199a2dd95SBruce Richardson 
10299a2dd95SBruce Richardson /**
10399a2dd95SBruce Richardson  * Checks that inside given rte_ipsec_session crypto/security fields
10499a2dd95SBruce Richardson  * are filled correctly and setups function pointers based on these values.
10599a2dd95SBruce Richardson  * Expects that all fields except IPsec processing function pointers
10699a2dd95SBruce Richardson  * (*pkt_func*) will be filled correctly by caller.
10799a2dd95SBruce Richardson  * @param ss
10899a2dd95SBruce Richardson  *   Pointer to the *rte_ipsec_session* object
10999a2dd95SBruce Richardson  * @return
11099a2dd95SBruce Richardson  *   - Zero if operation completed successfully.
11199a2dd95SBruce Richardson  *   - -EINVAL if the parameters are invalid.
11299a2dd95SBruce Richardson  */
11399a2dd95SBruce Richardson int
11499a2dd95SBruce Richardson rte_ipsec_session_prepare(struct rte_ipsec_session *ss);
11599a2dd95SBruce Richardson 
11699a2dd95SBruce Richardson /**
11799a2dd95SBruce Richardson  * For input mbufs and given IPsec session prepare crypto ops that can be
11899a2dd95SBruce Richardson  * enqueued into the cryptodev associated with given session.
11999a2dd95SBruce Richardson  * expects that for each input packet:
12099a2dd95SBruce Richardson  *      - l2_len, l3_len are setup correctly
12199a2dd95SBruce Richardson  * Note that erroneous mbufs are not freed by the function,
12299a2dd95SBruce Richardson  * but are placed beyond last valid mbuf in the *mb* array.
12399a2dd95SBruce Richardson  * It is a user responsibility to handle them further.
12499a2dd95SBruce Richardson  * @param ss
12599a2dd95SBruce Richardson  *   Pointer to the *rte_ipsec_session* object the packets belong to.
12699a2dd95SBruce Richardson  * @param mb
12799a2dd95SBruce Richardson  *   The address of an array of *num* pointers to *rte_mbuf* structures
12899a2dd95SBruce Richardson  *   which contain the input packets.
12999a2dd95SBruce Richardson  * @param cop
13099a2dd95SBruce Richardson  *   The address of an array of *num* pointers to the output *rte_crypto_op*
13199a2dd95SBruce Richardson  *   structures.
13299a2dd95SBruce Richardson  * @param num
13399a2dd95SBruce Richardson  *   The maximum number of packets to process.
13499a2dd95SBruce Richardson  * @return
13599a2dd95SBruce Richardson  *   Number of successfully processed packets, with error code set in rte_errno.
13699a2dd95SBruce Richardson  */
13799a2dd95SBruce Richardson static inline uint16_t
13899a2dd95SBruce Richardson rte_ipsec_pkt_crypto_prepare(const struct rte_ipsec_session *ss,
13999a2dd95SBruce Richardson 	struct rte_mbuf *mb[], struct rte_crypto_op *cop[], uint16_t num)
14099a2dd95SBruce Richardson {
14199a2dd95SBruce Richardson 	return ss->pkt_func.prepare.async(ss, mb, cop, num);
14299a2dd95SBruce Richardson }
14399a2dd95SBruce Richardson 
14499a2dd95SBruce Richardson static inline uint16_t
14599a2dd95SBruce Richardson rte_ipsec_pkt_cpu_prepare(const struct rte_ipsec_session *ss,
14699a2dd95SBruce Richardson 	struct rte_mbuf *mb[], uint16_t num)
14799a2dd95SBruce Richardson {
14899a2dd95SBruce Richardson 	return ss->pkt_func.prepare.sync(ss, mb, num);
14999a2dd95SBruce Richardson }
15099a2dd95SBruce Richardson 
15199a2dd95SBruce Richardson /**
152*aae98b8cSAakash Sasidharan  * Same as *rte_ipsec_pkt_crypto_prepare*, but processing is done based on
153*aae98b8cSAakash Sasidharan  * IPsec state provided by the 'state' parameter. Internal IPsec state won't
154*aae98b8cSAakash Sasidharan  * be updated when this API is called.
155*aae98b8cSAakash Sasidharan  *
156*aae98b8cSAakash Sasidharan  * For input mbufs and given IPsec session prepare crypto ops that can be
157*aae98b8cSAakash Sasidharan  * enqueued into the cryptodev associated with given session.
158*aae98b8cSAakash Sasidharan  * expects that for each input packet:
159*aae98b8cSAakash Sasidharan  *      - l2_len, l3_len are setup correctly
160*aae98b8cSAakash Sasidharan  * Note that erroneous mbufs are not freed by the function,
161*aae98b8cSAakash Sasidharan  * but are placed beyond last valid mbuf in the *mb* array.
162*aae98b8cSAakash Sasidharan  * It is a user responsibility to handle them further.
163*aae98b8cSAakash Sasidharan  * @param ss
164*aae98b8cSAakash Sasidharan  *   Pointer to the *rte_ipsec_session* object the packets belong to.
165*aae98b8cSAakash Sasidharan  * @param mb
166*aae98b8cSAakash Sasidharan  *   The address of an array of *num* pointers to *rte_mbuf* structures
167*aae98b8cSAakash Sasidharan  *   which contain the input packets.
168*aae98b8cSAakash Sasidharan  * @param cop
169*aae98b8cSAakash Sasidharan  *   The address of an array of *num* pointers to the output *rte_crypto_op*
170*aae98b8cSAakash Sasidharan  *   structures.
171*aae98b8cSAakash Sasidharan  * @param num
172*aae98b8cSAakash Sasidharan  *   The maximum number of packets to process.
173*aae98b8cSAakash Sasidharan  * @param state
174*aae98b8cSAakash Sasidharan  *   The IPsec state to be used for processing current batch of packets.
175*aae98b8cSAakash Sasidharan  * @return
176*aae98b8cSAakash Sasidharan  *   Number of successfully processed packets, with error code set in rte_errno.
177*aae98b8cSAakash Sasidharan  */
178*aae98b8cSAakash Sasidharan __rte_experimental
179*aae98b8cSAakash Sasidharan static inline uint16_t
180*aae98b8cSAakash Sasidharan rte_ipsec_pkt_crypto_prepare_stateless(const struct rte_ipsec_session *ss,
181*aae98b8cSAakash Sasidharan 	struct rte_mbuf *mb[], struct rte_crypto_op *cop[], uint16_t num,
182*aae98b8cSAakash Sasidharan 	struct rte_ipsec_state *state)
183*aae98b8cSAakash Sasidharan {
184*aae98b8cSAakash Sasidharan 	return ss->pkt_func.prepare_stateless.async(ss, mb, cop, num, state);
185*aae98b8cSAakash Sasidharan }
186*aae98b8cSAakash Sasidharan 
187*aae98b8cSAakash Sasidharan /**
188*aae98b8cSAakash Sasidharan  * Same as *rte_ipsec_pkt_crypto_prepare_stateless*, but processing is done
189*aae98b8cSAakash Sasidharan  * in synchronous mode.
190*aae98b8cSAakash Sasidharan  *
191*aae98b8cSAakash Sasidharan  * @param ss
192*aae98b8cSAakash Sasidharan  *   Pointer to the *rte_ipsec_session* object the packets belong to.
193*aae98b8cSAakash Sasidharan  * @param mb
194*aae98b8cSAakash Sasidharan  *   The address of an array of *num* pointers to *rte_mbuf* structures
195*aae98b8cSAakash Sasidharan  *   which contain the input packets.
196*aae98b8cSAakash Sasidharan  * @param num
197*aae98b8cSAakash Sasidharan  *   The maximum number of packets to process.
198*aae98b8cSAakash Sasidharan  * @param state
199*aae98b8cSAakash Sasidharan  *   The IPsec state to be used for processing current batch of packets.
200*aae98b8cSAakash Sasidharan  * @return
201*aae98b8cSAakash Sasidharan  *   Number of successfully processed packets, with error code set in rte_errno.
202*aae98b8cSAakash Sasidharan  */
203*aae98b8cSAakash Sasidharan __rte_experimental
204*aae98b8cSAakash Sasidharan static inline uint16_t
205*aae98b8cSAakash Sasidharan rte_ipsec_pkt_cpu_prepare_stateless(const struct rte_ipsec_session *ss,
206*aae98b8cSAakash Sasidharan 	struct rte_mbuf *mb[], uint16_t num, struct rte_ipsec_state *state)
207*aae98b8cSAakash Sasidharan {
208*aae98b8cSAakash Sasidharan 	return ss->pkt_func.prepare_stateless.sync(ss, mb, num, state);
209*aae98b8cSAakash Sasidharan }
210*aae98b8cSAakash Sasidharan 
211*aae98b8cSAakash Sasidharan /**
21299a2dd95SBruce Richardson  * Finalise processing of packets after crypto-dev finished with them or
21399a2dd95SBruce Richardson  * process packets that are subjects to inline IPsec offload.
21499a2dd95SBruce Richardson  * Expects that for each input packet:
21599a2dd95SBruce Richardson  *      - l2_len, l3_len are setup correctly
21699a2dd95SBruce Richardson  * Output mbufs will be:
21799a2dd95SBruce Richardson  * inbound - decrypted & authenticated, ESP(AH) related headers removed,
21899a2dd95SBruce Richardson  * *l2_len* and *l3_len* fields are updated.
21999a2dd95SBruce Richardson  * outbound - appropriate mbuf fields (ol_flags, tx_offloads, etc.)
22099a2dd95SBruce Richardson  * properly setup, if necessary - IP headers updated, ESP(AH) fields added,
22199a2dd95SBruce Richardson  * Note that erroneous mbufs are not freed by the function,
22299a2dd95SBruce Richardson  * but are placed beyond last valid mbuf in the *mb* array.
22399a2dd95SBruce Richardson  * It is a user responsibility to handle them further.
22499a2dd95SBruce Richardson  * @param ss
22599a2dd95SBruce Richardson  *   Pointer to the *rte_ipsec_session* object the packets belong to.
22699a2dd95SBruce Richardson  * @param mb
22799a2dd95SBruce Richardson  *   The address of an array of *num* pointers to *rte_mbuf* structures
22899a2dd95SBruce Richardson  *   which contain the input packets.
22999a2dd95SBruce Richardson  * @param num
23099a2dd95SBruce Richardson  *   The maximum number of packets to process.
23199a2dd95SBruce Richardson  * @return
23299a2dd95SBruce Richardson  *   Number of successfully processed packets, with error code set in rte_errno.
23399a2dd95SBruce Richardson  */
23499a2dd95SBruce Richardson static inline uint16_t
23599a2dd95SBruce Richardson rte_ipsec_pkt_process(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[],
23699a2dd95SBruce Richardson 	uint16_t num)
23799a2dd95SBruce Richardson {
23899a2dd95SBruce Richardson 	return ss->pkt_func.process(ss, mb, num);
23999a2dd95SBruce Richardson }
24099a2dd95SBruce Richardson 
24168977baaSRadu Nicolau 
24268977baaSRadu Nicolau /**
24368977baaSRadu Nicolau  * Enable per SA telemetry for a specific SA.
24468977baaSRadu Nicolau  * Note that this function is not thread safe
24568977baaSRadu Nicolau  * @param sa
24668977baaSRadu Nicolau  *   Pointer to the *rte_ipsec_sa* object that will have telemetry enabled.
24768977baaSRadu Nicolau  * @return
24868977baaSRadu Nicolau  *   0 on success, negative value otherwise.
24968977baaSRadu Nicolau  */
25068977baaSRadu Nicolau int
25168977baaSRadu Nicolau rte_ipsec_telemetry_sa_add(const struct rte_ipsec_sa *sa);
25268977baaSRadu Nicolau 
25368977baaSRadu Nicolau /**
25468977baaSRadu Nicolau  * Disable per SA telemetry for a specific SA.
25568977baaSRadu Nicolau  * Note that this function is not thread safe
25668977baaSRadu Nicolau  * @param sa
25768977baaSRadu Nicolau  *   Pointer to the *rte_ipsec_sa* object that will have telemetry disabled.
25868977baaSRadu Nicolau  */
25968977baaSRadu Nicolau void
26068977baaSRadu Nicolau rte_ipsec_telemetry_sa_del(const struct rte_ipsec_sa *sa);
26168977baaSRadu Nicolau 
26299a2dd95SBruce Richardson #include <rte_ipsec_group.h>
26399a2dd95SBruce Richardson 
26499a2dd95SBruce Richardson #ifdef __cplusplus
265719834a6SMattias Rönnblom extern "C" {
266719834a6SMattias Rönnblom #endif
267719834a6SMattias Rönnblom 
268719834a6SMattias Rönnblom #ifdef __cplusplus
26999a2dd95SBruce Richardson }
27099a2dd95SBruce Richardson #endif
27199a2dd95SBruce Richardson 
27299a2dd95SBruce Richardson #endif /* _RTE_IPSEC_H_ */
273