xref: /f-stack/dpdk/examples/ipsec-secgw/ipsec.h (revision 2bfe3f2e)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef __IPSEC_H__
35 #define __IPSEC_H__
36 
37 #include <stdint.h>
38 
39 #include <rte_byteorder.h>
40 #include <rte_crypto.h>
41 #include <rte_security.h>
42 #include <rte_flow.h>
43 
44 #define RTE_LOGTYPE_IPSEC       RTE_LOGTYPE_USER1
45 #define RTE_LOGTYPE_IPSEC_ESP   RTE_LOGTYPE_USER2
46 #define RTE_LOGTYPE_IPSEC_IPIP  RTE_LOGTYPE_USER3
47 
48 #define MAX_PKT_BURST 32
49 #define MAX_QP_PER_LCORE 256
50 
51 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
52 
53 #define IV_OFFSET		(sizeof(struct rte_crypto_op) + \
54 				sizeof(struct rte_crypto_sym_op))
55 
56 #define uint32_t_to_char(ip, a, b, c, d) do {\
57 		*a = (uint8_t)(ip >> 24 & 0xff);\
58 		*b = (uint8_t)(ip >> 16 & 0xff);\
59 		*c = (uint8_t)(ip >> 8 & 0xff);\
60 		*d = (uint8_t)(ip & 0xff);\
61 	} while (0)
62 
63 #define DEFAULT_MAX_CATEGORIES	1
64 
65 #define IPSEC_SA_MAX_ENTRIES (128) /* must be power of 2, max 2 power 30 */
66 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1))
67 #define INVALID_SPI (0)
68 
69 #define DISCARD (0x80000000)
70 #define BYPASS (0x40000000)
71 #define PROTECT_MASK (0x3fffffff)
72 #define PROTECT(sa_idx) (SPI2IDX(sa_idx) & PROTECT_MASK) /* SA idx 30 bits */
73 
74 #define IPSEC_XFORM_MAX 2
75 
76 #define IP6_VERSION (6)
77 
78 struct rte_crypto_xform;
79 struct ipsec_xform;
80 struct rte_mbuf;
81 
82 struct ipsec_sa;
83 
84 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
85 		struct rte_crypto_op *cop);
86 
87 struct ip_addr {
88 	union {
89 		uint32_t ip4;
90 		union {
91 			uint64_t ip6[2];
92 			uint8_t ip6_b[16];
93 		} ip6;
94 	} ip;
95 };
96 
97 #define MAX_KEY_SIZE		32
98 
99 struct ipsec_sa {
100 	uint32_t spi;
101 	uint32_t cdev_id_qp;
102 	uint64_t seq;
103 	uint32_t salt;
104 	union {
105 		struct rte_cryptodev_sym_session *crypto_session;
106 		struct rte_security_session *sec_session;
107 	};
108 	enum rte_crypto_cipher_algorithm cipher_algo;
109 	enum rte_crypto_auth_algorithm auth_algo;
110 	enum rte_crypto_aead_algorithm aead_algo;
111 	uint16_t digest_len;
112 	uint16_t iv_len;
113 	uint16_t block_size;
114 	uint16_t flags;
115 #define IP4_TUNNEL (1 << 0)
116 #define IP6_TUNNEL (1 << 1)
117 #define TRANSPORT  (1 << 2)
118 	struct ip_addr src;
119 	struct ip_addr dst;
120 	uint8_t cipher_key[MAX_KEY_SIZE];
121 	uint16_t cipher_key_len;
122 	uint8_t auth_key[MAX_KEY_SIZE];
123 	uint16_t auth_key_len;
124 	uint16_t aad_len;
125 	union {
126 		struct rte_crypto_sym_xform *xforms;
127 		struct rte_security_ipsec_xform *sec_xform;
128 	};
129 	enum rte_security_session_action_type type;
130 	enum rte_security_ipsec_sa_direction direction;
131 	uint16_t portid;
132 	struct rte_security_ctx *security_ctx;
133 	uint32_t ol_flags;
134 
135 #define MAX_RTE_FLOW_PATTERN (4)
136 #define MAX_RTE_FLOW_ACTIONS (2)
137 	struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
138 	struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
139 	struct rte_flow_attr attr;
140 	union {
141 		struct rte_flow_item_ipv4 ipv4_spec;
142 		struct rte_flow_item_ipv6 ipv6_spec;
143 	};
144 	struct rte_flow_item_esp esp_spec;
145 	struct rte_flow *flow;
146 	struct rte_security_session_conf sess_conf;
147 } __rte_cache_aligned;
148 
149 struct ipsec_mbuf_metadata {
150 	struct ipsec_sa *sa;
151 	struct rte_crypto_op cop;
152 	struct rte_crypto_sym_op sym_cop;
153 	uint8_t buf[32];
154 } __rte_cache_aligned;
155 
156 struct cdev_qp {
157 	uint16_t id;
158 	uint16_t qp;
159 	uint16_t in_flight;
160 	uint16_t len;
161 	struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
162 	struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
163 	uint16_t ol_pkts_cnt;
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 };
176 
177 struct cdev_key {
178 	uint16_t lcore_id;
179 	uint8_t cipher_algo;
180 	uint8_t auth_algo;
181 	uint8_t aead_algo;
182 };
183 
184 struct socket_ctx {
185 	struct sa_ctx *sa_in;
186 	struct sa_ctx *sa_out;
187 	struct sp_ctx *sp_ip4_in;
188 	struct sp_ctx *sp_ip4_out;
189 	struct sp_ctx *sp_ip6_in;
190 	struct sp_ctx *sp_ip6_out;
191 	struct rt_ctx *rt_ip4;
192 	struct rt_ctx *rt_ip6;
193 	struct rte_mempool *mbuf_pool;
194 	struct rte_mempool *session_pool;
195 };
196 
197 struct cnt_blk {
198 	uint32_t salt;
199 	uint64_t iv;
200 	uint32_t cnt;
201 } __attribute__((packed));
202 
203 uint16_t
204 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
205 		uint16_t nb_pkts, uint16_t len);
206 
207 uint16_t
208 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
209 		uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
210 
211 static inline uint16_t
212 ipsec_metadata_size(void)
213 {
214 	return sizeof(struct ipsec_mbuf_metadata);
215 }
216 
217 static inline struct ipsec_mbuf_metadata *
218 get_priv(struct rte_mbuf *m)
219 {
220 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
221 }
222 
223 static inline void *
224 get_cnt_blk(struct rte_mbuf *m)
225 {
226 	struct ipsec_mbuf_metadata *priv = get_priv(m);
227 
228 	return &priv->buf[0];
229 }
230 
231 static inline void *
232 get_aad(struct rte_mbuf *m)
233 {
234 	struct ipsec_mbuf_metadata *priv = get_priv(m);
235 
236 	return &priv->buf[16];
237 }
238 
239 static inline void *
240 get_sym_cop(struct rte_crypto_op *cop)
241 {
242 	return (cop + 1);
243 }
244 
245 int
246 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
247 
248 void
249 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
250 		struct ipsec_sa *sa[], uint16_t nb_pkts);
251 
252 void
253 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
254 		struct ipsec_sa *sa[], uint16_t nb_pkts);
255 
256 void
257 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
258 
259 void
260 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
261 
262 void
263 sa_init(struct socket_ctx *ctx, int32_t socket_id);
264 
265 void
266 rt_init(struct socket_ctx *ctx, int32_t socket_id);
267 
268 #endif /* __IPSEC_H__ */
269