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 #ifdef __cplusplus 2199a2dd95SBruce Richardson extern "C" { 2299a2dd95SBruce Richardson #endif 2399a2dd95SBruce Richardson 2499a2dd95SBruce Richardson struct rte_ipsec_session; 2599a2dd95SBruce Richardson 2699a2dd95SBruce Richardson /** 2799a2dd95SBruce Richardson * IPsec session specific functions that will be used to: 2899a2dd95SBruce Richardson * - prepare - for input mbufs and given IPsec session prepare crypto ops 2999a2dd95SBruce Richardson * that can be enqueued into the cryptodev associated with given session 3099a2dd95SBruce Richardson * (see *rte_ipsec_pkt_crypto_prepare* below for more details). 3199a2dd95SBruce Richardson * - process - finalize processing of packets after crypto-dev finished 3299a2dd95SBruce Richardson * with them or process packets that are subjects to inline IPsec offload 3399a2dd95SBruce Richardson * (see rte_ipsec_pkt_process for more details). 3499a2dd95SBruce Richardson */ 3599a2dd95SBruce Richardson struct rte_ipsec_sa_pkt_func { 3699a2dd95SBruce Richardson union { 3799a2dd95SBruce Richardson uint16_t (*async)(const struct rte_ipsec_session *ss, 3899a2dd95SBruce Richardson struct rte_mbuf *mb[], 3999a2dd95SBruce Richardson struct rte_crypto_op *cop[], 4099a2dd95SBruce Richardson uint16_t num); 4199a2dd95SBruce Richardson uint16_t (*sync)(const struct rte_ipsec_session *ss, 4299a2dd95SBruce Richardson struct rte_mbuf *mb[], 4399a2dd95SBruce Richardson uint16_t num); 4499a2dd95SBruce Richardson } prepare; 4599a2dd95SBruce Richardson uint16_t (*process)(const struct rte_ipsec_session *ss, 4699a2dd95SBruce Richardson struct rte_mbuf *mb[], 4799a2dd95SBruce Richardson uint16_t num); 4899a2dd95SBruce Richardson }; 4999a2dd95SBruce Richardson 5099a2dd95SBruce Richardson /** 5199a2dd95SBruce Richardson * rte_ipsec_session is an aggregate structure that defines particular 5299a2dd95SBruce Richardson * IPsec Security Association IPsec (SA) on given security/crypto device: 5399a2dd95SBruce Richardson * - pointer to the SA object 5499a2dd95SBruce Richardson * - security session action type 5599a2dd95SBruce Richardson * - pointer to security/crypto session, plus other related data 5699a2dd95SBruce Richardson * - session/device specific functions to prepare/process IPsec packets. 5799a2dd95SBruce Richardson */ 58*c6552d9aSTyler Retzlaff struct __rte_cache_aligned rte_ipsec_session { 5999a2dd95SBruce Richardson /** 6099a2dd95SBruce Richardson * SA that session belongs to. 6199a2dd95SBruce Richardson * Note that multiple sessions can belong to the same SA. 6299a2dd95SBruce Richardson */ 6399a2dd95SBruce Richardson struct rte_ipsec_sa *sa; 6499a2dd95SBruce Richardson /** session action type */ 6599a2dd95SBruce Richardson enum rte_security_session_action_type type; 6699a2dd95SBruce Richardson /** session and related data */ 6799a2dd95SBruce Richardson union { 6899a2dd95SBruce Richardson struct { 6999a2dd95SBruce Richardson struct rte_cryptodev_sym_session *ses; 7099a2dd95SBruce Richardson uint8_t dev_id; 7199a2dd95SBruce Richardson } crypto; 7299a2dd95SBruce Richardson struct { 7399a2dd95SBruce Richardson struct rte_security_session *ses; 7499a2dd95SBruce Richardson struct rte_security_ctx *ctx; 7599a2dd95SBruce Richardson uint32_t ol_flags; 7699a2dd95SBruce Richardson } security; 7799a2dd95SBruce Richardson }; 7899a2dd95SBruce Richardson /** functions to prepare/process IPsec packets */ 7999a2dd95SBruce Richardson struct rte_ipsec_sa_pkt_func pkt_func; 80*c6552d9aSTyler Retzlaff }; 8199a2dd95SBruce Richardson 8299a2dd95SBruce Richardson /** 8399a2dd95SBruce Richardson * Checks that inside given rte_ipsec_session crypto/security fields 8499a2dd95SBruce Richardson * are filled correctly and setups function pointers based on these values. 8599a2dd95SBruce Richardson * Expects that all fields except IPsec processing function pointers 8699a2dd95SBruce Richardson * (*pkt_func*) will be filled correctly by caller. 8799a2dd95SBruce Richardson * @param ss 8899a2dd95SBruce Richardson * Pointer to the *rte_ipsec_session* object 8999a2dd95SBruce Richardson * @return 9099a2dd95SBruce Richardson * - Zero if operation completed successfully. 9199a2dd95SBruce Richardson * - -EINVAL if the parameters are invalid. 9299a2dd95SBruce Richardson */ 9399a2dd95SBruce Richardson int 9499a2dd95SBruce Richardson rte_ipsec_session_prepare(struct rte_ipsec_session *ss); 9599a2dd95SBruce Richardson 9699a2dd95SBruce Richardson /** 9799a2dd95SBruce Richardson * For input mbufs and given IPsec session prepare crypto ops that can be 9899a2dd95SBruce Richardson * enqueued into the cryptodev associated with given session. 9999a2dd95SBruce Richardson * expects that for each input packet: 10099a2dd95SBruce Richardson * - l2_len, l3_len are setup correctly 10199a2dd95SBruce Richardson * Note that erroneous mbufs are not freed by the function, 10299a2dd95SBruce Richardson * but are placed beyond last valid mbuf in the *mb* array. 10399a2dd95SBruce Richardson * It is a user responsibility to handle them further. 10499a2dd95SBruce Richardson * @param ss 10599a2dd95SBruce Richardson * Pointer to the *rte_ipsec_session* object the packets belong to. 10699a2dd95SBruce Richardson * @param mb 10799a2dd95SBruce Richardson * The address of an array of *num* pointers to *rte_mbuf* structures 10899a2dd95SBruce Richardson * which contain the input packets. 10999a2dd95SBruce Richardson * @param cop 11099a2dd95SBruce Richardson * The address of an array of *num* pointers to the output *rte_crypto_op* 11199a2dd95SBruce Richardson * structures. 11299a2dd95SBruce Richardson * @param num 11399a2dd95SBruce Richardson * The maximum number of packets to process. 11499a2dd95SBruce Richardson * @return 11599a2dd95SBruce Richardson * Number of successfully processed packets, with error code set in rte_errno. 11699a2dd95SBruce Richardson */ 11799a2dd95SBruce Richardson static inline uint16_t 11899a2dd95SBruce Richardson rte_ipsec_pkt_crypto_prepare(const struct rte_ipsec_session *ss, 11999a2dd95SBruce Richardson struct rte_mbuf *mb[], struct rte_crypto_op *cop[], uint16_t num) 12099a2dd95SBruce Richardson { 12199a2dd95SBruce Richardson return ss->pkt_func.prepare.async(ss, mb, cop, num); 12299a2dd95SBruce Richardson } 12399a2dd95SBruce Richardson 12499a2dd95SBruce Richardson static inline uint16_t 12599a2dd95SBruce Richardson rte_ipsec_pkt_cpu_prepare(const struct rte_ipsec_session *ss, 12699a2dd95SBruce Richardson struct rte_mbuf *mb[], uint16_t num) 12799a2dd95SBruce Richardson { 12899a2dd95SBruce Richardson return ss->pkt_func.prepare.sync(ss, mb, num); 12999a2dd95SBruce Richardson } 13099a2dd95SBruce Richardson 13199a2dd95SBruce Richardson /** 13299a2dd95SBruce Richardson * Finalise processing of packets after crypto-dev finished with them or 13399a2dd95SBruce Richardson * process packets that are subjects to inline IPsec offload. 13499a2dd95SBruce Richardson * Expects that for each input packet: 13599a2dd95SBruce Richardson * - l2_len, l3_len are setup correctly 13699a2dd95SBruce Richardson * Output mbufs will be: 13799a2dd95SBruce Richardson * inbound - decrypted & authenticated, ESP(AH) related headers removed, 13899a2dd95SBruce Richardson * *l2_len* and *l3_len* fields are updated. 13999a2dd95SBruce Richardson * outbound - appropriate mbuf fields (ol_flags, tx_offloads, etc.) 14099a2dd95SBruce Richardson * properly setup, if necessary - IP headers updated, ESP(AH) fields added, 14199a2dd95SBruce Richardson * Note that erroneous mbufs are not freed by the function, 14299a2dd95SBruce Richardson * but are placed beyond last valid mbuf in the *mb* array. 14399a2dd95SBruce Richardson * It is a user responsibility to handle them further. 14499a2dd95SBruce Richardson * @param ss 14599a2dd95SBruce Richardson * Pointer to the *rte_ipsec_session* object the packets belong to. 14699a2dd95SBruce Richardson * @param mb 14799a2dd95SBruce Richardson * The address of an array of *num* pointers to *rte_mbuf* structures 14899a2dd95SBruce Richardson * which contain the input packets. 14999a2dd95SBruce Richardson * @param num 15099a2dd95SBruce Richardson * The maximum number of packets to process. 15199a2dd95SBruce Richardson * @return 15299a2dd95SBruce Richardson * Number of successfully processed packets, with error code set in rte_errno. 15399a2dd95SBruce Richardson */ 15499a2dd95SBruce Richardson static inline uint16_t 15599a2dd95SBruce Richardson rte_ipsec_pkt_process(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[], 15699a2dd95SBruce Richardson uint16_t num) 15799a2dd95SBruce Richardson { 15899a2dd95SBruce Richardson return ss->pkt_func.process(ss, mb, num); 15999a2dd95SBruce Richardson } 16099a2dd95SBruce Richardson 16168977baaSRadu Nicolau 16268977baaSRadu Nicolau /** 16368977baaSRadu Nicolau * Enable per SA telemetry for a specific SA. 16468977baaSRadu Nicolau * Note that this function is not thread safe 16568977baaSRadu Nicolau * @param sa 16668977baaSRadu Nicolau * Pointer to the *rte_ipsec_sa* object that will have telemetry enabled. 16768977baaSRadu Nicolau * @return 16868977baaSRadu Nicolau * 0 on success, negative value otherwise. 16968977baaSRadu Nicolau */ 17068977baaSRadu Nicolau int 17168977baaSRadu Nicolau rte_ipsec_telemetry_sa_add(const struct rte_ipsec_sa *sa); 17268977baaSRadu Nicolau 17368977baaSRadu Nicolau /** 17468977baaSRadu Nicolau * Disable per SA telemetry for a specific SA. 17568977baaSRadu Nicolau * Note that this function is not thread safe 17668977baaSRadu Nicolau * @param sa 17768977baaSRadu Nicolau * Pointer to the *rte_ipsec_sa* object that will have telemetry disabled. 17868977baaSRadu Nicolau */ 17968977baaSRadu Nicolau void 18068977baaSRadu Nicolau rte_ipsec_telemetry_sa_del(const struct rte_ipsec_sa *sa); 18168977baaSRadu Nicolau 18299a2dd95SBruce Richardson #include <rte_ipsec_group.h> 18399a2dd95SBruce Richardson 18499a2dd95SBruce Richardson #ifdef __cplusplus 18599a2dd95SBruce Richardson } 18699a2dd95SBruce Richardson #endif 18799a2dd95SBruce Richardson 18899a2dd95SBruce Richardson #endif /* _RTE_IPSEC_H_ */ 189