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