examples/ipsec-secgw: fix crypto-op might never get dequeued
[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_INFLIGHT 128
21 #define MAX_QP_PER_LCORE 256
22
23 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
24
25 #define IPSEC_OFFLOAD_ESN_SOFTLIMIT 0xffffff00
26
27 #define IV_OFFSET               (sizeof(struct rte_crypto_op) + \
28                                 sizeof(struct rte_crypto_sym_op))
29
30 #define uint32_t_to_char(ip, a, b, c, d) do {\
31                 *a = (uint8_t)(ip >> 24 & 0xff);\
32                 *b = (uint8_t)(ip >> 16 & 0xff);\
33                 *c = (uint8_t)(ip >> 8 & 0xff);\
34                 *d = (uint8_t)(ip & 0xff);\
35         } while (0)
36
37 #define DEFAULT_MAX_CATEGORIES  1
38
39 #define IPSEC_SA_MAX_ENTRIES (128) /* must be power of 2, max 2 power 30 */
40 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1))
41 #define INVALID_SPI (0)
42
43 #define DISCARD (0x80000000)
44 #define BYPASS (0x40000000)
45 #define PROTECT_MASK (0x3fffffff)
46 #define PROTECT(sa_idx) (SPI2IDX(sa_idx) & PROTECT_MASK) /* SA idx 30 bits */
47
48 #define IPSEC_XFORM_MAX 2
49
50 #define IP6_VERSION (6)
51
52 struct rte_crypto_xform;
53 struct ipsec_xform;
54 struct rte_mbuf;
55
56 struct ipsec_sa;
57
58 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa,
59                 struct rte_crypto_op *cop);
60
61 struct ip_addr {
62         union {
63                 uint32_t ip4;
64                 union {
65                         uint64_t ip6[2];
66                         uint8_t ip6_b[16];
67                 } ip6;
68         } ip;
69 };
70
71 #define MAX_KEY_SIZE            32
72
73 struct ipsec_sa {
74         uint32_t spi;
75         uint32_t cdev_id_qp;
76         uint64_t seq;
77         uint32_t salt;
78         union {
79                 struct rte_cryptodev_sym_session *crypto_session;
80                 struct rte_security_session *sec_session;
81         };
82         enum rte_crypto_cipher_algorithm cipher_algo;
83         enum rte_crypto_auth_algorithm auth_algo;
84         enum rte_crypto_aead_algorithm aead_algo;
85         uint16_t digest_len;
86         uint16_t iv_len;
87         uint16_t block_size;
88         uint16_t flags;
89 #define IP4_TUNNEL (1 << 0)
90 #define IP6_TUNNEL (1 << 1)
91 #define TRANSPORT  (1 << 2)
92         struct ip_addr src;
93         struct ip_addr dst;
94         uint8_t cipher_key[MAX_KEY_SIZE];
95         uint16_t cipher_key_len;
96         uint8_t auth_key[MAX_KEY_SIZE];
97         uint16_t auth_key_len;
98         uint16_t aad_len;
99         union {
100                 struct rte_crypto_sym_xform *xforms;
101                 struct rte_security_ipsec_xform *sec_xform;
102         };
103         enum rte_security_session_action_type type;
104         enum rte_security_ipsec_sa_direction direction;
105         uint16_t portid;
106         struct rte_security_ctx *security_ctx;
107         uint32_t ol_flags;
108
109 #define MAX_RTE_FLOW_PATTERN (4)
110 #define MAX_RTE_FLOW_ACTIONS (3)
111         struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN];
112         struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS];
113         struct rte_flow_attr attr;
114         union {
115                 struct rte_flow_item_ipv4 ipv4_spec;
116                 struct rte_flow_item_ipv6 ipv6_spec;
117         };
118         struct rte_flow_item_esp esp_spec;
119         struct rte_flow *flow;
120         struct rte_security_session_conf sess_conf;
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         struct rte_mempool *session_priv_pool;
148         struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
149         uint16_t ol_pkts_cnt;
150         uint64_t ipv4_offloads;
151         uint64_t ipv6_offloads;
152 };
153
154 struct cdev_key {
155         uint16_t lcore_id;
156         uint8_t cipher_algo;
157         uint8_t auth_algo;
158         uint8_t aead_algo;
159 };
160
161 struct socket_ctx {
162         struct sa_ctx *sa_in;
163         struct sa_ctx *sa_out;
164         struct sp_ctx *sp_ip4_in;
165         struct sp_ctx *sp_ip4_out;
166         struct sp_ctx *sp_ip6_in;
167         struct sp_ctx *sp_ip6_out;
168         struct rt_ctx *rt_ip4;
169         struct rt_ctx *rt_ip6;
170         struct rte_mempool *mbuf_pool;
171         struct rte_mempool *session_pool;
172         struct rte_mempool *session_priv_pool;
173 };
174
175 struct cnt_blk {
176         uint32_t salt;
177         uint64_t iv;
178         uint32_t cnt;
179 } __attribute__((packed));
180
181 uint16_t
182 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
183                 uint16_t nb_pkts, uint16_t len);
184
185 uint16_t
186 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
187                 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len);
188
189 uint16_t
190 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
191                 uint16_t len);
192
193 uint16_t
194 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
195                 uint16_t len);
196
197 static inline uint16_t
198 ipsec_metadata_size(void)
199 {
200         return sizeof(struct ipsec_mbuf_metadata);
201 }
202
203 static inline struct ipsec_mbuf_metadata *
204 get_priv(struct rte_mbuf *m)
205 {
206         return rte_mbuf_to_priv(m);
207 }
208
209 static inline void *
210 get_cnt_blk(struct rte_mbuf *m)
211 {
212         struct ipsec_mbuf_metadata *priv = get_priv(m);
213
214         return &priv->buf[0];
215 }
216
217 static inline void *
218 get_aad(struct rte_mbuf *m)
219 {
220         struct ipsec_mbuf_metadata *priv = get_priv(m);
221
222         return &priv->buf[16];
223 }
224
225 static inline void *
226 get_sym_cop(struct rte_crypto_op *cop)
227 {
228         return (cop + 1);
229 }
230
231 int
232 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx);
233
234 void
235 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[],
236                 struct ipsec_sa *sa[], uint16_t nb_pkts);
237
238 void
239 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[],
240                 struct ipsec_sa *sa[], uint16_t nb_pkts);
241
242 void
243 sp4_init(struct socket_ctx *ctx, int32_t socket_id);
244
245 void
246 sp6_init(struct socket_ctx *ctx, int32_t socket_id);
247
248 void
249 sa_init(struct socket_ctx *ctx, int32_t socket_id);
250
251 void
252 rt_init(struct socket_ctx *ctx, int32_t socket_id);
253
254 int
255 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads,
256                 uint64_t *tx_offloads);
257
258 int
259 add_dst_ethaddr(uint16_t port, const struct ether_addr *addr);
260
261 void
262 enqueue_cop_burst(struct cdev_qp *cqp);
263
264 #endif /* __IPSEC_H__ */