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