1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 2*99a2dd95SBruce Richardson * Copyright(c) 2018-2020 Intel Corporation 3*99a2dd95SBruce Richardson */ 4*99a2dd95SBruce Richardson 5*99a2dd95SBruce Richardson #ifndef _RTE_IPSEC_H_ 6*99a2dd95SBruce Richardson #define _RTE_IPSEC_H_ 7*99a2dd95SBruce Richardson 8*99a2dd95SBruce Richardson /** 9*99a2dd95SBruce Richardson * @file rte_ipsec.h 10*99a2dd95SBruce Richardson * 11*99a2dd95SBruce Richardson * RTE IPsec support. 12*99a2dd95SBruce Richardson * 13*99a2dd95SBruce Richardson * librte_ipsec provides a framework for data-path IPsec protocol 14*99a2dd95SBruce Richardson * processing (ESP/AH). 15*99a2dd95SBruce Richardson */ 16*99a2dd95SBruce Richardson 17*99a2dd95SBruce Richardson #include <rte_ipsec_sa.h> 18*99a2dd95SBruce Richardson #include <rte_mbuf.h> 19*99a2dd95SBruce Richardson 20*99a2dd95SBruce Richardson #ifdef __cplusplus 21*99a2dd95SBruce Richardson extern "C" { 22*99a2dd95SBruce Richardson #endif 23*99a2dd95SBruce Richardson 24*99a2dd95SBruce Richardson struct rte_ipsec_session; 25*99a2dd95SBruce Richardson 26*99a2dd95SBruce Richardson /** 27*99a2dd95SBruce Richardson * IPsec session specific functions that will be used to: 28*99a2dd95SBruce Richardson * - prepare - for input mbufs and given IPsec session prepare crypto ops 29*99a2dd95SBruce Richardson * that can be enqueued into the cryptodev associated with given session 30*99a2dd95SBruce Richardson * (see *rte_ipsec_pkt_crypto_prepare* below for more details). 31*99a2dd95SBruce Richardson * - process - finalize processing of packets after crypto-dev finished 32*99a2dd95SBruce Richardson * with them or process packets that are subjects to inline IPsec offload 33*99a2dd95SBruce Richardson * (see rte_ipsec_pkt_process for more details). 34*99a2dd95SBruce Richardson */ 35*99a2dd95SBruce Richardson struct rte_ipsec_sa_pkt_func { 36*99a2dd95SBruce Richardson union { 37*99a2dd95SBruce Richardson uint16_t (*async)(const struct rte_ipsec_session *ss, 38*99a2dd95SBruce Richardson struct rte_mbuf *mb[], 39*99a2dd95SBruce Richardson struct rte_crypto_op *cop[], 40*99a2dd95SBruce Richardson uint16_t num); 41*99a2dd95SBruce Richardson uint16_t (*sync)(const struct rte_ipsec_session *ss, 42*99a2dd95SBruce Richardson struct rte_mbuf *mb[], 43*99a2dd95SBruce Richardson uint16_t num); 44*99a2dd95SBruce Richardson } prepare; 45*99a2dd95SBruce Richardson uint16_t (*process)(const struct rte_ipsec_session *ss, 46*99a2dd95SBruce Richardson struct rte_mbuf *mb[], 47*99a2dd95SBruce Richardson uint16_t num); 48*99a2dd95SBruce Richardson }; 49*99a2dd95SBruce Richardson 50*99a2dd95SBruce Richardson /** 51*99a2dd95SBruce Richardson * rte_ipsec_session is an aggregate structure that defines particular 52*99a2dd95SBruce Richardson * IPsec Security Association IPsec (SA) on given security/crypto device: 53*99a2dd95SBruce Richardson * - pointer to the SA object 54*99a2dd95SBruce Richardson * - security session action type 55*99a2dd95SBruce Richardson * - pointer to security/crypto session, plus other related data 56*99a2dd95SBruce Richardson * - session/device specific functions to prepare/process IPsec packets. 57*99a2dd95SBruce Richardson */ 58*99a2dd95SBruce Richardson struct rte_ipsec_session { 59*99a2dd95SBruce Richardson /** 60*99a2dd95SBruce Richardson * SA that session belongs to. 61*99a2dd95SBruce Richardson * Note that multiple sessions can belong to the same SA. 62*99a2dd95SBruce Richardson */ 63*99a2dd95SBruce Richardson struct rte_ipsec_sa *sa; 64*99a2dd95SBruce Richardson /** session action type */ 65*99a2dd95SBruce Richardson enum rte_security_session_action_type type; 66*99a2dd95SBruce Richardson /** session and related data */ 67*99a2dd95SBruce Richardson union { 68*99a2dd95SBruce Richardson struct { 69*99a2dd95SBruce Richardson struct rte_cryptodev_sym_session *ses; 70*99a2dd95SBruce Richardson uint8_t dev_id; 71*99a2dd95SBruce Richardson } crypto; 72*99a2dd95SBruce Richardson struct { 73*99a2dd95SBruce Richardson struct rte_security_session *ses; 74*99a2dd95SBruce Richardson struct rte_security_ctx *ctx; 75*99a2dd95SBruce Richardson uint32_t ol_flags; 76*99a2dd95SBruce Richardson } security; 77*99a2dd95SBruce Richardson }; 78*99a2dd95SBruce Richardson /** functions to prepare/process IPsec packets */ 79*99a2dd95SBruce Richardson struct rte_ipsec_sa_pkt_func pkt_func; 80*99a2dd95SBruce Richardson } __rte_cache_aligned; 81*99a2dd95SBruce Richardson 82*99a2dd95SBruce Richardson /** 83*99a2dd95SBruce Richardson * Checks that inside given rte_ipsec_session crypto/security fields 84*99a2dd95SBruce Richardson * are filled correctly and setups function pointers based on these values. 85*99a2dd95SBruce Richardson * Expects that all fields except IPsec processing function pointers 86*99a2dd95SBruce Richardson * (*pkt_func*) will be filled correctly by caller. 87*99a2dd95SBruce Richardson * @param ss 88*99a2dd95SBruce Richardson * Pointer to the *rte_ipsec_session* object 89*99a2dd95SBruce Richardson * @return 90*99a2dd95SBruce Richardson * - Zero if operation completed successfully. 91*99a2dd95SBruce Richardson * - -EINVAL if the parameters are invalid. 92*99a2dd95SBruce Richardson */ 93*99a2dd95SBruce Richardson int 94*99a2dd95SBruce Richardson rte_ipsec_session_prepare(struct rte_ipsec_session *ss); 95*99a2dd95SBruce Richardson 96*99a2dd95SBruce Richardson /** 97*99a2dd95SBruce Richardson * For input mbufs and given IPsec session prepare crypto ops that can be 98*99a2dd95SBruce Richardson * enqueued into the cryptodev associated with given session. 99*99a2dd95SBruce Richardson * expects that for each input packet: 100*99a2dd95SBruce Richardson * - l2_len, l3_len are setup correctly 101*99a2dd95SBruce Richardson * Note that erroneous mbufs are not freed by the function, 102*99a2dd95SBruce Richardson * but are placed beyond last valid mbuf in the *mb* array. 103*99a2dd95SBruce Richardson * It is a user responsibility to handle them further. 104*99a2dd95SBruce Richardson * @param ss 105*99a2dd95SBruce Richardson * Pointer to the *rte_ipsec_session* object the packets belong to. 106*99a2dd95SBruce Richardson * @param mb 107*99a2dd95SBruce Richardson * The address of an array of *num* pointers to *rte_mbuf* structures 108*99a2dd95SBruce Richardson * which contain the input packets. 109*99a2dd95SBruce Richardson * @param cop 110*99a2dd95SBruce Richardson * The address of an array of *num* pointers to the output *rte_crypto_op* 111*99a2dd95SBruce Richardson * structures. 112*99a2dd95SBruce Richardson * @param num 113*99a2dd95SBruce Richardson * The maximum number of packets to process. 114*99a2dd95SBruce Richardson * @return 115*99a2dd95SBruce Richardson * Number of successfully processed packets, with error code set in rte_errno. 116*99a2dd95SBruce Richardson */ 117*99a2dd95SBruce Richardson static inline uint16_t 118*99a2dd95SBruce Richardson rte_ipsec_pkt_crypto_prepare(const struct rte_ipsec_session *ss, 119*99a2dd95SBruce Richardson struct rte_mbuf *mb[], struct rte_crypto_op *cop[], uint16_t num) 120*99a2dd95SBruce Richardson { 121*99a2dd95SBruce Richardson return ss->pkt_func.prepare.async(ss, mb, cop, num); 122*99a2dd95SBruce Richardson } 123*99a2dd95SBruce Richardson 124*99a2dd95SBruce Richardson static inline uint16_t 125*99a2dd95SBruce Richardson rte_ipsec_pkt_cpu_prepare(const struct rte_ipsec_session *ss, 126*99a2dd95SBruce Richardson struct rte_mbuf *mb[], uint16_t num) 127*99a2dd95SBruce Richardson { 128*99a2dd95SBruce Richardson return ss->pkt_func.prepare.sync(ss, mb, num); 129*99a2dd95SBruce Richardson } 130*99a2dd95SBruce Richardson 131*99a2dd95SBruce Richardson /** 132*99a2dd95SBruce Richardson * Finalise processing of packets after crypto-dev finished with them or 133*99a2dd95SBruce Richardson * process packets that are subjects to inline IPsec offload. 134*99a2dd95SBruce Richardson * Expects that for each input packet: 135*99a2dd95SBruce Richardson * - l2_len, l3_len are setup correctly 136*99a2dd95SBruce Richardson * Output mbufs will be: 137*99a2dd95SBruce Richardson * inbound - decrypted & authenticated, ESP(AH) related headers removed, 138*99a2dd95SBruce Richardson * *l2_len* and *l3_len* fields are updated. 139*99a2dd95SBruce Richardson * outbound - appropriate mbuf fields (ol_flags, tx_offloads, etc.) 140*99a2dd95SBruce Richardson * properly setup, if necessary - IP headers updated, ESP(AH) fields added, 141*99a2dd95SBruce Richardson * Note that erroneous mbufs are not freed by the function, 142*99a2dd95SBruce Richardson * but are placed beyond last valid mbuf in the *mb* array. 143*99a2dd95SBruce Richardson * It is a user responsibility to handle them further. 144*99a2dd95SBruce Richardson * @param ss 145*99a2dd95SBruce Richardson * Pointer to the *rte_ipsec_session* object the packets belong to. 146*99a2dd95SBruce Richardson * @param mb 147*99a2dd95SBruce Richardson * The address of an array of *num* pointers to *rte_mbuf* structures 148*99a2dd95SBruce Richardson * which contain the input packets. 149*99a2dd95SBruce Richardson * @param num 150*99a2dd95SBruce Richardson * The maximum number of packets to process. 151*99a2dd95SBruce Richardson * @return 152*99a2dd95SBruce Richardson * Number of successfully processed packets, with error code set in rte_errno. 153*99a2dd95SBruce Richardson */ 154*99a2dd95SBruce Richardson static inline uint16_t 155*99a2dd95SBruce Richardson rte_ipsec_pkt_process(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[], 156*99a2dd95SBruce Richardson uint16_t num) 157*99a2dd95SBruce Richardson { 158*99a2dd95SBruce Richardson return ss->pkt_func.process(ss, mb, num); 159*99a2dd95SBruce Richardson } 160*99a2dd95SBruce Richardson 161*99a2dd95SBruce Richardson #include <rte_ipsec_group.h> 162*99a2dd95SBruce Richardson 163*99a2dd95SBruce Richardson #ifdef __cplusplus 164*99a2dd95SBruce Richardson } 165*99a2dd95SBruce Richardson #endif 166*99a2dd95SBruce Richardson 167*99a2dd95SBruce Richardson #endif /* _RTE_IPSEC_H_ */ 168