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