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