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