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 #include "ipsec-secgw.h"
18 #define RTE_LOGTYPE_IPSEC_ESP RTE_LOGTYPE_USER2
19 #define RTE_LOGTYPE_IPSEC_IPIP RTE_LOGTYPE_USER3
21 #define MAX_INFLIGHT 128
22 #define MAX_QP_PER_LCORE 256
24 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
26 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
27 sizeof(struct rte_crypto_sym_op))
29 #define DEFAULT_MAX_CATEGORIES 1
31 #define INVALID_SPI (0)
33 #define DISCARD INVALID_SPI
34 #define BYPASS UINT32_MAX
36 #define IPSEC_XFORM_MAX 2
38 #define IP6_VERSION (6)
40 struct rte_crypto_xform;
46 * Keeps number of configured SA's for each address family:
53 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
54 struct rte_crypto_op *cop);
66 #define MAX_KEY_SIZE 36
69 * application wide SA parameters
72 uint32_t enable; /* use librte_ipsec API for ipsec pkt processing */
73 uint32_t window_size; /* replay window size */
74 uint32_t enable_esn; /* enable/disable ESN support */
75 uint32_t cache_sz; /* per lcore SA cache size */
76 uint32_t udp_encap; /* enable/disable UDP Encapsulation */
77 uint64_t flags; /* rte_ipsec_sa_prm.flags */
80 extern struct app_sa_prm app_sa_prm;
83 struct rte_flow *rx_def_flow;
86 extern struct flow_info flow_info_tbl[RTE_MAX_ETHPORTS];
89 IPSEC_SESSION_PRIMARY = 0,
90 IPSEC_SESSION_FALLBACK = 1,
94 #define IPSEC_SA_OFFLOAD_FALLBACK_FLAG (1)
96 static inline struct ipsec_sa *
97 ipsec_mask_saptr(void *ptr)
99 uintptr_t i = (uintptr_t)ptr;
100 static const uintptr_t mask = IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
104 return (struct ipsec_sa *)i;
108 struct rte_ipsec_session sessions[IPSEC_SESSION_MAX];
113 uint32_t fallback_sessions;
114 enum rte_crypto_cipher_algorithm cipher_algo;
115 enum rte_crypto_auth_algorithm auth_algo;
116 enum rte_crypto_aead_algorithm aead_algo;
121 #define IP4_TUNNEL (1 << 0)
122 #define IP6_TUNNEL (1 << 1)
123 #define TRANSPORT (1 << 2)
124 #define IP4_TRANSPORT (1 << 3)
125 #define IP6_TRANSPORT (1 << 4)
128 uint8_t cipher_key[MAX_KEY_SIZE];
129 uint16_t cipher_key_len;
130 uint8_t auth_key[MAX_KEY_SIZE];
131 uint16_t auth_key_len;
134 struct rte_crypto_sym_xform *xforms;
135 struct rte_security_ipsec_xform *sec_xform;
137 enum rte_security_ipsec_sa_direction direction;
144 #define MAX_RTE_FLOW_PATTERN (4)
145 #define MAX_RTE_FLOW_ACTIONS (3)
146 struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
147 struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
148 struct rte_flow_attr attr;
150 struct rte_flow_item_ipv4 ipv4_spec;
151 struct rte_flow_item_ipv6 ipv6_spec;
153 struct rte_flow_item_esp esp_spec;
154 struct rte_flow *flow;
155 struct rte_security_session_conf sess_conf;
156 } __rte_cache_aligned;
159 struct rte_crypto_sym_xform a;
160 struct rte_crypto_sym_xform b;
164 struct rte_ipsec_sad *sad_v4;
165 struct rte_ipsec_sad *sad_v6;
169 void *satbl; /* pointer to array of rte_ipsec_sa objects*/
170 struct ipsec_sad sad;
173 struct ipsec_sa sa[];
176 struct ipsec_mbuf_metadata {
178 struct rte_crypto_op cop;
179 struct rte_crypto_sym_op sym_cop;
181 } __rte_cache_aligned;
183 #define IS_TRANSPORT(flags) ((flags) & TRANSPORT)
185 #define IS_TUNNEL(flags) ((flags) & (IP4_TUNNEL | IP6_TUNNEL))
187 #define IS_IP4(flags) ((flags) & (IP4_TUNNEL | IP4_TRANSPORT))
189 #define IS_IP6(flags) ((flags) & (IP6_TUNNEL | IP6_TRANSPORT))
191 #define IS_IP4_TUNNEL(flags) ((flags) & IP4_TUNNEL)
193 #define IS_IP6_TUNNEL(flags) ((flags) & IP6_TUNNEL)
196 * Macro for getting ipsec_sa flags statuses without version of protocol
197 * used for transport (IP4_TRANSPORT and IP6_TRANSPORT flags).
199 #define WITHOUT_TRANSPORT_VERSION(flags) \
200 ((flags) & (IP4_TUNNEL | \
209 struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
213 struct rte_hash *cdev_map;
214 struct sp_ctx *sp4_ctx;
215 struct sp_ctx *sp6_ctx;
216 struct sa_ctx *sa_ctx;
219 struct cdev_qp tbl[MAX_QP_PER_LCORE];
220 struct rte_mempool *session_pool;
221 struct rte_mempool *session_priv_pool;
222 struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
223 uint16_t ol_pkts_cnt;
224 uint64_t ipv4_offloads;
225 uint64_t ipv6_offloads;
236 struct sa_ctx *sa_in;
237 struct sa_ctx *sa_out;
238 struct sp_ctx *sp_ip4_in;
239 struct sp_ctx *sp_ip4_out;
240 struct sp_ctx *sp_ip6_in;
241 struct sp_ctx *sp_ip6_out;
242 struct rt_ctx *rt_ip4;
243 struct rt_ctx *rt_ip6;
244 struct rte_mempool *mbuf_pool;
245 struct rte_mempool *mbuf_pool_indir;
246 struct rte_mempool *session_pool;
247 struct rte_mempool *session_priv_pool;
257 extern struct socket_ctx socket_ctx[NB_SOCKETS];
260 ipsec_poll_mode_worker(void);
263 ipsec_launch_one_lcore(void *args);
265 extern struct ipsec_sa *sa_out;
266 extern uint32_t nb_sa_out;
268 extern struct ipsec_sa *sa_in;
269 extern uint32_t nb_sa_in;
272 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
273 uint16_t nb_pkts, uint16_t len);
276 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
277 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
280 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
284 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
288 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf);
291 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf);
293 static inline uint16_t
294 ipsec_metadata_size(void)
296 return sizeof(struct ipsec_mbuf_metadata);
299 static inline struct ipsec_mbuf_metadata *
300 get_priv(struct rte_mbuf *m)
302 return rte_mbuf_to_priv(m);
306 get_cnt_blk(struct rte_mbuf *m)
308 struct ipsec_mbuf_metadata *priv = get_priv(m);
310 return &priv->buf[0];
314 get_aad(struct rte_mbuf *m)
316 struct ipsec_mbuf_metadata *priv = get_priv(m);
318 return &priv->buf[16];
322 get_sym_cop(struct rte_crypto_op *cop)
327 static inline struct rte_ipsec_session *
328 ipsec_get_primary_session(struct ipsec_sa *sa)
330 return &sa->sessions[IPSEC_SESSION_PRIMARY];
333 static inline struct rte_ipsec_session *
334 ipsec_get_fallback_session(struct ipsec_sa *sa)
336 return &sa->sessions[IPSEC_SESSION_FALLBACK];
339 static inline enum rte_security_session_action_type
340 ipsec_get_action_type(struct ipsec_sa *sa)
342 struct rte_ipsec_session *ips;
343 ips = ipsec_get_primary_session(sa);
348 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
351 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
352 void *sa[], uint16_t nb_pkts);
355 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
356 void *sa[], uint16_t nb_pkts);
359 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
362 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
365 * Search through SP rules for given SPI.
366 * Returns first rule index if found(greater or equal then zero),
367 * or -ENOENT otherwise.
370 sp4_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2],
373 sp6_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2],
377 * Search through SA entries for given SPI.
378 * Returns first entry index if found(greater or equal then zero),
379 * or -ENOENT otherwise.
382 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound);
385 sa_init(struct socket_ctx *ctx, int32_t socket_id);
388 rt_init(struct socket_ctx *ctx, int32_t socket_id);
391 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
392 uint64_t *tx_offloads);
395 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr);
398 enqueue_cop_burst(struct cdev_qp *cqp);
401 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa,
402 struct rte_ipsec_session *ips);
405 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa,
406 struct rte_ipsec_session *ips);
408 check_flow_params(uint16_t fdir_portid, uint8_t fdir_qid);
411 create_ipsec_esp_flow(struct ipsec_sa *sa);
414 get_nb_crypto_sessions(void);
416 #endif /* __IPSEC_H__ */