Imported Upstream version 16.04
[deb_dpdk.git] / examples / ipsec-secgw / ipsec.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 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_ip.h>
41 #include <rte_crypto.h>
42
43 #define RTE_LOGTYPE_IPSEC       RTE_LOGTYPE_USER1
44 #define RTE_LOGTYPE_IPSEC_ESP   RTE_LOGTYPE_USER2
45 #define RTE_LOGTYPE_IPSEC_IPIP  RTE_LOGTYPE_USER3
46
47 #define MAX_PKT_BURST 32
48 #define MAX_QP_PER_LCORE 256
49
50 #ifdef IPSEC_DEBUG
51 #define IPSEC_ASSERT(exp)                                            \
52 if (!(exp)) {                                                        \
53         rte_panic("line%d\tassert \"" #exp "\" failed\n", __LINE__); \
54 }
55
56 #define IPSEC_LOG RTE_LOG
57 #else
58 #define IPSEC_ASSERT(exp) do {} while (0)
59 #define IPSEC_LOG(...) do {} while (0)
60 #endif /* IPSEC_DEBUG */
61
62 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
63
64 #define uint32_t_to_char(ip, a, b, c, d) do {\
65                 *a = (unsigned char)(ip >> 24 & 0xff);\
66                 *b = (unsigned char)(ip >> 16 & 0xff);\
67                 *c = (unsigned char)(ip >> 8 & 0xff);\
68                 *d = (unsigned char)(ip & 0xff);\
69         } while (0)
70
71 #define DEFAULT_MAX_CATEGORIES  1
72
73 #define IPSEC_SA_MAX_ENTRIES (64) /* must be power of 2, max 2 power 30 */
74 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1))
75 #define INVALID_SPI (0)
76
77 #define DISCARD (0x80000000)
78 #define BYPASS (0x40000000)
79 #define PROTECT_MASK (0x3fffffff)
80 #define PROTECT(sa_idx) (SPI2IDX(sa_idx) & PROTECT_MASK) /* SA idx 30 bits */
81
82 #define IPSEC_XFORM_MAX 2
83
84 struct rte_crypto_xform;
85 struct ipsec_xform;
86 struct rte_cryptodev_session;
87 struct rte_mbuf;
88
89 struct ipsec_sa;
90
91 typedef int (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
92                 struct rte_crypto_op *cop);
93
94 struct ipsec_sa {
95         uint32_t spi;
96         uint32_t cdev_id_qp;
97         uint32_t src;
98         uint32_t dst;
99         struct rte_cryptodev_sym_session *crypto_session;
100         struct rte_crypto_sym_xform *xforms;
101         ipsec_xform_fn pre_crypto;
102         ipsec_xform_fn post_crypto;
103         enum rte_crypto_cipher_algorithm cipher_algo;
104         enum rte_crypto_auth_algorithm auth_algo;
105         uint16_t digest_len;
106         uint16_t iv_len;
107         uint16_t block_size;
108         uint16_t flags;
109         uint32_t seq;
110 } __rte_cache_aligned;
111
112 struct ipsec_mbuf_metadata {
113         struct ipsec_sa *sa;
114         struct rte_crypto_op cop;
115         struct rte_crypto_sym_op sym_cop;
116 };
117
118 struct cdev_qp {
119         uint16_t id;
120         uint16_t qp;
121         uint16_t in_flight;
122         uint16_t len;
123         struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
124 };
125
126 struct ipsec_ctx {
127         struct rte_hash *cdev_map;
128         struct sp_ctx *sp_ctx;
129         struct sa_ctx *sa_ctx;
130         uint16_t nb_qps;
131         uint16_t last_qp;
132         struct cdev_qp tbl[MAX_QP_PER_LCORE];
133 };
134
135 struct cdev_key {
136         uint16_t lcore_id;
137         uint8_t cipher_algo;
138         uint8_t auth_algo;
139 };
140
141 struct socket_ctx {
142         struct sa_ctx *sa_ipv4_in;
143         struct sa_ctx *sa_ipv4_out;
144         struct sp_ctx *sp_ipv4_in;
145         struct sp_ctx *sp_ipv4_out;
146         struct rt_ctx *rt_ipv4;
147         struct rte_mempool *mbuf_pool;
148 };
149
150 uint16_t
151 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
152                 uint16_t nb_pkts, uint16_t len);
153
154 uint16_t
155 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
156                 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
157
158 static inline uint16_t
159 ipsec_metadata_size(void)
160 {
161         return sizeof(struct ipsec_mbuf_metadata);
162 }
163
164 static inline struct ipsec_mbuf_metadata *
165 get_priv(struct rte_mbuf *m)
166 {
167         return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
168 }
169
170 int
171 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
172
173 void
174 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
175                 struct ipsec_sa *sa[], uint16_t nb_pkts);
176
177 void
178 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
179                 struct ipsec_sa *sa[], uint16_t nb_pkts);
180
181 void
182 sp_init(struct socket_ctx *ctx, int socket_id, unsigned ep);
183
184 void
185 sa_init(struct socket_ctx *ctx, int socket_id, unsigned ep);
186
187 void
188 rt_init(struct socket_ctx *ctx, int socket_id, unsigned ep);
189
190 #endif /* __IPSEC_H__ */