c154ef673cb5134b5692319267ad7716fbfe8a60
[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)     ((t)(uintptr_t)rte_vhost_gpa_to_vva(m, a))
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         while (desc->flags & VRING_DESC_F_NEXT)
480                 desc = &head[desc->next];
481
482         return GPA_TO_VVA(struct virtio_crypto_inhdr *, mem, desc->addr);
483 }
484
485 static __rte_always_inline int
486 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
487                 uint32_t size)
488 {
489         struct vring_desc *desc = *cur_desc;
490         int left = size;
491
492         rte_prefetch0(&head[desc->next]);
493         left -= desc->len;
494
495         while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
496                 desc = &head[desc->next];
497                 rte_prefetch0(&head[desc->next]);
498                 left -= desc->len;
499         }
500
501         if (unlikely(left < 0)) {
502                 VC_LOG_ERR("Incorrect virtio descriptor");
503                 return -1;
504         }
505
506         *cur_desc = &head[desc->next];
507         return 0;
508 }
509
510 static int
511 copy_data(void *dst_data, struct vring_desc *head, struct rte_vhost_memory *mem,
512                 struct vring_desc **cur_desc, uint32_t size)
513 {
514         struct vring_desc *desc = *cur_desc;
515         uint32_t to_copy;
516         uint8_t *data = dst_data;
517         uint8_t *src;
518         int left = size;
519
520         rte_prefetch0(&head[desc->next]);
521         to_copy = RTE_MIN(desc->len, (uint32_t)left);
522         src = GPA_TO_VVA(uint8_t *, mem, desc->addr);
523         rte_memcpy((uint8_t *)data, src, to_copy);
524         left -= to_copy;
525
526         while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
527                 desc = &head[desc->next];
528                 rte_prefetch0(&head[desc->next]);
529                 to_copy = RTE_MIN(desc->len, (uint32_t)left);
530                 src = GPA_TO_VVA(uint8_t *, mem, desc->addr);
531                 rte_memcpy(data + size - left, src, to_copy);
532                 left -= to_copy;
533         }
534
535         if (unlikely(left < 0)) {
536                 VC_LOG_ERR("Incorrect virtio descriptor");
537                 return -1;
538         }
539
540         *cur_desc = &head[desc->next];
541
542         return 0;
543 }
544
545 static __rte_always_inline void *
546 get_data_ptr(struct vring_desc *head, struct rte_vhost_memory *mem,
547                 struct vring_desc **cur_desc, uint32_t size)
548 {
549         void *data;
550
551         data = GPA_TO_VVA(void *, mem, (*cur_desc)->addr);
552         if (unlikely(!data)) {
553                 VC_LOG_ERR("Failed to get object");
554                 return NULL;
555         }
556
557         if (unlikely(move_desc(head, cur_desc, size) < 0))
558                 return NULL;
559
560         return data;
561 }
562
563 static int
564 write_back_data(struct rte_crypto_op *op, struct vhost_crypto_data_req *vc_req)
565 {
566         struct rte_mbuf *mbuf = op->sym->m_dst;
567         struct vring_desc *head = vc_req->head;
568         struct rte_vhost_memory *mem = vc_req->mem;
569         struct vring_desc *desc = vc_req->wb_desc;
570         int left = vc_req->wb_len;
571         uint32_t to_write;
572         uint8_t *src_data = mbuf->buf_addr, *dst;
573
574         rte_prefetch0(&head[desc->next]);
575         to_write = RTE_MIN(desc->len, (uint32_t)left);
576         dst = GPA_TO_VVA(uint8_t *, mem, desc->addr);
577         rte_memcpy(dst, src_data, to_write);
578         left -= to_write;
579         src_data += to_write;
580
581         while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
582                 desc = &head[desc->next];
583                 rte_prefetch0(&head[desc->next]);
584                 to_write = RTE_MIN(desc->len, (uint32_t)left);
585                 dst = GPA_TO_VVA(uint8_t *, mem, desc->addr);
586                 rte_memcpy(dst, src_data, to_write);
587                 left -= to_write;
588                 src_data += to_write;
589         }
590
591         if (unlikely(left < 0)) {
592                 VC_LOG_ERR("Incorrect virtio descriptor");
593                 return -1;
594         }
595
596         return 0;
597 }
598
599 static uint8_t
600 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
601                 struct vhost_crypto_data_req *vc_req,
602                 struct virtio_crypto_cipher_data_req *cipher,
603                 struct vring_desc *cur_desc)
604 {
605         struct vring_desc *head = vc_req->head;
606         struct vring_desc *desc = cur_desc;
607         struct rte_vhost_memory *mem = vc_req->mem;
608         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
609         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
610         uint8_t ret = 0;
611
612         /* prepare */
613         /* iv */
614         if (unlikely(copy_data(iv_data, head, mem, &desc,
615                         cipher->para.iv_len) < 0)) {
616                 ret = VIRTIO_CRYPTO_BADMSG;
617                 goto error_exit;
618         }
619
620         m_src->data_len = cipher->para.src_data_len;
621
622         switch (vcrypto->option) {
623         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
624                 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
625                                 cipher->para.src_data_len);
626                 m_src->buf_addr = get_data_ptr(head, mem, &desc,
627                                 cipher->para.src_data_len);
628                 if (unlikely(m_src->buf_iova == 0 ||
629                                 m_src->buf_addr == NULL)) {
630                         VC_LOG_ERR("zero_copy may fail due to cross page data");
631                         ret = VIRTIO_CRYPTO_ERR;
632                         goto error_exit;
633                 }
634                 break;
635         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
636                 if (unlikely(cipher->para.src_data_len >
637                                 RTE_MBUF_DEFAULT_BUF_SIZE)) {
638                         VC_LOG_ERR("Not enough space to do data copy");
639                         ret = VIRTIO_CRYPTO_ERR;
640                         goto error_exit;
641                 }
642                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), head,
643                                 mem, &desc, cipher->para.src_data_len))
644                                 < 0) {
645                         ret = VIRTIO_CRYPTO_BADMSG;
646                         goto error_exit;
647                 }
648                 break;
649         default:
650                 ret = VIRTIO_CRYPTO_BADMSG;
651                 goto error_exit;
652         }
653
654         /* dst */
655         desc = find_write_desc(head, desc);
656         if (unlikely(!desc)) {
657                 VC_LOG_ERR("Cannot find write location");
658                 ret = VIRTIO_CRYPTO_BADMSG;
659                 goto error_exit;
660         }
661
662         switch (vcrypto->option) {
663         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
664                 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
665                                 desc->addr, cipher->para.dst_data_len);
666                 m_dst->buf_addr = get_data_ptr(head, mem, &desc,
667                                 cipher->para.dst_data_len);
668                 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
669                         VC_LOG_ERR("zero_copy may fail due to cross page data");
670                         ret = VIRTIO_CRYPTO_ERR;
671                         goto error_exit;
672                 }
673
674                 m_dst->data_len = cipher->para.dst_data_len;
675                 break;
676         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
677                 vc_req->wb_desc = desc;
678                 vc_req->wb_len = cipher->para.dst_data_len;
679                 if (unlikely(move_desc(head, &desc, vc_req->wb_len) < 0)) {
680                         ret = VIRTIO_CRYPTO_ERR;
681                         goto error_exit;
682                 }
683                 break;
684         default:
685                 ret = VIRTIO_CRYPTO_BADMSG;
686                 goto error_exit;
687         }
688
689         /* src data */
690         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
691         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
692
693         op->sym->cipher.data.offset = 0;
694         op->sym->cipher.data.length = cipher->para.src_data_len;
695
696         vc_req->inhdr = get_data_ptr(head, mem, &desc, INHDR_LEN);
697         if (unlikely(vc_req->inhdr == NULL)) {
698                 ret = VIRTIO_CRYPTO_BADMSG;
699                 goto error_exit;
700         }
701
702         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
703         vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
704
705         return 0;
706
707 error_exit:
708         vc_req->len = INHDR_LEN;
709         return ret;
710 }
711
712 static uint8_t
713 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
714                 struct vhost_crypto_data_req *vc_req,
715                 struct virtio_crypto_alg_chain_data_req *chain,
716                 struct vring_desc *cur_desc)
717 {
718         struct vring_desc *head = vc_req->head;
719         struct vring_desc *desc = cur_desc;
720         struct rte_vhost_memory *mem = vc_req->mem;
721         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
722         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
723         uint32_t digest_offset;
724         void *digest_addr;
725         uint8_t ret = 0;
726
727         /* prepare */
728         /* iv */
729         if (unlikely(copy_data(iv_data, head, mem, &desc,
730                         chain->para.iv_len) < 0)) {
731                 ret = VIRTIO_CRYPTO_BADMSG;
732                 goto error_exit;
733         }
734
735         m_src->data_len = chain->para.src_data_len;
736         m_dst->data_len = chain->para.dst_data_len;
737
738         switch (vcrypto->option) {
739         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
740                 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
741                                 chain->para.src_data_len);
742                 m_src->buf_addr = get_data_ptr(head, mem, &desc,
743                                 chain->para.src_data_len);
744                 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
745                         VC_LOG_ERR("zero_copy may fail due to cross page data");
746                         ret = VIRTIO_CRYPTO_ERR;
747                         goto error_exit;
748                 }
749                 break;
750         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
751                 if (unlikely(chain->para.src_data_len >
752                                 RTE_MBUF_DEFAULT_BUF_SIZE)) {
753                         VC_LOG_ERR("Not enough space to do data copy");
754                         ret = VIRTIO_CRYPTO_ERR;
755                         goto error_exit;
756                 }
757                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), head,
758                                 mem, &desc, chain->para.src_data_len)) < 0) {
759                         ret = VIRTIO_CRYPTO_BADMSG;
760                         goto error_exit;
761                 }
762                 break;
763         default:
764                 ret = VIRTIO_CRYPTO_BADMSG;
765                 goto error_exit;
766         }
767
768         /* dst */
769         desc = find_write_desc(head, desc);
770         if (unlikely(!desc)) {
771                 VC_LOG_ERR("Cannot find write location");
772                 ret = VIRTIO_CRYPTO_BADMSG;
773                 goto error_exit;
774         }
775
776         switch (vcrypto->option) {
777         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
778                 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
779                                 desc->addr, chain->para.dst_data_len);
780                 m_dst->buf_addr = get_data_ptr(head, mem, &desc,
781                                 chain->para.dst_data_len);
782                 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
783                         VC_LOG_ERR("zero_copy may fail due to cross page data");
784                         ret = VIRTIO_CRYPTO_ERR;
785                         goto error_exit;
786                 }
787
788                 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
789                                 desc->addr, chain->para.hash_result_len);
790                 op->sym->auth.digest.data = get_data_ptr(head, mem, &desc,
791                                 chain->para.hash_result_len);
792                 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
793                         VC_LOG_ERR("zero_copy may fail due to cross page data");
794                         ret = VIRTIO_CRYPTO_ERR;
795                         goto error_exit;
796                 }
797                 break;
798         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
799                 digest_offset = m_dst->data_len;
800                 digest_addr = rte_pktmbuf_mtod_offset(m_dst, void *,
801                                 digest_offset);
802
803                 vc_req->wb_desc = desc;
804                 vc_req->wb_len = m_dst->data_len + chain->para.hash_result_len;
805
806                 if (unlikely(move_desc(head, &desc,
807                                 chain->para.dst_data_len) < 0)) {
808                         ret = VIRTIO_CRYPTO_BADMSG;
809                         goto error_exit;
810                 }
811
812                 if (unlikely(copy_data(digest_addr, head, mem, &desc,
813                                 chain->para.hash_result_len)) < 0) {
814                         ret = VIRTIO_CRYPTO_BADMSG;
815                         goto error_exit;
816                 }
817
818                 op->sym->auth.digest.data = digest_addr;
819                 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_dst,
820                                 digest_offset);
821                 if (unlikely(move_desc(head, &desc,
822                                 chain->para.hash_result_len) < 0)) {
823                         ret = VIRTIO_CRYPTO_ERR;
824                         goto error_exit;
825                 }
826                 break;
827         default:
828                 ret = VIRTIO_CRYPTO_BADMSG;
829                 goto error_exit;
830         }
831
832         /* record inhdr */
833         vc_req->inhdr = get_data_ptr(head, mem, &desc, INHDR_LEN);
834         if (unlikely(vc_req->inhdr == NULL)) {
835                 ret = VIRTIO_CRYPTO_BADMSG;
836                 goto error_exit;
837         }
838
839         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
840
841         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
842         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
843
844         op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
845         op->sym->cipher.data.length = chain->para.src_data_len -
846                         chain->para.cipher_start_src_offset;
847
848         op->sym->auth.data.offset = chain->para.hash_start_src_offset;
849         op->sym->auth.data.length = chain->para.len_to_hash;
850
851         vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
852                         INHDR_LEN;
853         return 0;
854
855 error_exit:
856         vc_req->len = INHDR_LEN;
857         return ret;
858 }
859
860 /**
861  * Process on descriptor
862  */
863 static __rte_always_inline int
864 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
865                 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
866                 struct vring_desc *head, uint16_t desc_idx,
867                 struct rte_vhost_memory *mem)
868 {
869         struct vhost_crypto_data_req *vc_req = RTE_PTR_ADD(op->sym->m_src,
870                         sizeof(struct rte_mbuf));
871         struct rte_cryptodev_sym_session *session;
872         struct virtio_crypto_op_data_req *req;
873         struct virtio_crypto_inhdr *inhdr;
874         struct vring_desc *desc = NULL;
875         uint64_t session_id;
876         int err = 0;
877
878         vc_req->desc_idx = desc_idx;
879
880         if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
881                 head = GPA_TO_VVA(struct vring_desc *, mem, head->addr);
882                 if (unlikely(!head))
883                         return 0;
884                 desc_idx = 0;
885         }
886
887         desc = head;
888
889         vc_req->mem = mem;
890         vc_req->head = head;
891         vc_req->vq = vq;
892
893         vc_req->zero_copy = vcrypto->option;
894
895         req = get_data_ptr(head, mem, &desc, sizeof(*req));
896         if (unlikely(req == NULL)) {
897                 err = VIRTIO_CRYPTO_ERR;
898                 VC_LOG_ERR("Invalid descriptor");
899                 goto error_exit;
900         }
901
902         switch (req->header.opcode) {
903         case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
904         case VIRTIO_CRYPTO_CIPHER_DECRYPT:
905                 session_id = req->header.session_id;
906
907                 /* one branch to avoid unnecessary table lookup */
908                 if (vcrypto->cache_session_id != session_id) {
909                         err = rte_hash_lookup_data(vcrypto->session_map,
910                                         &session_id, (void **)&session);
911                         if (unlikely(err < 0)) {
912                                 err = VIRTIO_CRYPTO_ERR;
913                                 VC_LOG_ERR("Failed to find session %"PRIu64,
914                                                 session_id);
915                                 goto error_exit;
916                         }
917
918                         vcrypto->cache_session = session;
919                         vcrypto->cache_session_id = session_id;
920                 }
921
922                 session = vcrypto->cache_session;
923
924                 err = rte_crypto_op_attach_sym_session(op, session);
925                 if (unlikely(err < 0)) {
926                         err = VIRTIO_CRYPTO_ERR;
927                         VC_LOG_ERR("Failed to attach session to op");
928                         goto error_exit;
929                 }
930
931                 switch (req->u.sym_req.op_type) {
932                 case VIRTIO_CRYPTO_SYM_OP_NONE:
933                         err = VIRTIO_CRYPTO_NOTSUPP;
934                         break;
935                 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
936                         err = prepare_sym_cipher_op(vcrypto, op, vc_req,
937                                         &req->u.sym_req.u.cipher, desc);
938                         break;
939                 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
940                         err = prepare_sym_chain_op(vcrypto, op, vc_req,
941                                         &req->u.sym_req.u.chain, desc);
942                         break;
943                 }
944                 if (unlikely(err != 0)) {
945                         VC_LOG_ERR("Failed to process sym request");
946                         goto error_exit;
947                 }
948                 break;
949         default:
950                 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
951                                 req->header.opcode);
952                 goto error_exit;
953         }
954
955         return 0;
956
957 error_exit:
958
959         inhdr = reach_inhdr(head, mem, desc);
960         if (likely(inhdr != NULL))
961                 inhdr->status = (uint8_t)err;
962
963         return -1;
964 }
965
966 static __rte_always_inline struct vhost_virtqueue *
967 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
968                 struct vhost_virtqueue *old_vq)
969 {
970         struct rte_mbuf *m_src = op->sym->m_src;
971         struct rte_mbuf *m_dst = op->sym->m_dst;
972         struct vhost_crypto_data_req *vc_req = RTE_PTR_ADD(m_src,
973                         sizeof(struct rte_mbuf));
974         uint16_t desc_idx;
975         int ret = 0;
976
977         if (unlikely(!vc_req)) {
978                 VC_LOG_ERR("Failed to retrieve vc_req");
979                 return NULL;
980         }
981
982         if (old_vq && (vc_req->vq != old_vq))
983                 return vc_req->vq;
984
985         desc_idx = vc_req->desc_idx;
986
987         if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
988                 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
989         else {
990                 if (vc_req->zero_copy == 0) {
991                         ret = write_back_data(op, vc_req);
992                         if (unlikely(ret != 0))
993                                 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
994                 }
995         }
996
997         vc_req->vq->used->ring[desc_idx].id = desc_idx;
998         vc_req->vq->used->ring[desc_idx].len = vc_req->len;
999
1000         rte_mempool_put(m_dst->pool, (void *)m_dst);
1001         rte_mempool_put(m_src->pool, (void *)m_src);
1002
1003         return vc_req->vq;
1004 }
1005
1006 static __rte_always_inline uint16_t
1007 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1008                 uint16_t nb_ops, int *callfd)
1009 {
1010         uint16_t processed = 1;
1011         struct vhost_virtqueue *vq, *tmp_vq;
1012
1013         if (unlikely(nb_ops == 0))
1014                 return 0;
1015
1016         vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1017         if (unlikely(vq == NULL))
1018                 return 0;
1019         tmp_vq = vq;
1020
1021         while ((processed < nb_ops)) {
1022                 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1023                                 tmp_vq);
1024
1025                 if (unlikely(vq != tmp_vq))
1026                         break;
1027
1028                 processed++;
1029         }
1030
1031         *callfd = vq->callfd;
1032
1033         *(volatile uint16_t *)&vq->used->idx += processed;
1034
1035         return processed;
1036 }
1037
1038 int __rte_experimental
1039 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1040                 struct rte_mempool *sess_pool, int socket_id)
1041 {
1042         struct virtio_net *dev = get_device(vid);
1043         struct rte_hash_parameters params = {0};
1044         struct vhost_crypto *vcrypto;
1045         char name[128];
1046         int ret;
1047
1048         if (!dev) {
1049                 VC_LOG_ERR("Invalid vid %i", vid);
1050                 return -EINVAL;
1051         }
1052
1053         ret = rte_vhost_driver_set_features(dev->ifname,
1054                         VIRTIO_CRYPTO_FEATURES);
1055         if (ret < 0) {
1056                 VC_LOG_ERR("Error setting features");
1057                 return -1;
1058         }
1059
1060         vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1061                         RTE_CACHE_LINE_SIZE, socket_id);
1062         if (!vcrypto) {
1063                 VC_LOG_ERR("Insufficient memory");
1064                 return -ENOMEM;
1065         }
1066
1067         vcrypto->sess_pool = sess_pool;
1068         vcrypto->cid = cryptodev_id;
1069         vcrypto->cache_session_id = UINT64_MAX;
1070         vcrypto->last_session_id = 1;
1071         vcrypto->dev = dev;
1072         vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1073
1074         snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1075         params.name = name;
1076         params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1077         params.hash_func = rte_jhash;
1078         params.key_len = sizeof(uint64_t);
1079         params.socket_id = socket_id;
1080         vcrypto->session_map = rte_hash_create(&params);
1081         if (!vcrypto->session_map) {
1082                 VC_LOG_ERR("Failed to creath session map");
1083                 ret = -ENOMEM;
1084                 goto error_exit;
1085         }
1086
1087         snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1088         vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1089                         VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1090                         sizeof(struct vhost_crypto_data_req),
1091                         RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1092                         rte_socket_id());
1093         if (!vcrypto->mbuf_pool) {
1094                 VC_LOG_ERR("Failed to creath mbuf pool");
1095                 ret = -ENOMEM;
1096                 goto error_exit;
1097         }
1098
1099         dev->extern_data = vcrypto;
1100         dev->extern_ops.pre_msg_handle = NULL;
1101         dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1102
1103         return 0;
1104
1105 error_exit:
1106         if (vcrypto->session_map)
1107                 rte_hash_free(vcrypto->session_map);
1108         if (vcrypto->mbuf_pool)
1109                 rte_mempool_free(vcrypto->mbuf_pool);
1110
1111         rte_free(vcrypto);
1112
1113         return ret;
1114 }
1115
1116 int __rte_experimental
1117 rte_vhost_crypto_free(int vid)
1118 {
1119         struct virtio_net *dev = get_device(vid);
1120         struct vhost_crypto *vcrypto;
1121
1122         if (unlikely(dev == NULL)) {
1123                 VC_LOG_ERR("Invalid vid %i", vid);
1124                 return -EINVAL;
1125         }
1126
1127         vcrypto = dev->extern_data;
1128         if (unlikely(vcrypto == NULL)) {
1129                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1130                 return -ENOENT;
1131         }
1132
1133         rte_hash_free(vcrypto->session_map);
1134         rte_mempool_free(vcrypto->mbuf_pool);
1135         rte_free(vcrypto);
1136
1137         dev->extern_data = NULL;
1138         dev->extern_ops.pre_msg_handle = NULL;
1139         dev->extern_ops.post_msg_handle = NULL;
1140
1141         return 0;
1142 }
1143
1144 int __rte_experimental
1145 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1146 {
1147         struct virtio_net *dev = get_device(vid);
1148         struct vhost_crypto *vcrypto;
1149
1150         if (unlikely(dev == NULL)) {
1151                 VC_LOG_ERR("Invalid vid %i", vid);
1152                 return -EINVAL;
1153         }
1154
1155         if (unlikely(option < 0 || option >=
1156                         RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1157                 VC_LOG_ERR("Invalid option %i", option);
1158                 return -EINVAL;
1159         }
1160
1161         vcrypto = (struct vhost_crypto *)dev->extern_data;
1162         if (unlikely(vcrypto == NULL)) {
1163                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1164                 return -ENOENT;
1165         }
1166
1167         if (vcrypto->option == (uint8_t)option)
1168                 return 0;
1169
1170         if (!(rte_mempool_full(vcrypto->mbuf_pool))) {
1171                 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1172                 return -EINVAL;
1173         }
1174
1175         vcrypto->option = (uint8_t)option;
1176
1177         return 0;
1178 }
1179
1180 uint16_t __rte_experimental
1181 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1182                 struct rte_crypto_op **ops, uint16_t nb_ops)
1183 {
1184         struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1185         struct virtio_net *dev = get_device(vid);
1186         struct rte_vhost_memory *mem;
1187         struct vhost_crypto *vcrypto;
1188         struct vhost_virtqueue *vq;
1189         uint16_t avail_idx;
1190         uint16_t start_idx;
1191         uint16_t required;
1192         uint16_t count;
1193         uint16_t i;
1194
1195         if (unlikely(dev == NULL)) {
1196                 VC_LOG_ERR("Invalid vid %i", vid);
1197                 return -EINVAL;
1198         }
1199
1200         if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1201                 VC_LOG_ERR("Invalid qid %u", qid);
1202                 return -EINVAL;
1203         }
1204
1205         vcrypto = (struct vhost_crypto *)dev->extern_data;
1206         if (unlikely(vcrypto == NULL)) {
1207                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1208                 return -ENOENT;
1209         }
1210
1211         vq = dev->virtqueue[qid];
1212         mem = dev->mem;
1213
1214         avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1215         start_idx = vq->last_used_idx;
1216         count = avail_idx - start_idx;
1217         count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1218         count = RTE_MIN(count, nb_ops);
1219
1220         if (unlikely(count == 0))
1221                 return 0;
1222
1223         /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1224          * we need only 1 mbuf as src and dst
1225          */
1226         required = count * 2;
1227         if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, (void **)mbufs,
1228                         required) < 0)) {
1229                 VC_LOG_ERR("Insufficient memory");
1230                 return -ENOMEM;
1231         }
1232
1233         for (i = 0; i < count; i++) {
1234                 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1235                 uint16_t desc_idx = vq->avail->ring[used_idx];
1236                 struct vring_desc *head = &vq->desc[desc_idx];
1237                 struct rte_crypto_op *op = ops[i];
1238
1239                 op->sym->m_src = mbufs[i * 2];
1240                 op->sym->m_dst = mbufs[i * 2 + 1];
1241                 op->sym->m_src->data_off = 0;
1242                 op->sym->m_dst->data_off = 0;
1243
1244                 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, op, head,
1245                                 desc_idx, mem)) < 0)
1246                         break;
1247         }
1248
1249         vq->last_used_idx += i;
1250
1251         return i;
1252 }
1253
1254 uint16_t __rte_experimental
1255 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1256                 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1257 {
1258         struct rte_crypto_op **tmp_ops = ops;
1259         uint16_t count = 0, left = nb_ops;
1260         int callfd;
1261         uint16_t idx = 0;
1262
1263         while (left) {
1264                 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1265                                 &callfd);
1266                 if (unlikely(count == 0))
1267                         break;
1268
1269                 tmp_ops = &tmp_ops[count];
1270                 left -= count;
1271
1272                 callfds[idx++] = callfd;
1273
1274                 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1275                         VC_LOG_ERR("Too many vqs");
1276                         break;
1277                 }
1278         }
1279
1280         *nb_callfds = idx;
1281
1282         return nb_ops - left;
1283 }