2 * Virtio crypto Support
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
7 * Gonglei <arei.gonglei@huawei.com>
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.
14 #include "qemu/osdep.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/module.h"
18 #include "qapi/error.h"
19 #include "qemu/error-report.h"
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-crypto.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/virtio/virtio-access.h"
25 #include "standard-headers/linux/virtio_ids.h"
26 #include "sysemu/cryptodev-vhost.h"
28 #define VIRTIO_CRYPTO_VM_VERSION 1
31 * Transfer virtqueue index to crypto queue index.
32 * The control virtqueue is after the data virtqueues
33 * so the input value doesn't need to be adjusted
35 static inline int virtio_crypto_vq2q(int queue_index
)
41 virtio_crypto_cipher_session_helper(VirtIODevice
*vdev
,
42 CryptoDevBackendSymSessionInfo
*info
,
43 struct virtio_crypto_cipher_session_para
*cipher_para
,
44 struct iovec
**iov
, unsigned int *out_num
)
46 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
47 unsigned int num
= *out_num
;
49 info
->cipher_alg
= ldl_le_p(&cipher_para
->algo
);
50 info
->key_len
= ldl_le_p(&cipher_para
->keylen
);
51 info
->direction
= ldl_le_p(&cipher_para
->op
);
52 DPRINTF("cipher_alg=%" PRIu32
", info->direction=%" PRIu32
"\n",
53 info
->cipher_alg
, info
->direction
);
55 if (info
->key_len
> vcrypto
->conf
.max_cipher_key_len
) {
56 error_report("virtio-crypto length of cipher key is too big: %u",
58 return -VIRTIO_CRYPTO_ERR
;
61 if (info
->key_len
> 0) {
63 DPRINTF("keylen=%" PRIu32
"\n", info
->key_len
);
65 info
->cipher_key
= g_malloc(info
->key_len
);
66 s
= iov_to_buf(*iov
, num
, 0, info
->cipher_key
, info
->key_len
);
67 if (unlikely(s
!= info
->key_len
)) {
68 virtio_error(vdev
, "virtio-crypto cipher key incorrect");
71 iov_discard_front(iov
, &num
, info
->key_len
);
79 virtio_crypto_create_sym_session(VirtIOCrypto
*vcrypto
,
80 struct virtio_crypto_sym_create_session_req
*sess_req
,
83 struct iovec
*iov
, unsigned int out_num
)
85 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
86 CryptoDevBackendSymSessionInfo info
;
90 Error
*local_err
= NULL
;
93 memset(&info
, 0, sizeof(info
));
94 op_type
= ldl_le_p(&sess_req
->op_type
);
95 info
.op_type
= op_type
;
96 info
.op_code
= opcode
;
98 if (op_type
== VIRTIO_CRYPTO_SYM_OP_CIPHER
) {
99 ret
= virtio_crypto_cipher_session_helper(vdev
, &info
,
100 &sess_req
->u
.cipher
.para
,
105 } else if (op_type
== VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING
) {
108 ret
= virtio_crypto_cipher_session_helper(vdev
, &info
,
109 &sess_req
->u
.chain
.para
.cipher_param
,
115 info
.alg_chain_order
= ldl_le_p(
116 &sess_req
->u
.chain
.para
.alg_chain_order
);
117 info
.add_len
= ldl_le_p(&sess_req
->u
.chain
.para
.aad_len
);
118 info
.hash_mode
= ldl_le_p(&sess_req
->u
.chain
.para
.hash_mode
);
119 if (info
.hash_mode
== VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH
) {
120 info
.hash_alg
= ldl_le_p(&sess_req
->u
.chain
.para
.u
.mac_param
.algo
);
121 info
.auth_key_len
= ldl_le_p(
122 &sess_req
->u
.chain
.para
.u
.mac_param
.auth_key_len
);
123 info
.hash_result_len
= ldl_le_p(
124 &sess_req
->u
.chain
.para
.u
.mac_param
.hash_result_len
);
125 if (info
.auth_key_len
> vcrypto
->conf
.max_auth_key_len
) {
126 error_report("virtio-crypto length of auth key is too big: %u",
128 ret
= -VIRTIO_CRYPTO_ERR
;
132 if (info
.auth_key_len
> 0) {
133 DPRINTF("auth_keylen=%" PRIu32
"\n", info
.auth_key_len
);
134 info
.auth_key
= g_malloc(info
.auth_key_len
);
135 s
= iov_to_buf(iov
, out_num
, 0, info
.auth_key
,
137 if (unlikely(s
!= info
.auth_key_len
)) {
139 "virtio-crypto authenticated key incorrect");
143 iov_discard_front(&iov
, &out_num
, info
.auth_key_len
);
145 } else if (info
.hash_mode
== VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN
) {
146 info
.hash_alg
= ldl_le_p(
147 &sess_req
->u
.chain
.para
.u
.hash_param
.algo
);
148 info
.hash_result_len
= ldl_le_p(
149 &sess_req
->u
.chain
.para
.u
.hash_param
.hash_result_len
);
151 /* VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED */
152 error_report("unsupported hash mode");
153 ret
= -VIRTIO_CRYPTO_NOTSUPP
;
157 /* VIRTIO_CRYPTO_SYM_OP_NONE */
158 error_report("unsupported cipher op_type: VIRTIO_CRYPTO_SYM_OP_NONE");
159 ret
= -VIRTIO_CRYPTO_NOTSUPP
;
163 queue_index
= virtio_crypto_vq2q(queue_id
);
164 session_id
= cryptodev_backend_sym_create_session(
166 &info
, queue_index
, &local_err
);
167 if (session_id
>= 0) {
168 DPRINTF("create session_id=%" PRIu64
" successfully\n",
174 error_report_err(local_err
);
176 ret
= -VIRTIO_CRYPTO_ERR
;
180 g_free(info
.cipher_key
);
181 g_free(info
.auth_key
);
186 virtio_crypto_handle_close_session(VirtIOCrypto
*vcrypto
,
187 struct virtio_crypto_destroy_session_req
*close_sess_req
,
193 Error
*local_err
= NULL
;
195 session_id
= ldq_le_p(&close_sess_req
->session_id
);
196 DPRINTF("close session, id=%" PRIu64
"\n", session_id
);
198 ret
= cryptodev_backend_sym_close_session(
199 vcrypto
->cryptodev
, session_id
, queue_id
, &local_err
);
201 status
= VIRTIO_CRYPTO_OK
;
204 error_report_err(local_err
);
206 error_report("destroy session failed");
208 status
= VIRTIO_CRYPTO_ERR
;
214 static void virtio_crypto_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
216 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
217 struct virtio_crypto_op_ctrl_req ctrl
;
218 VirtQueueElement
*elem
;
219 struct iovec
*in_iov
;
220 struct iovec
*out_iov
;
225 struct virtio_crypto_session_input input
;
231 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
235 if (elem
->out_num
< 1 || elem
->in_num
< 1) {
236 virtio_error(vdev
, "virtio-crypto ctrl missing headers");
237 virtqueue_detach_element(vq
, elem
, 0);
242 out_num
= elem
->out_num
;
243 out_iov
= elem
->out_sg
;
244 in_num
= elem
->in_num
;
245 in_iov
= elem
->in_sg
;
246 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &ctrl
, sizeof(ctrl
))
248 virtio_error(vdev
, "virtio-crypto request ctrl_hdr too short");
249 virtqueue_detach_element(vq
, elem
, 0);
253 iov_discard_front(&out_iov
, &out_num
, sizeof(ctrl
));
255 opcode
= ldl_le_p(&ctrl
.header
.opcode
);
256 queue_id
= ldl_le_p(&ctrl
.header
.queue_id
);
259 case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION
:
260 memset(&input
, 0, sizeof(input
));
261 session_id
= virtio_crypto_create_sym_session(vcrypto
,
262 &ctrl
.u
.sym_create_session
,
265 /* Serious errors, need to reset virtio crypto device */
266 if (session_id
== -EFAULT
) {
267 virtqueue_detach_element(vq
, elem
, 0);
269 } else if (session_id
== -VIRTIO_CRYPTO_NOTSUPP
) {
270 stl_le_p(&input
.status
, VIRTIO_CRYPTO_NOTSUPP
);
271 } else if (session_id
== -VIRTIO_CRYPTO_ERR
) {
272 stl_le_p(&input
.status
, VIRTIO_CRYPTO_ERR
);
274 /* Set the session id */
275 stq_le_p(&input
.session_id
, session_id
);
276 stl_le_p(&input
.status
, VIRTIO_CRYPTO_OK
);
279 s
= iov_from_buf(in_iov
, in_num
, 0, &input
, sizeof(input
));
280 if (unlikely(s
!= sizeof(input
))) {
281 virtio_error(vdev
, "virtio-crypto input incorrect");
282 virtqueue_detach_element(vq
, elem
, 0);
285 virtqueue_push(vq
, elem
, sizeof(input
));
286 virtio_notify(vdev
, vq
);
288 case VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION
:
289 case VIRTIO_CRYPTO_HASH_DESTROY_SESSION
:
290 case VIRTIO_CRYPTO_MAC_DESTROY_SESSION
:
291 case VIRTIO_CRYPTO_AEAD_DESTROY_SESSION
:
292 status
= virtio_crypto_handle_close_session(vcrypto
,
293 &ctrl
.u
.destroy_session
, queue_id
);
294 /* The status only occupy one byte, we can directly use it */
295 s
= iov_from_buf(in_iov
, in_num
, 0, &status
, sizeof(status
));
296 if (unlikely(s
!= sizeof(status
))) {
297 virtio_error(vdev
, "virtio-crypto status incorrect");
298 virtqueue_detach_element(vq
, elem
, 0);
301 virtqueue_push(vq
, elem
, sizeof(status
));
302 virtio_notify(vdev
, vq
);
304 case VIRTIO_CRYPTO_HASH_CREATE_SESSION
:
305 case VIRTIO_CRYPTO_MAC_CREATE_SESSION
:
306 case VIRTIO_CRYPTO_AEAD_CREATE_SESSION
:
308 error_report("virtio-crypto unsupported ctrl opcode: %d", opcode
);
309 memset(&input
, 0, sizeof(input
));
310 stl_le_p(&input
.status
, VIRTIO_CRYPTO_NOTSUPP
);
311 s
= iov_from_buf(in_iov
, in_num
, 0, &input
, sizeof(input
));
312 if (unlikely(s
!= sizeof(input
))) {
313 virtio_error(vdev
, "virtio-crypto input incorrect");
314 virtqueue_detach_element(vq
, elem
, 0);
317 virtqueue_push(vq
, elem
, sizeof(input
));
318 virtio_notify(vdev
, vq
);
321 } /* end switch case */
327 static void virtio_crypto_init_request(VirtIOCrypto
*vcrypto
, VirtQueue
*vq
,
328 VirtIOCryptoReq
*req
)
330 req
->vcrypto
= vcrypto
;
336 req
->flags
= CRYPTODEV_BACKEND_ALG__MAX
;
337 req
->u
.sym_op_info
= NULL
;
340 static void virtio_crypto_free_request(VirtIOCryptoReq
*req
)
343 if (req
->flags
== CRYPTODEV_BACKEND_ALG_SYM
) {
345 CryptoDevBackendSymOpInfo
*op_info
= req
->u
.sym_op_info
;
347 max_len
= op_info
->iv_len
+
351 op_info
->digest_result_len
;
353 /* Zeroize and free request data structure */
354 memset(op_info
, 0, sizeof(*op_info
) + max_len
);
362 virtio_crypto_sym_input_data_helper(VirtIODevice
*vdev
,
363 VirtIOCryptoReq
*req
,
365 CryptoDevBackendSymOpInfo
*sym_op_info
)
369 if (status
!= VIRTIO_CRYPTO_OK
) {
373 len
= sym_op_info
->src_len
;
374 /* Save the cipher result */
375 s
= iov_from_buf(req
->in_iov
, req
->in_num
, 0, sym_op_info
->dst
, len
);
377 virtio_error(vdev
, "virtio-crypto dest data incorrect");
381 iov_discard_front(&req
->in_iov
, &req
->in_num
, len
);
383 if (sym_op_info
->op_type
==
384 VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING
) {
385 /* Save the digest result */
386 s
= iov_from_buf(req
->in_iov
, req
->in_num
, 0,
387 sym_op_info
->digest_result
,
388 sym_op_info
->digest_result_len
);
389 if (s
!= sym_op_info
->digest_result_len
) {
390 virtio_error(vdev
, "virtio-crypto digest result incorrect");
395 static void virtio_crypto_req_complete(VirtIOCryptoReq
*req
, uint8_t status
)
397 VirtIOCrypto
*vcrypto
= req
->vcrypto
;
398 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
400 if (req
->flags
== CRYPTODEV_BACKEND_ALG_SYM
) {
401 virtio_crypto_sym_input_data_helper(vdev
, req
, status
,
404 stb_p(&req
->in
->status
, status
);
405 virtqueue_push(req
->vq
, &req
->elem
, req
->in_len
);
406 virtio_notify(vdev
, req
->vq
);
409 static VirtIOCryptoReq
*
410 virtio_crypto_get_request(VirtIOCrypto
*s
, VirtQueue
*vq
)
412 VirtIOCryptoReq
*req
= virtqueue_pop(vq
, sizeof(VirtIOCryptoReq
));
415 virtio_crypto_init_request(s
, vq
, req
);
420 static CryptoDevBackendSymOpInfo
*
421 virtio_crypto_sym_op_helper(VirtIODevice
*vdev
,
422 struct virtio_crypto_cipher_para
*cipher_para
,
423 struct virtio_crypto_alg_chain_data_para
*alg_chain_para
,
424 struct iovec
*iov
, unsigned int out_num
)
426 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
427 CryptoDevBackendSymOpInfo
*op_info
;
428 uint32_t src_len
= 0, dst_len
= 0;
430 uint32_t aad_len
= 0, hash_result_len
= 0;
431 uint32_t hash_start_src_offset
= 0, len_to_hash
= 0;
432 uint32_t cipher_start_src_offset
= 0, len_to_cipher
= 0;
434 uint64_t max_len
, curr_size
= 0;
439 iv_len
= ldl_le_p(&cipher_para
->iv_len
);
440 src_len
= ldl_le_p(&cipher_para
->src_data_len
);
441 dst_len
= ldl_le_p(&cipher_para
->dst_data_len
);
442 } else if (alg_chain_para
) { /* Algorithm chain */
443 iv_len
= ldl_le_p(&alg_chain_para
->iv_len
);
444 src_len
= ldl_le_p(&alg_chain_para
->src_data_len
);
445 dst_len
= ldl_le_p(&alg_chain_para
->dst_data_len
);
447 aad_len
= ldl_le_p(&alg_chain_para
->aad_len
);
448 hash_result_len
= ldl_le_p(&alg_chain_para
->hash_result_len
);
449 hash_start_src_offset
= ldl_le_p(
450 &alg_chain_para
->hash_start_src_offset
);
451 cipher_start_src_offset
= ldl_le_p(
452 &alg_chain_para
->cipher_start_src_offset
);
453 len_to_cipher
= ldl_le_p(&alg_chain_para
->len_to_cipher
);
454 len_to_hash
= ldl_le_p(&alg_chain_para
->len_to_hash
);
459 max_len
= (uint64_t)iv_len
+ aad_len
+ src_len
+ dst_len
+ hash_result_len
;
460 if (unlikely(max_len
> vcrypto
->conf
.max_size
)) {
461 virtio_error(vdev
, "virtio-crypto too big length");
465 op_info
= g_malloc0(sizeof(CryptoDevBackendSymOpInfo
) + max_len
);
466 op_info
->iv_len
= iv_len
;
467 op_info
->src_len
= src_len
;
468 op_info
->dst_len
= dst_len
;
469 op_info
->aad_len
= aad_len
;
470 op_info
->digest_result_len
= hash_result_len
;
471 op_info
->hash_start_src_offset
= hash_start_src_offset
;
472 op_info
->len_to_hash
= len_to_hash
;
473 op_info
->cipher_start_src_offset
= cipher_start_src_offset
;
474 op_info
->len_to_cipher
= len_to_cipher
;
475 /* Handle the initilization vector */
476 if (op_info
->iv_len
> 0) {
477 DPRINTF("iv_len=%" PRIu32
"\n", op_info
->iv_len
);
478 op_info
->iv
= op_info
->data
+ curr_size
;
480 s
= iov_to_buf(iov
, out_num
, 0, op_info
->iv
, op_info
->iv_len
);
481 if (unlikely(s
!= op_info
->iv_len
)) {
482 virtio_error(vdev
, "virtio-crypto iv incorrect");
485 iov_discard_front(&iov
, &out_num
, op_info
->iv_len
);
486 curr_size
+= op_info
->iv_len
;
489 /* Handle additional authentication data if exists */
490 if (op_info
->aad_len
> 0) {
491 DPRINTF("aad_len=%" PRIu32
"\n", op_info
->aad_len
);
492 op_info
->aad_data
= op_info
->data
+ curr_size
;
494 s
= iov_to_buf(iov
, out_num
, 0, op_info
->aad_data
, op_info
->aad_len
);
495 if (unlikely(s
!= op_info
->aad_len
)) {
496 virtio_error(vdev
, "virtio-crypto additional auth data incorrect");
499 iov_discard_front(&iov
, &out_num
, op_info
->aad_len
);
501 curr_size
+= op_info
->aad_len
;
504 /* Handle the source data */
505 if (op_info
->src_len
> 0) {
506 DPRINTF("src_len=%" PRIu32
"\n", op_info
->src_len
);
507 op_info
->src
= op_info
->data
+ curr_size
;
509 s
= iov_to_buf(iov
, out_num
, 0, op_info
->src
, op_info
->src_len
);
510 if (unlikely(s
!= op_info
->src_len
)) {
511 virtio_error(vdev
, "virtio-crypto source data incorrect");
514 iov_discard_front(&iov
, &out_num
, op_info
->src_len
);
516 curr_size
+= op_info
->src_len
;
519 /* Handle the destination data */
520 op_info
->dst
= op_info
->data
+ curr_size
;
521 curr_size
+= op_info
->dst_len
;
523 DPRINTF("dst_len=%" PRIu32
"\n", op_info
->dst_len
);
525 /* Handle the hash digest result */
526 if (hash_result_len
> 0) {
527 DPRINTF("hash_result_len=%" PRIu32
"\n", hash_result_len
);
528 op_info
->digest_result
= op_info
->data
+ curr_size
;
539 virtio_crypto_handle_sym_req(VirtIOCrypto
*vcrypto
,
540 struct virtio_crypto_sym_data_req
*req
,
541 CryptoDevBackendSymOpInfo
**sym_op_info
,
542 struct iovec
*iov
, unsigned int out_num
)
544 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
546 CryptoDevBackendSymOpInfo
*op_info
;
548 op_type
= ldl_le_p(&req
->op_type
);
550 if (op_type
== VIRTIO_CRYPTO_SYM_OP_CIPHER
) {
551 op_info
= virtio_crypto_sym_op_helper(vdev
, &req
->u
.cipher
.para
,
556 op_info
->op_type
= op_type
;
557 } else if (op_type
== VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING
) {
558 op_info
= virtio_crypto_sym_op_helper(vdev
, NULL
,
564 op_info
->op_type
= op_type
;
566 /* VIRTIO_CRYPTO_SYM_OP_NONE */
567 error_report("virtio-crypto unsupported cipher type");
568 return -VIRTIO_CRYPTO_NOTSUPP
;
571 *sym_op_info
= op_info
;
577 virtio_crypto_handle_request(VirtIOCryptoReq
*request
)
579 VirtIOCrypto
*vcrypto
= request
->vcrypto
;
580 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
581 VirtQueueElement
*elem
= &request
->elem
;
582 int queue_index
= virtio_crypto_vq2q(virtio_get_queue_index(request
->vq
));
583 struct virtio_crypto_op_data_req req
;
585 struct iovec
*in_iov
;
586 struct iovec
*out_iov
;
590 uint8_t status
= VIRTIO_CRYPTO_ERR
;
592 CryptoDevBackendSymOpInfo
*sym_op_info
= NULL
;
593 Error
*local_err
= NULL
;
595 if (elem
->out_num
< 1 || elem
->in_num
< 1) {
596 virtio_error(vdev
, "virtio-crypto dataq missing headers");
600 out_num
= elem
->out_num
;
601 out_iov
= elem
->out_sg
;
602 in_num
= elem
->in_num
;
603 in_iov
= elem
->in_sg
;
604 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &req
, sizeof(req
))
606 virtio_error(vdev
, "virtio-crypto request outhdr too short");
609 iov_discard_front(&out_iov
, &out_num
, sizeof(req
));
611 if (in_iov
[in_num
- 1].iov_len
<
612 sizeof(struct virtio_crypto_inhdr
)) {
613 virtio_error(vdev
, "virtio-crypto request inhdr too short");
616 /* We always touch the last byte, so just see how big in_iov is. */
617 request
->in_len
= iov_size(in_iov
, in_num
);
618 request
->in
= (void *)in_iov
[in_num
- 1].iov_base
619 + in_iov
[in_num
- 1].iov_len
620 - sizeof(struct virtio_crypto_inhdr
);
621 iov_discard_back(in_iov
, &in_num
, sizeof(struct virtio_crypto_inhdr
));
624 * The length of operation result, including dest_data
625 * and digest_result if exists.
627 request
->in_num
= in_num
;
628 request
->in_iov
= in_iov
;
630 opcode
= ldl_le_p(&req
.header
.opcode
);
631 session_id
= ldq_le_p(&req
.header
.session_id
);
634 case VIRTIO_CRYPTO_CIPHER_ENCRYPT
:
635 case VIRTIO_CRYPTO_CIPHER_DECRYPT
:
636 ret
= virtio_crypto_handle_sym_req(vcrypto
,
640 /* Serious errors, need to reset virtio crypto device */
641 if (ret
== -EFAULT
) {
643 } else if (ret
== -VIRTIO_CRYPTO_NOTSUPP
) {
644 virtio_crypto_req_complete(request
, VIRTIO_CRYPTO_NOTSUPP
);
645 virtio_crypto_free_request(request
);
647 sym_op_info
->session_id
= session_id
;
649 /* Set request's parameter */
650 request
->flags
= CRYPTODEV_BACKEND_ALG_SYM
;
651 request
->u
.sym_op_info
= sym_op_info
;
652 ret
= cryptodev_backend_crypto_operation(vcrypto
->cryptodev
,
653 request
, queue_index
, &local_err
);
657 error_report_err(local_err
);
659 } else { /* ret == VIRTIO_CRYPTO_OK */
662 virtio_crypto_req_complete(request
, status
);
663 virtio_crypto_free_request(request
);
666 case VIRTIO_CRYPTO_HASH
:
667 case VIRTIO_CRYPTO_MAC
:
668 case VIRTIO_CRYPTO_AEAD_ENCRYPT
:
669 case VIRTIO_CRYPTO_AEAD_DECRYPT
:
671 error_report("virtio-crypto unsupported dataq opcode: %u",
673 virtio_crypto_req_complete(request
, VIRTIO_CRYPTO_NOTSUPP
);
674 virtio_crypto_free_request(request
);
680 static void virtio_crypto_handle_dataq(VirtIODevice
*vdev
, VirtQueue
*vq
)
682 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
683 VirtIOCryptoReq
*req
;
685 while ((req
= virtio_crypto_get_request(vcrypto
, vq
))) {
686 if (virtio_crypto_handle_request(req
) < 0) {
687 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
688 virtio_crypto_free_request(req
);
694 static void virtio_crypto_dataq_bh(void *opaque
)
696 VirtIOCryptoQueue
*q
= opaque
;
697 VirtIOCrypto
*vcrypto
= q
->vcrypto
;
698 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
700 /* This happens when device was stopped but BH wasn't. */
701 if (!vdev
->vm_running
) {
705 /* Just in case the driver is not ready on more */
706 if (unlikely(!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))) {
711 virtio_crypto_handle_dataq(vdev
, q
->dataq
);
712 virtio_queue_set_notification(q
->dataq
, 1);
714 /* Are we done or did the guest add more buffers? */
715 if (virtio_queue_empty(q
->dataq
)) {
719 virtio_queue_set_notification(q
->dataq
, 0);
724 virtio_crypto_handle_dataq_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
726 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
727 VirtIOCryptoQueue
*q
=
728 &vcrypto
->vqs
[virtio_crypto_vq2q(virtio_get_queue_index(vq
))];
730 /* This happens when device was stopped but VCPU wasn't. */
731 if (!vdev
->vm_running
) {
734 virtio_queue_set_notification(vq
, 0);
735 qemu_bh_schedule(q
->dataq_bh
);
738 static uint64_t virtio_crypto_get_features(VirtIODevice
*vdev
,
745 static void virtio_crypto_reset(VirtIODevice
*vdev
)
747 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
748 /* multiqueue is disabled by default */
749 vcrypto
->curr_queues
= 1;
750 if (!cryptodev_backend_is_ready(vcrypto
->cryptodev
)) {
751 vcrypto
->status
&= ~VIRTIO_CRYPTO_S_HW_READY
;
753 vcrypto
->status
|= VIRTIO_CRYPTO_S_HW_READY
;
757 static void virtio_crypto_init_config(VirtIODevice
*vdev
)
759 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
761 vcrypto
->conf
.crypto_services
=
762 vcrypto
->conf
.cryptodev
->conf
.crypto_services
;
763 vcrypto
->conf
.cipher_algo_l
=
764 vcrypto
->conf
.cryptodev
->conf
.cipher_algo_l
;
765 vcrypto
->conf
.cipher_algo_h
=
766 vcrypto
->conf
.cryptodev
->conf
.cipher_algo_h
;
767 vcrypto
->conf
.hash_algo
= vcrypto
->conf
.cryptodev
->conf
.hash_algo
;
768 vcrypto
->conf
.mac_algo_l
= vcrypto
->conf
.cryptodev
->conf
.mac_algo_l
;
769 vcrypto
->conf
.mac_algo_h
= vcrypto
->conf
.cryptodev
->conf
.mac_algo_h
;
770 vcrypto
->conf
.aead_algo
= vcrypto
->conf
.cryptodev
->conf
.aead_algo
;
771 vcrypto
->conf
.max_cipher_key_len
=
772 vcrypto
->conf
.cryptodev
->conf
.max_cipher_key_len
;
773 vcrypto
->conf
.max_auth_key_len
=
774 vcrypto
->conf
.cryptodev
->conf
.max_auth_key_len
;
775 vcrypto
->conf
.max_size
= vcrypto
->conf
.cryptodev
->conf
.max_size
;
778 static void virtio_crypto_device_realize(DeviceState
*dev
, Error
**errp
)
780 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
781 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(dev
);
784 vcrypto
->cryptodev
= vcrypto
->conf
.cryptodev
;
785 if (vcrypto
->cryptodev
== NULL
) {
786 error_setg(errp
, "'cryptodev' parameter expects a valid object");
788 } else if (cryptodev_backend_is_used(vcrypto
->cryptodev
)) {
789 char *path
= object_get_canonical_path_component(OBJECT(vcrypto
->conf
.cryptodev
));
790 error_setg(errp
, "can't use already used cryptodev backend: %s", path
);
795 vcrypto
->max_queues
= MAX(vcrypto
->cryptodev
->conf
.peers
.queues
, 1);
796 if (vcrypto
->max_queues
+ 1 > VIRTIO_QUEUE_MAX
) {
797 error_setg(errp
, "Invalid number of queues (= %" PRIu32
"), "
798 "must be a positive integer less than %d.",
799 vcrypto
->max_queues
, VIRTIO_QUEUE_MAX
);
803 virtio_init(vdev
, "virtio-crypto", VIRTIO_ID_CRYPTO
, vcrypto
->config_size
);
804 vcrypto
->curr_queues
= 1;
805 vcrypto
->vqs
= g_malloc0(sizeof(VirtIOCryptoQueue
) * vcrypto
->max_queues
);
806 for (i
= 0; i
< vcrypto
->max_queues
; i
++) {
807 vcrypto
->vqs
[i
].dataq
=
808 virtio_add_queue(vdev
, 1024, virtio_crypto_handle_dataq_bh
);
809 vcrypto
->vqs
[i
].dataq_bh
=
810 qemu_bh_new(virtio_crypto_dataq_bh
, &vcrypto
->vqs
[i
]);
811 vcrypto
->vqs
[i
].vcrypto
= vcrypto
;
814 vcrypto
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_crypto_handle_ctrl
);
815 if (!cryptodev_backend_is_ready(vcrypto
->cryptodev
)) {
816 vcrypto
->status
&= ~VIRTIO_CRYPTO_S_HW_READY
;
818 vcrypto
->status
|= VIRTIO_CRYPTO_S_HW_READY
;
821 virtio_crypto_init_config(vdev
);
822 cryptodev_backend_set_used(vcrypto
->cryptodev
, true);
825 static void virtio_crypto_device_unrealize(DeviceState
*dev
)
827 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
828 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(dev
);
829 VirtIOCryptoQueue
*q
;
832 max_queues
= vcrypto
->multiqueue
? vcrypto
->max_queues
: 1;
833 for (i
= 0; i
< max_queues
; i
++) {
834 virtio_delete_queue(vcrypto
->vqs
[i
].dataq
);
835 q
= &vcrypto
->vqs
[i
];
836 qemu_bh_delete(q
->dataq_bh
);
839 g_free(vcrypto
->vqs
);
840 virtio_delete_queue(vcrypto
->ctrl_vq
);
842 virtio_cleanup(vdev
);
843 cryptodev_backend_set_used(vcrypto
->cryptodev
, false);
846 static const VMStateDescription vmstate_virtio_crypto
= {
847 .name
= "virtio-crypto",
849 .minimum_version_id
= VIRTIO_CRYPTO_VM_VERSION
,
850 .version_id
= VIRTIO_CRYPTO_VM_VERSION
,
851 .fields
= (VMStateField
[]) {
852 VMSTATE_VIRTIO_DEVICE
,
853 VMSTATE_END_OF_LIST()
857 static Property virtio_crypto_properties
[] = {
858 DEFINE_PROP_LINK("cryptodev", VirtIOCrypto
, conf
.cryptodev
,
859 TYPE_CRYPTODEV_BACKEND
, CryptoDevBackend
*),
860 DEFINE_PROP_END_OF_LIST(),
863 static void virtio_crypto_get_config(VirtIODevice
*vdev
, uint8_t *config
)
865 VirtIOCrypto
*c
= VIRTIO_CRYPTO(vdev
);
866 struct virtio_crypto_config crypto_cfg
= {};
869 * Virtio-crypto device conforms to VIRTIO 1.0 which is always LE,
870 * so we can use LE accessors directly.
872 stl_le_p(&crypto_cfg
.status
, c
->status
);
873 stl_le_p(&crypto_cfg
.max_dataqueues
, c
->max_queues
);
874 stl_le_p(&crypto_cfg
.crypto_services
, c
->conf
.crypto_services
);
875 stl_le_p(&crypto_cfg
.cipher_algo_l
, c
->conf
.cipher_algo_l
);
876 stl_le_p(&crypto_cfg
.cipher_algo_h
, c
->conf
.cipher_algo_h
);
877 stl_le_p(&crypto_cfg
.hash_algo
, c
->conf
.hash_algo
);
878 stl_le_p(&crypto_cfg
.mac_algo_l
, c
->conf
.mac_algo_l
);
879 stl_le_p(&crypto_cfg
.mac_algo_h
, c
->conf
.mac_algo_h
);
880 stl_le_p(&crypto_cfg
.aead_algo
, c
->conf
.aead_algo
);
881 stl_le_p(&crypto_cfg
.max_cipher_key_len
, c
->conf
.max_cipher_key_len
);
882 stl_le_p(&crypto_cfg
.max_auth_key_len
, c
->conf
.max_auth_key_len
);
883 stq_le_p(&crypto_cfg
.max_size
, c
->conf
.max_size
);
885 memcpy(config
, &crypto_cfg
, c
->config_size
);
888 static bool virtio_crypto_started(VirtIOCrypto
*c
, uint8_t status
)
890 VirtIODevice
*vdev
= VIRTIO_DEVICE(c
);
891 return (status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
892 (c
->status
& VIRTIO_CRYPTO_S_HW_READY
) && vdev
->vm_running
;
895 static void virtio_crypto_vhost_status(VirtIOCrypto
*c
, uint8_t status
)
897 VirtIODevice
*vdev
= VIRTIO_DEVICE(c
);
898 int queues
= c
->multiqueue
? c
->max_queues
: 1;
899 CryptoDevBackend
*b
= c
->cryptodev
;
900 CryptoDevBackendClient
*cc
= b
->conf
.peers
.ccs
[0];
902 if (!cryptodev_get_vhost(cc
, b
, 0)) {
906 if ((virtio_crypto_started(c
, status
)) == !!c
->vhost_started
) {
910 if (!c
->vhost_started
) {
913 c
->vhost_started
= 1;
914 r
= cryptodev_vhost_start(vdev
, queues
);
916 error_report("unable to start vhost crypto: %d: "
917 "falling back on userspace virtio", -r
);
918 c
->vhost_started
= 0;
921 cryptodev_vhost_stop(vdev
, queues
);
922 c
->vhost_started
= 0;
926 static void virtio_crypto_set_status(VirtIODevice
*vdev
, uint8_t status
)
928 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
930 virtio_crypto_vhost_status(vcrypto
, status
);
933 static void virtio_crypto_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
936 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
937 int queue
= virtio_crypto_vq2q(idx
);
939 assert(vcrypto
->vhost_started
);
941 cryptodev_vhost_virtqueue_mask(vdev
, queue
, idx
, mask
);
944 static bool virtio_crypto_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
946 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
947 int queue
= virtio_crypto_vq2q(idx
);
949 assert(vcrypto
->vhost_started
);
951 return cryptodev_vhost_virtqueue_pending(vdev
, queue
, idx
);
954 static void virtio_crypto_class_init(ObjectClass
*klass
, void *data
)
956 DeviceClass
*dc
= DEVICE_CLASS(klass
);
957 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
959 device_class_set_props(dc
, virtio_crypto_properties
);
960 dc
->vmsd
= &vmstate_virtio_crypto
;
961 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
962 vdc
->realize
= virtio_crypto_device_realize
;
963 vdc
->unrealize
= virtio_crypto_device_unrealize
;
964 vdc
->get_config
= virtio_crypto_get_config
;
965 vdc
->get_features
= virtio_crypto_get_features
;
966 vdc
->reset
= virtio_crypto_reset
;
967 vdc
->set_status
= virtio_crypto_set_status
;
968 vdc
->guest_notifier_mask
= virtio_crypto_guest_notifier_mask
;
969 vdc
->guest_notifier_pending
= virtio_crypto_guest_notifier_pending
;
972 static void virtio_crypto_instance_init(Object
*obj
)
974 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(obj
);
977 * The default config_size is sizeof(struct virtio_crypto_config).
978 * Can be overriden with virtio_crypto_set_config_size.
980 vcrypto
->config_size
= sizeof(struct virtio_crypto_config
);
983 static const TypeInfo virtio_crypto_info
= {
984 .name
= TYPE_VIRTIO_CRYPTO
,
985 .parent
= TYPE_VIRTIO_DEVICE
,
986 .instance_size
= sizeof(VirtIOCrypto
),
987 .instance_init
= virtio_crypto_instance_init
,
988 .class_init
= virtio_crypto_class_init
,
991 static void virtio_register_types(void)
993 type_register_static(&virtio_crypto_info
);
996 type_init(virtio_register_types
)