vhost/crypto: fix possible dead loop
[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         struct rte_mempool *sess_priv_pool;
202         struct rte_mempool *wb_pool;
203
204         /** DPDK cryptodev ID */
205         uint8_t cid;
206         uint16_t nb_qps;
207
208         uint64_t last_session_id;
209
210         uint64_t cache_session_id;
211         struct rte_cryptodev_sym_session *cache_session;
212         /** socket id for the device */
213         int socket_id;
214
215         struct virtio_net *dev;
216
217         uint8_t option;
218 } __rte_cache_aligned;
219
220 struct vhost_crypto_writeback_data {
221         uint8_t *src;
222         uint8_t *dst;
223         uint64_t len;
224         struct vhost_crypto_writeback_data *next;
225 };
226
227 struct vhost_crypto_data_req {
228         struct vring_desc *head;
229         struct virtio_net *dev;
230         struct virtio_crypto_inhdr *inhdr;
231         struct vhost_virtqueue *vq;
232         struct vhost_crypto_writeback_data *wb;
233         struct rte_mempool *wb_pool;
234         uint16_t desc_idx;
235         uint16_t len;
236         uint16_t zero_copy;
237 };
238
239 static int
240 transform_cipher_param(struct rte_crypto_sym_xform *xform,
241                 VhostUserCryptoSessionParam *param)
242 {
243         int ret;
244
245         ret = cipher_algo_transform(param->cipher_algo);
246         if (unlikely(ret < 0))
247                 return ret;
248
249         xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
250         xform->cipher.algo = (enum rte_crypto_cipher_algorithm)ret;
251         xform->cipher.key.length = param->cipher_key_len;
252         if (xform->cipher.key.length > 0)
253                 xform->cipher.key.data = param->cipher_key_buf;
254         if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
255                 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
256         else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
257                 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
258         else {
259                 VC_LOG_DBG("Bad operation type");
260                 return -VIRTIO_CRYPTO_BADMSG;
261         }
262
263         ret = get_iv_len(xform->cipher.algo);
264         if (unlikely(ret < 0))
265                 return ret;
266         xform->cipher.iv.length = (uint16_t)ret;
267         xform->cipher.iv.offset = IV_OFFSET;
268         return 0;
269 }
270
271 static int
272 transform_chain_param(struct rte_crypto_sym_xform *xforms,
273                 VhostUserCryptoSessionParam *param)
274 {
275         struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
276         int ret;
277
278         switch (param->chaining_dir) {
279         case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
280                 xform_auth = xforms;
281                 xform_cipher = xforms->next;
282                 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
283                 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
284                 break;
285         case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
286                 xform_cipher = xforms;
287                 xform_auth = xforms->next;
288                 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
289                 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
290                 break;
291         default:
292                 return -VIRTIO_CRYPTO_BADMSG;
293         }
294
295         /* cipher */
296         ret = cipher_algo_transform(param->cipher_algo);
297         if (unlikely(ret < 0))
298                 return ret;
299         xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
300         xform_cipher->cipher.algo = (enum rte_crypto_cipher_algorithm)ret;
301         xform_cipher->cipher.key.length = param->cipher_key_len;
302         xform_cipher->cipher.key.data = param->cipher_key_buf;
303         ret = get_iv_len(xform_cipher->cipher.algo);
304         if (unlikely(ret < 0))
305                 return ret;
306         xform_cipher->cipher.iv.length = (uint16_t)ret;
307         xform_cipher->cipher.iv.offset = IV_OFFSET;
308
309         /* auth */
310         xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
311         ret = auth_algo_transform(param->hash_algo);
312         if (unlikely(ret < 0))
313                 return ret;
314         xform_auth->auth.algo = (enum rte_crypto_auth_algorithm)ret;
315         xform_auth->auth.digest_length = param->digest_len;
316         xform_auth->auth.key.length = param->auth_key_len;
317         xform_auth->auth.key.data = param->auth_key_buf;
318
319         return 0;
320 }
321
322 static void
323 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
324                 VhostUserCryptoSessionParam *sess_param)
325 {
326         struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
327         struct rte_cryptodev_sym_session *session;
328         int ret;
329
330         switch (sess_param->op_type) {
331         case VIRTIO_CRYPTO_SYM_OP_NONE:
332         case VIRTIO_CRYPTO_SYM_OP_CIPHER:
333                 ret = transform_cipher_param(&xform1, sess_param);
334                 if (unlikely(ret)) {
335                         VC_LOG_ERR("Error transform session msg (%i)", ret);
336                         sess_param->session_id = ret;
337                         return;
338                 }
339                 break;
340         case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
341                 if (unlikely(sess_param->hash_mode !=
342                                 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
343                         sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
344                         VC_LOG_ERR("Error transform session message (%i)",
345                                         -VIRTIO_CRYPTO_NOTSUPP);
346                         return;
347                 }
348
349                 xform1.next = &xform2;
350
351                 ret = transform_chain_param(&xform1, sess_param);
352                 if (unlikely(ret)) {
353                         VC_LOG_ERR("Error transform session message (%i)", ret);
354                         sess_param->session_id = ret;
355                         return;
356                 }
357
358                 break;
359         default:
360                 VC_LOG_ERR("Algorithm not yet supported");
361                 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
362                 return;
363         }
364
365         session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
366         if (!session) {
367                 VC_LOG_ERR("Failed to create session");
368                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
369                 return;
370         }
371
372         if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
373                         vcrypto->sess_priv_pool) < 0) {
374                 VC_LOG_ERR("Failed to initialize session");
375                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
376                 return;
377         }
378
379         /* insert hash to map */
380         if (rte_hash_add_key_data(vcrypto->session_map,
381                         &vcrypto->last_session_id, session) < 0) {
382                 VC_LOG_ERR("Failed to insert session to hash table");
383
384                 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
385                         VC_LOG_ERR("Failed to clear session");
386                 else {
387                         if (rte_cryptodev_sym_session_free(session) < 0)
388                                 VC_LOG_ERR("Failed to free session");
389                 }
390                 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
391                 return;
392         }
393
394         VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
395                         vcrypto->last_session_id, vcrypto->dev->vid);
396
397         sess_param->session_id = vcrypto->last_session_id;
398         vcrypto->last_session_id++;
399 }
400
401 static int
402 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
403 {
404         struct rte_cryptodev_sym_session *session;
405         uint64_t sess_id = session_id;
406         int ret;
407
408         ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
409                         (void **)&session);
410
411         if (unlikely(ret < 0)) {
412                 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
413                 return -VIRTIO_CRYPTO_INVSESS;
414         }
415
416         if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
417                 VC_LOG_DBG("Failed to clear session");
418                 return -VIRTIO_CRYPTO_ERR;
419         }
420
421         if (rte_cryptodev_sym_session_free(session) < 0) {
422                 VC_LOG_DBG("Failed to free session");
423                 return -VIRTIO_CRYPTO_ERR;
424         }
425
426         if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
427                 VC_LOG_DBG("Failed to delete session from hash table.");
428                 return -VIRTIO_CRYPTO_ERR;
429         }
430
431         VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
432                         vcrypto->dev->vid);
433
434         return 0;
435 }
436
437 static enum vh_result
438 vhost_crypto_msg_post_handler(int vid, void *msg)
439 {
440         struct virtio_net *dev = get_device(vid);
441         struct vhost_crypto *vcrypto;
442         VhostUserMsg *vmsg = msg;
443         enum vh_result ret = VH_RESULT_OK;
444
445         if (dev == NULL) {
446                 VC_LOG_ERR("Invalid vid %i", vid);
447                 return VH_RESULT_ERR;
448         }
449
450         vcrypto = dev->extern_data;
451         if (vcrypto == NULL) {
452                 VC_LOG_ERR("Cannot find required data, is it initialized?");
453                 return VH_RESULT_ERR;
454         }
455
456         if (vmsg->request.master == VHOST_USER_CRYPTO_CREATE_SESS) {
457                 vhost_crypto_create_sess(vcrypto,
458                                 &vmsg->payload.crypto_session);
459                 vmsg->fd_num = 0;
460                 ret = VH_RESULT_REPLY;
461         } else if (vmsg->request.master == VHOST_USER_CRYPTO_CLOSE_SESS) {
462                 if (vhost_crypto_close_sess(vcrypto, vmsg->payload.u64))
463                         ret = VH_RESULT_ERR;
464         }
465
466         return ret;
467 }
468
469 static __rte_always_inline struct vring_desc *
470 find_write_desc(struct vring_desc *head, struct vring_desc *desc,
471                 uint32_t *nb_descs)
472 {
473         if (desc->flags & VRING_DESC_F_WRITE)
474                 return desc;
475
476         while (desc->flags & VRING_DESC_F_NEXT) {
477                 if (unlikely(*nb_descs == 0))
478                         return NULL;
479                 (*nb_descs)--;
480
481                 desc = &head[desc->next];
482                 if (desc->flags & VRING_DESC_F_WRITE)
483                         return desc;
484         }
485
486         return NULL;
487 }
488
489 static struct virtio_crypto_inhdr *
490 reach_inhdr(struct vhost_crypto_data_req *vc_req, struct vring_desc *desc,
491                 uint32_t *nb_descs)
492 {
493         uint64_t dlen;
494         struct virtio_crypto_inhdr *inhdr;
495
496         while (desc->flags & VRING_DESC_F_NEXT) {
497                 if (unlikely(*nb_descs == 0))
498                         return NULL;
499                 (*nb_descs)--;
500                 desc = &vc_req->head[desc->next];
501         }
502
503         dlen = desc->len;
504         inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, desc->addr,
505                         &dlen, VHOST_ACCESS_WO);
506         if (unlikely(!inhdr || dlen != desc->len))
507                 return NULL;
508
509         return inhdr;
510 }
511
512 static __rte_always_inline int
513 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
514                 uint32_t size, uint32_t *nb_descs)
515 {
516         struct vring_desc *desc = *cur_desc;
517         int left = size - desc->len;
518
519         while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
520                 (*nb_descs)--;
521                 if (unlikely(*nb_descs == 0))
522                         return -1;
523
524                 desc = &head[desc->next];
525                 rte_prefetch0(&head[desc->next]);
526                 left -= desc->len;
527         }
528
529         if (unlikely(left > 0))
530                 return -1;
531
532         if (unlikely(*nb_descs == 0))
533                 *cur_desc = NULL;
534         else
535                 *cur_desc = &head[desc->next];
536         return 0;
537 }
538
539 static __rte_always_inline void *
540 get_data_ptr(struct vhost_crypto_data_req *vc_req, struct vring_desc *cur_desc,
541                 uint8_t perm)
542 {
543         void *data;
544         uint64_t dlen = cur_desc->len;
545
546         data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
547         if (unlikely(!data || dlen != cur_desc->len)) {
548                 VC_LOG_ERR("Failed to map object");
549                 return NULL;
550         }
551
552         return data;
553 }
554
555 static int
556 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
557                 struct vring_desc **cur_desc, uint32_t size, uint32_t *nb_descs)
558 {
559         struct vring_desc *desc = *cur_desc;
560         uint64_t remain, addr, dlen, len;
561         uint32_t to_copy;
562         uint8_t *data = dst_data;
563         uint8_t *src;
564         int left = size;
565
566         to_copy = RTE_MIN(desc->len, (uint32_t)left);
567         dlen = to_copy;
568         src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
569                         VHOST_ACCESS_RO);
570         if (unlikely(!src || !dlen))
571                 return -1;
572
573         rte_memcpy((uint8_t *)data, src, dlen);
574         data += dlen;
575
576         if (unlikely(dlen < to_copy)) {
577                 remain = to_copy - dlen;
578                 addr = desc->addr + dlen;
579
580                 while (remain) {
581                         len = remain;
582                         src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
583                                         VHOST_ACCESS_RO);
584                         if (unlikely(!src || !len)) {
585                                 VC_LOG_ERR("Failed to map descriptor");
586                                 return -1;
587                         }
588
589                         rte_memcpy(data, src, len);
590                         addr += len;
591                         remain -= len;
592                         data += len;
593                 }
594         }
595
596         left -= to_copy;
597
598         while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
599                 if (unlikely(*nb_descs == 0)) {
600                         VC_LOG_ERR("Invalid descriptors");
601                         return -1;
602                 }
603                 (*nb_descs)--;
604
605                 desc = &vc_req->head[desc->next];
606                 rte_prefetch0(&vc_req->head[desc->next]);
607                 to_copy = RTE_MIN(desc->len, (uint32_t)left);
608                 dlen = desc->len;
609                 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
610                                 VHOST_ACCESS_RO);
611                 if (unlikely(!src || !dlen)) {
612                         VC_LOG_ERR("Failed to map descriptor");
613                         return -1;
614                 }
615
616                 rte_memcpy(data, src, dlen);
617                 data += dlen;
618
619                 if (unlikely(dlen < to_copy)) {
620                         remain = to_copy - dlen;
621                         addr = desc->addr + dlen;
622
623                         while (remain) {
624                                 len = remain;
625                                 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
626                                                 VHOST_ACCESS_RO);
627                                 if (unlikely(!src || !len)) {
628                                         VC_LOG_ERR("Failed to map descriptor");
629                                         return -1;
630                                 }
631
632                                 rte_memcpy(data, src, len);
633                                 addr += len;
634                                 remain -= len;
635                                 data += len;
636                         }
637                 }
638
639                 left -= to_copy;
640         }
641
642         if (unlikely(left > 0)) {
643                 VC_LOG_ERR("Incorrect virtio descriptor");
644                 return -1;
645         }
646
647         if (unlikely(*nb_descs == 0))
648                 *cur_desc = NULL;
649         else
650                 *cur_desc = &vc_req->head[desc->next];
651
652         return 0;
653 }
654
655 static void
656 write_back_data(struct vhost_crypto_data_req *vc_req)
657 {
658         struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
659
660         while (wb_data) {
661                 rte_prefetch0(wb_data->next);
662                 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
663                 wb_last = wb_data;
664                 wb_data = wb_data->next;
665                 rte_mempool_put(vc_req->wb_pool, wb_last);
666         }
667 }
668
669 static void
670 free_wb_data(struct vhost_crypto_writeback_data *wb_data,
671                 struct rte_mempool *mp)
672 {
673         while (wb_data->next != NULL)
674                 free_wb_data(wb_data->next, mp);
675
676         rte_mempool_put(mp, wb_data);
677 }
678
679 /**
680  * The function will allocate a vhost_crypto_writeback_data linked list
681  * containing the source and destination data pointers for the write back
682  * operation after dequeued from Cryptodev PMD queues.
683  *
684  * @param vc_req
685  *   The vhost crypto data request pointer
686  * @param cur_desc
687  *   The pointer of the current in use descriptor pointer. The content of
688  *   cur_desc is expected to be updated after the function execution.
689  * @param end_wb_data
690  *   The last write back data element to be returned. It is used only in cipher
691  *   and hash chain operations.
692  * @param src
693  *   The source data pointer
694  * @param offset
695  *   The offset to both source and destination data. For source data the offset
696  *   is the number of bytes between src and start point of cipher operation. For
697  *   destination data the offset is the number of bytes from *cur_desc->addr
698  *   to the point where the src will be written to.
699  * @param write_back_len
700  *   The size of the write back length.
701  * @return
702  *   The pointer to the start of the write back data linked list.
703  */
704 static struct vhost_crypto_writeback_data *
705 prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
706                 struct vring_desc **cur_desc,
707                 struct vhost_crypto_writeback_data **end_wb_data,
708                 uint8_t *src,
709                 uint32_t offset,
710                 uint64_t write_back_len,
711                 uint32_t *nb_descs)
712 {
713         struct vhost_crypto_writeback_data *wb_data, *head;
714         struct vring_desc *desc = *cur_desc;
715         uint64_t dlen;
716         uint8_t *dst;
717         int ret;
718
719         ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
720         if (unlikely(ret < 0)) {
721                 VC_LOG_ERR("no memory");
722                 goto error_exit;
723         }
724
725         wb_data = head;
726
727         if (likely(desc->len > offset)) {
728                 wb_data->src = src + offset;
729                 dlen = desc->len;
730                 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
731                         &dlen, VHOST_ACCESS_RW) + offset;
732                 if (unlikely(!dst || dlen != desc->len)) {
733                         VC_LOG_ERR("Failed to map descriptor");
734                         goto error_exit;
735                 }
736
737                 wb_data->dst = dst;
738                 wb_data->len = desc->len - offset;
739                 write_back_len -= wb_data->len;
740                 src += offset + wb_data->len;
741                 offset = 0;
742
743                 if (unlikely(write_back_len)) {
744                         ret = rte_mempool_get(vc_req->wb_pool,
745                                         (void **)&(wb_data->next));
746                         if (unlikely(ret < 0)) {
747                                 VC_LOG_ERR("no memory");
748                                 goto error_exit;
749                         }
750
751                         wb_data = wb_data->next;
752                 } else
753                         wb_data->next = NULL;
754         } else
755                 offset -= desc->len;
756
757         while (write_back_len) {
758                 if (unlikely(*nb_descs == 0)) {
759                         VC_LOG_ERR("Invalid descriptors");
760                         goto error_exit;
761                 }
762                 (*nb_descs)--;
763
764                 desc = &vc_req->head[desc->next];
765                 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
766                         VC_LOG_ERR("incorrect descriptor");
767                         goto error_exit;
768                 }
769
770                 if (desc->len <= offset) {
771                         offset -= desc->len;
772                         continue;
773                 }
774
775                 dlen = desc->len;
776                 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
777                                 VHOST_ACCESS_RW) + offset;
778                 if (unlikely(dst == NULL || dlen != desc->len)) {
779                         VC_LOG_ERR("Failed to map descriptor");
780                         goto error_exit;
781                 }
782
783                 wb_data->src = src;
784                 wb_data->dst = dst;
785                 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
786                 write_back_len -= wb_data->len;
787                 src += wb_data->len;
788                 offset = 0;
789
790                 if (write_back_len) {
791                         ret = rte_mempool_get(vc_req->wb_pool,
792                                         (void **)&(wb_data->next));
793                         if (unlikely(ret < 0)) {
794                                 VC_LOG_ERR("no memory");
795                                 goto error_exit;
796                         }
797
798                         wb_data = wb_data->next;
799                 } else
800                         wb_data->next = NULL;
801         }
802
803         if (unlikely(*nb_descs == 0))
804                 *cur_desc = NULL;
805         else
806                 *cur_desc = &vc_req->head[desc->next];
807
808         *end_wb_data = wb_data;
809
810         return head;
811
812 error_exit:
813         if (head)
814                 free_wb_data(head, vc_req->wb_pool);
815
816         return NULL;
817 }
818
819 static uint8_t
820 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
821                 struct vhost_crypto_data_req *vc_req,
822                 struct virtio_crypto_cipher_data_req *cipher,
823                 struct vring_desc *cur_desc,
824                 uint32_t *nb_descs)
825 {
826         struct vring_desc *desc = cur_desc;
827         struct vhost_crypto_writeback_data *ewb = NULL;
828         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
829         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
830         uint8_t ret = 0;
831
832         /* prepare */
833         /* iv */
834         if (unlikely(copy_data(iv_data, vc_req, &desc, cipher->para.iv_len,
835                         nb_descs) < 0)) {
836                 ret = VIRTIO_CRYPTO_BADMSG;
837                 goto error_exit;
838         }
839
840         m_src->data_len = cipher->para.src_data_len;
841
842         switch (vcrypto->option) {
843         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
844                 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
845                                 cipher->para.src_data_len);
846                 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
847                 if (unlikely(m_src->buf_iova == 0 ||
848                                 m_src->buf_addr == NULL)) {
849                         VC_LOG_ERR("zero_copy may fail due to cross page data");
850                         ret = VIRTIO_CRYPTO_ERR;
851                         goto error_exit;
852                 }
853
854                 if (unlikely(move_desc(vc_req->head, &desc,
855                                 cipher->para.src_data_len, nb_descs) < 0)) {
856                         VC_LOG_ERR("Incorrect descriptor");
857                         ret = VIRTIO_CRYPTO_ERR;
858                         goto error_exit;
859                 }
860
861                 break;
862         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
863                 vc_req->wb_pool = vcrypto->wb_pool;
864
865                 if (unlikely(cipher->para.src_data_len >
866                                 RTE_MBUF_DEFAULT_BUF_SIZE)) {
867                         VC_LOG_ERR("Not enough space to do data copy");
868                         ret = VIRTIO_CRYPTO_ERR;
869                         goto error_exit;
870                 }
871                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
872                                 vc_req, &desc, cipher->para.src_data_len,
873                                 nb_descs) < 0)) {
874                         ret = VIRTIO_CRYPTO_BADMSG;
875                         goto error_exit;
876                 }
877                 break;
878         default:
879                 ret = VIRTIO_CRYPTO_BADMSG;
880                 goto error_exit;
881         }
882
883         /* dst */
884         desc = find_write_desc(vc_req->head, desc, nb_descs);
885         if (unlikely(!desc)) {
886                 VC_LOG_ERR("Cannot find write location");
887                 ret = VIRTIO_CRYPTO_BADMSG;
888                 goto error_exit;
889         }
890
891         switch (vcrypto->option) {
892         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
893                 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
894                                 desc->addr, cipher->para.dst_data_len);
895                 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
896                 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
897                         VC_LOG_ERR("zero_copy may fail due to cross page data");
898                         ret = VIRTIO_CRYPTO_ERR;
899                         goto error_exit;
900                 }
901
902                 if (unlikely(move_desc(vc_req->head, &desc,
903                                 cipher->para.dst_data_len, nb_descs) < 0)) {
904                         VC_LOG_ERR("Incorrect descriptor");
905                         ret = VIRTIO_CRYPTO_ERR;
906                         goto error_exit;
907                 }
908
909                 m_dst->data_len = cipher->para.dst_data_len;
910                 break;
911         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
912                 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
913                                 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
914                                 cipher->para.dst_data_len, nb_descs);
915                 if (unlikely(vc_req->wb == NULL)) {
916                         ret = VIRTIO_CRYPTO_ERR;
917                         goto error_exit;
918                 }
919
920                 break;
921         default:
922                 ret = VIRTIO_CRYPTO_BADMSG;
923                 goto error_exit;
924         }
925
926         /* src data */
927         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
928         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
929
930         op->sym->cipher.data.offset = 0;
931         op->sym->cipher.data.length = cipher->para.src_data_len;
932
933         vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
934         if (unlikely(vc_req->inhdr == NULL)) {
935                 ret = VIRTIO_CRYPTO_BADMSG;
936                 goto error_exit;
937         }
938
939         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
940         vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
941
942         return 0;
943
944 error_exit:
945         if (vc_req->wb)
946                 free_wb_data(vc_req->wb, vc_req->wb_pool);
947
948         vc_req->len = INHDR_LEN;
949         return ret;
950 }
951
952 static uint8_t
953 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
954                 struct vhost_crypto_data_req *vc_req,
955                 struct virtio_crypto_alg_chain_data_req *chain,
956                 struct vring_desc *cur_desc,
957                 uint32_t *nb_descs)
958 {
959         struct vring_desc *desc = cur_desc, *digest_desc;
960         struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
961         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
962         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
963         uint32_t digest_offset;
964         void *digest_addr;
965         uint8_t ret = 0;
966
967         /* prepare */
968         /* iv */
969         if (unlikely(copy_data(iv_data, vc_req, &desc,
970                         chain->para.iv_len, nb_descs) < 0)) {
971                 ret = VIRTIO_CRYPTO_BADMSG;
972                 goto error_exit;
973         }
974
975         m_src->data_len = chain->para.src_data_len;
976
977         switch (vcrypto->option) {
978         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
979                 m_dst->data_len = chain->para.dst_data_len;
980
981                 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
982                                 chain->para.src_data_len);
983                 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
984                 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
985                         VC_LOG_ERR("zero_copy may fail due to cross page data");
986                         ret = VIRTIO_CRYPTO_ERR;
987                         goto error_exit;
988                 }
989
990                 if (unlikely(move_desc(vc_req->head, &desc,
991                                 chain->para.src_data_len, nb_descs) < 0)) {
992                         VC_LOG_ERR("Incorrect descriptor");
993                         ret = VIRTIO_CRYPTO_ERR;
994                         goto error_exit;
995                 }
996                 break;
997         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
998                 vc_req->wb_pool = vcrypto->wb_pool;
999
1000                 if (unlikely(chain->para.src_data_len >
1001                                 RTE_MBUF_DEFAULT_BUF_SIZE)) {
1002                         VC_LOG_ERR("Not enough space to do data copy");
1003                         ret = VIRTIO_CRYPTO_ERR;
1004                         goto error_exit;
1005                 }
1006                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1007                                 vc_req, &desc, chain->para.src_data_len,
1008                                 nb_descs)) < 0) {
1009                         ret = VIRTIO_CRYPTO_BADMSG;
1010                         goto error_exit;
1011                 }
1012
1013                 break;
1014         default:
1015                 ret = VIRTIO_CRYPTO_BADMSG;
1016                 goto error_exit;
1017         }
1018
1019         /* dst */
1020         desc = find_write_desc(vc_req->head, desc, nb_descs);
1021         if (unlikely(!desc)) {
1022                 VC_LOG_ERR("Cannot find write location");
1023                 ret = VIRTIO_CRYPTO_BADMSG;
1024                 goto error_exit;
1025         }
1026
1027         switch (vcrypto->option) {
1028         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1029                 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
1030                                 desc->addr, chain->para.dst_data_len);
1031                 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
1032                 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
1033                         VC_LOG_ERR("zero_copy may fail due to cross page data");
1034                         ret = VIRTIO_CRYPTO_ERR;
1035                         goto error_exit;
1036                 }
1037
1038                 if (unlikely(move_desc(vc_req->head, &desc,
1039                                 chain->para.dst_data_len, nb_descs) < 0)) {
1040                         VC_LOG_ERR("Incorrect descriptor");
1041                         ret = VIRTIO_CRYPTO_ERR;
1042                         goto error_exit;
1043                 }
1044
1045                 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1046                                 desc->addr, chain->para.hash_result_len);
1047                 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1048                                 VHOST_ACCESS_RW);
1049                 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1050                         VC_LOG_ERR("zero_copy may fail due to cross page data");
1051                         ret = VIRTIO_CRYPTO_ERR;
1052                         goto error_exit;
1053                 }
1054
1055                 if (unlikely(move_desc(vc_req->head, &desc,
1056                                 chain->para.hash_result_len, nb_descs) < 0)) {
1057                         VC_LOG_ERR("Incorrect descriptor");
1058                         ret = VIRTIO_CRYPTO_ERR;
1059                         goto error_exit;
1060                 }
1061
1062                 break;
1063         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1064                 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
1065                                 rte_pktmbuf_mtod(m_src, uint8_t *),
1066                                 chain->para.cipher_start_src_offset,
1067                                 chain->para.dst_data_len -
1068                                 chain->para.cipher_start_src_offset, nb_descs);
1069                 if (unlikely(vc_req->wb == NULL)) {
1070                         ret = VIRTIO_CRYPTO_ERR;
1071                         goto error_exit;
1072                 }
1073
1074                 digest_offset = m_src->data_len;
1075                 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1076                                 digest_offset);
1077                 digest_desc = desc;
1078
1079                 /** create a wb_data for digest */
1080                 ewb->next = prepare_write_back_data(vc_req, &desc, &ewb2,
1081                                 digest_addr, 0, chain->para.hash_result_len,
1082                                 nb_descs);
1083                 if (unlikely(ewb->next == NULL)) {
1084                         ret = VIRTIO_CRYPTO_ERR;
1085                         goto error_exit;
1086                 }
1087
1088                 if (unlikely(copy_data(digest_addr, vc_req, &digest_desc,
1089                                 chain->para.hash_result_len, nb_descs)) < 0) {
1090                         ret = VIRTIO_CRYPTO_BADMSG;
1091                         goto error_exit;
1092                 }
1093
1094                 op->sym->auth.digest.data = digest_addr;
1095                 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1096                                 digest_offset);
1097                 break;
1098         default:
1099                 ret = VIRTIO_CRYPTO_BADMSG;
1100                 goto error_exit;
1101         }
1102
1103         /* record inhdr */
1104         vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1105         if (unlikely(vc_req->inhdr == NULL)) {
1106                 ret = VIRTIO_CRYPTO_BADMSG;
1107                 goto error_exit;
1108         }
1109
1110         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1111
1112         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1113         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1114
1115         op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1116         op->sym->cipher.data.length = chain->para.src_data_len -
1117                         chain->para.cipher_start_src_offset;
1118
1119         op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1120         op->sym->auth.data.length = chain->para.len_to_hash;
1121
1122         vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1123                         INHDR_LEN;
1124         return 0;
1125
1126 error_exit:
1127         if (vc_req->wb)
1128                 free_wb_data(vc_req->wb, vc_req->wb_pool);
1129         vc_req->len = INHDR_LEN;
1130         return ret;
1131 }
1132
1133 /**
1134  * Process on descriptor
1135  */
1136 static __rte_always_inline int
1137 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1138                 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1139                 struct vring_desc *head, uint16_t desc_idx)
1140 {
1141         struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1142         struct rte_cryptodev_sym_session *session;
1143         struct virtio_crypto_op_data_req *req, tmp_req;
1144         struct virtio_crypto_inhdr *inhdr;
1145         struct vring_desc *desc = NULL;
1146         uint64_t session_id;
1147         uint64_t dlen;
1148         uint32_t nb_descs = vq->size;
1149         int err = 0;
1150
1151         vc_req->desc_idx = desc_idx;
1152         vc_req->dev = vcrypto->dev;
1153         vc_req->vq = vq;
1154
1155         if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
1156                 dlen = head->len;
1157                 nb_descs = dlen / sizeof(struct vring_desc);
1158                 /* drop invalid descriptors */
1159                 if (unlikely(nb_descs > vq->size))
1160                         return -1;
1161                 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1162                                 &dlen, VHOST_ACCESS_RO);
1163                 if (unlikely(!desc || dlen != head->len))
1164                         return -1;
1165                 desc_idx = 0;
1166                 head = desc;
1167         } else {
1168                 desc = head;
1169         }
1170
1171         vc_req->head = head;
1172         vc_req->zero_copy = vcrypto->option;
1173
1174         req = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1175         if (unlikely(req == NULL)) {
1176                 switch (vcrypto->option) {
1177                 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1178                         err = VIRTIO_CRYPTO_BADMSG;
1179                         VC_LOG_ERR("Invalid descriptor");
1180                         goto error_exit;
1181                 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1182                         req = &tmp_req;
1183                         if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req),
1184                                         &nb_descs) < 0)) {
1185                                 err = VIRTIO_CRYPTO_BADMSG;
1186                                 VC_LOG_ERR("Invalid descriptor");
1187                                 goto error_exit;
1188                         }
1189                         break;
1190                 default:
1191                         err = VIRTIO_CRYPTO_ERR;
1192                         VC_LOG_ERR("Invalid option");
1193                         goto error_exit;
1194                 }
1195         } else {
1196                 if (unlikely(move_desc(vc_req->head, &desc,
1197                                 sizeof(*req), &nb_descs) < 0)) {
1198                         VC_LOG_ERR("Incorrect descriptor");
1199                         goto error_exit;
1200                 }
1201         }
1202
1203         switch (req->header.opcode) {
1204         case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1205         case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1206                 session_id = req->header.session_id;
1207
1208                 /* one branch to avoid unnecessary table lookup */
1209                 if (vcrypto->cache_session_id != session_id) {
1210                         err = rte_hash_lookup_data(vcrypto->session_map,
1211                                         &session_id, (void **)&session);
1212                         if (unlikely(err < 0)) {
1213                                 err = VIRTIO_CRYPTO_ERR;
1214                                 VC_LOG_ERR("Failed to find session %"PRIu64,
1215                                                 session_id);
1216                                 goto error_exit;
1217                         }
1218
1219                         vcrypto->cache_session = session;
1220                         vcrypto->cache_session_id = session_id;
1221                 }
1222
1223                 session = vcrypto->cache_session;
1224
1225                 err = rte_crypto_op_attach_sym_session(op, session);
1226                 if (unlikely(err < 0)) {
1227                         err = VIRTIO_CRYPTO_ERR;
1228                         VC_LOG_ERR("Failed to attach session to op");
1229                         goto error_exit;
1230                 }
1231
1232                 switch (req->u.sym_req.op_type) {
1233                 case VIRTIO_CRYPTO_SYM_OP_NONE:
1234                         err = VIRTIO_CRYPTO_NOTSUPP;
1235                         break;
1236                 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1237                         err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1238                                         &req->u.sym_req.u.cipher, desc,
1239                                         &nb_descs);
1240                         break;
1241                 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1242                         err = prepare_sym_chain_op(vcrypto, op, vc_req,
1243                                         &req->u.sym_req.u.chain, desc,
1244                                         &nb_descs);
1245                         break;
1246                 }
1247                 if (unlikely(err != 0)) {
1248                         VC_LOG_ERR("Failed to process sym request");
1249                         goto error_exit;
1250                 }
1251                 break;
1252         default:
1253                 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1254                                 req->header.opcode);
1255                 goto error_exit;
1256         }
1257
1258         return 0;
1259
1260 error_exit:
1261
1262         inhdr = reach_inhdr(vc_req, desc, &nb_descs);
1263         if (likely(inhdr != NULL))
1264                 inhdr->status = (uint8_t)err;
1265
1266         return -1;
1267 }
1268
1269 static __rte_always_inline struct vhost_virtqueue *
1270 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1271                 struct vhost_virtqueue *old_vq)
1272 {
1273         struct rte_mbuf *m_src = op->sym->m_src;
1274         struct rte_mbuf *m_dst = op->sym->m_dst;
1275         struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1276         uint16_t desc_idx;
1277
1278         if (unlikely(!vc_req)) {
1279                 VC_LOG_ERR("Failed to retrieve vc_req");
1280                 return NULL;
1281         }
1282
1283         if (old_vq && (vc_req->vq != old_vq))
1284                 return vc_req->vq;
1285
1286         desc_idx = vc_req->desc_idx;
1287
1288         if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1289                 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1290         else {
1291                 if (vc_req->zero_copy == 0)
1292                         write_back_data(vc_req);
1293         }
1294
1295         vc_req->vq->used->ring[desc_idx].id = desc_idx;
1296         vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1297
1298         rte_mempool_put(m_src->pool, (void *)m_src);
1299
1300         if (m_dst)
1301                 rte_mempool_put(m_dst->pool, (void *)m_dst);
1302
1303         return vc_req->vq;
1304 }
1305
1306 static __rte_always_inline uint16_t
1307 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1308                 uint16_t nb_ops, int *callfd)
1309 {
1310         uint16_t processed = 1;
1311         struct vhost_virtqueue *vq, *tmp_vq;
1312
1313         if (unlikely(nb_ops == 0))
1314                 return 0;
1315
1316         vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1317         if (unlikely(vq == NULL))
1318                 return 0;
1319         tmp_vq = vq;
1320
1321         while ((processed < nb_ops)) {
1322                 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1323                                 tmp_vq);
1324
1325                 if (unlikely(vq != tmp_vq))
1326                         break;
1327
1328                 processed++;
1329         }
1330
1331         *callfd = vq->callfd;
1332
1333         *(volatile uint16_t *)&vq->used->idx += processed;
1334
1335         return processed;
1336 }
1337
1338 int __rte_experimental
1339 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1340                 struct rte_mempool *sess_pool,
1341                 struct rte_mempool *sess_priv_pool,
1342                 int socket_id)
1343 {
1344         struct virtio_net *dev = get_device(vid);
1345         struct rte_hash_parameters params = {0};
1346         struct vhost_crypto *vcrypto;
1347         char name[128];
1348         int ret;
1349
1350         if (!dev) {
1351                 VC_LOG_ERR("Invalid vid %i", vid);
1352                 return -EINVAL;
1353         }
1354
1355         ret = rte_vhost_driver_set_features(dev->ifname,
1356                         VIRTIO_CRYPTO_FEATURES);
1357         if (ret < 0) {
1358                 VC_LOG_ERR("Error setting features");
1359                 return -1;
1360         }
1361
1362         vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1363                         RTE_CACHE_LINE_SIZE, socket_id);
1364         if (!vcrypto) {
1365                 VC_LOG_ERR("Insufficient memory");
1366                 return -ENOMEM;
1367         }
1368
1369         vcrypto->sess_pool = sess_pool;
1370         vcrypto->sess_priv_pool = sess_priv_pool;
1371         vcrypto->cid = cryptodev_id;
1372         vcrypto->cache_session_id = UINT64_MAX;
1373         vcrypto->last_session_id = 1;
1374         vcrypto->dev = dev;
1375         vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1376
1377         snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1378         params.name = name;
1379         params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1380         params.hash_func = rte_jhash;
1381         params.key_len = sizeof(uint64_t);
1382         params.socket_id = socket_id;
1383         vcrypto->session_map = rte_hash_create(&params);
1384         if (!vcrypto->session_map) {
1385                 VC_LOG_ERR("Failed to creath session map");
1386                 ret = -ENOMEM;
1387                 goto error_exit;
1388         }
1389
1390         snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1391         vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1392                         VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1393                         sizeof(struct vhost_crypto_data_req),
1394                         RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1395                         rte_socket_id());
1396         if (!vcrypto->mbuf_pool) {
1397                 VC_LOG_ERR("Failed to creath mbuf pool");
1398                 ret = -ENOMEM;
1399                 goto error_exit;
1400         }
1401
1402         snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1403         vcrypto->wb_pool = rte_mempool_create(name,
1404                         VHOST_CRYPTO_MBUF_POOL_SIZE,
1405                         sizeof(struct vhost_crypto_writeback_data),
1406                         128, 0, NULL, NULL, NULL, NULL,
1407                         rte_socket_id(), 0);
1408         if (!vcrypto->wb_pool) {
1409                 VC_LOG_ERR("Failed to creath mempool");
1410                 ret = -ENOMEM;
1411                 goto error_exit;
1412         }
1413
1414         dev->extern_data = vcrypto;
1415         dev->extern_ops.pre_msg_handle = NULL;
1416         dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1417
1418         return 0;
1419
1420 error_exit:
1421         if (vcrypto->session_map)
1422                 rte_hash_free(vcrypto->session_map);
1423         if (vcrypto->mbuf_pool)
1424                 rte_mempool_free(vcrypto->mbuf_pool);
1425
1426         rte_free(vcrypto);
1427
1428         return ret;
1429 }
1430
1431 int __rte_experimental
1432 rte_vhost_crypto_free(int vid)
1433 {
1434         struct virtio_net *dev = get_device(vid);
1435         struct vhost_crypto *vcrypto;
1436
1437         if (unlikely(dev == NULL)) {
1438                 VC_LOG_ERR("Invalid vid %i", vid);
1439                 return -EINVAL;
1440         }
1441
1442         vcrypto = dev->extern_data;
1443         if (unlikely(vcrypto == NULL)) {
1444                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1445                 return -ENOENT;
1446         }
1447
1448         rte_hash_free(vcrypto->session_map);
1449         rte_mempool_free(vcrypto->mbuf_pool);
1450         rte_mempool_free(vcrypto->wb_pool);
1451         rte_free(vcrypto);
1452
1453         dev->extern_data = NULL;
1454         dev->extern_ops.pre_msg_handle = NULL;
1455         dev->extern_ops.post_msg_handle = NULL;
1456
1457         return 0;
1458 }
1459
1460 int __rte_experimental
1461 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1462 {
1463         struct virtio_net *dev = get_device(vid);
1464         struct vhost_crypto *vcrypto;
1465
1466         if (unlikely(dev == NULL)) {
1467                 VC_LOG_ERR("Invalid vid %i", vid);
1468                 return -EINVAL;
1469         }
1470
1471         if (unlikely((uint32_t)option >=
1472                                 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1473                 VC_LOG_ERR("Invalid option %i", option);
1474                 return -EINVAL;
1475         }
1476
1477         vcrypto = (struct vhost_crypto *)dev->extern_data;
1478         if (unlikely(vcrypto == NULL)) {
1479                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1480                 return -ENOENT;
1481         }
1482
1483         if (vcrypto->option == (uint8_t)option)
1484                 return 0;
1485
1486         if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1487                         !(rte_mempool_full(vcrypto->wb_pool))) {
1488                 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1489                 return -EINVAL;
1490         }
1491
1492         if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1493                 char name[128];
1494
1495                 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1496                 vcrypto->wb_pool = rte_mempool_create(name,
1497                                 VHOST_CRYPTO_MBUF_POOL_SIZE,
1498                                 sizeof(struct vhost_crypto_writeback_data),
1499                                 128, 0, NULL, NULL, NULL, NULL,
1500                                 rte_socket_id(), 0);
1501                 if (!vcrypto->wb_pool) {
1502                         VC_LOG_ERR("Failed to creath mbuf pool");
1503                         return -ENOMEM;
1504                 }
1505         } else {
1506                 rte_mempool_free(vcrypto->wb_pool);
1507                 vcrypto->wb_pool = NULL;
1508         }
1509
1510         vcrypto->option = (uint8_t)option;
1511
1512         return 0;
1513 }
1514
1515 uint16_t __rte_experimental
1516 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1517                 struct rte_crypto_op **ops, uint16_t nb_ops)
1518 {
1519         struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1520         struct virtio_net *dev = get_device(vid);
1521         struct vhost_crypto *vcrypto;
1522         struct vhost_virtqueue *vq;
1523         uint16_t avail_idx;
1524         uint16_t start_idx;
1525         uint16_t count;
1526         uint16_t i = 0;
1527
1528         if (unlikely(dev == NULL)) {
1529                 VC_LOG_ERR("Invalid vid %i", vid);
1530                 return -EINVAL;
1531         }
1532
1533         if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1534                 VC_LOG_ERR("Invalid qid %u", qid);
1535                 return -EINVAL;
1536         }
1537
1538         vcrypto = (struct vhost_crypto *)dev->extern_data;
1539         if (unlikely(vcrypto == NULL)) {
1540                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1541                 return -ENOENT;
1542         }
1543
1544         vq = dev->virtqueue[qid];
1545
1546         avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1547         start_idx = vq->last_used_idx;
1548         count = avail_idx - start_idx;
1549         count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1550         count = RTE_MIN(count, nb_ops);
1551
1552         if (unlikely(count == 0))
1553                 return 0;
1554
1555         /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1556          * we need only 1 mbuf as src and dst
1557          */
1558         switch (vcrypto->option) {
1559         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1560                 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1561                                 (void **)mbufs, count * 2) < 0)) {
1562                         VC_LOG_ERR("Insufficient memory");
1563                         return -ENOMEM;
1564                 }
1565
1566                 for (i = 0; i < count; i++) {
1567                         uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1568                         uint16_t desc_idx = vq->avail->ring[used_idx];
1569                         struct vring_desc *head = &vq->desc[desc_idx];
1570                         struct rte_crypto_op *op = ops[i];
1571
1572                         op->sym->m_src = mbufs[i * 2];
1573                         op->sym->m_dst = mbufs[i * 2 + 1];
1574                         op->sym->m_src->data_off = 0;
1575                         op->sym->m_dst->data_off = 0;
1576
1577                         if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1578                                         op, head, desc_idx)) < 0)
1579                                 break;
1580                 }
1581
1582                 if (unlikely(i < count))
1583                         rte_mempool_put_bulk(vcrypto->mbuf_pool,
1584                                         (void **)&mbufs[i * 2],
1585                                         (count - i) * 2);
1586
1587                 break;
1588
1589         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1590                 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1591                                 (void **)mbufs, count) < 0)) {
1592                         VC_LOG_ERR("Insufficient memory");
1593                         return -ENOMEM;
1594                 }
1595
1596                 for (i = 0; i < count; i++) {
1597                         uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1598                         uint16_t desc_idx = vq->avail->ring[used_idx];
1599                         struct vring_desc *head = &vq->desc[desc_idx];
1600                         struct rte_crypto_op *op = ops[i];
1601
1602                         op->sym->m_src = mbufs[i];
1603                         op->sym->m_dst = NULL;
1604                         op->sym->m_src->data_off = 0;
1605
1606                         if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1607                                         op, head, desc_idx)) < 0)
1608                                 break;
1609                 }
1610
1611                 if (unlikely(i < count))
1612                         rte_mempool_put_bulk(vcrypto->mbuf_pool,
1613                                         (void **)&mbufs[i],
1614                                         count - i);
1615
1616                 break;
1617
1618         }
1619
1620         vq->last_used_idx += i;
1621
1622         return i;
1623 }
1624
1625 uint16_t __rte_experimental
1626 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1627                 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1628 {
1629         struct rte_crypto_op **tmp_ops = ops;
1630         uint16_t count = 0, left = nb_ops;
1631         int callfd;
1632         uint16_t idx = 0;
1633
1634         while (left) {
1635                 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1636                                 &callfd);
1637                 if (unlikely(count == 0))
1638                         break;
1639
1640                 tmp_ops = &tmp_ops[count];
1641                 left -= count;
1642
1643                 callfds[idx++] = callfd;
1644
1645                 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1646                         VC_LOG_ERR("Too many vqs");
1647                         break;
1648                 }
1649         }
1650
1651         *nb_callfds = idx;
1652
1653         return nb_ops - left;
1654 }