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