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