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