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