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_QP_PER_LCORE 256
22 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
24 #define IPSEC_OFFLOAD_ESN_SOFTLIMIT 0xffffff00
26 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
27 sizeof(struct rte_crypto_sym_op))
29 #define uint32_t_to_char(ip, a, b, c, d) do {\
30 *a = (uint8_t)(ip >> 24 & 0xff);\
31 *b = (uint8_t)(ip >> 16 & 0xff);\
32 *c = (uint8_t)(ip >> 8 & 0xff);\
33 *d = (uint8_t)(ip & 0xff);\
36 #define DEFAULT_MAX_CATEGORIES 1
38 #define IPSEC_SA_MAX_ENTRIES (128) /* must be power of 2, max 2 power 30 */
39 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1))
40 #define INVALID_SPI (0)
42 #define DISCARD (0x80000000)
43 #define BYPASS (0x40000000)
44 #define PROTECT_MASK (0x3fffffff)
45 #define PROTECT(sa_idx) (SPI2IDX(sa_idx) & PROTECT_MASK) /* SA idx 30 bits */
47 #define IPSEC_XFORM_MAX 2
49 #define IP6_VERSION (6)
51 struct rte_crypto_xform;
57 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
58 struct rte_crypto_op *cop);
70 #define MAX_KEY_SIZE 32
78 struct rte_cryptodev_sym_session *crypto_session;
79 struct rte_security_session *sec_session;
81 enum rte_crypto_cipher_algorithm cipher_algo;
82 enum rte_crypto_auth_algorithm auth_algo;
83 enum rte_crypto_aead_algorithm aead_algo;
88 #define IP4_TUNNEL (1 << 0)
89 #define IP6_TUNNEL (1 << 1)
90 #define TRANSPORT (1 << 2)
93 uint8_t cipher_key[MAX_KEY_SIZE];
94 uint16_t cipher_key_len;
95 uint8_t auth_key[MAX_KEY_SIZE];
96 uint16_t auth_key_len;
99 struct rte_crypto_sym_xform *xforms;
100 struct rte_security_ipsec_xform *sec_xform;
102 enum rte_security_session_action_type type;
103 enum rte_security_ipsec_sa_direction direction;
105 struct rte_security_ctx *security_ctx;
108 #define MAX_RTE_FLOW_PATTERN (4)
109 #define MAX_RTE_FLOW_ACTIONS (3)
110 struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
111 struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
112 struct rte_flow_attr attr;
114 struct rte_flow_item_ipv4 ipv4_spec;
115 struct rte_flow_item_ipv6 ipv6_spec;
117 struct rte_flow_item_esp esp_spec;
118 struct rte_flow *flow;
119 struct rte_security_session_conf sess_conf;
120 } __rte_cache_aligned;
122 struct ipsec_mbuf_metadata {
124 struct rte_crypto_op cop;
125 struct rte_crypto_sym_op sym_cop;
127 } __rte_cache_aligned;
134 struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
138 struct rte_hash *cdev_map;
139 struct sp_ctx *sp4_ctx;
140 struct sp_ctx *sp6_ctx;
141 struct sa_ctx *sa_ctx;
144 struct cdev_qp tbl[MAX_QP_PER_LCORE];
145 struct rte_mempool *session_pool;
146 struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
147 uint16_t ol_pkts_cnt;
158 struct sa_ctx *sa_in;
159 struct sa_ctx *sa_out;
160 struct sp_ctx *sp_ip4_in;
161 struct sp_ctx *sp_ip4_out;
162 struct sp_ctx *sp_ip6_in;
163 struct sp_ctx *sp_ip6_out;
164 struct rt_ctx *rt_ip4;
165 struct rt_ctx *rt_ip6;
166 struct rte_mempool *mbuf_pool;
167 struct rte_mempool *session_pool;
174 } __attribute__((packed));
177 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
178 uint16_t nb_pkts, uint16_t len);
181 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
182 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
184 static inline uint16_t
185 ipsec_metadata_size(void)
187 return sizeof(struct ipsec_mbuf_metadata);
190 static inline struct ipsec_mbuf_metadata *
191 get_priv(struct rte_mbuf *m)
193 return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
197 get_cnt_blk(struct rte_mbuf *m)
199 struct ipsec_mbuf_metadata *priv = get_priv(m);
201 return &priv->buf[0];
205 get_aad(struct rte_mbuf *m)
207 struct ipsec_mbuf_metadata *priv = get_priv(m);
209 return &priv->buf[16];
213 get_sym_cop(struct rte_crypto_op *cop)
219 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
222 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
223 struct ipsec_sa *sa[], uint16_t nb_pkts);
226 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
227 struct ipsec_sa *sa[], uint16_t nb_pkts);
230 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
233 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
236 sa_init(struct socket_ctx *ctx, int32_t socket_id);
239 rt_init(struct socket_ctx *ctx, int32_t socket_id);
241 #endif /* __IPSEC_H__ */