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