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