dpdk: deprecate ipsec backend
[vpp.git] / src / plugins / dpdk / cryptodev / cryptodev.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2020 Intel and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/plugin/plugin.h>
20 #include <vnet/crypto/crypto.h>
21 #include <vnet/ipsec/ipsec.h>
22 #include <vpp/app/version.h>
23
24 #include <dpdk/buffer.h>
25 #include <dpdk/device/dpdk.h>
26 #include <dpdk/device/dpdk_priv.h>
27 #undef always_inline
28 #include <rte_bus_vdev.h>
29 #include <rte_cryptodev.h>
30 #include <rte_crypto_sym.h>
31 #include <rte_crypto.h>
32 #include <rte_cryptodev_pmd.h>
33 #include <rte_config.h>
34
35 #if CLIB_DEBUG > 0
36 #define always_inline static inline
37 #else
38 #define always_inline static inline __attribute__ ((__always_inline__))
39 #endif
40
41 #define CRYPTODEV_NB_CRYPTO_OPS 1024
42 #define CRYPTODEV_NB_SESSION    10240
43 #define CRYPTODEV_DEF_DRIVE     crypto_aesni_mb
44
45 #define CRYPTODEV_IV_OFFSET (offsetof (cryptodev_op_t, iv))
46 #define CRYPTODEV_AAD_OFFSET (offsetof (cryptodev_op_t, aad))
47
48 /* VNET_CRYPTO_ALGO, TYPE, DPDK_CRYPTO_ALGO, IV_LEN, TAG_LEN, AAD_LEN */
49 #define foreach_vnet_aead_crypto_conversion \
50   _(AES_128_GCM, AEAD, AES_GCM, 12, 16, 8)  \
51   _(AES_128_GCM, AEAD, AES_GCM, 12, 16, 12) \
52   _(AES_192_GCM, AEAD, AES_GCM, 12, 16, 8)  \
53   _(AES_192_GCM, AEAD, AES_GCM, 12, 16, 12) \
54   _(AES_256_GCM, AEAD, AES_GCM, 12, 16, 8)  \
55   _(AES_256_GCM, AEAD, AES_GCM, 12, 16, 12)
56
57 /**
58  * crypto (alg, cryptodev_alg), hash (alg, digest-size)
59  **/
60 #define foreach_cryptodev_link_async_alg        \
61   _ (AES_128_CBC, AES_CBC, SHA1, 12)            \
62   _ (AES_192_CBC, AES_CBC, SHA1, 12)            \
63   _ (AES_256_CBC, AES_CBC, SHA1, 12)            \
64   _ (AES_128_CBC, AES_CBC, SHA224, 14)          \
65   _ (AES_192_CBC, AES_CBC, SHA224, 14)          \
66   _ (AES_256_CBC, AES_CBC, SHA224, 14)          \
67   _ (AES_128_CBC, AES_CBC, SHA256, 16)          \
68   _ (AES_192_CBC, AES_CBC, SHA256, 16)          \
69   _ (AES_256_CBC, AES_CBC, SHA256, 16)          \
70   _ (AES_128_CBC, AES_CBC, SHA384, 24)          \
71   _ (AES_192_CBC, AES_CBC, SHA384, 24)          \
72   _ (AES_256_CBC, AES_CBC, SHA384, 24)          \
73   _ (AES_128_CBC, AES_CBC, SHA512, 32)          \
74   _ (AES_192_CBC, AES_CBC, SHA512, 32)          \
75   _ (AES_256_CBC, AES_CBC, SHA512, 32)
76
77 #define foreach_vnet_crypto_status_conversion \
78   _(SUCCESS, COMPLETED)                       \
79   _(NOT_PROCESSED, WORK_IN_PROGRESS)          \
80   _(AUTH_FAILED, FAIL_BAD_HMAC)               \
81   _(INVALID_SESSION, FAIL_ENGINE_ERR)         \
82   _(INVALID_ARGS, FAIL_ENGINE_ERR)            \
83   _(ERROR, FAIL_ENGINE_ERR)
84
85 static const vnet_crypto_op_status_t cryptodev_status_conversion[] = {
86 #define _(a, b) VNET_CRYPTO_OP_STATUS_##b,
87   foreach_vnet_crypto_status_conversion
88 #undef _
89 };
90
91 typedef struct
92 {
93   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
94   struct rte_crypto_op op;
95   struct rte_crypto_sym_op sop;
96   u8 iv[16];
97   u8 aad[16];
98   vnet_crypto_async_frame_t *frame;
99   u32 n_elts;
100 } cryptodev_op_t;
101
102 typedef enum
103 {
104   CRYPTODEV_OP_TYPE_ENCRYPT = 0,
105   CRYPTODEV_OP_TYPE_DECRYPT,
106   CRYPTODEV_N_OP_TYPES,
107 } cryptodev_op_type_t;
108
109 typedef struct
110 {
111   struct rte_cryptodev_sym_session ***keys;
112 } cryptodev_key_t;
113
114 typedef struct
115 {
116   u32 dev_id;
117   u32 q_id;
118   char *desc;
119 } cryptodev_inst_t;
120
121 typedef struct
122 {
123   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
124   struct rte_mempool *cop_pool;
125   struct rte_mempool *sess_pool;
126   struct rte_mempool *sess_priv_pool;
127 } cryptodev_numa_data_t;
128
129 typedef struct
130 {
131   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
132   u16 cryptodev_id;
133   u16 cryptodev_q;
134   u32 inflight;
135   cryptodev_op_t **cops;
136   struct rte_ring *ring;
137 } cryptodev_engine_thread_t;
138
139 typedef struct
140 {
141   cryptodev_numa_data_t *per_numa_data;
142   cryptodev_key_t *keys;
143   cryptodev_engine_thread_t *per_thread_data;
144   enum rte_iova_mode iova_mode;
145   cryptodev_inst_t *cryptodev_inst;
146   clib_bitmap_t *active_cdev_inst_mask;
147   clib_spinlock_t tlock;
148 } cryptodev_main_t;
149
150 cryptodev_main_t cryptodev_main;
151
152 static_always_inline int
153 prepare_aead_xform (struct rte_crypto_sym_xform *xform,
154                     cryptodev_op_type_t op_type, const vnet_crypto_key_t *key,
155                     u32 aad_len)
156 {
157   struct rte_crypto_aead_xform *aead_xform = &xform->aead;
158   memset (xform, 0, sizeof (*xform));
159   xform->type = RTE_CRYPTO_SYM_XFORM_AEAD;
160   xform->next = 0;
161
162   if (key->alg != VNET_CRYPTO_ALG_AES_128_GCM &&
163       key->alg != VNET_CRYPTO_ALG_AES_192_GCM &&
164       key->alg != VNET_CRYPTO_ALG_AES_256_GCM)
165     return -1;
166
167   aead_xform->algo = RTE_CRYPTO_AEAD_AES_GCM;
168   aead_xform->op = (op_type == CRYPTODEV_OP_TYPE_ENCRYPT) ?
169     RTE_CRYPTO_AEAD_OP_ENCRYPT : RTE_CRYPTO_AEAD_OP_DECRYPT;
170   aead_xform->aad_length = aad_len;
171   aead_xform->digest_length = 16;
172   aead_xform->iv.offset = CRYPTODEV_IV_OFFSET;
173   aead_xform->iv.length = 12;
174   aead_xform->key.data = key->data;
175   aead_xform->key.length = vec_len (key->data);
176
177   return 0;
178 }
179
180 static_always_inline int
181 prepare_linked_xform (struct rte_crypto_sym_xform *xforms,
182                       cryptodev_op_type_t op_type,
183                       const vnet_crypto_key_t *key)
184 {
185   struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
186   vnet_crypto_key_t *key_cipher, *key_auth;
187   enum rte_crypto_cipher_algorithm cipher_algo = ~0;
188   enum rte_crypto_auth_algorithm auth_algo = ~0;
189   u32 digest_len = ~0;
190
191   key_cipher = vnet_crypto_get_key (key->index_crypto);
192   key_auth = vnet_crypto_get_key (key->index_integ);
193   if (!key_cipher || !key_auth)
194     return -1;
195
196   if (op_type == CRYPTODEV_OP_TYPE_ENCRYPT)
197     {
198       xform_cipher = xforms;
199       xform_auth = xforms + 1;
200       xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
201       xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
202     }
203   else
204     {
205       xform_cipher = xforms + 1;
206       xform_auth = xforms;
207       xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
208       xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
209     }
210
211   xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
212   xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
213   xforms->next = xforms + 1;
214
215   switch (key->async_alg)
216     {
217 #define _(a, b, c, d) \
218   case VNET_CRYPTO_ALG_##a##_##c##_TAG##d:\
219     cipher_algo = RTE_CRYPTO_CIPHER_##b; \
220     auth_algo = RTE_CRYPTO_AUTH_##c##_HMAC; \
221     digest_len = d; \
222     break;
223
224       foreach_cryptodev_link_async_alg
225 #undef _
226     default:
227       return -1;
228     }
229
230   xform_cipher->cipher.algo = cipher_algo;
231   xform_cipher->cipher.key.data = key_cipher->data;
232   xform_cipher->cipher.key.length = vec_len (key_cipher->data);
233   xform_cipher->cipher.iv.length = 16;
234   xform_cipher->cipher.iv.offset = CRYPTODEV_IV_OFFSET;
235
236   xform_auth->auth.algo = auth_algo;
237   xform_auth->auth.digest_length = digest_len;
238   xform_auth->auth.key.data = key_auth->data;
239   xform_auth->auth.key.length = vec_len (key_auth->data);
240
241   return 0;
242 }
243
244 static_always_inline void
245 cryptodev_session_del (struct rte_cryptodev_sym_session *sess)
246 {
247   u32 n_devs, i;
248
249   if (sess == NULL)
250     return;
251
252   n_devs = rte_cryptodev_count ();
253
254   for (i = 0; i < n_devs; i++)
255     rte_cryptodev_sym_session_clear (i, sess);
256
257   rte_cryptodev_sym_session_free (sess);
258 }
259
260 static_always_inline int
261 cryptodev_check_supported_vnet_alg (vnet_crypto_key_t *key)
262 {
263   vnet_crypto_alg_t alg;
264   if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
265     return 0;
266
267   alg = key->alg;
268
269 #define _(a, b, c, d, e, f)     \
270   if (alg == VNET_CRYPTO_ALG_##a) \
271     return 0;
272
273   foreach_vnet_aead_crypto_conversion
274 #undef _
275     return -1;
276 }
277
278 static_always_inline int
279 cryptodev_session_create (vlib_main_t *vm, vnet_crypto_key_index_t idx,
280                           u32 aad_len)
281 {
282   cryptodev_main_t *cmt = &cryptodev_main;
283   cryptodev_numa_data_t *numa_data;
284   cryptodev_inst_t *dev_inst;
285   vnet_crypto_key_t *key = vnet_crypto_get_key (idx);
286   struct rte_mempool *sess_pool, *sess_priv_pool;
287   cryptodev_key_t *ckey = vec_elt_at_index (cmt->keys, idx);
288   struct rte_crypto_sym_xform xforms_enc[2] = { { 0 } };
289   struct rte_crypto_sym_xform xforms_dec[2] = { { 0 } };
290   struct rte_cryptodev_sym_session *sessions[CRYPTODEV_N_OP_TYPES] = { 0 };
291   u32 numa_node = vm->numa_node;
292   int ret;
293
294   numa_data = vec_elt_at_index (cmt->per_numa_data, numa_node);
295   sess_pool = numa_data->sess_pool;
296   sess_priv_pool = numa_data->sess_priv_pool;
297
298   sessions[CRYPTODEV_OP_TYPE_ENCRYPT] =
299     rte_cryptodev_sym_session_create (sess_pool);
300   if (!sessions[CRYPTODEV_OP_TYPE_ENCRYPT])
301     {
302       ret = -1;
303       goto clear_key;
304     }
305
306   sessions[CRYPTODEV_OP_TYPE_DECRYPT] =
307     rte_cryptodev_sym_session_create (sess_pool);
308   if (!sessions[CRYPTODEV_OP_TYPE_DECRYPT])
309     {
310       ret = -1;
311       goto clear_key;
312     }
313
314   if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
315     ret = prepare_linked_xform (xforms_enc, CRYPTODEV_OP_TYPE_ENCRYPT, key);
316   else
317     ret =
318       prepare_aead_xform (xforms_enc, CRYPTODEV_OP_TYPE_ENCRYPT, key, aad_len);
319   if (ret)
320     return 0;
321
322   if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
323     prepare_linked_xform (xforms_dec, CRYPTODEV_OP_TYPE_DECRYPT, key);
324   else
325     prepare_aead_xform (xforms_dec, CRYPTODEV_OP_TYPE_DECRYPT, key, aad_len);
326
327   vec_foreach (dev_inst, cmt->cryptodev_inst)
328     {
329       u32 dev_id = dev_inst->dev_id;
330       struct rte_cryptodev *cdev = rte_cryptodev_pmd_get_dev (dev_id);
331
332       /* if the session is already configured for the driver type, avoid
333          configuring it again to increase the session data's refcnt */
334       if (sessions[CRYPTODEV_OP_TYPE_ENCRYPT]
335             ->sess_data[cdev->driver_id]
336             .data &&
337           sessions[CRYPTODEV_OP_TYPE_DECRYPT]->sess_data[cdev->driver_id].data)
338         continue;
339
340       ret = rte_cryptodev_sym_session_init (
341         dev_id, sessions[CRYPTODEV_OP_TYPE_ENCRYPT], xforms_enc,
342         sess_priv_pool);
343       ret = rte_cryptodev_sym_session_init (
344         dev_id, sessions[CRYPTODEV_OP_TYPE_DECRYPT], xforms_dec,
345         sess_priv_pool);
346       if (ret < 0)
347         return ret;
348     }
349
350   sessions[CRYPTODEV_OP_TYPE_ENCRYPT]->opaque_data = aad_len;
351   sessions[CRYPTODEV_OP_TYPE_DECRYPT]->opaque_data = aad_len;
352
353   CLIB_MEMORY_STORE_BARRIER ();
354   ckey->keys[numa_node][CRYPTODEV_OP_TYPE_ENCRYPT] =
355     sessions[CRYPTODEV_OP_TYPE_ENCRYPT];
356   ckey->keys[numa_node][CRYPTODEV_OP_TYPE_DECRYPT] =
357     sessions[CRYPTODEV_OP_TYPE_DECRYPT];
358
359 clear_key:
360   if (ret != 0)
361     {
362       cryptodev_session_del (sessions[CRYPTODEV_OP_TYPE_ENCRYPT]);
363       cryptodev_session_del (sessions[CRYPTODEV_OP_TYPE_DECRYPT]);
364     }
365   return ret;
366 }
367
368 static_always_inline void
369 cryptodev_sess_handler (vlib_main_t *vm, vnet_crypto_key_op_t kop,
370                         vnet_crypto_key_index_t idx, u32 aad_len)
371 {
372   cryptodev_main_t *cmt = &cryptodev_main;
373   vnet_crypto_key_t *key = vnet_crypto_get_key (idx);
374   cryptodev_key_t *ckey = 0;
375   u32 i;
376
377   vec_validate (cmt->keys, idx);
378   ckey = vec_elt_at_index (cmt->keys, idx);
379
380   if (kop == VNET_CRYPTO_KEY_OP_DEL || kop == VNET_CRYPTO_KEY_OP_MODIFY)
381     {
382       if (idx >= vec_len (cmt->keys))
383         return;
384
385       vec_foreach_index (i, cmt->per_numa_data)
386         {
387           if (ckey->keys[i][CRYPTODEV_OP_TYPE_ENCRYPT])
388             {
389               cryptodev_session_del (ckey->keys[i][CRYPTODEV_OP_TYPE_ENCRYPT]);
390               cryptodev_session_del (ckey->keys[i][CRYPTODEV_OP_TYPE_DECRYPT]);
391
392               CLIB_MEMORY_STORE_BARRIER ();
393               ckey->keys[i][CRYPTODEV_OP_TYPE_ENCRYPT] = 0;
394               ckey->keys[i][CRYPTODEV_OP_TYPE_DECRYPT] = 0;
395             }
396         }
397       return;
398     }
399
400   /* create key */
401
402   /* do not create session for unsupported alg */
403   if (cryptodev_check_supported_vnet_alg (key))
404     return;
405
406   vec_validate (ckey->keys, vec_len (cmt->per_numa_data) - 1);
407   vec_foreach_index (i, ckey->keys)
408     vec_validate (ckey->keys[i], CRYPTODEV_N_OP_TYPES - 1);
409 }
410
411 /*static*/ void
412 cryptodev_key_handler (vlib_main_t * vm, vnet_crypto_key_op_t kop,
413                        vnet_crypto_key_index_t idx)
414 {
415   cryptodev_sess_handler (vm, kop, idx, 8);
416 }
417
418 static_always_inline void
419 cryptodev_mark_frame_err_status (vnet_crypto_async_frame_t * f,
420                                  vnet_crypto_op_status_t s)
421 {
422   u32 n_elts = f->n_elts, i;
423
424   for (i = 0; i < n_elts; i++)
425     f->elts[i].status = s;
426 }
427
428 static_always_inline rte_iova_t
429 cryptodev_get_iova (clib_pmalloc_main_t * pm, enum rte_iova_mode mode,
430                     void *data)
431 {
432   u64 index;
433   if (mode == RTE_IOVA_VA)
434     return (rte_iova_t) pointer_to_uword (data);
435
436   index = clib_pmalloc_get_page_index (pm, data);
437   return pointer_to_uword (data) - pm->lookup_table[index];
438 }
439
440 static_always_inline void
441 cryptodev_validate_mbuf_chain (vlib_main_t * vm, struct rte_mbuf *mb,
442                                vlib_buffer_t * b)
443 {
444   struct rte_mbuf *first_mb = mb, *last_mb = mb; /**< last mbuf */
445   /* when input node is not dpdk, mbuf data len is not initialized, for
446    * single buffer it is not a problem since the data length is written
447    * into cryptodev operation. For chained buffer a reference data length
448    * has to be computed through vlib_buffer.
449    *
450    * even when input node is dpdk, it is possible chained vlib_buffers
451    * are updated (either added or removed a buffer) but not not mbuf fields.
452    * we have to re-link every mbuf in the chain.
453    */
454   u16 data_len = b->current_length + (b->data + b->current_data -
455                                       rte_pktmbuf_mtod (mb, u8 *));
456
457   first_mb->nb_segs = 1;
458   first_mb->pkt_len = first_mb->data_len = data_len;
459
460   while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
461     {
462       b = vlib_get_buffer (vm, b->next_buffer);
463       mb = rte_mbuf_from_vlib_buffer (b);
464       if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_EXT_HDR_VALID) == 0))
465         rte_pktmbuf_reset (mb);
466       last_mb->next = mb;
467       last_mb = mb;
468       mb->data_len = b->current_length;
469       mb->pkt_len = b->current_length;
470       mb->data_off = VLIB_BUFFER_PRE_DATA_SIZE + b->current_data;
471       first_mb->nb_segs++;
472       if (PREDICT_FALSE (b->ref_count > 1))
473         mb->pool =
474           dpdk_no_cache_mempool_by_buffer_pool_index[b->buffer_pool_index];
475     }
476 }
477
478 static_always_inline int
479 cryptodev_frame_linked_algs_enqueue (vlib_main_t * vm,
480                                      vnet_crypto_async_frame_t * frame,
481                                      cryptodev_op_type_t op_type)
482 {
483   cryptodev_main_t *cmt = &cryptodev_main;
484   clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
485   cryptodev_numa_data_t *numa = cmt->per_numa_data + vm->numa_node;
486   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
487   vnet_crypto_async_frame_elt_t *fe;
488   struct rte_cryptodev_sym_session *sess = 0;
489   cryptodev_op_t **cop;
490   u32 *bi;
491   u32 n_enqueue, n_elts;
492   u32 last_key_index = ~0;
493
494   if (PREDICT_FALSE (frame == 0 || frame->n_elts == 0))
495     return -1;
496   n_elts = frame->n_elts;
497
498   if (PREDICT_FALSE (CRYPTODEV_NB_CRYPTO_OPS - cet->inflight < n_elts))
499     {
500       cryptodev_mark_frame_err_status (frame,
501                                        VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
502       return -1;
503     }
504
505   if (PREDICT_FALSE (rte_mempool_get_bulk (numa->cop_pool,
506                                            (void **) cet->cops, n_elts) < 0))
507     {
508       cryptodev_mark_frame_err_status (frame,
509                                        VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
510       return -1;
511     }
512
513   cop = cet->cops;
514   fe = frame->elts;
515   bi = frame->buffer_indices;
516   cop[0]->frame = frame;
517   cop[0]->n_elts = n_elts;
518
519   while (n_elts)
520     {
521       vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
522       struct rte_crypto_sym_op *sop = &cop[0]->sop;
523       i16 crypto_offset = fe->crypto_start_offset;
524       i16 integ_offset = fe->integ_start_offset;
525       u32 offset_diff = crypto_offset - integ_offset;
526
527       if (n_elts > 2)
528         {
529           CLIB_PREFETCH (cop[1], CLIB_CACHE_LINE_BYTES * 3, STORE);
530           CLIB_PREFETCH (cop[2], CLIB_CACHE_LINE_BYTES * 3, STORE);
531           CLIB_PREFETCH (&fe[1], CLIB_CACHE_LINE_BYTES, LOAD);
532           CLIB_PREFETCH (&fe[2], CLIB_CACHE_LINE_BYTES, LOAD);
533         }
534       if (last_key_index != fe->key_index)
535         {
536           cryptodev_key_t *key = vec_elt_at_index (cmt->keys, fe->key_index);
537           last_key_index = fe->key_index;
538
539           if (key->keys[vm->numa_node][op_type] == 0)
540             {
541               if (PREDICT_FALSE (
542                     cryptodev_session_create (vm, last_key_index, 0) < 0))
543                 {
544                   cryptodev_mark_frame_err_status (
545                     frame, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
546                   return -1;
547                 }
548             }
549           sess = key->keys[vm->numa_node][op_type];
550         }
551
552       sop->m_src = rte_mbuf_from_vlib_buffer (b);
553       sop->m_src->data_off = VLIB_BUFFER_PRE_DATA_SIZE;
554       sop->m_dst = 0;
555       /* mbuf prepend happens in the tx, but vlib_buffer happens in the nodes,
556        * so we have to manually adjust mbuf data_off here so cryptodev can
557        * correctly compute the data pointer. The prepend here will be later
558        * rewritten by tx. */
559       if (PREDICT_TRUE (fe->integ_start_offset < 0))
560         {
561           sop->m_src->data_off += fe->integ_start_offset;
562           integ_offset = 0;
563           crypto_offset = offset_diff;
564         }
565       sop->session = sess;
566       sop->cipher.data.offset = crypto_offset;
567       sop->cipher.data.length = fe->crypto_total_length;
568       sop->auth.data.offset = integ_offset;
569       sop->auth.data.length = fe->crypto_total_length + fe->integ_length_adj;
570       sop->auth.digest.data = fe->digest;
571       sop->auth.digest.phys_addr = cryptodev_get_iova (pm, cmt->iova_mode,
572                                                        fe->digest);
573       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
574         cryptodev_validate_mbuf_chain (vm, sop->m_src, b);
575       else
576         /* for input nodes that are not dpdk-input, it is possible the mbuf
577          * was updated before as one of the chained mbufs. Setting nb_segs
578          * to 1 here to prevent the cryptodev PMD to access potentially
579          * invalid m_src->next pointers.
580          */
581         sop->m_src->nb_segs = 1;
582       clib_memcpy_fast (cop[0]->iv, fe->iv, 16);
583       cop++;
584       bi++;
585       fe++;
586       n_elts--;
587     }
588
589   n_enqueue = rte_cryptodev_enqueue_burst (cet->cryptodev_id,
590                                            cet->cryptodev_q,
591                                            (struct rte_crypto_op **)
592                                            cet->cops, frame->n_elts);
593   ASSERT (n_enqueue == frame->n_elts);
594   cet->inflight += n_enqueue;
595
596   return 0;
597 }
598
599 static_always_inline int
600 cryptodev_frame_gcm_enqueue (vlib_main_t * vm,
601                              vnet_crypto_async_frame_t * frame,
602                              cryptodev_op_type_t op_type, u8 aad_len)
603 {
604   cryptodev_main_t *cmt = &cryptodev_main;
605   clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
606   cryptodev_numa_data_t *numa = cmt->per_numa_data + vm->numa_node;
607   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
608   vnet_crypto_async_frame_elt_t *fe;
609   struct rte_cryptodev_sym_session *sess = 0;
610   cryptodev_op_t **cop;
611   u32 *bi;
612   u32 n_enqueue = 0, n_elts;
613   u32 last_key_index = ~0;
614
615   if (PREDICT_FALSE (frame == 0 || frame->n_elts == 0))
616     return -1;
617   n_elts = frame->n_elts;
618
619   if (PREDICT_FALSE (CRYPTODEV_NB_CRYPTO_OPS - cet->inflight < n_elts))
620     {
621       cryptodev_mark_frame_err_status (frame,
622                                        VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
623       return -1;
624     }
625
626   if (PREDICT_FALSE (rte_mempool_get_bulk (numa->cop_pool,
627                                            (void **) cet->cops, n_elts) < 0))
628     {
629       cryptodev_mark_frame_err_status (frame,
630                                        VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
631       return -1;
632     }
633
634   cop = cet->cops;
635   fe = frame->elts;
636   bi = frame->buffer_indices;
637   cop[0]->frame = frame;
638   cop[0]->n_elts = n_elts;
639
640   while (n_elts)
641     {
642       vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
643       struct rte_crypto_sym_op *sop = &cop[0]->sop;
644       u16 crypto_offset = fe->crypto_start_offset;
645
646       if (n_elts > 2)
647         {
648           CLIB_PREFETCH (cop[1], CLIB_CACHE_LINE_BYTES * 3, STORE);
649           CLIB_PREFETCH (cop[2], CLIB_CACHE_LINE_BYTES * 3, STORE);
650           CLIB_PREFETCH (&fe[1], CLIB_CACHE_LINE_BYTES, LOAD);
651           CLIB_PREFETCH (&fe[2], CLIB_CACHE_LINE_BYTES, LOAD);
652         }
653       if (last_key_index != fe->key_index)
654         {
655           cryptodev_key_t *key = vec_elt_at_index (cmt->keys, fe->key_index);
656
657           last_key_index = fe->key_index;
658           if (key->keys[vm->numa_node][op_type] == 0)
659             {
660               if (PREDICT_FALSE (cryptodev_session_create (vm, last_key_index,
661                                                            aad_len) < 0))
662                 {
663                   cryptodev_mark_frame_err_status (
664                     frame, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
665                   return -1;
666                 }
667             }
668           else if (PREDICT_FALSE (
669                      key->keys[vm->numa_node][op_type]->opaque_data !=
670                      aad_len))
671             {
672               cryptodev_sess_handler (vm, VNET_CRYPTO_KEY_OP_DEL,
673                                       fe->key_index, aad_len);
674               if (PREDICT_FALSE (cryptodev_session_create (vm, last_key_index,
675                                                            aad_len) < 0))
676                 {
677                   cryptodev_mark_frame_err_status (
678                     frame, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
679                   return -1;
680                 }
681             }
682
683           sess = key->keys[vm->numa_node][op_type];
684         }
685
686       sop->m_src = rte_mbuf_from_vlib_buffer (b);
687       sop->m_dst = 0;
688       /* mbuf prepend happens in the tx, but vlib_buffer happens in the nodes,
689        * so we have to manually adjust mbuf data_off here so cryptodev can
690        * correctly compute the data pointer. The prepend here will be later
691        * rewritten by tx. */
692       if (PREDICT_FALSE (fe->crypto_start_offset < 0))
693         {
694           rte_pktmbuf_prepend (sop->m_src, -fe->crypto_start_offset);
695           crypto_offset = 0;
696         }
697
698       sop->session = sess;
699       sop->aead.aad.data = cop[0]->aad;
700       sop->aead.aad.phys_addr = cop[0]->op.phys_addr + CRYPTODEV_AAD_OFFSET;
701       sop->aead.data.length = fe->crypto_total_length;
702       sop->aead.data.offset = crypto_offset;
703       sop->aead.digest.data = fe->tag;
704       sop->aead.digest.phys_addr = cryptodev_get_iova (pm, cmt->iova_mode,
705                                                        fe->tag);
706       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
707         cryptodev_validate_mbuf_chain (vm, sop->m_src, b);
708       else
709         /* for input nodes that are not dpdk-input, it is possible the mbuf
710          * was updated before as one of the chained mbufs. Setting nb_segs
711          * to 1 here to prevent the cryptodev PMD to access potentially
712          * invalid m_src->next pointers.
713          */
714         sop->m_src->nb_segs = 1;
715       clib_memcpy_fast (cop[0]->iv, fe->iv, 12);
716       clib_memcpy_fast (cop[0]->aad, fe->aad, aad_len);
717       cop++;
718       bi++;
719       fe++;
720       n_elts--;
721     }
722
723   n_enqueue = rte_cryptodev_enqueue_burst (cet->cryptodev_id,
724                                            cet->cryptodev_q,
725                                            (struct rte_crypto_op **)
726                                            cet->cops, frame->n_elts);
727   ASSERT (n_enqueue == frame->n_elts);
728   cet->inflight += n_enqueue;
729
730   return 0;
731 }
732
733 static_always_inline cryptodev_op_t *
734 cryptodev_get_ring_head (struct rte_ring * ring)
735 {
736   cryptodev_op_t **r = (void *) &ring[1];
737   return r[ring->cons.head & ring->mask];
738 }
739
740 static_always_inline vnet_crypto_async_frame_t *
741 cryptodev_frame_dequeue (vlib_main_t * vm, u32 * nb_elts_processed,
742                          u32 * enqueue_thread_idx)
743 {
744   cryptodev_main_t *cmt = &cryptodev_main;
745   cryptodev_numa_data_t *numa = cmt->per_numa_data + vm->numa_node;
746   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
747   cryptodev_op_t *cop0, **cop = cet->cops;
748   vnet_crypto_async_frame_elt_t *fe;
749   vnet_crypto_async_frame_t *frame;
750   u32 n_elts, n_completed_ops = rte_ring_count (cet->ring);
751   u32 ss0 = 0, ss1 = 0, ss2 = 0, ss3 = 0;       /* sum of status */
752
753   if (cet->inflight)
754     {
755       n_elts = clib_min (CRYPTODEV_NB_CRYPTO_OPS - n_completed_ops,
756                          VNET_CRYPTO_FRAME_SIZE);
757       n_elts = rte_cryptodev_dequeue_burst
758         (cet->cryptodev_id, cet->cryptodev_q,
759          (struct rte_crypto_op **) cet->cops, n_elts);
760       cet->inflight -= n_elts;
761       n_completed_ops += n_elts;
762
763       rte_ring_sp_enqueue_burst (cet->ring, (void *) cet->cops, n_elts, NULL);
764     }
765
766   if (PREDICT_FALSE (n_completed_ops == 0))
767     return 0;
768
769   cop0 = cryptodev_get_ring_head (cet->ring);
770   /* not a single frame is finished */
771   if (PREDICT_FALSE (cop0->n_elts > rte_ring_count (cet->ring)))
772     return 0;
773
774   frame = cop0->frame;
775   n_elts = cop0->n_elts;
776   n_elts = rte_ring_sc_dequeue_bulk (cet->ring, (void **) cet->cops,
777                                      n_elts, 0);
778   fe = frame->elts;
779
780   while (n_elts > 4)
781     {
782       ss0 |= fe[0].status = cryptodev_status_conversion[cop[0]->op.status];
783       ss1 |= fe[1].status = cryptodev_status_conversion[cop[1]->op.status];
784       ss2 |= fe[2].status = cryptodev_status_conversion[cop[2]->op.status];
785       ss3 |= fe[3].status = cryptodev_status_conversion[cop[3]->op.status];
786
787       cop += 4;
788       fe += 4;
789       n_elts -= 4;
790     }
791
792   while (n_elts)
793     {
794       ss0 |= fe[0].status = cryptodev_status_conversion[cop[0]->op.status];
795       fe++;
796       cop++;
797       n_elts--;
798     }
799
800   frame->state = (ss0 | ss1 | ss2 | ss3) == VNET_CRYPTO_OP_STATUS_COMPLETED ?
801     VNET_CRYPTO_FRAME_STATE_SUCCESS : VNET_CRYPTO_FRAME_STATE_ELT_ERROR;
802
803   rte_mempool_put_bulk (numa->cop_pool, (void **) cet->cops, frame->n_elts);
804   *nb_elts_processed = frame->n_elts;
805   *enqueue_thread_idx = frame->enqueue_thread_index;
806   return frame;
807 }
808
809 /* *INDENT-OFF* */
810 static_always_inline int
811 cryptodev_enqueue_gcm_aad_8_enc (vlib_main_t * vm,
812                                  vnet_crypto_async_frame_t * frame)
813 {
814   return cryptodev_frame_gcm_enqueue (vm, frame,
815                                       CRYPTODEV_OP_TYPE_ENCRYPT, 8);
816 }
817 static_always_inline int
818 cryptodev_enqueue_gcm_aad_12_enc (vlib_main_t * vm,
819                                  vnet_crypto_async_frame_t * frame)
820 {
821   return cryptodev_frame_gcm_enqueue (vm, frame,
822                                       CRYPTODEV_OP_TYPE_ENCRYPT, 12);
823 }
824
825 static_always_inline int
826 cryptodev_enqueue_gcm_aad_8_dec (vlib_main_t * vm,
827                                  vnet_crypto_async_frame_t * frame)
828 {
829   return cryptodev_frame_gcm_enqueue (vm, frame,
830                                       CRYPTODEV_OP_TYPE_DECRYPT, 8);
831 }
832 static_always_inline int
833 cryptodev_enqueue_gcm_aad_12_dec (vlib_main_t * vm,
834                                  vnet_crypto_async_frame_t * frame)
835 {
836   return cryptodev_frame_gcm_enqueue (vm, frame,
837                                       CRYPTODEV_OP_TYPE_DECRYPT, 12);
838 }
839
840 static_always_inline int
841 cryptodev_enqueue_linked_alg_enc (vlib_main_t * vm,
842                                   vnet_crypto_async_frame_t * frame)
843 {
844   return cryptodev_frame_linked_algs_enqueue (vm, frame,
845                                               CRYPTODEV_OP_TYPE_ENCRYPT);
846 }
847
848 static_always_inline int
849 cryptodev_enqueue_linked_alg_dec (vlib_main_t * vm,
850                                   vnet_crypto_async_frame_t * frame)
851 {
852   return cryptodev_frame_linked_algs_enqueue (vm, frame,
853                                               CRYPTODEV_OP_TYPE_DECRYPT);
854 }
855
856 typedef enum
857 {
858   CRYPTODEV_RESOURCE_ASSIGN_AUTO = 0,
859   CRYPTODEV_RESOURCE_ASSIGN_UPDATE,
860 } cryptodev_resource_assign_op_t;
861
862 /**
863  *  assign a cryptodev resource to a worker.
864  *  @param cet: the worker thread data
865  *  @param cryptodev_inst_index: if op is "ASSIGN_AUTO" this param is ignored.
866  *  @param op: the assignment method.
867  *  @return: 0 if successfully, negative number otherwise.
868  **/
869 static_always_inline int
870 cryptodev_assign_resource (cryptodev_engine_thread_t * cet,
871                            u32 cryptodev_inst_index,
872                            cryptodev_resource_assign_op_t op)
873 {
874   cryptodev_main_t *cmt = &cryptodev_main;
875   cryptodev_inst_t *cinst = 0;
876   uword idx;
877
878   /* assign resource is only allowed when no inflight op is in the queue */
879   if (cet->inflight)
880     return -EBUSY;
881
882   switch (op)
883     {
884     case CRYPTODEV_RESOURCE_ASSIGN_AUTO:
885       if (clib_bitmap_count_set_bits (cmt->active_cdev_inst_mask) >=
886           vec_len (cmt->cryptodev_inst))
887         return -1;
888
889       clib_spinlock_lock (&cmt->tlock);
890       idx = clib_bitmap_first_clear (cmt->active_cdev_inst_mask);
891       clib_bitmap_set (cmt->active_cdev_inst_mask, idx, 1);
892       cinst = vec_elt_at_index (cmt->cryptodev_inst, idx);
893       cet->cryptodev_id = cinst->dev_id;
894       cet->cryptodev_q = cinst->q_id;
895       clib_spinlock_unlock (&cmt->tlock);
896       break;
897     case CRYPTODEV_RESOURCE_ASSIGN_UPDATE:
898       /* assigning a used cryptodev resource is not allowed */
899       if (clib_bitmap_get (cmt->active_cdev_inst_mask, cryptodev_inst_index)
900           == 1)
901         return -EBUSY;
902       vec_foreach_index (idx, cmt->cryptodev_inst)
903       {
904         cinst = cmt->cryptodev_inst + idx;
905         if (cinst->dev_id == cet->cryptodev_id &&
906             cinst->q_id == cet->cryptodev_q)
907           break;
908       }
909       /* invalid existing worker resource assignment */
910       if (idx == vec_len (cmt->cryptodev_inst))
911         return -EINVAL;
912       clib_spinlock_lock (&cmt->tlock);
913       clib_bitmap_set_no_check (cmt->active_cdev_inst_mask, idx, 0);
914       clib_bitmap_set_no_check (cmt->active_cdev_inst_mask,
915                                 cryptodev_inst_index, 1);
916       cinst = cmt->cryptodev_inst + cryptodev_inst_index;
917       cet->cryptodev_id = cinst->dev_id;
918       cet->cryptodev_q = cinst->q_id;
919       clib_spinlock_unlock (&cmt->tlock);
920       break;
921     default:
922       return -EINVAL;
923     }
924   return 0;
925 }
926
927 static u8 *
928 format_cryptodev_inst (u8 * s, va_list * args)
929 {
930   cryptodev_main_t *cmt = &cryptodev_main;
931   u32 inst = va_arg (*args, u32);
932   cryptodev_inst_t *cit = cmt->cryptodev_inst + inst;
933   u32 thread_index = 0;
934   struct rte_cryptodev_info info;
935
936   rte_cryptodev_info_get (cit->dev_id, &info);
937   s = format (s, "%-25s%-10u", info.device->name, cit->q_id);
938
939   vec_foreach_index (thread_index, cmt->per_thread_data)
940   {
941     cryptodev_engine_thread_t *cet = cmt->per_thread_data + thread_index;
942     if (vlib_num_workers () > 0 && thread_index == 0)
943       continue;
944
945     if (cet->cryptodev_id == cit->dev_id && cet->cryptodev_q == cit->q_id)
946       {
947         s = format (s, "%u (%v)\n", thread_index,
948                     vlib_worker_threads[thread_index].name);
949         break;
950       }
951   }
952
953   if (thread_index == vec_len (cmt->per_thread_data))
954     s = format (s, "%s\n", "free");
955
956   return s;
957 }
958
959 static clib_error_t *
960 cryptodev_show_assignment_fn (vlib_main_t * vm, unformat_input_t * input,
961                               vlib_cli_command_t * cmd)
962 {
963   cryptodev_main_t *cmt = &cryptodev_main;
964   u32 inst;
965
966   vlib_cli_output (vm, "%-5s%-25s%-10s%s\n", "No.", "Name", "Queue-id",
967                    "Assigned-to");
968   if (vec_len (cmt->cryptodev_inst) == 0)
969     {
970       vlib_cli_output (vm, "(nil)\n");
971       return 0;
972     }
973
974   vec_foreach_index (inst, cmt->cryptodev_inst)
975     vlib_cli_output (vm, "%-5u%U", inst, format_cryptodev_inst, inst);
976
977   return 0;
978 }
979
980 VLIB_CLI_COMMAND (show_cryptodev_assignment, static) = {
981     .path = "show cryptodev assignment",
982     .short_help = "show cryptodev assignment",
983     .function = cryptodev_show_assignment_fn,
984 };
985
986 static clib_error_t *
987 cryptodev_set_assignment_fn (vlib_main_t * vm, unformat_input_t * input,
988                              vlib_cli_command_t * cmd)
989 {
990   cryptodev_main_t *cmt = &cryptodev_main;
991   cryptodev_engine_thread_t *cet;
992   unformat_input_t _line_input, *line_input = &_line_input;
993   u32 thread_index, inst_index;
994   u32 thread_present = 0, inst_present = 0;
995   clib_error_t *error = 0;
996   int ret;
997
998   /* Get a line of input. */
999   if (!unformat_user (input, unformat_line_input, line_input))
1000     return 0;
1001
1002   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1003     {
1004       if (unformat (line_input, "thread %u", &thread_index))
1005         thread_present = 1;
1006       else if (unformat (line_input, "resource %u", &inst_index))
1007         inst_present = 1;
1008       else
1009         {
1010           error = clib_error_return (0, "unknown input `%U'",
1011                                      format_unformat_error, line_input);
1012           return error;
1013         }
1014     }
1015
1016   if (!thread_present || !inst_present)
1017     {
1018       error = clib_error_return (0, "mandatory argument(s) missing");
1019       return error;
1020     }
1021
1022   if (thread_index == 0 && vlib_num_workers () > 0)
1023     {
1024       error =
1025         clib_error_return (0, "assign crypto resource for master thread");
1026       return error;
1027     }
1028
1029   if (thread_index > vec_len (cmt->per_thread_data) ||
1030       inst_index > vec_len (cmt->cryptodev_inst))
1031     {
1032       error = clib_error_return (0, "wrong thread id or resource id");
1033       return error;
1034     }
1035
1036   cet = cmt->per_thread_data + thread_index;
1037   ret = cryptodev_assign_resource (cet, inst_index,
1038                                    CRYPTODEV_RESOURCE_ASSIGN_UPDATE);
1039   if (ret)
1040     {
1041       error = clib_error_return (0, "cryptodev_assign_resource returned %i",
1042                                  ret);
1043       return error;
1044     }
1045
1046   return 0;
1047 }
1048
1049 VLIB_CLI_COMMAND (set_cryptodev_assignment, static) = {
1050     .path = "set cryptodev assignment",
1051     .short_help = "set cryptodev assignment thread <thread_index> "
1052         "resource <inst_index>",
1053     .function = cryptodev_set_assignment_fn,
1054 };
1055
1056 static int
1057 check_cryptodev_alg_support (u32 dev_id)
1058 {
1059   const struct rte_cryptodev_symmetric_capability *cap;
1060   struct rte_cryptodev_sym_capability_idx cap_idx;
1061
1062 #define _(a, b, c, d, e, f) \
1063   cap_idx.type = RTE_CRYPTO_SYM_XFORM_##b; \
1064   cap_idx.algo.aead = RTE_CRYPTO_##b##_##c; \
1065   cap = rte_cryptodev_sym_capability_get (dev_id, &cap_idx); \
1066   if (!cap) \
1067     return -RTE_CRYPTO_##b##_##c; \
1068   else \
1069     { \
1070       if (cap->aead.digest_size.min > e || cap->aead.digest_size.max < e) \
1071         return -RTE_CRYPTO_##b##_##c; \
1072       if (cap->aead.aad_size.min > f || cap->aead.aad_size.max < f) \
1073         return -RTE_CRYPTO_##b##_##c; \
1074       if (cap->aead.iv_size.min > d || cap->aead.iv_size.max < d) \
1075         return -RTE_CRYPTO_##b##_##c; \
1076     }
1077
1078   foreach_vnet_aead_crypto_conversion
1079 #undef _
1080
1081 #define _(a, b, c, d) \
1082   cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER; \
1083   cap_idx.algo.cipher = RTE_CRYPTO_CIPHER_##b; \
1084   cap = rte_cryptodev_sym_capability_get (dev_id, &cap_idx); \
1085   if (!cap) \
1086     return -RTE_CRYPTO_CIPHER_##b; \
1087   cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH; \
1088   cap_idx.algo.auth = RTE_CRYPTO_AUTH_##c##_HMAC; \
1089   cap = rte_cryptodev_sym_capability_get (dev_id, &cap_idx); \
1090   if (!cap) \
1091     return -RTE_CRYPTO_AUTH_##c;
1092
1093   foreach_cryptodev_link_async_alg
1094 #undef _
1095     return 0;
1096 }
1097
1098 static u32
1099 cryptodev_count_queue (u32 numa)
1100 {
1101   struct rte_cryptodev_info info;
1102   u32 n_cryptodev = rte_cryptodev_count ();
1103   u32 i, q_count = 0;
1104
1105   for (i = 0; i < n_cryptodev; i++)
1106     {
1107       rte_cryptodev_info_get (i, &info);
1108
1109       /* only device support symmetric crypto is used */
1110       if (!(info.feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO))
1111         continue;
1112       q_count += info.max_nb_queue_pairs;
1113     }
1114
1115   return q_count;
1116 }
1117
1118 static int
1119 cryptodev_configure (vlib_main_t *vm, uint32_t cryptodev_id)
1120 {
1121   struct rte_cryptodev_info info;
1122   struct rte_cryptodev *cdev;
1123   cryptodev_main_t *cmt = &cryptodev_main;
1124   u32 i;
1125   int ret;
1126
1127   rte_cryptodev_info_get (cryptodev_id, &info);
1128
1129   /* do not configure the device that does not support symmetric crypto */
1130   if (!(info.feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO))
1131     return 0;
1132
1133   ret = check_cryptodev_alg_support (cryptodev_id);
1134   if (ret != 0)
1135     return ret;
1136
1137   cdev = rte_cryptodev_pmd_get_dev (cryptodev_id);
1138   /** If the device is already started, we reuse it, otherwise configure
1139    *  both the device and queue pair.
1140    **/
1141   if (!cdev->data->dev_started)
1142     {
1143       struct rte_cryptodev_config cfg;
1144
1145       cfg.socket_id = info.device->numa_node;
1146       cfg.nb_queue_pairs = info.max_nb_queue_pairs;
1147
1148       rte_cryptodev_configure (cryptodev_id, &cfg);
1149
1150       for (i = 0; i < info.max_nb_queue_pairs; i++)
1151         {
1152           struct rte_cryptodev_qp_conf qp_cfg;
1153
1154           int ret;
1155
1156           qp_cfg.mp_session = 0;
1157           qp_cfg.mp_session_private = 0;
1158           qp_cfg.nb_descriptors = CRYPTODEV_NB_CRYPTO_OPS;
1159
1160           ret = rte_cryptodev_queue_pair_setup (cryptodev_id, i, &qp_cfg,
1161                                                 info.device->numa_node);
1162           if (ret)
1163             break;
1164         }
1165       if (i != info.max_nb_queue_pairs)
1166         return -1;
1167       /* start the device */
1168       rte_cryptodev_start (i);
1169     }
1170
1171   for (i = 0; i < cdev->data->nb_queue_pairs; i++)
1172     {
1173       cryptodev_inst_t *cdev_inst;
1174       vec_add2(cmt->cryptodev_inst, cdev_inst, 1);
1175       cdev_inst->desc = vec_new (char, strlen (info.device->name) + 10);
1176       cdev_inst->dev_id = cryptodev_id;
1177       cdev_inst->q_id = i;
1178
1179       snprintf (cdev_inst->desc, strlen (info.device->name) + 9,
1180                 "%s_q%u", info.device->name, i);
1181     }
1182
1183   return 0;
1184 }
1185
1186 static int
1187 cryptodev_cmp (void *v1, void *v2)
1188 {
1189   cryptodev_inst_t *a1 = v1;
1190   cryptodev_inst_t *a2 = v2;
1191
1192   if (a1->q_id > a2->q_id)
1193     return 1;
1194   if (a1->q_id < a2->q_id)
1195     return -1;
1196   return 0;
1197 }
1198
1199 static int
1200 cryptodev_probe (vlib_main_t *vm, u32 n_workers)
1201 {
1202   cryptodev_main_t *cmt = &cryptodev_main;
1203   u32 n_queues = cryptodev_count_queue (vm->numa_node);
1204   u32 i;
1205   int ret;
1206
1207   /* If there is not enough queues, exit */
1208   if (n_queues < n_workers)
1209       return -1;
1210
1211   for (i = 0; i < rte_cryptodev_count (); i++)
1212     {
1213       ret = cryptodev_configure (vm, i);
1214       if (ret)
1215         return ret;
1216     }
1217
1218   vec_sort_with_function(cmt->cryptodev_inst, cryptodev_cmp);
1219
1220   return 0;
1221 }
1222
1223 static int
1224 cryptodev_get_session_sz (vlib_main_t *vm, uint32_t n_workers)
1225 {
1226   u32 sess_data_sz = 0, i;
1227
1228   if (rte_cryptodev_count () == 0)
1229     return -1;
1230
1231   for (i = 0; i < rte_cryptodev_count (); i++)
1232     {
1233       u32 dev_sess_sz = rte_cryptodev_sym_get_private_session_size (i);
1234
1235       sess_data_sz = dev_sess_sz > sess_data_sz ? dev_sess_sz : sess_data_sz;
1236     }
1237
1238   return sess_data_sz;
1239 }
1240
1241 static void
1242 dpdk_disable_cryptodev_engine (vlib_main_t * vm)
1243 {
1244   cryptodev_main_t *cmt = &cryptodev_main;
1245   cryptodev_numa_data_t *numa_data;
1246
1247   vec_validate (cmt->per_numa_data, vm->numa_node);
1248   numa_data = vec_elt_at_index (cmt->per_numa_data, vm->numa_node);
1249
1250   if (numa_data->sess_pool)
1251     rte_mempool_free (numa_data->sess_pool);
1252   if (numa_data->sess_priv_pool)
1253     rte_mempool_free (numa_data->sess_priv_pool);
1254   if (numa_data->cop_pool)
1255     rte_mempool_free (numa_data->cop_pool);
1256 }
1257
1258 static void
1259 crypto_op_init (struct rte_mempool *mempool,
1260                 void *_arg __attribute__ ((unused)),
1261                 void *_obj, unsigned i __attribute__ ((unused)))
1262 {
1263   struct rte_crypto_op *op = _obj;
1264
1265   op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1266   op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1267   op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1268   op->phys_addr = rte_mempool_virt2iova (_obj);
1269   op->mempool = mempool;
1270 }
1271
1272
1273 clib_error_t *
1274 dpdk_cryptodev_init (vlib_main_t * vm)
1275 {
1276   cryptodev_main_t *cmt = &cryptodev_main;
1277   vlib_thread_main_t *tm = vlib_get_thread_main ();
1278   cryptodev_engine_thread_t *ptd;
1279   cryptodev_numa_data_t *numa_data;
1280   struct rte_mempool *mp;
1281   u32 skip_master = vlib_num_workers () > 0;
1282   u32 n_workers = tm->n_vlib_mains - skip_master;
1283   u32 numa = vm->numa_node;
1284   i32 sess_sz;
1285   u64 n_cop_elts;
1286   u32 eidx;
1287   u32 i;
1288   u8 *name = 0;
1289   clib_error_t *error;
1290   struct rte_crypto_op_pool_private *priv;
1291
1292   cmt->iova_mode = rte_eal_iova_mode ();
1293
1294   sess_sz = cryptodev_get_session_sz(vm, n_workers);
1295   if (sess_sz < 0)
1296     {
1297       error = clib_error_return (0, "Not enough cryptodevs");
1298       return error;
1299     }
1300
1301   /* A total of 4 times n_worker threads * frame size as crypto ops */
1302   n_cop_elts = max_pow2 ((u64)n_workers * CRYPTODEV_NB_CRYPTO_OPS);
1303
1304   /* probe all cryptodev devices and get queue info */
1305   if (cryptodev_probe (vm, n_workers) < 0)
1306     {
1307       error = clib_error_return (0, "Failed to configure cryptodev");
1308       goto err_handling;
1309     }
1310
1311   clib_bitmap_vec_validate (cmt->active_cdev_inst_mask, tm->n_vlib_mains);
1312   clib_spinlock_init (&cmt->tlock);
1313
1314   vec_validate_aligned(cmt->per_thread_data, tm->n_vlib_mains - 1,
1315                        CLIB_CACHE_LINE_BYTES);
1316   for (i = skip_master; i < tm->n_vlib_mains; i++)
1317     {
1318       ptd = cmt->per_thread_data + i;
1319
1320       cryptodev_assign_resource (ptd, 0, CRYPTODEV_RESOURCE_ASSIGN_AUTO);
1321       name = format (0, "frames_ring_%u%c", i, 0);
1322       ptd->ring = rte_ring_create((char *) name, CRYPTODEV_NB_CRYPTO_OPS,
1323                                   vm->numa_node, RING_F_SP_ENQ|RING_F_SC_DEQ);
1324       if (!ptd->ring)
1325         {
1326           error = clib_error_return (0, "Not enough memory for mp %s", name);
1327           vec_free (name);
1328           goto err_handling;
1329         }
1330       vec_validate (ptd->cops, VNET_CRYPTO_FRAME_SIZE - 1);
1331       vec_free(name);
1332
1333       numa = vlib_mains[i]->numa_node;
1334
1335       vec_validate (cmt->per_numa_data, numa);
1336       numa_data = vec_elt_at_index (cmt->per_numa_data, numa);
1337
1338       if (numa_data->sess_pool)
1339         continue;
1340
1341       /* create session pool for the numa node */
1342       name = format (0, "vcryptodev_sess_pool_%u%c", numa, 0);
1343       mp = rte_cryptodev_sym_session_pool_create (
1344         (char *) name, CRYPTODEV_NB_SESSION, 0, 0, 0, numa);
1345       if (!mp)
1346         {
1347           error = clib_error_return (0, "Not enough memory for mp %s", name);
1348           goto err_handling;
1349         }
1350       vec_free (name);
1351
1352       numa_data->sess_pool = mp;
1353
1354       /* create session private pool for the numa node */
1355       name = format (0, "cryptodev_sess_pool_%u%c", numa, 0);
1356       mp = rte_mempool_create ((char *) name, CRYPTODEV_NB_SESSION, sess_sz, 0,
1357                                0, NULL, NULL, NULL, NULL, numa, 0);
1358       if (!mp)
1359         {
1360           error = clib_error_return (0, "Not enough memory for mp %s", name);
1361           vec_free (name);
1362           goto err_handling;
1363         }
1364
1365       vec_free (name);
1366
1367       numa_data->sess_priv_pool = mp;
1368
1369       /* create cryptodev op pool */
1370       name = format (0, "cryptodev_op_pool_%u%c", numa, 0);
1371
1372       mp = rte_mempool_create ((char *) name, n_cop_elts,
1373                                sizeof (cryptodev_op_t), VLIB_FRAME_SIZE * 2,
1374                                sizeof (struct rte_crypto_op_pool_private),
1375                                NULL, NULL, crypto_op_init, NULL, numa, 0);
1376       if (!mp)
1377         {
1378           error = clib_error_return (0, "Not enough memory for mp %s", name);
1379           vec_free (name);
1380           goto err_handling;
1381         }
1382
1383       priv = rte_mempool_get_priv (mp);
1384       priv->priv_size = sizeof (struct rte_crypto_op_pool_private);
1385       priv->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1386       vec_free (name);
1387       numa_data->cop_pool = mp;
1388     }
1389
1390   /* register handler */
1391   eidx = vnet_crypto_register_engine (vm, "dpdk_cryptodev", 100,
1392                                       "DPDK Cryptodev Engine");
1393
1394 #define _(a, b, c, d, e, f) \
1395   vnet_crypto_register_async_handler \
1396     (vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_ENC, \
1397         cryptodev_enqueue_gcm_aad_##f##_enc,\
1398         cryptodev_frame_dequeue); \
1399   vnet_crypto_register_async_handler \
1400     (vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_DEC, \
1401         cryptodev_enqueue_gcm_aad_##f##_dec, \
1402         cryptodev_frame_dequeue);
1403
1404   foreach_vnet_aead_crypto_conversion
1405 #undef _
1406
1407 #define _(a, b, c, d) \
1408   vnet_crypto_register_async_handler \
1409     (vm, eidx, VNET_CRYPTO_OP_##a##_##c##_TAG##d##_ENC, \
1410         cryptodev_enqueue_linked_alg_enc, \
1411         cryptodev_frame_dequeue); \
1412   vnet_crypto_register_async_handler \
1413     (vm, eidx, VNET_CRYPTO_OP_##a##_##c##_TAG##d##_DEC, \
1414         cryptodev_enqueue_linked_alg_dec, \
1415         cryptodev_frame_dequeue);
1416
1417     foreach_cryptodev_link_async_alg
1418 #undef _
1419
1420   vnet_crypto_register_key_handler (vm, eidx, cryptodev_key_handler);
1421
1422   /* this engine is only enabled when cryptodev device(s) are presented in
1423    * startup.conf. Assume it is wanted to be used, turn on async mode here.
1424    */
1425   vnet_crypto_request_async_mode (1);
1426   ipsec_set_async_mode (1);
1427
1428   return 0;
1429
1430 err_handling:
1431   dpdk_disable_cryptodev_engine (vm);
1432
1433   return error;
1434 }
1435 /* *INDENT-On* */
1436
1437 /*
1438  * fd.io coding-style-patch-verification: ON
1439  *
1440  * Local Variables:
1441  * eval: (c-set-style "gnu")
1442  * End:
1443  */