xref: /dpdk/lib/vhost/vhost_crypto.c (revision c6552d9a8deffa448de2d5e2e726f50508c1efd2)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2017-2018 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson #include <rte_malloc.h>
599a2dd95SBruce Richardson #include <rte_hash.h>
699a2dd95SBruce Richardson #include <rte_jhash.h>
7a526461bSStephen Hemminger #include <rte_log.h>
899a2dd95SBruce Richardson #include <rte_mbuf.h>
999a2dd95SBruce Richardson #include <rte_cryptodev.h>
1099a2dd95SBruce Richardson 
1199a2dd95SBruce Richardson #include "rte_vhost_crypto.h"
1299a2dd95SBruce Richardson #include "vhost.h"
1399a2dd95SBruce Richardson #include "vhost_user.h"
1499a2dd95SBruce Richardson #include "virtio_crypto.h"
1599a2dd95SBruce Richardson 
1699a2dd95SBruce Richardson #define INHDR_LEN		(sizeof(struct virtio_crypto_inhdr))
1799a2dd95SBruce Richardson #define IV_OFFSET		(sizeof(struct rte_crypto_op) + \
1899a2dd95SBruce Richardson 				sizeof(struct rte_crypto_sym_op))
1999a2dd95SBruce Richardson 
20a526461bSStephen Hemminger RTE_LOG_REGISTER_SUFFIX(vhost_crypto_logtype, crypto, INFO);
21a526461bSStephen Hemminger #define RTE_LOGTYPE_VHOST_CRYPTO	vhost_crypto_logtype
2299a2dd95SBruce Richardson 
230f1dc8cbSTyler Retzlaff #define VC_LOG_ERR(...)	\
240f1dc8cbSTyler Retzlaff 	RTE_LOG_LINE_PREFIX(ERR, VHOST_CRYPTO, "%s() line %u: ", \
250f1dc8cbSTyler Retzlaff 		__func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
260f1dc8cbSTyler Retzlaff 
270f1dc8cbSTyler Retzlaff #define VC_LOG_INFO(...) \
280f1dc8cbSTyler Retzlaff 	RTE_LOG_LINE_PREFIX(INFO, VHOST_CRYPTO, "%s() line %u: ", \
290f1dc8cbSTyler Retzlaff 		__func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
30a526461bSStephen Hemminger 
31a526461bSStephen Hemminger #ifdef RTE_LIBRTE_VHOST_DEBUG
320f1dc8cbSTyler Retzlaff #define VC_LOG_DBG(...)	\
330f1dc8cbSTyler Retzlaff 	RTE_LOG_LINE_PREFIX(DEBUG, VHOST_CRYPTO, "%s() line %u: ", \
340f1dc8cbSTyler Retzlaff 		__func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
35a526461bSStephen Hemminger #else
360f1dc8cbSTyler Retzlaff #define VC_LOG_DBG(...)
3799a2dd95SBruce Richardson #endif
3899a2dd95SBruce Richardson 
3999a2dd95SBruce Richardson #define VIRTIO_CRYPTO_FEATURES ((1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |	\
4099a2dd95SBruce Richardson 		(1ULL << VIRTIO_RING_F_INDIRECT_DESC) |			\
4199a2dd95SBruce Richardson 		(1ULL << VIRTIO_RING_F_EVENT_IDX) |			\
4299a2dd95SBruce Richardson 		(1ULL << VIRTIO_NET_F_CTRL_VQ) |			\
4399a2dd95SBruce Richardson 		(1ULL << VIRTIO_F_VERSION_1) |				\
4499a2dd95SBruce Richardson 		(1ULL << VHOST_USER_F_PROTOCOL_FEATURES))
4599a2dd95SBruce Richardson 
4699a2dd95SBruce Richardson #define IOVA_TO_VVA(t, r, a, l, p)					\
4799a2dd95SBruce Richardson 	((t)(uintptr_t)vhost_iova_to_vva(r->dev, r->vq, a, l, p))
4899a2dd95SBruce Richardson 
4999a2dd95SBruce Richardson /*
5099a2dd95SBruce Richardson  * vhost_crypto_desc is used to copy original vring_desc to the local buffer
5199a2dd95SBruce Richardson  * before processing (except the next index). The copy result will be an
5299a2dd95SBruce Richardson  * array of vhost_crypto_desc elements that follows the sequence of original
5399a2dd95SBruce Richardson  * vring_desc.next is arranged.
5499a2dd95SBruce Richardson  */
5599a2dd95SBruce Richardson #define vhost_crypto_desc vring_desc
5699a2dd95SBruce Richardson 
5799a2dd95SBruce Richardson static int
cipher_algo_transform(uint32_t virtio_cipher_algo,enum rte_crypto_cipher_algorithm * algo)5899a2dd95SBruce Richardson cipher_algo_transform(uint32_t virtio_cipher_algo,
5999a2dd95SBruce Richardson 		enum rte_crypto_cipher_algorithm *algo)
6099a2dd95SBruce Richardson {
6199a2dd95SBruce Richardson 	switch (virtio_cipher_algo) {
6299a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_AES_CBC:
6399a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_AES_CBC;
6499a2dd95SBruce Richardson 		break;
6599a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_AES_CTR:
6699a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_AES_CTR;
6799a2dd95SBruce Richardson 		break;
6899a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_DES_ECB:
6999a2dd95SBruce Richardson 		*algo = -VIRTIO_CRYPTO_NOTSUPP;
7099a2dd95SBruce Richardson 		break;
7199a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_DES_CBC:
7299a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_DES_CBC;
7399a2dd95SBruce Richardson 		break;
7499a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
7599a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_3DES_ECB;
7699a2dd95SBruce Richardson 		break;
7799a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
7899a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_3DES_CBC;
7999a2dd95SBruce Richardson 		break;
8099a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
8199a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_3DES_CTR;
8299a2dd95SBruce Richardson 		break;
8399a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
8499a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_KASUMI_F8;
8599a2dd95SBruce Richardson 		break;
8699a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
8799a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
8899a2dd95SBruce Richardson 		break;
8999a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_AES_F8:
9099a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_AES_F8;
9199a2dd95SBruce Richardson 		break;
9299a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_AES_XTS:
9399a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_AES_XTS;
9499a2dd95SBruce Richardson 		break;
9599a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
9699a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_CIPHER_ZUC_EEA3;
9799a2dd95SBruce Richardson 		break;
9899a2dd95SBruce Richardson 	default:
9999a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_BADMSG;
10099a2dd95SBruce Richardson 		break;
10199a2dd95SBruce Richardson 	}
10299a2dd95SBruce Richardson 
10399a2dd95SBruce Richardson 	return 0;
10499a2dd95SBruce Richardson }
10599a2dd95SBruce Richardson 
10699a2dd95SBruce Richardson static int
auth_algo_transform(uint32_t virtio_auth_algo,enum rte_crypto_auth_algorithm * algo)10799a2dd95SBruce Richardson auth_algo_transform(uint32_t virtio_auth_algo,
10899a2dd95SBruce Richardson 		enum rte_crypto_auth_algorithm *algo)
10999a2dd95SBruce Richardson {
11099a2dd95SBruce Richardson 	switch (virtio_auth_algo) {
11199a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_NO_MAC:
11299a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_NULL;
11399a2dd95SBruce Richardson 		break;
11499a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_HMAC_MD5:
11599a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_MD5_HMAC;
11699a2dd95SBruce Richardson 		break;
11799a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
11899a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
11999a2dd95SBruce Richardson 		break;
12099a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
12199a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
12299a2dd95SBruce Richardson 		break;
12399a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
12499a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
12599a2dd95SBruce Richardson 		break;
12699a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
12799a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
12899a2dd95SBruce Richardson 		break;
12999a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
13099a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
13199a2dd95SBruce Richardson 		break;
13299a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_CMAC_AES:
13399a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_AES_CMAC;
13499a2dd95SBruce Richardson 		break;
13599a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_KASUMI_F9:
13699a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_KASUMI_F9;
13799a2dd95SBruce Richardson 		break;
13899a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
13999a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
14099a2dd95SBruce Richardson 		break;
14199a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_GMAC_AES:
14299a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_AES_GMAC;
14399a2dd95SBruce Richardson 		break;
14499a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
14599a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_AES_CBC_MAC;
14699a2dd95SBruce Richardson 		break;
14799a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_XCBC_AES:
14899a2dd95SBruce Richardson 		*algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
14999a2dd95SBruce Richardson 		break;
15099a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_CMAC_3DES:
15199a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
15299a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
15399a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_NOTSUPP;
15499a2dd95SBruce Richardson 	default:
15599a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_BADMSG;
15699a2dd95SBruce Richardson 	}
15799a2dd95SBruce Richardson 
15899a2dd95SBruce Richardson 	return 0;
15999a2dd95SBruce Richardson }
16099a2dd95SBruce Richardson 
get_iv_len(enum rte_crypto_cipher_algorithm algo)16199a2dd95SBruce Richardson static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
16299a2dd95SBruce Richardson {
16399a2dd95SBruce Richardson 	int len;
16499a2dd95SBruce Richardson 
16599a2dd95SBruce Richardson 	switch (algo) {
16699a2dd95SBruce Richardson 	case RTE_CRYPTO_CIPHER_3DES_CBC:
16799a2dd95SBruce Richardson 		len = 8;
16899a2dd95SBruce Richardson 		break;
16999a2dd95SBruce Richardson 	case RTE_CRYPTO_CIPHER_3DES_CTR:
17099a2dd95SBruce Richardson 		len = 8;
17199a2dd95SBruce Richardson 		break;
17299a2dd95SBruce Richardson 	case RTE_CRYPTO_CIPHER_3DES_ECB:
17399a2dd95SBruce Richardson 		len = 8;
17499a2dd95SBruce Richardson 		break;
17599a2dd95SBruce Richardson 	case RTE_CRYPTO_CIPHER_AES_CBC:
17699a2dd95SBruce Richardson 		len = 16;
17799a2dd95SBruce Richardson 		break;
17899a2dd95SBruce Richardson 
17999a2dd95SBruce Richardson 	/* TODO: add common algos */
18099a2dd95SBruce Richardson 
18199a2dd95SBruce Richardson 	default:
18299a2dd95SBruce Richardson 		len = -1;
18399a2dd95SBruce Richardson 		break;
18499a2dd95SBruce Richardson 	}
18599a2dd95SBruce Richardson 
18699a2dd95SBruce Richardson 	return len;
18799a2dd95SBruce Richardson }
18899a2dd95SBruce Richardson 
18999a2dd95SBruce Richardson /**
19099a2dd95SBruce Richardson  * vhost_crypto struct is used to maintain a number of virtio_cryptos and
19199a2dd95SBruce Richardson  * one DPDK crypto device that deals with all crypto workloads. It is declared
19299a2dd95SBruce Richardson  * here and defined in vhost_crypto.c
19399a2dd95SBruce Richardson  */
194*c6552d9aSTyler Retzlaff struct __rte_cache_aligned vhost_crypto {
19599a2dd95SBruce Richardson 	/** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
19699a2dd95SBruce Richardson 	 *  session ID.
19799a2dd95SBruce Richardson 	 */
19899a2dd95SBruce Richardson 	struct rte_hash *session_map;
19999a2dd95SBruce Richardson 	struct rte_mempool *mbuf_pool;
20099a2dd95SBruce Richardson 	struct rte_mempool *sess_pool;
20199a2dd95SBruce Richardson 	struct rte_mempool *wb_pool;
20299a2dd95SBruce Richardson 
20399a2dd95SBruce Richardson 	/** DPDK cryptodev ID */
20499a2dd95SBruce Richardson 	uint8_t cid;
20599a2dd95SBruce Richardson 	uint16_t nb_qps;
20699a2dd95SBruce Richardson 
20799a2dd95SBruce Richardson 	uint64_t last_session_id;
20899a2dd95SBruce Richardson 
20999a2dd95SBruce Richardson 	uint64_t cache_session_id;
21099a2dd95SBruce Richardson 	struct rte_cryptodev_sym_session *cache_session;
21199a2dd95SBruce Richardson 	/** socket id for the device */
21299a2dd95SBruce Richardson 	int socket_id;
21399a2dd95SBruce Richardson 
21499a2dd95SBruce Richardson 	struct virtio_net *dev;
21599a2dd95SBruce Richardson 
21699a2dd95SBruce Richardson 	uint8_t option;
217*c6552d9aSTyler Retzlaff };
21899a2dd95SBruce Richardson 
21999a2dd95SBruce Richardson struct vhost_crypto_writeback_data {
22099a2dd95SBruce Richardson 	uint8_t *src;
22199a2dd95SBruce Richardson 	uint8_t *dst;
22299a2dd95SBruce Richardson 	uint64_t len;
22399a2dd95SBruce Richardson 	struct vhost_crypto_writeback_data *next;
22499a2dd95SBruce Richardson };
22599a2dd95SBruce Richardson 
22699a2dd95SBruce Richardson struct vhost_crypto_data_req {
22799a2dd95SBruce Richardson 	struct vring_desc *head;
22899a2dd95SBruce Richardson 	struct virtio_net *dev;
22999a2dd95SBruce Richardson 	struct virtio_crypto_inhdr *inhdr;
23099a2dd95SBruce Richardson 	struct vhost_virtqueue *vq;
23199a2dd95SBruce Richardson 	struct vhost_crypto_writeback_data *wb;
23299a2dd95SBruce Richardson 	struct rte_mempool *wb_pool;
23399a2dd95SBruce Richardson 	uint16_t desc_idx;
23499a2dd95SBruce Richardson 	uint16_t len;
23599a2dd95SBruce Richardson 	uint16_t zero_copy;
23699a2dd95SBruce Richardson };
23799a2dd95SBruce Richardson 
23899a2dd95SBruce Richardson static int
transform_cipher_param(struct rte_crypto_sym_xform * xform,VhostUserCryptoSessionParam * param)23999a2dd95SBruce Richardson transform_cipher_param(struct rte_crypto_sym_xform *xform,
24099a2dd95SBruce Richardson 		VhostUserCryptoSessionParam *param)
24199a2dd95SBruce Richardson {
24299a2dd95SBruce Richardson 	int ret;
24399a2dd95SBruce Richardson 
24499a2dd95SBruce Richardson 	ret = cipher_algo_transform(param->cipher_algo, &xform->cipher.algo);
24599a2dd95SBruce Richardson 	if (unlikely(ret < 0))
24699a2dd95SBruce Richardson 		return ret;
24799a2dd95SBruce Richardson 
24899a2dd95SBruce Richardson 	if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
249ae282b06SDavid Marchand 		VC_LOG_DBG("Invalid cipher key length");
25099a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_BADMSG;
25199a2dd95SBruce Richardson 	}
25299a2dd95SBruce Richardson 
25399a2dd95SBruce Richardson 	xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
25499a2dd95SBruce Richardson 	xform->cipher.key.length = param->cipher_key_len;
25599a2dd95SBruce Richardson 	if (xform->cipher.key.length > 0)
25699a2dd95SBruce Richardson 		xform->cipher.key.data = param->cipher_key_buf;
25799a2dd95SBruce Richardson 	if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
25899a2dd95SBruce Richardson 		xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
25999a2dd95SBruce Richardson 	else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
26099a2dd95SBruce Richardson 		xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
26199a2dd95SBruce Richardson 	else {
26299a2dd95SBruce Richardson 		VC_LOG_DBG("Bad operation type");
26399a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_BADMSG;
26499a2dd95SBruce Richardson 	}
26599a2dd95SBruce Richardson 
26699a2dd95SBruce Richardson 	ret = get_iv_len(xform->cipher.algo);
26799a2dd95SBruce Richardson 	if (unlikely(ret < 0))
26899a2dd95SBruce Richardson 		return ret;
26999a2dd95SBruce Richardson 	xform->cipher.iv.length = (uint16_t)ret;
27099a2dd95SBruce Richardson 	xform->cipher.iv.offset = IV_OFFSET;
27199a2dd95SBruce Richardson 	return 0;
27299a2dd95SBruce Richardson }
27399a2dd95SBruce Richardson 
27499a2dd95SBruce Richardson static int
transform_chain_param(struct rte_crypto_sym_xform * xforms,VhostUserCryptoSessionParam * param)27599a2dd95SBruce Richardson transform_chain_param(struct rte_crypto_sym_xform *xforms,
27699a2dd95SBruce Richardson 		VhostUserCryptoSessionParam *param)
27799a2dd95SBruce Richardson {
27899a2dd95SBruce Richardson 	struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
27999a2dd95SBruce Richardson 	int ret;
28099a2dd95SBruce Richardson 
28199a2dd95SBruce Richardson 	switch (param->chaining_dir) {
28299a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
28399a2dd95SBruce Richardson 		xform_auth = xforms;
28499a2dd95SBruce Richardson 		xform_cipher = xforms->next;
28599a2dd95SBruce Richardson 		xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
28699a2dd95SBruce Richardson 		xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
28799a2dd95SBruce Richardson 		break;
28899a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
28999a2dd95SBruce Richardson 		xform_cipher = xforms;
29099a2dd95SBruce Richardson 		xform_auth = xforms->next;
29199a2dd95SBruce Richardson 		xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
29299a2dd95SBruce Richardson 		xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
29399a2dd95SBruce Richardson 		break;
29499a2dd95SBruce Richardson 	default:
29599a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_BADMSG;
29699a2dd95SBruce Richardson 	}
29799a2dd95SBruce Richardson 
29899a2dd95SBruce Richardson 	/* cipher */
29999a2dd95SBruce Richardson 	ret = cipher_algo_transform(param->cipher_algo,
30099a2dd95SBruce Richardson 			&xform_cipher->cipher.algo);
30199a2dd95SBruce Richardson 	if (unlikely(ret < 0))
30299a2dd95SBruce Richardson 		return ret;
30399a2dd95SBruce Richardson 
30499a2dd95SBruce Richardson 	if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
305ae282b06SDavid Marchand 		VC_LOG_DBG("Invalid cipher key length");
30699a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_BADMSG;
30799a2dd95SBruce Richardson 	}
30899a2dd95SBruce Richardson 
30999a2dd95SBruce Richardson 	xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
31099a2dd95SBruce Richardson 	xform_cipher->cipher.key.length = param->cipher_key_len;
31199a2dd95SBruce Richardson 	xform_cipher->cipher.key.data = param->cipher_key_buf;
31299a2dd95SBruce Richardson 	ret = get_iv_len(xform_cipher->cipher.algo);
31399a2dd95SBruce Richardson 	if (unlikely(ret < 0))
31499a2dd95SBruce Richardson 		return ret;
31599a2dd95SBruce Richardson 	xform_cipher->cipher.iv.length = (uint16_t)ret;
31699a2dd95SBruce Richardson 	xform_cipher->cipher.iv.offset = IV_OFFSET;
31799a2dd95SBruce Richardson 
31899a2dd95SBruce Richardson 	/* auth */
31999a2dd95SBruce Richardson 	xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
32099a2dd95SBruce Richardson 	ret = auth_algo_transform(param->hash_algo, &xform_auth->auth.algo);
32199a2dd95SBruce Richardson 	if (unlikely(ret < 0))
32299a2dd95SBruce Richardson 		return ret;
32399a2dd95SBruce Richardson 
32499a2dd95SBruce Richardson 	if (param->auth_key_len > VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH) {
325ae282b06SDavid Marchand 		VC_LOG_DBG("Invalid auth key length");
32699a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_BADMSG;
32799a2dd95SBruce Richardson 	}
32899a2dd95SBruce Richardson 
32999a2dd95SBruce Richardson 	xform_auth->auth.digest_length = param->digest_len;
33099a2dd95SBruce Richardson 	xform_auth->auth.key.length = param->auth_key_len;
33199a2dd95SBruce Richardson 	xform_auth->auth.key.data = param->auth_key_buf;
33299a2dd95SBruce Richardson 
33399a2dd95SBruce Richardson 	return 0;
33499a2dd95SBruce Richardson }
33599a2dd95SBruce Richardson 
33699a2dd95SBruce Richardson static void
vhost_crypto_create_sess(struct vhost_crypto * vcrypto,VhostUserCryptoSessionParam * sess_param)33799a2dd95SBruce Richardson vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
33899a2dd95SBruce Richardson 		VhostUserCryptoSessionParam *sess_param)
33999a2dd95SBruce Richardson {
34099a2dd95SBruce Richardson 	struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
34199a2dd95SBruce Richardson 	struct rte_cryptodev_sym_session *session;
34299a2dd95SBruce Richardson 	int ret;
34399a2dd95SBruce Richardson 
34499a2dd95SBruce Richardson 	switch (sess_param->op_type) {
34599a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_SYM_OP_NONE:
34699a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_SYM_OP_CIPHER:
34799a2dd95SBruce Richardson 		ret = transform_cipher_param(&xform1, sess_param);
34899a2dd95SBruce Richardson 		if (unlikely(ret)) {
34999a2dd95SBruce Richardson 			VC_LOG_ERR("Error transform session msg (%i)", ret);
35099a2dd95SBruce Richardson 			sess_param->session_id = ret;
35199a2dd95SBruce Richardson 			return;
35299a2dd95SBruce Richardson 		}
35399a2dd95SBruce Richardson 		break;
35499a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
35599a2dd95SBruce Richardson 		if (unlikely(sess_param->hash_mode !=
35699a2dd95SBruce Richardson 				VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
35799a2dd95SBruce Richardson 			sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
35899a2dd95SBruce Richardson 			VC_LOG_ERR("Error transform session message (%i)",
35999a2dd95SBruce Richardson 					-VIRTIO_CRYPTO_NOTSUPP);
36099a2dd95SBruce Richardson 			return;
36199a2dd95SBruce Richardson 		}
36299a2dd95SBruce Richardson 
36399a2dd95SBruce Richardson 		xform1.next = &xform2;
36499a2dd95SBruce Richardson 
36599a2dd95SBruce Richardson 		ret = transform_chain_param(&xform1, sess_param);
36699a2dd95SBruce Richardson 		if (unlikely(ret)) {
36799a2dd95SBruce Richardson 			VC_LOG_ERR("Error transform session message (%i)", ret);
36899a2dd95SBruce Richardson 			sess_param->session_id = ret;
36999a2dd95SBruce Richardson 			return;
37099a2dd95SBruce Richardson 		}
37199a2dd95SBruce Richardson 
37299a2dd95SBruce Richardson 		break;
37399a2dd95SBruce Richardson 	default:
37499a2dd95SBruce Richardson 		VC_LOG_ERR("Algorithm not yet supported");
37599a2dd95SBruce Richardson 		sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
37699a2dd95SBruce Richardson 		return;
37799a2dd95SBruce Richardson 	}
37899a2dd95SBruce Richardson 
379bdce2564SAkhil Goyal 	session = rte_cryptodev_sym_session_create(vcrypto->cid, &xform1,
380bdce2564SAkhil Goyal 			vcrypto->sess_pool);
38199a2dd95SBruce Richardson 	if (!session) {
38299a2dd95SBruce Richardson 		VC_LOG_ERR("Failed to create session");
38399a2dd95SBruce Richardson 		sess_param->session_id = -VIRTIO_CRYPTO_ERR;
38499a2dd95SBruce Richardson 		return;
38599a2dd95SBruce Richardson 	}
38699a2dd95SBruce Richardson 
38799a2dd95SBruce Richardson 	/* insert hash to map */
38899a2dd95SBruce Richardson 	if (rte_hash_add_key_data(vcrypto->session_map,
38999a2dd95SBruce Richardson 			&vcrypto->last_session_id, session) < 0) {
39099a2dd95SBruce Richardson 		VC_LOG_ERR("Failed to insert session to hash table");
39199a2dd95SBruce Richardson 
392bdce2564SAkhil Goyal 		if (rte_cryptodev_sym_session_free(vcrypto->cid, session) < 0)
39399a2dd95SBruce Richardson 			VC_LOG_ERR("Failed to free session");
39499a2dd95SBruce Richardson 		sess_param->session_id = -VIRTIO_CRYPTO_ERR;
39599a2dd95SBruce Richardson 		return;
39699a2dd95SBruce Richardson 	}
39799a2dd95SBruce Richardson 
39899a2dd95SBruce Richardson 	VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
39999a2dd95SBruce Richardson 			vcrypto->last_session_id, vcrypto->dev->vid);
40099a2dd95SBruce Richardson 
40199a2dd95SBruce Richardson 	sess_param->session_id = vcrypto->last_session_id;
40299a2dd95SBruce Richardson 	vcrypto->last_session_id++;
40399a2dd95SBruce Richardson }
40499a2dd95SBruce Richardson 
40599a2dd95SBruce Richardson static int
vhost_crypto_close_sess(struct vhost_crypto * vcrypto,uint64_t session_id)40699a2dd95SBruce Richardson vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
40799a2dd95SBruce Richardson {
40899a2dd95SBruce Richardson 	struct rte_cryptodev_sym_session *session;
40999a2dd95SBruce Richardson 	uint64_t sess_id = session_id;
41099a2dd95SBruce Richardson 	int ret;
41199a2dd95SBruce Richardson 
41299a2dd95SBruce Richardson 	ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
41399a2dd95SBruce Richardson 			(void **)&session);
41499a2dd95SBruce Richardson 
41599a2dd95SBruce Richardson 	if (unlikely(ret < 0)) {
41699a2dd95SBruce Richardson 		VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
41799a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_INVSESS;
41899a2dd95SBruce Richardson 	}
41999a2dd95SBruce Richardson 
420bdce2564SAkhil Goyal 	if (rte_cryptodev_sym_session_free(vcrypto->cid, session) < 0) {
42199a2dd95SBruce Richardson 		VC_LOG_DBG("Failed to free session");
42299a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_ERR;
42399a2dd95SBruce Richardson 	}
42499a2dd95SBruce Richardson 
42599a2dd95SBruce Richardson 	if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
42699a2dd95SBruce Richardson 		VC_LOG_DBG("Failed to delete session from hash table.");
42799a2dd95SBruce Richardson 		return -VIRTIO_CRYPTO_ERR;
42899a2dd95SBruce Richardson 	}
42999a2dd95SBruce Richardson 
43099a2dd95SBruce Richardson 	VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
43199a2dd95SBruce Richardson 			vcrypto->dev->vid);
43299a2dd95SBruce Richardson 
43399a2dd95SBruce Richardson 	return 0;
43499a2dd95SBruce Richardson }
43599a2dd95SBruce Richardson 
43699a2dd95SBruce Richardson static enum rte_vhost_msg_result
vhost_crypto_msg_post_handler(int vid,void * msg)43799a2dd95SBruce Richardson vhost_crypto_msg_post_handler(int vid, void *msg)
43899a2dd95SBruce Richardson {
43999a2dd95SBruce Richardson 	struct virtio_net *dev = get_device(vid);
44099a2dd95SBruce Richardson 	struct vhost_crypto *vcrypto;
4415e0099dcSChristophe Fontaine 	struct vhu_msg_context *ctx = msg;
44299a2dd95SBruce Richardson 	enum rte_vhost_msg_result ret = RTE_VHOST_MSG_RESULT_OK;
44399a2dd95SBruce Richardson 
44499a2dd95SBruce Richardson 	if (dev == NULL) {
44599a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid vid %i", vid);
44699a2dd95SBruce Richardson 		return RTE_VHOST_MSG_RESULT_ERR;
44799a2dd95SBruce Richardson 	}
44899a2dd95SBruce Richardson 
44999a2dd95SBruce Richardson 	vcrypto = dev->extern_data;
45099a2dd95SBruce Richardson 	if (vcrypto == NULL) {
45199a2dd95SBruce Richardson 		VC_LOG_ERR("Cannot find required data, is it initialized?");
45299a2dd95SBruce Richardson 		return RTE_VHOST_MSG_RESULT_ERR;
45399a2dd95SBruce Richardson 	}
45499a2dd95SBruce Richardson 
45571998eb6SNobuhiro Miki 	switch (ctx->msg.request.frontend) {
45699a2dd95SBruce Richardson 	case VHOST_USER_CRYPTO_CREATE_SESS:
45799a2dd95SBruce Richardson 		vhost_crypto_create_sess(vcrypto,
4585e0099dcSChristophe Fontaine 				&ctx->msg.payload.crypto_session);
4595e0099dcSChristophe Fontaine 		ctx->fd_num = 0;
46099a2dd95SBruce Richardson 		ret = RTE_VHOST_MSG_RESULT_REPLY;
46199a2dd95SBruce Richardson 		break;
46299a2dd95SBruce Richardson 	case VHOST_USER_CRYPTO_CLOSE_SESS:
4635e0099dcSChristophe Fontaine 		if (vhost_crypto_close_sess(vcrypto, ctx->msg.payload.u64))
46499a2dd95SBruce Richardson 			ret = RTE_VHOST_MSG_RESULT_ERR;
46599a2dd95SBruce Richardson 		break;
46699a2dd95SBruce Richardson 	default:
46799a2dd95SBruce Richardson 		ret = RTE_VHOST_MSG_RESULT_NOT_HANDLED;
46899a2dd95SBruce Richardson 		break;
46999a2dd95SBruce Richardson 	}
47099a2dd95SBruce Richardson 
47199a2dd95SBruce Richardson 	return ret;
47299a2dd95SBruce Richardson }
47399a2dd95SBruce Richardson 
47499a2dd95SBruce Richardson static __rte_always_inline struct vhost_crypto_desc *
find_write_desc(struct vhost_crypto_desc * head,struct vhost_crypto_desc * desc,uint32_t max_n_descs)47599a2dd95SBruce Richardson find_write_desc(struct vhost_crypto_desc *head, struct vhost_crypto_desc *desc,
47699a2dd95SBruce Richardson 		uint32_t max_n_descs)
47799a2dd95SBruce Richardson {
47899a2dd95SBruce Richardson 	if (desc < head)
47999a2dd95SBruce Richardson 		return NULL;
48099a2dd95SBruce Richardson 
48199a2dd95SBruce Richardson 	while (desc - head < (int)max_n_descs) {
48299a2dd95SBruce Richardson 		if (desc->flags & VRING_DESC_F_WRITE)
48399a2dd95SBruce Richardson 			return desc;
48499a2dd95SBruce Richardson 		desc++;
48599a2dd95SBruce Richardson 	}
48699a2dd95SBruce Richardson 
48799a2dd95SBruce Richardson 	return NULL;
48899a2dd95SBruce Richardson }
48999a2dd95SBruce Richardson 
49099a2dd95SBruce Richardson static __rte_always_inline struct virtio_crypto_inhdr *
reach_inhdr(struct vhost_crypto_data_req * vc_req,struct vhost_crypto_desc * head,uint32_t max_n_descs)49199a2dd95SBruce Richardson reach_inhdr(struct vhost_crypto_data_req *vc_req,
49299a2dd95SBruce Richardson 		struct vhost_crypto_desc *head,
49399a2dd95SBruce Richardson 		uint32_t max_n_descs)
494bf42fb30SDavid Marchand 	__rte_shared_locks_required(&vc_req->vq->iotlb_lock)
49599a2dd95SBruce Richardson {
49699a2dd95SBruce Richardson 	struct virtio_crypto_inhdr *inhdr;
49799a2dd95SBruce Richardson 	struct vhost_crypto_desc *last = head + (max_n_descs - 1);
49899a2dd95SBruce Richardson 	uint64_t dlen = last->len;
49999a2dd95SBruce Richardson 
50099a2dd95SBruce Richardson 	if (unlikely(dlen != sizeof(*inhdr)))
50199a2dd95SBruce Richardson 		return NULL;
50299a2dd95SBruce Richardson 
50399a2dd95SBruce Richardson 	inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, last->addr,
50499a2dd95SBruce Richardson 			&dlen, VHOST_ACCESS_WO);
50599a2dd95SBruce Richardson 	if (unlikely(!inhdr || dlen != last->len))
50699a2dd95SBruce Richardson 		return NULL;
50799a2dd95SBruce Richardson 
50899a2dd95SBruce Richardson 	return inhdr;
50999a2dd95SBruce Richardson }
51099a2dd95SBruce Richardson 
51199a2dd95SBruce Richardson static __rte_always_inline int
move_desc(struct vhost_crypto_desc * head,struct vhost_crypto_desc ** cur_desc,uint32_t size,uint32_t max_n_descs)51299a2dd95SBruce Richardson move_desc(struct vhost_crypto_desc *head,
51399a2dd95SBruce Richardson 		struct vhost_crypto_desc **cur_desc,
51499a2dd95SBruce Richardson 		uint32_t size, uint32_t max_n_descs)
51599a2dd95SBruce Richardson {
51699a2dd95SBruce Richardson 	struct vhost_crypto_desc *desc = *cur_desc;
51799a2dd95SBruce Richardson 	int left = size - desc->len;
51899a2dd95SBruce Richardson 
51999a2dd95SBruce Richardson 	while (desc->flags & VRING_DESC_F_NEXT && left > 0 &&
52099a2dd95SBruce Richardson 			desc >= head &&
52199a2dd95SBruce Richardson 			desc - head < (int)max_n_descs) {
52299a2dd95SBruce Richardson 		desc++;
52399a2dd95SBruce Richardson 		left -= desc->len;
52499a2dd95SBruce Richardson 	}
52599a2dd95SBruce Richardson 
52699a2dd95SBruce Richardson 	if (unlikely(left > 0))
52799a2dd95SBruce Richardson 		return -1;
52899a2dd95SBruce Richardson 
52999a2dd95SBruce Richardson 	if (unlikely(head - desc == (int)max_n_descs))
53099a2dd95SBruce Richardson 		*cur_desc = NULL;
53199a2dd95SBruce Richardson 	else
53299a2dd95SBruce Richardson 		*cur_desc = desc + 1;
53399a2dd95SBruce Richardson 
53499a2dd95SBruce Richardson 	return 0;
53599a2dd95SBruce Richardson }
53699a2dd95SBruce Richardson 
53799a2dd95SBruce Richardson static __rte_always_inline void *
get_data_ptr(struct vhost_crypto_data_req * vc_req,struct vhost_crypto_desc * cur_desc,uint8_t perm)53899a2dd95SBruce Richardson get_data_ptr(struct vhost_crypto_data_req *vc_req,
53999a2dd95SBruce Richardson 		struct vhost_crypto_desc *cur_desc,
54099a2dd95SBruce Richardson 		uint8_t perm)
541bf42fb30SDavid Marchand 	__rte_shared_locks_required(&vc_req->vq->iotlb_lock)
54299a2dd95SBruce Richardson {
54399a2dd95SBruce Richardson 	void *data;
54499a2dd95SBruce Richardson 	uint64_t dlen = cur_desc->len;
54599a2dd95SBruce Richardson 
54699a2dd95SBruce Richardson 	data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
54799a2dd95SBruce Richardson 	if (unlikely(!data || dlen != cur_desc->len)) {
54899a2dd95SBruce Richardson 		VC_LOG_ERR("Failed to map object");
54999a2dd95SBruce Richardson 		return NULL;
55099a2dd95SBruce Richardson 	}
55199a2dd95SBruce Richardson 
55299a2dd95SBruce Richardson 	return data;
55399a2dd95SBruce Richardson }
55499a2dd95SBruce Richardson 
5554414bb67SDavid Marchand static __rte_always_inline uint32_t
copy_data_from_desc(void * dst,struct vhost_crypto_data_req * vc_req,struct vhost_crypto_desc * desc,uint32_t size)5564414bb67SDavid Marchand copy_data_from_desc(void *dst, struct vhost_crypto_data_req *vc_req,
5574414bb67SDavid Marchand 	struct vhost_crypto_desc *desc, uint32_t size)
558bf42fb30SDavid Marchand 	__rte_shared_locks_required(&vc_req->vq->iotlb_lock)
5594414bb67SDavid Marchand {
5604414bb67SDavid Marchand 	uint64_t remain;
5614414bb67SDavid Marchand 	uint64_t addr;
5624414bb67SDavid Marchand 
5634414bb67SDavid Marchand 	remain = RTE_MIN(desc->len, size);
5644414bb67SDavid Marchand 	addr = desc->addr;
5654414bb67SDavid Marchand 	do {
5664414bb67SDavid Marchand 		uint64_t len;
5674414bb67SDavid Marchand 		void *src;
5684414bb67SDavid Marchand 
5694414bb67SDavid Marchand 		len = remain;
5704414bb67SDavid Marchand 		src = IOVA_TO_VVA(void *, vc_req, addr, &len, VHOST_ACCESS_RO);
5714414bb67SDavid Marchand 		if (unlikely(src == NULL || len == 0))
5724414bb67SDavid Marchand 			return 0;
5734414bb67SDavid Marchand 
5744414bb67SDavid Marchand 		rte_memcpy(dst, src, len);
5754414bb67SDavid Marchand 		remain -= len;
5764414bb67SDavid Marchand 		/* cast is needed for 32-bit architecture */
5774414bb67SDavid Marchand 		dst = RTE_PTR_ADD(dst, (size_t)len);
5784414bb67SDavid Marchand 		addr += len;
5794414bb67SDavid Marchand 	} while (unlikely(remain != 0));
5804414bb67SDavid Marchand 
5814414bb67SDavid Marchand 	return RTE_MIN(desc->len, size);
5824414bb67SDavid Marchand }
5834414bb67SDavid Marchand 
5844414bb67SDavid Marchand 
58599a2dd95SBruce Richardson static __rte_always_inline int
copy_data(void * data,struct vhost_crypto_data_req * vc_req,struct vhost_crypto_desc * head,struct vhost_crypto_desc ** cur_desc,uint32_t size,uint32_t max_n_descs)5864414bb67SDavid Marchand copy_data(void *data, struct vhost_crypto_data_req *vc_req,
5874414bb67SDavid Marchand 	struct vhost_crypto_desc *head, struct vhost_crypto_desc **cur_desc,
58899a2dd95SBruce Richardson 	uint32_t size, uint32_t max_n_descs)
589bf42fb30SDavid Marchand 	__rte_shared_locks_required(&vc_req->vq->iotlb_lock)
59099a2dd95SBruce Richardson {
59199a2dd95SBruce Richardson 	struct vhost_crypto_desc *desc = *cur_desc;
5924414bb67SDavid Marchand 	uint32_t left = size;
59399a2dd95SBruce Richardson 
5944414bb67SDavid Marchand 	do {
5954414bb67SDavid Marchand 		uint32_t copied;
5964414bb67SDavid Marchand 
5974414bb67SDavid Marchand 		copied = copy_data_from_desc(data, vc_req, desc, left);
5984414bb67SDavid Marchand 		if (copied == 0)
59999a2dd95SBruce Richardson 			return -1;
6004414bb67SDavid Marchand 		left -= copied;
6014414bb67SDavid Marchand 		data = RTE_PTR_ADD(data, copied);
6022fbada91SDavid Marchand 	} while (left != 0 && ++desc < head + max_n_descs);
6034414bb67SDavid Marchand 
6044414bb67SDavid Marchand 	if (unlikely(left != 0))
60599a2dd95SBruce Richardson 		return -1;
60699a2dd95SBruce Richardson 
6074414bb67SDavid Marchand 	if (unlikely(desc == head + max_n_descs))
60899a2dd95SBruce Richardson 		*cur_desc = NULL;
60999a2dd95SBruce Richardson 	else
61099a2dd95SBruce Richardson 		*cur_desc = desc + 1;
61199a2dd95SBruce Richardson 
61299a2dd95SBruce Richardson 	return 0;
61399a2dd95SBruce Richardson }
61499a2dd95SBruce Richardson 
61599a2dd95SBruce Richardson static void
write_back_data(struct vhost_crypto_data_req * vc_req)61699a2dd95SBruce Richardson write_back_data(struct vhost_crypto_data_req *vc_req)
61799a2dd95SBruce Richardson {
61899a2dd95SBruce Richardson 	struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
61999a2dd95SBruce Richardson 
62099a2dd95SBruce Richardson 	while (wb_data) {
62199a2dd95SBruce Richardson 		rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
62299a2dd95SBruce Richardson 		memset(wb_data->src, 0, wb_data->len);
62399a2dd95SBruce Richardson 		wb_last = wb_data;
62499a2dd95SBruce Richardson 		wb_data = wb_data->next;
62599a2dd95SBruce Richardson 		rte_mempool_put(vc_req->wb_pool, wb_last);
62699a2dd95SBruce Richardson 	}
62799a2dd95SBruce Richardson }
62899a2dd95SBruce Richardson 
62999a2dd95SBruce Richardson static void
free_wb_data(struct vhost_crypto_writeback_data * wb_data,struct rte_mempool * mp)63099a2dd95SBruce Richardson free_wb_data(struct vhost_crypto_writeback_data *wb_data,
63199a2dd95SBruce Richardson 		struct rte_mempool *mp)
63299a2dd95SBruce Richardson {
63399a2dd95SBruce Richardson 	while (wb_data->next != NULL)
63499a2dd95SBruce Richardson 		free_wb_data(wb_data->next, mp);
63599a2dd95SBruce Richardson 
63699a2dd95SBruce Richardson 	rte_mempool_put(mp, wb_data);
63799a2dd95SBruce Richardson }
63899a2dd95SBruce Richardson 
63999a2dd95SBruce Richardson /**
64099a2dd95SBruce Richardson  * The function will allocate a vhost_crypto_writeback_data linked list
64199a2dd95SBruce Richardson  * containing the source and destination data pointers for the write back
64299a2dd95SBruce Richardson  * operation after dequeued from Cryptodev PMD queues.
64399a2dd95SBruce Richardson  *
64499a2dd95SBruce Richardson  * @param vc_req
64599a2dd95SBruce Richardson  *   The vhost crypto data request pointer
64699a2dd95SBruce Richardson  * @param cur_desc
64799a2dd95SBruce Richardson  *   The pointer of the current in use descriptor pointer. The content of
64899a2dd95SBruce Richardson  *   cur_desc is expected to be updated after the function execution.
64999a2dd95SBruce Richardson  * @param end_wb_data
65099a2dd95SBruce Richardson  *   The last write back data element to be returned. It is used only in cipher
65199a2dd95SBruce Richardson  *   and hash chain operations.
65299a2dd95SBruce Richardson  * @param src
65399a2dd95SBruce Richardson  *   The source data pointer
65499a2dd95SBruce Richardson  * @param offset
65599a2dd95SBruce Richardson  *   The offset to both source and destination data. For source data the offset
65699a2dd95SBruce Richardson  *   is the number of bytes between src and start point of cipher operation. For
65799a2dd95SBruce Richardson  *   destination data the offset is the number of bytes from *cur_desc->addr
65899a2dd95SBruce Richardson  *   to the point where the src will be written to.
65999a2dd95SBruce Richardson  * @param write_back_len
66099a2dd95SBruce Richardson  *   The size of the write back length.
66199a2dd95SBruce Richardson  * @return
66299a2dd95SBruce Richardson  *   The pointer to the start of the write back data linked list.
66399a2dd95SBruce Richardson  */
66499a2dd95SBruce Richardson static __rte_always_inline struct vhost_crypto_writeback_data *
prepare_write_back_data(struct vhost_crypto_data_req * vc_req,struct vhost_crypto_desc * head_desc,struct vhost_crypto_desc ** cur_desc,struct vhost_crypto_writeback_data ** end_wb_data,uint8_t * src,uint32_t offset,uint64_t write_back_len,uint32_t max_n_descs)66599a2dd95SBruce Richardson prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
66699a2dd95SBruce Richardson 		struct vhost_crypto_desc *head_desc,
66799a2dd95SBruce Richardson 		struct vhost_crypto_desc **cur_desc,
66899a2dd95SBruce Richardson 		struct vhost_crypto_writeback_data **end_wb_data,
66999a2dd95SBruce Richardson 		uint8_t *src,
67099a2dd95SBruce Richardson 		uint32_t offset,
67199a2dd95SBruce Richardson 		uint64_t write_back_len,
67299a2dd95SBruce Richardson 		uint32_t max_n_descs)
673bf42fb30SDavid Marchand 	__rte_shared_locks_required(&vc_req->vq->iotlb_lock)
67499a2dd95SBruce Richardson {
67599a2dd95SBruce Richardson 	struct vhost_crypto_writeback_data *wb_data, *head;
67699a2dd95SBruce Richardson 	struct vhost_crypto_desc *desc = *cur_desc;
67799a2dd95SBruce Richardson 	uint64_t dlen;
67899a2dd95SBruce Richardson 	uint8_t *dst;
67999a2dd95SBruce Richardson 	int ret;
68099a2dd95SBruce Richardson 
68199a2dd95SBruce Richardson 	ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
68299a2dd95SBruce Richardson 	if (unlikely(ret < 0)) {
68399a2dd95SBruce Richardson 		VC_LOG_ERR("no memory");
68499a2dd95SBruce Richardson 		goto error_exit;
68599a2dd95SBruce Richardson 	}
68699a2dd95SBruce Richardson 
68799a2dd95SBruce Richardson 	wb_data = head;
68899a2dd95SBruce Richardson 
68999a2dd95SBruce Richardson 	if (likely(desc->len > offset)) {
69099a2dd95SBruce Richardson 		wb_data->src = src + offset;
69199a2dd95SBruce Richardson 		dlen = desc->len;
69299a2dd95SBruce Richardson 		dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
69399a2dd95SBruce Richardson 			&dlen, VHOST_ACCESS_RW);
69499a2dd95SBruce Richardson 		if (unlikely(!dst || dlen != desc->len)) {
69599a2dd95SBruce Richardson 			VC_LOG_ERR("Failed to map descriptor");
69699a2dd95SBruce Richardson 			goto error_exit;
69799a2dd95SBruce Richardson 		}
69899a2dd95SBruce Richardson 
69999a2dd95SBruce Richardson 		wb_data->dst = dst + offset;
70099a2dd95SBruce Richardson 		wb_data->len = RTE_MIN(dlen - offset, write_back_len);
70199a2dd95SBruce Richardson 		write_back_len -= wb_data->len;
70299a2dd95SBruce Richardson 		src += offset + wb_data->len;
70399a2dd95SBruce Richardson 		offset = 0;
70499a2dd95SBruce Richardson 
70599a2dd95SBruce Richardson 		if (unlikely(write_back_len)) {
70699a2dd95SBruce Richardson 			ret = rte_mempool_get(vc_req->wb_pool,
70799a2dd95SBruce Richardson 					(void **)&(wb_data->next));
70899a2dd95SBruce Richardson 			if (unlikely(ret < 0)) {
70999a2dd95SBruce Richardson 				VC_LOG_ERR("no memory");
71099a2dd95SBruce Richardson 				goto error_exit;
71199a2dd95SBruce Richardson 			}
71299a2dd95SBruce Richardson 
71399a2dd95SBruce Richardson 			wb_data = wb_data->next;
71499a2dd95SBruce Richardson 		} else
71599a2dd95SBruce Richardson 			wb_data->next = NULL;
71699a2dd95SBruce Richardson 	} else
71799a2dd95SBruce Richardson 		offset -= desc->len;
71899a2dd95SBruce Richardson 
71999a2dd95SBruce Richardson 	while (write_back_len &&
72099a2dd95SBruce Richardson 			desc >= head_desc &&
72199a2dd95SBruce Richardson 			desc - head_desc < (int)max_n_descs) {
72299a2dd95SBruce Richardson 		desc++;
72399a2dd95SBruce Richardson 		if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
72499a2dd95SBruce Richardson 			VC_LOG_ERR("incorrect descriptor");
72599a2dd95SBruce Richardson 			goto error_exit;
72699a2dd95SBruce Richardson 		}
72799a2dd95SBruce Richardson 
72899a2dd95SBruce Richardson 		if (desc->len <= offset) {
72999a2dd95SBruce Richardson 			offset -= desc->len;
73099a2dd95SBruce Richardson 			continue;
73199a2dd95SBruce Richardson 		}
73299a2dd95SBruce Richardson 
73399a2dd95SBruce Richardson 		dlen = desc->len;
73499a2dd95SBruce Richardson 		dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
73599a2dd95SBruce Richardson 				VHOST_ACCESS_RW) + offset;
73699a2dd95SBruce Richardson 		if (unlikely(dst == NULL || dlen != desc->len)) {
73799a2dd95SBruce Richardson 			VC_LOG_ERR("Failed to map descriptor");
73899a2dd95SBruce Richardson 			goto error_exit;
73999a2dd95SBruce Richardson 		}
74099a2dd95SBruce Richardson 
74199a2dd95SBruce Richardson 		wb_data->src = src + offset;
74299a2dd95SBruce Richardson 		wb_data->dst = dst;
74399a2dd95SBruce Richardson 		wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
74499a2dd95SBruce Richardson 		write_back_len -= wb_data->len;
74599a2dd95SBruce Richardson 		src += wb_data->len;
74699a2dd95SBruce Richardson 		offset = 0;
74799a2dd95SBruce Richardson 
74899a2dd95SBruce Richardson 		if (write_back_len) {
74999a2dd95SBruce Richardson 			ret = rte_mempool_get(vc_req->wb_pool,
75099a2dd95SBruce Richardson 					(void **)&(wb_data->next));
75199a2dd95SBruce Richardson 			if (unlikely(ret < 0)) {
75299a2dd95SBruce Richardson 				VC_LOG_ERR("no memory");
75399a2dd95SBruce Richardson 				goto error_exit;
75499a2dd95SBruce Richardson 			}
75599a2dd95SBruce Richardson 
75699a2dd95SBruce Richardson 			wb_data = wb_data->next;
75799a2dd95SBruce Richardson 		} else
75899a2dd95SBruce Richardson 			wb_data->next = NULL;
75999a2dd95SBruce Richardson 	}
76099a2dd95SBruce Richardson 
76199a2dd95SBruce Richardson 	if (unlikely(desc - head_desc == (int)max_n_descs))
76299a2dd95SBruce Richardson 		*cur_desc = NULL;
76399a2dd95SBruce Richardson 	else
76499a2dd95SBruce Richardson 		*cur_desc = desc + 1;
76599a2dd95SBruce Richardson 
76699a2dd95SBruce Richardson 	*end_wb_data = wb_data;
76799a2dd95SBruce Richardson 
76899a2dd95SBruce Richardson 	return head;
76999a2dd95SBruce Richardson 
77099a2dd95SBruce Richardson error_exit:
77199a2dd95SBruce Richardson 	if (head)
77299a2dd95SBruce Richardson 		free_wb_data(head, vc_req->wb_pool);
77399a2dd95SBruce Richardson 
77499a2dd95SBruce Richardson 	return NULL;
77599a2dd95SBruce Richardson }
77699a2dd95SBruce Richardson 
77799a2dd95SBruce Richardson static __rte_always_inline uint8_t
vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req * req)77899a2dd95SBruce Richardson vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req *req)
77999a2dd95SBruce Richardson {
78099a2dd95SBruce Richardson 	if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
78199a2dd95SBruce Richardson 		(req->para.src_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE) &&
78299a2dd95SBruce Richardson 		(req->para.dst_data_len >= req->para.src_data_len) &&
78399a2dd95SBruce Richardson 		(req->para.dst_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE)))
78499a2dd95SBruce Richardson 		return VIRTIO_CRYPTO_OK;
78599a2dd95SBruce Richardson 	return VIRTIO_CRYPTO_BADMSG;
78699a2dd95SBruce Richardson }
78799a2dd95SBruce Richardson 
78899a2dd95SBruce Richardson static __rte_always_inline uint8_t
prepare_sym_cipher_op(struct vhost_crypto * vcrypto,struct rte_crypto_op * op,struct vhost_crypto_data_req * vc_req,struct virtio_crypto_cipher_data_req * cipher,struct vhost_crypto_desc * head,uint32_t max_n_descs)78999a2dd95SBruce Richardson prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
79099a2dd95SBruce Richardson 		struct vhost_crypto_data_req *vc_req,
79199a2dd95SBruce Richardson 		struct virtio_crypto_cipher_data_req *cipher,
79299a2dd95SBruce Richardson 		struct vhost_crypto_desc *head,
79399a2dd95SBruce Richardson 		uint32_t max_n_descs)
794bf42fb30SDavid Marchand 	__rte_shared_locks_required(&vc_req->vq->iotlb_lock)
79599a2dd95SBruce Richardson {
79699a2dd95SBruce Richardson 	struct vhost_crypto_desc *desc = head;
79799a2dd95SBruce Richardson 	struct vhost_crypto_writeback_data *ewb = NULL;
79899a2dd95SBruce Richardson 	struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
79999a2dd95SBruce Richardson 	uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
80099a2dd95SBruce Richardson 	uint8_t ret = vhost_crypto_check_cipher_request(cipher);
80199a2dd95SBruce Richardson 
80299a2dd95SBruce Richardson 	if (unlikely(ret != VIRTIO_CRYPTO_OK))
80399a2dd95SBruce Richardson 		goto error_exit;
80499a2dd95SBruce Richardson 
80599a2dd95SBruce Richardson 	/* prepare */
80699a2dd95SBruce Richardson 	/* iv */
80799a2dd95SBruce Richardson 	if (unlikely(copy_data(iv_data, vc_req, head, &desc,
80899a2dd95SBruce Richardson 			cipher->para.iv_len, max_n_descs))) {
8094414bb67SDavid Marchand 		VC_LOG_ERR("Incorrect virtio descriptor");
81099a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
81199a2dd95SBruce Richardson 		goto error_exit;
81299a2dd95SBruce Richardson 	}
81399a2dd95SBruce Richardson 
81499a2dd95SBruce Richardson 	switch (vcrypto->option) {
81599a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
81699a2dd95SBruce Richardson 		m_src->data_len = cipher->para.src_data_len;
817e811e2d7SShijith Thotton 		rte_mbuf_iova_set(m_src,
818e811e2d7SShijith Thotton 				  gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.src_data_len));
81999a2dd95SBruce Richardson 		m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
820e811e2d7SShijith Thotton 		if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) {
82199a2dd95SBruce Richardson 			VC_LOG_ERR("zero_copy may fail due to cross page data");
82299a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
82399a2dd95SBruce Richardson 			goto error_exit;
82499a2dd95SBruce Richardson 		}
82599a2dd95SBruce Richardson 
82699a2dd95SBruce Richardson 		if (unlikely(move_desc(head, &desc, cipher->para.src_data_len,
82799a2dd95SBruce Richardson 				max_n_descs) < 0)) {
82899a2dd95SBruce Richardson 			VC_LOG_ERR("Incorrect descriptor");
82999a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
83099a2dd95SBruce Richardson 			goto error_exit;
83199a2dd95SBruce Richardson 		}
83299a2dd95SBruce Richardson 
83399a2dd95SBruce Richardson 		break;
83499a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
83599a2dd95SBruce Richardson 		vc_req->wb_pool = vcrypto->wb_pool;
83699a2dd95SBruce Richardson 		m_src->data_len = cipher->para.src_data_len;
83799a2dd95SBruce Richardson 		if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
83899a2dd95SBruce Richardson 				vc_req, head, &desc, cipher->para.src_data_len,
83999a2dd95SBruce Richardson 				max_n_descs) < 0)) {
8404414bb67SDavid Marchand 			VC_LOG_ERR("Incorrect virtio descriptor");
84199a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_BADMSG;
84299a2dd95SBruce Richardson 			goto error_exit;
84399a2dd95SBruce Richardson 		}
84499a2dd95SBruce Richardson 		break;
84599a2dd95SBruce Richardson 	default:
84699a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
84799a2dd95SBruce Richardson 		goto error_exit;
84899a2dd95SBruce Richardson 	}
84999a2dd95SBruce Richardson 
85099a2dd95SBruce Richardson 	/* dst */
85199a2dd95SBruce Richardson 	desc = find_write_desc(head, desc, max_n_descs);
85299a2dd95SBruce Richardson 	if (unlikely(!desc)) {
85399a2dd95SBruce Richardson 		VC_LOG_ERR("Cannot find write location");
85499a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
85599a2dd95SBruce Richardson 		goto error_exit;
85699a2dd95SBruce Richardson 	}
85799a2dd95SBruce Richardson 
85899a2dd95SBruce Richardson 	switch (vcrypto->option) {
85999a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
860e811e2d7SShijith Thotton 		rte_mbuf_iova_set(m_dst,
861e811e2d7SShijith Thotton 				  gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.dst_data_len));
86299a2dd95SBruce Richardson 		m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
863e811e2d7SShijith Thotton 		if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) {
86499a2dd95SBruce Richardson 			VC_LOG_ERR("zero_copy may fail due to cross page data");
86599a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
86699a2dd95SBruce Richardson 			goto error_exit;
86799a2dd95SBruce Richardson 		}
86899a2dd95SBruce Richardson 
86999a2dd95SBruce Richardson 		if (unlikely(move_desc(head, &desc, cipher->para.dst_data_len,
87099a2dd95SBruce Richardson 				max_n_descs) < 0)) {
87199a2dd95SBruce Richardson 			VC_LOG_ERR("Incorrect descriptor");
87299a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
87399a2dd95SBruce Richardson 			goto error_exit;
87499a2dd95SBruce Richardson 		}
87599a2dd95SBruce Richardson 
87699a2dd95SBruce Richardson 		m_dst->data_len = cipher->para.dst_data_len;
87799a2dd95SBruce Richardson 		break;
87899a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
87999a2dd95SBruce Richardson 		vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb,
88099a2dd95SBruce Richardson 				rte_pktmbuf_mtod(m_src, uint8_t *), 0,
88199a2dd95SBruce Richardson 				cipher->para.dst_data_len, max_n_descs);
88299a2dd95SBruce Richardson 		if (unlikely(vc_req->wb == NULL)) {
88399a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
88499a2dd95SBruce Richardson 			goto error_exit;
88599a2dd95SBruce Richardson 		}
88699a2dd95SBruce Richardson 
88799a2dd95SBruce Richardson 		break;
88899a2dd95SBruce Richardson 	default:
88999a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
89099a2dd95SBruce Richardson 		goto error_exit;
89199a2dd95SBruce Richardson 	}
89299a2dd95SBruce Richardson 
89399a2dd95SBruce Richardson 	/* src data */
89499a2dd95SBruce Richardson 	op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
89599a2dd95SBruce Richardson 	op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
89699a2dd95SBruce Richardson 
89799a2dd95SBruce Richardson 	op->sym->cipher.data.offset = 0;
89899a2dd95SBruce Richardson 	op->sym->cipher.data.length = cipher->para.src_data_len;
89999a2dd95SBruce Richardson 
90099a2dd95SBruce Richardson 	vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
90199a2dd95SBruce Richardson 	if (unlikely(vc_req->inhdr == NULL)) {
90299a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
90399a2dd95SBruce Richardson 		goto error_exit;
90499a2dd95SBruce Richardson 	}
90599a2dd95SBruce Richardson 
90699a2dd95SBruce Richardson 	vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
90799a2dd95SBruce Richardson 	vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
90899a2dd95SBruce Richardson 
90999a2dd95SBruce Richardson 	return 0;
91099a2dd95SBruce Richardson 
91199a2dd95SBruce Richardson error_exit:
91299a2dd95SBruce Richardson 	if (vc_req->wb)
91399a2dd95SBruce Richardson 		free_wb_data(vc_req->wb, vc_req->wb_pool);
91499a2dd95SBruce Richardson 
91599a2dd95SBruce Richardson 	vc_req->len = INHDR_LEN;
91699a2dd95SBruce Richardson 	return ret;
91799a2dd95SBruce Richardson }
91899a2dd95SBruce Richardson 
91999a2dd95SBruce Richardson static __rte_always_inline uint8_t
vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req * req)92099a2dd95SBruce Richardson vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req)
92199a2dd95SBruce Richardson {
92299a2dd95SBruce Richardson 	if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
92399a2dd95SBruce Richardson 		(req->para.src_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
92499a2dd95SBruce Richardson 		(req->para.dst_data_len >= req->para.src_data_len) &&
92599a2dd95SBruce Richardson 		(req->para.dst_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
92699a2dd95SBruce Richardson 		(req->para.cipher_start_src_offset <
92799a2dd95SBruce Richardson 			VHOST_CRYPTO_MAX_DATA_SIZE) &&
92899a2dd95SBruce Richardson 		(req->para.len_to_cipher <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
92999a2dd95SBruce Richardson 		(req->para.hash_start_src_offset <
93099a2dd95SBruce Richardson 			VHOST_CRYPTO_MAX_DATA_SIZE) &&
93199a2dd95SBruce Richardson 		(req->para.len_to_hash <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
93299a2dd95SBruce Richardson 		(req->para.cipher_start_src_offset + req->para.len_to_cipher <=
93399a2dd95SBruce Richardson 			req->para.src_data_len) &&
93499a2dd95SBruce Richardson 		(req->para.hash_start_src_offset + req->para.len_to_hash <=
93599a2dd95SBruce Richardson 			req->para.src_data_len) &&
93699a2dd95SBruce Richardson 		(req->para.dst_data_len + req->para.hash_result_len <=
93799a2dd95SBruce Richardson 			VHOST_CRYPTO_MAX_DATA_SIZE)))
93899a2dd95SBruce Richardson 		return VIRTIO_CRYPTO_OK;
93999a2dd95SBruce Richardson 	return VIRTIO_CRYPTO_BADMSG;
94099a2dd95SBruce Richardson }
94199a2dd95SBruce Richardson 
94299a2dd95SBruce Richardson static __rte_always_inline uint8_t
prepare_sym_chain_op(struct vhost_crypto * vcrypto,struct rte_crypto_op * op,struct vhost_crypto_data_req * vc_req,struct virtio_crypto_alg_chain_data_req * chain,struct vhost_crypto_desc * head,uint32_t max_n_descs)94399a2dd95SBruce Richardson prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
94499a2dd95SBruce Richardson 		struct vhost_crypto_data_req *vc_req,
94599a2dd95SBruce Richardson 		struct virtio_crypto_alg_chain_data_req *chain,
94699a2dd95SBruce Richardson 		struct vhost_crypto_desc *head,
94799a2dd95SBruce Richardson 		uint32_t max_n_descs)
948bf42fb30SDavid Marchand 	__rte_shared_locks_required(&vc_req->vq->iotlb_lock)
94999a2dd95SBruce Richardson {
95099a2dd95SBruce Richardson 	struct vhost_crypto_desc *desc = head, *digest_desc;
95199a2dd95SBruce Richardson 	struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
95299a2dd95SBruce Richardson 	struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
95399a2dd95SBruce Richardson 	uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
95499a2dd95SBruce Richardson 	uint32_t digest_offset;
95599a2dd95SBruce Richardson 	void *digest_addr;
95699a2dd95SBruce Richardson 	uint8_t ret = vhost_crypto_check_chain_request(chain);
95799a2dd95SBruce Richardson 
95899a2dd95SBruce Richardson 	if (unlikely(ret != VIRTIO_CRYPTO_OK))
95999a2dd95SBruce Richardson 		goto error_exit;
96099a2dd95SBruce Richardson 
96199a2dd95SBruce Richardson 	/* prepare */
96299a2dd95SBruce Richardson 	/* iv */
96399a2dd95SBruce Richardson 	if (unlikely(copy_data(iv_data, vc_req, head, &desc,
96499a2dd95SBruce Richardson 			chain->para.iv_len, max_n_descs) < 0)) {
9654414bb67SDavid Marchand 		VC_LOG_ERR("Incorrect virtio descriptor");
96699a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
96799a2dd95SBruce Richardson 		goto error_exit;
96899a2dd95SBruce Richardson 	}
96999a2dd95SBruce Richardson 
97099a2dd95SBruce Richardson 	switch (vcrypto->option) {
97199a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
97299a2dd95SBruce Richardson 		m_src->data_len = chain->para.src_data_len;
97399a2dd95SBruce Richardson 		m_dst->data_len = chain->para.dst_data_len;
97499a2dd95SBruce Richardson 
975e811e2d7SShijith Thotton 		rte_mbuf_iova_set(m_src,
976e811e2d7SShijith Thotton 				  gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.src_data_len));
97799a2dd95SBruce Richardson 		m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
978e811e2d7SShijith Thotton 		if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) {
97999a2dd95SBruce Richardson 			VC_LOG_ERR("zero_copy may fail due to cross page data");
98099a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
98199a2dd95SBruce Richardson 			goto error_exit;
98299a2dd95SBruce Richardson 		}
98399a2dd95SBruce Richardson 
98499a2dd95SBruce Richardson 		if (unlikely(move_desc(head, &desc, chain->para.src_data_len,
98599a2dd95SBruce Richardson 				max_n_descs) < 0)) {
98699a2dd95SBruce Richardson 			VC_LOG_ERR("Incorrect descriptor");
98799a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
98899a2dd95SBruce Richardson 			goto error_exit;
98999a2dd95SBruce Richardson 		}
99099a2dd95SBruce Richardson 		break;
99199a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
99299a2dd95SBruce Richardson 		vc_req->wb_pool = vcrypto->wb_pool;
99399a2dd95SBruce Richardson 		m_src->data_len = chain->para.src_data_len;
99499a2dd95SBruce Richardson 		if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
99599a2dd95SBruce Richardson 				vc_req, head, &desc, chain->para.src_data_len,
99699a2dd95SBruce Richardson 				max_n_descs) < 0)) {
9974414bb67SDavid Marchand 			VC_LOG_ERR("Incorrect virtio descriptor");
99899a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_BADMSG;
99999a2dd95SBruce Richardson 			goto error_exit;
100099a2dd95SBruce Richardson 		}
100199a2dd95SBruce Richardson 
100299a2dd95SBruce Richardson 		break;
100399a2dd95SBruce Richardson 	default:
100499a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
100599a2dd95SBruce Richardson 		goto error_exit;
100699a2dd95SBruce Richardson 	}
100799a2dd95SBruce Richardson 
100899a2dd95SBruce Richardson 	/* dst */
100999a2dd95SBruce Richardson 	desc = find_write_desc(head, desc, max_n_descs);
101099a2dd95SBruce Richardson 	if (unlikely(!desc)) {
101199a2dd95SBruce Richardson 		VC_LOG_ERR("Cannot find write location");
101299a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
101399a2dd95SBruce Richardson 		goto error_exit;
101499a2dd95SBruce Richardson 	}
101599a2dd95SBruce Richardson 
101699a2dd95SBruce Richardson 	switch (vcrypto->option) {
101799a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1018e811e2d7SShijith Thotton 		rte_mbuf_iova_set(m_dst,
1019e811e2d7SShijith Thotton 				  gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.dst_data_len));
102099a2dd95SBruce Richardson 		m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
1021e811e2d7SShijith Thotton 		if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) {
102299a2dd95SBruce Richardson 			VC_LOG_ERR("zero_copy may fail due to cross page data");
102399a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
102499a2dd95SBruce Richardson 			goto error_exit;
102599a2dd95SBruce Richardson 		}
102699a2dd95SBruce Richardson 
102799a2dd95SBruce Richardson 		if (unlikely(move_desc(vc_req->head, &desc,
102899a2dd95SBruce Richardson 				chain->para.dst_data_len, max_n_descs) < 0)) {
102999a2dd95SBruce Richardson 			VC_LOG_ERR("Incorrect descriptor");
103099a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
103199a2dd95SBruce Richardson 			goto error_exit;
103299a2dd95SBruce Richardson 		}
103399a2dd95SBruce Richardson 
103499a2dd95SBruce Richardson 		op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
103599a2dd95SBruce Richardson 				desc->addr, chain->para.hash_result_len);
103699a2dd95SBruce Richardson 		op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
103799a2dd95SBruce Richardson 				VHOST_ACCESS_RW);
103899a2dd95SBruce Richardson 		if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
103999a2dd95SBruce Richardson 			VC_LOG_ERR("zero_copy may fail due to cross page data");
104099a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
104199a2dd95SBruce Richardson 			goto error_exit;
104299a2dd95SBruce Richardson 		}
104399a2dd95SBruce Richardson 
104499a2dd95SBruce Richardson 		if (unlikely(move_desc(head, &desc,
104599a2dd95SBruce Richardson 				chain->para.hash_result_len,
104699a2dd95SBruce Richardson 				max_n_descs) < 0)) {
104799a2dd95SBruce Richardson 			VC_LOG_ERR("Incorrect descriptor");
104899a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
104999a2dd95SBruce Richardson 			goto error_exit;
105099a2dd95SBruce Richardson 		}
105199a2dd95SBruce Richardson 
105299a2dd95SBruce Richardson 		break;
105399a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
105499a2dd95SBruce Richardson 		vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb,
105599a2dd95SBruce Richardson 				rte_pktmbuf_mtod(m_src, uint8_t *),
105699a2dd95SBruce Richardson 				chain->para.cipher_start_src_offset,
105799a2dd95SBruce Richardson 				chain->para.dst_data_len -
105899a2dd95SBruce Richardson 					chain->para.cipher_start_src_offset,
105999a2dd95SBruce Richardson 				max_n_descs);
106099a2dd95SBruce Richardson 		if (unlikely(vc_req->wb == NULL)) {
106199a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
106299a2dd95SBruce Richardson 			goto error_exit;
106399a2dd95SBruce Richardson 		}
106499a2dd95SBruce Richardson 
106599a2dd95SBruce Richardson 		digest_desc = desc;
106699a2dd95SBruce Richardson 		digest_offset = m_src->data_len;
106799a2dd95SBruce Richardson 		digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
106899a2dd95SBruce Richardson 				digest_offset);
106999a2dd95SBruce Richardson 
107099a2dd95SBruce Richardson 		/** create a wb_data for digest */
107199a2dd95SBruce Richardson 		ewb->next = prepare_write_back_data(vc_req, head, &desc,
107299a2dd95SBruce Richardson 				&ewb2, digest_addr, 0,
107399a2dd95SBruce Richardson 				chain->para.hash_result_len, max_n_descs);
107499a2dd95SBruce Richardson 		if (unlikely(ewb->next == NULL)) {
107599a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_ERR;
107699a2dd95SBruce Richardson 			goto error_exit;
107799a2dd95SBruce Richardson 		}
107899a2dd95SBruce Richardson 
107999a2dd95SBruce Richardson 		if (unlikely(copy_data(digest_addr, vc_req, head, &digest_desc,
108099a2dd95SBruce Richardson 				chain->para.hash_result_len,
108199a2dd95SBruce Richardson 				max_n_descs) < 0)) {
10824414bb67SDavid Marchand 			VC_LOG_ERR("Incorrect virtio descriptor");
108399a2dd95SBruce Richardson 			ret = VIRTIO_CRYPTO_BADMSG;
108499a2dd95SBruce Richardson 			goto error_exit;
108599a2dd95SBruce Richardson 		}
108699a2dd95SBruce Richardson 
108799a2dd95SBruce Richardson 		op->sym->auth.digest.data = digest_addr;
108899a2dd95SBruce Richardson 		op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
108999a2dd95SBruce Richardson 				digest_offset);
109099a2dd95SBruce Richardson 		break;
109199a2dd95SBruce Richardson 	default:
109299a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
109399a2dd95SBruce Richardson 		goto error_exit;
109499a2dd95SBruce Richardson 	}
109599a2dd95SBruce Richardson 
109699a2dd95SBruce Richardson 	/* record inhdr */
109799a2dd95SBruce Richardson 	vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
109899a2dd95SBruce Richardson 	if (unlikely(vc_req->inhdr == NULL)) {
109999a2dd95SBruce Richardson 		ret = VIRTIO_CRYPTO_BADMSG;
110099a2dd95SBruce Richardson 		goto error_exit;
110199a2dd95SBruce Richardson 	}
110299a2dd95SBruce Richardson 
110399a2dd95SBruce Richardson 	vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
110499a2dd95SBruce Richardson 
110599a2dd95SBruce Richardson 	op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
110699a2dd95SBruce Richardson 	op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
110799a2dd95SBruce Richardson 
110899a2dd95SBruce Richardson 	op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
110999a2dd95SBruce Richardson 	op->sym->cipher.data.length = chain->para.src_data_len -
111099a2dd95SBruce Richardson 			chain->para.cipher_start_src_offset;
111199a2dd95SBruce Richardson 
111299a2dd95SBruce Richardson 	op->sym->auth.data.offset = chain->para.hash_start_src_offset;
111399a2dd95SBruce Richardson 	op->sym->auth.data.length = chain->para.len_to_hash;
111499a2dd95SBruce Richardson 
111599a2dd95SBruce Richardson 	vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
111699a2dd95SBruce Richardson 			INHDR_LEN;
111799a2dd95SBruce Richardson 	return 0;
111899a2dd95SBruce Richardson 
111999a2dd95SBruce Richardson error_exit:
112099a2dd95SBruce Richardson 	if (vc_req->wb)
112199a2dd95SBruce Richardson 		free_wb_data(vc_req->wb, vc_req->wb_pool);
112299a2dd95SBruce Richardson 	vc_req->len = INHDR_LEN;
112399a2dd95SBruce Richardson 	return ret;
112499a2dd95SBruce Richardson }
112599a2dd95SBruce Richardson 
112699a2dd95SBruce Richardson /**
112799a2dd95SBruce Richardson  * Process on descriptor
112899a2dd95SBruce Richardson  */
112999a2dd95SBruce Richardson static __rte_always_inline int
vhost_crypto_process_one_req(struct vhost_crypto * vcrypto,struct vhost_virtqueue * vq,struct rte_crypto_op * op,struct vring_desc * head,struct vhost_crypto_desc * descs,uint16_t desc_idx)113099a2dd95SBruce Richardson vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
113199a2dd95SBruce Richardson 		struct vhost_virtqueue *vq, struct rte_crypto_op *op,
113299a2dd95SBruce Richardson 		struct vring_desc *head, struct vhost_crypto_desc *descs,
113399a2dd95SBruce Richardson 		uint16_t desc_idx)
1134bf42fb30SDavid Marchand 	__rte_no_thread_safety_analysis /* FIXME: requires iotlb_lock? */
113599a2dd95SBruce Richardson {
113699a2dd95SBruce Richardson 	struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
113799a2dd95SBruce Richardson 	struct rte_cryptodev_sym_session *session;
113899a2dd95SBruce Richardson 	struct virtio_crypto_op_data_req req;
113999a2dd95SBruce Richardson 	struct virtio_crypto_inhdr *inhdr;
114099a2dd95SBruce Richardson 	struct vhost_crypto_desc *desc = descs;
114199a2dd95SBruce Richardson 	struct vring_desc *src_desc;
114299a2dd95SBruce Richardson 	uint64_t session_id;
114399a2dd95SBruce Richardson 	uint64_t dlen;
114499a2dd95SBruce Richardson 	uint32_t nb_descs = 0, max_n_descs, i;
114599a2dd95SBruce Richardson 	int err;
114699a2dd95SBruce Richardson 
114799a2dd95SBruce Richardson 	vc_req->desc_idx = desc_idx;
114899a2dd95SBruce Richardson 	vc_req->dev = vcrypto->dev;
114999a2dd95SBruce Richardson 	vc_req->vq = vq;
115099a2dd95SBruce Richardson 
115199a2dd95SBruce Richardson 	if (unlikely((head->flags & VRING_DESC_F_INDIRECT) == 0)) {
115299a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid descriptor");
115399a2dd95SBruce Richardson 		return -1;
115499a2dd95SBruce Richardson 	}
115599a2dd95SBruce Richardson 
115699a2dd95SBruce Richardson 	dlen = head->len;
115799a2dd95SBruce Richardson 	src_desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
115899a2dd95SBruce Richardson 			&dlen, VHOST_ACCESS_RO);
115999a2dd95SBruce Richardson 	if (unlikely(!src_desc || dlen != head->len)) {
116099a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid descriptor");
116199a2dd95SBruce Richardson 		return -1;
116299a2dd95SBruce Richardson 	}
116399a2dd95SBruce Richardson 	head = src_desc;
116499a2dd95SBruce Richardson 
116599a2dd95SBruce Richardson 	nb_descs = max_n_descs = dlen / sizeof(struct vring_desc);
116699a2dd95SBruce Richardson 	if (unlikely(nb_descs > VHOST_CRYPTO_MAX_N_DESC || nb_descs == 0)) {
116799a2dd95SBruce Richardson 		err = VIRTIO_CRYPTO_ERR;
116899a2dd95SBruce Richardson 		VC_LOG_ERR("Cannot process num of descriptors %u", nb_descs);
116999a2dd95SBruce Richardson 		if (nb_descs > 0) {
117099a2dd95SBruce Richardson 			struct vring_desc *inhdr_desc = head;
117199a2dd95SBruce Richardson 			while (inhdr_desc->flags & VRING_DESC_F_NEXT) {
117299a2dd95SBruce Richardson 				if (inhdr_desc->next >= max_n_descs)
117399a2dd95SBruce Richardson 					return -1;
117499a2dd95SBruce Richardson 				inhdr_desc = &head[inhdr_desc->next];
117599a2dd95SBruce Richardson 			}
117699a2dd95SBruce Richardson 			if (inhdr_desc->len != sizeof(*inhdr))
117799a2dd95SBruce Richardson 				return -1;
117899a2dd95SBruce Richardson 			inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *,
117999a2dd95SBruce Richardson 					vc_req, inhdr_desc->addr, &dlen,
118099a2dd95SBruce Richardson 					VHOST_ACCESS_WO);
118199a2dd95SBruce Richardson 			if (unlikely(!inhdr || dlen != inhdr_desc->len))
118299a2dd95SBruce Richardson 				return -1;
118399a2dd95SBruce Richardson 			inhdr->status = VIRTIO_CRYPTO_ERR;
118499a2dd95SBruce Richardson 			return -1;
118599a2dd95SBruce Richardson 		}
118699a2dd95SBruce Richardson 	}
118799a2dd95SBruce Richardson 
118899a2dd95SBruce Richardson 	/* copy descriptors to local variable */
118999a2dd95SBruce Richardson 	for (i = 0; i < max_n_descs; i++) {
119099a2dd95SBruce Richardson 		desc->addr = src_desc->addr;
119199a2dd95SBruce Richardson 		desc->len = src_desc->len;
119299a2dd95SBruce Richardson 		desc->flags = src_desc->flags;
119399a2dd95SBruce Richardson 		desc++;
119499a2dd95SBruce Richardson 		if (unlikely((src_desc->flags & VRING_DESC_F_NEXT) == 0))
119599a2dd95SBruce Richardson 			break;
119699a2dd95SBruce Richardson 		if (unlikely(src_desc->next >= max_n_descs)) {
119799a2dd95SBruce Richardson 			err = VIRTIO_CRYPTO_BADMSG;
119899a2dd95SBruce Richardson 			VC_LOG_ERR("Invalid descriptor");
119999a2dd95SBruce Richardson 			goto error_exit;
120099a2dd95SBruce Richardson 		}
120199a2dd95SBruce Richardson 		src_desc = &head[src_desc->next];
120299a2dd95SBruce Richardson 	}
120399a2dd95SBruce Richardson 
120499a2dd95SBruce Richardson 	vc_req->head = head;
120599a2dd95SBruce Richardson 	vc_req->zero_copy = vcrypto->option;
120699a2dd95SBruce Richardson 
120799a2dd95SBruce Richardson 	nb_descs = desc - descs;
120899a2dd95SBruce Richardson 	desc = descs;
120999a2dd95SBruce Richardson 
121099a2dd95SBruce Richardson 	if (unlikely(desc->len < sizeof(req))) {
121199a2dd95SBruce Richardson 		err = VIRTIO_CRYPTO_BADMSG;
121299a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid descriptor");
121399a2dd95SBruce Richardson 		goto error_exit;
121499a2dd95SBruce Richardson 	}
121599a2dd95SBruce Richardson 
121699a2dd95SBruce Richardson 	if (unlikely(copy_data(&req, vc_req, descs, &desc, sizeof(req),
121799a2dd95SBruce Richardson 			max_n_descs) < 0)) {
121899a2dd95SBruce Richardson 		err = VIRTIO_CRYPTO_BADMSG;
121999a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid descriptor");
122099a2dd95SBruce Richardson 		goto error_exit;
122199a2dd95SBruce Richardson 	}
122299a2dd95SBruce Richardson 
122399a2dd95SBruce Richardson 	/* desc is advanced by 1 now */
122499a2dd95SBruce Richardson 	max_n_descs -= 1;
122599a2dd95SBruce Richardson 
122699a2dd95SBruce Richardson 	switch (req.header.opcode) {
122799a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
122899a2dd95SBruce Richardson 	case VIRTIO_CRYPTO_CIPHER_DECRYPT:
122999a2dd95SBruce Richardson 		session_id = req.header.session_id;
123099a2dd95SBruce Richardson 
123199a2dd95SBruce Richardson 		/* one branch to avoid unnecessary table lookup */
123299a2dd95SBruce Richardson 		if (vcrypto->cache_session_id != session_id) {
123399a2dd95SBruce Richardson 			err = rte_hash_lookup_data(vcrypto->session_map,
123499a2dd95SBruce Richardson 					&session_id, (void **)&session);
123599a2dd95SBruce Richardson 			if (unlikely(err < 0)) {
123699a2dd95SBruce Richardson 				err = VIRTIO_CRYPTO_ERR;
123799a2dd95SBruce Richardson 				VC_LOG_ERR("Failed to find session %"PRIu64,
123899a2dd95SBruce Richardson 						session_id);
123999a2dd95SBruce Richardson 				goto error_exit;
124099a2dd95SBruce Richardson 			}
124199a2dd95SBruce Richardson 
124299a2dd95SBruce Richardson 			vcrypto->cache_session = session;
124399a2dd95SBruce Richardson 			vcrypto->cache_session_id = session_id;
124499a2dd95SBruce Richardson 		}
124599a2dd95SBruce Richardson 
124699a2dd95SBruce Richardson 		session = vcrypto->cache_session;
124799a2dd95SBruce Richardson 
124899a2dd95SBruce Richardson 		err = rte_crypto_op_attach_sym_session(op, session);
124999a2dd95SBruce Richardson 		if (unlikely(err < 0)) {
125099a2dd95SBruce Richardson 			err = VIRTIO_CRYPTO_ERR;
125199a2dd95SBruce Richardson 			VC_LOG_ERR("Failed to attach session to op");
125299a2dd95SBruce Richardson 			goto error_exit;
125399a2dd95SBruce Richardson 		}
125499a2dd95SBruce Richardson 
125599a2dd95SBruce Richardson 		switch (req.u.sym_req.op_type) {
125699a2dd95SBruce Richardson 		case VIRTIO_CRYPTO_SYM_OP_NONE:
125799a2dd95SBruce Richardson 			err = VIRTIO_CRYPTO_NOTSUPP;
125899a2dd95SBruce Richardson 			break;
125999a2dd95SBruce Richardson 		case VIRTIO_CRYPTO_SYM_OP_CIPHER:
126099a2dd95SBruce Richardson 			err = prepare_sym_cipher_op(vcrypto, op, vc_req,
126199a2dd95SBruce Richardson 					&req.u.sym_req.u.cipher, desc,
126299a2dd95SBruce Richardson 					max_n_descs);
126399a2dd95SBruce Richardson 			break;
126499a2dd95SBruce Richardson 		case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
126599a2dd95SBruce Richardson 			err = prepare_sym_chain_op(vcrypto, op, vc_req,
126699a2dd95SBruce Richardson 					&req.u.sym_req.u.chain, desc,
126799a2dd95SBruce Richardson 					max_n_descs);
126899a2dd95SBruce Richardson 			break;
126999a2dd95SBruce Richardson 		}
127099a2dd95SBruce Richardson 		if (unlikely(err != 0)) {
127199a2dd95SBruce Richardson 			VC_LOG_ERR("Failed to process sym request");
127299a2dd95SBruce Richardson 			goto error_exit;
127399a2dd95SBruce Richardson 		}
127499a2dd95SBruce Richardson 		break;
127599a2dd95SBruce Richardson 	default:
127699a2dd95SBruce Richardson 		err = VIRTIO_CRYPTO_ERR;
127799a2dd95SBruce Richardson 		VC_LOG_ERR("Unsupported symmetric crypto request type %u",
127899a2dd95SBruce Richardson 				req.header.opcode);
127999a2dd95SBruce Richardson 		goto error_exit;
128099a2dd95SBruce Richardson 	}
128199a2dd95SBruce Richardson 
128299a2dd95SBruce Richardson 	return 0;
128399a2dd95SBruce Richardson 
128499a2dd95SBruce Richardson error_exit:
128599a2dd95SBruce Richardson 
128699a2dd95SBruce Richardson 	inhdr = reach_inhdr(vc_req, descs, max_n_descs);
128799a2dd95SBruce Richardson 	if (likely(inhdr != NULL))
128899a2dd95SBruce Richardson 		inhdr->status = (uint8_t)err;
128999a2dd95SBruce Richardson 
129099a2dd95SBruce Richardson 	return -1;
129199a2dd95SBruce Richardson }
129299a2dd95SBruce Richardson 
129399a2dd95SBruce Richardson static __rte_always_inline struct vhost_virtqueue *
vhost_crypto_finalize_one_request(struct rte_crypto_op * op,struct vhost_virtqueue * old_vq)129499a2dd95SBruce Richardson vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
129599a2dd95SBruce Richardson 		struct vhost_virtqueue *old_vq)
129699a2dd95SBruce Richardson {
129799a2dd95SBruce Richardson 	struct rte_mbuf *m_src = op->sym->m_src;
129899a2dd95SBruce Richardson 	struct rte_mbuf *m_dst = op->sym->m_dst;
129999a2dd95SBruce Richardson 	struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
13009cfbe676SThierry Herbelot 	struct vhost_virtqueue *vq;
13019cfbe676SThierry Herbelot 	uint16_t used_idx, desc_idx;
130299a2dd95SBruce Richardson 
130399a2dd95SBruce Richardson 	if (unlikely(!vc_req)) {
130499a2dd95SBruce Richardson 		VC_LOG_ERR("Failed to retrieve vc_req");
130599a2dd95SBruce Richardson 		return NULL;
130699a2dd95SBruce Richardson 	}
13079cfbe676SThierry Herbelot 	vq = vc_req->vq;
13089cfbe676SThierry Herbelot 	used_idx = vc_req->desc_idx;
130999a2dd95SBruce Richardson 
131099a2dd95SBruce Richardson 	if (old_vq && (vq != old_vq))
131199a2dd95SBruce Richardson 		return vq;
131299a2dd95SBruce Richardson 
131399a2dd95SBruce Richardson 	if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
131499a2dd95SBruce Richardson 		vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
131599a2dd95SBruce Richardson 	else {
131699a2dd95SBruce Richardson 		if (vc_req->zero_copy == 0)
131799a2dd95SBruce Richardson 			write_back_data(vc_req);
131899a2dd95SBruce Richardson 	}
131999a2dd95SBruce Richardson 
132099a2dd95SBruce Richardson 	desc_idx = vq->avail->ring[used_idx];
132199a2dd95SBruce Richardson 	vq->used->ring[desc_idx].id = vq->avail->ring[desc_idx];
132299a2dd95SBruce Richardson 	vq->used->ring[desc_idx].len = vc_req->len;
132399a2dd95SBruce Richardson 
132499a2dd95SBruce Richardson 	rte_mempool_put(m_src->pool, (void *)m_src);
132599a2dd95SBruce Richardson 
132699a2dd95SBruce Richardson 	if (m_dst)
132799a2dd95SBruce Richardson 		rte_mempool_put(m_dst->pool, (void *)m_dst);
132899a2dd95SBruce Richardson 
132999a2dd95SBruce Richardson 	return vc_req->vq;
133099a2dd95SBruce Richardson }
133199a2dd95SBruce Richardson 
133299a2dd95SBruce Richardson static __rte_always_inline uint16_t
vhost_crypto_complete_one_vm_requests(struct rte_crypto_op ** ops,uint16_t nb_ops,int * callfd)133399a2dd95SBruce Richardson vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
133499a2dd95SBruce Richardson 		uint16_t nb_ops, int *callfd)
133599a2dd95SBruce Richardson {
133699a2dd95SBruce Richardson 	uint16_t processed = 1;
133799a2dd95SBruce Richardson 	struct vhost_virtqueue *vq, *tmp_vq;
133899a2dd95SBruce Richardson 
133999a2dd95SBruce Richardson 	if (unlikely(nb_ops == 0))
134099a2dd95SBruce Richardson 		return 0;
134199a2dd95SBruce Richardson 
134299a2dd95SBruce Richardson 	vq = vhost_crypto_finalize_one_request(ops[0], NULL);
134399a2dd95SBruce Richardson 	if (unlikely(vq == NULL))
134499a2dd95SBruce Richardson 		return 0;
134599a2dd95SBruce Richardson 	tmp_vq = vq;
134699a2dd95SBruce Richardson 
134799a2dd95SBruce Richardson 	while ((processed < nb_ops)) {
134899a2dd95SBruce Richardson 		tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
134999a2dd95SBruce Richardson 				tmp_vq);
135099a2dd95SBruce Richardson 
135199a2dd95SBruce Richardson 		if (unlikely(vq != tmp_vq))
135299a2dd95SBruce Richardson 			break;
135399a2dd95SBruce Richardson 
135499a2dd95SBruce Richardson 		processed++;
135599a2dd95SBruce Richardson 	}
135699a2dd95SBruce Richardson 
135799a2dd95SBruce Richardson 	*callfd = vq->callfd;
135899a2dd95SBruce Richardson 
135999a2dd95SBruce Richardson 	*(volatile uint16_t *)&vq->used->idx += processed;
136099a2dd95SBruce Richardson 
136199a2dd95SBruce Richardson 	return processed;
136299a2dd95SBruce Richardson }
136399a2dd95SBruce Richardson 
136499a2dd95SBruce Richardson int
rte_vhost_crypto_driver_start(const char * path)136599a2dd95SBruce Richardson rte_vhost_crypto_driver_start(const char *path)
136699a2dd95SBruce Richardson {
136799a2dd95SBruce Richardson 	uint64_t protocol_features;
136899a2dd95SBruce Richardson 	int ret;
136999a2dd95SBruce Richardson 
137099a2dd95SBruce Richardson 	ret = rte_vhost_driver_set_features(path, VIRTIO_CRYPTO_FEATURES);
137199a2dd95SBruce Richardson 	if (ret)
137299a2dd95SBruce Richardson 		return -1;
137399a2dd95SBruce Richardson 
137499a2dd95SBruce Richardson 	ret = rte_vhost_driver_get_protocol_features(path, &protocol_features);
137599a2dd95SBruce Richardson 	if (ret)
137699a2dd95SBruce Richardson 		return -1;
137799a2dd95SBruce Richardson 	protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_CONFIG);
137899a2dd95SBruce Richardson 	ret = rte_vhost_driver_set_protocol_features(path, protocol_features);
137999a2dd95SBruce Richardson 	if (ret)
138099a2dd95SBruce Richardson 		return -1;
138199a2dd95SBruce Richardson 
138299a2dd95SBruce Richardson 	return rte_vhost_driver_start(path);
138399a2dd95SBruce Richardson }
138499a2dd95SBruce Richardson 
138599a2dd95SBruce Richardson int
rte_vhost_crypto_create(int vid,uint8_t cryptodev_id,struct rte_mempool * sess_pool,int socket_id)138699a2dd95SBruce Richardson rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
138799a2dd95SBruce Richardson 		struct rte_mempool *sess_pool,
138899a2dd95SBruce Richardson 		int socket_id)
138999a2dd95SBruce Richardson {
139099a2dd95SBruce Richardson 	struct virtio_net *dev = get_device(vid);
139199a2dd95SBruce Richardson 	struct rte_hash_parameters params = {0};
139299a2dd95SBruce Richardson 	struct vhost_crypto *vcrypto;
139399a2dd95SBruce Richardson 	char name[128];
139499a2dd95SBruce Richardson 	int ret;
139599a2dd95SBruce Richardson 
139699a2dd95SBruce Richardson 	if (!dev) {
139799a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid vid %i", vid);
139899a2dd95SBruce Richardson 		return -EINVAL;
139999a2dd95SBruce Richardson 	}
140099a2dd95SBruce Richardson 
140199a2dd95SBruce Richardson 	vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
140299a2dd95SBruce Richardson 			RTE_CACHE_LINE_SIZE, socket_id);
140399a2dd95SBruce Richardson 	if (!vcrypto) {
140499a2dd95SBruce Richardson 		VC_LOG_ERR("Insufficient memory");
140599a2dd95SBruce Richardson 		return -ENOMEM;
140699a2dd95SBruce Richardson 	}
140799a2dd95SBruce Richardson 
140899a2dd95SBruce Richardson 	vcrypto->sess_pool = sess_pool;
140999a2dd95SBruce Richardson 	vcrypto->cid = cryptodev_id;
141099a2dd95SBruce Richardson 	vcrypto->cache_session_id = UINT64_MAX;
141199a2dd95SBruce Richardson 	vcrypto->last_session_id = 1;
141299a2dd95SBruce Richardson 	vcrypto->dev = dev;
141399a2dd95SBruce Richardson 	vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
141499a2dd95SBruce Richardson 
141599a2dd95SBruce Richardson 	snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
141699a2dd95SBruce Richardson 	params.name = name;
141799a2dd95SBruce Richardson 	params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
141899a2dd95SBruce Richardson 	params.hash_func = rte_jhash;
141999a2dd95SBruce Richardson 	params.key_len = sizeof(uint64_t);
142099a2dd95SBruce Richardson 	params.socket_id = socket_id;
142199a2dd95SBruce Richardson 	vcrypto->session_map = rte_hash_create(&params);
142299a2dd95SBruce Richardson 	if (!vcrypto->session_map) {
142399a2dd95SBruce Richardson 		VC_LOG_ERR("Failed to creath session map");
142499a2dd95SBruce Richardson 		ret = -ENOMEM;
142599a2dd95SBruce Richardson 		goto error_exit;
142699a2dd95SBruce Richardson 	}
142799a2dd95SBruce Richardson 
142899a2dd95SBruce Richardson 	snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
142999a2dd95SBruce Richardson 	vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
143099a2dd95SBruce Richardson 			VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
143199a2dd95SBruce Richardson 			sizeof(struct vhost_crypto_data_req),
143299a2dd95SBruce Richardson 			VHOST_CRYPTO_MAX_DATA_SIZE + RTE_PKTMBUF_HEADROOM,
143399a2dd95SBruce Richardson 			rte_socket_id());
143499a2dd95SBruce Richardson 	if (!vcrypto->mbuf_pool) {
143599a2dd95SBruce Richardson 		VC_LOG_ERR("Failed to creath mbuf pool");
143699a2dd95SBruce Richardson 		ret = -ENOMEM;
143799a2dd95SBruce Richardson 		goto error_exit;
143899a2dd95SBruce Richardson 	}
143999a2dd95SBruce Richardson 
144099a2dd95SBruce Richardson 	snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
144199a2dd95SBruce Richardson 	vcrypto->wb_pool = rte_mempool_create(name,
144299a2dd95SBruce Richardson 			VHOST_CRYPTO_MBUF_POOL_SIZE,
144399a2dd95SBruce Richardson 			sizeof(struct vhost_crypto_writeback_data),
144499a2dd95SBruce Richardson 			128, 0, NULL, NULL, NULL, NULL,
144599a2dd95SBruce Richardson 			rte_socket_id(), 0);
144699a2dd95SBruce Richardson 	if (!vcrypto->wb_pool) {
144799a2dd95SBruce Richardson 		VC_LOG_ERR("Failed to creath mempool");
144899a2dd95SBruce Richardson 		ret = -ENOMEM;
144999a2dd95SBruce Richardson 		goto error_exit;
145099a2dd95SBruce Richardson 	}
145199a2dd95SBruce Richardson 
145299a2dd95SBruce Richardson 	dev->extern_data = vcrypto;
145399a2dd95SBruce Richardson 	dev->extern_ops.pre_msg_handle = NULL;
145499a2dd95SBruce Richardson 	dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
145599a2dd95SBruce Richardson 
145699a2dd95SBruce Richardson 	return 0;
145799a2dd95SBruce Richardson 
145899a2dd95SBruce Richardson error_exit:
145999a2dd95SBruce Richardson 	rte_hash_free(vcrypto->session_map);
146099a2dd95SBruce Richardson 	rte_mempool_free(vcrypto->mbuf_pool);
146199a2dd95SBruce Richardson 
146299a2dd95SBruce Richardson 	rte_free(vcrypto);
146399a2dd95SBruce Richardson 
146499a2dd95SBruce Richardson 	return ret;
146599a2dd95SBruce Richardson }
146699a2dd95SBruce Richardson 
146799a2dd95SBruce Richardson int
rte_vhost_crypto_free(int vid)146899a2dd95SBruce Richardson rte_vhost_crypto_free(int vid)
146999a2dd95SBruce Richardson {
147099a2dd95SBruce Richardson 	struct virtio_net *dev = get_device(vid);
147199a2dd95SBruce Richardson 	struct vhost_crypto *vcrypto;
147299a2dd95SBruce Richardson 
147399a2dd95SBruce Richardson 	if (unlikely(dev == NULL)) {
147499a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid vid %i", vid);
147599a2dd95SBruce Richardson 		return -EINVAL;
147699a2dd95SBruce Richardson 	}
147799a2dd95SBruce Richardson 
147899a2dd95SBruce Richardson 	vcrypto = dev->extern_data;
147999a2dd95SBruce Richardson 	if (unlikely(vcrypto == NULL)) {
148099a2dd95SBruce Richardson 		VC_LOG_ERR("Cannot find required data, is it initialized?");
148199a2dd95SBruce Richardson 		return -ENOENT;
148299a2dd95SBruce Richardson 	}
148399a2dd95SBruce Richardson 
148499a2dd95SBruce Richardson 	rte_hash_free(vcrypto->session_map);
148599a2dd95SBruce Richardson 	rte_mempool_free(vcrypto->mbuf_pool);
148699a2dd95SBruce Richardson 	rte_mempool_free(vcrypto->wb_pool);
148799a2dd95SBruce Richardson 	rte_free(vcrypto);
148899a2dd95SBruce Richardson 
148999a2dd95SBruce Richardson 	dev->extern_data = NULL;
149099a2dd95SBruce Richardson 	dev->extern_ops.pre_msg_handle = NULL;
149199a2dd95SBruce Richardson 	dev->extern_ops.post_msg_handle = NULL;
149299a2dd95SBruce Richardson 
149399a2dd95SBruce Richardson 	return 0;
149499a2dd95SBruce Richardson }
149599a2dd95SBruce Richardson 
149699a2dd95SBruce Richardson int
rte_vhost_crypto_set_zero_copy(int vid,enum rte_vhost_crypto_zero_copy option)149799a2dd95SBruce Richardson rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
149899a2dd95SBruce Richardson {
149999a2dd95SBruce Richardson 	struct virtio_net *dev = get_device(vid);
150099a2dd95SBruce Richardson 	struct vhost_crypto *vcrypto;
150199a2dd95SBruce Richardson 
150299a2dd95SBruce Richardson 	if (unlikely(dev == NULL)) {
150399a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid vid %i", vid);
150499a2dd95SBruce Richardson 		return -EINVAL;
150599a2dd95SBruce Richardson 	}
150699a2dd95SBruce Richardson 
150799a2dd95SBruce Richardson 	if (unlikely((uint32_t)option >=
150899a2dd95SBruce Richardson 				RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
150999a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid option %i", option);
151099a2dd95SBruce Richardson 		return -EINVAL;
151199a2dd95SBruce Richardson 	}
151299a2dd95SBruce Richardson 
151399a2dd95SBruce Richardson 	vcrypto = (struct vhost_crypto *)dev->extern_data;
151499a2dd95SBruce Richardson 	if (unlikely(vcrypto == NULL)) {
151599a2dd95SBruce Richardson 		VC_LOG_ERR("Cannot find required data, is it initialized?");
151699a2dd95SBruce Richardson 		return -ENOENT;
151799a2dd95SBruce Richardson 	}
151899a2dd95SBruce Richardson 
151999a2dd95SBruce Richardson 	if (vcrypto->option == (uint8_t)option)
152099a2dd95SBruce Richardson 		return 0;
152199a2dd95SBruce Richardson 
152299a2dd95SBruce Richardson 	if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
152399a2dd95SBruce Richardson 			!(rte_mempool_full(vcrypto->wb_pool))) {
152499a2dd95SBruce Richardson 		VC_LOG_ERR("Cannot update zero copy as mempool is not full");
152599a2dd95SBruce Richardson 		return -EINVAL;
152699a2dd95SBruce Richardson 	}
152799a2dd95SBruce Richardson 
152899a2dd95SBruce Richardson 	if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
152999a2dd95SBruce Richardson 		char name[128];
153099a2dd95SBruce Richardson 
153199a2dd95SBruce Richardson 		snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
153299a2dd95SBruce Richardson 		vcrypto->wb_pool = rte_mempool_create(name,
153399a2dd95SBruce Richardson 				VHOST_CRYPTO_MBUF_POOL_SIZE,
153499a2dd95SBruce Richardson 				sizeof(struct vhost_crypto_writeback_data),
153599a2dd95SBruce Richardson 				128, 0, NULL, NULL, NULL, NULL,
153699a2dd95SBruce Richardson 				rte_socket_id(), 0);
153799a2dd95SBruce Richardson 		if (!vcrypto->wb_pool) {
153899a2dd95SBruce Richardson 			VC_LOG_ERR("Failed to creath mbuf pool");
153999a2dd95SBruce Richardson 			return -ENOMEM;
154099a2dd95SBruce Richardson 		}
154199a2dd95SBruce Richardson 	} else {
154299a2dd95SBruce Richardson 		rte_mempool_free(vcrypto->wb_pool);
154399a2dd95SBruce Richardson 		vcrypto->wb_pool = NULL;
154499a2dd95SBruce Richardson 	}
154599a2dd95SBruce Richardson 
154699a2dd95SBruce Richardson 	vcrypto->option = (uint8_t)option;
154799a2dd95SBruce Richardson 
154899a2dd95SBruce Richardson 	return 0;
154999a2dd95SBruce Richardson }
155099a2dd95SBruce Richardson 
155199a2dd95SBruce Richardson uint16_t
rte_vhost_crypto_fetch_requests(int vid,uint32_t qid,struct rte_crypto_op ** ops,uint16_t nb_ops)155299a2dd95SBruce Richardson rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
155399a2dd95SBruce Richardson 		struct rte_crypto_op **ops, uint16_t nb_ops)
155499a2dd95SBruce Richardson {
155599a2dd95SBruce Richardson 	struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
155699a2dd95SBruce Richardson 	struct vhost_crypto_desc descs[VHOST_CRYPTO_MAX_N_DESC];
155799a2dd95SBruce Richardson 	struct virtio_net *dev = get_device(vid);
155899a2dd95SBruce Richardson 	struct vhost_crypto *vcrypto;
155999a2dd95SBruce Richardson 	struct vhost_virtqueue *vq;
156099a2dd95SBruce Richardson 	uint16_t avail_idx;
156199a2dd95SBruce Richardson 	uint16_t start_idx;
156299a2dd95SBruce Richardson 	uint16_t count;
156399a2dd95SBruce Richardson 	uint16_t i = 0;
156499a2dd95SBruce Richardson 
156599a2dd95SBruce Richardson 	if (unlikely(dev == NULL)) {
156699a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid vid %i", vid);
156799a2dd95SBruce Richardson 		return 0;
156899a2dd95SBruce Richardson 	}
156999a2dd95SBruce Richardson 
157099a2dd95SBruce Richardson 	if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
157199a2dd95SBruce Richardson 		VC_LOG_ERR("Invalid qid %u", qid);
157299a2dd95SBruce Richardson 		return 0;
157399a2dd95SBruce Richardson 	}
157499a2dd95SBruce Richardson 
157599a2dd95SBruce Richardson 	vcrypto = (struct vhost_crypto *)dev->extern_data;
157699a2dd95SBruce Richardson 	if (unlikely(vcrypto == NULL)) {
157799a2dd95SBruce Richardson 		VC_LOG_ERR("Cannot find required data, is it initialized?");
157899a2dd95SBruce Richardson 		return 0;
157999a2dd95SBruce Richardson 	}
158099a2dd95SBruce Richardson 
158199a2dd95SBruce Richardson 	vq = dev->virtqueue[qid];
158299a2dd95SBruce Richardson 
158399a2dd95SBruce Richardson 	avail_idx = *((volatile uint16_t *)&vq->avail->idx);
158499a2dd95SBruce Richardson 	start_idx = vq->last_used_idx;
158599a2dd95SBruce Richardson 	count = avail_idx - start_idx;
158699a2dd95SBruce Richardson 	count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
158799a2dd95SBruce Richardson 	count = RTE_MIN(count, nb_ops);
158899a2dd95SBruce Richardson 
158999a2dd95SBruce Richardson 	if (unlikely(count == 0))
159099a2dd95SBruce Richardson 		return 0;
159199a2dd95SBruce Richardson 
159299a2dd95SBruce Richardson 	/* for zero copy, we need 2 empty mbufs for src and dst, otherwise
159399a2dd95SBruce Richardson 	 * we need only 1 mbuf as src and dst
159499a2dd95SBruce Richardson 	 */
159599a2dd95SBruce Richardson 	switch (vcrypto->option) {
159699a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
159799a2dd95SBruce Richardson 		if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
159899a2dd95SBruce Richardson 				(void **)mbufs, count * 2) < 0)) {
159999a2dd95SBruce Richardson 			VC_LOG_ERR("Insufficient memory");
160099a2dd95SBruce Richardson 			return 0;
160199a2dd95SBruce Richardson 		}
160299a2dd95SBruce Richardson 
160399a2dd95SBruce Richardson 		for (i = 0; i < count; i++) {
160499a2dd95SBruce Richardson 			uint16_t used_idx = (start_idx + i) & (vq->size - 1);
160599a2dd95SBruce Richardson 			uint16_t desc_idx = vq->avail->ring[used_idx];
160699a2dd95SBruce Richardson 			struct vring_desc *head = &vq->desc[desc_idx];
160799a2dd95SBruce Richardson 			struct rte_crypto_op *op = ops[i];
160899a2dd95SBruce Richardson 
160999a2dd95SBruce Richardson 			op->sym->m_src = mbufs[i * 2];
161099a2dd95SBruce Richardson 			op->sym->m_dst = mbufs[i * 2 + 1];
161199a2dd95SBruce Richardson 			op->sym->m_src->data_off = 0;
161299a2dd95SBruce Richardson 			op->sym->m_dst->data_off = 0;
161399a2dd95SBruce Richardson 
161499a2dd95SBruce Richardson 			if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
161599a2dd95SBruce Richardson 					op, head, descs, used_idx) < 0))
161699a2dd95SBruce Richardson 				break;
161799a2dd95SBruce Richardson 		}
161899a2dd95SBruce Richardson 
161999a2dd95SBruce Richardson 		if (unlikely(i < count))
162099a2dd95SBruce Richardson 			rte_mempool_put_bulk(vcrypto->mbuf_pool,
162199a2dd95SBruce Richardson 					(void **)&mbufs[i * 2],
162299a2dd95SBruce Richardson 					(count - i) * 2);
162399a2dd95SBruce Richardson 
162499a2dd95SBruce Richardson 		break;
162599a2dd95SBruce Richardson 
162699a2dd95SBruce Richardson 	case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
162799a2dd95SBruce Richardson 		if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
162899a2dd95SBruce Richardson 				(void **)mbufs, count) < 0)) {
162999a2dd95SBruce Richardson 			VC_LOG_ERR("Insufficient memory");
163099a2dd95SBruce Richardson 			return 0;
163199a2dd95SBruce Richardson 		}
163299a2dd95SBruce Richardson 
163399a2dd95SBruce Richardson 		for (i = 0; i < count; i++) {
163499a2dd95SBruce Richardson 			uint16_t used_idx = (start_idx + i) & (vq->size - 1);
163599a2dd95SBruce Richardson 			uint16_t desc_idx = vq->avail->ring[used_idx];
163699a2dd95SBruce Richardson 			struct vring_desc *head = &vq->desc[desc_idx];
163799a2dd95SBruce Richardson 			struct rte_crypto_op *op = ops[i];
163899a2dd95SBruce Richardson 
163999a2dd95SBruce Richardson 			op->sym->m_src = mbufs[i];
164099a2dd95SBruce Richardson 			op->sym->m_dst = NULL;
164199a2dd95SBruce Richardson 			op->sym->m_src->data_off = 0;
164299a2dd95SBruce Richardson 
164399a2dd95SBruce Richardson 			if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
164499a2dd95SBruce Richardson 					op, head, descs, desc_idx) < 0))
164599a2dd95SBruce Richardson 				break;
164699a2dd95SBruce Richardson 		}
164799a2dd95SBruce Richardson 
164899a2dd95SBruce Richardson 		if (unlikely(i < count))
164999a2dd95SBruce Richardson 			rte_mempool_put_bulk(vcrypto->mbuf_pool,
165099a2dd95SBruce Richardson 					(void **)&mbufs[i],
165199a2dd95SBruce Richardson 					count - i);
165299a2dd95SBruce Richardson 
165399a2dd95SBruce Richardson 		break;
165499a2dd95SBruce Richardson 
165599a2dd95SBruce Richardson 	}
165699a2dd95SBruce Richardson 
165799a2dd95SBruce Richardson 	vq->last_used_idx += i;
165899a2dd95SBruce Richardson 
165999a2dd95SBruce Richardson 	return i;
166099a2dd95SBruce Richardson }
166199a2dd95SBruce Richardson 
166299a2dd95SBruce Richardson uint16_t
rte_vhost_crypto_finalize_requests(struct rte_crypto_op ** ops,uint16_t nb_ops,int * callfds,uint16_t * nb_callfds)166399a2dd95SBruce Richardson rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
166499a2dd95SBruce Richardson 		uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
166599a2dd95SBruce Richardson {
166699a2dd95SBruce Richardson 	struct rte_crypto_op **tmp_ops = ops;
166799a2dd95SBruce Richardson 	uint16_t count = 0, left = nb_ops;
166899a2dd95SBruce Richardson 	int callfd;
166999a2dd95SBruce Richardson 	uint16_t idx = 0;
167099a2dd95SBruce Richardson 
167199a2dd95SBruce Richardson 	while (left) {
167299a2dd95SBruce Richardson 		count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
167399a2dd95SBruce Richardson 				&callfd);
167499a2dd95SBruce Richardson 		if (unlikely(count == 0))
167599a2dd95SBruce Richardson 			break;
167699a2dd95SBruce Richardson 
167799a2dd95SBruce Richardson 		tmp_ops = &tmp_ops[count];
167899a2dd95SBruce Richardson 		left -= count;
167999a2dd95SBruce Richardson 
168099a2dd95SBruce Richardson 		callfds[idx++] = callfd;
168199a2dd95SBruce Richardson 
168299a2dd95SBruce Richardson 		if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
168399a2dd95SBruce Richardson 			VC_LOG_ERR("Too many vqs");
168499a2dd95SBruce Richardson 			break;
168599a2dd95SBruce Richardson 		}
168699a2dd95SBruce Richardson 	}
168799a2dd95SBruce Richardson 
168899a2dd95SBruce Richardson 	*nb_callfds = idx;
168999a2dd95SBruce Richardson 
169099a2dd95SBruce Richardson 	return nb_ops - left;
169199a2dd95SBruce Richardson }
1692