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