xref: /dpdk/examples/ipsec-secgw/ipsec.h (revision 97b914f4e715565d53d38ac6e04815b9be5e58a9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4 
5 #ifndef __IPSEC_H__
6 #define __IPSEC_H__
7 
8 #include <stdint.h>
9 
10 #include <rte_byteorder.h>
11 #include <rte_crypto.h>
12 #include <rte_security.h>
13 #include <rte_flow.h>
14 #include <rte_ipsec.h>
15 
16 #include "ipsec-secgw.h"
17 
18 #define RTE_LOGTYPE_IPSEC_ESP   RTE_LOGTYPE_USER2
19 #define RTE_LOGTYPE_IPSEC_IPIP  RTE_LOGTYPE_USER3
20 
21 #define MAX_INFLIGHT 128
22 #define MAX_QP_PER_LCORE 256
23 
24 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
25 
26 #define IV_OFFSET		(sizeof(struct rte_crypto_op) + \
27 				sizeof(struct rte_crypto_sym_op))
28 
29 #define DEFAULT_MAX_CATEGORIES	1
30 
31 #define INVALID_SPI (0)
32 
33 #define DISCARD	INVALID_SPI
34 #define BYPASS	UINT32_MAX
35 
36 #define IPSEC_XFORM_MAX 2
37 
38 #define IP6_VERSION (6)
39 
40 struct rte_crypto_xform;
41 struct ipsec_xform;
42 struct rte_mbuf;
43 
44 struct ipsec_sa;
45 /*
46  * Keeps number of configured SA's for each address family:
47  */
48 struct ipsec_sa_cnt {
49 	uint32_t	nb_v4;
50 	uint32_t	nb_v6;
51 };
52 
53 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
54 		struct rte_crypto_op *cop);
55 
56 struct ip_addr {
57 	union {
58 		uint32_t ip4;
59 		union {
60 			uint64_t ip6[2];
61 			uint8_t ip6_b[16];
62 		} ip6;
63 	} ip;
64 };
65 
66 #define MAX_KEY_SIZE		64
67 /*
68  * application wide SA parameters
69  */
70 struct app_sa_prm {
71 	uint32_t enable; /* use librte_ipsec API for ipsec pkt processing */
72 	uint32_t window_size; /* replay window size */
73 	uint32_t enable_esn;  /* enable/disable ESN support */
74 	uint32_t cache_sz;	/* per lcore SA cache size */
75 	uint32_t udp_encap;   /* enable/disable UDP Encapsulation */
76 	uint64_t flags;       /* rte_ipsec_sa_prm.flags */
77 };
78 
79 extern struct app_sa_prm app_sa_prm;
80 
81 struct flow_info {
82 	struct rte_flow *rx_def_flow;
83 };
84 
85 extern struct flow_info flow_info_tbl[RTE_MAX_ETHPORTS];
86 
87 enum {
88 	IPSEC_SESSION_PRIMARY = 0,
89 	IPSEC_SESSION_FALLBACK = 1,
90 	IPSEC_SESSION_MAX
91 };
92 
93 #define IPSEC_SA_OFFLOAD_FALLBACK_FLAG (1)
94 
95 static inline struct ipsec_sa *
96 ipsec_mask_saptr(void *ptr)
97 {
98 	uintptr_t i = (uintptr_t)ptr;
99 	static const uintptr_t mask = IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
100 
101 	i &= ~mask;
102 
103 	return (struct ipsec_sa *)i;
104 }
105 
106 struct ipsec_sa {
107 	struct rte_ipsec_session sessions[IPSEC_SESSION_MAX];
108 	uint32_t spi;
109 	uint32_t cdev_id_qp;
110 	uint64_t seq;
111 	uint32_t salt;
112 	uint32_t fallback_sessions;
113 	enum rte_crypto_cipher_algorithm cipher_algo;
114 	enum rte_crypto_auth_algorithm auth_algo;
115 	enum rte_crypto_aead_algorithm aead_algo;
116 	uint16_t digest_len;
117 	uint16_t iv_len;
118 	uint16_t block_size;
119 	uint16_t flags;
120 #define IP4_TUNNEL (1 << 0)
121 #define IP6_TUNNEL (1 << 1)
122 #define TRANSPORT  (1 << 2)
123 #define IP4_TRANSPORT (1 << 3)
124 #define IP6_TRANSPORT (1 << 4)
125 #define SA_TELEMETRY_ENABLE (1 << 5)
126 
127 	struct ip_addr src;
128 	struct ip_addr dst;
129 	struct {
130 		uint16_t sport;
131 		uint16_t dport;
132 	} udp;
133 	uint8_t cipher_key[MAX_KEY_SIZE];
134 	uint16_t cipher_key_len;
135 	uint8_t auth_key[MAX_KEY_SIZE];
136 	uint16_t auth_key_len;
137 	uint16_t aad_len;
138 	union {
139 		struct rte_crypto_sym_xform *xforms;
140 		struct rte_security_ipsec_xform *sec_xform;
141 	};
142 	enum rte_security_ipsec_sa_direction direction;
143 	uint8_t udp_encap;
144 	uint16_t portid;
145 	uint64_t esn;
146 	uint16_t mss;
147 	uint8_t fdir_qid;
148 	uint8_t fdir_flag;
149 
150 #define MAX_RTE_FLOW_PATTERN (5)
151 #define MAX_RTE_FLOW_ACTIONS (3)
152 	struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
153 	struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
154 	struct rte_flow_attr attr;
155 	union {
156 		struct rte_flow_item_ipv4 ipv4_spec;
157 		struct rte_flow_item_ipv6 ipv6_spec;
158 	};
159 	struct rte_flow_item_udp udp_spec;
160 	struct rte_flow_item_esp esp_spec;
161 	struct rte_flow *flow;
162 	struct rte_security_session_conf sess_conf;
163 } __rte_cache_aligned;
164 
165 struct ipsec_xf {
166 	struct rte_crypto_sym_xform a;
167 	struct rte_crypto_sym_xform b;
168 };
169 
170 struct ipsec_sad {
171 	struct rte_ipsec_sad *sad_v4;
172 	struct rte_ipsec_sad *sad_v6;
173 };
174 
175 struct sa_ctx {
176 	void *satbl; /* pointer to array of rte_ipsec_sa objects*/
177 	struct ipsec_sad sad;
178 	struct ipsec_xf *xf;
179 	uint32_t nb_sa;
180 	struct ipsec_sa sa[];
181 };
182 
183 struct ipsec_mbuf_metadata {
184 	struct ipsec_sa *sa;
185 	struct rte_crypto_op cop;
186 	struct rte_crypto_sym_op sym_cop;
187 	uint8_t buf[32];
188 } __rte_cache_aligned;
189 
190 #define IS_TRANSPORT(flags) ((flags) & TRANSPORT)
191 
192 #define IS_TUNNEL(flags) ((flags) & (IP4_TUNNEL | IP6_TUNNEL))
193 
194 #define IS_IP4(flags) ((flags) & (IP4_TUNNEL | IP4_TRANSPORT))
195 
196 #define IS_IP6(flags) ((flags) & (IP6_TUNNEL | IP6_TRANSPORT))
197 
198 #define IS_IP4_TUNNEL(flags) ((flags) & IP4_TUNNEL)
199 
200 #define IS_IP6_TUNNEL(flags) ((flags) & IP6_TUNNEL)
201 
202 /*
203  * Macro for getting ipsec_sa flags statuses without version of protocol
204  * used for transport (IP4_TRANSPORT and IP6_TRANSPORT flags).
205  */
206 #define WITHOUT_TRANSPORT_VERSION(flags) \
207 		((flags) & (IP4_TUNNEL | \
208 			IP6_TUNNEL | \
209 			TRANSPORT))
210 
211 struct cdev_qp {
212 	uint16_t id;
213 	uint16_t qp;
214 	uint16_t in_flight;
215 	uint16_t len;
216 	struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
217 };
218 
219 struct ipsec_ctx {
220 	struct rte_hash *cdev_map;
221 	struct sp_ctx *sp4_ctx;
222 	struct sp_ctx *sp6_ctx;
223 	struct sa_ctx *sa_ctx;
224 	uint16_t nb_qps;
225 	uint16_t last_qp;
226 	struct cdev_qp tbl[MAX_QP_PER_LCORE];
227 	struct rte_mempool *session_pool;
228 	struct rte_mempool *session_priv_pool;
229 	struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
230 	uint16_t ol_pkts_cnt;
231 	uint64_t ipv4_offloads;
232 	uint64_t ipv6_offloads;
233 };
234 
235 struct cdev_key {
236 	uint16_t lcore_id;
237 	uint8_t cipher_algo;
238 	uint8_t auth_algo;
239 	uint8_t aead_algo;
240 };
241 
242 struct socket_ctx {
243 	struct sa_ctx *sa_in;
244 	struct sa_ctx *sa_out;
245 	struct sp_ctx *sp_ip4_in;
246 	struct sp_ctx *sp_ip4_out;
247 	struct sp_ctx *sp_ip6_in;
248 	struct sp_ctx *sp_ip6_out;
249 	struct rt_ctx *rt_ip4;
250 	struct rt_ctx *rt_ip6;
251 	struct rte_mempool *mbuf_pool[RTE_MAX_ETHPORTS];
252 	struct rte_mempool *mbuf_pool_indir;
253 	struct rte_mempool *session_pool;
254 	struct rte_mempool *session_priv_pool;
255 };
256 
257 struct cnt_blk {
258 	uint32_t salt;
259 	uint64_t iv;
260 	uint32_t cnt;
261 } __rte_packed;
262 
263 /* Socket ctx */
264 extern struct socket_ctx socket_ctx[NB_SOCKETS];
265 
266 void
267 ipsec_poll_mode_worker(void);
268 
269 int
270 ipsec_launch_one_lcore(void *args);
271 
272 extern struct ipsec_sa *sa_out;
273 extern uint32_t nb_sa_out;
274 
275 extern struct ipsec_sa *sa_in;
276 extern uint32_t nb_sa_in;
277 
278 uint16_t
279 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
280 		uint16_t nb_pkts, uint16_t len);
281 
282 uint16_t
283 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
284 		uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
285 
286 uint16_t
287 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
288 		uint16_t len);
289 
290 uint16_t
291 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
292 		uint16_t len);
293 
294 void
295 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf);
296 
297 void
298 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf);
299 
300 static inline uint16_t
301 ipsec_metadata_size(void)
302 {
303 	return sizeof(struct ipsec_mbuf_metadata);
304 }
305 
306 static inline struct ipsec_mbuf_metadata *
307 get_priv(struct rte_mbuf *m)
308 {
309 	return rte_mbuf_to_priv(m);
310 }
311 
312 static inline void *
313 get_cnt_blk(struct rte_mbuf *m)
314 {
315 	struct ipsec_mbuf_metadata *priv = get_priv(m);
316 
317 	return &priv->buf[0];
318 }
319 
320 static inline void *
321 get_aad(struct rte_mbuf *m)
322 {
323 	struct ipsec_mbuf_metadata *priv = get_priv(m);
324 
325 	return &priv->buf[16];
326 }
327 
328 static inline void *
329 get_sym_cop(struct rte_crypto_op *cop)
330 {
331 	return (cop + 1);
332 }
333 
334 static inline struct rte_ipsec_session *
335 ipsec_get_primary_session(struct ipsec_sa *sa)
336 {
337 	return &sa->sessions[IPSEC_SESSION_PRIMARY];
338 }
339 
340 static inline struct rte_ipsec_session *
341 ipsec_get_fallback_session(struct ipsec_sa *sa)
342 {
343 	return &sa->sessions[IPSEC_SESSION_FALLBACK];
344 }
345 
346 static inline enum rte_security_session_action_type
347 ipsec_get_action_type(struct ipsec_sa *sa)
348 {
349 	struct rte_ipsec_session *ips;
350 	ips = ipsec_get_primary_session(sa);
351 	return ips->type;
352 }
353 
354 int
355 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
356 
357 void
358 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
359 		void *sa[], uint16_t nb_pkts);
360 
361 void
362 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
363 		void *sa[], uint16_t nb_pkts);
364 
365 void
366 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
367 
368 void
369 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
370 
371 /*
372  * Search through SP rules for given SPI.
373  * Returns first rule index if found(greater or equal then zero),
374  * or -ENOENT otherwise.
375  */
376 int
377 sp4_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2],
378 			uint32_t mask[2]);
379 int
380 sp6_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2],
381 			uint32_t mask[2]);
382 
383 /*
384  * Search through SA entries for given SPI.
385  * Returns first entry index if found(greater or equal then zero),
386  * or -ENOENT otherwise.
387  */
388 int
389 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound);
390 
391 void
392 sa_init(struct socket_ctx *ctx, int32_t socket_id);
393 
394 void
395 rt_init(struct socket_ctx *ctx, int32_t socket_id);
396 
397 int
398 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
399 		uint64_t *tx_offloads);
400 
401 int
402 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr);
403 
404 void
405 enqueue_cop_burst(struct cdev_qp *cqp);
406 
407 int
408 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa,
409 		struct rte_ipsec_session *ips);
410 
411 int
412 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa,
413 		struct rte_ipsec_session *ips);
414 int
415 check_flow_params(uint16_t fdir_portid, uint8_t fdir_qid);
416 
417 int
418 create_ipsec_esp_flow(struct ipsec_sa *sa);
419 
420 uint32_t
421 get_nb_crypto_sessions(void);
422 
423 #endif /* __IPSEC_H__ */
424