1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
10 #include <rte_byteorder.h>
11 #include <rte_crypto.h>
12 #include <rte_security.h>
15 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
16 #define RTE_LOGTYPE_IPSEC_ESP RTE_LOGTYPE_USER2
17 #define RTE_LOGTYPE_IPSEC_IPIP RTE_LOGTYPE_USER3
19 #define MAX_PKT_BURST 32
20 #define MAX_INFLIGHT 128
21 #define MAX_QP_PER_LCORE 256
23 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
25 #define IPSEC_OFFLOAD_ESN_SOFTLIMIT 0xffffff00
27 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
28 sizeof(struct rte_crypto_sym_op))
30 #define uint32_t_to_char(ip, a, b, c, d) do {\
31 *a = (uint8_t)(ip >> 24 & 0xff);\
32 *b = (uint8_t)(ip >> 16 & 0xff);\
33 *c = (uint8_t)(ip >> 8 & 0xff);\
34 *d = (uint8_t)(ip & 0xff);\
37 #define DEFAULT_MAX_CATEGORIES 1
39 #define IPSEC_SA_MAX_ENTRIES (128) /* must be power of 2, max 2 power 30 */
40 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1))
41 #define INVALID_SPI (0)
43 #define DISCARD (0x80000000)
44 #define BYPASS (0x40000000)
45 #define PROTECT_MASK (0x3fffffff)
46 #define PROTECT(sa_idx) (SPI2IDX(sa_idx) & PROTECT_MASK) /* SA idx 30 bits */
48 #define IPSEC_XFORM_MAX 2
50 #define IP6_VERSION (6)
52 struct rte_crypto_xform;
58 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
59 struct rte_crypto_op *cop);
71 #define MAX_KEY_SIZE 32
79 struct rte_cryptodev_sym_session *crypto_session;
80 struct rte_security_session *sec_session;
82 enum rte_crypto_cipher_algorithm cipher_algo;
83 enum rte_crypto_auth_algorithm auth_algo;
84 enum rte_crypto_aead_algorithm aead_algo;
89 #define IP4_TUNNEL (1 << 0)
90 #define IP6_TUNNEL (1 << 1)
91 #define TRANSPORT (1 << 2)
94 uint8_t cipher_key[MAX_KEY_SIZE];
95 uint16_t cipher_key_len;
96 uint8_t auth_key[MAX_KEY_SIZE];
97 uint16_t auth_key_len;
100 struct rte_crypto_sym_xform *xforms;
101 struct rte_security_ipsec_xform *sec_xform;
103 enum rte_security_session_action_type type;
104 enum rte_security_ipsec_sa_direction direction;
106 struct rte_security_ctx *security_ctx;
109 #define MAX_RTE_FLOW_PATTERN (4)
110 #define MAX_RTE_FLOW_ACTIONS (3)
111 struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
112 struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
113 struct rte_flow_attr attr;
115 struct rte_flow_item_ipv4 ipv4_spec;
116 struct rte_flow_item_ipv6 ipv6_spec;
118 struct rte_flow_item_esp esp_spec;
119 struct rte_flow *flow;
120 struct rte_security_session_conf sess_conf;
121 } __rte_cache_aligned;
123 struct ipsec_mbuf_metadata {
125 struct rte_crypto_op cop;
126 struct rte_crypto_sym_op sym_cop;
128 } __rte_cache_aligned;
135 struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
139 struct rte_hash *cdev_map;
140 struct sp_ctx *sp4_ctx;
141 struct sp_ctx *sp6_ctx;
142 struct sa_ctx *sa_ctx;
145 struct cdev_qp tbl[MAX_QP_PER_LCORE];
146 struct rte_mempool *session_pool;
147 struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
148 uint16_t ol_pkts_cnt;
159 struct sa_ctx *sa_in;
160 struct sa_ctx *sa_out;
161 struct sp_ctx *sp_ip4_in;
162 struct sp_ctx *sp_ip4_out;
163 struct sp_ctx *sp_ip6_in;
164 struct sp_ctx *sp_ip6_out;
165 struct rt_ctx *rt_ip4;
166 struct rt_ctx *rt_ip6;
167 struct rte_mempool *mbuf_pool;
168 struct rte_mempool *session_pool;
175 } __attribute__((packed));
178 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
179 uint16_t nb_pkts, uint16_t len);
182 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
183 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
185 static inline uint16_t
186 ipsec_metadata_size(void)
188 return sizeof(struct ipsec_mbuf_metadata);
191 static inline struct ipsec_mbuf_metadata *
192 get_priv(struct rte_mbuf *m)
194 return rte_mbuf_to_priv(m);
198 get_cnt_blk(struct rte_mbuf *m)
200 struct ipsec_mbuf_metadata *priv = get_priv(m);
202 return &priv->buf[0];
206 get_aad(struct rte_mbuf *m)
208 struct ipsec_mbuf_metadata *priv = get_priv(m);
210 return &priv->buf[16];
214 get_sym_cop(struct rte_crypto_op *cop)
220 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
223 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
224 struct ipsec_sa *sa[], uint16_t nb_pkts);
227 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
228 struct ipsec_sa *sa[], uint16_t nb_pkts);
231 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
234 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
237 sa_init(struct socket_ctx *ctx, int32_t socket_id);
240 rt_init(struct socket_ctx *ctx, int32_t socket_id);
242 #endif /* __IPSEC_H__ */