master
c 1,324 lines 43 KB
Raw
1 /*
2 * Virtio crypto Support
3 *
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 *
6 * Authors:
7 * Gonglei <arei.gonglei@huawei.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * (at your option) any later version. See the COPYING file in the
11 * top-level directory.
12 */
13
14 #include "qemu/osdep.h"
15 #include "qemu/iov.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/module.h"
18 #include "qapi/error.h"
19 #include "qemu/error-report.h"
20
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-crypto.h"
23 #include "hw/core/qdev-properties.h"
24 #include "standard-headers/linux/virtio_ids.h"
25 #include "system/cryptodev-vhost.h"
26
27 #define VIRTIO_CRYPTO_VM_VERSION 1
28
29 typedef struct VirtIOCryptoSessionReq {
30 VirtIODevice *vdev;
31 VirtQueue *vq;
32 VirtQueueElement *elem;
33 CryptoDevBackendSessionInfo info;
34 CryptoDevCompletionFunc cb;
35 } VirtIOCryptoSessionReq;
36
37 static void virtio_crypto_free_create_session_req(VirtIOCryptoSessionReq *sreq)
38 {
39 switch (sreq->info.op_code) {
40 case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION:
41 g_free(sreq->info.u.sym_sess_info.cipher_key);
42 g_free(sreq->info.u.sym_sess_info.auth_key);
43 break;
44
45 case VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION:
46 g_free(sreq->info.u.asym_sess_info.key);
47 break;
48
49 case VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION:
50 case VIRTIO_CRYPTO_HASH_DESTROY_SESSION:
51 case VIRTIO_CRYPTO_MAC_DESTROY_SESSION:
52 case VIRTIO_CRYPTO_AEAD_DESTROY_SESSION:
53 case VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION:
54 break;
55
56 default:
57 error_report("Unknown opcode: %u", sreq->info.op_code);
58 }
59 g_free(sreq);
60 }
61
62 /*
63 * Transfer virtqueue index to crypto queue index.
64 * The control virtqueue is after the data virtqueues
65 * so the input value doesn't need to be adjusted
66 */
67 static inline int virtio_crypto_vq2q(int queue_index)
68 {
69 return queue_index;
70 }
71
72 static int
73 virtio_crypto_cipher_session_helper(VirtIODevice *vdev,
74 CryptoDevBackendSymSessionInfo *info,
75 struct virtio_crypto_cipher_session_para *cipher_para,
76 struct iovec **iov, unsigned int *out_num)
77 {
78 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
79 unsigned int num = *out_num;
80
81 info->cipher_alg = ldl_le_p(&cipher_para->algo);
82 info->key_len = ldl_le_p(&cipher_para->keylen);
83 info->direction = ldl_le_p(&cipher_para->op);
84 DPRINTF("cipher_alg=%" PRIu32 ", info->direction=%" PRIu32 "\n",
85 info->cipher_alg, info->direction);
86
87 if (info->key_len > vcrypto->conf.max_cipher_key_len) {
88 error_report("virtio-crypto length of cipher key is too big: %u",
89 info->key_len);
90 return -VIRTIO_CRYPTO_ERR;
91 }
92 /* Get cipher key */
93 if (info->key_len > 0) {
94 size_t s;
95 DPRINTF("keylen=%" PRIu32 "\n", info->key_len);
96
97 info->cipher_key = g_malloc(info->key_len);
98 s = iov_to_buf(*iov, num, 0, info->cipher_key, info->key_len);
99 if (unlikely(s != info->key_len)) {
100 virtio_error(vdev, "virtio-crypto cipher key incorrect");
101 return -EFAULT;
102 }
103 iov_discard_front(iov, &num, info->key_len);
104 *out_num = num;
105 }
106
107 return 0;
108 }
109
110 static int
111 virtio_crypto_create_sym_session(VirtIOCrypto *vcrypto,
112 struct virtio_crypto_sym_create_session_req *sess_req,
113 uint32_t queue_id,
114 uint32_t opcode,
115 struct iovec *iov, unsigned int out_num,
116 VirtIOCryptoSessionReq *sreq)
117 {
118 VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
119 CryptoDevBackendSymSessionInfo *sym_info = &sreq->info.u.sym_sess_info;
120 int queue_index;
121 uint32_t op_type;
122 int ret;
123
124 op_type = ldl_le_p(&sess_req->op_type);
125 sreq->info.op_code = opcode;
126
127 sym_info = &sreq->info.u.sym_sess_info;
128 sym_info->op_type = op_type;
129
130 if (op_type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
131 ret = virtio_crypto_cipher_session_helper(vdev, sym_info,
132 &sess_req->u.cipher.para,
133 &iov, &out_num);
134 if (ret < 0) {
135 return ret;
136 }
137 } else if (op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
138 size_t s;
139 /* cipher part */
140 ret = virtio_crypto_cipher_session_helper(vdev, sym_info,
141 &sess_req->u.chain.para.cipher_param,
142 &iov, &out_num);
143 if (ret < 0) {
144 return ret;
145 }
146 /* hash part */
147 sym_info->alg_chain_order = ldl_le_p(
148 &sess_req->u.chain.para.alg_chain_order);
149 sym_info->add_len = ldl_le_p(&sess_req->u.chain.para.aad_len);
150 sym_info->hash_mode = ldl_le_p(&sess_req->u.chain.para.hash_mode);
151 if (sym_info->hash_mode == VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH) {
152 sym_info->hash_alg =
153 ldl_le_p(&sess_req->u.chain.para.u.mac_param.algo);
154 sym_info->auth_key_len = ldl_le_p(
155 &sess_req->u.chain.para.u.mac_param.auth_key_len);
156 sym_info->hash_result_len = ldl_le_p(
157 &sess_req->u.chain.para.u.mac_param.hash_result_len);
158 if (sym_info->auth_key_len > vcrypto->conf.max_auth_key_len) {
159 error_report("virtio-crypto length of auth key is too big: %u",
160 sym_info->auth_key_len);
161 return -VIRTIO_CRYPTO_ERR;
162 }
163 /* get auth key */
164 if (sym_info->auth_key_len > 0) {
165 sym_info->auth_key = g_malloc(sym_info->auth_key_len);
166 s = iov_to_buf(iov, out_num, 0, sym_info->auth_key,
167 sym_info->auth_key_len);
168 if (unlikely(s != sym_info->auth_key_len)) {
169 virtio_error(vdev,
170 "virtio-crypto authenticated key incorrect");
171 return -EFAULT;
172 }
173 iov_discard_front(&iov, &out_num, sym_info->auth_key_len);
174 }
175 } else if (sym_info->hash_mode == VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN) {
176 sym_info->hash_alg = ldl_le_p(
177 &sess_req->u.chain.para.u.hash_param.algo);
178 sym_info->hash_result_len = ldl_le_p(
179 &sess_req->u.chain.para.u.hash_param.hash_result_len);
180 } else {
181 /* VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED */
182 error_report("unsupported hash mode");
183 return -VIRTIO_CRYPTO_NOTSUPP;
184 }
185 } else {
186 /* VIRTIO_CRYPTO_SYM_OP_NONE */
187 error_report("unsupported cipher op_type: VIRTIO_CRYPTO_SYM_OP_NONE");
188 return -VIRTIO_CRYPTO_NOTSUPP;
189 }
190
191 queue_index = virtio_crypto_vq2q(queue_id);
192 return cryptodev_backend_create_session(vcrypto->cryptodev, &sreq->info,
193 queue_index, sreq->cb, sreq);
194 }
195
196 static int
197 virtio_crypto_create_asym_session(VirtIOCrypto *vcrypto,
198 struct virtio_crypto_akcipher_create_session_req *sess_req,
199 uint32_t queue_id, uint32_t opcode,
200 struct iovec *iov, unsigned int out_num,
201 VirtIOCryptoSessionReq *sreq)
202 {
203 VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
204 CryptoDevBackendAsymSessionInfo *asym_info = &sreq->info.u.asym_sess_info;
205 int queue_index;
206 uint32_t algo, keytype, keylen;
207
208 sreq->info.op_code = opcode;
209 algo = ldl_le_p(&sess_req->para.algo);
210 keytype = ldl_le_p(&sess_req->para.keytype);
211 keylen = ldl_le_p(&sess_req->para.keylen);
212
213 if ((keytype != VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC)
214 && (keytype != VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE)) {
215 error_report("unsupported asym keytype: %d", keytype);
216 return -VIRTIO_CRYPTO_NOTSUPP;
217 }
218
219 if (unlikely(keylen > vcrypto->conf.max_size)) {
220 error_report("virtio-crypto length of akcipher key is too large: %u",
221 keylen);
222 return -VIRTIO_CRYPTO_ERR;
223 }
224
225 if (keylen) {
226 asym_info->key = g_malloc(keylen);
227 if (iov_to_buf(iov, out_num, 0, asym_info->key, keylen) != keylen) {
228 virtio_error(vdev, "virtio-crypto asym key incorrect");
229 return -EFAULT;
230 }
231 iov_discard_front(&iov, &out_num, keylen);
232 }
233
234 asym_info = &sreq->info.u.asym_sess_info;
235 asym_info->algo = algo;
236 asym_info->keytype = keytype;
237 asym_info->keylen = keylen;
238 switch (asym_info->algo) {
239 case VIRTIO_CRYPTO_AKCIPHER_RSA:
240 asym_info->u.rsa.padding_algo =
241 ldl_le_p(&sess_req->para.u.rsa.padding_algo);
242 asym_info->u.rsa.hash_algo =
243 ldl_le_p(&sess_req->para.u.rsa.hash_algo);
244 break;
245
246 /* TODO DSA&ECDSA handling */
247
248 default:
249 return -VIRTIO_CRYPTO_ERR;
250 }
251
252 queue_index = virtio_crypto_vq2q(queue_id);
253 return cryptodev_backend_create_session(vcrypto->cryptodev, &sreq->info,
254 queue_index, sreq->cb, sreq);
255 }
256
257 static int
258 virtio_crypto_handle_close_session(VirtIOCrypto *vcrypto,
259 struct virtio_crypto_destroy_session_req *close_sess_req,
260 uint32_t queue_id,
261 VirtIOCryptoSessionReq *sreq)
262 {
263 uint64_t session_id;
264
265 session_id = ldq_le_p(&close_sess_req->session_id);
266 DPRINTF("close session, id=%" PRIu64 "\n", session_id);
267
268 return cryptodev_backend_close_session(
269 vcrypto->cryptodev, session_id, queue_id, sreq->cb, sreq);
270 }
271
272 static void virtio_crypto_create_session_completion(void *opaque, int ret)
273 {
274 VirtIOCryptoSessionReq *sreq = (VirtIOCryptoSessionReq *)opaque;
275 VirtQueue *vq = sreq->vq;
276 VirtQueueElement *elem = sreq->elem;
277 VirtIODevice *vdev = sreq->vdev;
278 struct virtio_crypto_session_input input;
279 struct iovec *in_iov = elem->in_sg;
280 unsigned in_num = elem->in_num;
281 size_t s;
282
283 memset(&input, 0, sizeof(input));
284 /* Serious errors, need to reset virtio crypto device */
285 if (ret == -EFAULT) {
286 virtqueue_detach_element(vq, elem, 0);
287 goto out;
288 } else if (ret == -VIRTIO_CRYPTO_NOTSUPP) {
289 stl_le_p(&input.status, VIRTIO_CRYPTO_NOTSUPP);
290 } else if (ret == -VIRTIO_CRYPTO_KEY_REJECTED) {
291 stl_le_p(&input.status, VIRTIO_CRYPTO_KEY_REJECTED);
292 } else if (ret != VIRTIO_CRYPTO_OK) {
293 stl_le_p(&input.status, VIRTIO_CRYPTO_ERR);
294 } else {
295 /* Set the session id */
296 stq_le_p(&input.session_id, sreq->info.session_id);
297 stl_le_p(&input.status, VIRTIO_CRYPTO_OK);
298 }
299
300 s = iov_from_buf(in_iov, in_num, 0, &input, sizeof(input));
301 if (unlikely(s != sizeof(input))) {
302 virtio_error(vdev, "virtio-crypto input incorrect");
303 virtqueue_detach_element(vq, elem, 0);
304 goto out;
305 }
306 virtqueue_push(vq, elem, sizeof(input));
307 virtio_notify(vdev, vq);
308
309 out:
310 g_free(elem);
311 virtio_crypto_free_create_session_req(sreq);
312 }
313
314 static void virtio_crypto_destroy_session_completion(void *opaque, int ret)
315 {
316 VirtIOCryptoSessionReq *sreq = (VirtIOCryptoSessionReq *)opaque;
317 VirtQueue *vq = sreq->vq;
318 VirtQueueElement *elem = sreq->elem;
319 VirtIODevice *vdev = sreq->vdev;
320 struct iovec *in_iov = elem->in_sg;
321 unsigned in_num = elem->in_num;
322 uint8_t status;
323 size_t s;
324
325 if (ret < 0) {
326 status = VIRTIO_CRYPTO_ERR;
327 } else {
328 status = VIRTIO_CRYPTO_OK;
329 }
330 s = iov_from_buf(in_iov, in_num, 0, &status, sizeof(status));
331 if (unlikely(s != sizeof(status))) {
332 virtio_error(vdev, "virtio-crypto status incorrect");
333 virtqueue_detach_element(vq, elem, 0);
334 goto out;
335 }
336 virtqueue_push(vq, elem, sizeof(status));
337 virtio_notify(vdev, vq);
338
339 out:
340 g_free(elem);
341 g_free(sreq);
342 }
343
344 static void virtio_crypto_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
345 {
346 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
347 struct virtio_crypto_op_ctrl_req ctrl;
348 VirtQueueElement *elem;
349 VirtIOCryptoSessionReq *sreq;
350 unsigned out_num;
351 unsigned in_num;
352 uint32_t queue_id;
353 uint32_t opcode;
354 struct virtio_crypto_session_input input;
355 size_t s;
356 int ret;
357 struct iovec *out_iov;
358 struct iovec *in_iov;
359
360 for (;;) {
361 g_autofree struct iovec *out_iov_copy = NULL;
362
363 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
364 if (!elem) {
365 break;
366 }
367 if (elem->out_num < 1 || elem->in_num < 1) {
368 virtio_error(vdev, "virtio-crypto ctrl missing headers");
369 virtqueue_detach_element(vq, elem, 0);
370 g_free(elem);
371 break;
372 }
373
374 out_num = elem->out_num;
375 out_iov_copy = g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num);
376 out_iov = out_iov_copy;
377
378 in_num = elem->in_num;
379 in_iov = elem->in_sg;
380
381 if (unlikely(iov_to_buf(out_iov, out_num, 0, &ctrl, sizeof(ctrl))
382 != sizeof(ctrl))) {
383 virtio_error(vdev, "virtio-crypto request ctrl_hdr too short");
384 virtqueue_detach_element(vq, elem, 0);
385 g_free(elem);
386 break;
387 }
388 iov_discard_front(&out_iov, &out_num, sizeof(ctrl));
389
390 opcode = ldl_le_p(&ctrl.header.opcode);
391 queue_id = ldl_le_p(&ctrl.header.queue_id);
392
393 sreq = g_new0(VirtIOCryptoSessionReq, 1);
394 sreq->vdev = vdev;
395 sreq->vq = vq;
396 sreq->elem = elem;
397
398 switch (opcode) {
399 case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION:
400 sreq->cb = virtio_crypto_create_session_completion;
401 ret = virtio_crypto_create_sym_session(vcrypto,
402 &ctrl.u.sym_create_session,
403 queue_id, opcode,
404 out_iov, out_num,
405 sreq);
406 if (ret < 0) {
407 virtio_crypto_create_session_completion(sreq, ret);
408 }
409 break;
410
411 case VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION:
412 sreq->cb = virtio_crypto_create_session_completion;
413 ret = virtio_crypto_create_asym_session(vcrypto,
414 &ctrl.u.akcipher_create_session,
415 queue_id, opcode,
416 out_iov, out_num,
417 sreq);
418 if (ret < 0) {
419 virtio_crypto_create_session_completion(sreq, ret);
420 }
421 break;
422
423 case VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION:
424 case VIRTIO_CRYPTO_HASH_DESTROY_SESSION:
425 case VIRTIO_CRYPTO_MAC_DESTROY_SESSION:
426 case VIRTIO_CRYPTO_AEAD_DESTROY_SESSION:
427 case VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION:
428 sreq->cb = virtio_crypto_destroy_session_completion;
429 ret = virtio_crypto_handle_close_session(vcrypto,
430 &ctrl.u.destroy_session, queue_id,
431 sreq);
432 if (ret < 0) {
433 virtio_crypto_destroy_session_completion(sreq, ret);
434 }
435 break;
436
437 case VIRTIO_CRYPTO_HASH_CREATE_SESSION:
438 case VIRTIO_CRYPTO_MAC_CREATE_SESSION:
439 case VIRTIO_CRYPTO_AEAD_CREATE_SESSION:
440 default:
441 memset(&input, 0, sizeof(input));
442 error_report("virtio-crypto unsupported ctrl opcode: %d", opcode);
443 stl_le_p(&input.status, VIRTIO_CRYPTO_NOTSUPP);
444 s = iov_from_buf(in_iov, in_num, 0, &input, sizeof(input));
445 if (unlikely(s != sizeof(input))) {
446 virtio_error(vdev, "virtio-crypto input incorrect");
447 virtqueue_detach_element(vq, elem, 0);
448 } else {
449 virtqueue_push(vq, elem, sizeof(input));
450 virtio_notify(vdev, vq);
451 }
452 g_free(sreq);
453 g_free(elem);
454
455 break;
456 } /* end switch case */
457
458 } /* end for loop */
459 }
460
461 static void virtio_crypto_init_request(VirtIOCrypto *vcrypto, VirtQueue *vq,
462 VirtIOCryptoReq *req)
463 {
464 req->vcrypto = vcrypto;
465 req->vq = vq;
466 req->in = NULL;
467 req->in_iov = NULL;
468 req->in_num = 0;
469 req->in_len = 0;
470 req->flags = QCRYPTODEV_BACKEND_ALGO_TYPE__MAX;
471 memset(&req->op_info, 0x00, sizeof(req->op_info));
472 }
473
474 static void virtio_crypto_free_request(VirtIOCryptoReq *req)
475 {
476 if (!req) {
477 return;
478 }
479
480 if (req->flags == QCRYPTODEV_BACKEND_ALGO_TYPE_SYM) {
481 size_t max_len;
482 CryptoDevBackendSymOpInfo *op_info = req->op_info.u.sym_op_info;
483
484 if (op_info) {
485 max_len = op_info->iv_len +
486 op_info->aad_len +
487 op_info->src_len +
488 op_info->dst_len +
489 op_info->digest_result_len;
490
491 /* Zeroize and free request data structure */
492 memset(op_info, 0, sizeof(*op_info) + max_len);
493 g_free(op_info);
494 }
495 } else if (req->flags == QCRYPTODEV_BACKEND_ALGO_TYPE_ASYM) {
496 CryptoDevBackendAsymOpInfo *op_info = req->op_info.u.asym_op_info;
497 if (op_info) {
498 g_free(op_info->src);
499 g_free(op_info->dst);
500 memset(op_info, 0, sizeof(*op_info));
501 g_free(op_info);
502 }
503 }
504
505 g_free(req->in_iov);
506 g_free(req);
507 }
508
509 static void
510 virtio_crypto_sym_input_data_helper(VirtIODevice *vdev,
511 VirtIOCryptoReq *req,
512 uint32_t status,
513 CryptoDevBackendSymOpInfo *sym_op_info)
514 {
515 size_t s, len;
516 struct iovec *in_iov = req->in_iov;
517
518 if (status != VIRTIO_CRYPTO_OK) {
519 return;
520 }
521
522 len = sym_op_info->src_len;
523 /* Save the cipher result */
524 s = iov_from_buf(in_iov, req->in_num, 0, sym_op_info->dst, len);
525 if (s != len) {
526 virtio_error(vdev, "virtio-crypto dest data incorrect");
527 return;
528 }
529
530 iov_discard_front(&in_iov, &req->in_num, len);
531
532 if (sym_op_info->op_type ==
533 VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
534 /* Save the digest result */
535 s = iov_from_buf(in_iov, req->in_num, 0,
536 sym_op_info->digest_result,
537 sym_op_info->digest_result_len);
538 if (s != sym_op_info->digest_result_len) {
539 virtio_error(vdev, "virtio-crypto digest result incorrect");
540 }
541 }
542 }
543
544 static void
545 virtio_crypto_akcipher_input_data_helper(VirtIODevice *vdev,
546 VirtIOCryptoReq *req, int32_t status,
547 CryptoDevBackendAsymOpInfo *asym_op_info)
548 {
549 size_t s, len;
550 struct iovec *in_iov = req->in_iov;
551
552 if (status != VIRTIO_CRYPTO_OK) {
553 return;
554 }
555
556 len = asym_op_info->dst_len;
557 if (!len) {
558 return;
559 }
560
561 s = iov_from_buf(in_iov, req->in_num, 0, asym_op_info->dst, len);
562 if (s != len) {
563 virtio_error(vdev, "virtio-crypto asym dest data incorrect");
564 return;
565 }
566
567 iov_discard_front(&in_iov, &req->in_num, len);
568
569 /* For akcipher, dst_len may be changed after operation */
570 req->in_len = sizeof(struct virtio_crypto_inhdr) + asym_op_info->dst_len;
571 }
572
573 static void virtio_crypto_req_complete(void *opaque, int ret)
574 {
575 VirtIOCryptoReq *req = (VirtIOCryptoReq *)opaque;
576 VirtIOCrypto *vcrypto = req->vcrypto;
577 VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
578 uint8_t status = -ret;
579
580 if (req->flags == QCRYPTODEV_BACKEND_ALGO_TYPE_SYM) {
581 virtio_crypto_sym_input_data_helper(vdev, req, status,
582 req->op_info.u.sym_op_info);
583 } else if (req->flags == QCRYPTODEV_BACKEND_ALGO_TYPE_ASYM) {
584 virtio_crypto_akcipher_input_data_helper(vdev, req, status,
585 req->op_info.u.asym_op_info);
586 }
587 stb_p(&req->in->status, status);
588 virtqueue_push(req->vq, &req->elem, req->in_len);
589 virtio_notify(vdev, req->vq);
590 virtio_crypto_free_request(req);
591 }
592
593 static VirtIOCryptoReq *
594 virtio_crypto_get_request(VirtIOCrypto *s, VirtQueue *vq)
595 {
596 VirtIOCryptoReq *req = virtqueue_pop(vq, sizeof(VirtIOCryptoReq));
597
598 if (req) {
599 virtio_crypto_init_request(s, vq, req);
600 }
601 return req;
602 }
603
604 static CryptoDevBackendSymOpInfo *
605 virtio_crypto_sym_op_helper(VirtIODevice *vdev,
606 struct virtio_crypto_cipher_para *cipher_para,
607 struct virtio_crypto_alg_chain_data_para *alg_chain_para,
608 struct iovec *iov, unsigned int out_num)
609 {
610 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
611 CryptoDevBackendSymOpInfo *op_info;
612 uint32_t src_len = 0, dst_len = 0;
613 uint32_t iv_len = 0;
614 uint32_t aad_len = 0, hash_result_len = 0;
615 uint32_t hash_start_src_offset = 0, len_to_hash = 0;
616 uint32_t cipher_start_src_offset = 0, len_to_cipher = 0;
617
618 uint64_t max_len, curr_size = 0;
619 size_t s;
620
621 /* Plain cipher */
622 if (cipher_para) {
623 iv_len = ldl_le_p(&cipher_para->iv_len);
624 src_len = ldl_le_p(&cipher_para->src_data_len);
625 dst_len = ldl_le_p(&cipher_para->dst_data_len);
626 } else if (alg_chain_para) { /* Algorithm chain */
627 iv_len = ldl_le_p(&alg_chain_para->iv_len);
628 src_len = ldl_le_p(&alg_chain_para->src_data_len);
629 dst_len = ldl_le_p(&alg_chain_para->dst_data_len);
630
631 aad_len = ldl_le_p(&alg_chain_para->aad_len);
632 hash_result_len = ldl_le_p(&alg_chain_para->hash_result_len);
633 hash_start_src_offset = ldl_le_p(
634 &alg_chain_para->hash_start_src_offset);
635 cipher_start_src_offset = ldl_le_p(
636 &alg_chain_para->cipher_start_src_offset);
637 len_to_cipher = ldl_le_p(&alg_chain_para->len_to_cipher);
638 len_to_hash = ldl_le_p(&alg_chain_para->len_to_hash);
639 } else {
640 return NULL;
641 }
642
643 if (unlikely(src_len != dst_len)) {
644 virtio_error(vdev, "sym request src len is different from dst len");
645 return NULL;
646 }
647
648 max_len = (uint64_t)iv_len + aad_len + src_len + dst_len + hash_result_len;
649 if (unlikely(max_len > vcrypto->conf.max_size)) {
650 virtio_error(vdev, "virtio-crypto too big length");
651 return NULL;
652 }
653
654 op_info = g_malloc0(sizeof(CryptoDevBackendSymOpInfo) + max_len);
655 op_info->iv_len = iv_len;
656 op_info->src_len = src_len;
657 op_info->dst_len = dst_len;
658 op_info->aad_len = aad_len;
659 op_info->digest_result_len = hash_result_len;
660 op_info->hash_start_src_offset = hash_start_src_offset;
661 op_info->len_to_hash = len_to_hash;
662 op_info->cipher_start_src_offset = cipher_start_src_offset;
663 op_info->len_to_cipher = len_to_cipher;
664 /* Handle the initialization vector */
665 if (op_info->iv_len > 0) {
666 DPRINTF("iv_len=%" PRIu32 "\n", op_info->iv_len);
667 op_info->iv = op_info->data + curr_size;
668
669 s = iov_to_buf(iov, out_num, 0, op_info->iv, op_info->iv_len);
670 if (unlikely(s != op_info->iv_len)) {
671 virtio_error(vdev, "virtio-crypto iv incorrect");
672 goto err;
673 }
674 iov_discard_front(&iov, &out_num, op_info->iv_len);
675 curr_size += op_info->iv_len;
676 }
677
678 /* Handle additional authentication data if exists */
679 if (op_info->aad_len > 0) {
680 DPRINTF("aad_len=%" PRIu32 "\n", op_info->aad_len);
681 op_info->aad_data = op_info->data + curr_size;
682
683 s = iov_to_buf(iov, out_num, 0, op_info->aad_data, op_info->aad_len);
684 if (unlikely(s != op_info->aad_len)) {
685 virtio_error(vdev, "virtio-crypto additional auth data incorrect");
686 goto err;
687 }
688 iov_discard_front(&iov, &out_num, op_info->aad_len);
689
690 curr_size += op_info->aad_len;
691 }
692
693 /* Handle the source data */
694 if (op_info->src_len > 0) {
695 DPRINTF("src_len=%" PRIu32 "\n", op_info->src_len);
696 op_info->src = op_info->data + curr_size;
697
698 s = iov_to_buf(iov, out_num, 0, op_info->src, op_info->src_len);
699 if (unlikely(s != op_info->src_len)) {
700 virtio_error(vdev, "virtio-crypto source data incorrect");
701 goto err;
702 }
703 iov_discard_front(&iov, &out_num, op_info->src_len);
704
705 curr_size += op_info->src_len;
706 }
707
708 /* Handle the destination data */
709 op_info->dst = op_info->data + curr_size;
710 curr_size += op_info->dst_len;
711
712 DPRINTF("dst_len=%" PRIu32 "\n", op_info->dst_len);
713
714 /* Handle the hash digest result */
715 if (hash_result_len > 0) {
716 DPRINTF("hash_result_len=%" PRIu32 "\n", hash_result_len);
717 op_info->digest_result = op_info->data + curr_size;
718 }
719
720 return op_info;
721
722 err:
723 g_free(op_info);
724 return NULL;
725 }
726
727 static int
728 virtio_crypto_handle_sym_req(VirtIOCrypto *vcrypto,
729 struct virtio_crypto_sym_data_req *req,
730 CryptoDevBackendOpInfo *op_info,
731 struct iovec *iov, unsigned int out_num)
732 {
733 VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
734 CryptoDevBackendSymOpInfo *sym_op_info;
735 uint32_t op_type;
736
737 op_type = ldl_le_p(&req->op_type);
738 if (op_type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
739 sym_op_info = virtio_crypto_sym_op_helper(vdev, &req->u.cipher.para,
740 NULL, iov, out_num);
741 if (!sym_op_info) {
742 return -EFAULT;
743 }
744 } else if (op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
745 sym_op_info = virtio_crypto_sym_op_helper(vdev, NULL,
746 &req->u.chain.para,
747 iov, out_num);
748 if (!sym_op_info) {
749 return -EFAULT;
750 }
751 } else {
752 /* VIRTIO_CRYPTO_SYM_OP_NONE */
753 error_report("virtio-crypto unsupported cipher type");
754 return -VIRTIO_CRYPTO_NOTSUPP;
755 }
756
757 sym_op_info->op_type = op_type;
758 op_info->u.sym_op_info = sym_op_info;
759
760 return 0;
761 }
762
763 static int
764 virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
765 struct virtio_crypto_akcipher_data_req *req,
766 CryptoDevBackendOpInfo *op_info,
767 struct iovec *iov, unsigned int out_num)
768 {
769 VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
770 CryptoDevBackendAsymOpInfo *asym_op_info;
771 uint32_t src_len;
772 uint32_t dst_len;
773 uint32_t len;
774 uint8_t *src = NULL;
775 uint8_t *dst = NULL;
776 uint64_t max_len;
777
778 asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
779 src_len = ldl_le_p(&req->para.src_data_len);
780 dst_len = ldl_le_p(&req->para.dst_data_len);
781
782 max_len = (uint64_t)src_len + dst_len;
783 if (unlikely(max_len > vcrypto->conf.max_size)) {
784 virtio_error(vdev, "virtio-crypto asym request is too large");
785 goto err;
786 }
787
788 if (src_len > 0) {
789 src = g_malloc0(src_len);
790 len = iov_to_buf(iov, out_num, 0, src, src_len);
791 if (unlikely(len != src_len)) {
792 virtio_error(vdev, "virtio-crypto asym src data incorrect"
793 "expected %u, actual %u", src_len, len);
794 goto err;
795 }
796
797 iov_discard_front(&iov, &out_num, src_len);
798 }
799
800 if (dst_len > 0) {
801 dst = g_malloc0(dst_len);
802
803 if (op_info->op_code == VIRTIO_CRYPTO_AKCIPHER_VERIFY) {
804 len = iov_to_buf(iov, out_num, 0, dst, dst_len);
805 if (unlikely(len != dst_len)) {
806 virtio_error(vdev, "virtio-crypto asym dst data incorrect"
807 "expected %u, actual %u", dst_len, len);
808 goto err;
809 }
810
811 iov_discard_front(&iov, &out_num, dst_len);
812 }
813 }
814
815 asym_op_info->src_len = src_len;
816 asym_op_info->dst_len = dst_len;
817 asym_op_info->src = src;
818 asym_op_info->dst = dst;
819 op_info->u.asym_op_info = asym_op_info;
820
821 return 0;
822
823 err:
824 g_free(asym_op_info);
825 g_free(src);
826 g_free(dst);
827
828 return -EFAULT;
829 }
830
831 static int
832 virtio_crypto_handle_request(VirtIOCryptoReq *request)
833 {
834 VirtIOCrypto *vcrypto = request->vcrypto;
835 VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
836 VirtQueueElement *elem = &request->elem;
837 int queue_index = virtio_crypto_vq2q(virtio_get_queue_index(request->vq));
838 struct virtio_crypto_op_data_req req;
839 int ret;
840 g_autofree struct iovec *in_iov_copy = NULL;
841 g_autofree struct iovec *out_iov_copy = NULL;
842 struct iovec *in_iov;
843 struct iovec *out_iov;
844 unsigned in_num;
845 unsigned out_num;
846 uint32_t opcode;
847 CryptoDevBackendOpInfo *op_info = &request->op_info;
848
849 if (elem->out_num < 1 || elem->in_num < 1) {
850 virtio_error(vdev, "virtio-crypto dataq missing headers");
851 return -1;
852 }
853
854 out_num = elem->out_num;
855 out_iov_copy = g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num);
856 out_iov = out_iov_copy;
857
858 in_num = elem->in_num;
859 in_iov_copy = g_memdup2(elem->in_sg, sizeof(in_iov[0]) * in_num);
860 in_iov = in_iov_copy;
861
862 if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req))
863 != sizeof(req))) {
864 virtio_error(vdev, "virtio-crypto request outhdr too short");
865 return -1;
866 }
867 iov_discard_front(&out_iov, &out_num, sizeof(req));
868
869 if (in_iov[in_num - 1].iov_len <
870 sizeof(struct virtio_crypto_inhdr)) {
871 virtio_error(vdev, "virtio-crypto request inhdr too short");
872 return -1;
873 }
874 /* We always touch the last byte, so just see how big in_iov is. */
875 request->in_len = iov_size(in_iov, in_num);
876 request->in = (void *)in_iov[in_num - 1].iov_base
877 + in_iov[in_num - 1].iov_len
878 - sizeof(struct virtio_crypto_inhdr);
879 iov_discard_back(in_iov, &in_num, sizeof(struct virtio_crypto_inhdr));
880
881 /*
882 * The length of operation result, including dest_data
883 * and digest_result if exists.
884 */
885 request->in_num = in_num;
886 request->in_iov = in_iov;
887 /* now, we free the in_iov_copy inside virtio_crypto_free_request */
888 in_iov_copy = NULL;
889
890 opcode = ldl_le_p(&req.header.opcode);
891 op_info->session_id = ldq_le_p(&req.header.session_id);
892 op_info->op_code = opcode;
893 op_info->queue_index = queue_index;
894 op_info->cb = virtio_crypto_req_complete;
895 op_info->opaque = request;
896
897 switch (opcode) {
898 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
899 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
900 op_info->algtype = request->flags = QCRYPTODEV_BACKEND_ALGO_TYPE_SYM;
901 ret = virtio_crypto_handle_sym_req(vcrypto,
902 &req.u.sym_req, op_info,
903 out_iov, out_num);
904 goto check_result;
905
906 case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
907 case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
908 case VIRTIO_CRYPTO_AKCIPHER_SIGN:
909 case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
910 op_info->algtype = request->flags = QCRYPTODEV_BACKEND_ALGO_TYPE_ASYM;
911 ret = virtio_crypto_handle_asym_req(vcrypto,
912 &req.u.akcipher_req, op_info,
913 out_iov, out_num);
914
915 check_result:
916 /* Serious errors, need to reset virtio crypto device */
917 if (ret == -EFAULT) {
918 return -1;
919 } else if (ret == -VIRTIO_CRYPTO_NOTSUPP) {
920 virtio_crypto_req_complete(request, -VIRTIO_CRYPTO_NOTSUPP);
921 } else {
922 ret = cryptodev_backend_crypto_operation(vcrypto->cryptodev,
923 op_info);
924 if (ret < 0) {
925 virtio_crypto_req_complete(request, ret);
926 }
927 }
928 break;
929
930 case VIRTIO_CRYPTO_HASH:
931 case VIRTIO_CRYPTO_MAC:
932 case VIRTIO_CRYPTO_AEAD_ENCRYPT:
933 case VIRTIO_CRYPTO_AEAD_DECRYPT:
934 default:
935 error_report("virtio-crypto unsupported dataq opcode: %u",
936 opcode);
937 virtio_crypto_req_complete(request, -VIRTIO_CRYPTO_NOTSUPP);
938 }
939
940 return 0;
941 }
942
943 static void virtio_crypto_handle_dataq(VirtIODevice *vdev, VirtQueue *vq)
944 {
945 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
946 VirtIOCryptoReq *req;
947
948 while ((req = virtio_crypto_get_request(vcrypto, vq))) {
949 if (virtio_crypto_handle_request(req) < 0) {
950 virtqueue_detach_element(req->vq, &req->elem, 0);
951 virtio_crypto_free_request(req);
952 break;
953 }
954 }
955 }
956
957 static void virtio_crypto_dataq_bh(void *opaque)
958 {
959 VirtIOCryptoQueue *q = opaque;
960 VirtIOCrypto *vcrypto = q->vcrypto;
961 VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
962
963 /* This happens when device was stopped but BH wasn't. */
964 if (!vdev->vm_running) {
965 return;
966 }
967
968 /* Just in case the driver is not ready on more */
969 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
970 return;
971 }
972
973 for (;;) {
974 virtio_crypto_handle_dataq(vdev, q->dataq);
975 virtio_queue_set_notification(q->dataq, 1);
976
977 /* Are we done or did the guest add more buffers? */
978 if (virtio_queue_empty(q->dataq)) {
979 break;
980 }
981
982 virtio_queue_set_notification(q->dataq, 0);
983 }
984 }
985
986 static void
987 virtio_crypto_handle_dataq_bh(VirtIODevice *vdev, VirtQueue *vq)
988 {
989 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
990 VirtIOCryptoQueue *q =
991 &vcrypto->vqs[virtio_crypto_vq2q(virtio_get_queue_index(vq))];
992
993 /* This happens when device was stopped but VCPU wasn't. */
994 if (!vdev->vm_running) {
995 return;
996 }
997 virtio_queue_set_notification(vq, 0);
998 qemu_bh_schedule(q->dataq_bh);
999 }
1000
1001 static uint64_t virtio_crypto_get_features(VirtIODevice *vdev,
1002 uint64_t features,
1003 Error **errp)
1004 {
1005 return features;
1006 }
1007
1008 static void virtio_crypto_reset(VirtIODevice *vdev)
1009 {
1010 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
1011 /* multiqueue is disabled by default */
1012 vcrypto->curr_queues = 1;
1013 if (!cryptodev_backend_is_ready(vcrypto->cryptodev)) {
1014 vcrypto->status &= ~VIRTIO_CRYPTO_S_HW_READY;
1015 } else {
1016 vcrypto->status |= VIRTIO_CRYPTO_S_HW_READY;
1017 }
1018 }
1019
1020 static uint32_t virtio_crypto_init_services(uint32_t qservices)
1021 {
1022 uint32_t vservices = 0;
1023
1024 if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_TYPE_CIPHER)) {
1025 vservices |= (1 << VIRTIO_CRYPTO_SERVICE_CIPHER);
1026 }
1027 if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_TYPE_HASH)) {
1028 vservices |= (1 << VIRTIO_CRYPTO_SERVICE_HASH);
1029 }
1030 if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_TYPE_MAC)) {
1031 vservices |= (1 << VIRTIO_CRYPTO_SERVICE_MAC);
1032 }
1033 if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_TYPE_AEAD)) {
1034 vservices |= (1 << VIRTIO_CRYPTO_SERVICE_AEAD);
1035 }
1036 if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_TYPE_AKCIPHER)) {
1037 vservices |= (1 << VIRTIO_CRYPTO_SERVICE_AKCIPHER);
1038 }
1039
1040 return vservices;
1041 }
1042
1043 static void virtio_crypto_init_config(VirtIODevice *vdev)
1044 {
1045 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
1046
1047 vcrypto->conf.crypto_services = virtio_crypto_init_services(
1048 vcrypto->conf.cryptodev->conf.crypto_services);
1049 vcrypto->conf.cipher_algo_l =
1050 vcrypto->conf.cryptodev->conf.cipher_algo_l;
1051 vcrypto->conf.cipher_algo_h =
1052 vcrypto->conf.cryptodev->conf.cipher_algo_h;
1053 vcrypto->conf.hash_algo = vcrypto->conf.cryptodev->conf.hash_algo;
1054 vcrypto->conf.mac_algo_l = vcrypto->conf.cryptodev->conf.mac_algo_l;
1055 vcrypto->conf.mac_algo_h = vcrypto->conf.cryptodev->conf.mac_algo_h;
1056 vcrypto->conf.aead_algo = vcrypto->conf.cryptodev->conf.aead_algo;
1057 vcrypto->conf.akcipher_algo = vcrypto->conf.cryptodev->conf.akcipher_algo;
1058 vcrypto->conf.max_cipher_key_len =
1059 vcrypto->conf.cryptodev->conf.max_cipher_key_len;
1060 vcrypto->conf.max_auth_key_len =
1061 vcrypto->conf.cryptodev->conf.max_auth_key_len;
1062 vcrypto->conf.max_size = vcrypto->conf.cryptodev->conf.max_size;
1063 }
1064
1065 static void virtio_crypto_device_realize(DeviceState *dev, Error **errp)
1066 {
1067 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1068 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
1069 int i;
1070
1071 vcrypto->cryptodev = vcrypto->conf.cryptodev;
1072 if (vcrypto->cryptodev == NULL) {
1073 error_setg(errp, "'cryptodev' parameter expects a valid object");
1074 return;
1075 } else if (cryptodev_backend_is_used(vcrypto->cryptodev)) {
1076 error_setg(errp, "can't use already used cryptodev backend: %s",
1077 object_get_canonical_path_component(OBJECT(vcrypto->conf.cryptodev)));
1078 return;
1079 }
1080
1081 vcrypto->max_queues = MAX(vcrypto->cryptodev->conf.peers.queues, 1);
1082 if (vcrypto->max_queues + 1 > VIRTIO_QUEUE_MAX) {
1083 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
1084 "must be a positive integer less than %d.",
1085 vcrypto->max_queues, VIRTIO_QUEUE_MAX);
1086 return;
1087 }
1088
1089 virtio_init(vdev, VIRTIO_ID_CRYPTO, vcrypto->config_size);
1090 vcrypto->curr_queues = 1;
1091 vcrypto->vqs = g_new0(VirtIOCryptoQueue, vcrypto->max_queues);
1092 for (i = 0; i < vcrypto->max_queues; i++) {
1093 vcrypto->vqs[i].dataq =
1094 virtio_add_queue(vdev, 1024, virtio_crypto_handle_dataq_bh);
1095 vcrypto->vqs[i].dataq_bh =
1096 virtio_bh_new_guarded(dev, virtio_crypto_dataq_bh,
1097 &vcrypto->vqs[i]);
1098 vcrypto->vqs[i].vcrypto = vcrypto;
1099 }
1100
1101 vcrypto->ctrl_vq = virtio_add_queue(vdev, 1024, virtio_crypto_handle_ctrl);
1102 if (!cryptodev_backend_is_ready(vcrypto->cryptodev)) {
1103 vcrypto->status &= ~VIRTIO_CRYPTO_S_HW_READY;
1104 } else {
1105 vcrypto->status |= VIRTIO_CRYPTO_S_HW_READY;
1106 }
1107
1108 virtio_crypto_init_config(vdev);
1109 cryptodev_backend_set_used(vcrypto->cryptodev, true);
1110 }
1111
1112 static void virtio_crypto_device_unrealize(DeviceState *dev)
1113 {
1114 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1115 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
1116 VirtIOCryptoQueue *q;
1117 int i, max_queues;
1118
1119 max_queues = vcrypto->multiqueue ? vcrypto->max_queues : 1;
1120 for (i = 0; i < max_queues; i++) {
1121 virtio_delete_queue(vcrypto->vqs[i].dataq);
1122 q = &vcrypto->vqs[i];
1123 qemu_bh_delete(q->dataq_bh);
1124 }
1125
1126 g_free(vcrypto->vqs);
1127 virtio_delete_queue(vcrypto->ctrl_vq);
1128
1129 virtio_cleanup(vdev);
1130 cryptodev_backend_set_used(vcrypto->cryptodev, false);
1131 }
1132
1133 static const VMStateDescription vmstate_virtio_crypto = {
1134 .name = "virtio-crypto",
1135 .unmigratable = 1,
1136 .minimum_version_id = VIRTIO_CRYPTO_VM_VERSION,
1137 .version_id = VIRTIO_CRYPTO_VM_VERSION,
1138 .fields = (const VMStateField[]) {
1139 VMSTATE_VIRTIO_DEVICE,
1140 VMSTATE_END_OF_LIST()
1141 },
1142 };
1143
1144 static const Property virtio_crypto_properties[] = {
1145 DEFINE_PROP_LINK("cryptodev", VirtIOCrypto, conf.cryptodev,
1146 TYPE_CRYPTODEV_BACKEND, CryptoDevBackend *),
1147 };
1148
1149 static void virtio_crypto_get_config(VirtIODevice *vdev, uint8_t *config)
1150 {
1151 VirtIOCrypto *c = VIRTIO_CRYPTO(vdev);
1152 struct virtio_crypto_config crypto_cfg = {};
1153
1154 /*
1155 * Virtio-crypto device conforms to VIRTIO 1.0 which is always LE,
1156 * so we can use LE accessors directly.
1157 */
1158 stl_le_p(&crypto_cfg.status, c->status);
1159 stl_le_p(&crypto_cfg.max_dataqueues, c->max_queues);
1160 stl_le_p(&crypto_cfg.crypto_services, c->conf.crypto_services);
1161 stl_le_p(&crypto_cfg.cipher_algo_l, c->conf.cipher_algo_l);
1162 stl_le_p(&crypto_cfg.cipher_algo_h, c->conf.cipher_algo_h);
1163 stl_le_p(&crypto_cfg.hash_algo, c->conf.hash_algo);
1164 stl_le_p(&crypto_cfg.mac_algo_l, c->conf.mac_algo_l);
1165 stl_le_p(&crypto_cfg.mac_algo_h, c->conf.mac_algo_h);
1166 stl_le_p(&crypto_cfg.aead_algo, c->conf.aead_algo);
1167 stl_le_p(&crypto_cfg.max_cipher_key_len, c->conf.max_cipher_key_len);
1168 stl_le_p(&crypto_cfg.max_auth_key_len, c->conf.max_auth_key_len);
1169 stq_le_p(&crypto_cfg.max_size, c->conf.max_size);
1170 stl_le_p(&crypto_cfg.akcipher_algo, c->conf.akcipher_algo);
1171
1172 memcpy(config, &crypto_cfg, c->config_size);
1173 }
1174
1175 static bool virtio_crypto_started(VirtIOCrypto *c, uint8_t status)
1176 {
1177 VirtIODevice *vdev = VIRTIO_DEVICE(c);
1178 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
1179 (c->status & VIRTIO_CRYPTO_S_HW_READY) && vdev->vm_running;
1180 }
1181
1182 static void virtio_crypto_vhost_status(VirtIOCrypto *c, uint8_t status)
1183 {
1184 VirtIODevice *vdev = VIRTIO_DEVICE(c);
1185 int queues = c->multiqueue ? c->max_queues : 1;
1186 CryptoDevBackend *b = c->cryptodev;
1187 CryptoDevBackendClient *cc = b->conf.peers.ccs[0];
1188
1189 if (!cryptodev_get_vhost(cc, b, 0)) {
1190 return;
1191 }
1192
1193 if ((virtio_crypto_started(c, status)) == !!c->vhost_started) {
1194 return;
1195 }
1196
1197 if (!c->vhost_started) {
1198 int r;
1199
1200 c->vhost_started = 1;
1201 r = cryptodev_vhost_start(vdev, queues);
1202 if (r < 0) {
1203 error_report("unable to start vhost crypto: %d: "
1204 "falling back on userspace virtio", -r);
1205 c->vhost_started = 0;
1206 }
1207 } else {
1208 cryptodev_vhost_stop(vdev, queues);
1209 c->vhost_started = 0;
1210 }
1211 }
1212
1213 static int virtio_crypto_set_status(VirtIODevice *vdev, uint8_t status)
1214 {
1215 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
1216
1217 virtio_crypto_vhost_status(vcrypto, status);
1218 return 0;
1219 }
1220
1221 static void virtio_crypto_guest_notifier_mask(VirtIODevice *vdev, int idx,
1222 bool mask)
1223 {
1224 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
1225 int queue = virtio_crypto_vq2q(idx);
1226
1227 assert(vcrypto->vhost_started);
1228
1229 /*
1230 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
1231 * as the macro of configure interrupt's IDX, If this driver does not
1232 * support, the function will return
1233 */
1234
1235 if (idx == VIRTIO_CONFIG_IRQ_IDX) {
1236 return;
1237 }
1238 cryptodev_vhost_virtqueue_mask(vdev, queue, idx, mask);
1239 }
1240
1241 static bool virtio_crypto_guest_notifier_pending(VirtIODevice *vdev, int idx)
1242 {
1243 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
1244 int queue = virtio_crypto_vq2q(idx);
1245
1246 assert(vcrypto->vhost_started);
1247
1248 /*
1249 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
1250 * as the macro of configure interrupt's IDX, If this driver does not
1251 * support, the function will return
1252 */
1253
1254 if (idx == VIRTIO_CONFIG_IRQ_IDX) {
1255 return false;
1256 }
1257 return cryptodev_vhost_virtqueue_pending(vdev, queue, idx);
1258 }
1259
1260 static struct vhost_dev *virtio_crypto_get_vhost(VirtIODevice *vdev)
1261 {
1262 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
1263 CryptoDevBackend *b;
1264 CryptoDevBackendClient *cc;
1265 CryptoDevBackendVhost *vhost_crypto;
1266
1267 b = vcrypto->cryptodev;
1268 if (!b) {
1269 return NULL;
1270 }
1271
1272 cc = b->conf.peers.ccs[0];
1273 vhost_crypto = cryptodev_get_vhost(cc, b, 0);
1274 if (!vhost_crypto) {
1275 return NULL;
1276 }
1277
1278 return &vhost_crypto->dev;
1279 }
1280
1281 static void virtio_crypto_class_init(ObjectClass *klass, const void *data)
1282 {
1283 DeviceClass *dc = DEVICE_CLASS(klass);
1284 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1285
1286 device_class_set_props(dc, virtio_crypto_properties);
1287 dc->vmsd = &vmstate_virtio_crypto;
1288 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1289 vdc->realize = virtio_crypto_device_realize;
1290 vdc->unrealize = virtio_crypto_device_unrealize;
1291 vdc->get_config = virtio_crypto_get_config;
1292 vdc->get_features = virtio_crypto_get_features;
1293 vdc->reset = virtio_crypto_reset;
1294 vdc->set_status = virtio_crypto_set_status;
1295 vdc->guest_notifier_mask = virtio_crypto_guest_notifier_mask;
1296 vdc->guest_notifier_pending = virtio_crypto_guest_notifier_pending;
1297 vdc->get_vhost = virtio_crypto_get_vhost;
1298 }
1299
1300 static void virtio_crypto_instance_init(Object *obj)
1301 {
1302 VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(obj);
1303
1304 /*
1305 * The default config_size is sizeof(struct virtio_crypto_config).
1306 * Can be overridden with virtio_crypto_set_config_size.
1307 */
1308 vcrypto->config_size = sizeof(struct virtio_crypto_config);
1309 }
1310
1311 static const TypeInfo virtio_crypto_info = {
1312 .name = TYPE_VIRTIO_CRYPTO,
1313 .parent = TYPE_VIRTIO_DEVICE,
1314 .instance_size = sizeof(VirtIOCrypto),
1315 .instance_init = virtio_crypto_instance_init,
1316 .class_init = virtio_crypto_class_init,
1317 };
1318
1319 static void virtio_register_types(void)
1320 {
1321 type_register_static(&virtio_crypto_info);
1322 }
1323
1324 type_init(virtio_register_types)