da1fb1b296ab2bc102b4e775a59287f07f9c4edf
[deb_dpdk.git] / examples / ipsec-secgw / ipsec.h
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
42 #define RTE_LOGTYPE_IPSEC       RTE_LOGTYPE_USER1
43 #define RTE_LOGTYPE_IPSEC_ESP   RTE_LOGTYPE_USER2
44 #define RTE_LOGTYPE_IPSEC_IPIP  RTE_LOGTYPE_USER3
45
46 #define MAX_PKT_BURST 32
47 #define MAX_QP_PER_LCORE 256
48
49 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
50
51 #define IV_OFFSET               (sizeof(struct rte_crypto_op) + \
52                                 sizeof(struct rte_crypto_sym_op))
53
54 #define uint32_t_to_char(ip, a, b, c, d) do {\
55                 *a = (uint8_t)(ip >> 24 & 0xff);\
56                 *b = (uint8_t)(ip >> 16 & 0xff);\
57                 *c = (uint8_t)(ip >> 8 & 0xff);\
58                 *d = (uint8_t)(ip & 0xff);\
59         } while (0)
60
61 #define DEFAULT_MAX_CATEGORIES  1
62
63 #define IPSEC_SA_MAX_ENTRIES (128) /* must be power of 2, max 2 power 30 */
64 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1))
65 #define INVALID_SPI (0)
66
67 #define DISCARD (0x80000000)
68 #define BYPASS (0x40000000)
69 #define PROTECT_MASK (0x3fffffff)
70 #define PROTECT(sa_idx) (SPI2IDX(sa_idx) & PROTECT_MASK) /* SA idx 30 bits */
71
72 #define IPSEC_XFORM_MAX 2
73
74 #define IP6_VERSION (6)
75
76 struct rte_crypto_xform;
77 struct ipsec_xform;
78 struct rte_mbuf;
79
80 struct ipsec_sa;
81
82 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
83                 struct rte_crypto_op *cop);
84
85 struct ip_addr {
86         union {
87                 uint32_t ip4;
88                 union {
89                         uint64_t ip6[2];
90                         uint8_t ip6_b[16];
91                 } ip6;
92         } ip;
93 };
94
95 #define MAX_KEY_SIZE            32
96
97 struct ipsec_sa {
98         uint32_t spi;
99         uint32_t cdev_id_qp;
100         uint64_t seq;
101         uint32_t salt;
102         struct rte_cryptodev_sym_session *crypto_session;
103         enum rte_crypto_cipher_algorithm cipher_algo;
104         enum rte_crypto_auth_algorithm auth_algo;
105         enum rte_crypto_aead_algorithm aead_algo;
106         uint16_t digest_len;
107         uint16_t iv_len;
108         uint16_t block_size;
109         uint16_t flags;
110 #define IP4_TUNNEL (1 << 0)
111 #define IP6_TUNNEL (1 << 1)
112 #define TRANSPORT  (1 << 2)
113         struct ip_addr src;
114         struct ip_addr dst;
115         uint8_t cipher_key[MAX_KEY_SIZE];
116         uint16_t cipher_key_len;
117         uint8_t auth_key[MAX_KEY_SIZE];
118         uint16_t auth_key_len;
119         uint16_t aad_len;
120         struct rte_crypto_sym_xform *xforms;
121 } __rte_cache_aligned;
122
123 struct ipsec_mbuf_metadata {
124         struct ipsec_sa *sa;
125         struct rte_crypto_op cop;
126         struct rte_crypto_sym_op sym_cop;
127         uint8_t buf[32];
128 } __rte_cache_aligned;
129
130 struct cdev_qp {
131         uint16_t id;
132         uint16_t qp;
133         uint16_t in_flight;
134         uint16_t len;
135         struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
136 };
137
138 struct ipsec_ctx {
139         struct rte_hash *cdev_map;
140         struct sp_ctx *sp4_ctx;
141         struct sp_ctx *sp6_ctx;
142         struct sa_ctx *sa_ctx;
143         uint16_t nb_qps;
144         uint16_t last_qp;
145         struct cdev_qp tbl[MAX_QP_PER_LCORE];
146         struct rte_mempool *session_pool;
147 };
148
149 struct cdev_key {
150         uint16_t lcore_id;
151         uint8_t cipher_algo;
152         uint8_t auth_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__ */