Fixes for buliding for 32bit targets:
[vpp.git] / src / plugins / dpdk / ipsec / ipsec.h
1 /*
2  * Copyright (c) 2017 Intel and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __DPDK_IPSEC_H__
16 #define __DPDK_IPSEC_H__
17
18 #include <vnet/vnet.h>
19 #include <vppinfra/cache.h>
20 #include <vnet/ipsec/ipsec.h>
21
22 #undef always_inline
23 #include <rte_config.h>
24 #include <rte_crypto.h>
25 #include <rte_cryptodev.h>
26
27 #if CLIB_DEBUG > 0
28 #define always_inline static inline
29 #else
30 #define always_inline static inline __attribute__ ((__always_inline__))
31 #endif
32
33 #define foreach_dpdk_crypto_input_next          \
34   _(DROP, "error-drop")                         \
35   _(IP4_LOOKUP, "ip4-lookup")                   \
36   _(IP6_LOOKUP, "ip6-lookup")                   \
37   _(INTERFACE_OUTPUT, "interface-output")       \
38   _(DECRYPT4_POST, "dpdk-esp4-decrypt-post")     \
39   _(DECRYPT6_POST, "dpdk-esp6-decrypt-post")
40
41 typedef enum
42 {
43 #define _(f,s) DPDK_CRYPTO_INPUT_NEXT_##f,
44   foreach_dpdk_crypto_input_next
45 #undef _
46     DPDK_CRYPTO_INPUT_N_NEXT,
47 } dpdk_crypto_input_next_t;
48
49 #define MAX_QP_PER_LCORE 16
50
51 typedef struct
52 {
53   u32 salt;
54   u32 iv[2];
55   u32 cnt;
56 } dpdk_gcm_cnt_blk;
57
58 typedef struct
59 {
60   u32 next;
61   dpdk_gcm_cnt_blk cb __attribute__ ((aligned (16)));
62   u8 aad[16];
63   u8 icv[32];
64 } dpdk_op_priv_t;
65
66 typedef struct
67 {
68   u16 *resource_idx;
69   struct rte_crypto_op **ops;
70   u16 cipher_resource_idx[IPSEC_CRYPTO_N_ALG];
71   u16 auth_resource_idx[IPSEC_INTEG_N_ALG];
72     CLIB_CACHE_LINE_ALIGN_MARK (pad);
73 } crypto_worker_main_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
74
75 typedef struct
76 {
77   CLIB_ALIGN_MARK (pad, 8);     /* align up to 8 bytes for 32bit builds */
78   char *name;
79   enum rte_crypto_sym_xform_type type;
80   u32 alg;
81   u8 key_len;
82   u8 iv_len;
83   u8 trunc_size;
84   u8 boundary;
85   u8 disabled;
86   u8 resources;
87 } crypto_alg_t;
88
89 typedef struct
90 {
91   u16 *free_resources;
92   u16 *used_resources;
93   u8 cipher_support[IPSEC_CRYPTO_N_ALG];
94   u8 auth_support[IPSEC_INTEG_N_ALG];
95   u8 drv_id;
96   u8 numa;
97   u16 id;
98   const char *name;
99   u32 max_qp;
100   u64 features;
101 } crypto_dev_t;
102
103 typedef struct
104 {
105   const char *name;
106   u16 *devs;
107 } crypto_drv_t;
108
109 typedef struct
110 {
111   u16 thread_idx;
112   u8 remove;
113   u8 drv_id;
114   u8 dev_id;
115   u8 numa;
116   u16 qp_id;
117   u16 inflights[2];
118   u16 n_ops;
119   u16 __unused;
120   struct rte_crypto_op *ops[VLIB_FRAME_SIZE];
121   u32 bi[VLIB_FRAME_SIZE];
122 } crypto_resource_t __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES)));
123
124 typedef struct
125 {
126   u64 ts;
127   struct rte_cryptodev_sym_session *session;
128 } crypto_session_disposal_t;
129
130 typedef struct
131 {
132   CLIB_ALIGN_MARK (pad, 16);    /* align up to 16 bytes for 32bit builds */
133   struct rte_cryptodev_sym_session *session;
134   u64 dev_mask;
135 } crypto_session_by_drv_t;
136
137 typedef struct
138 {
139   /* Required for vec_validate_aligned */
140   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
141   struct rte_mempool *crypto_op;
142   struct rte_mempool *session_h;
143   struct rte_mempool **session_drv;
144   crypto_session_disposal_t *session_disposal;
145   uword *session_by_sa_index;
146   u64 crypto_op_get_failed;
147   u64 session_h_failed;
148   u64 *session_drv_failed;
149   crypto_session_by_drv_t *session_by_drv_id_and_sa_index;
150   clib_spinlock_t lockp;
151 } crypto_data_t;
152
153 typedef struct
154 {
155   crypto_worker_main_t *workers_main;
156   crypto_dev_t *dev;
157   crypto_resource_t *resource;
158   crypto_alg_t *cipher_algs;
159   crypto_alg_t *auth_algs;
160   crypto_data_t *data;
161   crypto_drv_t *drv;
162   u64 session_timeout;          /* nsec */
163   u8 enabled;
164 } dpdk_crypto_main_t;
165
166 extern dpdk_crypto_main_t dpdk_crypto_main;
167
168 static const u8 pad_data[] =
169   { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 };
170
171 void crypto_auto_placement (void);
172
173 clib_error_t *create_sym_session (struct rte_cryptodev_sym_session **session,
174                                   u32 sa_idx, crypto_resource_t * res,
175                                   crypto_worker_main_t * cwm, u8 is_outbound);
176
177 static_always_inline u32
178 crypto_op_len (void)
179 {
180   const u32 align = 4;
181   u32 op_size =
182     sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op);
183
184   return ((op_size + align - 1) & ~(align - 1)) + sizeof (dpdk_op_priv_t);
185 }
186
187 static_always_inline u32
188 crypto_op_get_priv_offset (void)
189 {
190   const u32 align = 16;
191   u32 offset;
192
193   offset = sizeof (struct rte_crypto_op) + sizeof (struct rte_crypto_sym_op);
194   offset = (offset + align - 1) & ~(align - 1);
195
196   return offset;
197 }
198
199 static_always_inline dpdk_op_priv_t *
200 crypto_op_get_priv (struct rte_crypto_op * op)
201 {
202   return (dpdk_op_priv_t *) (((u8 *) op) + crypto_op_get_priv_offset ());
203 }
204
205
206 static_always_inline void
207 add_session_by_drv_and_sa_idx (struct rte_cryptodev_sym_session *session,
208                                crypto_data_t * data, u32 drv_id, u32 sa_idx)
209 {
210   crypto_session_by_drv_t *sbd;
211   vec_validate_aligned (data->session_by_drv_id_and_sa_index, sa_idx,
212                         CLIB_CACHE_LINE_BYTES);
213   sbd = vec_elt_at_index (data->session_by_drv_id_and_sa_index, sa_idx);
214   sbd->dev_mask |= 1L << drv_id;
215   sbd->session = session;
216 }
217
218 static_always_inline struct rte_cryptodev_sym_session *
219 get_session_by_drv_and_sa_idx (crypto_data_t * data, u32 drv_id, u32 sa_idx)
220 {
221   crypto_session_by_drv_t *sess_by_sa;
222   if (_vec_len (data->session_by_drv_id_and_sa_index) <= sa_idx)
223     return NULL;
224   sess_by_sa =
225     vec_elt_at_index (data->session_by_drv_id_and_sa_index, sa_idx);
226   return (sess_by_sa->dev_mask & (1L << drv_id)) ? sess_by_sa->session : NULL;
227 }
228
229 static_always_inline clib_error_t *
230 crypto_get_session (struct rte_cryptodev_sym_session ** session,
231                     u32 sa_idx,
232                     crypto_resource_t * res,
233                     crypto_worker_main_t * cwm, u8 is_outbound)
234 {
235   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
236   crypto_data_t *data;
237   struct rte_cryptodev_sym_session *sess;
238
239   data = vec_elt_at_index (dcm->data, res->numa);
240   sess = get_session_by_drv_and_sa_idx (data, res->drv_id, sa_idx);
241
242   if (PREDICT_FALSE (!sess))
243     return create_sym_session (session, sa_idx, res, cwm, is_outbound);
244
245   session[0] = sess;
246
247   return NULL;
248 }
249
250 static_always_inline u16
251 get_resource (crypto_worker_main_t * cwm, ipsec_sa_t * sa)
252 {
253   u16 cipher_res = cwm->cipher_resource_idx[sa->crypto_alg];
254   u16 auth_res = cwm->auth_resource_idx[sa->integ_alg];
255   u8 is_aead;
256
257   /* Not allowed to setup SA with no-aead-cipher/NULL or NULL/NULL */
258
259   is_aead = ((sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_128) ||
260              (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_192) ||
261              (sa->crypto_alg == IPSEC_CRYPTO_ALG_AES_GCM_256));
262
263   if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE)
264     return auth_res;
265
266   if (cipher_res == auth_res)
267     return cipher_res;
268
269   if (is_aead)
270     return cipher_res;
271
272   return (u16) ~ 0;
273 }
274
275 static_always_inline i32
276 crypto_alloc_ops (u8 numa, struct rte_crypto_op ** ops, u32 n)
277 {
278   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
279   crypto_data_t *data = vec_elt_at_index (dcm->data, numa);
280   i32 ret;
281
282   ret = rte_mempool_get_bulk (data->crypto_op, (void **) ops, n);
283
284   /* *INDENT-OFF* */
285   data->crypto_op_get_failed += ! !ret;
286   /* *INDENT-ON* */
287
288   return ret;
289 }
290
291 static_always_inline void
292 crypto_free_ops (u8 numa, struct rte_crypto_op **ops, u32 n)
293 {
294   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
295   crypto_data_t *data = vec_elt_at_index (dcm->data, numa);
296
297   if (!n)
298     return;
299
300   rte_mempool_put_bulk (data->crypto_op, (void **) ops, n);
301 }
302
303 static_always_inline void
304 crypto_enqueue_ops (vlib_main_t * vm, crypto_worker_main_t * cwm, u8 outbound,
305                     u32 node_index, u32 error, u8 numa)
306 {
307   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
308   crypto_resource_t *res;
309   u16 *res_idx;
310
311   /* *INDENT-OFF* */
312   vec_foreach (res_idx, cwm->resource_idx)
313     {
314       u16 enq;
315       res = vec_elt_at_index (dcm->resource, res_idx[0]);
316
317       if (!res->n_ops)
318         continue;
319
320       enq = rte_cryptodev_enqueue_burst (res->dev_id, res->qp_id + outbound,
321                                          res->ops, res->n_ops);
322       res->inflights[outbound] += enq;
323
324       if (PREDICT_FALSE (enq < res->n_ops))
325         {
326           crypto_free_ops (numa, &res->ops[enq], res->n_ops - enq);
327           vlib_buffer_free (vm, &res->bi[enq], res->n_ops - enq);
328
329           vlib_node_increment_counter (vm, node_index, error,
330                                        res->n_ops - enq);
331         }
332       res->n_ops = 0;
333     }
334   /* *INDENT-ON* */
335 }
336
337 static_always_inline void
338 crypto_set_icb (dpdk_gcm_cnt_blk * icb, u32 salt, u32 seq, u32 seq_hi)
339 {
340   icb->salt = salt;
341   icb->iv[0] = seq;
342   icb->iv[1] = seq_hi;
343 }
344
345 static_always_inline void
346 crypto_op_setup (u8 is_aead, struct rte_mbuf *mb0,
347                  struct rte_crypto_op *op, void *session,
348                  u32 cipher_off, u32 cipher_len,
349                  u32 auth_off, u32 auth_len,
350                  u8 * aad, u8 * digest, u64 digest_paddr)
351 {
352   struct rte_crypto_sym_op *sym_op;
353
354   sym_op = (struct rte_crypto_sym_op *) (op + 1);
355
356   sym_op->m_src = mb0;
357   sym_op->session = session;
358
359   if (is_aead)
360     {
361       sym_op->aead.data.offset = cipher_off;
362       sym_op->aead.data.length = cipher_len;
363
364       sym_op->aead.aad.data = aad;
365       sym_op->aead.aad.phys_addr =
366         op->phys_addr + (uintptr_t) aad - (uintptr_t) op;
367
368       sym_op->aead.digest.data = digest;
369       sym_op->aead.digest.phys_addr = digest_paddr;
370     }
371   else
372     {
373       sym_op->cipher.data.offset = cipher_off;
374       sym_op->cipher.data.length = cipher_len;
375
376       sym_op->auth.data.offset = auth_off;
377       sym_op->auth.data.length = auth_len;
378
379       sym_op->auth.digest.data = digest;
380       sym_op->auth.digest.phys_addr = digest_paddr;
381     }
382 }
383
384 #endif /* __DPDK_IPSEC_H__ */
385
386 /*
387  * fd.io coding-style-patch-verification: ON
388  *
389  * Local Variables:
390  * eval: (c-set-style "gnu")
391  * End:
392  */