dpdk: fix packet offset for GCM crypto ops
[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_src->data_off = VLIB_BUFFER_PRE_DATA_SIZE;
688       sop->m_dst = 0;
689       /* mbuf prepend happens in the tx, but vlib_buffer happens in the nodes,
690        * so we have to manually adjust mbuf data_off here so cryptodev can
691        * correctly compute the data pointer. The prepend here will be later
692        * rewritten by tx. */
693       if (PREDICT_FALSE (fe->crypto_start_offset < 0))
694         {
695           rte_pktmbuf_prepend (sop->m_src, -fe->crypto_start_offset);
696           crypto_offset = 0;
697         }
698
699       sop->session = sess;
700       sop->aead.aad.data = cop[0]->aad;
701       sop->aead.aad.phys_addr = cop[0]->op.phys_addr + CRYPTODEV_AAD_OFFSET;
702       sop->aead.data.length = fe->crypto_total_length;
703       sop->aead.data.offset = crypto_offset;
704       sop->aead.digest.data = fe->tag;
705       sop->aead.digest.phys_addr = cryptodev_get_iova (pm, cmt->iova_mode,
706                                                        fe->tag);
707       if (PREDICT_FALSE (fe->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS))
708         cryptodev_validate_mbuf_chain (vm, sop->m_src, b);
709       else
710         /* for input nodes that are not dpdk-input, it is possible the mbuf
711          * was updated before as one of the chained mbufs. Setting nb_segs
712          * to 1 here to prevent the cryptodev PMD to access potentially
713          * invalid m_src->next pointers.
714          */
715         sop->m_src->nb_segs = 1;
716       clib_memcpy_fast (cop[0]->iv, fe->iv, 12);
717       clib_memcpy_fast (cop[0]->aad, fe->aad, aad_len);
718       cop++;
719       bi++;
720       fe++;
721       n_elts--;
722     }
723
724   n_enqueue = rte_cryptodev_enqueue_burst (cet->cryptodev_id,
725                                            cet->cryptodev_q,
726                                            (struct rte_crypto_op **)
727                                            cet->cops, frame->n_elts);
728   ASSERT (n_enqueue == frame->n_elts);
729   cet->inflight += n_enqueue;
730
731   return 0;
732 }
733
734 static_always_inline cryptodev_op_t *
735 cryptodev_get_ring_head (struct rte_ring * ring)
736 {
737   cryptodev_op_t **r = (void *) &ring[1];
738   return r[ring->cons.head & ring->mask];
739 }
740
741 static_always_inline vnet_crypto_async_frame_t *
742 cryptodev_frame_dequeue (vlib_main_t * vm, u32 * nb_elts_processed,
743                          u32 * enqueue_thread_idx)
744 {
745   cryptodev_main_t *cmt = &cryptodev_main;
746   cryptodev_numa_data_t *numa = cmt->per_numa_data + vm->numa_node;
747   cryptodev_engine_thread_t *cet = cmt->per_thread_data + vm->thread_index;
748   cryptodev_op_t *cop0, **cop = cet->cops;
749   vnet_crypto_async_frame_elt_t *fe;
750   vnet_crypto_async_frame_t *frame;
751   u32 n_elts, n_completed_ops = rte_ring_count (cet->ring);
752   u32 ss0 = 0, ss1 = 0, ss2 = 0, ss3 = 0;       /* sum of status */
753
754   if (cet->inflight)
755     {
756       n_elts = clib_min (CRYPTODEV_NB_CRYPTO_OPS - n_completed_ops,
757                          VNET_CRYPTO_FRAME_SIZE);
758       n_elts = rte_cryptodev_dequeue_burst
759         (cet->cryptodev_id, cet->cryptodev_q,
760          (struct rte_crypto_op **) cet->cops, n_elts);
761       cet->inflight -= n_elts;
762       n_completed_ops += n_elts;
763
764       rte_ring_sp_enqueue_burst (cet->ring, (void *) cet->cops, n_elts, NULL);
765     }
766
767   if (PREDICT_FALSE (n_completed_ops == 0))
768     return 0;
769
770   cop0 = cryptodev_get_ring_head (cet->ring);
771   /* not a single frame is finished */
772   if (PREDICT_FALSE (cop0->n_elts > rte_ring_count (cet->ring)))
773     return 0;
774
775   frame = cop0->frame;
776   n_elts = cop0->n_elts;
777   n_elts = rte_ring_sc_dequeue_bulk (cet->ring, (void **) cet->cops,
778                                      n_elts, 0);
779   fe = frame->elts;
780
781   while (n_elts > 4)
782     {
783       ss0 |= fe[0].status = cryptodev_status_conversion[cop[0]->op.status];
784       ss1 |= fe[1].status = cryptodev_status_conversion[cop[1]->op.status];
785       ss2 |= fe[2].status = cryptodev_status_conversion[cop[2]->op.status];
786       ss3 |= fe[3].status = cryptodev_status_conversion[cop[3]->op.status];
787
788       cop += 4;
789       fe += 4;
790       n_elts -= 4;
791     }
792
793   while (n_elts)
794     {
795       ss0 |= fe[0].status = cryptodev_status_conversion[cop[0]->op.status];
796       fe++;
797       cop++;
798       n_elts--;
799     }
800
801   frame->state = (ss0 | ss1 | ss2 | ss3) == VNET_CRYPTO_OP_STATUS_COMPLETED ?
802     VNET_CRYPTO_FRAME_STATE_SUCCESS : VNET_CRYPTO_FRAME_STATE_ELT_ERROR;
803
804   rte_mempool_put_bulk (numa->cop_pool, (void **) cet->cops, frame->n_elts);
805   *nb_elts_processed = frame->n_elts;
806   *enqueue_thread_idx = frame->enqueue_thread_index;
807   return frame;
808 }
809
810 /* *INDENT-OFF* */
811 static_always_inline int
812 cryptodev_enqueue_gcm_aad_8_enc (vlib_main_t * vm,
813                                  vnet_crypto_async_frame_t * frame)
814 {
815   return cryptodev_frame_gcm_enqueue (vm, frame,
816                                       CRYPTODEV_OP_TYPE_ENCRYPT, 8);
817 }
818 static_always_inline int
819 cryptodev_enqueue_gcm_aad_12_enc (vlib_main_t * vm,
820                                  vnet_crypto_async_frame_t * frame)
821 {
822   return cryptodev_frame_gcm_enqueue (vm, frame,
823                                       CRYPTODEV_OP_TYPE_ENCRYPT, 12);
824 }
825
826 static_always_inline int
827 cryptodev_enqueue_gcm_aad_8_dec (vlib_main_t * vm,
828                                  vnet_crypto_async_frame_t * frame)
829 {
830   return cryptodev_frame_gcm_enqueue (vm, frame,
831                                       CRYPTODEV_OP_TYPE_DECRYPT, 8);
832 }
833 static_always_inline int
834 cryptodev_enqueue_gcm_aad_12_dec (vlib_main_t * vm,
835                                  vnet_crypto_async_frame_t * frame)
836 {
837   return cryptodev_frame_gcm_enqueue (vm, frame,
838                                       CRYPTODEV_OP_TYPE_DECRYPT, 12);
839 }
840
841 static_always_inline int
842 cryptodev_enqueue_linked_alg_enc (vlib_main_t * vm,
843                                   vnet_crypto_async_frame_t * frame)
844 {
845   return cryptodev_frame_linked_algs_enqueue (vm, frame,
846                                               CRYPTODEV_OP_TYPE_ENCRYPT);
847 }
848
849 static_always_inline int
850 cryptodev_enqueue_linked_alg_dec (vlib_main_t * vm,
851                                   vnet_crypto_async_frame_t * frame)
852 {
853   return cryptodev_frame_linked_algs_enqueue (vm, frame,
854                                               CRYPTODEV_OP_TYPE_DECRYPT);
855 }
856
857 typedef enum
858 {
859   CRYPTODEV_RESOURCE_ASSIGN_AUTO = 0,
860   CRYPTODEV_RESOURCE_ASSIGN_UPDATE,
861 } cryptodev_resource_assign_op_t;
862
863 /**
864  *  assign a cryptodev resource to a worker.
865  *  @param cet: the worker thread data
866  *  @param cryptodev_inst_index: if op is "ASSIGN_AUTO" this param is ignored.
867  *  @param op: the assignment method.
868  *  @return: 0 if successfully, negative number otherwise.
869  **/
870 static_always_inline int
871 cryptodev_assign_resource (cryptodev_engine_thread_t * cet,
872                            u32 cryptodev_inst_index,
873                            cryptodev_resource_assign_op_t op)
874 {
875   cryptodev_main_t *cmt = &cryptodev_main;
876   cryptodev_inst_t *cinst = 0;
877   uword idx;
878
879   /* assign resource is only allowed when no inflight op is in the queue */
880   if (cet->inflight)
881     return -EBUSY;
882
883   switch (op)
884     {
885     case CRYPTODEV_RESOURCE_ASSIGN_AUTO:
886       if (clib_bitmap_count_set_bits (cmt->active_cdev_inst_mask) >=
887           vec_len (cmt->cryptodev_inst))
888         return -1;
889
890       clib_spinlock_lock (&cmt->tlock);
891       idx = clib_bitmap_first_clear (cmt->active_cdev_inst_mask);
892       clib_bitmap_set (cmt->active_cdev_inst_mask, idx, 1);
893       cinst = vec_elt_at_index (cmt->cryptodev_inst, idx);
894       cet->cryptodev_id = cinst->dev_id;
895       cet->cryptodev_q = cinst->q_id;
896       clib_spinlock_unlock (&cmt->tlock);
897       break;
898     case CRYPTODEV_RESOURCE_ASSIGN_UPDATE:
899       /* assigning a used cryptodev resource is not allowed */
900       if (clib_bitmap_get (cmt->active_cdev_inst_mask, cryptodev_inst_index)
901           == 1)
902         return -EBUSY;
903       vec_foreach_index (idx, cmt->cryptodev_inst)
904       {
905         cinst = cmt->cryptodev_inst + idx;
906         if (cinst->dev_id == cet->cryptodev_id &&
907             cinst->q_id == cet->cryptodev_q)
908           break;
909       }
910       /* invalid existing worker resource assignment */
911       if (idx == vec_len (cmt->cryptodev_inst))
912         return -EINVAL;
913       clib_spinlock_lock (&cmt->tlock);
914       clib_bitmap_set_no_check (cmt->active_cdev_inst_mask, idx, 0);
915       clib_bitmap_set_no_check (cmt->active_cdev_inst_mask,
916                                 cryptodev_inst_index, 1);
917       cinst = cmt->cryptodev_inst + cryptodev_inst_index;
918       cet->cryptodev_id = cinst->dev_id;
919       cet->cryptodev_q = cinst->q_id;
920       clib_spinlock_unlock (&cmt->tlock);
921       break;
922     default:
923       return -EINVAL;
924     }
925   return 0;
926 }
927
928 static u8 *
929 format_cryptodev_inst (u8 * s, va_list * args)
930 {
931   cryptodev_main_t *cmt = &cryptodev_main;
932   u32 inst = va_arg (*args, u32);
933   cryptodev_inst_t *cit = cmt->cryptodev_inst + inst;
934   u32 thread_index = 0;
935   struct rte_cryptodev_info info;
936
937   rte_cryptodev_info_get (cit->dev_id, &info);
938   s = format (s, "%-25s%-10u", info.device->name, cit->q_id);
939
940   vec_foreach_index (thread_index, cmt->per_thread_data)
941   {
942     cryptodev_engine_thread_t *cet = cmt->per_thread_data + thread_index;
943     if (vlib_num_workers () > 0 && thread_index == 0)
944       continue;
945
946     if (cet->cryptodev_id == cit->dev_id && cet->cryptodev_q == cit->q_id)
947       {
948         s = format (s, "%u (%v)\n", thread_index,
949                     vlib_worker_threads[thread_index].name);
950         break;
951       }
952   }
953
954   if (thread_index == vec_len (cmt->per_thread_data))
955     s = format (s, "%s\n", "free");
956
957   return s;
958 }
959
960 static clib_error_t *
961 cryptodev_show_assignment_fn (vlib_main_t * vm, unformat_input_t * input,
962                               vlib_cli_command_t * cmd)
963 {
964   cryptodev_main_t *cmt = &cryptodev_main;
965   u32 inst;
966
967   vlib_cli_output (vm, "%-5s%-25s%-10s%s\n", "No.", "Name", "Queue-id",
968                    "Assigned-to");
969   if (vec_len (cmt->cryptodev_inst) == 0)
970     {
971       vlib_cli_output (vm, "(nil)\n");
972       return 0;
973     }
974
975   vec_foreach_index (inst, cmt->cryptodev_inst)
976     vlib_cli_output (vm, "%-5u%U", inst, format_cryptodev_inst, inst);
977
978   return 0;
979 }
980
981 VLIB_CLI_COMMAND (show_cryptodev_assignment, static) = {
982     .path = "show cryptodev assignment",
983     .short_help = "show cryptodev assignment",
984     .function = cryptodev_show_assignment_fn,
985 };
986
987 static clib_error_t *
988 cryptodev_set_assignment_fn (vlib_main_t * vm, unformat_input_t * input,
989                              vlib_cli_command_t * cmd)
990 {
991   cryptodev_main_t *cmt = &cryptodev_main;
992   cryptodev_engine_thread_t *cet;
993   unformat_input_t _line_input, *line_input = &_line_input;
994   u32 thread_index, inst_index;
995   u32 thread_present = 0, inst_present = 0;
996   clib_error_t *error = 0;
997   int ret;
998
999   /* Get a line of input. */
1000   if (!unformat_user (input, unformat_line_input, line_input))
1001     return 0;
1002
1003   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1004     {
1005       if (unformat (line_input, "thread %u", &thread_index))
1006         thread_present = 1;
1007       else if (unformat (line_input, "resource %u", &inst_index))
1008         inst_present = 1;
1009       else
1010         {
1011           error = clib_error_return (0, "unknown input `%U'",
1012                                      format_unformat_error, line_input);
1013           return error;
1014         }
1015     }
1016
1017   if (!thread_present || !inst_present)
1018     {
1019       error = clib_error_return (0, "mandatory argument(s) missing");
1020       return error;
1021     }
1022
1023   if (thread_index == 0 && vlib_num_workers () > 0)
1024     {
1025       error =
1026         clib_error_return (0, "assign crypto resource for master thread");
1027       return error;
1028     }
1029
1030   if (thread_index > vec_len (cmt->per_thread_data) ||
1031       inst_index > vec_len (cmt->cryptodev_inst))
1032     {
1033       error = clib_error_return (0, "wrong thread id or resource id");
1034       return error;
1035     }
1036
1037   cet = cmt->per_thread_data + thread_index;
1038   ret = cryptodev_assign_resource (cet, inst_index,
1039                                    CRYPTODEV_RESOURCE_ASSIGN_UPDATE);
1040   if (ret)
1041     {
1042       error = clib_error_return (0, "cryptodev_assign_resource returned %i",
1043                                  ret);
1044       return error;
1045     }
1046
1047   return 0;
1048 }
1049
1050 VLIB_CLI_COMMAND (set_cryptodev_assignment, static) = {
1051     .path = "set cryptodev assignment",
1052     .short_help = "set cryptodev assignment thread <thread_index> "
1053         "resource <inst_index>",
1054     .function = cryptodev_set_assignment_fn,
1055 };
1056
1057 static int
1058 check_cryptodev_alg_support (u32 dev_id)
1059 {
1060   const struct rte_cryptodev_symmetric_capability *cap;
1061   struct rte_cryptodev_sym_capability_idx cap_idx;
1062
1063 #define _(a, b, c, d, e, f) \
1064   cap_idx.type = RTE_CRYPTO_SYM_XFORM_##b; \
1065   cap_idx.algo.aead = RTE_CRYPTO_##b##_##c; \
1066   cap = rte_cryptodev_sym_capability_get (dev_id, &cap_idx); \
1067   if (!cap) \
1068     return -RTE_CRYPTO_##b##_##c; \
1069   else \
1070     { \
1071       if (cap->aead.digest_size.min > e || cap->aead.digest_size.max < e) \
1072         return -RTE_CRYPTO_##b##_##c; \
1073       if (cap->aead.aad_size.min > f || cap->aead.aad_size.max < f) \
1074         return -RTE_CRYPTO_##b##_##c; \
1075       if (cap->aead.iv_size.min > d || cap->aead.iv_size.max < d) \
1076         return -RTE_CRYPTO_##b##_##c; \
1077     }
1078
1079   foreach_vnet_aead_crypto_conversion
1080 #undef _
1081
1082 #define _(a, b, c, d) \
1083   cap_idx.type = RTE_CRYPTO_SYM_XFORM_CIPHER; \
1084   cap_idx.algo.cipher = RTE_CRYPTO_CIPHER_##b; \
1085   cap = rte_cryptodev_sym_capability_get (dev_id, &cap_idx); \
1086   if (!cap) \
1087     return -RTE_CRYPTO_CIPHER_##b; \
1088   cap_idx.type = RTE_CRYPTO_SYM_XFORM_AUTH; \
1089   cap_idx.algo.auth = RTE_CRYPTO_AUTH_##c##_HMAC; \
1090   cap = rte_cryptodev_sym_capability_get (dev_id, &cap_idx); \
1091   if (!cap) \
1092     return -RTE_CRYPTO_AUTH_##c;
1093
1094   foreach_cryptodev_link_async_alg
1095 #undef _
1096     return 0;
1097 }
1098
1099 static u32
1100 cryptodev_count_queue (u32 numa)
1101 {
1102   struct rte_cryptodev_info info;
1103   u32 n_cryptodev = rte_cryptodev_count ();
1104   u32 i, q_count = 0;
1105
1106   for (i = 0; i < n_cryptodev; i++)
1107     {
1108       rte_cryptodev_info_get (i, &info);
1109
1110       /* only device support symmetric crypto is used */
1111       if (!(info.feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO))
1112         continue;
1113       q_count += info.max_nb_queue_pairs;
1114     }
1115
1116   return q_count;
1117 }
1118
1119 static int
1120 cryptodev_configure (vlib_main_t *vm, uint32_t cryptodev_id)
1121 {
1122   struct rte_cryptodev_info info;
1123   struct rte_cryptodev *cdev;
1124   cryptodev_main_t *cmt = &cryptodev_main;
1125   u32 i;
1126   int ret;
1127
1128   rte_cryptodev_info_get (cryptodev_id, &info);
1129
1130   /* do not configure the device that does not support symmetric crypto */
1131   if (!(info.feature_flags & RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO))
1132     return 0;
1133
1134   ret = check_cryptodev_alg_support (cryptodev_id);
1135   if (ret != 0)
1136     return ret;
1137
1138   cdev = rte_cryptodev_pmd_get_dev (cryptodev_id);
1139   /** If the device is already started, we reuse it, otherwise configure
1140    *  both the device and queue pair.
1141    **/
1142   if (!cdev->data->dev_started)
1143     {
1144       struct rte_cryptodev_config cfg;
1145
1146       cfg.socket_id = info.device->numa_node;
1147       cfg.nb_queue_pairs = info.max_nb_queue_pairs;
1148
1149       rte_cryptodev_configure (cryptodev_id, &cfg);
1150
1151       for (i = 0; i < info.max_nb_queue_pairs; i++)
1152         {
1153           struct rte_cryptodev_qp_conf qp_cfg;
1154
1155           int ret;
1156
1157           qp_cfg.mp_session = 0;
1158           qp_cfg.mp_session_private = 0;
1159           qp_cfg.nb_descriptors = CRYPTODEV_NB_CRYPTO_OPS;
1160
1161           ret = rte_cryptodev_queue_pair_setup (cryptodev_id, i, &qp_cfg,
1162                                                 info.device->numa_node);
1163           if (ret)
1164             break;
1165         }
1166       if (i != info.max_nb_queue_pairs)
1167         return -1;
1168       /* start the device */
1169       rte_cryptodev_start (i);
1170     }
1171
1172   for (i = 0; i < cdev->data->nb_queue_pairs; i++)
1173     {
1174       cryptodev_inst_t *cdev_inst;
1175       vec_add2(cmt->cryptodev_inst, cdev_inst, 1);
1176       cdev_inst->desc = vec_new (char, strlen (info.device->name) + 10);
1177       cdev_inst->dev_id = cryptodev_id;
1178       cdev_inst->q_id = i;
1179
1180       snprintf (cdev_inst->desc, strlen (info.device->name) + 9,
1181                 "%s_q%u", info.device->name, i);
1182     }
1183
1184   return 0;
1185 }
1186
1187 static int
1188 cryptodev_cmp (void *v1, void *v2)
1189 {
1190   cryptodev_inst_t *a1 = v1;
1191   cryptodev_inst_t *a2 = v2;
1192
1193   if (a1->q_id > a2->q_id)
1194     return 1;
1195   if (a1->q_id < a2->q_id)
1196     return -1;
1197   return 0;
1198 }
1199
1200 static int
1201 cryptodev_probe (vlib_main_t *vm, u32 n_workers)
1202 {
1203   cryptodev_main_t *cmt = &cryptodev_main;
1204   u32 n_queues = cryptodev_count_queue (vm->numa_node);
1205   u32 i;
1206   int ret;
1207
1208   /* If there is not enough queues, exit */
1209   if (n_queues < n_workers)
1210       return -1;
1211
1212   for (i = 0; i < rte_cryptodev_count (); i++)
1213     {
1214       ret = cryptodev_configure (vm, i);
1215       if (ret)
1216         return ret;
1217     }
1218
1219   vec_sort_with_function(cmt->cryptodev_inst, cryptodev_cmp);
1220
1221   return 0;
1222 }
1223
1224 static int
1225 cryptodev_get_session_sz (vlib_main_t *vm, uint32_t n_workers)
1226 {
1227   u32 sess_data_sz = 0, i;
1228
1229   if (rte_cryptodev_count () == 0)
1230     return -1;
1231
1232   for (i = 0; i < rte_cryptodev_count (); i++)
1233     {
1234       u32 dev_sess_sz = rte_cryptodev_sym_get_private_session_size (i);
1235
1236       sess_data_sz = dev_sess_sz > sess_data_sz ? dev_sess_sz : sess_data_sz;
1237     }
1238
1239   return sess_data_sz;
1240 }
1241
1242 static void
1243 dpdk_disable_cryptodev_engine (vlib_main_t * vm)
1244 {
1245   cryptodev_main_t *cmt = &cryptodev_main;
1246   cryptodev_numa_data_t *numa_data;
1247
1248   vec_validate (cmt->per_numa_data, vm->numa_node);
1249   numa_data = vec_elt_at_index (cmt->per_numa_data, vm->numa_node);
1250
1251   if (numa_data->sess_pool)
1252     rte_mempool_free (numa_data->sess_pool);
1253   if (numa_data->sess_priv_pool)
1254     rte_mempool_free (numa_data->sess_priv_pool);
1255   if (numa_data->cop_pool)
1256     rte_mempool_free (numa_data->cop_pool);
1257 }
1258
1259 static void
1260 crypto_op_init (struct rte_mempool *mempool,
1261                 void *_arg __attribute__ ((unused)),
1262                 void *_obj, unsigned i __attribute__ ((unused)))
1263 {
1264   struct rte_crypto_op *op = _obj;
1265
1266   op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1267   op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1268   op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1269   op->phys_addr = rte_mempool_virt2iova (_obj);
1270   op->mempool = mempool;
1271 }
1272
1273
1274 clib_error_t *
1275 dpdk_cryptodev_init (vlib_main_t * vm)
1276 {
1277   cryptodev_main_t *cmt = &cryptodev_main;
1278   vlib_thread_main_t *tm = vlib_get_thread_main ();
1279   cryptodev_engine_thread_t *ptd;
1280   cryptodev_numa_data_t *numa_data;
1281   struct rte_mempool *mp;
1282   u32 skip_master = vlib_num_workers () > 0;
1283   u32 n_workers = tm->n_vlib_mains - skip_master;
1284   u32 numa = vm->numa_node;
1285   i32 sess_sz;
1286   u64 n_cop_elts;
1287   u32 eidx;
1288   u32 i;
1289   u8 *name = 0;
1290   clib_error_t *error;
1291   struct rte_crypto_op_pool_private *priv;
1292
1293   cmt->iova_mode = rte_eal_iova_mode ();
1294
1295   sess_sz = cryptodev_get_session_sz(vm, n_workers);
1296   if (sess_sz < 0)
1297     {
1298       error = clib_error_return (0, "Not enough cryptodevs");
1299       return error;
1300     }
1301
1302   /* A total of 4 times n_worker threads * frame size as crypto ops */
1303   n_cop_elts = max_pow2 ((u64)n_workers * CRYPTODEV_NB_CRYPTO_OPS);
1304
1305   /* probe all cryptodev devices and get queue info */
1306   if (cryptodev_probe (vm, n_workers) < 0)
1307     {
1308       error = clib_error_return (0, "Failed to configure cryptodev");
1309       goto err_handling;
1310     }
1311
1312   clib_bitmap_vec_validate (cmt->active_cdev_inst_mask, tm->n_vlib_mains);
1313   clib_spinlock_init (&cmt->tlock);
1314
1315   vec_validate_aligned(cmt->per_thread_data, tm->n_vlib_mains - 1,
1316                        CLIB_CACHE_LINE_BYTES);
1317   for (i = skip_master; i < tm->n_vlib_mains; i++)
1318     {
1319       ptd = cmt->per_thread_data + i;
1320
1321       cryptodev_assign_resource (ptd, 0, CRYPTODEV_RESOURCE_ASSIGN_AUTO);
1322       name = format (0, "frames_ring_%u%c", i, 0);
1323       ptd->ring = rte_ring_create((char *) name, CRYPTODEV_NB_CRYPTO_OPS,
1324                                   vm->numa_node, RING_F_SP_ENQ|RING_F_SC_DEQ);
1325       if (!ptd->ring)
1326         {
1327           error = clib_error_return (0, "Not enough memory for mp %s", name);
1328           vec_free (name);
1329           goto err_handling;
1330         }
1331       vec_validate (ptd->cops, VNET_CRYPTO_FRAME_SIZE - 1);
1332       vec_free(name);
1333
1334       numa = vlib_mains[i]->numa_node;
1335
1336       vec_validate (cmt->per_numa_data, numa);
1337       numa_data = vec_elt_at_index (cmt->per_numa_data, numa);
1338
1339       if (numa_data->sess_pool)
1340         continue;
1341
1342       /* create session pool for the numa node */
1343       name = format (0, "vcryptodev_sess_pool_%u%c", numa, 0);
1344       mp = rte_cryptodev_sym_session_pool_create (
1345         (char *) name, CRYPTODEV_NB_SESSION, 0, 0, 0, numa);
1346       if (!mp)
1347         {
1348           error = clib_error_return (0, "Not enough memory for mp %s", name);
1349           goto err_handling;
1350         }
1351       vec_free (name);
1352
1353       numa_data->sess_pool = mp;
1354
1355       /* create session private pool for the numa node */
1356       name = format (0, "cryptodev_sess_pool_%u%c", numa, 0);
1357       mp = rte_mempool_create ((char *) name, CRYPTODEV_NB_SESSION, sess_sz, 0,
1358                                0, NULL, NULL, NULL, NULL, numa, 0);
1359       if (!mp)
1360         {
1361           error = clib_error_return (0, "Not enough memory for mp %s", name);
1362           vec_free (name);
1363           goto err_handling;
1364         }
1365
1366       vec_free (name);
1367
1368       numa_data->sess_priv_pool = mp;
1369
1370       /* create cryptodev op pool */
1371       name = format (0, "cryptodev_op_pool_%u%c", numa, 0);
1372
1373       mp = rte_mempool_create ((char *) name, n_cop_elts,
1374                                sizeof (cryptodev_op_t), VLIB_FRAME_SIZE * 2,
1375                                sizeof (struct rte_crypto_op_pool_private),
1376                                NULL, NULL, crypto_op_init, NULL, numa, 0);
1377       if (!mp)
1378         {
1379           error = clib_error_return (0, "Not enough memory for mp %s", name);
1380           vec_free (name);
1381           goto err_handling;
1382         }
1383
1384       priv = rte_mempool_get_priv (mp);
1385       priv->priv_size = sizeof (struct rte_crypto_op_pool_private);
1386       priv->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1387       vec_free (name);
1388       numa_data->cop_pool = mp;
1389     }
1390
1391   /* register handler */
1392   eidx = vnet_crypto_register_engine (vm, "dpdk_cryptodev", 100,
1393                                       "DPDK Cryptodev Engine");
1394
1395 #define _(a, b, c, d, e, f) \
1396   vnet_crypto_register_async_handler \
1397     (vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_ENC, \
1398         cryptodev_enqueue_gcm_aad_##f##_enc,\
1399         cryptodev_frame_dequeue); \
1400   vnet_crypto_register_async_handler \
1401     (vm, eidx, VNET_CRYPTO_OP_##a##_TAG##e##_AAD##f##_DEC, \
1402         cryptodev_enqueue_gcm_aad_##f##_dec, \
1403         cryptodev_frame_dequeue);
1404
1405   foreach_vnet_aead_crypto_conversion
1406 #undef _
1407
1408 #define _(a, b, c, d) \
1409   vnet_crypto_register_async_handler \
1410     (vm, eidx, VNET_CRYPTO_OP_##a##_##c##_TAG##d##_ENC, \
1411         cryptodev_enqueue_linked_alg_enc, \
1412         cryptodev_frame_dequeue); \
1413   vnet_crypto_register_async_handler \
1414     (vm, eidx, VNET_CRYPTO_OP_##a##_##c##_TAG##d##_DEC, \
1415         cryptodev_enqueue_linked_alg_dec, \
1416         cryptodev_frame_dequeue);
1417
1418     foreach_cryptodev_link_async_alg
1419 #undef _
1420
1421   vnet_crypto_register_key_handler (vm, eidx, cryptodev_key_handler);
1422
1423   /* this engine is only enabled when cryptodev device(s) are presented in
1424    * startup.conf. Assume it is wanted to be used, turn on async mode here.
1425    */
1426   vnet_crypto_request_async_mode (1);
1427   ipsec_set_async_mode (1);
1428
1429   return 0;
1430
1431 err_handling:
1432   dpdk_disable_cryptodev_engine (vm);
1433
1434   return error;
1435 }
1436 /* *INDENT-On* */
1437
1438 /*
1439  * fd.io coding-style-patch-verification: ON
1440  *
1441  * Local Variables:
1442  * eval: (c-set-style "gnu")
1443  * End:
1444  */