vhost/crypto: fix write back source
[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);
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 + offset;
759                 wb_data->len = RTE_MIN(dlen - offset, write_back_len);
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 + offset;
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 __rte_always_inline uint8_t
844 vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req *req)
845 {
846         if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
847                 (req->para.src_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE) &&
848                 (req->para.dst_data_len >= req->para.src_data_len) &&
849                 (req->para.dst_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE)))
850                 return VIRTIO_CRYPTO_OK;
851         return VIRTIO_CRYPTO_BADMSG;
852 }
853
854 static uint8_t
855 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
856                 struct vhost_crypto_data_req *vc_req,
857                 struct virtio_crypto_cipher_data_req *cipher,
858                 struct vring_desc *cur_desc,
859                 uint32_t *nb_descs, uint32_t vq_size)
860 {
861         struct vring_desc *desc = cur_desc;
862         struct vhost_crypto_writeback_data *ewb = NULL;
863         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
864         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
865         uint8_t ret = vhost_crypto_check_cipher_request(cipher);
866
867         if (unlikely(ret != VIRTIO_CRYPTO_OK))
868                 goto error_exit;
869
870         /* prepare */
871         /* iv */
872         if (unlikely(copy_data(iv_data, vc_req, &desc, cipher->para.iv_len,
873                         nb_descs, vq_size) < 0)) {
874                 ret = VIRTIO_CRYPTO_BADMSG;
875                 goto error_exit;
876         }
877
878         switch (vcrypto->option) {
879         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
880                 m_src->data_len = cipher->para.src_data_len;
881                 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
882                                 cipher->para.src_data_len);
883                 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
884                 if (unlikely(m_src->buf_iova == 0 ||
885                                 m_src->buf_addr == NULL)) {
886                         VC_LOG_ERR("zero_copy may fail due to cross page data");
887                         ret = VIRTIO_CRYPTO_ERR;
888                         goto error_exit;
889                 }
890
891                 if (unlikely(move_desc(vc_req->head, &desc,
892                                 cipher->para.src_data_len, nb_descs,
893                                 vq_size) < 0)) {
894                         VC_LOG_ERR("Incorrect descriptor");
895                         ret = VIRTIO_CRYPTO_ERR;
896                         goto error_exit;
897                 }
898
899                 break;
900         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
901                 vc_req->wb_pool = vcrypto->wb_pool;
902                 m_src->data_len = cipher->para.src_data_len;
903                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
904                                 vc_req, &desc, cipher->para.src_data_len,
905                                 nb_descs, vq_size) < 0)) {
906                         ret = VIRTIO_CRYPTO_BADMSG;
907                         goto error_exit;
908                 }
909                 break;
910         default:
911                 ret = VIRTIO_CRYPTO_BADMSG;
912                 goto error_exit;
913         }
914
915         /* dst */
916         desc = find_write_desc(vc_req->head, desc, nb_descs, vq_size);
917         if (unlikely(!desc)) {
918                 VC_LOG_ERR("Cannot find write location");
919                 ret = VIRTIO_CRYPTO_BADMSG;
920                 goto error_exit;
921         }
922
923         switch (vcrypto->option) {
924         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
925                 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
926                                 desc->addr, cipher->para.dst_data_len);
927                 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
928                 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
929                         VC_LOG_ERR("zero_copy may fail due to cross page data");
930                         ret = VIRTIO_CRYPTO_ERR;
931                         goto error_exit;
932                 }
933
934                 if (unlikely(move_desc(vc_req->head, &desc,
935                                 cipher->para.dst_data_len,
936                                 nb_descs, vq_size) < 0)) {
937                         VC_LOG_ERR("Incorrect descriptor");
938                         ret = VIRTIO_CRYPTO_ERR;
939                         goto error_exit;
940                 }
941
942                 m_dst->data_len = cipher->para.dst_data_len;
943                 break;
944         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
945                 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
946                                 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
947                                 cipher->para.dst_data_len, nb_descs, vq_size);
948                 if (unlikely(vc_req->wb == NULL)) {
949                         ret = VIRTIO_CRYPTO_ERR;
950                         goto error_exit;
951                 }
952
953                 break;
954         default:
955                 ret = VIRTIO_CRYPTO_BADMSG;
956                 goto error_exit;
957         }
958
959         /* src data */
960         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
961         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
962
963         op->sym->cipher.data.offset = 0;
964         op->sym->cipher.data.length = cipher->para.src_data_len;
965
966         vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
967         if (unlikely(vc_req->inhdr == NULL)) {
968                 ret = VIRTIO_CRYPTO_BADMSG;
969                 goto error_exit;
970         }
971
972         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
973         vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
974
975         return 0;
976
977 error_exit:
978         if (vc_req->wb)
979                 free_wb_data(vc_req->wb, vc_req->wb_pool);
980
981         vc_req->len = INHDR_LEN;
982         return ret;
983 }
984
985 static __rte_always_inline uint8_t
986 vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req)
987 {
988         if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
989                 (req->para.src_data_len <= RTE_MBUF_DEFAULT_DATAROOM) &&
990                 (req->para.dst_data_len >= req->para.src_data_len) &&
991                 (req->para.dst_data_len <= RTE_MBUF_DEFAULT_DATAROOM) &&
992                 (req->para.cipher_start_src_offset <
993                         RTE_MBUF_DEFAULT_DATAROOM) &&
994                 (req->para.len_to_cipher < RTE_MBUF_DEFAULT_DATAROOM) &&
995                 (req->para.hash_start_src_offset <
996                         RTE_MBUF_DEFAULT_DATAROOM) &&
997                 (req->para.len_to_hash < RTE_MBUF_DEFAULT_DATAROOM) &&
998                 (req->para.cipher_start_src_offset + req->para.len_to_cipher <=
999                         req->para.src_data_len) &&
1000                 (req->para.hash_start_src_offset + req->para.len_to_hash <=
1001                         req->para.src_data_len) &&
1002                 (req->para.dst_data_len + req->para.hash_result_len <=
1003                         RTE_MBUF_DEFAULT_DATAROOM)))
1004                 return VIRTIO_CRYPTO_OK;
1005         return VIRTIO_CRYPTO_BADMSG;
1006 }
1007
1008 static uint8_t
1009 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
1010                 struct vhost_crypto_data_req *vc_req,
1011                 struct virtio_crypto_alg_chain_data_req *chain,
1012                 struct vring_desc *cur_desc,
1013                 uint32_t *nb_descs, uint32_t vq_size)
1014 {
1015         struct vring_desc *desc = cur_desc, *digest_desc;
1016         struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
1017         struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
1018         uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
1019         uint32_t digest_offset;
1020         void *digest_addr;
1021         uint8_t ret = vhost_crypto_check_chain_request(chain);
1022
1023         if (unlikely(ret != VIRTIO_CRYPTO_OK))
1024                 goto error_exit;
1025
1026         /* prepare */
1027         /* iv */
1028         if (unlikely(copy_data(iv_data, vc_req, &desc,
1029                         chain->para.iv_len, nb_descs, vq_size) < 0)) {
1030                 ret = VIRTIO_CRYPTO_BADMSG;
1031                 goto error_exit;
1032         }
1033
1034         switch (vcrypto->option) {
1035         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1036                 m_src->data_len = chain->para.src_data_len;
1037                 m_dst->data_len = chain->para.dst_data_len;
1038
1039                 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
1040                                 chain->para.src_data_len);
1041                 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1042                 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
1043                         VC_LOG_ERR("zero_copy may fail due to cross page data");
1044                         ret = VIRTIO_CRYPTO_ERR;
1045                         goto error_exit;
1046                 }
1047
1048                 if (unlikely(move_desc(vc_req->head, &desc,
1049                                 chain->para.src_data_len,
1050                                 nb_descs, vq_size) < 0)) {
1051                         VC_LOG_ERR("Incorrect descriptor");
1052                         ret = VIRTIO_CRYPTO_ERR;
1053                         goto error_exit;
1054                 }
1055                 break;
1056         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1057                 vc_req->wb_pool = vcrypto->wb_pool;
1058                 m_src->data_len = chain->para.src_data_len;
1059                 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1060                                 vc_req, &desc, chain->para.src_data_len,
1061                                 nb_descs, vq_size) < 0)) {
1062                         ret = VIRTIO_CRYPTO_BADMSG;
1063                         goto error_exit;
1064                 }
1065
1066                 break;
1067         default:
1068                 ret = VIRTIO_CRYPTO_BADMSG;
1069                 goto error_exit;
1070         }
1071
1072         /* dst */
1073         desc = find_write_desc(vc_req->head, desc, nb_descs, vq_size);
1074         if (unlikely(!desc)) {
1075                 VC_LOG_ERR("Cannot find write location");
1076                 ret = VIRTIO_CRYPTO_BADMSG;
1077                 goto error_exit;
1078         }
1079
1080         switch (vcrypto->option) {
1081         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1082                 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
1083                                 desc->addr, chain->para.dst_data_len);
1084                 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
1085                 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
1086                         VC_LOG_ERR("zero_copy may fail due to cross page data");
1087                         ret = VIRTIO_CRYPTO_ERR;
1088                         goto error_exit;
1089                 }
1090
1091                 if (unlikely(move_desc(vc_req->head, &desc,
1092                                 chain->para.dst_data_len,
1093                                 nb_descs, vq_size) < 0)) {
1094                         VC_LOG_ERR("Incorrect descriptor");
1095                         ret = VIRTIO_CRYPTO_ERR;
1096                         goto error_exit;
1097                 }
1098
1099                 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1100                                 desc->addr, chain->para.hash_result_len);
1101                 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1102                                 VHOST_ACCESS_RW);
1103                 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1104                         VC_LOG_ERR("zero_copy may fail due to cross page data");
1105                         ret = VIRTIO_CRYPTO_ERR;
1106                         goto error_exit;
1107                 }
1108
1109                 if (unlikely(move_desc(vc_req->head, &desc,
1110                                 chain->para.hash_result_len,
1111                                 nb_descs, vq_size) < 0)) {
1112                         VC_LOG_ERR("Incorrect descriptor");
1113                         ret = VIRTIO_CRYPTO_ERR;
1114                         goto error_exit;
1115                 }
1116
1117                 break;
1118         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1119                 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
1120                                 rte_pktmbuf_mtod(m_src, uint8_t *),
1121                                 chain->para.cipher_start_src_offset,
1122                                 chain->para.dst_data_len -
1123                                 chain->para.cipher_start_src_offset,
1124                                 nb_descs, vq_size);
1125                 if (unlikely(vc_req->wb == NULL)) {
1126                         ret = VIRTIO_CRYPTO_ERR;
1127                         goto error_exit;
1128                 }
1129
1130                 digest_offset = m_src->data_len;
1131                 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1132                                 digest_offset);
1133                 digest_desc = desc;
1134
1135                 /** create a wb_data for digest */
1136                 ewb->next = prepare_write_back_data(vc_req, &desc, &ewb2,
1137                                 digest_addr, 0, chain->para.hash_result_len,
1138                                 nb_descs, vq_size);
1139                 if (unlikely(ewb->next == NULL)) {
1140                         ret = VIRTIO_CRYPTO_ERR;
1141                         goto error_exit;
1142                 }
1143
1144                 if (unlikely(copy_data(digest_addr, vc_req, &digest_desc,
1145                                 chain->para.hash_result_len,
1146                                 nb_descs, vq_size) < 0)) {
1147                         ret = VIRTIO_CRYPTO_BADMSG;
1148                         goto error_exit;
1149                 }
1150
1151                 op->sym->auth.digest.data = digest_addr;
1152                 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1153                                 digest_offset);
1154                 break;
1155         default:
1156                 ret = VIRTIO_CRYPTO_BADMSG;
1157                 goto error_exit;
1158         }
1159
1160         /* record inhdr */
1161         vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1162         if (unlikely(vc_req->inhdr == NULL)) {
1163                 ret = VIRTIO_CRYPTO_BADMSG;
1164                 goto error_exit;
1165         }
1166
1167         vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1168
1169         op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1170         op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1171
1172         op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1173         op->sym->cipher.data.length = chain->para.src_data_len -
1174                         chain->para.cipher_start_src_offset;
1175
1176         op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1177         op->sym->auth.data.length = chain->para.len_to_hash;
1178
1179         vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1180                         INHDR_LEN;
1181         return 0;
1182
1183 error_exit:
1184         if (vc_req->wb)
1185                 free_wb_data(vc_req->wb, vc_req->wb_pool);
1186         vc_req->len = INHDR_LEN;
1187         return ret;
1188 }
1189
1190 /**
1191  * Process on descriptor
1192  */
1193 static __rte_always_inline int
1194 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1195                 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1196                 struct vring_desc *head, uint16_t desc_idx)
1197 {
1198         struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1199         struct rte_cryptodev_sym_session *session;
1200         struct virtio_crypto_op_data_req *req, tmp_req;
1201         struct virtio_crypto_inhdr *inhdr;
1202         struct vring_desc *desc = NULL;
1203         uint64_t session_id;
1204         uint64_t dlen;
1205         uint32_t nb_descs = vq->size;
1206         int err = 0;
1207
1208         vc_req->desc_idx = desc_idx;
1209         vc_req->dev = vcrypto->dev;
1210         vc_req->vq = vq;
1211
1212         if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
1213                 dlen = head->len;
1214                 nb_descs = dlen / sizeof(struct vring_desc);
1215                 /* drop invalid descriptors */
1216                 if (unlikely(nb_descs > vq->size))
1217                         return -1;
1218                 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1219                                 &dlen, VHOST_ACCESS_RO);
1220                 if (unlikely(!desc || dlen != head->len))
1221                         return -1;
1222                 desc_idx = 0;
1223                 head = desc;
1224         } else {
1225                 desc = head;
1226         }
1227
1228         vc_req->head = head;
1229         vc_req->zero_copy = vcrypto->option;
1230
1231         req = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1232         if (unlikely(req == NULL)) {
1233                 switch (vcrypto->option) {
1234                 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1235                         err = VIRTIO_CRYPTO_BADMSG;
1236                         VC_LOG_ERR("Invalid descriptor");
1237                         goto error_exit;
1238                 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1239                         req = &tmp_req;
1240                         if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req),
1241                                         &nb_descs, vq->size) < 0)) {
1242                                 err = VIRTIO_CRYPTO_BADMSG;
1243                                 VC_LOG_ERR("Invalid descriptor");
1244                                 goto error_exit;
1245                         }
1246                         break;
1247                 default:
1248                         err = VIRTIO_CRYPTO_ERR;
1249                         VC_LOG_ERR("Invalid option");
1250                         goto error_exit;
1251                 }
1252         } else {
1253                 if (unlikely(move_desc(vc_req->head, &desc,
1254                                 sizeof(*req), &nb_descs, vq->size) < 0)) {
1255                         VC_LOG_ERR("Incorrect descriptor");
1256                         goto error_exit;
1257                 }
1258         }
1259
1260         switch (req->header.opcode) {
1261         case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1262         case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1263                 session_id = req->header.session_id;
1264
1265                 /* one branch to avoid unnecessary table lookup */
1266                 if (vcrypto->cache_session_id != session_id) {
1267                         err = rte_hash_lookup_data(vcrypto->session_map,
1268                                         &session_id, (void **)&session);
1269                         if (unlikely(err < 0)) {
1270                                 err = VIRTIO_CRYPTO_ERR;
1271                                 VC_LOG_ERR("Failed to find session %"PRIu64,
1272                                                 session_id);
1273                                 goto error_exit;
1274                         }
1275
1276                         vcrypto->cache_session = session;
1277                         vcrypto->cache_session_id = session_id;
1278                 }
1279
1280                 session = vcrypto->cache_session;
1281
1282                 err = rte_crypto_op_attach_sym_session(op, session);
1283                 if (unlikely(err < 0)) {
1284                         err = VIRTIO_CRYPTO_ERR;
1285                         VC_LOG_ERR("Failed to attach session to op");
1286                         goto error_exit;
1287                 }
1288
1289                 switch (req->u.sym_req.op_type) {
1290                 case VIRTIO_CRYPTO_SYM_OP_NONE:
1291                         err = VIRTIO_CRYPTO_NOTSUPP;
1292                         break;
1293                 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1294                         err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1295                                         &req->u.sym_req.u.cipher, desc,
1296                                         &nb_descs, vq->size);
1297                         break;
1298                 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1299                         err = prepare_sym_chain_op(vcrypto, op, vc_req,
1300                                         &req->u.sym_req.u.chain, desc,
1301                                         &nb_descs, vq->size);
1302                         break;
1303                 }
1304                 if (unlikely(err != 0)) {
1305                         VC_LOG_ERR("Failed to process sym request");
1306                         goto error_exit;
1307                 }
1308                 break;
1309         default:
1310                 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1311                                 req->header.opcode);
1312                 goto error_exit;
1313         }
1314
1315         return 0;
1316
1317 error_exit:
1318
1319         inhdr = reach_inhdr(vc_req, desc, &nb_descs, vq->size);
1320         if (likely(inhdr != NULL))
1321                 inhdr->status = (uint8_t)err;
1322
1323         return -1;
1324 }
1325
1326 static __rte_always_inline struct vhost_virtqueue *
1327 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1328                 struct vhost_virtqueue *old_vq)
1329 {
1330         struct rte_mbuf *m_src = op->sym->m_src;
1331         struct rte_mbuf *m_dst = op->sym->m_dst;
1332         struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1333         uint16_t desc_idx;
1334
1335         if (unlikely(!vc_req)) {
1336                 VC_LOG_ERR("Failed to retrieve vc_req");
1337                 return NULL;
1338         }
1339
1340         if (old_vq && (vc_req->vq != old_vq))
1341                 return vc_req->vq;
1342
1343         desc_idx = vc_req->desc_idx;
1344
1345         if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1346                 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1347         else {
1348                 if (vc_req->zero_copy == 0)
1349                         write_back_data(vc_req);
1350         }
1351
1352         vc_req->vq->used->ring[desc_idx].id = desc_idx;
1353         vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1354
1355         rte_mempool_put(m_src->pool, (void *)m_src);
1356
1357         if (m_dst)
1358                 rte_mempool_put(m_dst->pool, (void *)m_dst);
1359
1360         return vc_req->vq;
1361 }
1362
1363 static __rte_always_inline uint16_t
1364 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1365                 uint16_t nb_ops, int *callfd)
1366 {
1367         uint16_t processed = 1;
1368         struct vhost_virtqueue *vq, *tmp_vq;
1369
1370         if (unlikely(nb_ops == 0))
1371                 return 0;
1372
1373         vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1374         if (unlikely(vq == NULL))
1375                 return 0;
1376         tmp_vq = vq;
1377
1378         while ((processed < nb_ops)) {
1379                 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1380                                 tmp_vq);
1381
1382                 if (unlikely(vq != tmp_vq))
1383                         break;
1384
1385                 processed++;
1386         }
1387
1388         *callfd = vq->callfd;
1389
1390         *(volatile uint16_t *)&vq->used->idx += processed;
1391
1392         return processed;
1393 }
1394
1395 int
1396 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1397                 struct rte_mempool *sess_pool,
1398                 struct rte_mempool *sess_priv_pool,
1399                 int socket_id)
1400 {
1401         struct virtio_net *dev = get_device(vid);
1402         struct rte_hash_parameters params = {0};
1403         struct vhost_crypto *vcrypto;
1404         char name[128];
1405         int ret;
1406
1407         if (!dev) {
1408                 VC_LOG_ERR("Invalid vid %i", vid);
1409                 return -EINVAL;
1410         }
1411
1412         ret = rte_vhost_driver_set_features(dev->ifname,
1413                         VIRTIO_CRYPTO_FEATURES);
1414         if (ret < 0) {
1415                 VC_LOG_ERR("Error setting features");
1416                 return -1;
1417         }
1418
1419         vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1420                         RTE_CACHE_LINE_SIZE, socket_id);
1421         if (!vcrypto) {
1422                 VC_LOG_ERR("Insufficient memory");
1423                 return -ENOMEM;
1424         }
1425
1426         vcrypto->sess_pool = sess_pool;
1427         vcrypto->sess_priv_pool = sess_priv_pool;
1428         vcrypto->cid = cryptodev_id;
1429         vcrypto->cache_session_id = UINT64_MAX;
1430         vcrypto->last_session_id = 1;
1431         vcrypto->dev = dev;
1432         vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1433
1434         snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1435         params.name = name;
1436         params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1437         params.hash_func = rte_jhash;
1438         params.key_len = sizeof(uint64_t);
1439         params.socket_id = socket_id;
1440         vcrypto->session_map = rte_hash_create(&params);
1441         if (!vcrypto->session_map) {
1442                 VC_LOG_ERR("Failed to creath session map");
1443                 ret = -ENOMEM;
1444                 goto error_exit;
1445         }
1446
1447         snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1448         vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1449                         VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1450                         sizeof(struct vhost_crypto_data_req),
1451                         RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1452                         rte_socket_id());
1453         if (!vcrypto->mbuf_pool) {
1454                 VC_LOG_ERR("Failed to creath mbuf pool");
1455                 ret = -ENOMEM;
1456                 goto error_exit;
1457         }
1458
1459         snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1460         vcrypto->wb_pool = rte_mempool_create(name,
1461                         VHOST_CRYPTO_MBUF_POOL_SIZE,
1462                         sizeof(struct vhost_crypto_writeback_data),
1463                         128, 0, NULL, NULL, NULL, NULL,
1464                         rte_socket_id(), 0);
1465         if (!vcrypto->wb_pool) {
1466                 VC_LOG_ERR("Failed to creath mempool");
1467                 ret = -ENOMEM;
1468                 goto error_exit;
1469         }
1470
1471         dev->extern_data = vcrypto;
1472         dev->extern_ops.pre_msg_handle = NULL;
1473         dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1474
1475         return 0;
1476
1477 error_exit:
1478         if (vcrypto->session_map)
1479                 rte_hash_free(vcrypto->session_map);
1480         if (vcrypto->mbuf_pool)
1481                 rte_mempool_free(vcrypto->mbuf_pool);
1482
1483         rte_free(vcrypto);
1484
1485         return ret;
1486 }
1487
1488 int
1489 rte_vhost_crypto_free(int vid)
1490 {
1491         struct virtio_net *dev = get_device(vid);
1492         struct vhost_crypto *vcrypto;
1493
1494         if (unlikely(dev == NULL)) {
1495                 VC_LOG_ERR("Invalid vid %i", vid);
1496                 return -EINVAL;
1497         }
1498
1499         vcrypto = dev->extern_data;
1500         if (unlikely(vcrypto == NULL)) {
1501                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1502                 return -ENOENT;
1503         }
1504
1505         rte_hash_free(vcrypto->session_map);
1506         rte_mempool_free(vcrypto->mbuf_pool);
1507         rte_mempool_free(vcrypto->wb_pool);
1508         rte_free(vcrypto);
1509
1510         dev->extern_data = NULL;
1511         dev->extern_ops.pre_msg_handle = NULL;
1512         dev->extern_ops.post_msg_handle = NULL;
1513
1514         return 0;
1515 }
1516
1517 int
1518 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1519 {
1520         struct virtio_net *dev = get_device(vid);
1521         struct vhost_crypto *vcrypto;
1522
1523         if (unlikely(dev == NULL)) {
1524                 VC_LOG_ERR("Invalid vid %i", vid);
1525                 return -EINVAL;
1526         }
1527
1528         if (unlikely((uint32_t)option >=
1529                                 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1530                 VC_LOG_ERR("Invalid option %i", option);
1531                 return -EINVAL;
1532         }
1533
1534         vcrypto = (struct vhost_crypto *)dev->extern_data;
1535         if (unlikely(vcrypto == NULL)) {
1536                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1537                 return -ENOENT;
1538         }
1539
1540         if (vcrypto->option == (uint8_t)option)
1541                 return 0;
1542
1543         if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1544                         !(rte_mempool_full(vcrypto->wb_pool))) {
1545                 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1546                 return -EINVAL;
1547         }
1548
1549         if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1550                 char name[128];
1551
1552                 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1553                 vcrypto->wb_pool = rte_mempool_create(name,
1554                                 VHOST_CRYPTO_MBUF_POOL_SIZE,
1555                                 sizeof(struct vhost_crypto_writeback_data),
1556                                 128, 0, NULL, NULL, NULL, NULL,
1557                                 rte_socket_id(), 0);
1558                 if (!vcrypto->wb_pool) {
1559                         VC_LOG_ERR("Failed to creath mbuf pool");
1560                         return -ENOMEM;
1561                 }
1562         } else {
1563                 rte_mempool_free(vcrypto->wb_pool);
1564                 vcrypto->wb_pool = NULL;
1565         }
1566
1567         vcrypto->option = (uint8_t)option;
1568
1569         return 0;
1570 }
1571
1572 uint16_t
1573 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1574                 struct rte_crypto_op **ops, uint16_t nb_ops)
1575 {
1576         struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1577         struct virtio_net *dev = get_device(vid);
1578         struct vhost_crypto *vcrypto;
1579         struct vhost_virtqueue *vq;
1580         uint16_t avail_idx;
1581         uint16_t start_idx;
1582         uint16_t count;
1583         uint16_t i = 0;
1584
1585         if (unlikely(dev == NULL)) {
1586                 VC_LOG_ERR("Invalid vid %i", vid);
1587                 return 0;
1588         }
1589
1590         if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1591                 VC_LOG_ERR("Invalid qid %u", qid);
1592                 return 0;
1593         }
1594
1595         vcrypto = (struct vhost_crypto *)dev->extern_data;
1596         if (unlikely(vcrypto == NULL)) {
1597                 VC_LOG_ERR("Cannot find required data, is it initialized?");
1598                 return 0;
1599         }
1600
1601         vq = dev->virtqueue[qid];
1602
1603         avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1604         start_idx = vq->last_used_idx;
1605         count = avail_idx - start_idx;
1606         count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1607         count = RTE_MIN(count, nb_ops);
1608
1609         if (unlikely(count == 0))
1610                 return 0;
1611
1612         /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1613          * we need only 1 mbuf as src and dst
1614          */
1615         switch (vcrypto->option) {
1616         case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1617                 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1618                                 (void **)mbufs, count * 2) < 0)) {
1619                         VC_LOG_ERR("Insufficient memory");
1620                         return 0;
1621                 }
1622
1623                 for (i = 0; i < count; i++) {
1624                         uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1625                         uint16_t desc_idx = vq->avail->ring[used_idx];
1626                         struct vring_desc *head = &vq->desc[desc_idx];
1627                         struct rte_crypto_op *op = ops[i];
1628
1629                         op->sym->m_src = mbufs[i * 2];
1630                         op->sym->m_dst = mbufs[i * 2 + 1];
1631                         op->sym->m_src->data_off = 0;
1632                         op->sym->m_dst->data_off = 0;
1633
1634                         if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1635                                         op, head, desc_idx) < 0))
1636                                 break;
1637                 }
1638
1639                 if (unlikely(i < count))
1640                         rte_mempool_put_bulk(vcrypto->mbuf_pool,
1641                                         (void **)&mbufs[i * 2],
1642                                         (count - i) * 2);
1643
1644                 break;
1645
1646         case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1647                 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1648                                 (void **)mbufs, count) < 0)) {
1649                         VC_LOG_ERR("Insufficient memory");
1650                         return 0;
1651                 }
1652
1653                 for (i = 0; i < count; i++) {
1654                         uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1655                         uint16_t desc_idx = vq->avail->ring[used_idx];
1656                         struct vring_desc *head = &vq->desc[desc_idx];
1657                         struct rte_crypto_op *op = ops[i];
1658
1659                         op->sym->m_src = mbufs[i];
1660                         op->sym->m_dst = NULL;
1661                         op->sym->m_src->data_off = 0;
1662
1663                         if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1664                                         op, head, desc_idx) < 0))
1665                                 break;
1666                 }
1667
1668                 if (unlikely(i < count))
1669                         rte_mempool_put_bulk(vcrypto->mbuf_pool,
1670                                         (void **)&mbufs[i],
1671                                         count - i);
1672
1673                 break;
1674
1675         }
1676
1677         vq->last_used_idx += i;
1678
1679         return i;
1680 }
1681
1682 uint16_t
1683 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1684                 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1685 {
1686         struct rte_crypto_op **tmp_ops = ops;
1687         uint16_t count = 0, left = nb_ops;
1688         int callfd;
1689         uint16_t idx = 0;
1690
1691         while (left) {
1692                 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1693                                 &callfd);
1694                 if (unlikely(count == 0))
1695                         break;
1696
1697                 tmp_ops = &tmp_ops[count];
1698                 left -= count;
1699
1700                 callfds[idx++] = callfd;
1701
1702                 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1703                         VC_LOG_ERR("Too many vqs");
1704                         break;
1705                 }
1706         }
1707
1708         *nb_callfds = idx;
1709
1710         return nb_ops - left;
1711 }