4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <rte_byteorder.h>
40 #include <rte_crypto.h>
41 #include <rte_security.h>
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
48 #define MAX_PKT_BURST 32
49 #define MAX_QP_PER_LCORE 256
51 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
53 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
54 sizeof(struct rte_crypto_sym_op))
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);\
63 #define DEFAULT_MAX_CATEGORIES 1
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)
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 */
74 #define IPSEC_XFORM_MAX 2
76 #define IP6_VERSION (6)
78 struct rte_crypto_xform;
84 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
85 struct rte_crypto_op *cop);
97 #define MAX_KEY_SIZE 32
105 struct rte_cryptodev_sym_session *crypto_session;
106 struct rte_security_session *sec_session;
108 enum rte_crypto_cipher_algorithm cipher_algo;
109 enum rte_crypto_auth_algorithm auth_algo;
110 enum rte_crypto_aead_algorithm aead_algo;
115 #define IP4_TUNNEL (1 << 0)
116 #define IP6_TUNNEL (1 << 1)
117 #define TRANSPORT (1 << 2)
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;
126 struct rte_crypto_sym_xform *xforms;
127 struct rte_security_ipsec_xform *sec_xform;
129 enum rte_security_session_action_type type;
130 enum rte_security_ipsec_sa_direction direction;
132 struct rte_security_ctx *security_ctx;
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;
141 struct rte_flow_item_ipv4 ipv4_spec;
142 struct rte_flow_item_ipv6 ipv6_spec;
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;
149 struct ipsec_mbuf_metadata {
151 struct rte_crypto_op cop;
152 struct rte_crypto_sym_op sym_cop;
154 } __rte_cache_aligned;
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;
167 struct rte_hash *cdev_map;
168 struct sp_ctx *sp4_ctx;
169 struct sp_ctx *sp6_ctx;
170 struct sa_ctx *sa_ctx;
173 struct cdev_qp tbl[MAX_QP_PER_LCORE];
174 struct rte_mempool *session_pool;
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;
201 } __attribute__((packed));
204 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
205 uint16_t nb_pkts, uint16_t len);
208 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
209 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
211 static inline uint16_t
212 ipsec_metadata_size(void)
214 return sizeof(struct ipsec_mbuf_metadata);
217 static inline struct ipsec_mbuf_metadata *
218 get_priv(struct rte_mbuf *m)
220 return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
224 get_cnt_blk(struct rte_mbuf *m)
226 struct ipsec_mbuf_metadata *priv = get_priv(m);
228 return &priv->buf[0];
232 get_aad(struct rte_mbuf *m)
234 struct ipsec_mbuf_metadata *priv = get_priv(m);
236 return &priv->buf[16];
240 get_sym_cop(struct rte_crypto_op *cop)
246 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
249 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
250 struct ipsec_sa *sa[], uint16_t nb_pkts);
253 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
254 struct ipsec_sa *sa[], uint16_t nb_pkts);
257 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
260 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
263 sa_init(struct socket_ctx *ctx, int32_t socket_id);
266 rt_init(struct socket_ctx *ctx, int32_t socket_id);
268 #endif /* __IPSEC_H__ */