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