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