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/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/virtio/virtio-access.h"
24 #include "standard-headers/linux/virtio_ids.h"
25 #include "sysemu/cryptodev-vhost.h"
27 #define VIRTIO_CRYPTO_VM_VERSION 1
30 * Transfer virtqueue index to crypto queue index.
31 * The control virtqueue is after the data virtqueues
32 * so the input value doesn't need to be adjusted
34 static inline int virtio_crypto_vq2q(int queue_index
)
40 virtio_crypto_cipher_session_helper(VirtIODevice
*vdev
,
41 CryptoDevBackendSymSessionInfo
*info
,
42 struct virtio_crypto_cipher_session_para
*cipher_para
,
43 struct iovec
**iov
, unsigned int *out_num
)
45 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
46 unsigned int num
= *out_num
;
48 info
->cipher_alg
= ldl_le_p(&cipher_para
->algo
);
49 info
->key_len
= ldl_le_p(&cipher_para
->keylen
);
50 info
->direction
= ldl_le_p(&cipher_para
->op
);
51 DPRINTF("cipher_alg=%" PRIu32
", info->direction=%" PRIu32
"\n",
52 info
->cipher_alg
, info
->direction
);
54 if (info
->key_len
> vcrypto
->conf
.max_cipher_key_len
) {
55 error_report("virtio-crypto length of cipher key is too big: %u",
57 return -VIRTIO_CRYPTO_ERR
;
60 if (info
->key_len
> 0) {
62 DPRINTF("keylen=%" PRIu32
"\n", info
->key_len
);
64 info
->cipher_key
= g_malloc(info
->key_len
);
65 s
= iov_to_buf(*iov
, num
, 0, info
->cipher_key
, info
->key_len
);
66 if (unlikely(s
!= info
->key_len
)) {
67 virtio_error(vdev
, "virtio-crypto cipher key incorrect");
70 iov_discard_front(iov
, &num
, info
->key_len
);
78 virtio_crypto_create_sym_session(VirtIOCrypto
*vcrypto
,
79 struct virtio_crypto_sym_create_session_req
*sess_req
,
82 struct iovec
*iov
, unsigned int out_num
)
84 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
85 CryptoDevBackendSymSessionInfo info
;
89 Error
*local_err
= NULL
;
92 memset(&info
, 0, sizeof(info
));
93 op_type
= ldl_le_p(&sess_req
->op_type
);
94 info
.op_type
= op_type
;
95 info
.op_code
= opcode
;
97 if (op_type
== VIRTIO_CRYPTO_SYM_OP_CIPHER
) {
98 ret
= virtio_crypto_cipher_session_helper(vdev
, &info
,
99 &sess_req
->u
.cipher
.para
,
104 } else if (op_type
== VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING
) {
107 ret
= virtio_crypto_cipher_session_helper(vdev
, &info
,
108 &sess_req
->u
.chain
.para
.cipher_param
,
114 info
.alg_chain_order
= ldl_le_p(
115 &sess_req
->u
.chain
.para
.alg_chain_order
);
116 info
.add_len
= ldl_le_p(&sess_req
->u
.chain
.para
.aad_len
);
117 info
.hash_mode
= ldl_le_p(&sess_req
->u
.chain
.para
.hash_mode
);
118 if (info
.hash_mode
== VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH
) {
119 info
.hash_alg
= ldl_le_p(&sess_req
->u
.chain
.para
.u
.mac_param
.algo
);
120 info
.auth_key_len
= ldl_le_p(
121 &sess_req
->u
.chain
.para
.u
.mac_param
.auth_key_len
);
122 info
.hash_result_len
= ldl_le_p(
123 &sess_req
->u
.chain
.para
.u
.mac_param
.hash_result_len
);
124 if (info
.auth_key_len
> vcrypto
->conf
.max_auth_key_len
) {
125 error_report("virtio-crypto length of auth key is too big: %u",
127 ret
= -VIRTIO_CRYPTO_ERR
;
131 if (info
.auth_key_len
> 0) {
132 DPRINTF("auth_keylen=%" PRIu32
"\n", info
.auth_key_len
);
133 info
.auth_key
= g_malloc(info
.auth_key_len
);
134 s
= iov_to_buf(iov
, out_num
, 0, info
.auth_key
,
136 if (unlikely(s
!= info
.auth_key_len
)) {
138 "virtio-crypto authenticated key incorrect");
142 iov_discard_front(&iov
, &out_num
, info
.auth_key_len
);
144 } else if (info
.hash_mode
== VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN
) {
145 info
.hash_alg
= ldl_le_p(
146 &sess_req
->u
.chain
.para
.u
.hash_param
.algo
);
147 info
.hash_result_len
= ldl_le_p(
148 &sess_req
->u
.chain
.para
.u
.hash_param
.hash_result_len
);
150 /* VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED */
151 error_report("unsupported hash mode");
152 ret
= -VIRTIO_CRYPTO_NOTSUPP
;
156 /* VIRTIO_CRYPTO_SYM_OP_NONE */
157 error_report("unsupported cipher op_type: VIRTIO_CRYPTO_SYM_OP_NONE");
158 ret
= -VIRTIO_CRYPTO_NOTSUPP
;
162 queue_index
= virtio_crypto_vq2q(queue_id
);
163 session_id
= cryptodev_backend_sym_create_session(
165 &info
, queue_index
, &local_err
);
166 if (session_id
>= 0) {
167 DPRINTF("create session_id=%" PRIu64
" successfully\n",
173 error_report_err(local_err
);
175 ret
= -VIRTIO_CRYPTO_ERR
;
179 g_free(info
.cipher_key
);
180 g_free(info
.auth_key
);
185 virtio_crypto_handle_close_session(VirtIOCrypto
*vcrypto
,
186 struct virtio_crypto_destroy_session_req
*close_sess_req
,
192 Error
*local_err
= NULL
;
194 session_id
= ldq_le_p(&close_sess_req
->session_id
);
195 DPRINTF("close session, id=%" PRIu64
"\n", session_id
);
197 ret
= cryptodev_backend_sym_close_session(
198 vcrypto
->cryptodev
, session_id
, queue_id
, &local_err
);
200 status
= VIRTIO_CRYPTO_OK
;
203 error_report_err(local_err
);
205 error_report("destroy session failed");
207 status
= VIRTIO_CRYPTO_ERR
;
213 static void virtio_crypto_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
215 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
216 struct virtio_crypto_op_ctrl_req ctrl
;
217 VirtQueueElement
*elem
;
218 struct iovec
*in_iov
;
219 struct iovec
*out_iov
;
224 struct virtio_crypto_session_input input
;
230 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
234 if (elem
->out_num
< 1 || elem
->in_num
< 1) {
235 virtio_error(vdev
, "virtio-crypto ctrl missing headers");
236 virtqueue_detach_element(vq
, elem
, 0);
241 out_num
= elem
->out_num
;
242 out_iov
= elem
->out_sg
;
243 in_num
= elem
->in_num
;
244 in_iov
= elem
->in_sg
;
245 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &ctrl
, sizeof(ctrl
))
247 virtio_error(vdev
, "virtio-crypto request ctrl_hdr too short");
248 virtqueue_detach_element(vq
, elem
, 0);
252 iov_discard_front(&out_iov
, &out_num
, sizeof(ctrl
));
254 opcode
= ldl_le_p(&ctrl
.header
.opcode
);
255 queue_id
= ldl_le_p(&ctrl
.header
.queue_id
);
258 case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION
:
259 memset(&input
, 0, sizeof(input
));
260 session_id
= virtio_crypto_create_sym_session(vcrypto
,
261 &ctrl
.u
.sym_create_session
,
264 /* Serious errors, need to reset virtio crypto device */
265 if (session_id
== -EFAULT
) {
266 virtqueue_detach_element(vq
, elem
, 0);
268 } else if (session_id
== -VIRTIO_CRYPTO_NOTSUPP
) {
269 stl_le_p(&input
.status
, VIRTIO_CRYPTO_NOTSUPP
);
270 } else if (session_id
== -VIRTIO_CRYPTO_ERR
) {
271 stl_le_p(&input
.status
, VIRTIO_CRYPTO_ERR
);
273 /* Set the session id */
274 stq_le_p(&input
.session_id
, session_id
);
275 stl_le_p(&input
.status
, VIRTIO_CRYPTO_OK
);
278 s
= iov_from_buf(in_iov
, in_num
, 0, &input
, sizeof(input
));
279 if (unlikely(s
!= sizeof(input
))) {
280 virtio_error(vdev
, "virtio-crypto input incorrect");
281 virtqueue_detach_element(vq
, elem
, 0);
284 virtqueue_push(vq
, elem
, sizeof(input
));
285 virtio_notify(vdev
, vq
);
287 case VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION
:
288 case VIRTIO_CRYPTO_HASH_DESTROY_SESSION
:
289 case VIRTIO_CRYPTO_MAC_DESTROY_SESSION
:
290 case VIRTIO_CRYPTO_AEAD_DESTROY_SESSION
:
291 status
= virtio_crypto_handle_close_session(vcrypto
,
292 &ctrl
.u
.destroy_session
, queue_id
);
293 /* The status only occupy one byte, we can directly use it */
294 s
= iov_from_buf(in_iov
, in_num
, 0, &status
, sizeof(status
));
295 if (unlikely(s
!= sizeof(status
))) {
296 virtio_error(vdev
, "virtio-crypto status incorrect");
297 virtqueue_detach_element(vq
, elem
, 0);
300 virtqueue_push(vq
, elem
, sizeof(status
));
301 virtio_notify(vdev
, vq
);
303 case VIRTIO_CRYPTO_HASH_CREATE_SESSION
:
304 case VIRTIO_CRYPTO_MAC_CREATE_SESSION
:
305 case VIRTIO_CRYPTO_AEAD_CREATE_SESSION
:
307 error_report("virtio-crypto unsupported ctrl opcode: %d", opcode
);
308 memset(&input
, 0, sizeof(input
));
309 stl_le_p(&input
.status
, VIRTIO_CRYPTO_NOTSUPP
);
310 s
= iov_from_buf(in_iov
, in_num
, 0, &input
, sizeof(input
));
311 if (unlikely(s
!= sizeof(input
))) {
312 virtio_error(vdev
, "virtio-crypto input incorrect");
313 virtqueue_detach_element(vq
, elem
, 0);
316 virtqueue_push(vq
, elem
, sizeof(input
));
317 virtio_notify(vdev
, vq
);
320 } /* end switch case */
326 static void virtio_crypto_init_request(VirtIOCrypto
*vcrypto
, VirtQueue
*vq
,
327 VirtIOCryptoReq
*req
)
329 req
->vcrypto
= vcrypto
;
335 req
->flags
= CRYPTODEV_BACKEND_ALG__MAX
;
336 req
->u
.sym_op_info
= NULL
;
339 static void virtio_crypto_free_request(VirtIOCryptoReq
*req
)
342 if (req
->flags
== CRYPTODEV_BACKEND_ALG_SYM
) {
344 CryptoDevBackendSymOpInfo
*op_info
= req
->u
.sym_op_info
;
346 max_len
= op_info
->iv_len
+
350 op_info
->digest_result_len
;
352 /* Zeroize and free request data structure */
353 memset(op_info
, 0, sizeof(*op_info
) + max_len
);
361 virtio_crypto_sym_input_data_helper(VirtIODevice
*vdev
,
362 VirtIOCryptoReq
*req
,
364 CryptoDevBackendSymOpInfo
*sym_op_info
)
368 if (status
!= VIRTIO_CRYPTO_OK
) {
372 len
= sym_op_info
->src_len
;
373 /* Save the cipher result */
374 s
= iov_from_buf(req
->in_iov
, req
->in_num
, 0, sym_op_info
->dst
, len
);
376 virtio_error(vdev
, "virtio-crypto dest data incorrect");
380 iov_discard_front(&req
->in_iov
, &req
->in_num
, len
);
382 if (sym_op_info
->op_type
==
383 VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING
) {
384 /* Save the digest result */
385 s
= iov_from_buf(req
->in_iov
, req
->in_num
, 0,
386 sym_op_info
->digest_result
,
387 sym_op_info
->digest_result_len
);
388 if (s
!= sym_op_info
->digest_result_len
) {
389 virtio_error(vdev
, "virtio-crypto digest result incorrect");
394 static void virtio_crypto_req_complete(VirtIOCryptoReq
*req
, uint8_t status
)
396 VirtIOCrypto
*vcrypto
= req
->vcrypto
;
397 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
399 if (req
->flags
== CRYPTODEV_BACKEND_ALG_SYM
) {
400 virtio_crypto_sym_input_data_helper(vdev
, req
, status
,
403 stb_p(&req
->in
->status
, status
);
404 virtqueue_push(req
->vq
, &req
->elem
, req
->in_len
);
405 virtio_notify(vdev
, req
->vq
);
408 static VirtIOCryptoReq
*
409 virtio_crypto_get_request(VirtIOCrypto
*s
, VirtQueue
*vq
)
411 VirtIOCryptoReq
*req
= virtqueue_pop(vq
, sizeof(VirtIOCryptoReq
));
414 virtio_crypto_init_request(s
, vq
, req
);
419 static CryptoDevBackendSymOpInfo
*
420 virtio_crypto_sym_op_helper(VirtIODevice
*vdev
,
421 struct virtio_crypto_cipher_para
*cipher_para
,
422 struct virtio_crypto_alg_chain_data_para
*alg_chain_para
,
423 struct iovec
*iov
, unsigned int out_num
)
425 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
426 CryptoDevBackendSymOpInfo
*op_info
;
427 uint32_t src_len
= 0, dst_len
= 0;
429 uint32_t aad_len
= 0, hash_result_len
= 0;
430 uint32_t hash_start_src_offset
= 0, len_to_hash
= 0;
431 uint32_t cipher_start_src_offset
= 0, len_to_cipher
= 0;
433 uint64_t max_len
, curr_size
= 0;
438 iv_len
= ldl_le_p(&cipher_para
->iv_len
);
439 src_len
= ldl_le_p(&cipher_para
->src_data_len
);
440 dst_len
= ldl_le_p(&cipher_para
->dst_data_len
);
441 } else if (alg_chain_para
) { /* Algorithm chain */
442 iv_len
= ldl_le_p(&alg_chain_para
->iv_len
);
443 src_len
= ldl_le_p(&alg_chain_para
->src_data_len
);
444 dst_len
= ldl_le_p(&alg_chain_para
->dst_data_len
);
446 aad_len
= ldl_le_p(&alg_chain_para
->aad_len
);
447 hash_result_len
= ldl_le_p(&alg_chain_para
->hash_result_len
);
448 hash_start_src_offset
= ldl_le_p(
449 &alg_chain_para
->hash_start_src_offset
);
450 cipher_start_src_offset
= ldl_le_p(
451 &alg_chain_para
->cipher_start_src_offset
);
452 len_to_cipher
= ldl_le_p(&alg_chain_para
->len_to_cipher
);
453 len_to_hash
= ldl_le_p(&alg_chain_para
->len_to_hash
);
458 max_len
= (uint64_t)iv_len
+ aad_len
+ src_len
+ dst_len
+ hash_result_len
;
459 if (unlikely(max_len
> vcrypto
->conf
.max_size
)) {
460 virtio_error(vdev
, "virtio-crypto too big length");
464 op_info
= g_malloc0(sizeof(CryptoDevBackendSymOpInfo
) + max_len
);
465 op_info
->iv_len
= iv_len
;
466 op_info
->src_len
= src_len
;
467 op_info
->dst_len
= dst_len
;
468 op_info
->aad_len
= aad_len
;
469 op_info
->digest_result_len
= hash_result_len
;
470 op_info
->hash_start_src_offset
= hash_start_src_offset
;
471 op_info
->len_to_hash
= len_to_hash
;
472 op_info
->cipher_start_src_offset
= cipher_start_src_offset
;
473 op_info
->len_to_cipher
= len_to_cipher
;
474 /* Handle the initilization vector */
475 if (op_info
->iv_len
> 0) {
476 DPRINTF("iv_len=%" PRIu32
"\n", op_info
->iv_len
);
477 op_info
->iv
= op_info
->data
+ curr_size
;
479 s
= iov_to_buf(iov
, out_num
, 0, op_info
->iv
, op_info
->iv_len
);
480 if (unlikely(s
!= op_info
->iv_len
)) {
481 virtio_error(vdev
, "virtio-crypto iv incorrect");
484 iov_discard_front(&iov
, &out_num
, op_info
->iv_len
);
485 curr_size
+= op_info
->iv_len
;
488 /* Handle additional authentication data if exists */
489 if (op_info
->aad_len
> 0) {
490 DPRINTF("aad_len=%" PRIu32
"\n", op_info
->aad_len
);
491 op_info
->aad_data
= op_info
->data
+ curr_size
;
493 s
= iov_to_buf(iov
, out_num
, 0, op_info
->aad_data
, op_info
->aad_len
);
494 if (unlikely(s
!= op_info
->aad_len
)) {
495 virtio_error(vdev
, "virtio-crypto additional auth data incorrect");
498 iov_discard_front(&iov
, &out_num
, op_info
->aad_len
);
500 curr_size
+= op_info
->aad_len
;
503 /* Handle the source data */
504 if (op_info
->src_len
> 0) {
505 DPRINTF("src_len=%" PRIu32
"\n", op_info
->src_len
);
506 op_info
->src
= op_info
->data
+ curr_size
;
508 s
= iov_to_buf(iov
, out_num
, 0, op_info
->src
, op_info
->src_len
);
509 if (unlikely(s
!= op_info
->src_len
)) {
510 virtio_error(vdev
, "virtio-crypto source data incorrect");
513 iov_discard_front(&iov
, &out_num
, op_info
->src_len
);
515 curr_size
+= op_info
->src_len
;
518 /* Handle the destination data */
519 op_info
->dst
= op_info
->data
+ curr_size
;
520 curr_size
+= op_info
->dst_len
;
522 DPRINTF("dst_len=%" PRIu32
"\n", op_info
->dst_len
);
524 /* Handle the hash digest result */
525 if (hash_result_len
> 0) {
526 DPRINTF("hash_result_len=%" PRIu32
"\n", hash_result_len
);
527 op_info
->digest_result
= op_info
->data
+ curr_size
;
538 virtio_crypto_handle_sym_req(VirtIOCrypto
*vcrypto
,
539 struct virtio_crypto_sym_data_req
*req
,
540 CryptoDevBackendSymOpInfo
**sym_op_info
,
541 struct iovec
*iov
, unsigned int out_num
)
543 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
545 CryptoDevBackendSymOpInfo
*op_info
;
547 op_type
= ldl_le_p(&req
->op_type
);
549 if (op_type
== VIRTIO_CRYPTO_SYM_OP_CIPHER
) {
550 op_info
= virtio_crypto_sym_op_helper(vdev
, &req
->u
.cipher
.para
,
555 op_info
->op_type
= op_type
;
556 } else if (op_type
== VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING
) {
557 op_info
= virtio_crypto_sym_op_helper(vdev
, NULL
,
563 op_info
->op_type
= op_type
;
565 /* VIRTIO_CRYPTO_SYM_OP_NONE */
566 error_report("virtio-crypto unsupported cipher type");
567 return -VIRTIO_CRYPTO_NOTSUPP
;
570 *sym_op_info
= op_info
;
576 virtio_crypto_handle_request(VirtIOCryptoReq
*request
)
578 VirtIOCrypto
*vcrypto
= request
->vcrypto
;
579 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
580 VirtQueueElement
*elem
= &request
->elem
;
581 int queue_index
= virtio_crypto_vq2q(virtio_get_queue_index(request
->vq
));
582 struct virtio_crypto_op_data_req req
;
584 struct iovec
*in_iov
;
585 struct iovec
*out_iov
;
589 uint8_t status
= VIRTIO_CRYPTO_ERR
;
591 CryptoDevBackendSymOpInfo
*sym_op_info
= NULL
;
592 Error
*local_err
= NULL
;
594 if (elem
->out_num
< 1 || elem
->in_num
< 1) {
595 virtio_error(vdev
, "virtio-crypto dataq missing headers");
599 out_num
= elem
->out_num
;
600 out_iov
= elem
->out_sg
;
601 in_num
= elem
->in_num
;
602 in_iov
= elem
->in_sg
;
603 if (unlikely(iov_to_buf(out_iov
, out_num
, 0, &req
, sizeof(req
))
605 virtio_error(vdev
, "virtio-crypto request outhdr too short");
608 iov_discard_front(&out_iov
, &out_num
, sizeof(req
));
610 if (in_iov
[in_num
- 1].iov_len
<
611 sizeof(struct virtio_crypto_inhdr
)) {
612 virtio_error(vdev
, "virtio-crypto request inhdr too short");
615 /* We always touch the last byte, so just see how big in_iov is. */
616 request
->in_len
= iov_size(in_iov
, in_num
);
617 request
->in
= (void *)in_iov
[in_num
- 1].iov_base
618 + in_iov
[in_num
- 1].iov_len
619 - sizeof(struct virtio_crypto_inhdr
);
620 iov_discard_back(in_iov
, &in_num
, sizeof(struct virtio_crypto_inhdr
));
623 * The length of operation result, including dest_data
624 * and digest_result if exists.
626 request
->in_num
= in_num
;
627 request
->in_iov
= in_iov
;
629 opcode
= ldl_le_p(&req
.header
.opcode
);
630 session_id
= ldq_le_p(&req
.header
.session_id
);
633 case VIRTIO_CRYPTO_CIPHER_ENCRYPT
:
634 case VIRTIO_CRYPTO_CIPHER_DECRYPT
:
635 ret
= virtio_crypto_handle_sym_req(vcrypto
,
639 /* Serious errors, need to reset virtio crypto device */
640 if (ret
== -EFAULT
) {
642 } else if (ret
== -VIRTIO_CRYPTO_NOTSUPP
) {
643 virtio_crypto_req_complete(request
, VIRTIO_CRYPTO_NOTSUPP
);
644 virtio_crypto_free_request(request
);
646 sym_op_info
->session_id
= session_id
;
648 /* Set request's parameter */
649 request
->flags
= CRYPTODEV_BACKEND_ALG_SYM
;
650 request
->u
.sym_op_info
= sym_op_info
;
651 ret
= cryptodev_backend_crypto_operation(vcrypto
->cryptodev
,
652 request
, queue_index
, &local_err
);
656 error_report_err(local_err
);
658 } else { /* ret == VIRTIO_CRYPTO_OK */
661 virtio_crypto_req_complete(request
, status
);
662 virtio_crypto_free_request(request
);
665 case VIRTIO_CRYPTO_HASH
:
666 case VIRTIO_CRYPTO_MAC
:
667 case VIRTIO_CRYPTO_AEAD_ENCRYPT
:
668 case VIRTIO_CRYPTO_AEAD_DECRYPT
:
670 error_report("virtio-crypto unsupported dataq opcode: %u",
672 virtio_crypto_req_complete(request
, VIRTIO_CRYPTO_NOTSUPP
);
673 virtio_crypto_free_request(request
);
679 static void virtio_crypto_handle_dataq(VirtIODevice
*vdev
, VirtQueue
*vq
)
681 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
682 VirtIOCryptoReq
*req
;
684 while ((req
= virtio_crypto_get_request(vcrypto
, vq
))) {
685 if (virtio_crypto_handle_request(req
) < 0) {
686 virtqueue_detach_element(req
->vq
, &req
->elem
, 0);
687 virtio_crypto_free_request(req
);
693 static void virtio_crypto_dataq_bh(void *opaque
)
695 VirtIOCryptoQueue
*q
= opaque
;
696 VirtIOCrypto
*vcrypto
= q
->vcrypto
;
697 VirtIODevice
*vdev
= VIRTIO_DEVICE(vcrypto
);
699 /* This happens when device was stopped but BH wasn't. */
700 if (!vdev
->vm_running
) {
704 /* Just in case the driver is not ready on more */
705 if (unlikely(!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))) {
710 virtio_crypto_handle_dataq(vdev
, q
->dataq
);
711 virtio_queue_set_notification(q
->dataq
, 1);
713 /* Are we done or did the guest add more buffers? */
714 if (virtio_queue_empty(q
->dataq
)) {
718 virtio_queue_set_notification(q
->dataq
, 0);
723 virtio_crypto_handle_dataq_bh(VirtIODevice
*vdev
, VirtQueue
*vq
)
725 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
726 VirtIOCryptoQueue
*q
=
727 &vcrypto
->vqs
[virtio_crypto_vq2q(virtio_get_queue_index(vq
))];
729 /* This happens when device was stopped but VCPU wasn't. */
730 if (!vdev
->vm_running
) {
733 virtio_queue_set_notification(vq
, 0);
734 qemu_bh_schedule(q
->dataq_bh
);
737 static uint64_t virtio_crypto_get_features(VirtIODevice
*vdev
,
744 static void virtio_crypto_reset(VirtIODevice
*vdev
)
746 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
747 /* multiqueue is disabled by default */
748 vcrypto
->curr_queues
= 1;
749 if (!cryptodev_backend_is_ready(vcrypto
->cryptodev
)) {
750 vcrypto
->status
&= ~VIRTIO_CRYPTO_S_HW_READY
;
752 vcrypto
->status
|= VIRTIO_CRYPTO_S_HW_READY
;
756 static void virtio_crypto_init_config(VirtIODevice
*vdev
)
758 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
760 vcrypto
->conf
.crypto_services
=
761 vcrypto
->conf
.cryptodev
->conf
.crypto_services
;
762 vcrypto
->conf
.cipher_algo_l
=
763 vcrypto
->conf
.cryptodev
->conf
.cipher_algo_l
;
764 vcrypto
->conf
.cipher_algo_h
=
765 vcrypto
->conf
.cryptodev
->conf
.cipher_algo_h
;
766 vcrypto
->conf
.hash_algo
= vcrypto
->conf
.cryptodev
->conf
.hash_algo
;
767 vcrypto
->conf
.mac_algo_l
= vcrypto
->conf
.cryptodev
->conf
.mac_algo_l
;
768 vcrypto
->conf
.mac_algo_h
= vcrypto
->conf
.cryptodev
->conf
.mac_algo_h
;
769 vcrypto
->conf
.aead_algo
= vcrypto
->conf
.cryptodev
->conf
.aead_algo
;
770 vcrypto
->conf
.max_cipher_key_len
=
771 vcrypto
->conf
.cryptodev
->conf
.max_cipher_key_len
;
772 vcrypto
->conf
.max_auth_key_len
=
773 vcrypto
->conf
.cryptodev
->conf
.max_auth_key_len
;
774 vcrypto
->conf
.max_size
= vcrypto
->conf
.cryptodev
->conf
.max_size
;
777 static void virtio_crypto_device_realize(DeviceState
*dev
, Error
**errp
)
779 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
780 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(dev
);
783 vcrypto
->cryptodev
= vcrypto
->conf
.cryptodev
;
784 if (vcrypto
->cryptodev
== NULL
) {
785 error_setg(errp
, "'cryptodev' parameter expects a valid object");
787 } else if (cryptodev_backend_is_used(vcrypto
->cryptodev
)) {
788 char *path
= object_get_canonical_path_component(OBJECT(vcrypto
->conf
.cryptodev
));
789 error_setg(errp
, "can't use already used cryptodev backend: %s", path
);
794 vcrypto
->max_queues
= MAX(vcrypto
->cryptodev
->conf
.peers
.queues
, 1);
795 if (vcrypto
->max_queues
+ 1 > VIRTIO_QUEUE_MAX
) {
796 error_setg(errp
, "Invalid number of queues (= %" PRIu32
"), "
797 "must be a positive integer less than %d.",
798 vcrypto
->max_queues
, VIRTIO_QUEUE_MAX
);
802 virtio_init(vdev
, "virtio-crypto", VIRTIO_ID_CRYPTO
, vcrypto
->config_size
);
803 vcrypto
->curr_queues
= 1;
804 vcrypto
->vqs
= g_malloc0(sizeof(VirtIOCryptoQueue
) * vcrypto
->max_queues
);
805 for (i
= 0; i
< vcrypto
->max_queues
; i
++) {
806 vcrypto
->vqs
[i
].dataq
=
807 virtio_add_queue(vdev
, 1024, virtio_crypto_handle_dataq_bh
);
808 vcrypto
->vqs
[i
].dataq_bh
=
809 qemu_bh_new(virtio_crypto_dataq_bh
, &vcrypto
->vqs
[i
]);
810 vcrypto
->vqs
[i
].vcrypto
= vcrypto
;
813 vcrypto
->ctrl_vq
= virtio_add_queue(vdev
, 64, virtio_crypto_handle_ctrl
);
814 if (!cryptodev_backend_is_ready(vcrypto
->cryptodev
)) {
815 vcrypto
->status
&= ~VIRTIO_CRYPTO_S_HW_READY
;
817 vcrypto
->status
|= VIRTIO_CRYPTO_S_HW_READY
;
820 virtio_crypto_init_config(vdev
);
821 cryptodev_backend_set_used(vcrypto
->cryptodev
, true);
824 static void virtio_crypto_device_unrealize(DeviceState
*dev
, Error
**errp
)
826 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
827 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(dev
);
828 VirtIOCryptoQueue
*q
;
831 max_queues
= vcrypto
->multiqueue
? vcrypto
->max_queues
: 1;
832 for (i
= 0; i
< max_queues
; i
++) {
833 virtio_del_queue(vdev
, i
);
834 q
= &vcrypto
->vqs
[i
];
835 qemu_bh_delete(q
->dataq_bh
);
838 g_free(vcrypto
->vqs
);
840 virtio_cleanup(vdev
);
841 cryptodev_backend_set_used(vcrypto
->cryptodev
, false);
844 static const VMStateDescription vmstate_virtio_crypto
= {
845 .name
= "virtio-crypto",
847 .minimum_version_id
= VIRTIO_CRYPTO_VM_VERSION
,
848 .version_id
= VIRTIO_CRYPTO_VM_VERSION
,
849 .fields
= (VMStateField
[]) {
850 VMSTATE_VIRTIO_DEVICE
,
851 VMSTATE_END_OF_LIST()
855 static Property virtio_crypto_properties
[] = {
856 DEFINE_PROP_LINK("cryptodev", VirtIOCrypto
, conf
.cryptodev
,
857 TYPE_CRYPTODEV_BACKEND
, CryptoDevBackend
*),
858 DEFINE_PROP_END_OF_LIST(),
861 static void virtio_crypto_get_config(VirtIODevice
*vdev
, uint8_t *config
)
863 VirtIOCrypto
*c
= VIRTIO_CRYPTO(vdev
);
864 struct virtio_crypto_config crypto_cfg
= {};
867 * Virtio-crypto device conforms to VIRTIO 1.0 which is always LE,
868 * so we can use LE accessors directly.
870 stl_le_p(&crypto_cfg
.status
, c
->status
);
871 stl_le_p(&crypto_cfg
.max_dataqueues
, c
->max_queues
);
872 stl_le_p(&crypto_cfg
.crypto_services
, c
->conf
.crypto_services
);
873 stl_le_p(&crypto_cfg
.cipher_algo_l
, c
->conf
.cipher_algo_l
);
874 stl_le_p(&crypto_cfg
.cipher_algo_h
, c
->conf
.cipher_algo_h
);
875 stl_le_p(&crypto_cfg
.hash_algo
, c
->conf
.hash_algo
);
876 stl_le_p(&crypto_cfg
.mac_algo_l
, c
->conf
.mac_algo_l
);
877 stl_le_p(&crypto_cfg
.mac_algo_h
, c
->conf
.mac_algo_h
);
878 stl_le_p(&crypto_cfg
.aead_algo
, c
->conf
.aead_algo
);
879 stl_le_p(&crypto_cfg
.max_cipher_key_len
, c
->conf
.max_cipher_key_len
);
880 stl_le_p(&crypto_cfg
.max_auth_key_len
, c
->conf
.max_auth_key_len
);
881 stq_le_p(&crypto_cfg
.max_size
, c
->conf
.max_size
);
883 memcpy(config
, &crypto_cfg
, c
->config_size
);
886 static bool virtio_crypto_started(VirtIOCrypto
*c
, uint8_t status
)
888 VirtIODevice
*vdev
= VIRTIO_DEVICE(c
);
889 return (status
& VIRTIO_CONFIG_S_DRIVER_OK
) &&
890 (c
->status
& VIRTIO_CRYPTO_S_HW_READY
) && vdev
->vm_running
;
893 static void virtio_crypto_vhost_status(VirtIOCrypto
*c
, uint8_t status
)
895 VirtIODevice
*vdev
= VIRTIO_DEVICE(c
);
896 int queues
= c
->multiqueue
? c
->max_queues
: 1;
897 CryptoDevBackend
*b
= c
->cryptodev
;
898 CryptoDevBackendClient
*cc
= b
->conf
.peers
.ccs
[0];
900 if (!cryptodev_get_vhost(cc
, b
, 0)) {
904 if ((virtio_crypto_started(c
, status
)) == !!c
->vhost_started
) {
908 if (!c
->vhost_started
) {
911 c
->vhost_started
= 1;
912 r
= cryptodev_vhost_start(vdev
, queues
);
914 error_report("unable to start vhost crypto: %d: "
915 "falling back on userspace virtio", -r
);
916 c
->vhost_started
= 0;
919 cryptodev_vhost_stop(vdev
, queues
);
920 c
->vhost_started
= 0;
924 static void virtio_crypto_set_status(VirtIODevice
*vdev
, uint8_t status
)
926 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
928 virtio_crypto_vhost_status(vcrypto
, status
);
931 static void virtio_crypto_guest_notifier_mask(VirtIODevice
*vdev
, int idx
,
934 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
935 int queue
= virtio_crypto_vq2q(idx
);
937 assert(vcrypto
->vhost_started
);
939 cryptodev_vhost_virtqueue_mask(vdev
, queue
, idx
, mask
);
942 static bool virtio_crypto_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
944 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(vdev
);
945 int queue
= virtio_crypto_vq2q(idx
);
947 assert(vcrypto
->vhost_started
);
949 return cryptodev_vhost_virtqueue_pending(vdev
, queue
, idx
);
952 static void virtio_crypto_class_init(ObjectClass
*klass
, void *data
)
954 DeviceClass
*dc
= DEVICE_CLASS(klass
);
955 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
957 dc
->props
= virtio_crypto_properties
;
958 dc
->vmsd
= &vmstate_virtio_crypto
;
959 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
960 vdc
->realize
= virtio_crypto_device_realize
;
961 vdc
->unrealize
= virtio_crypto_device_unrealize
;
962 vdc
->get_config
= virtio_crypto_get_config
;
963 vdc
->get_features
= virtio_crypto_get_features
;
964 vdc
->reset
= virtio_crypto_reset
;
965 vdc
->set_status
= virtio_crypto_set_status
;
966 vdc
->guest_notifier_mask
= virtio_crypto_guest_notifier_mask
;
967 vdc
->guest_notifier_pending
= virtio_crypto_guest_notifier_pending
;
970 static void virtio_crypto_instance_init(Object
*obj
)
972 VirtIOCrypto
*vcrypto
= VIRTIO_CRYPTO(obj
);
975 * The default config_size is sizeof(struct virtio_crypto_config).
976 * Can be overriden with virtio_crypto_set_config_size.
978 vcrypto
->config_size
= sizeof(struct virtio_crypto_config
);
981 static const TypeInfo virtio_crypto_info
= {
982 .name
= TYPE_VIRTIO_CRYPTO
,
983 .parent
= TYPE_VIRTIO_DEVICE
,
984 .instance_size
= sizeof(VirtIOCrypto
),
985 .instance_init
= virtio_crypto_instance_init
,
986 .class_init
= virtio_crypto_class_init
,
989 static void virtio_register_types(void)
991 type_register_static(&virtio_crypto_info
);
994 type_init(virtio_register_types
)