dpdk-cryptodev: enq/deq scheme rework
[vpp.git] / src / plugins / dpdk / cryptodev / cryptodev_op_data_path.c
1
2 /*
3  *------------------------------------------------------------------
4  * Copyright (c) 2019 - 2021 Intel and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *------------------------------------------------------------------
17  */
18
19 #include <vlib/vlib.h>
20 #include <vnet/crypto/crypto.h>
21
22 #include <dpdk/buffer.h>
23 #include <dpdk/device/dpdk.h>
24 #include <dpdk/device/dpdk_priv.h>
25 #undef always_inline
26 #include <rte_bus_vdev.h>
27 #include <rte_cryptodev.h>
28 #include <rte_crypto_sym.h>
29 #include <rte_crypto.h>
30 #include <rte_ring_peek_zc.h>
31 #include <rte_config.h>
32
33 #include "cryptodev.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_AAD_OFFSET (offsetof (cryptodev_op_t, aad))
42
43 #define foreach_vnet_crypto_status_conversion                                 \
44   _ (SUCCESS, COMPLETED)                                                      \
45   _ (NOT_PROCESSED, WORK_IN_PROGRESS)                                         \
46   _ (AUTH_FAILED, FAIL_BAD_HMAC)                                              \
47   _ (INVALID_SESSION, FAIL_ENGINE_ERR)                                        \
48   _ (INVALID_ARGS, FAIL_ENGINE_ERR)                                           \
49   _ (ERROR, FAIL_ENGINE_ERR)
50
51 static const vnet_crypto_op_status_t cryptodev_status_conversion[] = {
52 #define _(a, b) VNET_CRYPTO_OP_STATUS_##b,
53   foreach_vnet_crypto_status_conversion
54 #undef _
55 };
56
57 static_always_inline rte_iova_t
58 cryptodev_get_iova (clib_pmalloc_main_t *pm, enum rte_iova_mode mode,
59                     void *data)
60 {
61   u64 index;
62   if (mode == RTE_IOVA_VA)
63     return (rte_iova_t) pointer_to_uword (data);
64
65   index = clib_pmalloc_get_page_index (pm, data);
66   return pointer_to_uword (data) - pm->lookup_table[index];
67 }
68
69 static_always_inline void
70 cryptodev_validate_mbuf_chain (vlib_main_t *vm, struct rte_mbuf *mb,
71                                vlib_buffer_t *b)
72 {
73   struct rte_mbuf *first_mb = mb, *last_mb = mb; /**< last mbuf */
74   /* when input node is not dpdk, mbuf data len is not initialized, for
75    * single buffer it is not a problem since the data length is written
76    * into cryptodev operation. For chained buffer a reference data length
77    * has to be computed through vlib_buffer.
78    *
79    * even when input node is dpdk, it is possible chained vlib_buffers
80    * are updated (either added or removed a buffer) but not not mbuf fields.
81    * we have to re-link every mbuf in the chain.
82    */
83   u16 data_len = b->current_length +
84                  (b->data + b->current_data - rte_pktmbuf_mtod (mb, u8 *));
85
86   first_mb->nb_segs = 1;
87   first_mb->pkt_len = first_mb->data_len = data_len;
88
89   while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
90     {
91       b = vlib_get_buffer (vm, b->next_buffer);
92       mb = rte_mbuf_from_vlib_buffer (b);
93       if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_EXT_HDR_VALID) == 0))
94         rte_pktmbuf_reset (mb);
95       last_mb->next = mb;
96       last_mb = mb;
97       mb->data_len = b->current_length;
98       mb->pkt_len = b->current_length;
99       mb->data_off = VLIB_BUFFER_PRE_DATA_SIZE + b->current_data;
100       first_mb->nb_segs++;
101       if (PREDICT_FALSE (b->ref_count > 1))
102         mb->pool =
103           dpdk_no_cache_mempool_by_buffer_pool_index[b->buffer_pool_index];
104     }
105 }
106
107 static_always_inline void
108 crypto_op_init (struct rte_mempool *mempool,
109                 void *_arg __attribute__ ((unused)), void *_obj,
110                 unsigned i __attribute__ ((unused)))
111 {
112   struct rte_crypto_op *op = _obj;
113
114   op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
115   op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
116   op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
117   op->phys_addr = rte_mempool_virt2iova (_obj);
118   op->mempool = mempool;
119 }
120
121 static_always_inline int
122 cryptodev_frame_linked_algs_enqueue (vlib_main_t *vm,
123                                      vnet_crypto_async_frame_t *frame,
124                                      cryptodev_op_type_t op_type)
125 {
126   cryptodev_main_t *cmt = &cryptodev_main;
127   clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
128   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
129   vnet_crypto_async_frame_elt_t *fe;
130   cryptodev_session_t *sess = 0;
131   cryptodev_op_t **cop;
132   u32 *bi;
133   u32 n_enqueue, n_elts;
134   u32 last_key_index = ~0;
135
136   if (PREDICT_FALSE (frame == 0 || frame->n_elts == 0))
137     return -1;
138   n_elts = frame->n_elts;
139
140   if (PREDICT_FALSE (CRYPTODEV_NB_CRYPTO_OPS - cet->inflight < n_elts))
141     {
142       cryptodev_mark_frame_err_status (frame,
143                                        VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR,
144                                        VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED);
145       return -1;
146     }
147
148   if (PREDICT_FALSE (
149         rte_mempool_get_bulk (cet->cop_pool, (void **) cet->cops, n_elts) < 0))
150     {
151       cryptodev_mark_frame_err_status (frame,
152                                        VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR,
153                                        VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED);
154       return -1;
155     }
156
157   cop = cet->cops;
158   fe = frame->elts;
159   bi = frame->buffer_indices;
160   cop[0]->frame = frame;
161   cop[0]->n_elts = n_elts;
162
163   while (n_elts)
164     {
165       vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
166       struct rte_crypto_sym_op *sop = &cop[0]->sop;
167       i16 crypto_offset = fe->crypto_start_offset;
168       i16 integ_offset = fe->integ_start_offset;
169       u32 offset_diff = crypto_offset - integ_offset;
170
171       if (n_elts > 2)
172         {
173           CLIB_PREFETCH (cop[1], sizeof (*cop[1]), STORE);
174           CLIB_PREFETCH (cop[2], sizeof (*cop[2]), STORE);
175           clib_prefetch_load (&fe[1]);
176           clib_prefetch_load (&fe[2]);
177         }
178       if (last_key_index != fe->key_index)
179         {
180           cryptodev_key_t *key = vec_elt_at_index (cmt->keys, fe->key_index);
181           last_key_index = fe->key_index;
182
183           if (key->keys[vm->numa_node][op_type] == 0)
184             {
185               if (PREDICT_FALSE (
186                     cryptodev_session_create (vm, last_key_index, 0) < 0))
187                 {
188                   cryptodev_mark_frame_err_status (
189                     frame, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR,
190                     VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED);
191                   return -1;
192                 }
193             }
194           sess = key->keys[vm->numa_node][op_type];
195         }
196
197       sop->m_src = rte_mbuf_from_vlib_buffer (b);
198       sop->m_src->data_off = VLIB_BUFFER_PRE_DATA_SIZE;
199       sop->m_dst = 0;
200       /* mbuf prepend happens in the tx, but vlib_buffer happens in the nodes,
201        * so we have to manually adjust mbuf data_off here so cryptodev can
202        * correctly compute the data pointer. The prepend here will be later
203        * rewritten by tx. */
204       if (PREDICT_TRUE (fe->integ_start_offset < 0))
205         {
206           sop->m_src->data_off += fe->integ_start_offset;
207           integ_offset = 0;
208           crypto_offset = offset_diff;
209         }
210       sop->session = sess;
211       sop->cipher.data.offset = crypto_offset;
212       sop->cipher.data.length = fe->crypto_total_length;
213       sop->auth.data.offset = integ_offset;
214       sop->auth.data.length = fe->crypto_total_length + fe->integ_length_adj;
215       sop->auth.digest.data = fe->digest;
216       sop->auth.digest.phys_addr =
217         cryptodev_get_iova (pm, cmt->iova_mode, fe->digest);
218       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
219         cryptodev_validate_mbuf_chain (vm, sop->m_src, b);
220       else
221         /* for input nodes that are not dpdk-input, it is possible the mbuf
222          * was updated before as one of the chained mbufs. Setting nb_segs
223          * to 1 here to prevent the cryptodev PMD to access potentially
224          * invalid m_src->next pointers.
225          */
226         sop->m_src->nb_segs = 1;
227       clib_memcpy_fast (cop[0]->iv, fe->iv, 16);
228       cop++;
229       bi++;
230       fe++;
231       n_elts--;
232     }
233
234   n_enqueue = rte_cryptodev_enqueue_burst (cet->cryptodev_id, cet->cryptodev_q,
235                                            (struct rte_crypto_op **) cet->cops,
236                                            frame->n_elts);
237   ASSERT (n_enqueue == frame->n_elts);
238   cet->inflight += n_enqueue;
239
240   return 0;
241 }
242
243 static_always_inline int
244 cryptodev_frame_aead_enqueue (vlib_main_t *vm,
245                               vnet_crypto_async_frame_t *frame,
246                               cryptodev_op_type_t op_type, u8 aad_len)
247 {
248   cryptodev_main_t *cmt = &cryptodev_main;
249   clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
250   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
251   vnet_crypto_async_frame_elt_t *fe;
252   cryptodev_session_t *sess = 0;
253   cryptodev_op_t **cop;
254   u32 *bi;
255   u32 n_enqueue = 0, n_elts;
256   u32 last_key_index = ~0;
257
258   if (PREDICT_FALSE (frame == 0 || frame->n_elts == 0))
259     return -1;
260   n_elts = frame->n_elts;
261
262   if (PREDICT_FALSE (CRYPTODEV_MAX_INFLIGHT - cet->inflight < n_elts))
263     {
264       cryptodev_mark_frame_err_status (frame,
265                                        VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR,
266                                        VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED);
267       return -1;
268     }
269
270   if (PREDICT_FALSE (
271         rte_mempool_get_bulk (cet->cop_pool, (void **) cet->cops, n_elts) < 0))
272     {
273       cryptodev_mark_frame_err_status (frame,
274                                        VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR,
275                                        VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED);
276       return -1;
277     }
278
279   cop = cet->cops;
280   fe = frame->elts;
281   bi = frame->buffer_indices;
282   cop[0]->frame = frame;
283   cop[0]->n_elts = n_elts;
284
285   while (n_elts)
286     {
287       vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
288       struct rte_crypto_sym_op *sop = &cop[0]->sop;
289       u16 crypto_offset = fe->crypto_start_offset;
290
291       if (n_elts > 2)
292         {
293           CLIB_PREFETCH (cop[1], sizeof (*cop[1]), STORE);
294           CLIB_PREFETCH (cop[2], sizeof (*cop[2]), STORE);
295           clib_prefetch_load (&fe[1]);
296           clib_prefetch_load (&fe[2]);
297         }
298       if (last_key_index != fe->key_index)
299         {
300           cryptodev_key_t *key = vec_elt_at_index (cmt->keys, fe->key_index);
301
302           last_key_index = fe->key_index;
303           if (key->keys[vm->numa_node][op_type] == 0)
304             {
305               if (PREDICT_FALSE (cryptodev_session_create (vm, last_key_index,
306                                                            aad_len) < 0))
307                 {
308                   cryptodev_mark_frame_err_status (
309                     frame, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR,
310                     VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED);
311                   return -1;
312                 }
313             }
314           else if (PREDICT_FALSE (
315 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
316                      rte_cryptodev_sym_session_opaque_data_get (
317                        key->keys[vm->numa_node][op_type]) != (u64) aad_len
318 #else
319                      key->keys[vm->numa_node][op_type]->opaque_data != aad_len
320 #endif
321                      ))
322             {
323               cryptodev_sess_handler (vm, VNET_CRYPTO_KEY_OP_DEL,
324                                       fe->key_index, aad_len);
325               if (PREDICT_FALSE (cryptodev_session_create (vm, last_key_index,
326                                                            aad_len) < 0))
327                 {
328                   cryptodev_mark_frame_err_status (
329                     frame, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR,
330                     VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED);
331                   return -1;
332                 }
333             }
334
335           sess = key->keys[vm->numa_node][op_type];
336         }
337
338       sop->m_src = rte_mbuf_from_vlib_buffer (b);
339       sop->m_src->data_off = VLIB_BUFFER_PRE_DATA_SIZE;
340       sop->m_dst = 0;
341       /* mbuf prepend happens in the tx, but vlib_buffer happens in the nodes,
342        * so we have to manually adjust mbuf data_off here so cryptodev can
343        * correctly compute the data pointer. The prepend here will be later
344        * rewritten by tx. */
345       if (PREDICT_FALSE (fe->crypto_start_offset < 0))
346         {
347           rte_pktmbuf_prepend (sop->m_src, -fe->crypto_start_offset);
348           crypto_offset = 0;
349         }
350
351       sop->session = sess;
352       sop->aead.aad.data = cop[0]->aad;
353       sop->aead.aad.phys_addr = cop[0]->op.phys_addr + CRYPTODEV_AAD_OFFSET;
354       sop->aead.data.length = fe->crypto_total_length;
355       sop->aead.data.offset = crypto_offset;
356       sop->aead.digest.data = fe->tag;
357       sop->aead.digest.phys_addr =
358         cryptodev_get_iova (pm, cmt->iova_mode, fe->tag);
359       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
360         cryptodev_validate_mbuf_chain (vm, sop->m_src, b);
361       else
362         /* for input nodes that are not dpdk-input, it is possible the mbuf
363          * was updated before as one of the chained mbufs. Setting nb_segs
364          * to 1 here to prevent the cryptodev PMD to access potentially
365          * invalid m_src->next pointers.
366          */
367         sop->m_src->nb_segs = 1;
368       clib_memcpy_fast (cop[0]->iv, fe->iv, 12);
369       clib_memcpy_fast (cop[0]->aad, fe->aad, aad_len);
370       cop++;
371       bi++;
372       fe++;
373       n_elts--;
374     }
375
376   n_enqueue = rte_cryptodev_enqueue_burst (cet->cryptodev_id, cet->cryptodev_q,
377                                            (struct rte_crypto_op **) cet->cops,
378                                            frame->n_elts);
379   ASSERT (n_enqueue == frame->n_elts);
380   cet->inflight += n_enqueue;
381
382   return 0;
383 }
384
385 static_always_inline u16
386 cryptodev_ring_deq (struct rte_ring *r, cryptodev_op_t **cops)
387 {
388   u16 n, n_elts = 0;
389
390   n = rte_ring_dequeue_bulk_start (r, (void **) cops, 1, 0);
391   rte_ring_dequeue_finish (r, 0);
392   if (!n)
393     return 0;
394
395   n = cops[0]->n_elts;
396   if (rte_ring_count (r) < n)
397     return 0;
398
399   n_elts = rte_ring_sc_dequeue_bulk (r, (void **) cops, n, 0);
400   ASSERT (n_elts == n);
401
402   return n_elts;
403 }
404
405 static_always_inline vnet_crypto_async_frame_t *
406 cryptodev_frame_dequeue (vlib_main_t *vm, u32 *nb_elts_processed,
407                          u32 *enqueue_thread_idx)
408 {
409   cryptodev_main_t *cmt = &cryptodev_main;
410   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
411   cryptodev_op_t **cop = cet->cops;
412   vnet_crypto_async_frame_elt_t *fe;
413   vnet_crypto_async_frame_t *frame;
414   u32 n_elts, n_completed_ops = rte_ring_count (cet->ring);
415   u32 ss0 = 0, ss1 = 0, ss2 = 0, ss3 = 0; /* sum of status */
416
417   if (cet->inflight)
418     {
419       n_elts = rte_cryptodev_dequeue_burst (
420         cet->cryptodev_id, cet->cryptodev_q,
421         (struct rte_crypto_op **) cet->cops, VNET_CRYPTO_FRAME_SIZE);
422
423       if (n_elts)
424         {
425           cet->inflight -= n_elts;
426           n_completed_ops += n_elts;
427
428           rte_ring_sp_enqueue_burst (cet->ring, (void **) cet->cops, n_elts,
429                                      NULL);
430         }
431     }
432
433   if (PREDICT_FALSE (n_completed_ops == 0))
434     return 0;
435
436   n_elts = cryptodev_ring_deq (cet->ring, cop);
437   if (!n_elts)
438     return 0;
439
440   frame = cop[0]->frame;
441   fe = frame->elts;
442
443   while (n_elts > 4)
444     {
445       ss0 |= fe[0].status = cryptodev_status_conversion[cop[0]->op.status];
446       ss1 |= fe[1].status = cryptodev_status_conversion[cop[1]->op.status];
447       ss2 |= fe[2].status = cryptodev_status_conversion[cop[2]->op.status];
448       ss3 |= fe[3].status = cryptodev_status_conversion[cop[3]->op.status];
449
450       cop += 4;
451       fe += 4;
452       n_elts -= 4;
453     }
454
455   while (n_elts)
456     {
457       ss0 |= fe[0].status = cryptodev_status_conversion[cop[0]->op.status];
458       fe++;
459       cop++;
460       n_elts--;
461     }
462
463   frame->state = (ss0 | ss1 | ss2 | ss3) == VNET_CRYPTO_OP_STATUS_COMPLETED ?
464                    VNET_CRYPTO_FRAME_STATE_SUCCESS :
465                    VNET_CRYPTO_FRAME_STATE_ELT_ERROR;
466
467   rte_mempool_put_bulk (cet->cop_pool, (void **) cet->cops, frame->n_elts);
468   *nb_elts_processed = frame->n_elts;
469   *enqueue_thread_idx = frame->enqueue_thread_index;
470   return frame;
471 }
472
473 static_always_inline int
474 cryptodev_enqueue_aead_aad_0_enc (vlib_main_t *vm,
475                                   vnet_crypto_async_frame_t *frame)
476 {
477   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT,
478                                        0);
479 }
480 static_always_inline int
481 cryptodev_enqueue_aead_aad_8_enc (vlib_main_t *vm,
482                                   vnet_crypto_async_frame_t *frame)
483 {
484   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT,
485                                        8);
486 }
487 static_always_inline int
488 cryptodev_enqueue_aead_aad_12_enc (vlib_main_t *vm,
489                                    vnet_crypto_async_frame_t *frame)
490 {
491   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT,
492                                        12);
493 }
494
495 static_always_inline int
496 cryptodev_enqueue_aead_aad_0_dec (vlib_main_t *vm,
497                                   vnet_crypto_async_frame_t *frame)
498 {
499   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT,
500                                        0);
501 }
502 static_always_inline int
503 cryptodev_enqueue_aead_aad_8_dec (vlib_main_t *vm,
504                                   vnet_crypto_async_frame_t *frame)
505 {
506   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT,
507                                        8);
508 }
509 static_always_inline int
510 cryptodev_enqueue_aead_aad_12_dec (vlib_main_t *vm,
511                                    vnet_crypto_async_frame_t *frame)
512 {
513   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT,
514                                        12);
515 }
516
517 static_always_inline int
518 cryptodev_enqueue_linked_alg_enc (vlib_main_t *vm,
519                                   vnet_crypto_async_frame_t *frame)
520 {
521   return cryptodev_frame_linked_algs_enqueue (vm, frame,
522                                               CRYPTODEV_OP_TYPE_ENCRYPT);
523 }
524
525 static_always_inline int
526 cryptodev_enqueue_linked_alg_dec (vlib_main_t *vm,
527                                   vnet_crypto_async_frame_t *frame)
528 {
529   return cryptodev_frame_linked_algs_enqueue (vm, frame,
530                                               CRYPTODEV_OP_TYPE_DECRYPT);
531 }
532
533 clib_error_t *
534 cryptodev_register_cop_hdl (vlib_main_t *vm, u32 eidx)
535 {
536   cryptodev_main_t *cmt = &cryptodev_main;
537   cryptodev_engine_thread_t *cet;
538   struct rte_cryptodev_sym_capability_idx cap_auth_idx;
539   struct rte_cryptodev_sym_capability_idx cap_cipher_idx;
540   struct rte_cryptodev_sym_capability_idx cap_aead_idx;
541   u8 *name;
542   clib_error_t *error = 0;
543   u32 ref_cnt = 0;
544
545   vec_foreach (cet, cmt->per_thread_data)
546     {
547       u32 thread_index = cet - cmt->per_thread_data;
548       u32 numa = vlib_get_main_by_index (thread_index)->numa_node;
549       name = format (0, "vpp_cop_pool_%u_%u", numa, thread_index);
550       cet->cop_pool = rte_mempool_create (
551         (char *) name, CRYPTODEV_NB_CRYPTO_OPS, sizeof (cryptodev_op_t), 0,
552         sizeof (struct rte_crypto_op_pool_private), NULL, NULL, crypto_op_init,
553         NULL, vm->numa_node, 0);
554       if (!cet->cop_pool)
555         {
556           error = clib_error_return (
557             0, "Failed to create cryptodev op pool %s", name);
558
559           goto error_exit;
560         }
561       vec_free (name);
562
563       name = format (0, "frames_ring_%u_%u", numa, thread_index);
564       cet->ring =
565         rte_ring_create ((char *) name, CRYPTODEV_NB_CRYPTO_OPS, vm->numa_node,
566                          RING_F_SP_ENQ | RING_F_SC_DEQ);
567       if (!cet->ring)
568         {
569           error = clib_error_return (
570             0, "Failed to create cryptodev op pool %s", name);
571
572           goto error_exit;
573         }
574       vec_free (name);
575
576       vec_validate (cet->cops, VNET_CRYPTO_FRAME_SIZE - 1);
577     }
578
579 #define _(a, b, c, d, e, f, g)                                                \
580   cap_aead_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;                              \
581   cap_aead_idx.algo.aead = RTE_CRYPTO_##b##_##c;                              \
582   if (cryptodev_check_cap_support (&cap_aead_idx, g, e, f))                   \
583     {                                                                         \
584       vnet_crypto_register_enqueue_handler (                                  \
585         vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_ENC,                 \
586         cryptodev_enqueue_aead_aad_##f##_enc);                                \
587       vnet_crypto_register_enqueue_handler (                                  \
588         vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_DEC,                 \
589         cryptodev_enqueue_aead_aad_##f##_dec);                                \
590       ref_cnt++;                                                              \
591     }
592   foreach_vnet_aead_crypto_conversion
593 #undef _
594
595 #define _(a, b, c, d, e)                                                      \
596   cap_auth_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;                              \
597   cap_auth_idx.algo.auth = RTE_CRYPTO_AUTH_##d##_HMAC;                        \
598   cap_cipher_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;                          \
599   cap_cipher_idx.algo.cipher = RTE_CRYPTO_CIPHER_##b;                         \
600   if (cryptodev_check_cap_support (&cap_cipher_idx, c, -1, -1) &&             \
601       cryptodev_check_cap_support (&cap_auth_idx, -1, e, -1))                 \
602     {                                                                         \
603       vnet_crypto_register_enqueue_handler (                                  \
604         vm, eidx, VNET_CRYPTO_OP_##a##_##d##_TAG##e##_ENC,                    \
605         cryptodev_enqueue_linked_alg_enc);                                    \
606       vnet_crypto_register_enqueue_handler (                                  \
607         vm, eidx, VNET_CRYPTO_OP_##a##_##d##_TAG##e##_DEC,                    \
608         cryptodev_enqueue_linked_alg_dec);                                    \
609       ref_cnt++;                                                              \
610     }
611     foreach_cryptodev_link_async_alg
612 #undef _
613
614     if (ref_cnt)
615       vnet_crypto_register_dequeue_handler (vm, eidx, cryptodev_frame_dequeue);
616
617     return 0;
618
619 error_exit:
620   vec_foreach (cet, cmt->per_thread_data)
621     {
622       if (cet->ring)
623         rte_ring_free (cet->ring);
624
625       if (cet->cop_pool)
626         rte_mempool_free (cet->cop_pool);
627     }
628
629   return error;
630 }