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