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