examples/ipsec-secgw: implement inbound SAD
[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 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)
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         uint64_t flags;       /* rte_ipsec_sa_prm.flags */
87 };
88
89 extern struct app_sa_prm app_sa_prm;
90
91 enum {
92         IPSEC_SESSION_PRIMARY = 0,
93         IPSEC_SESSION_FALLBACK = 1,
94         IPSEC_SESSION_MAX
95 };
96
97 #define IPSEC_SA_OFFLOAD_FALLBACK_FLAG (1)
98
99 static inline struct ipsec_sa *
100 ipsec_mask_saptr(void *ptr)
101 {
102         uintptr_t i = (uintptr_t)ptr;
103         static const uintptr_t mask = IPSEC_SA_OFFLOAD_FALLBACK_FLAG;
104
105         i &= ~mask;
106
107         return (struct ipsec_sa *)i;
108 }
109
110 struct ipsec_sa {
111         struct rte_ipsec_session sessions[IPSEC_SESSION_MAX];
112         uint32_t spi;
113         uint32_t cdev_id_qp;
114         uint64_t seq;
115         uint32_t salt;
116         uint32_t fallback_sessions;
117         enum rte_crypto_cipher_algorithm cipher_algo;
118         enum rte_crypto_auth_algorithm auth_algo;
119         enum rte_crypto_aead_algorithm aead_algo;
120         uint16_t digest_len;
121         uint16_t iv_len;
122         uint16_t block_size;
123         uint16_t flags;
124 #define IP4_TUNNEL (1 << 0)
125 #define IP6_TUNNEL (1 << 1)
126 #define TRANSPORT  (1 << 2)
127 #define IP4_TRANSPORT (1 << 3)
128 #define IP6_TRANSPORT (1 << 4)
129         struct ip_addr src;
130         struct ip_addr dst;
131         uint8_t cipher_key[MAX_KEY_SIZE];
132         uint16_t cipher_key_len;
133         uint8_t auth_key[MAX_KEY_SIZE];
134         uint16_t auth_key_len;
135         uint16_t aad_len;
136         union {
137                 struct rte_crypto_sym_xform *xforms;
138                 struct rte_security_ipsec_xform *sec_xform;
139         };
140         enum rte_security_ipsec_sa_direction direction;
141         uint16_t portid;
142
143 #define MAX_RTE_FLOW_PATTERN (4)
144 #define MAX_RTE_FLOW_ACTIONS (3)
145         struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
146         struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
147         struct rte_flow_attr attr;
148         union {
149                 struct rte_flow_item_ipv4 ipv4_spec;
150                 struct rte_flow_item_ipv6 ipv6_spec;
151         };
152         struct rte_flow_item_esp esp_spec;
153         struct rte_flow *flow;
154         struct rte_security_session_conf sess_conf;
155 } __rte_cache_aligned;
156
157 struct ipsec_mbuf_metadata {
158         struct ipsec_sa *sa;
159         struct rte_crypto_op cop;
160         struct rte_crypto_sym_op sym_cop;
161         uint8_t buf[32];
162 } __rte_cache_aligned;
163
164 #define IS_TRANSPORT(flags) ((flags) & TRANSPORT)
165
166 #define IS_TUNNEL(flags) ((flags) & (IP4_TUNNEL | IP6_TUNNEL))
167
168 #define IS_IP4(flags) ((flags) & (IP4_TUNNEL | IP4_TRANSPORT))
169
170 #define IS_IP6(flags) ((flags) & (IP6_TUNNEL | IP6_TRANSPORT))
171
172 #define IS_IP4_TUNNEL(flags) ((flags) & IP4_TUNNEL)
173
174 #define IS_IP6_TUNNEL(flags) ((flags) & IP6_TUNNEL)
175
176 /*
177  * Macro for getting ipsec_sa flags statuses without version of protocol
178  * used for transport (IP4_TRANSPORT and IP6_TRANSPORT flags).
179  */
180 #define WITHOUT_TRANSPORT_VERSION(flags) \
181                 ((flags) & (IP4_TUNNEL | \
182                         IP6_TUNNEL | \
183                         TRANSPORT))
184
185 struct cdev_qp {
186         uint16_t id;
187         uint16_t qp;
188         uint16_t in_flight;
189         uint16_t len;
190         struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
191 };
192
193 struct ipsec_ctx {
194         struct rte_hash *cdev_map;
195         struct sp_ctx *sp4_ctx;
196         struct sp_ctx *sp6_ctx;
197         struct sa_ctx *sa_ctx;
198         uint16_t nb_qps;
199         uint16_t last_qp;
200         struct cdev_qp tbl[MAX_QP_PER_LCORE];
201         struct rte_mempool *session_pool;
202         struct rte_mempool *session_priv_pool;
203         struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
204         uint16_t ol_pkts_cnt;
205         uint64_t ipv4_offloads;
206         uint64_t ipv6_offloads;
207 };
208
209 struct cdev_key {
210         uint16_t lcore_id;
211         uint8_t cipher_algo;
212         uint8_t auth_algo;
213         uint8_t aead_algo;
214 };
215
216 struct socket_ctx {
217         struct sa_ctx *sa_in;
218         struct sa_ctx *sa_out;
219         struct sp_ctx *sp_ip4_in;
220         struct sp_ctx *sp_ip4_out;
221         struct sp_ctx *sp_ip6_in;
222         struct sp_ctx *sp_ip6_out;
223         struct rt_ctx *rt_ip4;
224         struct rt_ctx *rt_ip6;
225         struct rte_mempool *mbuf_pool;
226         struct rte_mempool *mbuf_pool_indir;
227         struct rte_mempool *session_pool;
228         struct rte_mempool *session_priv_pool;
229 };
230
231 struct cnt_blk {
232         uint32_t salt;
233         uint64_t iv;
234         uint32_t cnt;
235 } __attribute__((packed));
236
237 struct traffic_type {
238         const uint8_t *data[MAX_PKT_BURST * 2];
239         struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
240         void *saptr[MAX_PKT_BURST * 2];
241         uint32_t res[MAX_PKT_BURST * 2];
242         uint32_t num;
243 };
244
245 struct ipsec_traffic {
246         struct traffic_type ipsec;
247         struct traffic_type ip4;
248         struct traffic_type ip6;
249 };
250
251 uint16_t
252 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
253                 uint16_t nb_pkts, uint16_t len);
254
255 uint16_t
256 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
257                 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
258
259 uint16_t
260 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
261                 uint16_t len);
262
263 uint16_t
264 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
265                 uint16_t len);
266
267 void
268 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf);
269
270 void
271 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf);
272
273 static inline uint16_t
274 ipsec_metadata_size(void)
275 {
276         return sizeof(struct ipsec_mbuf_metadata);
277 }
278
279 static inline struct ipsec_mbuf_metadata *
280 get_priv(struct rte_mbuf *m)
281 {
282         return rte_mbuf_to_priv(m);
283 }
284
285 static inline void *
286 get_cnt_blk(struct rte_mbuf *m)
287 {
288         struct ipsec_mbuf_metadata *priv = get_priv(m);
289
290         return &priv->buf[0];
291 }
292
293 static inline void *
294 get_aad(struct rte_mbuf *m)
295 {
296         struct ipsec_mbuf_metadata *priv = get_priv(m);
297
298         return &priv->buf[16];
299 }
300
301 static inline void *
302 get_sym_cop(struct rte_crypto_op *cop)
303 {
304         return (cop + 1);
305 }
306
307 static inline struct rte_ipsec_session *
308 ipsec_get_primary_session(struct ipsec_sa *sa)
309 {
310         return &sa->sessions[IPSEC_SESSION_PRIMARY];
311 }
312
313 static inline struct rte_ipsec_session *
314 ipsec_get_fallback_session(struct ipsec_sa *sa)
315 {
316         return &sa->sessions[IPSEC_SESSION_FALLBACK];
317 }
318
319 static inline enum rte_security_session_action_type
320 ipsec_get_action_type(struct ipsec_sa *sa)
321 {
322         struct rte_ipsec_session *ips;
323         ips = ipsec_get_primary_session(sa);
324         return ips->type;
325 }
326
327 int
328 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
329
330 void
331 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
332                 void *sa[], uint16_t nb_pkts);
333
334 void
335 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
336                 void *sa[], uint16_t nb_pkts);
337
338 void
339 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
340
341 void
342 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
343
344 /*
345  * Search through SP rules for given SPI.
346  * Returns first rule index if found(greater or equal then zero),
347  * or -ENOENT otherwise.
348  */
349 int
350 sp4_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2],
351                         uint32_t mask[2]);
352 int
353 sp6_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2],
354                         uint32_t mask[2]);
355
356 /*
357  * Search through SA entries for given SPI.
358  * Returns first entry index if found(greater or equal then zero),
359  * or -ENOENT otherwise.
360  */
361 int
362 sa_spi_present(uint32_t spi, int inbound);
363
364 void
365 sa_init(struct socket_ctx *ctx, int32_t socket_id);
366
367 void
368 rt_init(struct socket_ctx *ctx, int32_t socket_id);
369
370 int
371 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
372                 uint64_t *tx_offloads);
373
374 int
375 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr);
376
377 void
378 enqueue_cop_burst(struct cdev_qp *cqp);
379
380 int
381 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa,
382                 struct rte_ipsec_session *ips);
383
384 int
385 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa,
386                 struct rte_ipsec_session *ips);
387
388 #endif /* __IPSEC_H__ */