Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2017-2018 Intel Corporation
3 : : */
4 : : #include <eal_export.h>
5 : : #include <rte_malloc.h>
6 : : #include <rte_hash.h>
7 : : #include <rte_jhash.h>
8 : : #include <rte_log.h>
9 : : #include <rte_mbuf.h>
10 : : #include <rte_cryptodev.h>
11 : :
12 : : #include "iotlb.h"
13 : : #include "rte_vhost_crypto.h"
14 : : #include "vhost.h"
15 : : #include "vhost_user.h"
16 : : #include "virtio_crypto.h"
17 : :
18 : : #define INHDR_LEN (sizeof(struct virtio_crypto_inhdr))
19 : : #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
20 : : sizeof(struct rte_crypto_sym_op))
21 : :
22 [ - + ]: 305 : RTE_LOG_REGISTER_SUFFIX(vhost_crypto_logtype, crypto, INFO);
23 : : #define RTE_LOGTYPE_VHOST_CRYPTO vhost_crypto_logtype
24 : :
25 : : #define VC_LOG_ERR(...) \
26 : : RTE_LOG_LINE_PREFIX(ERR, VHOST_CRYPTO, "%s() line %u: ", \
27 : : __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
28 : :
29 : : #define VC_LOG_INFO(...) \
30 : : RTE_LOG_LINE_PREFIX(INFO, VHOST_CRYPTO, "%s() line %u: ", \
31 : : __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
32 : :
33 : : #ifdef RTE_LIBRTE_VHOST_DEBUG
34 : : #define VC_LOG_DBG(...) \
35 : : RTE_LOG_LINE_PREFIX(DEBUG, VHOST_CRYPTO, "%s() line %u: ", \
36 : : __func__ RTE_LOG_COMMA __LINE__, __VA_ARGS__)
37 : : #else
38 : : #define VC_LOG_DBG(...)
39 : : #endif
40 : :
41 : : #define VIRTIO_CRYPTO_FEATURES ((1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \
42 : : (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
43 : : (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
44 : : (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
45 : : (1ULL << VIRTIO_F_VERSION_1) | \
46 : : (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))
47 : :
48 : : #define IOVA_TO_VVA(t, dev, vq, a, l, p) \
49 : : ((t)(uintptr_t)vhost_iova_to_vva(dev, vq, a, l, p))
50 : :
51 : : /*
52 : : * vhost_crypto_desc is used to copy original vring_desc to the local buffer
53 : : * before processing (except the next index). The copy result will be an
54 : : * array of vhost_crypto_desc elements that follows the sequence of original
55 : : * vring_desc.next is arranged.
56 : : */
57 : : #define vhost_crypto_desc vring_desc
58 : :
59 : : struct vhost_crypto_session {
60 : : union {
61 : : struct rte_cryptodev_asym_session *asym;
62 : : struct rte_cryptodev_sym_session *sym;
63 : : };
64 : : enum rte_crypto_op_type type;
65 : : };
66 : :
67 : : static int
68 : 0 : cipher_algo_transform(uint32_t virtio_cipher_algo,
69 : : enum rte_crypto_cipher_algorithm *algo)
70 : : {
71 [ # # # # : 0 : switch (virtio_cipher_algo) {
# # # # #
# # # # ]
72 : 0 : case VIRTIO_CRYPTO_CIPHER_AES_CBC:
73 : 0 : *algo = RTE_CRYPTO_CIPHER_AES_CBC;
74 : 0 : break;
75 : 0 : case VIRTIO_CRYPTO_CIPHER_AES_CTR:
76 : 0 : *algo = RTE_CRYPTO_CIPHER_AES_CTR;
77 : 0 : break;
78 : 0 : case VIRTIO_CRYPTO_CIPHER_DES_ECB:
79 : 0 : *algo = -VIRTIO_CRYPTO_NOTSUPP;
80 : 0 : break;
81 : 0 : case VIRTIO_CRYPTO_CIPHER_DES_CBC:
82 : 0 : *algo = RTE_CRYPTO_CIPHER_DES_CBC;
83 : 0 : break;
84 : 0 : case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
85 : 0 : *algo = RTE_CRYPTO_CIPHER_3DES_ECB;
86 : 0 : break;
87 : 0 : case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
88 : 0 : *algo = RTE_CRYPTO_CIPHER_3DES_CBC;
89 : 0 : break;
90 : 0 : case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
91 : 0 : *algo = RTE_CRYPTO_CIPHER_3DES_CTR;
92 : 0 : break;
93 : 0 : case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
94 : 0 : *algo = RTE_CRYPTO_CIPHER_KASUMI_F8;
95 : 0 : break;
96 : 0 : case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
97 : 0 : *algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
98 : 0 : break;
99 : 0 : case VIRTIO_CRYPTO_CIPHER_AES_F8:
100 : 0 : *algo = RTE_CRYPTO_CIPHER_AES_F8;
101 : 0 : break;
102 : 0 : case VIRTIO_CRYPTO_CIPHER_AES_XTS:
103 : 0 : *algo = RTE_CRYPTO_CIPHER_AES_XTS;
104 : 0 : break;
105 : 0 : case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
106 : 0 : *algo = RTE_CRYPTO_CIPHER_ZUC_EEA3;
107 : 0 : break;
108 : : default:
109 : : return -VIRTIO_CRYPTO_BADMSG;
110 : : break;
111 : : }
112 : :
113 : : return 0;
114 : : }
115 : :
116 : : static int
117 : 0 : auth_algo_transform(uint32_t virtio_auth_algo,
118 : : enum rte_crypto_auth_algorithm *algo)
119 : : {
120 [ # # # # : 0 : switch (virtio_auth_algo) {
# # # # #
# # # # #
# ]
121 : 0 : case VIRTIO_CRYPTO_NO_MAC:
122 : 0 : *algo = RTE_CRYPTO_AUTH_NULL;
123 : 0 : break;
124 : 0 : case VIRTIO_CRYPTO_MAC_HMAC_MD5:
125 : 0 : *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
126 : 0 : break;
127 : 0 : case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
128 : 0 : *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
129 : 0 : break;
130 : 0 : case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
131 : 0 : *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
132 : 0 : break;
133 : 0 : case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
134 : 0 : *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
135 : 0 : break;
136 : 0 : case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
137 : 0 : *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
138 : 0 : break;
139 : 0 : case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
140 : 0 : *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
141 : 0 : break;
142 : 0 : case VIRTIO_CRYPTO_MAC_CMAC_AES:
143 : 0 : *algo = RTE_CRYPTO_AUTH_AES_CMAC;
144 : 0 : break;
145 : 0 : case VIRTIO_CRYPTO_MAC_KASUMI_F9:
146 : 0 : *algo = RTE_CRYPTO_AUTH_KASUMI_F9;
147 : 0 : break;
148 : 0 : case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
149 : 0 : *algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
150 : 0 : break;
151 : 0 : case VIRTIO_CRYPTO_MAC_GMAC_AES:
152 : 0 : *algo = RTE_CRYPTO_AUTH_AES_GMAC;
153 : 0 : break;
154 : 0 : case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
155 : 0 : *algo = RTE_CRYPTO_AUTH_AES_CBC_MAC;
156 : 0 : break;
157 : 0 : case VIRTIO_CRYPTO_MAC_XCBC_AES:
158 : 0 : *algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
159 : 0 : break;
160 : : case VIRTIO_CRYPTO_MAC_CMAC_3DES:
161 : : case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
162 : : case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
163 : : return -VIRTIO_CRYPTO_NOTSUPP;
164 : 0 : default:
165 : 0 : return -VIRTIO_CRYPTO_BADMSG;
166 : : }
167 : :
168 : : return 0;
169 : : }
170 : :
171 : : static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
172 : : {
173 : : int len;
174 : :
175 : 0 : switch (algo) {
176 : : case RTE_CRYPTO_CIPHER_3DES_CBC:
177 : : len = 8;
178 : : break;
179 : : case RTE_CRYPTO_CIPHER_3DES_CTR:
180 : : len = 8;
181 : : break;
182 : : case RTE_CRYPTO_CIPHER_3DES_ECB:
183 : : len = 8;
184 : : break;
185 : 0 : case RTE_CRYPTO_CIPHER_AES_CBC:
186 : : len = 16;
187 : 0 : break;
188 : :
189 : : /* TODO: add common algos */
190 : :
191 : 0 : default:
192 : : len = -1;
193 : 0 : break;
194 : : }
195 : :
196 : : return len;
197 : : }
198 : :
199 : : /**
200 : : * vhost_crypto struct is used to maintain a number of virtio_cryptos and
201 : : * one DPDK crypto device that deals with all crypto workloads. It is declared
202 : : * here and defined in vhost_crypto.c
203 : : */
204 : : struct __rte_cache_aligned vhost_crypto {
205 : : /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
206 : : * session ID.
207 : : */
208 : : struct rte_hash *session_map;
209 : : struct rte_mempool *mbuf_pool;
210 : : struct rte_mempool *sess_pool;
211 : : struct rte_mempool *wb_pool;
212 : :
213 : : /** DPDK cryptodev ID */
214 : : uint8_t cid;
215 : : uint16_t nb_qps;
216 : :
217 : : uint64_t last_session_id;
218 : :
219 : : /** socket id for the device */
220 : : int socket_id;
221 : :
222 : : struct virtio_net *dev;
223 : :
224 : : uint8_t option;
225 : : };
226 : :
227 : : struct vhost_crypto_writeback_data {
228 : : uint8_t *src;
229 : : uint8_t *dst;
230 : : uint64_t len;
231 : : struct vhost_crypto_writeback_data *next;
232 : : };
233 : :
234 : : struct vhost_crypto_data_req {
235 : : struct vring_desc *head;
236 : : struct virtio_net *dev;
237 : : struct virtio_crypto_inhdr *inhdr;
238 : : struct vhost_virtqueue *vq;
239 : : struct vhost_crypto_writeback_data *wb;
240 : : struct rte_mempool *wb_pool;
241 : : uint16_t desc_idx;
242 : : uint16_t len;
243 : : uint16_t zero_copy;
244 : : };
245 : :
246 : : static int
247 : 0 : transform_cipher_param(struct rte_crypto_sym_xform *xform,
248 : : VhostUserCryptoSymSessionParam *param)
249 : : {
250 : : int ret;
251 : :
252 : 0 : ret = cipher_algo_transform(param->cipher_algo, &xform->cipher.algo);
253 [ # # ]: 0 : if (unlikely(ret < 0))
254 : : return ret;
255 : :
256 [ # # ]: 0 : if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
257 : : VC_LOG_DBG("Invalid cipher key length");
258 : : return -VIRTIO_CRYPTO_BADMSG;
259 : : }
260 : :
261 : 0 : xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
262 : 0 : xform->cipher.key.length = param->cipher_key_len;
263 [ # # ]: 0 : if (xform->cipher.key.length > 0)
264 : 0 : xform->cipher.key.data = param->cipher_key_buf;
265 [ # # ]: 0 : if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
266 : 0 : xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
267 [ # # ]: 0 : else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
268 : 0 : xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
269 : : else {
270 : : VC_LOG_DBG("Bad operation type");
271 : : return -VIRTIO_CRYPTO_BADMSG;
272 : : }
273 : :
274 [ # # # ]: 0 : ret = get_iv_len(xform->cipher.algo);
275 [ # # ]: 0 : if (unlikely(ret < 0))
276 : : return ret;
277 : 0 : xform->cipher.iv.length = (uint16_t)ret;
278 : 0 : xform->cipher.iv.offset = IV_OFFSET;
279 : 0 : return 0;
280 : : }
281 : :
282 : : static int
283 : 0 : transform_chain_param(struct rte_crypto_sym_xform *xforms,
284 : : VhostUserCryptoSymSessionParam *param)
285 : : {
286 : : struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
287 : : int ret;
288 : :
289 [ # # # ]: 0 : switch (param->chaining_dir) {
290 : 0 : case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
291 : : xform_auth = xforms;
292 : 0 : xform_cipher = xforms->next;
293 : 0 : xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
294 : 0 : xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
295 : 0 : break;
296 : 0 : case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
297 : : xform_cipher = xforms;
298 : 0 : xform_auth = xforms->next;
299 : 0 : xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
300 : 0 : xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
301 : 0 : break;
302 : : default:
303 : : return -VIRTIO_CRYPTO_BADMSG;
304 : : }
305 : :
306 : : /* cipher */
307 : 0 : ret = cipher_algo_transform(param->cipher_algo,
308 : : &xform_cipher->cipher.algo);
309 [ # # ]: 0 : if (unlikely(ret < 0))
310 : : return ret;
311 : :
312 [ # # ]: 0 : if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
313 : : VC_LOG_DBG("Invalid cipher key length");
314 : : return -VIRTIO_CRYPTO_BADMSG;
315 : : }
316 : :
317 : 0 : xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
318 : 0 : xform_cipher->cipher.key.length = param->cipher_key_len;
319 : 0 : xform_cipher->cipher.key.data = param->cipher_key_buf;
320 [ # # # ]: 0 : ret = get_iv_len(xform_cipher->cipher.algo);
321 [ # # ]: 0 : if (unlikely(ret < 0))
322 : : return ret;
323 : 0 : xform_cipher->cipher.iv.length = (uint16_t)ret;
324 : 0 : xform_cipher->cipher.iv.offset = IV_OFFSET;
325 : :
326 : : /* auth */
327 : 0 : xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
328 : 0 : ret = auth_algo_transform(param->hash_algo, &xform_auth->auth.algo);
329 [ # # ]: 0 : if (unlikely(ret < 0))
330 : : return ret;
331 : :
332 [ # # ]: 0 : if (param->auth_key_len > VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH) {
333 : : VC_LOG_DBG("Invalid auth key length");
334 : : return -VIRTIO_CRYPTO_BADMSG;
335 : : }
336 : :
337 : 0 : xform_auth->auth.digest_length = param->digest_len;
338 : 0 : xform_auth->auth.key.length = param->auth_key_len;
339 : 0 : xform_auth->auth.key.data = param->auth_key_buf;
340 : :
341 : 0 : return 0;
342 : : }
343 : :
344 : : static void
345 : 0 : vhost_crypto_create_sym_sess(struct vhost_crypto *vcrypto,
346 : : VhostUserCryptoSessionParam *sess_param)
347 : : {
348 : 0 : struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
349 : : struct vhost_crypto_session *vhost_session;
350 : : struct rte_cryptodev_sym_session *session;
351 : : int ret;
352 : :
353 [ # # # ]: 0 : switch (sess_param->u.sym_sess.op_type) {
354 : 0 : case VIRTIO_CRYPTO_SYM_OP_NONE:
355 : : case VIRTIO_CRYPTO_SYM_OP_CIPHER:
356 : 0 : ret = transform_cipher_param(&xform1, &sess_param->u.sym_sess);
357 [ # # ]: 0 : if (unlikely(ret)) {
358 : 0 : VC_LOG_ERR("Error transform session msg (%i)", ret);
359 : 0 : sess_param->session_id = ret;
360 : 0 : return;
361 : : }
362 : : break;
363 : 0 : case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
364 [ # # ]: 0 : if (unlikely(sess_param->u.sym_sess.hash_mode !=
365 : : VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
366 : 0 : sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
367 : 0 : VC_LOG_ERR("Error transform session message (%i)",
368 : : -VIRTIO_CRYPTO_NOTSUPP);
369 : 0 : return;
370 : : }
371 : :
372 : 0 : xform1.next = &xform2;
373 : :
374 : 0 : ret = transform_chain_param(&xform1, &sess_param->u.sym_sess);
375 [ # # ]: 0 : if (unlikely(ret)) {
376 : 0 : VC_LOG_ERR("Error transform session message (%i)", ret);
377 : 0 : sess_param->session_id = ret;
378 : 0 : return;
379 : : }
380 : :
381 : : break;
382 : 0 : default:
383 : 0 : VC_LOG_ERR("Algorithm not yet supported");
384 : 0 : sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
385 : 0 : return;
386 : : }
387 : :
388 : 0 : session = rte_cryptodev_sym_session_create(vcrypto->cid, &xform1,
389 : : vcrypto->sess_pool);
390 [ # # ]: 0 : if (!session) {
391 : 0 : VC_LOG_ERR("Failed to create session");
392 : 0 : sess_param->session_id = -VIRTIO_CRYPTO_ERR;
393 : 0 : return;
394 : : }
395 : :
396 : 0 : vhost_session = rte_zmalloc(NULL, sizeof(*vhost_session), 0);
397 [ # # ]: 0 : if (vhost_session == NULL) {
398 : 0 : VC_LOG_ERR("Failed to alloc session memory");
399 : 0 : goto error_exit;
400 : : }
401 : :
402 : 0 : vhost_session->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
403 : 0 : vhost_session->sym = session;
404 : :
405 : : /* insert session to map */
406 [ # # ]: 0 : if ((rte_hash_add_key_data(vcrypto->session_map,
407 : 0 : &vcrypto->last_session_id, vhost_session) < 0)) {
408 : 0 : VC_LOG_ERR("Failed to insert session to hash table");
409 : 0 : goto error_exit;
410 : : }
411 : :
412 : 0 : VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
413 : : vcrypto->last_session_id, vcrypto->dev->vid);
414 : :
415 : 0 : sess_param->session_id = vcrypto->last_session_id;
416 : 0 : vcrypto->last_session_id++;
417 : 0 : return;
418 : :
419 : 0 : error_exit:
420 [ # # ]: 0 : if (rte_cryptodev_sym_session_free(vcrypto->cid, session) < 0)
421 : 0 : VC_LOG_ERR("Failed to free session");
422 : :
423 : 0 : sess_param->session_id = -VIRTIO_CRYPTO_ERR;
424 : 0 : rte_free(vhost_session);
425 : : }
426 : :
427 : : static int
428 : : tlv_decode(uint8_t *tlv, uint8_t type, uint8_t **data, size_t *data_len)
429 : : {
430 : : size_t tlen = -EINVAL, len;
431 : :
432 [ # # ]: 0 : if (tlv[0] != type)
433 : : return -EINVAL;
434 : :
435 [ # # # # : 0 : if (tlv[1] == 0x82) {
# # # # #
# # # # #
# # # # ]
436 : 0 : len = (tlv[2] << 8) | tlv[3];
437 : 0 : *data = &tlv[4];
438 : 0 : tlen = len + 4;
439 [ # # # # : 0 : } else if (tlv[1] == 0x81) {
# # # # #
# # # # #
# # # # ]
440 : 0 : len = tlv[2];
441 : 0 : *data = &tlv[3];
442 : 0 : tlen = len + 3;
443 : : } else {
444 : 0 : len = tlv[1];
445 : 0 : *data = &tlv[2];
446 : 0 : tlen = len + 2;
447 : : }
448 : :
449 : : *data_len = len;
450 : 0 : return tlen;
451 : : }
452 : :
453 : : static int
454 : 0 : virtio_crypto_asym_rsa_der_to_xform(uint8_t *der, size_t der_len,
455 : : struct rte_crypto_asym_xform *xform)
456 : : {
457 : : uint8_t *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL, *dp = NULL,
458 : : *dq = NULL, *qinv = NULL, *v = NULL, *tlv;
459 : : size_t nlen, elen, dlen, plen, qlen, dplen, dqlen, qinvlen, vlen;
460 : : int len;
461 : :
462 : : RTE_SET_USED(der_len);
463 : :
464 [ # # ]: 0 : if (der[0] != 0x30)
465 : : return -EINVAL;
466 : :
467 [ # # ]: 0 : if (der[1] == 0x82)
468 : 0 : tlv = &der[4];
469 [ # # ]: 0 : else if (der[1] == 0x81)
470 : 0 : tlv = &der[3];
471 : : else
472 : : return -EINVAL;
473 : :
474 : : len = tlv_decode(tlv, 0x02, &v, &vlen);
475 [ # # # # : 0 : if (len < 0 || v[0] != 0x0 || vlen != 1)
# # ]
476 : : return -EINVAL;
477 : :
478 [ # # ]: 0 : tlv = tlv + len;
479 : : len = tlv_decode(tlv, 0x02, &n, &nlen);
480 [ # # ]: 0 : if (len < 0)
481 : : return len;
482 : :
483 [ # # ]: 0 : tlv = tlv + len;
484 : : len = tlv_decode(tlv, 0x02, &e, &elen);
485 [ # # ]: 0 : if (len < 0)
486 : : return len;
487 : :
488 [ # # ]: 0 : tlv = tlv + len;
489 : : len = tlv_decode(tlv, 0x02, &d, &dlen);
490 [ # # ]: 0 : if (len < 0)
491 : : return len;
492 : :
493 [ # # ]: 0 : tlv = tlv + len;
494 : : len = tlv_decode(tlv, 0x02, &p, &plen);
495 [ # # ]: 0 : if (len < 0)
496 : : return len;
497 : :
498 [ # # ]: 0 : tlv = tlv + len;
499 : : len = tlv_decode(tlv, 0x02, &q, &qlen);
500 [ # # ]: 0 : if (len < 0)
501 : : return len;
502 : :
503 [ # # ]: 0 : tlv = tlv + len;
504 : : len = tlv_decode(tlv, 0x02, &dp, &dplen);
505 [ # # ]: 0 : if (len < 0)
506 : : return len;
507 : :
508 [ # # ]: 0 : tlv = tlv + len;
509 : : len = tlv_decode(tlv, 0x02, &dq, &dqlen);
510 [ # # ]: 0 : if (len < 0)
511 : : return len;
512 : :
513 [ # # ]: 0 : tlv = tlv + len;
514 : : len = tlv_decode(tlv, 0x02, &qinv, &qinvlen);
515 [ # # ]: 0 : if (len < 0)
516 : : return len;
517 : :
518 : 0 : xform->rsa.n.data = n;
519 : 0 : xform->rsa.n.length = nlen;
520 : 0 : xform->rsa.e.data = e;
521 : 0 : xform->rsa.e.length = elen;
522 : 0 : xform->rsa.d.data = d;
523 : 0 : xform->rsa.d.length = dlen;
524 : 0 : xform->rsa.qt.p.data = p;
525 : 0 : xform->rsa.qt.p.length = plen;
526 : 0 : xform->rsa.qt.q.data = q;
527 : 0 : xform->rsa.qt.q.length = qlen;
528 : 0 : xform->rsa.qt.dP.data = dp;
529 : 0 : xform->rsa.qt.dP.length = dplen;
530 : 0 : xform->rsa.qt.dQ.data = dq;
531 : 0 : xform->rsa.qt.dQ.length = dqlen;
532 : 0 : xform->rsa.qt.qInv.data = qinv;
533 : 0 : xform->rsa.qt.qInv.length = qinvlen;
534 : :
535 : : RTE_ASSERT(tlv + len == der + der_len);
536 : 0 : return 0;
537 : : }
538 : :
539 : : static int
540 : 0 : rsa_param_transform(struct rte_crypto_asym_xform *xform,
541 : : VhostUserCryptoAsymSessionParam *param)
542 : : {
543 : : int ret;
544 : :
545 : 0 : ret = virtio_crypto_asym_rsa_der_to_xform(param->key_buf, param->key_len, xform);
546 [ # # ]: 0 : if (ret < 0)
547 : : return ret;
548 : :
549 [ # # # ]: 0 : switch (param->u.rsa.padding_algo) {
550 : 0 : case VIRTIO_CRYPTO_RSA_RAW_PADDING:
551 : 0 : xform->rsa.padding.type = RTE_CRYPTO_RSA_PADDING_NONE;
552 : 0 : break;
553 : 0 : case VIRTIO_CRYPTO_RSA_PKCS1_PADDING:
554 : 0 : xform->rsa.padding.type = RTE_CRYPTO_RSA_PADDING_PKCS1_5;
555 : 0 : break;
556 : 0 : default:
557 : 0 : VC_LOG_ERR("Unknown padding type");
558 : 0 : return -EINVAL;
559 : : }
560 : :
561 : 0 : xform->rsa.key_type = RTE_RSA_KEY_TYPE_QT;
562 : 0 : xform->xform_type = RTE_CRYPTO_ASYM_XFORM_RSA;
563 : 0 : return 0;
564 : : }
565 : :
566 : : static void
567 : 0 : vhost_crypto_create_asym_sess(struct vhost_crypto *vcrypto,
568 : : VhostUserCryptoSessionParam *sess_param)
569 : : {
570 : 0 : struct rte_cryptodev_asym_session *session = NULL;
571 : : struct vhost_crypto_session *vhost_session;
572 : 0 : struct rte_crypto_asym_xform xform = {0};
573 : : int ret;
574 : :
575 [ # # ]: 0 : switch (sess_param->u.asym_sess.algo) {
576 : 0 : case VIRTIO_CRYPTO_AKCIPHER_RSA:
577 : 0 : ret = rsa_param_transform(&xform, &sess_param->u.asym_sess);
578 [ # # ]: 0 : if (unlikely(ret < 0)) {
579 : 0 : VC_LOG_ERR("Error transform session msg (%i)", ret);
580 : 0 : sess_param->session_id = ret;
581 : 0 : return;
582 : : }
583 : : break;
584 : 0 : default:
585 : 0 : VC_LOG_ERR("Invalid op algo");
586 : 0 : sess_param->session_id = -VIRTIO_CRYPTO_ERR;
587 : 0 : return;
588 : : }
589 : :
590 : 0 : ret = rte_cryptodev_asym_session_create(vcrypto->cid, &xform,
591 : : vcrypto->sess_pool, (void *)&session);
592 [ # # ]: 0 : if (session == NULL) {
593 : 0 : VC_LOG_ERR("Failed to create session");
594 : 0 : sess_param->session_id = -VIRTIO_CRYPTO_ERR;
595 : 0 : return;
596 : : }
597 : :
598 : 0 : vhost_session = rte_zmalloc(NULL, sizeof(*vhost_session), 0);
599 [ # # ]: 0 : if (vhost_session == NULL) {
600 : 0 : VC_LOG_ERR("Failed to alloc session memory");
601 : 0 : goto error_exit;
602 : : }
603 : :
604 : 0 : vhost_session->type = RTE_CRYPTO_OP_TYPE_ASYMMETRIC;
605 : 0 : vhost_session->asym = session;
606 : :
607 : : /* insert session to map */
608 [ # # ]: 0 : if ((rte_hash_add_key_data(vcrypto->session_map,
609 : 0 : &vcrypto->last_session_id, vhost_session) < 0)) {
610 : 0 : VC_LOG_ERR("Failed to insert session to hash table");
611 : 0 : goto error_exit;
612 : : }
613 : :
614 : 0 : VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
615 : : vcrypto->last_session_id, vcrypto->dev->vid);
616 : :
617 : 0 : sess_param->session_id = vcrypto->last_session_id;
618 : 0 : vcrypto->last_session_id++;
619 : 0 : return;
620 : :
621 : 0 : error_exit:
622 [ # # ]: 0 : if (rte_cryptodev_asym_session_free(vcrypto->cid, session) < 0)
623 : 0 : VC_LOG_ERR("Failed to free session");
624 : 0 : sess_param->session_id = -VIRTIO_CRYPTO_ERR;
625 : 0 : rte_free(vhost_session);
626 : : }
627 : :
628 : : static void
629 : 0 : vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
630 : : VhostUserCryptoSessionParam *sess_param)
631 : : {
632 [ # # ]: 0 : if (sess_param->op_code == VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION)
633 : 0 : vhost_crypto_create_asym_sess(vcrypto, sess_param);
634 : : else
635 : 0 : vhost_crypto_create_sym_sess(vcrypto, sess_param);
636 : 0 : }
637 : :
638 : : static int
639 : 0 : vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
640 : : {
641 : 0 : struct vhost_crypto_session *vhost_session = NULL;
642 : 0 : uint64_t sess_id = session_id;
643 : : int ret;
644 : :
645 : 0 : ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
646 : : (void **)&vhost_session);
647 [ # # ]: 0 : if (unlikely(ret < 0)) {
648 : 0 : VC_LOG_ERR("Failed to find session for id %"PRIu64".", session_id);
649 : 0 : return -VIRTIO_CRYPTO_INVSESS;
650 : : }
651 : :
652 [ # # ]: 0 : if (vhost_session->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
653 [ # # ]: 0 : if (rte_cryptodev_sym_session_free(vcrypto->cid,
654 : 0 : vhost_session->sym) < 0) {
655 : : VC_LOG_DBG("Failed to free session");
656 : : return -VIRTIO_CRYPTO_ERR;
657 : : }
658 [ # # ]: 0 : } else if (vhost_session->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
659 [ # # ]: 0 : if (rte_cryptodev_asym_session_free(vcrypto->cid,
660 : 0 : vhost_session->asym) < 0) {
661 : : VC_LOG_DBG("Failed to free session");
662 : : return -VIRTIO_CRYPTO_ERR;
663 : : }
664 : : } else {
665 : 0 : VC_LOG_ERR("Invalid session for id %"PRIu64".", session_id);
666 : 0 : return -VIRTIO_CRYPTO_INVSESS;
667 : : }
668 : :
669 [ # # ]: 0 : if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
670 : : VC_LOG_DBG("Failed to delete session from hash table.");
671 : : return -VIRTIO_CRYPTO_ERR;
672 : : }
673 : :
674 : 0 : VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
675 : : vcrypto->dev->vid);
676 : :
677 : 0 : rte_free(vhost_session);
678 : 0 : return 0;
679 : : }
680 : :
681 : : static enum rte_vhost_msg_result
682 [ # # ]: 0 : vhost_crypto_msg_post_handler(int vid, void *msg)
683 : : {
684 : : struct virtio_net *dev = get_device(vid);
685 : : struct vhost_crypto *vcrypto;
686 : : struct vhu_msg_context *ctx = msg;
687 : : enum rte_vhost_msg_result ret = RTE_VHOST_MSG_RESULT_OK;
688 : :
689 [ # # ]: 0 : if (dev == NULL) {
690 : 0 : VC_LOG_ERR("Invalid vid %i", vid);
691 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
692 : : }
693 : :
694 : 0 : vcrypto = dev->extern_data;
695 [ # # ]: 0 : if (vcrypto == NULL) {
696 : 0 : VC_LOG_ERR("Cannot find required data, is it initialized?");
697 : 0 : return RTE_VHOST_MSG_RESULT_ERR;
698 : : }
699 : :
700 [ # # # ]: 0 : switch (ctx->msg.request.frontend) {
701 : 0 : case VHOST_USER_CRYPTO_CREATE_SESS:
702 : 0 : vhost_crypto_create_sess(vcrypto,
703 : : &ctx->msg.payload.crypto_session);
704 : 0 : ctx->fd_num = 0;
705 : : ret = RTE_VHOST_MSG_RESULT_REPLY;
706 : 0 : break;
707 : 0 : case VHOST_USER_CRYPTO_CLOSE_SESS:
708 [ # # ]: 0 : if (vhost_crypto_close_sess(vcrypto, ctx->msg.payload.u64))
709 : : ret = RTE_VHOST_MSG_RESULT_ERR;
710 : : break;
711 : : default:
712 : : ret = RTE_VHOST_MSG_RESULT_NOT_HANDLED;
713 : : break;
714 : : }
715 : :
716 : : return ret;
717 : : }
718 : :
719 : : static __rte_always_inline struct vhost_crypto_desc *
720 : : find_write_desc(struct vhost_crypto_desc *head, struct vhost_crypto_desc *desc,
721 : : uint32_t max_n_descs)
722 : : {
723 [ # # # # : 0 : if (desc < head)
# # # # ]
724 : : return NULL;
725 : :
726 [ # # # # : 0 : while (desc - head < (int)max_n_descs) {
# # # # #
# # # # #
# # ]
727 [ # # # # : 0 : if (desc->flags & VRING_DESC_F_WRITE)
# # # # #
# # # # #
# # ]
728 : : return desc;
729 : 0 : desc++;
730 : : }
731 : :
732 : : return NULL;
733 : : }
734 : :
735 : : static __rte_always_inline struct virtio_crypto_inhdr *
736 : : reach_inhdr(struct virtio_net *dev, struct vhost_virtqueue *vq,
737 : : struct vhost_crypto_desc *head,
738 : : uint32_t max_n_descs)
739 : : __rte_requires_shared_capability(&vq->iotlb_lock)
740 : : {
741 : : struct virtio_crypto_inhdr *inhdr;
742 : 0 : struct vhost_crypto_desc *last = head + (max_n_descs - 1);
743 : 0 : uint64_t dlen = last->len;
744 : :
745 : 0 : if (unlikely(dlen != sizeof(*inhdr)))
746 : : return NULL;
747 : :
748 [ # # # # ]: 0 : inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, dev, vq,
749 : : last->addr, &dlen, VHOST_ACCESS_WO);
750 [ # # # # : 0 : if (unlikely(!inhdr || dlen != last->len))
# # # # ]
751 : 0 : return NULL;
752 : :
753 : : return inhdr;
754 : : }
755 : :
756 : : static __rte_always_inline int
757 : : move_desc(struct vhost_crypto_desc *head,
758 : : struct vhost_crypto_desc **cur_desc,
759 : : uint32_t size, uint32_t max_n_descs)
760 : : {
761 : : struct vhost_crypto_desc *desc = *cur_desc;
762 : 0 : int left = size - desc->len;
763 : :
764 : 0 : while (desc->flags & VRING_DESC_F_NEXT && left > 0 &&
765 [ # # # # : 0 : desc >= head &&
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
766 [ # # # # : 0 : desc - head < (int)max_n_descs) {
# # # # #
# # # # #
# # # # #
# ]
767 : 0 : desc++;
768 : 0 : left -= desc->len;
769 : : }
770 : :
771 [ # # # # : 0 : if (unlikely(left > 0))
# # # # #
# # # # #
# # # # #
# ]
772 : : return -1;
773 : :
774 [ # # # # : 0 : if (unlikely(head - desc == (int)max_n_descs))
# # # # #
# # # # #
# # # # #
# ]
775 : : *cur_desc = NULL;
776 : : else
777 : 0 : *cur_desc = desc + 1;
778 : :
779 : : return 0;
780 : : }
781 : :
782 : : static __rte_always_inline void *
783 : : get_data_ptr(struct vhost_virtqueue *vq, struct vhost_crypto_data_req *vc_req,
784 : : struct vhost_crypto_desc *cur_desc,
785 : : uint8_t perm)
786 : : __rte_requires_shared_capability(&vq->iotlb_lock)
787 : : {
788 : : void *data;
789 : 0 : uint64_t dlen = cur_desc->len;
790 : :
791 [ # # # # : 0 : data = IOVA_TO_VVA(void *, vc_req->dev, vq,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
792 : : cur_desc->addr, &dlen, perm);
793 [ # # # # : 0 : if (unlikely(!data || dlen != cur_desc->len)) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
794 : 0 : VC_LOG_ERR("Failed to map object");
795 : 0 : return NULL;
796 : : }
797 : :
798 : : return data;
799 : : }
800 : :
801 : : static __rte_always_inline uint32_t
802 : : copy_data_from_desc(void *dst, struct virtio_net *dev,
803 : : struct vhost_virtqueue *vq, struct vhost_crypto_desc *desc, uint32_t size)
804 : : __rte_requires_shared_capability(&vq->iotlb_lock)
805 : : {
806 : : uint64_t remain;
807 : : uint64_t addr;
808 : :
809 : 0 : remain = RTE_MIN(desc->len, size);
810 : 0 : addr = desc->addr;
811 : : do {
812 : : uint64_t len;
813 : : void *src;
814 : :
815 [ # # # # : 0 : len = remain;
# # # # #
# # # # #
# # # # #
# # # #
# ]
816 : 0 : src = IOVA_TO_VVA(void *, dev, vq,
817 : : addr, &len, VHOST_ACCESS_RO);
818 [ # # # # : 0 : if (unlikely(src == NULL || len == 0))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
819 : 0 : return 0;
820 : :
821 : : rte_memcpy(dst, src, len);
822 : 0 : remain -= len;
823 : : /* cast is needed for 32-bit architecture */
824 : 0 : dst = RTE_PTR_ADD(dst, (size_t)len);
825 : 0 : addr += len;
826 [ # # # # : 0 : } while (unlikely(remain != 0));
# # # # #
# # # # #
# # # # #
# # # #
# ]
827 : :
828 : : return RTE_MIN(desc->len, size);
829 : : }
830 : :
831 : :
832 : : static __rte_always_inline int
833 : : copy_data(void *data, struct virtio_net *dev, struct vhost_virtqueue *vq,
834 : : struct vhost_crypto_desc *head, struct vhost_crypto_desc **cur_desc,
835 : : uint32_t size, uint32_t max_n_descs)
836 : : __rte_requires_shared_capability(&vq->iotlb_lock)
837 : : {
838 : : struct vhost_crypto_desc *desc = *cur_desc;
839 : : uint32_t left = size;
840 : :
841 : : do {
842 : : uint32_t copied;
843 : :
844 : : copied = copy_data_from_desc(data, dev, vq, desc, left);
845 [ # # # # : 0 : if (copied == 0)
# # # # #
# # # # #
# # # # #
# # # #
# ]
846 : : return -1;
847 : 0 : left -= copied;
848 : 0 : data = RTE_PTR_ADD(data, copied);
849 [ # # # # : 0 : } while (left != 0 && ++desc < head + max_n_descs);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
850 : :
851 [ # # # # : 0 : if (unlikely(left != 0))
# # # # #
# # # # #
# # # # #
# # # #
# ]
852 : : return -1;
853 : :
854 [ # # # # : 0 : if (unlikely(desc == head + max_n_descs))
# # # # #
# # # # #
# # # # #
# ]
855 : : *cur_desc = NULL;
856 : : else
857 : 0 : *cur_desc = desc + 1;
858 : :
859 : : return 0;
860 : : }
861 : :
862 : : static void
863 : 0 : init_wb_data(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
864 : : void *obj, unsigned int obj_idx __rte_unused)
865 : : {
866 : : struct vhost_crypto_writeback_data *wb_data = obj;
867 : :
868 : 0 : wb_data->next = NULL;
869 : 0 : }
870 : :
871 : : static void
872 : 0 : write_back_data(struct vhost_crypto_data_req *vc_req)
873 : : {
874 : 0 : struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
875 : :
876 [ # # ]: 0 : while (wb_data) {
877 [ # # ]: 0 : rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
878 [ # # ]: 0 : memset(wb_data->src, 0, wb_data->len);
879 : : wb_last = wb_data;
880 : 0 : wb_data = wb_data->next;
881 : :
882 : 0 : wb_last->next = NULL;
883 [ # # ]: 0 : rte_mempool_put(vc_req->wb_pool, wb_last);
884 : : }
885 : 0 : }
886 : :
887 : : static void
888 : 0 : free_wb_data(struct vhost_crypto_writeback_data *wb_data,
889 : : struct rte_mempool *mp)
890 : : {
891 [ # # ]: 0 : if (wb_data->next != NULL)
892 : 0 : free_wb_data(wb_data->next, mp);
893 : :
894 [ # # ]: 0 : wb_data->next = NULL;
895 : 0 : rte_mempool_put(mp, wb_data);
896 : 0 : }
897 : :
898 : : /**
899 : : * The function will allocate a vhost_crypto_writeback_data linked list
900 : : * containing the source and destination data pointers for the write back
901 : : * operation after dequeued from Cryptodev PMD queues.
902 : : *
903 : : * @param vc_req
904 : : * The vhost crypto data request pointer
905 : : * @param cur_desc
906 : : * The pointer of the current in use descriptor pointer. The content of
907 : : * cur_desc is expected to be updated after the function execution.
908 : : * @param end_wb_data
909 : : * The last write back data element to be returned. It is used only in cipher
910 : : * and hash chain operations.
911 : : * @param src
912 : : * The source data pointer
913 : : * @param offset
914 : : * The offset to both source and destination data. For source data the offset
915 : : * is the number of bytes between src and start point of cipher operation. For
916 : : * destination data the offset is the number of bytes from *cur_desc->addr
917 : : * to the point where the src will be written to.
918 : : * @param write_back_len
919 : : * The size of the write back length.
920 : : * @return
921 : : * The pointer to the start of the write back data linked list.
922 : : */
923 : : static __rte_always_inline struct vhost_crypto_writeback_data *
924 : : prepare_write_back_data(struct vhost_virtqueue *vq,
925 : : struct vhost_crypto_data_req *vc_req,
926 : : struct vhost_crypto_desc *head_desc,
927 : : struct vhost_crypto_desc **cur_desc,
928 : : struct vhost_crypto_writeback_data **end_wb_data,
929 : : uint8_t *src,
930 : : uint32_t offset,
931 : : uint64_t write_back_len,
932 : : uint32_t max_n_descs)
933 : : __rte_requires_shared_capability(&vq->iotlb_lock)
934 : : {
935 : 0 : struct vhost_crypto_writeback_data *wb_data, *head = NULL;
936 : : struct vhost_crypto_desc *desc = *cur_desc;
937 : : uint64_t dlen;
938 : : uint8_t *dst;
939 : : int ret;
940 : :
941 : 0 : ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
942 [ # # # # : 0 : if (unlikely(ret < 0)) {
# # # # #
# # # ]
943 : 0 : VC_LOG_ERR("no memory");
944 : 0 : goto error_exit;
945 : : }
946 : :
947 : 0 : wb_data = head;
948 : :
949 [ # # # # : 0 : if (likely(desc->len > offset)) {
# # # # #
# # # ]
950 : 0 : wb_data->src = src + offset;
951 : 0 : dlen = desc->len;
952 [ # # # # : 0 : dst = IOVA_TO_VVA(uint8_t *, vc_req->dev, vq,
# # # # #
# # # ]
953 : : desc->addr, &dlen, VHOST_ACCESS_RW);
954 [ # # # # : 0 : if (unlikely(!dst || dlen != desc->len)) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
955 : 0 : VC_LOG_ERR("Failed to map descriptor");
956 : 0 : goto error_exit;
957 : : }
958 : :
959 : 0 : wb_data->dst = dst + offset;
960 : 0 : wb_data->len = RTE_MIN(dlen - offset, write_back_len);
961 : 0 : write_back_len -= wb_data->len;
962 : 0 : src += offset + wb_data->len;
963 : : offset = 0;
964 : :
965 [ # # # # : 0 : if (unlikely(write_back_len)) {
# # # # #
# # # ]
966 : 0 : ret = rte_mempool_get(vc_req->wb_pool,
967 [ # # # # : 0 : (void **)&(wb_data->next));
# # # # #
# # # ]
968 [ # # # # : 0 : if (unlikely(ret < 0)) {
# # # # #
# # # ]
969 : 0 : VC_LOG_ERR("no memory");
970 : 0 : goto error_exit;
971 : : }
972 : :
973 : 0 : wb_data = wb_data->next;
974 : : }
975 : : } else
976 : 0 : offset -= desc->len;
977 : :
978 : 0 : while (write_back_len &&
979 [ # # # # : 0 : desc >= head_desc &&
# # # # #
# # # ]
980 [ # # # # : 0 : desc - head_desc < (int)max_n_descs) {
# # # # #
# # # ]
981 : 0 : desc++;
982 [ # # # # : 0 : if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
# # # # #
# # # ]
983 : 0 : VC_LOG_ERR("incorrect descriptor");
984 : 0 : goto error_exit;
985 : : }
986 : :
987 [ # # # # : 0 : if (desc->len <= offset) {
# # # # #
# # # ]
988 : 0 : offset -= desc->len;
989 : 0 : continue;
990 : : }
991 : :
992 : 0 : dlen = desc->len;
993 : 0 : dst = IOVA_TO_VVA(uint8_t *, vc_req->dev, vq,
994 [ # # # # : 0 : desc->addr, &dlen, VHOST_ACCESS_RW) + offset;
# # # # #
# # # ]
995 [ # # # # : 0 : if (unlikely(dst == NULL || dlen != desc->len)) {
# # # # #
# # # # #
# # # # #
# # # #
# ]
996 : 0 : VC_LOG_ERR("Failed to map descriptor");
997 : 0 : goto error_exit;
998 : : }
999 : :
1000 : 0 : wb_data->src = src + offset;
1001 : 0 : wb_data->dst = dst;
1002 : 0 : wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
1003 : 0 : write_back_len -= wb_data->len;
1004 : 0 : src += wb_data->len;
1005 : : offset = 0;
1006 : :
1007 [ # # # # : 0 : if (write_back_len) {
# # # # #
# # # ]
1008 : 0 : ret = rte_mempool_get(vc_req->wb_pool,
1009 [ # # # # : 0 : (void **)&(wb_data->next));
# # # # #
# # # ]
1010 [ # # # # : 0 : if (unlikely(ret < 0)) {
# # # # #
# # # ]
1011 : 0 : VC_LOG_ERR("no memory");
1012 : 0 : goto error_exit;
1013 : : }
1014 : :
1015 : 0 : wb_data = wb_data->next;
1016 : : }
1017 : : }
1018 : :
1019 [ # # # # : 0 : if (unlikely(desc - head_desc == (int)max_n_descs))
# # # # #
# # # ]
1020 : : *cur_desc = NULL;
1021 : : else
1022 : 0 : *cur_desc = desc + 1;
1023 : :
1024 : : *end_wb_data = wb_data;
1025 : :
1026 : 0 : return head;
1027 : :
1028 : 0 : error_exit:
1029 [ # # # # : 0 : if (head)
# # # # #
# # # ]
1030 : 0 : free_wb_data(head, vc_req->wb_pool);
1031 : :
1032 : : return NULL;
1033 : : }
1034 : :
1035 : : static __rte_always_inline uint8_t
1036 : : vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req *req)
1037 : : {
1038 [ # # # # : 0 : if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
# # # # #
# # # ]
1039 : : (req->para.src_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE) &&
1040 : : (req->para.dst_data_len >= req->para.src_data_len) &&
1041 : : (req->para.dst_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE)))
1042 : 0 : return VIRTIO_CRYPTO_OK;
1043 : : return VIRTIO_CRYPTO_BADMSG;
1044 : : }
1045 : :
1046 : : static __rte_always_inline uint8_t
1047 : : prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
1048 : : struct vhost_virtqueue *vq,
1049 : : struct vhost_crypto_data_req *vc_req,
1050 : : struct virtio_crypto_cipher_data_req *cipher,
1051 : : struct vhost_crypto_desc *head,
1052 : : uint32_t max_n_descs)
1053 : : __rte_requires_shared_capability(&vq->iotlb_lock)
1054 : : {
1055 : : struct vhost_crypto_desc *desc = head;
1056 : : struct vhost_crypto_writeback_data *ewb = NULL;
1057 : 0 : struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
1058 : 0 : uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
1059 : : uint8_t ret = vhost_crypto_check_cipher_request(cipher);
1060 : :
1061 [ # # # # ]: 0 : if (unlikely(ret != VIRTIO_CRYPTO_OK))
1062 : 0 : goto error_exit;
1063 : :
1064 : : /* prepare */
1065 : : /* iv */
1066 [ # # # # ]: 0 : if (unlikely(copy_data(iv_data, vcrypto->dev, vq, head, &desc,
1067 : : cipher->para.iv_len, max_n_descs))) {
1068 : 0 : VC_LOG_ERR("Incorrect virtio descriptor");
1069 : : ret = VIRTIO_CRYPTO_BADMSG;
1070 : 0 : goto error_exit;
1071 : : }
1072 : :
1073 [ # # # # : 0 : switch (vcrypto->option) {
# # ]
1074 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1075 : 0 : m_src->data_len = cipher->para.src_data_len;
1076 : 0 : rte_mbuf_iova_set(m_src,
1077 [ # # # # ]: 0 : gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.src_data_len));
1078 : 0 : m_src->buf_addr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
1079 [ # # # # : 0 : if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) {
# # # # ]
1080 : 0 : VC_LOG_ERR("zero_copy may fail due to cross page data");
1081 : : ret = VIRTIO_CRYPTO_ERR;
1082 : 0 : goto error_exit;
1083 : : }
1084 : :
1085 [ # # # # ]: 0 : if (unlikely(move_desc(head, &desc, cipher->para.src_data_len,
1086 : : max_n_descs) < 0)) {
1087 : 0 : VC_LOG_ERR("Incorrect descriptor");
1088 : : ret = VIRTIO_CRYPTO_ERR;
1089 : 0 : goto error_exit;
1090 : : }
1091 : :
1092 : : break;
1093 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1094 : 0 : vc_req->wb_pool = vcrypto->wb_pool;
1095 : 0 : m_src->data_len = cipher->para.src_data_len;
1096 [ # # # # ]: 0 : if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1097 : : vcrypto->dev, vq, head, &desc,
1098 : : cipher->para.src_data_len, max_n_descs) < 0)) {
1099 : 0 : VC_LOG_ERR("Incorrect virtio descriptor");
1100 : : ret = VIRTIO_CRYPTO_BADMSG;
1101 : 0 : goto error_exit;
1102 : : }
1103 : : break;
1104 : 0 : default:
1105 : : ret = VIRTIO_CRYPTO_BADMSG;
1106 : 0 : goto error_exit;
1107 : : }
1108 : :
1109 : : /* dst */
1110 : : desc = find_write_desc(head, desc, max_n_descs);
1111 [ # # # # ]: 0 : if (unlikely(!desc)) {
1112 : 0 : VC_LOG_ERR("Cannot find write location");
1113 : : ret = VIRTIO_CRYPTO_BADMSG;
1114 : 0 : goto error_exit;
1115 : : }
1116 : :
1117 [ # # # # : 0 : switch (vcrypto->option) {
# # ]
1118 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1119 : 0 : rte_mbuf_iova_set(m_dst,
1120 [ # # # # ]: 0 : gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.dst_data_len));
1121 : 0 : m_dst->buf_addr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
1122 [ # # # # : 0 : if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) {
# # # # ]
1123 : 0 : VC_LOG_ERR("zero_copy may fail due to cross page data");
1124 : : ret = VIRTIO_CRYPTO_ERR;
1125 : 0 : goto error_exit;
1126 : : }
1127 : :
1128 [ # # # # ]: 0 : if (unlikely(move_desc(head, &desc, cipher->para.dst_data_len,
1129 : : max_n_descs) < 0)) {
1130 : 0 : VC_LOG_ERR("Incorrect descriptor");
1131 : : ret = VIRTIO_CRYPTO_ERR;
1132 : 0 : goto error_exit;
1133 : : }
1134 : :
1135 : 0 : m_dst->data_len = cipher->para.dst_data_len;
1136 : 0 : break;
1137 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1138 : 0 : vc_req->wb = prepare_write_back_data(vq, vc_req, head, &desc, &ewb,
1139 : 0 : rte_pktmbuf_mtod(m_src, uint8_t *), 0,
1140 [ # # # # ]: 0 : cipher->para.dst_data_len, max_n_descs);
1141 [ # # # # ]: 0 : if (unlikely(vc_req->wb == NULL)) {
1142 : : ret = VIRTIO_CRYPTO_ERR;
1143 : 0 : goto error_exit;
1144 : : }
1145 : :
1146 : : break;
1147 : 0 : default:
1148 : : ret = VIRTIO_CRYPTO_BADMSG;
1149 : 0 : goto error_exit;
1150 : : }
1151 : :
1152 : : /* src data */
1153 : 0 : op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1154 : 0 : op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1155 : :
1156 : 0 : op->sym->cipher.data.offset = 0;
1157 [ # # # # ]: 0 : op->sym->cipher.data.length = cipher->para.src_data_len;
1158 : :
1159 : 0 : vc_req->inhdr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_WO);
1160 [ # # # # ]: 0 : if (unlikely(vc_req->inhdr == NULL)) {
1161 : : ret = VIRTIO_CRYPTO_BADMSG;
1162 : 0 : goto error_exit;
1163 : : }
1164 : :
1165 : 0 : vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1166 : 0 : vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
1167 : :
1168 : 0 : return 0;
1169 : :
1170 : 0 : error_exit:
1171 [ # # # # ]: 0 : if (vc_req->wb)
1172 : 0 : free_wb_data(vc_req->wb, vc_req->wb_pool);
1173 : :
1174 : 0 : vc_req->len = INHDR_LEN;
1175 : 0 : return ret;
1176 : : }
1177 : :
1178 : : static __rte_always_inline uint8_t
1179 : : vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req)
1180 : : {
1181 [ # # # # : 0 : if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
1182 : : (req->para.src_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
1183 : : (req->para.dst_data_len >= req->para.src_data_len) &&
1184 : : (req->para.dst_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
1185 : : (req->para.cipher_start_src_offset <
1186 : : VHOST_CRYPTO_MAX_DATA_SIZE) &&
1187 : : (req->para.len_to_cipher <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
1188 : : (req->para.hash_start_src_offset <
1189 : : VHOST_CRYPTO_MAX_DATA_SIZE) &&
1190 : : (req->para.len_to_hash <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
1191 : : ((uint64_t)req->para.cipher_start_src_offset + req->para.len_to_cipher <=
1192 : : req->para.src_data_len) &&
1193 : : ((uint64_t)req->para.hash_start_src_offset + req->para.len_to_hash <=
1194 : : req->para.src_data_len) &&
1195 : : ((uint64_t)req->para.dst_data_len + req->para.hash_result_len <=
1196 : : VHOST_CRYPTO_MAX_DATA_SIZE)))
1197 : 0 : return VIRTIO_CRYPTO_OK;
1198 : : return VIRTIO_CRYPTO_BADMSG;
1199 : : }
1200 : :
1201 : : static __rte_always_inline uint8_t
1202 : : prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
1203 : : struct vhost_virtqueue *vq,
1204 : : struct vhost_crypto_data_req *vc_req,
1205 : : struct virtio_crypto_alg_chain_data_req *chain,
1206 : : struct vhost_crypto_desc *head,
1207 : : uint32_t max_n_descs)
1208 : : __rte_requires_shared_capability(&vq->iotlb_lock)
1209 : : {
1210 : : struct vhost_crypto_desc *desc = head, *digest_desc;
1211 : : struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
1212 : 0 : struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
1213 : 0 : uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
1214 : : uint32_t digest_offset;
1215 : : void *digest_addr;
1216 : : uint8_t ret = vhost_crypto_check_chain_request(chain);
1217 : :
1218 [ # # # # ]: 0 : if (unlikely(ret != VIRTIO_CRYPTO_OK))
1219 : 0 : goto error_exit;
1220 : :
1221 : : /* prepare */
1222 : : /* iv */
1223 [ # # # # ]: 0 : if (unlikely(copy_data(iv_data, vcrypto->dev, vq, head, &desc,
1224 : : chain->para.iv_len, max_n_descs) < 0)) {
1225 : 0 : VC_LOG_ERR("Incorrect virtio descriptor");
1226 : : ret = VIRTIO_CRYPTO_BADMSG;
1227 : 0 : goto error_exit;
1228 : : }
1229 : :
1230 [ # # # # : 0 : switch (vcrypto->option) {
# # ]
1231 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1232 : 0 : m_src->data_len = chain->para.src_data_len;
1233 : 0 : m_dst->data_len = chain->para.dst_data_len;
1234 : :
1235 : 0 : rte_mbuf_iova_set(m_src,
1236 [ # # # # ]: 0 : gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.src_data_len));
1237 : 0 : m_src->buf_addr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
1238 [ # # # # : 0 : if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) {
# # # # ]
1239 : 0 : VC_LOG_ERR("zero_copy may fail due to cross page data");
1240 : : ret = VIRTIO_CRYPTO_ERR;
1241 : 0 : goto error_exit;
1242 : : }
1243 : :
1244 [ # # # # ]: 0 : if (unlikely(move_desc(head, &desc, chain->para.src_data_len,
1245 : : max_n_descs) < 0)) {
1246 : 0 : VC_LOG_ERR("Incorrect descriptor");
1247 : : ret = VIRTIO_CRYPTO_ERR;
1248 : 0 : goto error_exit;
1249 : : }
1250 : : break;
1251 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1252 : 0 : vc_req->wb_pool = vcrypto->wb_pool;
1253 : 0 : m_src->data_len = chain->para.src_data_len;
1254 [ # # # # ]: 0 : if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1255 : : vcrypto->dev, vq, head, &desc,
1256 : : chain->para.src_data_len, max_n_descs) < 0)) {
1257 : 0 : VC_LOG_ERR("Incorrect virtio descriptor");
1258 : : ret = VIRTIO_CRYPTO_BADMSG;
1259 : 0 : goto error_exit;
1260 : : }
1261 : :
1262 : : break;
1263 : 0 : default:
1264 : : ret = VIRTIO_CRYPTO_BADMSG;
1265 : 0 : goto error_exit;
1266 : : }
1267 : :
1268 : : /* dst */
1269 : : desc = find_write_desc(head, desc, max_n_descs);
1270 [ # # # # ]: 0 : if (unlikely(!desc)) {
1271 : 0 : VC_LOG_ERR("Cannot find write location");
1272 : : ret = VIRTIO_CRYPTO_BADMSG;
1273 : 0 : goto error_exit;
1274 : : }
1275 : :
1276 [ # # # # : 0 : switch (vcrypto->option) {
# # ]
1277 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1278 : 0 : rte_mbuf_iova_set(m_dst,
1279 [ # # # # ]: 0 : gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.dst_data_len));
1280 : 0 : m_dst->buf_addr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
1281 [ # # # # : 0 : if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) {
# # # # ]
1282 : 0 : VC_LOG_ERR("zero_copy may fail due to cross page data");
1283 : : ret = VIRTIO_CRYPTO_ERR;
1284 : 0 : goto error_exit;
1285 : : }
1286 : :
1287 [ # # # # ]: 0 : if (unlikely(move_desc(vc_req->head, &desc,
1288 : : chain->para.dst_data_len, max_n_descs) < 0)) {
1289 : 0 : VC_LOG_ERR("Incorrect descriptor");
1290 : : ret = VIRTIO_CRYPTO_ERR;
1291 : 0 : goto error_exit;
1292 : : }
1293 : :
1294 [ # # # # ]: 0 : op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1295 [ # # # # ]: 0 : desc->addr, chain->para.hash_result_len);
1296 : 0 : op->sym->auth.digest.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
1297 [ # # # # ]: 0 : if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1298 : 0 : VC_LOG_ERR("zero_copy may fail due to cross page data");
1299 : : ret = VIRTIO_CRYPTO_ERR;
1300 : 0 : goto error_exit;
1301 : : }
1302 : :
1303 [ # # # # ]: 0 : if (unlikely(move_desc(head, &desc,
1304 : : chain->para.hash_result_len,
1305 : : max_n_descs) < 0)) {
1306 : 0 : VC_LOG_ERR("Incorrect descriptor");
1307 : : ret = VIRTIO_CRYPTO_ERR;
1308 : 0 : goto error_exit;
1309 : : }
1310 : :
1311 : : break;
1312 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1313 : 0 : vc_req->wb = prepare_write_back_data(vq, vc_req, head, &desc, &ewb,
1314 : 0 : rte_pktmbuf_mtod(m_src, uint8_t *),
1315 : : chain->para.cipher_start_src_offset,
1316 : 0 : chain->para.dst_data_len -
1317 [ # # # # ]: 0 : chain->para.cipher_start_src_offset,
1318 : : max_n_descs);
1319 [ # # # # ]: 0 : if (unlikely(vc_req->wb == NULL)) {
1320 : : ret = VIRTIO_CRYPTO_ERR;
1321 : 0 : goto error_exit;
1322 : : }
1323 : :
1324 : : digest_desc = desc;
1325 : 0 : digest_offset = m_src->data_len;
1326 : 0 : digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1327 : : digest_offset);
1328 : :
1329 : : /** create a wb_data for digest */
1330 : 0 : ewb->next = prepare_write_back_data(vq, vc_req, head, &desc,
1331 : : &ewb2, digest_addr, 0,
1332 [ # # # # ]: 0 : chain->para.hash_result_len, max_n_descs);
1333 [ # # # # ]: 0 : if (unlikely(ewb->next == NULL)) {
1334 : : ret = VIRTIO_CRYPTO_ERR;
1335 : 0 : goto error_exit;
1336 : : }
1337 : :
1338 [ # # # # ]: 0 : if (unlikely(copy_data(digest_addr, vcrypto->dev, vq, head,
1339 : : &digest_desc, chain->para.hash_result_len,
1340 : : max_n_descs) < 0)) {
1341 : 0 : VC_LOG_ERR("Incorrect virtio descriptor");
1342 : : ret = VIRTIO_CRYPTO_BADMSG;
1343 : 0 : goto error_exit;
1344 : : }
1345 : :
1346 : 0 : op->sym->auth.digest.data = digest_addr;
1347 : 0 : op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1348 : : digest_offset);
1349 : 0 : break;
1350 : 0 : default:
1351 : : ret = VIRTIO_CRYPTO_BADMSG;
1352 : 0 : goto error_exit;
1353 : : }
1354 : :
1355 : : /* record inhdr */
1356 : 0 : vc_req->inhdr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_WO);
1357 [ # # # # ]: 0 : if (unlikely(vc_req->inhdr == NULL)) {
1358 : : ret = VIRTIO_CRYPTO_BADMSG;
1359 : 0 : goto error_exit;
1360 : : }
1361 : :
1362 : 0 : vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1363 : :
1364 : 0 : op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1365 : 0 : op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1366 : :
1367 : 0 : op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1368 : 0 : op->sym->cipher.data.length = chain->para.len_to_cipher;
1369 : :
1370 : 0 : op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1371 : 0 : op->sym->auth.data.length = chain->para.len_to_hash;
1372 : :
1373 : 0 : vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1374 : : INHDR_LEN;
1375 : 0 : return 0;
1376 : :
1377 : 0 : error_exit:
1378 [ # # # # ]: 0 : if (vc_req->wb)
1379 : 0 : free_wb_data(vc_req->wb, vc_req->wb_pool);
1380 : 0 : vc_req->len = INHDR_LEN;
1381 : 0 : return ret;
1382 : : }
1383 : :
1384 : : static __rte_always_inline uint8_t
1385 : : prepare_asym_rsa_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
1386 : : struct vhost_virtqueue *vq,
1387 : : struct vhost_crypto_data_req *vc_req,
1388 : : struct virtio_crypto_op_data_req *req,
1389 : : struct vhost_crypto_desc *head,
1390 : : uint32_t max_n_descs)
1391 : : __rte_requires_shared_capability(&vq->iotlb_lock)
1392 : : {
1393 : : struct rte_crypto_rsa_op_param *rsa = &op->asym->rsa;
1394 : : struct vhost_crypto_desc *desc = head;
1395 : : uint8_t ret = VIRTIO_CRYPTO_ERR;
1396 : : uint16_t wlen = 0;
1397 : :
1398 : : /* prepare */
1399 : 0 : switch (vcrypto->option) {
1400 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1401 : 0 : vc_req->wb_pool = vcrypto->wb_pool;
1402 [ # # # # ]: 0 : if (req->header.opcode == VIRTIO_CRYPTO_AKCIPHER_SIGN) {
1403 [ # # # # ]: 0 : rsa->op_type = RTE_CRYPTO_ASYM_OP_SIGN;
1404 : 0 : rsa->message.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
1405 : 0 : rsa->message.length = req->u.akcipher_req.para.src_data_len;
1406 : 0 : rsa->sign.length = req->u.akcipher_req.para.dst_data_len;
1407 : 0 : wlen = rsa->sign.length;
1408 : : desc = find_write_desc(head, desc, max_n_descs);
1409 [ # # # # ]: 0 : if (unlikely(!desc)) {
1410 : 0 : VC_LOG_ERR("Cannot find write location");
1411 : : ret = VIRTIO_CRYPTO_BADMSG;
1412 : 0 : goto error_exit;
1413 : : }
1414 : :
1415 : 0 : rsa->sign.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
1416 [ # # # # ]: 0 : if (unlikely(rsa->sign.data == NULL)) {
1417 : : ret = VIRTIO_CRYPTO_ERR;
1418 : 0 : goto error_exit;
1419 : : }
1420 : :
1421 : 0 : desc += 1;
1422 [ # # # # ]: 0 : } else if (req->header.opcode == VIRTIO_CRYPTO_AKCIPHER_VERIFY) {
1423 [ # # # # ]: 0 : rsa->op_type = RTE_CRYPTO_ASYM_OP_VERIFY;
1424 : 0 : rsa->sign.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
1425 [ # # # # ]: 0 : rsa->sign.length = req->u.akcipher_req.para.src_data_len;
1426 : : desc += 1;
1427 : 0 : rsa->message.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
1428 : 0 : rsa->message.length = req->u.akcipher_req.para.dst_data_len;
1429 : 0 : desc += 1;
1430 [ # # # # ]: 0 : } else if (req->header.opcode == VIRTIO_CRYPTO_AKCIPHER_ENCRYPT) {
1431 [ # # # # ]: 0 : rsa->op_type = RTE_CRYPTO_ASYM_OP_ENCRYPT;
1432 : 0 : rsa->message.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
1433 : 0 : rsa->message.length = req->u.akcipher_req.para.src_data_len;
1434 : 0 : rsa->cipher.length = req->u.akcipher_req.para.dst_data_len;
1435 : 0 : wlen = rsa->cipher.length;
1436 : : desc = find_write_desc(head, desc, max_n_descs);
1437 [ # # # # ]: 0 : if (unlikely(!desc)) {
1438 : 0 : VC_LOG_ERR("Cannot find write location");
1439 : : ret = VIRTIO_CRYPTO_BADMSG;
1440 : 0 : goto error_exit;
1441 : : }
1442 : :
1443 : 0 : rsa->cipher.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RW);
1444 [ # # # # ]: 0 : if (unlikely(rsa->cipher.data == NULL)) {
1445 : : ret = VIRTIO_CRYPTO_ERR;
1446 : 0 : goto error_exit;
1447 : : }
1448 : :
1449 : 0 : desc += 1;
1450 : : } else if (req->header.opcode == VIRTIO_CRYPTO_AKCIPHER_DECRYPT) {
1451 [ # # # # ]: 0 : rsa->op_type = RTE_CRYPTO_ASYM_OP_DECRYPT;
1452 : 0 : rsa->cipher.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
1453 [ # # # # ]: 0 : rsa->cipher.length = req->u.akcipher_req.para.src_data_len;
1454 : : desc += 1;
1455 : 0 : rsa->message.data = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_RO);
1456 : 0 : rsa->message.length = req->u.akcipher_req.para.dst_data_len;
1457 : 0 : desc += 1;
1458 : : } else {
1459 : : goto error_exit;
1460 : : }
1461 : : break;
1462 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1463 : : default:
1464 : : ret = VIRTIO_CRYPTO_BADMSG;
1465 : 0 : goto error_exit;
1466 : : }
1467 : :
1468 : 0 : op->type = RTE_CRYPTO_OP_TYPE_ASYMMETRIC;
1469 [ # # # # ]: 0 : op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1470 : :
1471 : 0 : vc_req->inhdr = get_data_ptr(vq, vc_req, desc, VHOST_ACCESS_WO);
1472 [ # # # # ]: 0 : if (unlikely(vc_req->inhdr == NULL)) {
1473 : : ret = VIRTIO_CRYPTO_BADMSG;
1474 : 0 : goto error_exit;
1475 : : }
1476 : :
1477 : 0 : vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1478 : 0 : vc_req->len = wlen + INHDR_LEN;
1479 : 0 : return 0;
1480 : 0 : error_exit:
1481 [ # # # # ]: 0 : if (vc_req->wb)
1482 : 0 : free_wb_data(vc_req->wb, vc_req->wb_pool);
1483 : :
1484 : 0 : vc_req->len = INHDR_LEN;
1485 : 0 : return ret;
1486 : : }
1487 : :
1488 : : /**
1489 : : * Process on descriptor
1490 : : */
1491 : : static __rte_always_inline int
1492 : : vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1493 : : struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1494 : : struct vring_desc *head, struct vhost_crypto_desc *descs,
1495 : : uint16_t desc_idx)
1496 : : __rte_requires_shared_capability(&vq->iotlb_lock)
1497 : : {
1498 : : struct vhost_crypto_data_req *vc_req, *vc_req_out;
1499 : : struct rte_cryptodev_asym_session *asym_session;
1500 : : struct rte_cryptodev_sym_session *sym_session;
1501 : 0 : struct vhost_crypto_data_req data_req = {0};
1502 : : struct vhost_crypto_session *vhost_session;
1503 : : struct vhost_crypto_desc *desc = descs;
1504 : : uint32_t nb_descs = 0, max_n_descs, i;
1505 : : struct virtio_crypto_op_data_req req;
1506 : : struct virtio_crypto_inhdr *inhdr;
1507 : : struct vring_desc *src_desc;
1508 : : uint64_t session_id;
1509 : : uint64_t dlen;
1510 : : int err;
1511 : :
1512 : : vc_req = &data_req;
1513 : 0 : vc_req->desc_idx = desc_idx;
1514 : 0 : vc_req->dev = vcrypto->dev;
1515 : 0 : vc_req->vq = vq;
1516 : :
1517 : 0 : if (unlikely((head->flags & VRING_DESC_F_INDIRECT) == 0)) {
1518 : 0 : VC_LOG_ERR("Invalid descriptor");
1519 : 0 : return -1;
1520 : : }
1521 : :
1522 : 0 : dlen = head->len;
1523 [ # # # # ]: 0 : src_desc = IOVA_TO_VVA(struct vring_desc *, vc_req->dev, vq,
1524 : : head->addr, &dlen, VHOST_ACCESS_RO);
1525 [ # # # # : 0 : if (unlikely(!src_desc || dlen != head->len)) {
# # # # ]
1526 : 0 : VC_LOG_ERR("Invalid descriptor");
1527 : 0 : return -1;
1528 : : }
1529 : : head = src_desc;
1530 : :
1531 : 0 : nb_descs = max_n_descs = dlen / sizeof(struct vring_desc);
1532 [ # # # # ]: 0 : if (unlikely(nb_descs > VHOST_CRYPTO_MAX_N_DESC || nb_descs == 0)) {
1533 : : err = VIRTIO_CRYPTO_ERR;
1534 : 0 : VC_LOG_ERR("Cannot process num of descriptors %u", nb_descs);
1535 [ # # # # ]: 0 : if (nb_descs > 0) {
1536 : : struct vring_desc *inhdr_desc = head;
1537 [ # # # # ]: 0 : while (inhdr_desc->flags & VRING_DESC_F_NEXT) {
1538 [ # # # # ]: 0 : if (inhdr_desc->next >= max_n_descs)
1539 : : return -1;
1540 : 0 : inhdr_desc = &head[inhdr_desc->next];
1541 : : }
1542 [ # # # # ]: 0 : if (inhdr_desc->len != sizeof(*inhdr))
1543 : : return -1;
1544 [ # # # # ]: 0 : inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req->dev,
1545 : : vq, inhdr_desc->addr, &dlen,
1546 : : VHOST_ACCESS_WO);
1547 [ # # # # : 0 : if (unlikely(!inhdr || dlen != inhdr_desc->len))
# # # # ]
1548 : : return -1;
1549 : 0 : inhdr->status = VIRTIO_CRYPTO_ERR;
1550 : 0 : return -1;
1551 : : }
1552 : : }
1553 : :
1554 : : /* copy descriptors to local variable */
1555 [ # # # # ]: 0 : for (i = 0; i < max_n_descs; i++) {
1556 : 0 : desc->addr = src_desc->addr;
1557 : 0 : desc->len = src_desc->len;
1558 : 0 : desc->flags = src_desc->flags;
1559 : 0 : desc++;
1560 [ # # # # ]: 0 : if (unlikely((src_desc->flags & VRING_DESC_F_NEXT) == 0))
1561 : : break;
1562 [ # # # # ]: 0 : if (unlikely(src_desc->next >= max_n_descs)) {
1563 : : err = VIRTIO_CRYPTO_BADMSG;
1564 : 0 : VC_LOG_ERR("Invalid descriptor");
1565 : 0 : goto error_exit;
1566 : : }
1567 : 0 : src_desc = &head[src_desc->next];
1568 : : }
1569 : :
1570 : 0 : vc_req->head = head;
1571 : 0 : vc_req->zero_copy = vcrypto->option;
1572 : :
1573 : : nb_descs = desc - descs;
1574 : : desc = descs;
1575 : :
1576 [ # # # # ]: 0 : if (unlikely(desc->len < sizeof(req))) {
1577 : : err = VIRTIO_CRYPTO_BADMSG;
1578 : 0 : VC_LOG_ERR("Invalid descriptor");
1579 : 0 : goto error_exit;
1580 : : }
1581 : :
1582 [ # # # # ]: 0 : if (unlikely(copy_data(&req, vcrypto->dev, vq, descs, &desc, sizeof(req),
1583 : : max_n_descs) < 0)) {
1584 : : err = VIRTIO_CRYPTO_BADMSG;
1585 : 0 : VC_LOG_ERR("Invalid descriptor");
1586 : 0 : goto error_exit;
1587 : : }
1588 : :
1589 : : /* desc is advanced by 1 now */
1590 : : max_n_descs -= 1;
1591 : :
1592 [ # # # # : 0 : switch (req.header.opcode) {
# # ]
1593 : 0 : case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1594 : : case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1595 : 0 : vc_req_out = rte_mbuf_to_priv(op->sym->m_src);
1596 : : memcpy(vc_req_out, vc_req, sizeof(struct vhost_crypto_data_req));
1597 : 0 : session_id = req.header.session_id;
1598 : :
1599 : 0 : err = rte_hash_lookup_data(vcrypto->session_map,
1600 : : &session_id, (void **)&vhost_session);
1601 [ # # # # ]: 0 : if (unlikely(err < 0)) {
1602 : : err = VIRTIO_CRYPTO_ERR;
1603 : 0 : VC_LOG_ERR("Failed to find session %"PRIu64,
1604 : : session_id);
1605 : 0 : goto error_exit;
1606 : : }
1607 : :
1608 : 0 : sym_session = vhost_session->sym;
1609 : 0 : op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1610 : :
1611 : 0 : err = rte_crypto_op_attach_sym_session(op, sym_session);
1612 [ # # # # ]: 0 : if (unlikely(err < 0)) {
1613 : : err = VIRTIO_CRYPTO_ERR;
1614 : 0 : VC_LOG_ERR("Failed to attach session to op");
1615 : 0 : goto error_exit;
1616 : : }
1617 : :
1618 [ # # # # : 0 : switch (req.u.sym_req.op_type) {
# # # # ]
1619 : 0 : case VIRTIO_CRYPTO_SYM_OP_NONE:
1620 : : err = VIRTIO_CRYPTO_NOTSUPP;
1621 : 0 : break;
1622 [ # # # # ]: 0 : case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1623 : 0 : err = prepare_sym_cipher_op(vcrypto, op, vq, vc_req_out,
1624 : : &req.u.sym_req.u.cipher, desc,
1625 : : max_n_descs);
1626 : 0 : break;
1627 [ # # # # ]: 0 : case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1628 : 0 : err = prepare_sym_chain_op(vcrypto, op, vq, vc_req_out,
1629 : : &req.u.sym_req.u.chain, desc,
1630 : : max_n_descs);
1631 : 0 : break;
1632 : : }
1633 [ # # # # ]: 0 : if (unlikely(err != 0)) {
1634 : 0 : VC_LOG_ERR("Failed to process sym request");
1635 : 0 : goto error_exit;
1636 : : }
1637 : : break;
1638 : 0 : case VIRTIO_CRYPTO_AKCIPHER_SIGN:
1639 : : case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
1640 : : case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
1641 : : case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
1642 : 0 : session_id = req.header.session_id;
1643 : :
1644 : 0 : err = rte_hash_lookup_data(vcrypto->session_map,
1645 : : &session_id, (void **)&vhost_session);
1646 [ # # # # ]: 0 : if (unlikely(err < 0)) {
1647 : : err = VIRTIO_CRYPTO_ERR;
1648 : 0 : VC_LOG_ERR("Failed to find asym session %"PRIu64,
1649 : : session_id);
1650 : 0 : goto error_exit;
1651 : : }
1652 : :
1653 : 0 : asym_session = vhost_session->asym;
1654 : 0 : op->type = RTE_CRYPTO_OP_TYPE_ASYMMETRIC;
1655 : :
1656 : 0 : err = rte_crypto_op_attach_asym_session(op, asym_session);
1657 [ # # # # ]: 0 : if (unlikely(err < 0)) {
1658 : : err = VIRTIO_CRYPTO_ERR;
1659 : 0 : VC_LOG_ERR("Failed to attach asym session to op");
1660 : 0 : goto error_exit;
1661 : : }
1662 : :
1663 : 0 : vc_req_out = rte_cryptodev_asym_session_get_user_data(asym_session);
1664 : : rte_memcpy(vc_req_out, vc_req, sizeof(struct vhost_crypto_data_req));
1665 : 0 : vc_req_out->wb = NULL;
1666 : :
1667 [ # # # # ]: 0 : switch (req.header.algo) {
1668 [ # # # # ]: 0 : case VIRTIO_CRYPTO_AKCIPHER_RSA:
1669 : 0 : err = prepare_asym_rsa_op(vcrypto, op, vq, vc_req_out,
1670 : : &req, desc, max_n_descs);
1671 : 0 : break;
1672 : : }
1673 [ # # # # ]: 0 : if (unlikely(err != 0)) {
1674 : 0 : VC_LOG_ERR("Failed to process asym request");
1675 : 0 : goto error_exit;
1676 : : }
1677 : :
1678 : : break;
1679 : 0 : default:
1680 : : err = VIRTIO_CRYPTO_ERR;
1681 : 0 : VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1682 : : req.header.opcode);
1683 : 0 : goto error_exit;
1684 : : }
1685 : :
1686 : : return 0;
1687 : :
1688 [ # # # # ]: 0 : error_exit:
1689 : :
1690 : : inhdr = reach_inhdr(vc_req->dev, vq, descs, max_n_descs);
1691 [ # # # # ]: 0 : if (likely(inhdr != NULL))
1692 : 0 : inhdr->status = (uint8_t)err;
1693 : :
1694 : : return -1;
1695 : : }
1696 : :
1697 : : static __rte_always_inline struct vhost_virtqueue *
1698 : : vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1699 : : struct vhost_virtqueue *old_vq)
1700 : : {
1701 : : struct rte_mbuf *m_src = NULL, *m_dst = NULL;
1702 : : struct vhost_crypto_data_req *vc_req;
1703 : : struct vhost_virtqueue *vq;
1704 : : uint16_t desc_idx;
1705 : :
1706 : 0 : if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
1707 : 0 : m_src = op->sym->m_src;
1708 [ # # # # ]: 0 : m_dst = op->sym->m_dst;
1709 : : vc_req = rte_mbuf_to_priv(m_src);
1710 [ # # # # ]: 0 : } else if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
1711 : 0 : vc_req = rte_cryptodev_asym_session_get_user_data(op->asym->session);
1712 : : } else {
1713 : 0 : VC_LOG_ERR("Invalid crypto op type");
1714 : 0 : return NULL;
1715 : : }
1716 : :
1717 [ # # # # ]: 0 : if (unlikely(!vc_req)) {
1718 : 0 : VC_LOG_ERR("Failed to retrieve vc_req");
1719 : 0 : return NULL;
1720 : : }
1721 : 0 : vq = vc_req->vq;
1722 : 0 : desc_idx = vc_req->desc_idx;
1723 : :
1724 [ # # ]: 0 : if (old_vq && (vq != old_vq))
1725 : : return vq;
1726 : :
1727 [ # # # # ]: 0 : if (unlikely(desc_idx >= vq->size)) {
1728 : 0 : VC_LOG_ERR("Invalid descriptor index %u", desc_idx);
1729 : 0 : return NULL;
1730 : : }
1731 : :
1732 [ # # # # ]: 0 : if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1733 : 0 : vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1734 : : else {
1735 [ # # # # ]: 0 : if (vc_req->zero_copy == 0)
1736 : 0 : write_back_data(vc_req);
1737 : : }
1738 : :
1739 : 0 : vq->used->ring[desc_idx].id = desc_idx;
1740 : 0 : vq->used->ring[desc_idx].len = vc_req->len;
1741 : :
1742 [ # # # # ]: 0 : if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
1743 [ # # # # ]: 0 : rte_mempool_put(m_src->pool, (void *)m_src);
1744 [ # # # # ]: 0 : if (m_dst)
1745 [ # # # # ]: 0 : rte_mempool_put(m_dst->pool, (void *)m_dst);
1746 : : }
1747 : :
1748 : : return vq;
1749 : : }
1750 : :
1751 : : static __rte_always_inline uint16_t
1752 : : vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1753 : : uint16_t nb_ops, int *callfd)
1754 : : {
1755 : : uint16_t processed = 1;
1756 : : struct vhost_virtqueue *vq, *tmp_vq;
1757 : :
1758 : : if (unlikely(nb_ops == 0))
1759 : : return 0;
1760 : :
1761 [ # # ]: 0 : vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1762 [ # # ]: 0 : if (unlikely(vq == NULL))
1763 : : return 0;
1764 : : tmp_vq = vq;
1765 : :
1766 [ # # ]: 0 : while ((processed < nb_ops)) {
1767 [ # # ]: 0 : tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1768 : : tmp_vq);
1769 : :
1770 [ # # ]: 0 : if (unlikely(vq != tmp_vq))
1771 : : break;
1772 : :
1773 : 0 : processed++;
1774 : : }
1775 : :
1776 : 0 : *callfd = vq->callfd;
1777 : :
1778 : 0 : *(volatile uint16_t *)&vq->used->idx += processed;
1779 : :
1780 : 0 : return processed;
1781 : : }
1782 : :
1783 : : RTE_EXPORT_SYMBOL(rte_vhost_crypto_driver_start)
1784 : : int
1785 : 0 : rte_vhost_crypto_driver_start(const char *path)
1786 : : {
1787 : : uint64_t protocol_features;
1788 : : int ret;
1789 : :
1790 : 0 : ret = rte_vhost_driver_set_features(path, VIRTIO_CRYPTO_FEATURES);
1791 [ # # ]: 0 : if (ret)
1792 : : return -1;
1793 : :
1794 : 0 : ret = rte_vhost_driver_get_protocol_features(path, &protocol_features);
1795 [ # # ]: 0 : if (ret)
1796 : : return -1;
1797 : 0 : protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_CONFIG);
1798 : 0 : ret = rte_vhost_driver_set_protocol_features(path, protocol_features);
1799 [ # # ]: 0 : if (ret)
1800 : : return -1;
1801 : :
1802 : 0 : return rte_vhost_driver_start(path);
1803 : : }
1804 : :
1805 : : RTE_EXPORT_SYMBOL(rte_vhost_crypto_create)
1806 : : int
1807 [ # # ]: 0 : rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1808 : : struct rte_mempool *sess_pool,
1809 : : int socket_id)
1810 : : {
1811 : : struct virtio_net *dev = get_device(vid);
1812 : 0 : struct rte_hash_parameters params = {0};
1813 : : struct vhost_crypto *vcrypto;
1814 : : char name[128];
1815 : : int ret;
1816 : :
1817 [ # # ]: 0 : if (!dev) {
1818 : 0 : VC_LOG_ERR("Invalid vid %i", vid);
1819 : 0 : return -EINVAL;
1820 : : }
1821 : :
1822 : 0 : vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1823 : : RTE_CACHE_LINE_SIZE, socket_id);
1824 [ # # ]: 0 : if (!vcrypto) {
1825 : 0 : VC_LOG_ERR("Insufficient memory");
1826 : 0 : return -ENOMEM;
1827 : : }
1828 : :
1829 : 0 : vcrypto->sess_pool = sess_pool;
1830 : 0 : vcrypto->cid = cryptodev_id;
1831 : 0 : vcrypto->last_session_id = 1;
1832 : 0 : vcrypto->dev = dev;
1833 : 0 : vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1834 : :
1835 : : snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1836 : 0 : params.name = name;
1837 : 0 : params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1838 : 0 : params.hash_func = rte_jhash;
1839 : 0 : params.key_len = sizeof(uint64_t);
1840 : 0 : params.socket_id = socket_id;
1841 : 0 : vcrypto->session_map = rte_hash_create(¶ms);
1842 [ # # ]: 0 : if (!vcrypto->session_map) {
1843 : 0 : VC_LOG_ERR("Failed to creath session map");
1844 : : ret = -ENOMEM;
1845 : 0 : goto error_exit;
1846 : : }
1847 : :
1848 : : snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1849 : 0 : vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1850 : : VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1851 : : sizeof(struct vhost_crypto_data_req),
1852 : : VHOST_CRYPTO_MAX_DATA_SIZE + RTE_PKTMBUF_HEADROOM,
1853 : 0 : rte_socket_id());
1854 [ # # ]: 0 : if (!vcrypto->mbuf_pool) {
1855 : 0 : VC_LOG_ERR("Failed to creath mbuf pool");
1856 : : ret = -ENOMEM;
1857 : 0 : goto error_exit;
1858 : : }
1859 : :
1860 : : snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1861 : 0 : vcrypto->wb_pool = rte_mempool_create(name,
1862 : : VHOST_CRYPTO_MBUF_POOL_SIZE,
1863 : : sizeof(struct vhost_crypto_writeback_data),
1864 : : 128, 0, NULL, NULL, init_wb_data, NULL,
1865 : 0 : rte_socket_id(), 0);
1866 [ # # ]: 0 : if (!vcrypto->wb_pool) {
1867 : 0 : VC_LOG_ERR("Failed to creath mempool");
1868 : : ret = -ENOMEM;
1869 : 0 : goto error_exit;
1870 : : }
1871 : :
1872 : 0 : dev->extern_data = vcrypto;
1873 : 0 : dev->extern_ops.pre_msg_handle = NULL;
1874 : 0 : dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1875 : :
1876 : 0 : return 0;
1877 : :
1878 : 0 : error_exit:
1879 : 0 : rte_hash_free(vcrypto->session_map);
1880 : 0 : rte_mempool_free(vcrypto->mbuf_pool);
1881 : :
1882 : 0 : rte_free(vcrypto);
1883 : :
1884 : 0 : return ret;
1885 : : }
1886 : :
1887 : : RTE_EXPORT_SYMBOL(rte_vhost_crypto_free)
1888 : : int
1889 [ # # ]: 0 : rte_vhost_crypto_free(int vid)
1890 : : {
1891 : : struct virtio_net *dev = get_device(vid);
1892 : : struct vhost_crypto *vcrypto;
1893 : :
1894 [ # # ]: 0 : if (unlikely(dev == NULL)) {
1895 : 0 : VC_LOG_ERR("Invalid vid %i", vid);
1896 : 0 : return -EINVAL;
1897 : : }
1898 : :
1899 : 0 : vcrypto = dev->extern_data;
1900 [ # # ]: 0 : if (unlikely(vcrypto == NULL)) {
1901 : 0 : VC_LOG_ERR("Cannot find required data, is it initialized?");
1902 : 0 : return -ENOENT;
1903 : : }
1904 : :
1905 : 0 : rte_hash_free(vcrypto->session_map);
1906 : 0 : rte_mempool_free(vcrypto->mbuf_pool);
1907 : 0 : rte_mempool_free(vcrypto->wb_pool);
1908 : 0 : rte_free(vcrypto);
1909 : :
1910 : 0 : dev->extern_data = NULL;
1911 : 0 : dev->extern_ops.pre_msg_handle = NULL;
1912 : 0 : dev->extern_ops.post_msg_handle = NULL;
1913 : :
1914 : 0 : return 0;
1915 : : }
1916 : :
1917 : : RTE_EXPORT_SYMBOL(rte_vhost_crypto_set_zero_copy)
1918 : : int
1919 [ # # ]: 0 : rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1920 : : {
1921 : : struct virtio_net *dev = get_device(vid);
1922 : : struct vhost_crypto *vcrypto;
1923 : :
1924 [ # # ]: 0 : if (unlikely(dev == NULL)) {
1925 : 0 : VC_LOG_ERR("Invalid vid %i", vid);
1926 : 0 : return -EINVAL;
1927 : : }
1928 : :
1929 [ # # ]: 0 : if (unlikely((uint32_t)option >=
1930 : : RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1931 : 0 : VC_LOG_ERR("Invalid option %i", option);
1932 : 0 : return -EINVAL;
1933 : : }
1934 : :
1935 : 0 : vcrypto = (struct vhost_crypto *)dev->extern_data;
1936 [ # # ]: 0 : if (unlikely(vcrypto == NULL)) {
1937 : 0 : VC_LOG_ERR("Cannot find required data, is it initialized?");
1938 : 0 : return -ENOENT;
1939 : : }
1940 : :
1941 [ # # ]: 0 : if (vcrypto->option == (uint8_t)option)
1942 : : return 0;
1943 : :
1944 [ # # # # ]: 0 : if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1945 : 0 : !(rte_mempool_full(vcrypto->wb_pool))) {
1946 : 0 : VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1947 : 0 : return -EINVAL;
1948 : : }
1949 : :
1950 [ # # ]: 0 : if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1951 : : char name[128];
1952 : :
1953 : : snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1954 : 0 : vcrypto->wb_pool = rte_mempool_create(name,
1955 : : VHOST_CRYPTO_MBUF_POOL_SIZE,
1956 : : sizeof(struct vhost_crypto_writeback_data),
1957 : : 128, 0, NULL, NULL, init_wb_data, NULL,
1958 : 0 : rte_socket_id(), 0);
1959 [ # # ]: 0 : if (!vcrypto->wb_pool) {
1960 : 0 : VC_LOG_ERR("Failed to creath mbuf pool");
1961 : 0 : return -ENOMEM;
1962 : : }
1963 : : } else {
1964 : 0 : rte_mempool_free(vcrypto->wb_pool);
1965 : 0 : vcrypto->wb_pool = NULL;
1966 : : }
1967 : :
1968 : 0 : vcrypto->option = (uint8_t)option;
1969 : :
1970 : 0 : return 0;
1971 : : }
1972 : :
1973 : : RTE_EXPORT_SYMBOL(rte_vhost_crypto_fetch_requests)
1974 : : uint16_t
1975 [ # # ]: 0 : rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1976 : : struct rte_crypto_op **ops, uint16_t nb_ops)
1977 : : {
1978 : : struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1979 : : struct vhost_crypto_desc descs[VHOST_CRYPTO_MAX_N_DESC];
1980 : : struct virtio_net *dev = get_device(vid);
1981 : : struct vhost_crypto *vcrypto;
1982 : : struct vhost_virtqueue *vq;
1983 : : uint16_t avail_idx;
1984 : : uint16_t start_idx;
1985 : : uint16_t count;
1986 : : uint16_t i = 0;
1987 : :
1988 [ # # ]: 0 : if (unlikely(dev == NULL)) {
1989 : 0 : VC_LOG_ERR("Invalid vid %i", vid);
1990 : 0 : return 0;
1991 : : }
1992 : :
1993 [ # # ]: 0 : if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1994 : 0 : VC_LOG_ERR("Invalid qid %u", qid);
1995 : 0 : return 0;
1996 : : }
1997 : :
1998 : 0 : vcrypto = (struct vhost_crypto *)dev->extern_data;
1999 [ # # ]: 0 : if (unlikely(vcrypto == NULL)) {
2000 : 0 : VC_LOG_ERR("Cannot find required data, is it initialized?");
2001 : 0 : return 0;
2002 : : }
2003 : :
2004 : 0 : vq = dev->virtqueue[qid];
2005 : :
2006 [ # # ]: 0 : if (unlikely(vq == NULL)) {
2007 : 0 : VC_LOG_ERR("Invalid virtqueue %u", qid);
2008 : 0 : return 0;
2009 : : }
2010 : :
2011 [ # # ]: 0 : if (unlikely(rte_rwlock_read_trylock(&vq->access_lock) != 0))
2012 : : return 0;
2013 : :
2014 : : vhost_user_iotlb_rd_lock(vq);
2015 [ # # ]: 0 : if (unlikely(!vq->access_ok)) {
2016 : : VC_LOG_DBG("Virtqueue %u vrings not yet initialized", qid);
2017 : 0 : goto out_unlock;
2018 : : }
2019 : :
2020 : 0 : avail_idx = *((volatile uint16_t *)&vq->avail->idx);
2021 : 0 : start_idx = vq->last_used_idx;
2022 : 0 : count = avail_idx - start_idx;
2023 : 0 : count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
2024 : 0 : count = RTE_MIN(count, nb_ops);
2025 : :
2026 [ # # ]: 0 : if (unlikely(count == 0))
2027 : 0 : goto out_unlock;
2028 : :
2029 : : /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
2030 : : * we need only 1 mbuf as src and dst
2031 : : */
2032 [ # # # ]: 0 : switch (vcrypto->option) {
2033 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
2034 [ # # # # ]: 0 : if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
2035 : : (void **)mbufs, count * 2) < 0)) {
2036 : 0 : VC_LOG_ERR("Insufficient memory");
2037 : 0 : goto out_unlock;
2038 : : }
2039 : :
2040 [ # # ]: 0 : for (i = 0; i < count; i++) {
2041 : 0 : uint16_t used_idx = (start_idx + i) & (vq->size - 1);
2042 : 0 : uint16_t desc_idx = vq->avail->ring[used_idx];
2043 : : struct vring_desc *head;
2044 : 0 : struct rte_crypto_op *op = ops[i];
2045 : :
2046 [ # # ]: 0 : if (unlikely(desc_idx >= vq->size))
2047 : : break;
2048 : :
2049 : 0 : head = &vq->desc[desc_idx];
2050 : 0 : op->sym->m_src = mbufs[i * 2];
2051 : 0 : op->sym->m_dst = mbufs[i * 2 + 1];
2052 : 0 : op->sym->m_src->data_off = 0;
2053 [ # # ]: 0 : op->sym->m_dst->data_off = 0;
2054 : :
2055 [ # # ]: 0 : if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
2056 : : op, head, descs, desc_idx) < 0))
2057 : : break;
2058 : : }
2059 : :
2060 [ # # ]: 0 : if (unlikely(i < count))
2061 : 0 : rte_mempool_put_bulk(vcrypto->mbuf_pool,
2062 : 0 : (void **)&mbufs[i * 2],
2063 [ # # ]: 0 : (count - i) * 2);
2064 : :
2065 : : break;
2066 : :
2067 : 0 : case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
2068 [ # # # # ]: 0 : if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
2069 : : (void **)mbufs, count) < 0)) {
2070 : 0 : VC_LOG_ERR("Insufficient memory");
2071 : 0 : goto out_unlock;
2072 : : }
2073 : :
2074 [ # # ]: 0 : for (i = 0; i < count; i++) {
2075 : 0 : uint16_t used_idx = (start_idx + i) & (vq->size - 1);
2076 : 0 : uint16_t desc_idx = vq->avail->ring[used_idx];
2077 : : struct vring_desc *head;
2078 : 0 : struct rte_crypto_op *op = ops[i];
2079 : :
2080 [ # # ]: 0 : if (unlikely(desc_idx >= vq->size))
2081 : : break;
2082 : :
2083 : 0 : head = &vq->desc[desc_idx];
2084 : 0 : op->sym->m_src = mbufs[i];
2085 : 0 : op->sym->m_dst = NULL;
2086 [ # # ]: 0 : op->sym->m_src->data_off = 0;
2087 : :
2088 [ # # ]: 0 : if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
2089 : : op, head, descs, desc_idx) < 0))
2090 : : break;
2091 : : }
2092 : :
2093 [ # # ]: 0 : if (unlikely(i < count))
2094 : 0 : rte_mempool_put_bulk(vcrypto->mbuf_pool,
2095 : 0 : (void **)&mbufs[i],
2096 [ # # ]: 0 : count - i);
2097 : :
2098 : : break;
2099 : :
2100 : : }
2101 : :
2102 : 0 : vq->last_used_idx += i;
2103 : :
2104 : 0 : out_unlock:
2105 : : vhost_user_iotlb_rd_unlock(vq);
2106 : 0 : rte_rwlock_read_unlock(&vq->access_lock);
2107 : :
2108 : 0 : return i;
2109 : : }
2110 : :
2111 : : RTE_EXPORT_SYMBOL(rte_vhost_crypto_finalize_requests)
2112 : : uint16_t
2113 : 0 : rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
2114 : : uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
2115 : : {
2116 : : struct rte_crypto_op **tmp_ops = ops;
2117 : : uint16_t count = 0, left = nb_ops;
2118 : : int callfd;
2119 : : uint16_t idx = 0;
2120 : :
2121 [ # # ]: 0 : while (left) {
2122 : : count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
2123 : : &callfd);
2124 [ # # ]: 0 : if (unlikely(count == 0))
2125 : : break;
2126 : :
2127 : 0 : tmp_ops = &tmp_ops[count];
2128 : 0 : left -= count;
2129 : :
2130 : 0 : callfds[idx++] = callfd;
2131 : :
2132 [ # # ]: 0 : if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
2133 : 0 : VC_LOG_ERR("Too many vqs");
2134 : 0 : break;
2135 : : }
2136 : : }
2137 : :
2138 : 0 : *nb_callfds = idx;
2139 : :
2140 : 0 : return nb_ops - left;
2141 : : }
|