19291eb3b597d6bbc45d8e1f542a174b58953a49
[vpp.git] / src / plugins / dpdk / cryptodev / cryptodev_raw_data_path.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_malloc.h>
33 #include <rte_config.h>
34
35 #include "cryptodev.h"
36
37 #if CLIB_DEBUG > 0
38 #define always_inline static inline
39 #else
40 #define always_inline static inline __attribute__ ((__always_inline__))
41 #endif
42
43 static_always_inline u64
44 compute_ofs_linked_alg (vnet_crypto_async_frame_elt_t *fe, i16 *min_ofs,
45                         u32 *max_end)
46 {
47   union rte_crypto_sym_ofs ofs;
48   u32 crypto_end = fe->crypto_start_offset + fe->crypto_total_length;
49   u32 integ_end =
50     fe->integ_start_offset + fe->crypto_total_length + fe->integ_length_adj;
51
52   *min_ofs = clib_min (fe->crypto_start_offset, fe->integ_start_offset);
53   *max_end = clib_max (crypto_end, integ_end);
54
55   ofs.ofs.cipher.head = fe->crypto_start_offset - *min_ofs;
56   ofs.ofs.cipher.tail = *max_end - crypto_end;
57   ofs.ofs.auth.head = fe->integ_start_offset - *min_ofs;
58   ofs.ofs.auth.tail = *max_end - integ_end;
59
60   return ofs.raw;
61 }
62
63 static_always_inline int
64 cryptodev_frame_build_sgl (vlib_main_t *vm, enum rte_iova_mode iova_mode,
65                            struct rte_crypto_vec *data_vec, u16 *n_seg,
66                            vlib_buffer_t *b, u32 size)
67 {
68   struct rte_crypto_vec *vec = data_vec + 1;
69   if (vlib_buffer_chain_linearize (vm, b) > CRYPTODEV_MAX_N_SGL)
70     return -1;
71
72   while ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && size)
73     {
74       u32 len;
75       b = vlib_get_buffer (vm, b->next_buffer);
76       len = clib_min (b->current_length, size);
77       vec->base = (void *) vlib_buffer_get_current (b);
78       if (iova_mode == RTE_IOVA_VA)
79         vec->iova = pointer_to_uword (vec->base);
80       else
81         vec->iova = vlib_buffer_get_current_pa (vm, b);
82       vec->len = len;
83       size -= len;
84       vec++;
85       *n_seg += 1;
86     }
87
88   if (size)
89     return -1;
90
91   return 0;
92 }
93
94 static_always_inline void
95 cryptodev_reset_ctx (cryptodev_engine_thread_t *cet)
96 {
97   union rte_cryptodev_session_ctx sess_ctx;
98
99   ERROR_ASSERT (cet->reset_sess != 0);
100
101   sess_ctx.crypto_sess = cet->reset_sess;
102
103   rte_cryptodev_configure_raw_dp_ctx (cet->cryptodev_id, cet->cryptodev_q,
104                                       cet->ctx, RTE_CRYPTO_OP_WITH_SESSION,
105                                       sess_ctx, 0);
106 }
107
108 static_always_inline int
109 cryptodev_frame_linked_algs_enqueue (vlib_main_t *vm,
110                                      vnet_crypto_async_frame_t *frame,
111                                      cryptodev_op_type_t op_type)
112 {
113   cryptodev_main_t *cmt = &cryptodev_main;
114   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
115   cryptodev_cache_ring_t *ring = &cet->cache_ring;
116   ERROR_ASSERT (frame != 0);
117   ERROR_ASSERT (frame->n_elts > 0);
118   cryptodev_cache_ring_elt_t *ring_elt =
119     cryptodev_cache_ring_push (ring, frame);
120
121   ring_elt->aad_len = 1;
122   ring_elt->op_type = (u8) op_type;
123   return 0;
124 }
125
126 static_always_inline void
127 cryptodev_frame_linked_algs_enqueue_internal (vlib_main_t *vm,
128                                               vnet_crypto_async_frame_t *frame,
129                                               cryptodev_op_type_t op_type)
130 {
131   cryptodev_main_t *cmt = &cryptodev_main;
132   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
133   vnet_crypto_async_frame_elt_t *fe;
134   vlib_buffer_t **b;
135   struct rte_crypto_vec vec[CRYPTODEV_MAX_N_SGL];
136   struct rte_crypto_va_iova_ptr iv_vec, digest_vec;
137   cryptodev_cache_ring_t *ring = &cet->cache_ring;
138   u16 *const enq = &ring->enq_head;
139   u32 n_elts;
140   u32 last_key_index = ~0;
141   i16 min_ofs;
142   u32 max_end;
143   u32 max_to_enq = clib_min (CRYPTODE_ENQ_MAX,
144                              frame->n_elts - ring->frames[*enq].enq_elts_head);
145   u8 is_update = 0;
146   int status;
147
148   if (cet->inflight + max_to_enq > CRYPTODEV_MAX_INFLIGHT)
149     return;
150
151   n_elts = max_to_enq;
152
153   vlib_get_buffers (vm, frame->buffer_indices, cet->b, frame->n_elts);
154
155   b = cet->b + ring->frames[*enq].enq_elts_head;
156   fe = frame->elts + ring->frames[*enq].enq_elts_head;
157
158   while (n_elts)
159     {
160       union rte_crypto_sym_ofs cofs;
161       u16 n_seg = 1;
162
163       if (n_elts > 2)
164         {
165           clib_prefetch_load (&fe[1]);
166           clib_prefetch_load (&fe[2]);
167           vlib_prefetch_buffer_header (b[1], LOAD);
168           vlib_prefetch_buffer_header (b[2], LOAD);
169         }
170
171       if (PREDICT_FALSE (last_key_index != fe->key_index))
172         {
173           cryptodev_key_t *key = vec_elt_at_index (cmt->keys, fe->key_index);
174           union rte_cryptodev_session_ctx sess_ctx;
175
176           if (PREDICT_FALSE (key->keys[vm->numa_node][op_type] == 0))
177             {
178               status = cryptodev_session_create (vm, fe->key_index, 0);
179               if (PREDICT_FALSE (status < 0))
180                 goto error_exit;
181             }
182
183           /* Borrow a created session to reset session ctx, based on a valid
184            * assumption that the session reset won't happen until first valid
185            * packet is processed */
186           if (PREDICT_FALSE (cet->reset_sess == 0))
187             cet->reset_sess = key->keys[vm->numa_node][op_type];
188
189           sess_ctx.crypto_sess = key->keys[vm->numa_node][op_type];
190
191           status = rte_cryptodev_configure_raw_dp_ctx (
192             cet->cryptodev_id, cet->cryptodev_q, cet->ctx,
193             RTE_CRYPTO_OP_WITH_SESSION, sess_ctx, is_update);
194           if (PREDICT_FALSE (status < 0))
195             goto error_exit;
196
197           last_key_index = fe->key_index;
198           is_update = 1;
199         }
200
201       cofs.raw = compute_ofs_linked_alg (fe, &min_ofs, &max_end);
202
203       vec->len = max_end - min_ofs;
204       if (cmt->iova_mode == RTE_IOVA_VA)
205         {
206           vec[0].base = (void *) (b[0]->data + min_ofs);
207           vec[0].iova = pointer_to_uword (b[0]->data) + min_ofs;
208           iv_vec.va = (void *) fe->iv;
209           iv_vec.iova = pointer_to_uword (fe->iv);
210           digest_vec.va = (void *) fe->tag;
211           digest_vec.iova = pointer_to_uword (fe->tag);
212         }
213       else
214         {
215           vec[0].base = (void *) (b[0]->data + min_ofs);
216           vec[0].iova = vlib_buffer_get_pa (vm, b[0]) + min_ofs;
217           iv_vec.va = (void *) fe->iv;
218           iv_vec.iova = vlib_physmem_get_pa (vm, fe->iv);
219           digest_vec.va = (void *) fe->tag;
220           digest_vec.iova = vlib_physmem_get_pa (vm, fe->digest);
221         }
222
223       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
224         {
225           vec[0].len = b[0]->current_data + b[0]->current_length - min_ofs;
226           if (cryptodev_frame_build_sgl (vm, cmt->iova_mode, vec, &n_seg, b[0],
227                                          max_end - min_ofs - vec->len) < 0)
228             goto error_exit;
229         }
230
231       status = rte_cryptodev_raw_enqueue (cet->ctx, vec, n_seg, cofs, &iv_vec,
232                                           &digest_vec, 0, (void *) frame);
233       if (PREDICT_FALSE (status < 0))
234         goto error_exit;
235
236       ring->frames[*enq].enq_elts_head += 1;
237       b++;
238       fe++;
239       n_elts--;
240     }
241
242   status = rte_cryptodev_raw_enqueue_done (cet->ctx, max_to_enq);
243   if (PREDICT_FALSE (status < 0))
244     goto error_exit;
245
246   cet->inflight += max_to_enq;
247   cryptodev_cache_ring_update_enq_head (ring, frame);
248   return;
249
250 error_exit:
251   cryptodev_mark_frame_fill_err (frame,
252                                  ring->frames[*enq].frame_elts_errs_mask,
253                                  ring->frames[*enq].enq_elts_head, max_to_enq,
254                                  VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
255   ring->frames[*enq].enq_elts_head += max_to_enq;
256   ring->frames[*enq].deq_elts_tail += max_to_enq;
257   cryptodev_cache_ring_update_enq_head (ring, frame);
258   cryptodev_reset_ctx (cet);
259
260   return;
261 }
262
263 static_always_inline int
264 cryptodev_raw_aead_enqueue (vlib_main_t *vm, vnet_crypto_async_frame_t *frame,
265                             cryptodev_op_type_t op_type, u8 aad_len)
266 {
267   cryptodev_main_t *cmt = &cryptodev_main;
268   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
269   cryptodev_cache_ring_t *ring = &cet->cache_ring;
270   ERROR_ASSERT (frame != 0);
271   ERROR_ASSERT (frame->n_elts > 0);
272   cryptodev_cache_ring_elt_t *ring_elt =
273     cryptodev_cache_ring_push (ring, frame);
274
275   ring_elt->aad_len = aad_len;
276   ring_elt->op_type = (u8) op_type;
277   return 0;
278 }
279
280 static_always_inline void
281 cryptodev_raw_aead_enqueue_internal (vlib_main_t *vm,
282                                      vnet_crypto_async_frame_t *frame,
283                                      cryptodev_op_type_t op_type, u8 aad_len)
284 {
285   cryptodev_main_t *cmt = &cryptodev_main;
286   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
287   cryptodev_cache_ring_t *ring = &cet->cache_ring;
288   vnet_crypto_async_frame_elt_t *fe;
289   vlib_buffer_t **b;
290   u32 n_elts;
291   union rte_crypto_sym_ofs cofs;
292   struct rte_crypto_vec vec[CRYPTODEV_MAX_N_SGL];
293   struct rte_crypto_va_iova_ptr iv_vec, digest_vec, aad_vec;
294   u32 last_key_index = ~0;
295   u16 *const enq = &ring->enq_head;
296   u16 left_to_enq = frame->n_elts - ring->frames[*enq].enq_elts_head;
297   u16 max_to_enq = clib_min (CRYPTODE_ENQ_MAX, left_to_enq);
298   u8 is_update = 0;
299   int status;
300
301   if (cet->inflight + max_to_enq > CRYPTODEV_MAX_INFLIGHT)
302     {
303       return;
304     }
305
306   n_elts = max_to_enq;
307
308   vlib_get_buffers (vm, frame->buffer_indices, cet->b, frame->n_elts);
309
310   fe = frame->elts + ring->frames[*enq].enq_elts_head;
311   b = cet->b + ring->frames[*enq].enq_elts_head;
312   cofs.raw = 0;
313
314   while (n_elts)
315     {
316       u32 aad_offset = ((cet->aad_index++) & CRYPTODEV_AAD_MASK) << 4;
317       u16 n_seg = 1;
318
319       if (n_elts > 1)
320         {
321           clib_prefetch_load (&fe[1]);
322           vlib_prefetch_buffer_header (b[1], LOAD);
323         }
324
325       if (PREDICT_FALSE (last_key_index != fe->key_index))
326         {
327           cryptodev_key_t *key = vec_elt_at_index (cmt->keys, fe->key_index);
328           union rte_cryptodev_session_ctx sess_ctx;
329
330           if (PREDICT_FALSE (key->keys[vm->numa_node][op_type] == 0))
331             {
332               status = cryptodev_session_create (vm, fe->key_index, aad_len);
333               if (PREDICT_FALSE (status < 0))
334                 goto error_exit;
335             }
336
337           if (PREDICT_FALSE (
338 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
339                 rte_cryptodev_sym_session_opaque_data_get (
340                   key->keys[vm->numa_node][op_type]) != (u64) aad_len
341 #else
342                 (u8) key->keys[vm->numa_node][op_type]->opaque_data != aad_len
343 #endif
344                 ))
345             {
346               cryptodev_sess_handler (vm, VNET_CRYPTO_KEY_OP_DEL,
347                                       fe->key_index, aad_len);
348               status = cryptodev_session_create (vm, fe->key_index, aad_len);
349               if (PREDICT_FALSE (status < 0))
350                 goto error_exit;
351             }
352
353           /* Borrow a created session to reset session ctx, based on a valid
354            * assumption that the session reset won't happen until first valid
355            * packet is processed */
356
357           if (PREDICT_FALSE (cet->reset_sess == 0))
358             cet->reset_sess = key->keys[vm->numa_node][op_type];
359
360           sess_ctx.crypto_sess = key->keys[vm->numa_node][op_type];
361
362           status = rte_cryptodev_configure_raw_dp_ctx (
363             cet->cryptodev_id, cet->cryptodev_q, cet->ctx,
364             RTE_CRYPTO_OP_WITH_SESSION, sess_ctx, is_update);
365           if (PREDICT_FALSE (status < 0))
366             goto error_exit;
367
368           last_key_index = fe->key_index;
369           is_update = 1;
370         }
371
372       if (cmt->iova_mode == RTE_IOVA_VA)
373         {
374           vec[0].base = (void *) (b[0]->data + fe->crypto_start_offset);
375           vec[0].iova = pointer_to_uword (vec[0].base);
376           vec[0].len = fe->crypto_total_length;
377           iv_vec.va = (void *) fe->iv;
378           iv_vec.iova = pointer_to_uword (fe->iv);
379           digest_vec.va = (void *) fe->tag;
380           digest_vec.iova = pointer_to_uword (fe->tag);
381           aad_vec.va = (void *) (cet->aad_buf + aad_offset);
382           aad_vec.iova = cet->aad_phy_addr + aad_offset;
383         }
384       else
385         {
386           vec[0].base = (void *) (b[0]->data + fe->crypto_start_offset);
387           vec[0].iova =
388             vlib_buffer_get_pa (vm, b[0]) + fe->crypto_start_offset;
389           vec[0].len = fe->crypto_total_length;
390           iv_vec.va = (void *) fe->iv;
391           iv_vec.iova = vlib_physmem_get_pa (vm, fe->iv);
392           aad_vec.va = (void *) (cet->aad_buf + aad_offset);
393           aad_vec.iova = cet->aad_phy_addr + aad_offset;
394           digest_vec.va = (void *) fe->tag;
395           digest_vec.iova = vlib_physmem_get_pa (vm, fe->tag);
396         }
397
398       if (aad_len == 8)
399         *(u64 *) (cet->aad_buf + aad_offset) = *(u64 *) fe->aad;
400       else if (aad_len != 0)
401         {
402           /* aad_len == 12 */
403           *(u64 *) (cet->aad_buf + aad_offset) = *(u64 *) fe->aad;
404           *(u32 *) (cet->aad_buf + aad_offset + 8) = *(u32 *) (fe->aad + 8);
405         }
406
407       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
408         {
409           vec[0].len = b[0]->current_data + b[0]->current_length -
410                        fe->crypto_start_offset;
411           status =
412             cryptodev_frame_build_sgl (vm, cmt->iova_mode, vec, &n_seg, b[0],
413                                        fe->crypto_total_length - vec[0].len);
414           if (status < 0)
415             goto error_exit;
416         }
417
418       status =
419         rte_cryptodev_raw_enqueue (cet->ctx, vec, n_seg, cofs, &iv_vec,
420                                    &digest_vec, &aad_vec, (void *) frame);
421       if (PREDICT_FALSE (status < 0))
422         goto error_exit;
423
424       ring->frames[*enq].enq_elts_head += 1;
425       fe++;
426       b++;
427       n_elts--;
428     }
429
430   status = rte_cryptodev_raw_enqueue_done (cet->ctx, max_to_enq);
431   if (PREDICT_FALSE (status < 0))
432     goto error_exit;
433
434   cet->inflight += max_to_enq;
435   cryptodev_cache_ring_update_enq_head (ring, frame);
436   return;
437
438 error_exit:
439   cryptodev_mark_frame_fill_err (frame,
440                                  ring->frames[*enq].frame_elts_errs_mask,
441                                  ring->frames[*enq].enq_elts_head, max_to_enq,
442                                  VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
443   ring->frames[*enq].enq_elts_head += max_to_enq;
444   ring->frames[*enq].deq_elts_tail += max_to_enq;
445   cryptodev_cache_ring_update_enq_head (ring, frame);
446   cryptodev_reset_ctx (cet);
447   return;
448 }
449
450 static_always_inline void
451 cryptodev_post_dequeue (void *frame, u32 index, u8 is_op_success)
452 {
453   vnet_crypto_async_frame_t *f = (vnet_crypto_async_frame_t *) frame;
454
455   f->elts[index].status = is_op_success ? VNET_CRYPTO_OP_STATUS_COMPLETED :
456                                           VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
457 }
458
459 static_always_inline u8
460 cryptodev_raw_dequeue_internal (vlib_main_t *vm, u32 *nb_elts_processed,
461                                 u32 *enqueue_thread_idx)
462 {
463   cryptodev_main_t *cmt = &cryptodev_main;
464   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
465   vnet_crypto_async_frame_t *frame;
466   cryptodev_cache_ring_t *ring = &cet->cache_ring;
467   u16 *const deq = &ring->deq_tail;
468   u32 n_success;
469   u16 n_deq, indice, i, left_to_deq;
470   u16 max_to_deq = 0;
471   u16 inflight = cet->inflight;
472   u8 dequeue_more = 0;
473   int dequeue_status;
474
475   indice = *deq;
476
477   for (i = 0; i < VNET_CRYPTO_FRAME_POOL_SIZE; i++)
478     {
479       if (PREDICT_TRUE (
480             CRYPTODEV_CACHE_RING_GET_FRAME_ELTS_INFLIGHT (ring, indice) > 0))
481         break;
482       indice += 1;
483       indice &= CRYPTODEV_CACHE_QUEUE_MASK;
484     }
485
486   ERROR_ASSERT (i != VNET_CRYPTO_FRAME_POOL_SIZE);
487
488   *deq = indice;
489
490   left_to_deq = ring->frames[*deq].n_elts - ring->frames[*deq].deq_elts_tail;
491   max_to_deq = clib_min (left_to_deq, CRYPTODE_DEQ_MAX);
492
493   /* you can use deq field to track frame that is currently dequeued */
494   /* based on that you can specify the amount of elements to deq for the frame
495    */
496
497   n_deq = rte_cryptodev_raw_dequeue_burst (
498     cet->ctx, NULL, max_to_deq, cryptodev_post_dequeue, (void **) &frame, 0,
499     &n_success, &dequeue_status);
500
501   if (n_deq == 0)
502     return dequeue_more;
503
504   inflight -= n_deq;
505   if (PREDICT_FALSE (n_success < n_deq))
506     {
507       u16 idx = ring->frames[*deq].deq_elts_tail;
508
509       for (i = 0; i < n_deq; i++)
510         {
511           if (frame->elts[idx + i].status != VNET_CRYPTO_OP_STATUS_COMPLETED)
512             ring->frames[*deq].frame_elts_errs_mask |= 1 << (idx + i);
513         }
514     }
515   ring->frames[*deq].deq_elts_tail += n_deq;
516
517   if (cryptodev_cache_ring_update_deq_tail (ring, deq))
518     {
519       *nb_elts_processed = frame->n_elts;
520       *enqueue_thread_idx = frame->enqueue_thread_index;
521       dequeue_more = max_to_deq < CRYPTODE_DEQ_MAX;
522     }
523
524   int res =
525     rte_cryptodev_raw_dequeue_done (cet->ctx, cet->inflight - inflight);
526   ERROR_ASSERT (res == 0);
527   cet->inflight = inflight;
528   return dequeue_more;
529 }
530
531 static_always_inline void
532 cryptodev_enqueue_frame_to_qat (vlib_main_t *vm,
533                                 cryptodev_cache_ring_elt_t *ring_elt)
534 {
535   cryptodev_op_type_t op_type = (cryptodev_op_type_t) ring_elt->op_type;
536   u8 linked_or_aad_len = ring_elt->aad_len;
537
538   if (linked_or_aad_len == 1)
539     cryptodev_frame_linked_algs_enqueue_internal (vm, ring_elt->f, op_type);
540   else
541     cryptodev_raw_aead_enqueue_internal (vm, ring_elt->f, op_type,
542                                          linked_or_aad_len);
543 }
544
545 static_always_inline vnet_crypto_async_frame_t *
546 cryptodev_raw_dequeue (vlib_main_t *vm, u32 *nb_elts_processed,
547                        u32 *enqueue_thread_idx)
548 {
549   cryptodev_main_t *cmt = &cryptodev_main;
550   vnet_crypto_main_t *cm = &crypto_main;
551   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
552   cryptodev_cache_ring_t *ring = &cet->cache_ring;
553   cryptodev_cache_ring_elt_t *ring_elt = &ring->frames[ring->tail];
554   vnet_crypto_async_frame_t *ret_frame = 0;
555   u8 dequeue_more = 1;
556
557   while (cet->inflight > 0 && dequeue_more)
558     {
559       dequeue_more = cryptodev_raw_dequeue_internal (vm, nb_elts_processed,
560                                                      enqueue_thread_idx);
561     }
562
563   if (PREDICT_TRUE (ring->frames[ring->enq_head].f != 0))
564     cryptodev_enqueue_frame_to_qat (vm, &ring->frames[ring->enq_head]);
565
566   if (PREDICT_TRUE (ring_elt->f != 0))
567     {
568       if (ring_elt->enq_elts_head == ring_elt->deq_elts_tail)
569         {
570           vlib_node_set_interrupt_pending (
571             vlib_get_main_by_index (vm->thread_index), cm->crypto_node_index);
572           ret_frame = cryptodev_cache_ring_pop (ring);
573
574           return ret_frame;
575         }
576     }
577
578   return ret_frame;
579 }
580
581 static_always_inline int
582 cryptodev_raw_enq_aead_aad_0_enc (vlib_main_t *vm,
583                                   vnet_crypto_async_frame_t *frame)
584 {
585   return cryptodev_raw_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT, 0);
586 }
587
588 static_always_inline int
589 cryptodev_raw_enq_aead_aad_8_enc (vlib_main_t *vm,
590                                   vnet_crypto_async_frame_t *frame)
591 {
592   return cryptodev_raw_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT, 8);
593 }
594 static_always_inline int
595 cryptodev_raw_enq_aead_aad_12_enc (vlib_main_t *vm,
596                                    vnet_crypto_async_frame_t *frame)
597 {
598   return cryptodev_raw_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_ENCRYPT, 12);
599 }
600
601 static_always_inline int
602 cryptodev_raw_enq_aead_aad_0_dec (vlib_main_t *vm,
603                                   vnet_crypto_async_frame_t *frame)
604 {
605   return cryptodev_raw_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT, 0);
606 }
607
608 static_always_inline int
609 cryptodev_raw_enq_aead_aad_8_dec (vlib_main_t *vm,
610                                   vnet_crypto_async_frame_t *frame)
611 {
612   return cryptodev_raw_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT, 8);
613 }
614 static_always_inline int
615 cryptodev_raw_enq_aead_aad_12_dec (vlib_main_t *vm,
616                                    vnet_crypto_async_frame_t *frame)
617 {
618   return cryptodev_raw_aead_enqueue (vm, frame, CRYPTODEV_OP_TYPE_DECRYPT, 12);
619 }
620
621 static_always_inline int
622 cryptodev_raw_enq_linked_alg_enc (vlib_main_t *vm,
623                                   vnet_crypto_async_frame_t *frame)
624 {
625   return cryptodev_frame_linked_algs_enqueue (vm, frame,
626                                               CRYPTODEV_OP_TYPE_ENCRYPT);
627 }
628
629 static_always_inline int
630 cryptodev_raw_enq_linked_alg_dec (vlib_main_t *vm,
631                                   vnet_crypto_async_frame_t *frame)
632 {
633   return cryptodev_frame_linked_algs_enqueue (vm, frame,
634                                               CRYPTODEV_OP_TYPE_DECRYPT);
635 }
636
637 clib_error_t *
638 cryptodev_register_raw_hdl (vlib_main_t *vm, u32 eidx)
639 {
640   cryptodev_main_t *cmt = &cryptodev_main;
641   cryptodev_engine_thread_t *cet;
642   cryptodev_inst_t *cinst;
643   struct rte_cryptodev_info info;
644   struct rte_cryptodev_sym_capability_idx cap_auth_idx;
645   struct rte_cryptodev_sym_capability_idx cap_cipher_idx;
646   struct rte_cryptodev_sym_capability_idx cap_aead_idx;
647   u32 support_raw_api = 1, max_ctx_size = 0;
648   clib_error_t *error = 0;
649   u8 ref_cnt = 0;
650
651   vec_foreach (cinst, cmt->cryptodev_inst)
652     {
653       u32 ctx_size;
654       rte_cryptodev_info_get (cinst->dev_id, &info);
655       if (!(info.feature_flags & RTE_CRYPTODEV_FF_SYM_RAW_DP))
656         {
657           support_raw_api = 0;
658           break;
659         }
660
661       ctx_size = rte_cryptodev_get_raw_dp_ctx_size (cinst->dev_id);
662       max_ctx_size = clib_max (ctx_size, max_ctx_size);
663     }
664
665   if (!support_raw_api)
666     return cryptodev_register_cop_hdl (vm, eidx);
667
668   vec_foreach (cet, cmt->per_thread_data)
669     {
670       u32 thread_id = cet - cmt->per_thread_data;
671       u32 numa = vlib_get_main_by_index (thread_id)->numa_node;
672       u8 *name = format (0, "cache_cache_ring_%u_%u", numa, thread_id);
673
674       cet->aad_buf = rte_zmalloc_socket (
675         0, CRYPTODEV_NB_CRYPTO_OPS * CRYPTODEV_MAX_AAD_SIZE,
676         CLIB_CACHE_LINE_BYTES, numa);
677       if (cet->aad_buf == 0)
678         {
679           error = clib_error_return (0, "Failed to alloc aad buf");
680           goto err_handling;
681         }
682       cet->aad_phy_addr = rte_malloc_virt2iova (cet->aad_buf);
683
684       cet->ctx =
685         rte_zmalloc_socket (0, max_ctx_size, CLIB_CACHE_LINE_BYTES, numa);
686       if (!cet->ctx)
687         {
688           error = clib_error_return (0, "Failed to alloc raw dp ctx");
689           goto err_handling;
690         }
691       vec_free (name);
692     }
693
694 #define _(a, b, c, d, e, f, g)                                                \
695   cap_aead_idx.type = RTE_CRYPTO_SYM_XFORM_AEAD;                              \
696   cap_aead_idx.algo.aead = RTE_CRYPTO_##b##_##c;                              \
697   if (cryptodev_check_cap_support (&cap_aead_idx, g, e, f))                   \
698     {                                                                         \
699       vnet_crypto_register_enqueue_handler (                                  \
700         vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_ENC,                 \
701         cryptodev_raw_enq_aead_aad_##f##_enc);                                \
702       vnet_crypto_register_enqueue_handler (                                  \
703         vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_DEC,                 \
704         cryptodev_raw_enq_aead_aad_##f##_dec);                                \
705       ref_cnt++;                                                              \
706     }
707   foreach_vnet_aead_crypto_conversion
708 #undef _
709
710 #define _(a, b, c, d, e)                                                      \
711   cap_auth_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH;                              \
712   cap_auth_idx.algo.auth = RTE_CRYPTO_AUTH_##d##_HMAC;                        \
713   cap_cipher_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER;                          \
714   cap_cipher_idx.algo.cipher = RTE_CRYPTO_CIPHER_##b;                         \
715   if (cryptodev_check_cap_support (&cap_cipher_idx, c, -1, -1) &&             \
716       cryptodev_check_cap_support (&cap_auth_idx, -1, e, -1))                 \
717     {                                                                         \
718       vnet_crypto_register_enqueue_handler (                                  \
719         vm, eidx, VNET_CRYPTO_OP_##a##_##d##_TAG##e##_ENC,                    \
720         cryptodev_raw_enq_linked_alg_enc);                                    \
721       vnet_crypto_register_enqueue_handler (                                  \
722         vm, eidx, VNET_CRYPTO_OP_##a##_##d##_TAG##e##_DEC,                    \
723         cryptodev_raw_enq_linked_alg_dec);                                    \
724       ref_cnt++;                                                              \
725     }
726     foreach_cryptodev_link_async_alg
727 #undef _
728
729     if (ref_cnt)
730       vnet_crypto_register_dequeue_handler (vm, eidx, cryptodev_raw_dequeue);
731
732   cmt->is_raw_api = 1;
733
734   return 0;
735
736 err_handling:
737   return error;
738 }