4e4295a9041f35432103f3e0bf8f43fa8ea876e2
[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 (struct rte_mbuf *mb, vlib_buffer_t *b)
71 {
72   /* on vnet side vlib_buffer current_length is updated by cipher padding and
73    * icv_sh. mbuf needs to be sync with these changes */
74   u16 data_len = b->current_length +
75                  (b->data + b->current_data - rte_pktmbuf_mtod (mb, u8 *));
76
77   /* for input nodes that are not dpdk-input, it is possible the mbuf
78    * was updated before as one of the chained mbufs. Setting nb_segs
79    * to 1 here to prevent the cryptodev PMD to access potentially
80    * invalid m_src->next pointers.
81    */
82   mb->nb_segs = 1;
83   mb->pkt_len = mb->data_len = data_len;
84 }
85
86 static_always_inline void
87 cryptodev_validate_mbuf_chain (vlib_main_t *vm, struct rte_mbuf *mb,
88                                vlib_buffer_t *b)
89 {
90   struct rte_mbuf *first_mb = mb, *last_mb = mb; /**< last mbuf */
91   /* when input node is not dpdk, mbuf data len is not initialized, for
92    * single buffer it is not a problem since the data length is written
93    * into cryptodev operation. For chained buffer a reference data length
94    * has to be computed through vlib_buffer.
95    *
96    * even when input node is dpdk, it is possible chained vlib_buffers
97    * are updated (either added or removed a buffer) but not not mbuf fields.
98    * we have to re-link every mbuf in the chain.
99    */
100   u16 data_len = b->current_length +
101                  (b->data + b->current_data - rte_pktmbuf_mtod (mb, u8 *));
102
103   first_mb->nb_segs = 1;
104   first_mb->pkt_len = first_mb->data_len = data_len;
105
106   while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
107     {
108       b = vlib_get_buffer (vm, b->next_buffer);
109       mb = rte_mbuf_from_vlib_buffer (b);
110       if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_EXT_HDR_VALID) == 0))
111         rte_pktmbuf_reset (mb);
112       last_mb->next = mb;
113       last_mb = mb;
114       mb->data_len = b->current_length;
115       mb->pkt_len = b->current_length;
116       mb->data_off = VLIB_BUFFER_PRE_DATA_SIZE + b->current_data;
117       first_mb->nb_segs++;
118       if (PREDICT_FALSE (b->ref_count > 1))
119         mb->pool =
120           dpdk_no_cache_mempool_by_buffer_pool_index[b->buffer_pool_index];
121     }
122 }
123
124 static_always_inline void
125 crypto_op_init (struct rte_mempool *mempool,
126                 void *_arg __attribute__ ((unused)), void *_obj,
127                 unsigned i __attribute__ ((unused)))
128 {
129   struct rte_crypto_op *op = _obj;
130
131   op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
132   op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
133   op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
134   op->phys_addr = rte_mempool_virt2iova (_obj);
135   op->mempool = mempool;
136 }
137
138 static_always_inline int
139 cryptodev_frame_linked_algs_enqueue (vlib_main_t *vm,
140                                      vnet_crypto_async_frame_t *frame,
141                                      cryptodev_op_type_t op_type)
142 {
143   cryptodev_main_t *cmt = &cryptodev_main;
144   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
145   cryptodev_cache_ring_t *ring = &cet->cache_ring;
146   ERROR_ASSERT (frame != 0);
147   ERROR_ASSERT (frame->n_elts > 0);
148   cryptodev_cache_ring_elt_t *ring_elt =
149     cryptodev_cache_ring_push (ring, frame);
150
151   ring_elt->aad_len = 1;
152   ring_elt->op_type = (u8) op_type;
153   return 0;
154 }
155
156 static_always_inline void
157 cryptodev_frame_linked_algs_enqueue_internal (vlib_main_t *vm,
158                                               vnet_crypto_async_frame_t *frame,
159                                               cryptodev_op_type_t op_type)
160 {
161   cryptodev_main_t *cmt = &cryptodev_main;
162   clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
163   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
164   cryptodev_cache_ring_t *ring = &cet->cache_ring;
165   u16 *const enq = &ring->enq_head;
166   vnet_crypto_async_frame_elt_t *fe;
167   cryptodev_session_t *sess = 0;
168   cryptodev_op_t *cops[CRYPTODE_ENQ_MAX] = {};
169   cryptodev_op_t **cop = cops;
170   u32 *bi = 0;
171   u32 n_enqueue, n_elts;
172   u32 last_key_index = ~0;
173   u32 max_to_enq;
174
175   if (PREDICT_FALSE (frame == 0 || frame->n_elts == 0))
176     return;
177
178   max_to_enq = clib_min (CRYPTODE_ENQ_MAX,
179                          frame->n_elts - ring->frames[*enq].enq_elts_head);
180
181   if (cet->inflight + max_to_enq > CRYPTODEV_MAX_INFLIGHT)
182     return;
183
184   n_elts = max_to_enq;
185
186   if (PREDICT_FALSE (
187         rte_mempool_get_bulk (cet->cop_pool, (void **) cops, n_elts) < 0))
188     {
189       cryptodev_mark_frame_fill_err (
190         frame, ring->frames[*enq].frame_elts_errs_mask,
191         ring->frames[*enq].enq_elts_head, max_to_enq,
192         VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
193       ring->frames[*enq].enq_elts_head += max_to_enq;
194       ring->frames[*enq].deq_elts_tail += max_to_enq;
195       cryptodev_cache_ring_update_enq_head (ring, frame);
196       return;
197     }
198
199   fe = frame->elts + ring->frames[*enq].enq_elts_head;
200   bi = frame->buffer_indices + ring->frames[*enq].enq_elts_head;
201
202   while (n_elts)
203     {
204       vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
205       struct rte_crypto_sym_op *sop = &cop[0]->sop;
206       i16 crypto_offset = fe->crypto_start_offset;
207       i16 integ_offset = fe->integ_start_offset;
208       u32 offset_diff = crypto_offset - integ_offset;
209
210       if (n_elts > 2)
211         {
212           CLIB_PREFETCH (cop[1], sizeof (*cop[1]), STORE);
213           CLIB_PREFETCH (cop[2], sizeof (*cop[2]), STORE);
214           clib_prefetch_load (&fe[1]);
215           clib_prefetch_load (&fe[2]);
216         }
217       if (last_key_index != fe->key_index)
218         {
219           cryptodev_key_t *key = vec_elt_at_index (cmt->keys, fe->key_index);
220           last_key_index = fe->key_index;
221
222           if (key->keys[vm->numa_node][op_type] == 0)
223             {
224               if (PREDICT_FALSE (
225                     cryptodev_session_create (vm, last_key_index, 0) < 0))
226                 {
227                   cryptodev_mark_frame_fill_err (
228                     frame, ring->frames[*enq].frame_elts_errs_mask,
229                     ring->frames[*enq].enq_elts_head, max_to_enq,
230                     VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
231                   goto error_exit;
232                 }
233             }
234           sess = key->keys[vm->numa_node][op_type];
235         }
236
237       sop->m_src = rte_mbuf_from_vlib_buffer (b);
238       sop->m_src->data_off = VLIB_BUFFER_PRE_DATA_SIZE;
239       sop->m_dst = 0;
240       /* mbuf prepend happens in the tx, but vlib_buffer happens in the nodes,
241        * so we have to manually adjust mbuf data_off here so cryptodev can
242        * correctly compute the data pointer. The prepend here will be later
243        * rewritten by tx. */
244       if (PREDICT_TRUE (fe->integ_start_offset < 0))
245         {
246           sop->m_src->data_off += fe->integ_start_offset;
247           integ_offset = 0;
248           crypto_offset = offset_diff;
249         }
250       sop->session = sess;
251       sop->cipher.data.offset = crypto_offset;
252       sop->cipher.data.length = fe->crypto_total_length;
253       sop->auth.data.offset = integ_offset;
254       sop->auth.data.length = fe->crypto_total_length + fe->integ_length_adj;
255       sop->auth.digest.data = fe->digest;
256       sop->auth.digest.phys_addr =
257         cryptodev_get_iova (pm, cmt->iova_mode, fe->digest);
258       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
259         cryptodev_validate_mbuf_chain (vm, sop->m_src, b);
260       else
261         cryptodev_validate_mbuf (sop->m_src, b);
262
263       clib_memcpy_fast (cop[0]->iv, fe->iv, 16);
264       ring->frames[*enq].enq_elts_head++;
265       cop++;
266       bi++;
267       fe++;
268       n_elts--;
269     }
270
271   n_enqueue =
272     rte_cryptodev_enqueue_burst (cet->cryptodev_id, cet->cryptodev_q,
273                                  (struct rte_crypto_op **) cops, max_to_enq);
274   ERROR_ASSERT (n_enqueue == max_to_enq);
275   cet->inflight += max_to_enq;
276   cryptodev_cache_ring_update_enq_head (ring, frame);
277   return;
278
279 error_exit:
280   ring->frames[*enq].enq_elts_head += max_to_enq;
281   ring->frames[*enq].deq_elts_tail += max_to_enq;
282   cryptodev_cache_ring_update_enq_head (ring, frame);
283   rte_mempool_put_bulk (cet->cop_pool, (void **) cops, max_to_enq);
284 }
285
286 static_always_inline int
287 cryptodev_frame_aead_enqueue (vlib_main_t *vm,
288                               vnet_crypto_async_frame_t *frame,
289                               cryptodev_op_type_t op_type, u8 aad_len)
290 {
291   cryptodev_main_t *cmt = &cryptodev_main;
292   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
293   cryptodev_cache_ring_t *ring = &cet->cache_ring;
294   ERROR_ASSERT (frame != 0);
295   ERROR_ASSERT (frame->n_elts > 0);
296   cryptodev_cache_ring_elt_t *ring_elt =
297     cryptodev_cache_ring_push (ring, frame);
298   ring_elt->aad_len = aad_len;
299   ring_elt->op_type = (u8) op_type;
300   return 0;
301 }
302
303 static_always_inline int
304 cryptodev_aead_enqueue_internal (vlib_main_t *vm,
305                                  vnet_crypto_async_frame_t *frame,
306                                  cryptodev_op_type_t op_type, u8 aad_len)
307 {
308   cryptodev_main_t *cmt = &cryptodev_main;
309   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
310   cryptodev_cache_ring_t *ring = &cet->cache_ring;
311   u16 *const enq = &ring->enq_head;
312   clib_pmalloc_main_t *pm = vm->physmem_main.pmalloc_main;
313   vnet_crypto_async_frame_elt_t *fe;
314   cryptodev_session_t *sess = 0;
315   cryptodev_op_t *cops[CRYPTODE_ENQ_MAX] = {};
316   cryptodev_op_t **cop = cops;
317   u32 *bi = 0;
318   u32 n_enqueue = 0, n_elts;
319   u32 last_key_index = ~0;
320   u16 left_to_enq = frame->n_elts - ring->frames[*enq].enq_elts_head;
321   const u16 max_to_enq = clib_min (CRYPTODE_ENQ_MAX, left_to_enq);
322
323   if (PREDICT_FALSE (frame == 0 || frame->n_elts == 0))
324     return -1;
325
326   if (cet->inflight + max_to_enq > CRYPTODEV_MAX_INFLIGHT)
327     return -1;
328
329   n_elts = max_to_enq;
330
331   if (PREDICT_FALSE (
332         rte_mempool_get_bulk (cet->cop_pool, (void **) cops, n_elts) < 0))
333     {
334       cryptodev_mark_frame_fill_err (
335         frame, ring->frames[*enq].frame_elts_errs_mask,
336         ring->frames[*enq].enq_elts_head, max_to_enq,
337         VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
338       ring->frames[*enq].enq_elts_head += max_to_enq;
339       ring->frames[*enq].deq_elts_tail += max_to_enq;
340       cryptodev_cache_ring_update_enq_head (ring, frame);
341       return -1;
342     }
343
344   fe = frame->elts + ring->frames[*enq].enq_elts_head;
345   bi = frame->buffer_indices + ring->frames[*enq].enq_elts_head;
346
347   while (n_elts)
348     {
349       vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
350       struct rte_crypto_sym_op *sop = &cop[0]->sop;
351       u16 crypto_offset = fe->crypto_start_offset;
352
353       if (n_elts > 2)
354         {
355           CLIB_PREFETCH (cop[1], sizeof (*cop[1]), STORE);
356           CLIB_PREFETCH (cop[2], sizeof (*cop[2]), STORE);
357           clib_prefetch_load (&fe[1]);
358           clib_prefetch_load (&fe[2]);
359         }
360       if (last_key_index != fe->key_index)
361         {
362           cryptodev_key_t *key = vec_elt_at_index (cmt->keys, fe->key_index);
363
364           last_key_index = fe->key_index;
365           if (key->keys[vm->numa_node][op_type] == 0)
366             {
367               if (PREDICT_FALSE (cryptodev_session_create (vm, last_key_index,
368                                                            aad_len) < 0))
369                 {
370                   cryptodev_mark_frame_fill_err (
371                     frame, ring->frames[*enq].frame_elts_errs_mask,
372                     ring->frames[*enq].enq_elts_head, max_to_enq,
373                     VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
374                   goto error_exit;
375                 }
376             }
377           else if (PREDICT_FALSE (
378 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
379                      rte_cryptodev_sym_session_opaque_data_get (
380                        key->keys[vm->numa_node][op_type]) != (u64) aad_len
381 #else
382                      key->keys[vm->numa_node][op_type]->opaque_data != aad_len
383 #endif
384                      ))
385             {
386               cryptodev_sess_handler (vm, VNET_CRYPTO_KEY_OP_DEL,
387                                       fe->key_index, aad_len);
388               if (PREDICT_FALSE (cryptodev_session_create (vm, last_key_index,
389                                                            aad_len) < 0))
390                 {
391                   cryptodev_mark_frame_fill_err (
392                     frame, ring->frames[*enq].frame_elts_errs_mask,
393                     ring->frames[*enq].enq_elts_head, max_to_enq,
394                     VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
395                   goto error_exit;
396                 }
397             }
398
399           sess = key->keys[vm->numa_node][op_type];
400         }
401
402       sop->m_src = rte_mbuf_from_vlib_buffer (b);
403       sop->m_src->data_off = VLIB_BUFFER_PRE_DATA_SIZE;
404       sop->m_dst = 0;
405       /* mbuf prepend happens in the tx, but vlib_buffer happens in the nodes,
406        * so we have to manually adjust mbuf data_off here so cryptodev can
407        * correctly compute the data pointer. The prepend here will be later
408        * rewritten by tx. */
409       if (PREDICT_FALSE (fe->crypto_start_offset < 0))
410         {
411           rte_pktmbuf_prepend (sop->m_src, -fe->crypto_start_offset);
412           crypto_offset = 0;
413         }
414
415       sop->session = sess;
416       sop->aead.aad.data = cop[0]->aad;
417       sop->aead.aad.phys_addr = cop[0]->op.phys_addr + CRYPTODEV_AAD_OFFSET;
418       sop->aead.data.length = fe->crypto_total_length;
419       sop->aead.data.offset = crypto_offset;
420       sop->aead.digest.data = fe->tag;
421       sop->aead.digest.phys_addr =
422         cryptodev_get_iova (pm, cmt->iova_mode, fe->tag);
423       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
424         cryptodev_validate_mbuf_chain (vm, sop->m_src, b);
425       else
426         cryptodev_validate_mbuf (sop->m_src, b);
427
428       clib_memcpy_fast (cop[0]->iv, fe->iv, 12);
429       clib_memcpy_fast (cop[0]->aad, fe->aad, aad_len);
430
431       cop++;
432       bi++;
433       fe++;
434       n_elts--;
435     }
436
437   n_enqueue =
438     rte_cryptodev_enqueue_burst (cet->cryptodev_id, cet->cryptodev_q,
439                                  (struct rte_crypto_op **) cops, max_to_enq);
440   ERROR_ASSERT (n_enqueue == max_to_enq);
441   cet->inflight += max_to_enq;
442   ring->frames[*enq].enq_elts_head += max_to_enq;
443   cryptodev_cache_ring_update_enq_head (ring, frame);
444
445   return 0;
446
447 error_exit:
448   ring->frames[*enq].enq_elts_head += max_to_enq;
449   ring->frames[*enq].deq_elts_tail += max_to_enq;
450   cryptodev_cache_ring_update_enq_head (ring, frame);
451   rte_mempool_put_bulk (cet->cop_pool, (void **) cops, max_to_enq);
452
453   return -1;
454 }
455
456 static_always_inline u8
457 cryptodev_frame_dequeue_internal (vlib_main_t *vm, u32 *nb_elts_processed,
458                                   u32 *enqueue_thread_idx)
459 {
460   cryptodev_main_t *cmt = &cryptodev_main;
461   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
462   vnet_crypto_async_frame_t *frame = NULL;
463   cryptodev_cache_ring_t *ring = &cet->cache_ring;
464   u16 *const deq = &ring->deq_tail;
465   u16 n_deq, idx, left_to_deq, i;
466   u16 max_to_deq = 0;
467   u16 inflight = cet->inflight;
468   u8 dequeue_more = 0;
469   cryptodev_op_t *cops[CRYPTODE_DEQ_MAX] = {};
470   cryptodev_op_t **cop = cops;
471   vnet_crypto_async_frame_elt_t *fe;
472   u32 n_elts, n;
473   u64 err0 = 0, err1 = 0, err2 = 0, err3 = 0; /* partial errors mask */
474
475   idx = ring->deq_tail;
476
477   for (i = 0; i < VNET_CRYPTO_FRAME_POOL_SIZE; i++)
478     {
479       u32 frame_inflight =
480         CRYPTODEV_CACHE_RING_GET_FRAME_ELTS_INFLIGHT (ring, idx);
481
482       if (PREDICT_TRUE (frame_inflight > 0))
483         break;
484       idx++;
485       idx &= (VNET_CRYPTO_FRAME_POOL_SIZE - 1);
486     }
487
488   ERROR_ASSERT (i != VNET_CRYPTO_FRAME_POOL_SIZE);
489   ring->deq_tail = idx;
490
491   left_to_deq =
492     ring->frames[*deq].f->n_elts - ring->frames[*deq].deq_elts_tail;
493   max_to_deq = clib_min (left_to_deq, CRYPTODE_DEQ_MAX);
494
495   /* deq field can be used to track frame that is currently dequeued
496    based on that you can specify the amount of elements to deq for the frame */
497
498   n_deq =
499     rte_cryptodev_dequeue_burst (cet->cryptodev_id, cet->cryptodev_q,
500                                  (struct rte_crypto_op **) cops, max_to_deq);
501
502   if (n_deq == 0)
503     return dequeue_more;
504
505   frame = ring->frames[*deq].f;
506   fe = frame->elts + ring->frames[*deq].deq_elts_tail;
507
508   n_elts = n_deq;
509   n = ring->frames[*deq].deq_elts_tail;
510
511   while (n_elts > 4)
512     {
513       fe[0].status = cryptodev_status_conversion[cop[0]->op.status];
514       fe[1].status = cryptodev_status_conversion[cop[1]->op.status];
515       fe[2].status = cryptodev_status_conversion[cop[2]->op.status];
516       fe[3].status = cryptodev_status_conversion[cop[3]->op.status];
517
518       err0 |= (fe[0].status == VNET_CRYPTO_OP_STATUS_COMPLETED) << n;
519       err1 |= (fe[1].status == VNET_CRYPTO_OP_STATUS_COMPLETED) << (n + 1);
520       err2 |= (fe[2].status == VNET_CRYPTO_OP_STATUS_COMPLETED) << (n + 2);
521       err3 |= (fe[3].status == VNET_CRYPTO_OP_STATUS_COMPLETED) << (n + 3);
522
523       cop += 4;
524       fe += 4;
525       n_elts -= 4;
526       n += 4;
527     }
528
529   while (n_elts)
530     {
531       fe[0].status = cryptodev_status_conversion[cop[0]->op.status];
532       err0 |= (fe[0].status == VNET_CRYPTO_OP_STATUS_COMPLETED) << n;
533       n++;
534       fe++;
535       cop++;
536       n_elts--;
537     }
538
539   ring->frames[*deq].frame_elts_errs_mask |= (err0 | err1 | err2 | err3);
540
541   rte_mempool_put_bulk (cet->cop_pool, (void **) cops, n_deq);
542
543   inflight -= n_deq;
544   ring->frames[*deq].deq_elts_tail += n_deq;
545   if (cryptodev_cache_ring_update_deq_tail (ring, deq))
546     {
547       *nb_elts_processed = frame->n_elts;
548       *enqueue_thread_idx = frame->enqueue_thread_index;
549       dequeue_more = (max_to_deq < CRYPTODE_DEQ_MAX);
550     }
551
552   cet->inflight = inflight;
553   return dequeue_more;
554 }
555
556 static_always_inline void
557 cryptodev_enqueue_frame (vlib_main_t *vm, cryptodev_cache_ring_elt_t *ring_elt)
558 {
559   cryptodev_op_type_t op_type = (cryptodev_op_type_t) ring_elt->op_type;
560   u8 linked_or_aad_len = ring_elt->aad_len;
561
562   if (linked_or_aad_len == 1)
563     cryptodev_frame_linked_algs_enqueue_internal (vm, ring_elt->f, op_type);
564   else
565     cryptodev_aead_enqueue_internal (vm, ring_elt->f, op_type,
566                                      linked_or_aad_len);
567 }
568
569 static_always_inline vnet_crypto_async_frame_t *
570 cryptodev_frame_dequeue (vlib_main_t *vm, u32 *nb_elts_processed,
571                          u32 *enqueue_thread_idx)
572 {
573   cryptodev_main_t *cmt = &cryptodev_main;
574   vnet_crypto_main_t *cm = &crypto_main;
575   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
576   cryptodev_cache_ring_t *ring = &cet->cache_ring;
577   cryptodev_cache_ring_elt_t *ring_elt = &ring->frames[ring->tail];
578
579   vnet_crypto_async_frame_t *ret_frame = 0;
580   u8 dequeue_more = 1;
581
582   while (cet->inflight > 0 && dequeue_more)
583     {
584       dequeue_more = cryptodev_frame_dequeue_internal (vm, nb_elts_processed,
585                                                        enqueue_thread_idx);
586     }
587
588   if (PREDICT_TRUE (ring->frames[ring->enq_head].f != 0))
589     cryptodev_enqueue_frame (vm, &ring->frames[ring->enq_head]);
590
591   if (PREDICT_TRUE (ring_elt->f != 0))
592     {
593       if (ring_elt->enq_elts_head == ring_elt->deq_elts_tail)
594         {
595           vlib_node_set_interrupt_pending (
596             vlib_get_main_by_index (vm->thread_index), cm->crypto_node_index);
597           ret_frame = cryptodev_cache_ring_pop (ring);
598           return ret_frame;
599         }
600     }
601
602   return ret_frame;
603 }
604 static_always_inline int
605 cryptodev_enqueue_aead_aad_0_enc (vlib_main_t *vm,
606                                   vnet_crypto_async_frame_t *frame)
607 {
608   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT,
609                                        0);
610 }
611 static_always_inline int
612 cryptodev_enqueue_aead_aad_8_enc (vlib_main_t *vm,
613                                   vnet_crypto_async_frame_t *frame)
614 {
615   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT,
616                                        8);
617 }
618 static_always_inline int
619 cryptodev_enqueue_aead_aad_12_enc (vlib_main_t *vm,
620                                    vnet_crypto_async_frame_t *frame)
621 {
622   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT,
623                                        12);
624 }
625
626 static_always_inline int
627 cryptodev_enqueue_aead_aad_0_dec (vlib_main_t *vm,
628                                   vnet_crypto_async_frame_t *frame)
629 {
630   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT,
631                                        0);
632 }
633 static_always_inline int
634 cryptodev_enqueue_aead_aad_8_dec (vlib_main_t *vm,
635                                   vnet_crypto_async_frame_t *frame)
636 {
637   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT,
638                                        8);
639 }
640 static_always_inline int
641 cryptodev_enqueue_aead_aad_12_dec (vlib_main_t *vm,
642                                    vnet_crypto_async_frame_t *frame)
643 {
644   return cryptodev_frame_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT,
645                                        12);
646 }
647
648 static_always_inline int
649 cryptodev_enqueue_linked_alg_enc (vlib_main_t *vm,
650                                   vnet_crypto_async_frame_t *frame)
651 {
652   return cryptodev_frame_linked_algs_enqueue (vm, frame,
653                                               CRYPTODEV_OP_TYPE_ENCRYPT);
654 }
655
656 static_always_inline int
657 cryptodev_enqueue_linked_alg_dec (vlib_main_t *vm,
658                                   vnet_crypto_async_frame_t *frame)
659 {
660   return cryptodev_frame_linked_algs_enqueue (vm, frame,
661                                               CRYPTODEV_OP_TYPE_DECRYPT);
662 }
663
664 clib_error_t *
665 cryptodev_register_cop_hdl (vlib_main_t *vm, u32 eidx)
666 {
667   cryptodev_main_t *cmt = &cryptodev_main;
668   cryptodev_engine_thread_t *cet;
669   struct rte_cryptodev_sym_capability_idx cap_auth_idx;
670   struct rte_cryptodev_sym_capability_idx cap_cipher_idx;
671   struct rte_cryptodev_sym_capability_idx cap_aead_idx;
672   u8 *name;
673   clib_error_t *error = 0;
674   u32 ref_cnt = 0;
675
676   vec_foreach (cet, cmt->per_thread_data)
677     {
678       u32 thread_index = cet - cmt->per_thread_data;
679       u32 numa = vlib_get_main_by_index (thread_index)->numa_node;
680       name = format (0, "vpp_cop_pool_%u_%u", numa, thread_index);
681       cet->cop_pool = rte_mempool_create (
682         (char *) name, CRYPTODEV_NB_CRYPTO_OPS, sizeof (cryptodev_op_t), 0,
683         sizeof (struct rte_crypto_op_pool_private), NULL, NULL, crypto_op_init,
684         NULL, vm->numa_node, 0);
685       vec_free (name);
686       if (!cet->cop_pool)
687         {
688           error = clib_error_return (
689             0, "Failed to create cryptodev op pool %s", name);
690
691           goto error_exit;
692         }
693     }
694
695 #define _(a, b, c, d, e, f, g)                                                \
696   cap_aead_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;                              \
697   cap_aead_idx.algo.aead = RTE_CRYPTO_##b##_##c;                              \
698   if (cryptodev_check_cap_support (&cap_aead_idx, g, e, f))                   \
699     {                                                                         \
700       vnet_crypto_register_enqueue_handler (                                  \
701         vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_ENC,                 \
702         cryptodev_enqueue_aead_aad_##f##_enc);                                \
703       vnet_crypto_register_enqueue_handler (                                  \
704         vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_DEC,                 \
705         cryptodev_enqueue_aead_aad_##f##_dec);                                \
706       ref_cnt++;                                                              \
707     }
708   foreach_vnet_aead_crypto_conversion
709 #undef _
710
711 #define _(a, b, c, d, e)                                                      \
712   cap_auth_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;                              \
713   cap_auth_idx.algo.auth = RTE_CRYPTO_AUTH_##d##_HMAC;                        \
714   cap_cipher_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;                          \
715   cap_cipher_idx.algo.cipher = RTE_CRYPTO_CIPHER_##b;                         \
716   if (cryptodev_check_cap_support (&cap_cipher_idx, c, -1, -1) &&             \
717       cryptodev_check_cap_support (&cap_auth_idx, -1, e, -1))                 \
718     {                                                                         \
719       vnet_crypto_register_enqueue_handler (                                  \
720         vm, eidx, VNET_CRYPTO_OP_##a##_##d##_TAG##e##_ENC,                    \
721         cryptodev_enqueue_linked_alg_enc);                                    \
722       vnet_crypto_register_enqueue_handler (                                  \
723         vm, eidx, VNET_CRYPTO_OP_##a##_##d##_TAG##e##_DEC,                    \
724         cryptodev_enqueue_linked_alg_dec);                                    \
725       ref_cnt++;                                                              \
726     }
727     foreach_cryptodev_link_async_alg
728 #undef _
729
730     if (ref_cnt)
731       vnet_crypto_register_dequeue_handler (vm, eidx, cryptodev_frame_dequeue);
732
733     return 0;
734
735 error_exit:
736   vec_foreach (cet, cmt->per_thread_data)
737     {
738       if (cet->cop_pool)
739         rte_mempool_free (cet->cop_pool);
740     }
741
742   return error;
743 }