examples: use new API to create control threads
[dpdk.git] / lib / librte_vhost / vhost_crypto.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017-2018 Intel Corporation
3  */
4 #include <rte_malloc.h>
5 #include <rte_hash.h>
6 #include <rte_jhash.h>
7 #include <rte_mbuf.h>
8 #include <rte_cryptodev.h>
9
10 #include "rte_vhost_crypto.h"
11 #include "vhost.h"
12 #include "vhost_user.h"
13 #include "virtio_crypto.h"
14
15 #define INHDR_LEN               (sizeof(struct virtio_crypto_inhdr))
16 #define IV_OFFSET               (sizeof(struct rte_crypto_op) + \
17                                 sizeof(struct rte_crypto_sym_op))
18
19 #ifdef RTE_LIBRTE_VHOST_DEBUG
20 #define VC_LOG_ERR(fmt, args...)                                \
21         RTE_LOG(ERR, USER1, "[%s] %s() line %u: " fmt "\n",     \
22                 "Vhost-Crypto", __func__, __LINE__, ## args)
23 #define VC_LOG_INFO(fmt, args...)                               \
24         RTE_LOG(INFO, USER1, "[%s] %s() line %u: " fmt "\n",    \
25                 "Vhost-Crypto", __func__, __LINE__, ## args)
26
27 #define VC_LOG_DBG(fmt, args...)                                \
28         RTE_LOG(DEBUG, USER1, "[%s] %s() line %u: " fmt "\n",   \
29                 "Vhost-Crypto", __func__, __LINE__, ## args)
30 #else
31 #define VC_LOG_ERR(fmt, args...)                                \
32         RTE_LOG(ERR, USER1, "[VHOST-Crypto]: " fmt "\n", ## args)
33 #define VC_LOG_INFO(fmt, args...)                               \
34         RTE_LOG(INFO, USER1, "[VHOST-Crypto]: " fmt "\n", ## args)
35 #define VC_LOG_DBG(fmt, args...)
36 #endif
37
38 #define VIRTIO_CRYPTO_FEATURES ((1 << VIRTIO_F_NOTIFY_ON_EMPTY) |       \
39                 (1 << VIRTIO_RING_F_INDIRECT_DESC) |                    \
40                 (1 << VIRTIO_RING_F_EVENT_IDX) |                        \
41                 (1 << VIRTIO_CRYPTO_SERVICE_CIPHER) |                   \
42                 (1 << VIRTIO_CRYPTO_SERVICE_MAC) |                      \
43                 (1 << VIRTIO_NET_F_CTRL_VQ))
44
45 #define GPA_TO_VVA(t, m, a, l)  ((t)(uintptr_t)rte_vhost_va_from_guest_pa(m, a, l))
46
47 static int
48 cipher_algo_transform(uint32_t virtio_cipher_algo)
49 {
50         int ret;
51
52         switch (virtio_cipher_algo) {
53         case VIRTIO_CRYPTO_CIPHER_AES_CBC:
54                 ret = RTE_CRYPTO_CIPHER_AES_CBC;
55                 break;
56         case VIRTIO_CRYPTO_CIPHER_AES_CTR:
57                 ret = RTE_CRYPTO_CIPHER_AES_CTR;
58                 break;
59         case VIRTIO_CRYPTO_CIPHER_DES_ECB:
60                 ret = -VIRTIO_CRYPTO_NOTSUPP;
61                 break;
62         case VIRTIO_CRYPTO_CIPHER_DES_CBC:
63                 ret = RTE_CRYPTO_CIPHER_DES_CBC;
64                 break;
65         case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
66                 ret = RTE_CRYPTO_CIPHER_3DES_ECB;
67                 break;
68         case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
69                 ret = RTE_CRYPTO_CIPHER_3DES_CBC;
70                 break;
71         case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
72                 ret = RTE_CRYPTO_CIPHER_3DES_CTR;
73                 break;
74         case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
75                 ret = RTE_CRYPTO_CIPHER_KASUMI_F8;
76                 break;
77         case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
78                 ret = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
79                 break;
80         case VIRTIO_CRYPTO_CIPHER_AES_F8:
81                 ret = RTE_CRYPTO_CIPHER_AES_F8;
82                 break;
83         case VIRTIO_CRYPTO_CIPHER_AES_XTS:
84                 ret = RTE_CRYPTO_CIPHER_AES_XTS;
85                 break;
86         case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
87                 ret = RTE_CRYPTO_CIPHER_ZUC_EEA3;
88                 break;
89         default:
90                 ret = -VIRTIO_CRYPTO_BADMSG;
91                 break;
92         }
93
94         return ret;
95 }
96
97 static int
98 auth_algo_transform(uint32_t virtio_auth_algo)
99 {
100         int ret;
101
102         switch (virtio_auth_algo) {
103
104         case VIRTIO_CRYPTO_NO_MAC:
105                 ret = RTE_CRYPTO_AUTH_NULL;
106                 break;
107         case VIRTIO_CRYPTO_MAC_HMAC_MD5:
108                 ret = RTE_CRYPTO_AUTH_MD5_HMAC;
109                 break;
110         case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
111                 ret = RTE_CRYPTO_AUTH_SHA1_HMAC;
112                 break;
113         case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
114                 ret = RTE_CRYPTO_AUTH_SHA224_HMAC;
115                 break;
116         case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
117                 ret = RTE_CRYPTO_AUTH_SHA256_HMAC;
118                 break;
119         case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
120                 ret = RTE_CRYPTO_AUTH_SHA384_HMAC;
121                 break;
122         case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
123                 ret = RTE_CRYPTO_AUTH_SHA512_HMAC;
124                 break;
125         case VIRTIO_CRYPTO_MAC_CMAC_3DES:
126                 ret = -VIRTIO_CRYPTO_NOTSUPP;
127                 break;
128         case VIRTIO_CRYPTO_MAC_CMAC_AES:
129                 ret = RTE_CRYPTO_AUTH_AES_CMAC;
130                 break;
131         case VIRTIO_CRYPTO_MAC_KASUMI_F9:
132                 ret = RTE_CRYPTO_AUTH_KASUMI_F9;
133                 break;
134         case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
135                 ret = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
136                 break;
137         case VIRTIO_CRYPTO_MAC_GMAC_AES:
138                 ret = RTE_CRYPTO_AUTH_AES_GMAC;
139                 break;
140         case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
141                 ret = -VIRTIO_CRYPTO_NOTSUPP;
142                 break;
143         case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
144                 ret = RTE_CRYPTO_AUTH_AES_CBC_MAC;
145                 break;
146         case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
147                 ret = -VIRTIO_CRYPTO_NOTSUPP;
148                 break;
149         case VIRTIO_CRYPTO_MAC_XCBC_AES:
150                 ret = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
151                 break;
152         default:
153                 ret = -VIRTIO_CRYPTO_BADMSG;
154                 break;
155         }
156
157         return ret;
158 }
159
160 static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
161 {
162         int len;
163
164         switch (algo) {
165         case RTE_CRYPTO_CIPHER_3DES_CBC:
166                 len = 8;
167                 break;
168         case RTE_CRYPTO_CIPHER_3DES_CTR:
169                 len = 8;
170                 break;
171         case RTE_CRYPTO_CIPHER_3DES_ECB:
172                 len = 8;
173                 break;
174         case RTE_CRYPTO_CIPHER_AES_CBC:
175                 len = 16;
176                 break;
177
178         /* TODO: add common algos */
179
180         default:
181                 len = -1;
182                 break;
183         }
184
185         return len;
186 }
187
188 /**
189  * vhost_crypto struct is used to maintain a number of virtio_cryptos and
190  * one DPDK crypto device that deals with all crypto workloads. It is declared
191  * here and defined in vhost_crypto.c
192  */
193 struct vhost_crypto {
194         /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
195          *  session ID.
196          */
197         struct rte_hash *session_map;
198         struct rte_mempool *mbuf_pool;
199         struct rte_mempool *sess_pool;
200
201         /** DPDK cryptodev ID */
202         uint8_t cid;
203         uint16_t nb_qps;
204
205         uint64_t last_session_id;
206
207         uint64_t cache_session_id;
208         struct rte_cryptodev_sym_session *cache_session;
209         /** socket id for the device */
210         int socket_id;
211
212         struct virtio_net *dev;
213
214         uint8_t option;
215 } __rte_cache_aligned;
216
217 struct vhost_crypto_data_req {
218         struct vring_desc *head;
219         struct rte_vhost_memory *mem;
220         struct virtio_crypto_inhdr *inhdr;
221         struct vhost_virtqueue *vq;
222         struct vring_desc *wb_desc;
223         uint16_t wb_len;
224         uint16_t desc_idx;
225         uint16_t len;
226         uint16_t zero_copy;
227 };
228
229 static int
230 transform_cipher_param(struct rte_crypto_sym_xform *xform,
231                 VhostUserCryptoSessionParam *param)
232 {
233         int ret;
234
235         ret = cipher_algo_transform(param->cipher_algo);
236         if (unlikely(ret < 0))
237                 return ret;
238
239         xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
240         xform->cipher.algo = (uint32_t)ret;
241         xform->cipher.key.length = param->cipher_key_len;
242         if (xform->cipher.key.length > 0)
243                 xform->cipher.key.data = param->cipher_key_buf;
244         if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
245                 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
246         else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
247                 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
248         else {
249                 VC_LOG_DBG("Bad operation type");
250                 return -VIRTIO_CRYPTO_BADMSG;
251         }
252
253         ret = get_iv_len(xform->cipher.algo);
254         if (unlikely(ret < 0))
255                 return ret;
256         xform->cipher.iv.length = (uint16_t)ret;
257         xform->cipher.iv.offset = IV_OFFSET;
258         return 0;
259 }
260
261 static int
262 transform_chain_param(struct rte_crypto_sym_xform *xforms,
263                 VhostUserCryptoSessionParam *param)
264 {
265         struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
266         int ret;
267
268         switch (param->chaining_dir) {
269         case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
270                 xform_auth = xforms;
271                 xform_cipher = xforms->next;
272                 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
273                 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
274                 break;
275         case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
276                 xform_cipher = xforms;
277                 xform_auth = xforms->next;
278                 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
279                 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
280                 break;
281         default:
282                 return -VIRTIO_CRYPTO_BADMSG;
283         }
284
285         /* cipher */
286         ret = cipher_algo_transform(param->cipher_algo);
287         if (unlikely(ret < 0))
288                 return ret;
289         xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
290         xform_cipher->cipher.algo = (uint32_t)ret;
291         xform_cipher->cipher.key.length = param->cipher_key_len;
292         xform_cipher->cipher.key.data = param->cipher_key_buf;
293         ret = get_iv_len(xform_cipher->cipher.algo);
294         if (unlikely(ret < 0))
295                 return ret;
296         xform_cipher->cipher.iv.length = (uint16_t)ret;
297         xform_cipher->cipher.iv.offset = IV_OFFSET;
298
299         /* auth */
300         xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
301         ret = auth_algo_transform(param->hash_algo);
302         if (unlikely(ret < 0))
303                 return ret;
304         xform_auth->auth.algo = (uint32_t)ret;
305         xform_auth->auth.digest_length = param->digest_len;
306         xform_auth->auth.key.length = param->auth_key_len;
307         xform_auth->auth.key.data = param->auth_key_buf;
308
309         return 0;
310 }
311
312 static void
313 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
314                 VhostUserCryptoSessionParam *sess_param)
315 {
316         struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
317         struct rte_cryptodev_sym_session *session;
318         int ret;
319
320         switch (sess_param->op_type) {
321         case VIRTIO_CRYPTO_SYM_OP_NONE:
322         case VIRTIO_CRYPTO_SYM_OP_CIPHER:
323                 ret = transform_cipher_param(&xform1, sess_param);
324                 if (unlikely(ret)) {
325                         VC_LOG_ERR("Error transform session msg (%i)", ret);
326                         sess_param->session_id = ret;
327                         return;
328                 }
329                 break;
330         case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
331                 if (unlikely(sess_param->hash_mode !=
332                                 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
333                         sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
334                         VC_LOG_ERR("Error transform session message (%i)",
335                                         -VIRTIO_CRYPTO_NOTSUPP);
336                         return;
337                 }
338
339                 xform1.next = &xform2;
340
341                 ret = transform_chain_param(&xform1, sess_param);
342                 if (unlikely(ret)) {
343                         VC_LOG_ERR("Error transform session message (%i)", ret);
344                         sess_param->session_id = ret;
345                         return;
346                 }
347
348                 break;
349         default:
350                 VC_LOG_ERR("Algorithm not yet supported");
351                 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
352                 return;
353         }
354
355         session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
356         if (!session) {
357                 VC_LOG_ERR("Failed to create session");
358                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
359                 return;
360         }
361
362         if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
363                         vcrypto->sess_pool) < 0) {
364                 VC_LOG_ERR("Failed to initialize session");
365                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
366                 return;
367         }
368
369         /* insert hash to map */
370         if (rte_hash_add_key_data(vcrypto->session_map,
371                         &vcrypto->last_session_id, session) < 0) {
372                 VC_LOG_ERR("Failed to insert session to hash table");
373
374                 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
375                         VC_LOG_ERR("Failed to clear session");
376                 else {
377                         if (rte_cryptodev_sym_session_free(session) < 0)
378                                 VC_LOG_ERR("Failed to free session");
379                 }
380                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
381                 return;
382         }
383
384         VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
385                         vcrypto->last_session_id, vcrypto->dev->vid);
386
387         sess_param->session_id = vcrypto->last_session_id;
388         vcrypto->last_session_id++;
389 }
390
391 static int
392 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
393 {
394         struct rte_cryptodev_sym_session *session;
395         uint64_t sess_id = session_id;
396         int ret;
397
398         ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
399                         (void **)&session);
400
401         if (unlikely(ret < 0)) {
402                 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
403                 return -VIRTIO_CRYPTO_INVSESS;
404         }
405
406         if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
407                 VC_LOG_DBG("Failed to clear session");
408                 return -VIRTIO_CRYPTO_ERR;
409         }
410
411         if (rte_cryptodev_sym_session_free(session) < 0) {
412                 VC_LOG_DBG("Failed to free session");
413                 return -VIRTIO_CRYPTO_ERR;
414         }
415
416         if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
417                 VC_LOG_DBG("Failed to delete session from hash table.");
418                 return -VIRTIO_CRYPTO_ERR;
419         }
420
421         VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
422                         vcrypto->dev->vid);
423
424         return 0;
425 }
426
427 static int
428 vhost_crypto_msg_post_handler(int vid, void *msg, uint32_t *require_reply)
429 {
430         struct virtio_net *dev = get_device(vid);
431         struct vhost_crypto *vcrypto;
432         VhostUserMsg *vmsg = msg;
433         int ret = 0;
434
435         if (dev == NULL || require_reply == NULL) {
436                 VC_LOG_ERR("Invalid vid %i", vid);
437                 return -EINVAL;
438         }
439
440         vcrypto = dev->extern_data;
441         if (vcrypto == NULL) {
442                 VC_LOG_ERR("Cannot find required data, is it initialized?");
443                 return -ENOENT;
444         }
445
446         *require_reply = 0;
447
448         if (vmsg->request.master == VHOST_USER_CRYPTO_CREATE_SESS) {
449                 vhost_crypto_create_sess(vcrypto,
450                                 &vmsg->payload.crypto_session);
451                 *require_reply = 1;
452         } else if (vmsg->request.master == VHOST_USER_CRYPTO_CLOSE_SESS)
453                 ret = vhost_crypto_close_sess(vcrypto, vmsg->payload.u64);
454         else
455                 ret = -EINVAL;
456
457         return ret;
458 }
459
460 static __rte_always_inline struct vring_desc *
461 find_write_desc(struct vring_desc *head, struct vring_desc *desc)
462 {
463         if (desc->flags & VRING_DESC_F_WRITE)
464                 return desc;
465
466         while (desc->flags & VRING_DESC_F_NEXT) {
467                 desc = &head[desc->next];
468                 if (desc->flags & VRING_DESC_F_WRITE)
469                         return desc;
470         }
471
472         return NULL;
473 }
474
475 static struct virtio_crypto_inhdr *
476 reach_inhdr(struct vring_desc *head, struct rte_vhost_memory *mem,
477                 struct vring_desc *desc)
478 {
479         uint64_t dlen;
480         struct virtio_crypto_inhdr *inhdr;
481
482         while (desc->flags & VRING_DESC_F_NEXT)
483                 desc = &head[desc->next];
484
485         dlen = desc->len;
486         inhdr = GPA_TO_VVA(struct virtio_crypto_inhdr *, mem, desc->addr, &dlen);
487         if (unlikely(dlen != desc->len))
488                 return NULL;
489
490         return inhdr;
491 }
492
493 static __rte_always_inline int
494 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
495                 uint32_t size)
496 {
497         struct vring_desc *desc = *cur_desc;
498         int left = size;
499
500         rte_prefetch0(&head[desc->next]);
501         left -= desc->len;
502
503         while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
504                 desc = &head[desc->next];
505                 rte_prefetch0(&head[desc->next]);
506                 left -= desc->len;
507         }
508
509         if (unlikely(left < 0)) {
510                 VC_LOG_ERR("Incorrect virtio descriptor");
511                 return -1;
512         }
513
514         *cur_desc = &head[desc->next];
515         return 0;
516 }
517
518 static int
519 copy_data(void *dst_data, struct vring_desc *head, struct rte_vhost_memory *mem,
520                 struct vring_desc **cur_desc, uint32_t size)
521 {
522         struct vring_desc *desc = *cur_desc;
523         uint32_t to_copy;
524         uint8_t *data = dst_data;
525         uint8_t *src;
526         int left = size;
527         uint64_t dlen;
528
529         rte_prefetch0(&head[desc->next]);
530         to_copy = RTE_MIN(desc->len, (uint32_t)left);
531         dlen = desc->len;
532         src = GPA_TO_VVA(uint8_t *, mem, desc->addr, &dlen);
533         if (unlikely(!src || dlen != desc->len)) {
534                 VC_LOG_ERR("Failed to map descriptor");
535                 return -1;
536         }
537
538         rte_memcpy((uint8_t *)data, src, to_copy);
539         left -= to_copy;
540
541         while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
542                 desc = &head[desc->next];
543                 rte_prefetch0(&head[desc->next]);
544                 to_copy = RTE_MIN(desc->len, (uint32_t)left);
545                 dlen = desc->len;
546                 src = GPA_TO_VVA(uint8_t *, mem, desc->addr, &dlen);
547                 if (unlikely(!src || dlen != desc->len)) {
548                         VC_LOG_ERR("Failed to map descriptor");
549                         return -1;
550                 }
551
552                 rte_memcpy(data + size - left, src, to_copy);
553                 left -= to_copy;
554         }
555
556         if (unlikely(left < 0)) {
557                 VC_LOG_ERR("Incorrect virtio descriptor");
558                 return -1;
559         }
560
561         *cur_desc = &head[desc->next];
562
563         return 0;
564 }
565
566 static __rte_always_inline void *
567 get_data_ptr(struct vring_desc *head, struct rte_vhost_memory *mem,
568                 struct vring_desc **cur_desc, uint32_t size)
569 {
570         void *data;
571         uint64_t dlen = (*cur_desc)->len;
572
573         data = GPA_TO_VVA(void *, mem, (*cur_desc)->addr, &dlen);
574         if (unlikely(!data || dlen != (*cur_desc)->len)) {
575                 VC_LOG_ERR("Failed to map object");
576                 return NULL;
577         }
578
579         if (unlikely(move_desc(head, cur_desc, size) < 0))
580                 return NULL;
581
582         return data;
583 }
584
585 static int
586 write_back_data(struct rte_crypto_op *op, struct vhost_crypto_data_req *vc_req)
587 {
588         struct rte_mbuf *mbuf = op->sym->m_dst;
589         struct vring_desc *head = vc_req->head;
590         struct rte_vhost_memory *mem = vc_req->mem;
591         struct vring_desc *desc = vc_req->wb_desc;
592         int left = vc_req->wb_len;
593         uint32_t to_write;
594         uint8_t *src_data = mbuf->buf_addr, *dst;
595         uint64_t dlen;
596
597         rte_prefetch0(&head[desc->next]);
598         to_write = RTE_MIN(desc->len, (uint32_t)left);
599         dlen = desc->len;
600         dst = GPA_TO_VVA(uint8_t *, mem, desc->addr, &dlen);
601         if (unlikely(!dst || dlen != desc->len)) {
602                 VC_LOG_ERR("Failed to map descriptor");
603                 return -1;
604         }
605
606         rte_memcpy(dst, src_data, to_write);
607         left -= to_write;
608         src_data += to_write;
609
610         while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
611                 desc = &head[desc->next];
612                 rte_prefetch0(&head[desc->next]);
613                 to_write = RTE_MIN(desc->len, (uint32_t)left);
614                 dlen = desc->len;
615                 dst = GPA_TO_VVA(uint8_t *, mem, desc->addr, &dlen);
616                 if (unlikely(!dst || dlen != desc->len)) {
617                         VC_LOG_ERR("Failed to map descriptor");
618                         return -1;
619                 }
620
621                 rte_memcpy(dst, src_data, to_write);
622                 left -= to_write;
623                 src_data += to_write;
624         }
625
626         if (unlikely(left < 0)) {
627                 VC_LOG_ERR("Incorrect virtio descriptor");
628                 return -1;
629         }
630
631         return 0;
632 }
633
634 static uint8_t
635 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
636                 struct vhost_crypto_data_req *vc_req,
637                 struct virtio_crypto_cipher_data_req *cipher,
638                 struct vring_desc *cur_desc)
639 {
640         struct vring_desc *head = vc_req->head;
641         struct vring_desc *desc = cur_desc;
642         struct rte_vhost_memory *mem = vc_req->mem;
643         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
644         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
645         uint8_t ret = 0;
646
647         /* prepare */
648         /* iv */
649         if (unlikely(copy_data(iv_data, head, mem, &desc,
650                         cipher->para.iv_len) < 0)) {
651                 ret = VIRTIO_CRYPTO_BADMSG;
652                 goto error_exit;
653         }
654
655         m_src->data_len = cipher->para.src_data_len;
656
657         switch (vcrypto->option) {
658         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
659                 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
660                                 cipher->para.src_data_len);
661                 m_src->buf_addr = get_data_ptr(head, mem, &desc,
662                                 cipher->para.src_data_len);
663                 if (unlikely(m_src->buf_iova == 0 ||
664                                 m_src->buf_addr == NULL)) {
665                         VC_LOG_ERR("zero_copy may fail due to cross page data");
666                         ret = VIRTIO_CRYPTO_ERR;
667                         goto error_exit;
668                 }
669                 break;
670         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
671                 if (unlikely(cipher->para.src_data_len >
672                                 RTE_MBUF_DEFAULT_BUF_SIZE)) {
673                         VC_LOG_ERR("Not enough space to do data copy");
674                         ret = VIRTIO_CRYPTO_ERR;
675                         goto error_exit;
676                 }
677                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), head,
678                                 mem, &desc, cipher->para.src_data_len))
679                                 < 0) {
680                         ret = VIRTIO_CRYPTO_BADMSG;
681                         goto error_exit;
682                 }
683                 break;
684         default:
685                 ret = VIRTIO_CRYPTO_BADMSG;
686                 goto error_exit;
687         }
688
689         /* dst */
690         desc = find_write_desc(head, desc);
691         if (unlikely(!desc)) {
692                 VC_LOG_ERR("Cannot find write location");
693                 ret = VIRTIO_CRYPTO_BADMSG;
694                 goto error_exit;
695         }
696
697         switch (vcrypto->option) {
698         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
699                 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
700                                 desc->addr, cipher->para.dst_data_len);
701                 m_dst->buf_addr = get_data_ptr(head, mem, &desc,
702                                 cipher->para.dst_data_len);
703                 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
704                         VC_LOG_ERR("zero_copy may fail due to cross page data");
705                         ret = VIRTIO_CRYPTO_ERR;
706                         goto error_exit;
707                 }
708
709                 m_dst->data_len = cipher->para.dst_data_len;
710                 break;
711         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
712                 vc_req->wb_desc = desc;
713                 vc_req->wb_len = cipher->para.dst_data_len;
714                 if (unlikely(move_desc(head, &desc, vc_req->wb_len) < 0)) {
715                         ret = VIRTIO_CRYPTO_ERR;
716                         goto error_exit;
717                 }
718                 break;
719         default:
720                 ret = VIRTIO_CRYPTO_BADMSG;
721                 goto error_exit;
722         }
723
724         /* src data */
725         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
726         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
727
728         op->sym->cipher.data.offset = 0;
729         op->sym->cipher.data.length = cipher->para.src_data_len;
730
731         vc_req->inhdr = get_data_ptr(head, mem, &desc, INHDR_LEN);
732         if (unlikely(vc_req->inhdr == NULL)) {
733                 ret = VIRTIO_CRYPTO_BADMSG;
734                 goto error_exit;
735         }
736
737         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
738         vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
739
740         return 0;
741
742 error_exit:
743         vc_req->len = INHDR_LEN;
744         return ret;
745 }
746
747 static uint8_t
748 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
749                 struct vhost_crypto_data_req *vc_req,
750                 struct virtio_crypto_alg_chain_data_req *chain,
751                 struct vring_desc *cur_desc)
752 {
753         struct vring_desc *head = vc_req->head;
754         struct vring_desc *desc = cur_desc;
755         struct rte_vhost_memory *mem = vc_req->mem;
756         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
757         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
758         uint32_t digest_offset;
759         void *digest_addr;
760         uint8_t ret = 0;
761
762         /* prepare */
763         /* iv */
764         if (unlikely(copy_data(iv_data, head, mem, &desc,
765                         chain->para.iv_len) < 0)) {
766                 ret = VIRTIO_CRYPTO_BADMSG;
767                 goto error_exit;
768         }
769
770         m_src->data_len = chain->para.src_data_len;
771         m_dst->data_len = chain->para.dst_data_len;
772
773         switch (vcrypto->option) {
774         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
775                 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
776                                 chain->para.src_data_len);
777                 m_src->buf_addr = get_data_ptr(head, mem, &desc,
778                                 chain->para.src_data_len);
779                 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
780                         VC_LOG_ERR("zero_copy may fail due to cross page data");
781                         ret = VIRTIO_CRYPTO_ERR;
782                         goto error_exit;
783                 }
784                 break;
785         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
786                 if (unlikely(chain->para.src_data_len >
787                                 RTE_MBUF_DEFAULT_BUF_SIZE)) {
788                         VC_LOG_ERR("Not enough space to do data copy");
789                         ret = VIRTIO_CRYPTO_ERR;
790                         goto error_exit;
791                 }
792                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), head,
793                                 mem, &desc, chain->para.src_data_len)) < 0) {
794                         ret = VIRTIO_CRYPTO_BADMSG;
795                         goto error_exit;
796                 }
797                 break;
798         default:
799                 ret = VIRTIO_CRYPTO_BADMSG;
800                 goto error_exit;
801         }
802
803         /* dst */
804         desc = find_write_desc(head, desc);
805         if (unlikely(!desc)) {
806                 VC_LOG_ERR("Cannot find write location");
807                 ret = VIRTIO_CRYPTO_BADMSG;
808                 goto error_exit;
809         }
810
811         switch (vcrypto->option) {
812         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
813                 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
814                                 desc->addr, chain->para.dst_data_len);
815                 m_dst->buf_addr = get_data_ptr(head, mem, &desc,
816                                 chain->para.dst_data_len);
817                 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
818                         VC_LOG_ERR("zero_copy may fail due to cross page data");
819                         ret = VIRTIO_CRYPTO_ERR;
820                         goto error_exit;
821                 }
822
823                 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
824                                 desc->addr, chain->para.hash_result_len);
825                 op->sym->auth.digest.data = get_data_ptr(head, mem, &desc,
826                                 chain->para.hash_result_len);
827                 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
828                         VC_LOG_ERR("zero_copy may fail due to cross page data");
829                         ret = VIRTIO_CRYPTO_ERR;
830                         goto error_exit;
831                 }
832                 break;
833         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
834                 digest_offset = m_dst->data_len;
835                 digest_addr = rte_pktmbuf_mtod_offset(m_dst, void *,
836                                 digest_offset);
837
838                 vc_req->wb_desc = desc;
839                 vc_req->wb_len = m_dst->data_len + chain->para.hash_result_len;
840
841                 if (unlikely(move_desc(head, &desc,
842                                 chain->para.dst_data_len) < 0)) {
843                         ret = VIRTIO_CRYPTO_BADMSG;
844                         goto error_exit;
845                 }
846
847                 if (unlikely(copy_data(digest_addr, head, mem, &desc,
848                                 chain->para.hash_result_len)) < 0) {
849                         ret = VIRTIO_CRYPTO_BADMSG;
850                         goto error_exit;
851                 }
852
853                 op->sym->auth.digest.data = digest_addr;
854                 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_dst,
855                                 digest_offset);
856                 if (unlikely(move_desc(head, &desc,
857                                 chain->para.hash_result_len) < 0)) {
858                         ret = VIRTIO_CRYPTO_ERR;
859                         goto error_exit;
860                 }
861                 break;
862         default:
863                 ret = VIRTIO_CRYPTO_BADMSG;
864                 goto error_exit;
865         }
866
867         /* record inhdr */
868         vc_req->inhdr = get_data_ptr(head, mem, &desc, INHDR_LEN);
869         if (unlikely(vc_req->inhdr == NULL)) {
870                 ret = VIRTIO_CRYPTO_BADMSG;
871                 goto error_exit;
872         }
873
874         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
875
876         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
877         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
878
879         op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
880         op->sym->cipher.data.length = chain->para.src_data_len -
881                         chain->para.cipher_start_src_offset;
882
883         op->sym->auth.data.offset = chain->para.hash_start_src_offset;
884         op->sym->auth.data.length = chain->para.len_to_hash;
885
886         vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
887                         INHDR_LEN;
888         return 0;
889
890 error_exit:
891         vc_req->len = INHDR_LEN;
892         return ret;
893 }
894
895 /**
896  * Process on descriptor
897  */
898 static __rte_always_inline int
899 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
900                 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
901                 struct vring_desc *head, uint16_t desc_idx,
902                 struct rte_vhost_memory *mem)
903 {
904         struct vhost_crypto_data_req *vc_req = RTE_PTR_ADD(op->sym->m_src,
905                         sizeof(struct rte_mbuf));
906         struct rte_cryptodev_sym_session *session;
907         struct virtio_crypto_op_data_req *req;
908         struct virtio_crypto_inhdr *inhdr;
909         struct vring_desc *desc = NULL;
910         uint64_t session_id;
911         uint64_t dlen;
912         int err = 0;
913
914         vc_req->desc_idx = desc_idx;
915
916         if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
917                 dlen = head->len;
918                 desc = GPA_TO_VVA(struct vring_desc *, mem, head->addr, &dlen);
919                 if (unlikely(!desc || dlen != head->len))
920                         return -1;
921                 desc_idx = 0;
922         } else {
923                 desc = head;
924         }
925
926         vc_req->mem = mem;
927         vc_req->head = head;
928         vc_req->vq = vq;
929
930         vc_req->zero_copy = vcrypto->option;
931
932         req = get_data_ptr(head, mem, &desc, sizeof(*req));
933         if (unlikely(req == NULL)) {
934                 err = VIRTIO_CRYPTO_ERR;
935                 VC_LOG_ERR("Invalid descriptor");
936                 goto error_exit;
937         }
938
939         switch (req->header.opcode) {
940         case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
941         case VIRTIO_CRYPTO_CIPHER_DECRYPT:
942                 session_id = req->header.session_id;
943
944                 /* one branch to avoid unnecessary table lookup */
945                 if (vcrypto->cache_session_id != session_id) {
946                         err = rte_hash_lookup_data(vcrypto->session_map,
947                                         &session_id, (void **)&session);
948                         if (unlikely(err < 0)) {
949                                 err = VIRTIO_CRYPTO_ERR;
950                                 VC_LOG_ERR("Failed to find session %"PRIu64,
951                                                 session_id);
952                                 goto error_exit;
953                         }
954
955                         vcrypto->cache_session = session;
956                         vcrypto->cache_session_id = session_id;
957                 }
958
959                 session = vcrypto->cache_session;
960
961                 err = rte_crypto_op_attach_sym_session(op, session);
962                 if (unlikely(err < 0)) {
963                         err = VIRTIO_CRYPTO_ERR;
964                         VC_LOG_ERR("Failed to attach session to op");
965                         goto error_exit;
966                 }
967
968                 switch (req->u.sym_req.op_type) {
969                 case VIRTIO_CRYPTO_SYM_OP_NONE:
970                         err = VIRTIO_CRYPTO_NOTSUPP;
971                         break;
972                 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
973                         err = prepare_sym_cipher_op(vcrypto, op, vc_req,
974                                         &req->u.sym_req.u.cipher, desc);
975                         break;
976                 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
977                         err = prepare_sym_chain_op(vcrypto, op, vc_req,
978                                         &req->u.sym_req.u.chain, desc);
979                         break;
980                 }
981                 if (unlikely(err != 0)) {
982                         VC_LOG_ERR("Failed to process sym request");
983                         goto error_exit;
984                 }
985                 break;
986         default:
987                 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
988                                 req->header.opcode);
989                 goto error_exit;
990         }
991
992         return 0;
993
994 error_exit:
995
996         inhdr = reach_inhdr(head, mem, desc);
997         if (likely(inhdr != NULL))
998                 inhdr->status = (uint8_t)err;
999
1000         return -1;
1001 }
1002
1003 static __rte_always_inline struct vhost_virtqueue *
1004 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1005                 struct vhost_virtqueue *old_vq)
1006 {
1007         struct rte_mbuf *m_src = op->sym->m_src;
1008         struct rte_mbuf *m_dst = op->sym->m_dst;
1009         struct vhost_crypto_data_req *vc_req = RTE_PTR_ADD(m_src,
1010                         sizeof(struct rte_mbuf));
1011         uint16_t desc_idx;
1012         int ret = 0;
1013
1014         if (unlikely(!vc_req)) {
1015                 VC_LOG_ERR("Failed to retrieve vc_req");
1016                 return NULL;
1017         }
1018
1019         if (old_vq && (vc_req->vq != old_vq))
1020                 return vc_req->vq;
1021
1022         desc_idx = vc_req->desc_idx;
1023
1024         if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1025                 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1026         else {
1027                 if (vc_req->zero_copy == 0) {
1028                         ret = write_back_data(op, vc_req);
1029                         if (unlikely(ret != 0))
1030                                 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1031                 }
1032         }
1033
1034         vc_req->vq->used->ring[desc_idx].id = desc_idx;
1035         vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1036
1037         rte_mempool_put(m_dst->pool, (void *)m_dst);
1038         rte_mempool_put(m_src->pool, (void *)m_src);
1039
1040         return vc_req->vq;
1041 }
1042
1043 static __rte_always_inline uint16_t
1044 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1045                 uint16_t nb_ops, int *callfd)
1046 {
1047         uint16_t processed = 1;
1048         struct vhost_virtqueue *vq, *tmp_vq;
1049
1050         if (unlikely(nb_ops == 0))
1051                 return 0;
1052
1053         vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1054         if (unlikely(vq == NULL))
1055                 return 0;
1056         tmp_vq = vq;
1057
1058         while ((processed < nb_ops)) {
1059                 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1060                                 tmp_vq);
1061
1062                 if (unlikely(vq != tmp_vq))
1063                         break;
1064
1065                 processed++;
1066         }
1067
1068         *callfd = vq->callfd;
1069
1070         *(volatile uint16_t *)&vq->used->idx += processed;
1071
1072         return processed;
1073 }
1074
1075 int __rte_experimental
1076 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1077                 struct rte_mempool *sess_pool, int socket_id)
1078 {
1079         struct virtio_net *dev = get_device(vid);
1080         struct rte_hash_parameters params = {0};
1081         struct vhost_crypto *vcrypto;
1082         char name[128];
1083         int ret;
1084
1085         if (!dev) {
1086                 VC_LOG_ERR("Invalid vid %i", vid);
1087                 return -EINVAL;
1088         }
1089
1090         ret = rte_vhost_driver_set_features(dev->ifname,
1091                         VIRTIO_CRYPTO_FEATURES);
1092         if (ret < 0) {
1093                 VC_LOG_ERR("Error setting features");
1094                 return -1;
1095         }
1096
1097         vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1098                         RTE_CACHE_LINE_SIZE, socket_id);
1099         if (!vcrypto) {
1100                 VC_LOG_ERR("Insufficient memory");
1101                 return -ENOMEM;
1102         }
1103
1104         vcrypto->sess_pool = sess_pool;
1105         vcrypto->cid = cryptodev_id;
1106         vcrypto->cache_session_id = UINT64_MAX;
1107         vcrypto->last_session_id = 1;
1108         vcrypto->dev = dev;
1109         vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1110
1111         snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1112         params.name = name;
1113         params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1114         params.hash_func = rte_jhash;
1115         params.key_len = sizeof(uint64_t);
1116         params.socket_id = socket_id;
1117         vcrypto->session_map = rte_hash_create(&params);
1118         if (!vcrypto->session_map) {
1119                 VC_LOG_ERR("Failed to creath session map");
1120                 ret = -ENOMEM;
1121                 goto error_exit;
1122         }
1123
1124         snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1125         vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1126                         VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1127                         sizeof(struct vhost_crypto_data_req),
1128                         RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1129                         rte_socket_id());
1130         if (!vcrypto->mbuf_pool) {
1131                 VC_LOG_ERR("Failed to creath mbuf pool");
1132                 ret = -ENOMEM;
1133                 goto error_exit;
1134         }
1135
1136         dev->extern_data = vcrypto;
1137         dev->extern_ops.pre_msg_handle = NULL;
1138         dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1139
1140         return 0;
1141
1142 error_exit:
1143         if (vcrypto->session_map)
1144                 rte_hash_free(vcrypto->session_map);
1145         if (vcrypto->mbuf_pool)
1146                 rte_mempool_free(vcrypto->mbuf_pool);
1147
1148         rte_free(vcrypto);
1149
1150         return ret;
1151 }
1152
1153 int __rte_experimental
1154 rte_vhost_crypto_free(int vid)
1155 {
1156         struct virtio_net *dev = get_device(vid);
1157         struct vhost_crypto *vcrypto;
1158
1159         if (unlikely(dev == NULL)) {
1160                 VC_LOG_ERR("Invalid vid %i", vid);
1161                 return -EINVAL;
1162         }
1163
1164         vcrypto = dev->extern_data;
1165         if (unlikely(vcrypto == NULL)) {
1166                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1167                 return -ENOENT;
1168         }
1169
1170         rte_hash_free(vcrypto->session_map);
1171         rte_mempool_free(vcrypto->mbuf_pool);
1172         rte_free(vcrypto);
1173
1174         dev->extern_data = NULL;
1175         dev->extern_ops.pre_msg_handle = NULL;
1176         dev->extern_ops.post_msg_handle = NULL;
1177
1178         return 0;
1179 }
1180
1181 int __rte_experimental
1182 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1183 {
1184         struct virtio_net *dev = get_device(vid);
1185         struct vhost_crypto *vcrypto;
1186
1187         if (unlikely(dev == NULL)) {
1188                 VC_LOG_ERR("Invalid vid %i", vid);
1189                 return -EINVAL;
1190         }
1191
1192         if (unlikely(option < 0 || option >=
1193                         RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1194                 VC_LOG_ERR("Invalid option %i", option);
1195                 return -EINVAL;
1196         }
1197
1198         vcrypto = (struct vhost_crypto *)dev->extern_data;
1199         if (unlikely(vcrypto == NULL)) {
1200                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1201                 return -ENOENT;
1202         }
1203
1204         if (vcrypto->option == (uint8_t)option)
1205                 return 0;
1206
1207         if (!(rte_mempool_full(vcrypto->mbuf_pool))) {
1208                 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1209                 return -EINVAL;
1210         }
1211
1212         vcrypto->option = (uint8_t)option;
1213
1214         return 0;
1215 }
1216
1217 uint16_t __rte_experimental
1218 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1219                 struct rte_crypto_op **ops, uint16_t nb_ops)
1220 {
1221         struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1222         struct virtio_net *dev = get_device(vid);
1223         struct rte_vhost_memory *mem;
1224         struct vhost_crypto *vcrypto;
1225         struct vhost_virtqueue *vq;
1226         uint16_t avail_idx;
1227         uint16_t start_idx;
1228         uint16_t required;
1229         uint16_t count;
1230         uint16_t i;
1231
1232         if (unlikely(dev == NULL)) {
1233                 VC_LOG_ERR("Invalid vid %i", vid);
1234                 return -EINVAL;
1235         }
1236
1237         if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1238                 VC_LOG_ERR("Invalid qid %u", qid);
1239                 return -EINVAL;
1240         }
1241
1242         vcrypto = (struct vhost_crypto *)dev->extern_data;
1243         if (unlikely(vcrypto == NULL)) {
1244                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1245                 return -ENOENT;
1246         }
1247
1248         vq = dev->virtqueue[qid];
1249         mem = dev->mem;
1250
1251         avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1252         start_idx = vq->last_used_idx;
1253         count = avail_idx - start_idx;
1254         count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1255         count = RTE_MIN(count, nb_ops);
1256
1257         if (unlikely(count == 0))
1258                 return 0;
1259
1260         /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1261          * we need only 1 mbuf as src and dst
1262          */
1263         required = count * 2;
1264         if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, (void **)mbufs,
1265                         required) < 0)) {
1266                 VC_LOG_ERR("Insufficient memory");
1267                 return -ENOMEM;
1268         }
1269
1270         for (i = 0; i < count; i++) {
1271                 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1272                 uint16_t desc_idx = vq->avail->ring[used_idx];
1273                 struct vring_desc *head = &vq->desc[desc_idx];
1274                 struct rte_crypto_op *op = ops[i];
1275
1276                 op->sym->m_src = mbufs[i * 2];
1277                 op->sym->m_dst = mbufs[i * 2 + 1];
1278                 op->sym->m_src->data_off = 0;
1279                 op->sym->m_dst->data_off = 0;
1280
1281                 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, op, head,
1282                                 desc_idx, mem)) < 0)
1283                         break;
1284         }
1285
1286         vq->last_used_idx += i;
1287
1288         return i;
1289 }
1290
1291 uint16_t __rte_experimental
1292 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1293                 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1294 {
1295         struct rte_crypto_op **tmp_ops = ops;
1296         uint16_t count = 0, left = nb_ops;
1297         int callfd;
1298         uint16_t idx = 0;
1299
1300         while (left) {
1301                 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1302                                 &callfd);
1303                 if (unlikely(count == 0))
1304                         break;
1305
1306                 tmp_ops = &tmp_ops[count];
1307                 left -= count;
1308
1309                 callfds[idx++] = callfd;
1310
1311                 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1312                         VC_LOG_ERR("Too many vqs");
1313                         break;
1314                 }
1315         }
1316
1317         *nb_callfds = idx;
1318
1319         return nb_ops - left;
1320 }