2 * QEMU Cryptodev backend for QEMU cipher APIs
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
7 * Gonglei <arei.gonglei@huawei.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qapi/qmp/qerror.h"
27 #include "qemu/error-report.h"
28 #include "hw/virtio/vhost-user.h"
29 #include "standard-headers/linux/virtio_crypto.h"
30 #include "sysemu/cryptodev-vhost.h"
31 #include "chardev/char-fe.h"
32 #include "sysemu/cryptodev-vhost-user.h"
33 #include "qom/object.h"
37 * @TYPE_CRYPTODEV_BACKEND_VHOST_USER:
38 * name of backend that uses vhost user server
40 #define TYPE_CRYPTODEV_BACKEND_VHOST_USER "cryptodev-vhost-user"
42 OBJECT_DECLARE_SIMPLE_TYPE(CryptoDevBackendVhostUser
, CRYPTODEV_BACKEND_VHOST_USER
)
45 struct CryptoDevBackendVhostUser
{
46 CryptoDevBackend parent_obj
;
48 VhostUserState vhost_user
;
52 CryptoDevBackendVhost
*vhost_crypto
[MAX_CRYPTO_QUEUE_NUM
];
56 cryptodev_vhost_user_running(
57 CryptoDevBackendVhost
*crypto
)
59 return crypto
? 1 : 0;
62 CryptoDevBackendVhost
*
63 cryptodev_vhost_user_get_vhost(
64 CryptoDevBackendClient
*cc
,
68 CryptoDevBackendVhostUser
*s
=
69 CRYPTODEV_BACKEND_VHOST_USER(b
);
70 assert(cc
->type
== QCRYPTODEV_BACKEND_TYPE_VHOST_USER
);
71 assert(queue
< MAX_CRYPTO_QUEUE_NUM
);
73 return s
->vhost_crypto
[queue
];
76 static void cryptodev_vhost_user_stop(int queues
,
77 CryptoDevBackendVhostUser
*s
)
81 for (i
= 0; i
< queues
; i
++) {
82 if (!cryptodev_vhost_user_running(s
->vhost_crypto
[i
])) {
86 cryptodev_vhost_cleanup(s
->vhost_crypto
[i
]);
87 s
->vhost_crypto
[i
] = NULL
;
92 cryptodev_vhost_user_start(int queues
,
93 CryptoDevBackendVhostUser
*s
)
95 CryptoDevBackendVhostOptions options
;
96 CryptoDevBackend
*b
= CRYPTODEV_BACKEND(s
);
100 for (i
= 0; i
< queues
; i
++) {
101 if (cryptodev_vhost_user_running(s
->vhost_crypto
[i
])) {
105 options
.opaque
= &s
->vhost_user
;
106 options
.backend_type
= VHOST_BACKEND_TYPE_USER
;
107 options
.cc
= b
->conf
.peers
.ccs
[i
];
108 s
->vhost_crypto
[i
] = cryptodev_vhost_init(&options
);
109 if (!s
->vhost_crypto
[i
]) {
110 error_report("failed to init vhost_crypto for queue %zu", i
);
116 cryptodev_vhost_get_max_queues(s
->vhost_crypto
[i
]);
117 if (queues
> max_queues
) {
118 error_report("you are asking more queues than supported: %d",
128 cryptodev_vhost_user_stop(i
+ 1, s
);
133 cryptodev_vhost_claim_chardev(CryptoDevBackendVhostUser
*s
,
138 if (s
->chr_name
== NULL
) {
139 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
140 "chardev", "a valid character device");
144 chr
= qemu_chr_find(s
->chr_name
);
146 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
147 "Device '%s' not found", s
->chr_name
);
154 static void cryptodev_vhost_user_event(void *opaque
, QEMUChrEvent event
)
156 CryptoDevBackendVhostUser
*s
= opaque
;
157 CryptoDevBackend
*b
= CRYPTODEV_BACKEND(s
);
158 int queues
= b
->conf
.peers
.queues
;
160 assert(queues
< MAX_CRYPTO_QUEUE_NUM
);
163 case CHR_EVENT_OPENED
:
164 if (cryptodev_vhost_user_start(queues
, s
) < 0) {
169 case CHR_EVENT_CLOSED
:
171 cryptodev_vhost_user_stop(queues
, s
);
173 case CHR_EVENT_BREAK
:
174 case CHR_EVENT_MUX_IN
:
175 case CHR_EVENT_MUX_OUT
:
181 static void cryptodev_vhost_user_init(
182 CryptoDevBackend
*backend
, Error
**errp
)
184 int queues
= backend
->conf
.peers
.queues
;
186 Error
*local_err
= NULL
;
188 CryptoDevBackendClient
*cc
;
189 CryptoDevBackendVhostUser
*s
=
190 CRYPTODEV_BACKEND_VHOST_USER(backend
);
192 chr
= cryptodev_vhost_claim_chardev(s
, &local_err
);
194 error_propagate(errp
, local_err
);
200 for (i
= 0; i
< queues
; i
++) {
201 cc
= cryptodev_backend_new_client();
202 cc
->info_str
= g_strdup_printf("cryptodev-vhost-user%zu to %s ",
205 cc
->type
= QCRYPTODEV_BACKEND_TYPE_VHOST_USER
;
207 backend
->conf
.peers
.ccs
[i
] = cc
;
210 if (!qemu_chr_fe_init(&s
->chr
, chr
, errp
)) {
216 if (!vhost_user_init(&s
->vhost_user
, &s
->chr
, errp
)) {
220 qemu_chr_fe_set_handlers(&s
->chr
, NULL
, NULL
,
221 cryptodev_vhost_user_event
, NULL
, s
, NULL
, true);
223 backend
->conf
.crypto_services
=
224 1u << QCRYPTODEV_BACKEND_SERVICE_CIPHER
|
225 1u << QCRYPTODEV_BACKEND_SERVICE_HASH
|
226 1u << QCRYPTODEV_BACKEND_SERVICE_MAC
;
227 backend
->conf
.cipher_algo_l
= 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC
;
228 backend
->conf
.hash_algo
= 1u << VIRTIO_CRYPTO_HASH_SHA1
;
230 backend
->conf
.max_size
= UINT64_MAX
;
231 backend
->conf
.max_cipher_key_len
= VHOST_USER_MAX_CIPHER_KEY_LEN
;
232 backend
->conf
.max_auth_key_len
= VHOST_USER_MAX_AUTH_KEY_LEN
;
235 static int64_t cryptodev_vhost_user_sym_create_session(
236 CryptoDevBackend
*backend
,
237 CryptoDevBackendSymSessionInfo
*sess_info
,
238 uint32_t queue_index
, Error
**errp
)
240 CryptoDevBackendClient
*cc
=
241 backend
->conf
.peers
.ccs
[queue_index
];
242 CryptoDevBackendVhost
*vhost_crypto
;
243 uint64_t session_id
= 0;
246 vhost_crypto
= cryptodev_vhost_user_get_vhost(cc
, backend
, queue_index
);
248 struct vhost_dev
*dev
= &(vhost_crypto
->dev
);
249 ret
= dev
->vhost_ops
->vhost_crypto_create_session(dev
,
261 static int cryptodev_vhost_user_create_session(
262 CryptoDevBackend
*backend
,
263 CryptoDevBackendSessionInfo
*sess_info
,
264 uint32_t queue_index
,
265 CryptoDevCompletionFunc cb
,
268 uint32_t op_code
= sess_info
->op_code
;
269 CryptoDevBackendSymSessionInfo
*sym_sess_info
;
271 Error
*local_error
= NULL
;
275 case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION
:
276 case VIRTIO_CRYPTO_HASH_CREATE_SESSION
:
277 case VIRTIO_CRYPTO_MAC_CREATE_SESSION
:
278 case VIRTIO_CRYPTO_AEAD_CREATE_SESSION
:
279 sym_sess_info
= &sess_info
->u
.sym_sess_info
;
280 ret
= cryptodev_vhost_user_sym_create_session(backend
, sym_sess_info
,
281 queue_index
, &local_error
);
285 error_setg(&local_error
, "Unsupported opcode :%" PRIu32
"",
287 return -VIRTIO_CRYPTO_NOTSUPP
;
291 error_report_err(local_error
);
294 status
= -VIRTIO_CRYPTO_ERR
;
296 sess_info
->session_id
= ret
;
297 status
= VIRTIO_CRYPTO_OK
;
305 static int cryptodev_vhost_user_close_session(
306 CryptoDevBackend
*backend
,
308 uint32_t queue_index
,
309 CryptoDevCompletionFunc cb
,
312 CryptoDevBackendClient
*cc
=
313 backend
->conf
.peers
.ccs
[queue_index
];
314 CryptoDevBackendVhost
*vhost_crypto
;
315 int ret
= -1, status
;
317 vhost_crypto
= cryptodev_vhost_user_get_vhost(cc
, backend
, queue_index
);
319 struct vhost_dev
*dev
= &(vhost_crypto
->dev
);
320 ret
= dev
->vhost_ops
->vhost_crypto_close_session(dev
,
323 status
= -VIRTIO_CRYPTO_ERR
;
325 status
= VIRTIO_CRYPTO_OK
;
328 status
= -VIRTIO_CRYPTO_NOTSUPP
;
336 static void cryptodev_vhost_user_cleanup(
337 CryptoDevBackend
*backend
,
340 CryptoDevBackendVhostUser
*s
=
341 CRYPTODEV_BACKEND_VHOST_USER(backend
);
343 int queues
= backend
->conf
.peers
.queues
;
344 CryptoDevBackendClient
*cc
;
346 cryptodev_vhost_user_stop(queues
, s
);
348 for (i
= 0; i
< queues
; i
++) {
349 cc
= backend
->conf
.peers
.ccs
[i
];
351 cryptodev_backend_free_client(cc
);
352 backend
->conf
.peers
.ccs
[i
] = NULL
;
356 vhost_user_cleanup(&s
->vhost_user
);
359 static void cryptodev_vhost_user_set_chardev(Object
*obj
,
360 const char *value
, Error
**errp
)
362 CryptoDevBackendVhostUser
*s
=
363 CRYPTODEV_BACKEND_VHOST_USER(obj
);
366 error_setg(errp
, "Property 'chardev' can no longer be set");
369 s
->chr_name
= g_strdup(value
);
374 cryptodev_vhost_user_get_chardev(Object
*obj
, Error
**errp
)
376 CryptoDevBackendVhostUser
*s
=
377 CRYPTODEV_BACKEND_VHOST_USER(obj
);
378 Chardev
*chr
= qemu_chr_fe_get_driver(&s
->chr
);
380 if (chr
&& chr
->label
) {
381 return g_strdup(chr
->label
);
387 static void cryptodev_vhost_user_finalize(Object
*obj
)
389 CryptoDevBackendVhostUser
*s
=
390 CRYPTODEV_BACKEND_VHOST_USER(obj
);
392 qemu_chr_fe_deinit(&s
->chr
, false);
398 cryptodev_vhost_user_class_init(ObjectClass
*oc
, void *data
)
400 CryptoDevBackendClass
*bc
= CRYPTODEV_BACKEND_CLASS(oc
);
402 bc
->init
= cryptodev_vhost_user_init
;
403 bc
->cleanup
= cryptodev_vhost_user_cleanup
;
404 bc
->create_session
= cryptodev_vhost_user_create_session
;
405 bc
->close_session
= cryptodev_vhost_user_close_session
;
408 object_class_property_add_str(oc
, "chardev",
409 cryptodev_vhost_user_get_chardev
,
410 cryptodev_vhost_user_set_chardev
);
414 static const TypeInfo cryptodev_vhost_user_info
= {
415 .name
= TYPE_CRYPTODEV_BACKEND_VHOST_USER
,
416 .parent
= TYPE_CRYPTODEV_BACKEND
,
417 .class_init
= cryptodev_vhost_user_class_init
,
418 .instance_finalize
= cryptodev_vhost_user_finalize
,
419 .instance_size
= sizeof(CryptoDevBackendVhostUser
),
423 cryptodev_vhost_user_register_types(void)
425 type_register_static(&cryptodev_vhost_user_info
);
428 type_init(cryptodev_vhost_user_register_types
);