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>
14 #include <rte_ipsec.h>
16 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
17 #define RTE_LOGTYPE_IPSEC_ESP RTE_LOGTYPE_USER2
18 #define RTE_LOGTYPE_IPSEC_IPIP RTE_LOGTYPE_USER3
20 #define MAX_PKT_BURST 32
21 #define MAX_INFLIGHT 128
22 #define MAX_QP_PER_LCORE 256
24 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
26 #define IPSEC_OFFLOAD_ESN_SOFTLIMIT 0xffffff00
28 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
29 sizeof(struct rte_crypto_sym_op))
31 #define uint32_t_to_char(ip, a, b, c, d) do {\
32 *a = (uint8_t)(ip >> 24 & 0xff);\
33 *b = (uint8_t)(ip >> 16 & 0xff);\
34 *c = (uint8_t)(ip >> 8 & 0xff);\
35 *d = (uint8_t)(ip & 0xff);\
38 #define DEFAULT_MAX_CATEGORIES 1
40 #define IPSEC_SA_MAX_ENTRIES (128) /* must be power of 2, max 2 power 30 */
41 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1))
42 #define INVALID_SPI (0)
44 #define DISCARD INVALID_SPI
45 #define BYPASS UINT32_MAX
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
73 * application wide SA parameters
76 uint32_t enable; /* use librte_ipsec API for ipsec pkt processing */
77 uint32_t window_size; /* replay window size */
78 uint32_t enable_esn; /* enable/disable ESN support */
79 uint64_t flags; /* rte_ipsec_sa_prm.flags */
82 extern struct app_sa_prm app_sa_prm;
85 struct rte_ipsec_session ips; /* one session per sa for now */
91 struct rte_cryptodev_sym_session *crypto_session;
92 struct rte_security_session *sec_session;
94 enum rte_crypto_cipher_algorithm cipher_algo;
95 enum rte_crypto_auth_algorithm auth_algo;
96 enum rte_crypto_aead_algorithm aead_algo;
101 #define IP4_TUNNEL (1 << 0)
102 #define IP6_TUNNEL (1 << 1)
103 #define TRANSPORT (1 << 2)
106 uint8_t cipher_key[MAX_KEY_SIZE];
107 uint16_t cipher_key_len;
108 uint8_t auth_key[MAX_KEY_SIZE];
109 uint16_t auth_key_len;
112 struct rte_crypto_sym_xform *xforms;
113 struct rte_security_ipsec_xform *sec_xform;
115 enum rte_security_session_action_type type;
116 enum rte_security_ipsec_sa_direction direction;
118 struct rte_security_ctx *security_ctx;
121 #define MAX_RTE_FLOW_PATTERN (4)
122 #define MAX_RTE_FLOW_ACTIONS (3)
123 struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
124 struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
125 struct rte_flow_attr attr;
127 struct rte_flow_item_ipv4 ipv4_spec;
128 struct rte_flow_item_ipv6 ipv6_spec;
130 struct rte_flow_item_esp esp_spec;
131 struct rte_flow *flow;
132 struct rte_security_session_conf sess_conf;
133 } __rte_cache_aligned;
135 struct ipsec_mbuf_metadata {
137 struct rte_crypto_op cop;
138 struct rte_crypto_sym_op sym_cop;
140 } __rte_cache_aligned;
147 struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
151 struct rte_hash *cdev_map;
152 struct sp_ctx *sp4_ctx;
153 struct sp_ctx *sp6_ctx;
154 struct sa_ctx *sa_ctx;
157 struct cdev_qp tbl[MAX_QP_PER_LCORE];
158 struct rte_mempool *session_pool;
159 struct rte_mempool *session_priv_pool;
160 struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
161 uint16_t ol_pkts_cnt;
162 uint64_t ipv4_offloads;
163 uint64_t ipv6_offloads;
174 struct sa_ctx *sa_in;
175 struct sa_ctx *sa_out;
176 struct sp_ctx *sp_ip4_in;
177 struct sp_ctx *sp_ip4_out;
178 struct sp_ctx *sp_ip6_in;
179 struct sp_ctx *sp_ip6_out;
180 struct rt_ctx *rt_ip4;
181 struct rt_ctx *rt_ip6;
182 struct rte_mempool *mbuf_pool;
183 struct rte_mempool *session_pool;
184 struct rte_mempool *session_priv_pool;
191 } __attribute__((packed));
193 struct traffic_type {
194 const uint8_t *data[MAX_PKT_BURST * 2];
195 struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
196 struct ipsec_sa *saptr[MAX_PKT_BURST * 2];
197 uint32_t res[MAX_PKT_BURST * 2];
201 struct ipsec_traffic {
202 struct traffic_type ipsec;
203 struct traffic_type ip4;
204 struct traffic_type ip6;
208 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
209 uint16_t nb_pkts, uint16_t len);
212 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
213 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
216 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
220 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
224 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf);
227 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf);
229 static inline uint16_t
230 ipsec_metadata_size(void)
232 return sizeof(struct ipsec_mbuf_metadata);
235 static inline struct ipsec_mbuf_metadata *
236 get_priv(struct rte_mbuf *m)
238 return rte_mbuf_to_priv(m);
242 get_cnt_blk(struct rte_mbuf *m)
244 struct ipsec_mbuf_metadata *priv = get_priv(m);
246 return &priv->buf[0];
250 get_aad(struct rte_mbuf *m)
252 struct ipsec_mbuf_metadata *priv = get_priv(m);
254 return &priv->buf[16];
258 get_sym_cop(struct rte_crypto_op *cop)
264 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
267 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
268 struct ipsec_sa *sa[], uint16_t nb_pkts);
271 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
272 struct ipsec_sa *sa[], uint16_t nb_pkts);
275 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
278 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
281 * Search through SP rules for given SPI.
282 * Returns first rule index if found(greater or equal then zero),
283 * or -ENOENT otherwise.
286 sp4_spi_present(uint32_t spi, int inbound);
288 sp6_spi_present(uint32_t spi, int inbound);
291 * Search through SA entries for given SPI.
292 * Returns first entry index if found(greater or equal then zero),
293 * or -ENOENT otherwise.
296 sa_spi_present(uint32_t spi, int inbound);
299 sa_init(struct socket_ctx *ctx, int32_t socket_id);
302 rt_init(struct socket_ctx *ctx, int32_t socket_id);
305 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
306 uint64_t *tx_offloads);
309 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr);
312 enqueue_cop_burst(struct cdev_qp *cqp);
315 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa);
317 #endif /* __IPSEC_H__ */