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