New upstream version 18.02
[deb_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
15 #define RTE_LOGTYPE_IPSEC       RTE_LOGTYPE_USER1
16 #define RTE_LOGTYPE_IPSEC_ESP   RTE_LOGTYPE_USER2
17 #define RTE_LOGTYPE_IPSEC_IPIP  RTE_LOGTYPE_USER3
18
19 #define MAX_PKT_BURST 32
20 #define MAX_QP_PER_LCORE 256
21
22 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
23
24 #define IV_OFFSET               (sizeof(struct rte_crypto_op) + \
25                                 sizeof(struct rte_crypto_sym_op))
26
27 #define uint32_t_to_char(ip, a, b, c, d) do {\
28                 *a = (uint8_t)(ip >> 24 & 0xff);\
29                 *b = (uint8_t)(ip >> 16 & 0xff);\
30                 *c = (uint8_t)(ip >> 8 & 0xff);\
31                 *d = (uint8_t)(ip & 0xff);\
32         } while (0)
33
34 #define DEFAULT_MAX_CATEGORIES  1
35
36 #define IPSEC_SA_MAX_ENTRIES (128) /* must be power of 2, max 2 power 30 */
37 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1))
38 #define INVALID_SPI (0)
39
40 #define DISCARD (0x80000000)
41 #define BYPASS (0x40000000)
42 #define PROTECT_MASK (0x3fffffff)
43 #define PROTECT(sa_idx) (SPI2IDX(sa_idx) & PROTECT_MASK) /* SA idx 30 bits */
44
45 #define IPSEC_XFORM_MAX 2
46
47 #define IP6_VERSION (6)
48
49 struct rte_crypto_xform;
50 struct ipsec_xform;
51 struct rte_mbuf;
52
53 struct ipsec_sa;
54
55 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
56                 struct rte_crypto_op *cop);
57
58 struct ip_addr {
59         union {
60                 uint32_t ip4;
61                 union {
62                         uint64_t ip6[2];
63                         uint8_t ip6_b[16];
64                 } ip6;
65         } ip;
66 };
67
68 #define MAX_KEY_SIZE            32
69
70 struct ipsec_sa {
71         uint32_t spi;
72         uint32_t cdev_id_qp;
73         uint64_t seq;
74         uint32_t salt;
75         union {
76                 struct rte_cryptodev_sym_session *crypto_session;
77                 struct rte_security_session *sec_session;
78         };
79         enum rte_crypto_cipher_algorithm cipher_algo;
80         enum rte_crypto_auth_algorithm auth_algo;
81         enum rte_crypto_aead_algorithm aead_algo;
82         uint16_t digest_len;
83         uint16_t iv_len;
84         uint16_t block_size;
85         uint16_t flags;
86 #define IP4_TUNNEL (1 << 0)
87 #define IP6_TUNNEL (1 << 1)
88 #define TRANSPORT  (1 << 2)
89         struct ip_addr src;
90         struct ip_addr dst;
91         uint8_t cipher_key[MAX_KEY_SIZE];
92         uint16_t cipher_key_len;
93         uint8_t auth_key[MAX_KEY_SIZE];
94         uint16_t auth_key_len;
95         uint16_t aad_len;
96         union {
97                 struct rte_crypto_sym_xform *xforms;
98                 struct rte_security_ipsec_xform *sec_xform;
99         };
100         enum rte_security_session_action_type type;
101         enum rte_security_ipsec_sa_direction direction;
102         uint16_t portid;
103         struct rte_security_ctx *security_ctx;
104         uint32_t ol_flags;
105
106 #define MAX_RTE_FLOW_PATTERN (4)
107 #define MAX_RTE_FLOW_ACTIONS (3)
108         struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
109         struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
110         struct rte_flow_attr attr;
111         union {
112                 struct rte_flow_item_ipv4 ipv4_spec;
113                 struct rte_flow_item_ipv6 ipv6_spec;
114         };
115         struct rte_flow_item_esp esp_spec;
116         struct rte_flow *flow;
117         struct rte_security_session_conf sess_conf;
118 } __rte_cache_aligned;
119
120 struct ipsec_mbuf_metadata {
121         struct ipsec_sa *sa;
122         struct rte_crypto_op cop;
123         struct rte_crypto_sym_op sym_cop;
124         uint8_t buf[32];
125 } __rte_cache_aligned;
126
127 struct cdev_qp {
128         uint16_t id;
129         uint16_t qp;
130         uint16_t in_flight;
131         uint16_t len;
132         struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
133 };
134
135 struct ipsec_ctx {
136         struct rte_hash *cdev_map;
137         struct sp_ctx *sp4_ctx;
138         struct sp_ctx *sp6_ctx;
139         struct sa_ctx *sa_ctx;
140         uint16_t nb_qps;
141         uint16_t last_qp;
142         struct cdev_qp tbl[MAX_QP_PER_LCORE];
143         struct rte_mempool *session_pool;
144         struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
145         uint16_t ol_pkts_cnt;
146 };
147
148 struct cdev_key {
149         uint16_t lcore_id;
150         uint8_t cipher_algo;
151         uint8_t auth_algo;
152         uint8_t aead_algo;
153 };
154
155 struct socket_ctx {
156         struct sa_ctx *sa_in;
157         struct sa_ctx *sa_out;
158         struct sp_ctx *sp_ip4_in;
159         struct sp_ctx *sp_ip4_out;
160         struct sp_ctx *sp_ip6_in;
161         struct sp_ctx *sp_ip6_out;
162         struct rt_ctx *rt_ip4;
163         struct rt_ctx *rt_ip6;
164         struct rte_mempool *mbuf_pool;
165         struct rte_mempool *session_pool;
166 };
167
168 struct cnt_blk {
169         uint32_t salt;
170         uint64_t iv;
171         uint32_t cnt;
172 } __attribute__((packed));
173
174 uint16_t
175 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
176                 uint16_t nb_pkts, uint16_t len);
177
178 uint16_t
179 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
180                 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
181
182 static inline uint16_t
183 ipsec_metadata_size(void)
184 {
185         return sizeof(struct ipsec_mbuf_metadata);
186 }
187
188 static inline struct ipsec_mbuf_metadata *
189 get_priv(struct rte_mbuf *m)
190 {
191         return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
192 }
193
194 static inline void *
195 get_cnt_blk(struct rte_mbuf *m)
196 {
197         struct ipsec_mbuf_metadata *priv = get_priv(m);
198
199         return &priv->buf[0];
200 }
201
202 static inline void *
203 get_aad(struct rte_mbuf *m)
204 {
205         struct ipsec_mbuf_metadata *priv = get_priv(m);
206
207         return &priv->buf[16];
208 }
209
210 static inline void *
211 get_sym_cop(struct rte_crypto_op *cop)
212 {
213         return (cop + 1);
214 }
215
216 int
217 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
218
219 void
220 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
221                 struct ipsec_sa *sa[], uint16_t nb_pkts);
222
223 void
224 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
225                 struct ipsec_sa *sa[], uint16_t nb_pkts);
226
227 void
228 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
229
230 void
231 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
232
233 void
234 sa_init(struct socket_ctx *ctx, int32_t socket_id);
235
236 void
237 rt_init(struct socket_ctx *ctx, int32_t socket_id);
238
239 #endif /* __IPSEC_H__ */