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 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"
36 * @TYPE_CRYPTODEV_BACKEND_VHOST_USER:
37 * name of backend that uses vhost user server
39 #define TYPE_CRYPTODEV_BACKEND_VHOST_USER "cryptodev-vhost-user"
41 #define CRYPTODEV_BACKEND_VHOST_USER(obj) \
42 OBJECT_CHECK(CryptoDevBackendVhostUser, \
43 (obj), TYPE_CRYPTODEV_BACKEND_VHOST_USER)
46 typedef struct CryptoDevBackendVhostUser
{
47 CryptoDevBackend parent_obj
;
49 VhostUserState vhost_user
;
53 CryptoDevBackendVhost
*vhost_crypto
[MAX_CRYPTO_QUEUE_NUM
];
54 } CryptoDevBackendVhostUser
;
57 cryptodev_vhost_user_running(
58 CryptoDevBackendVhost
*crypto
)
60 return crypto
? 1 : 0;
63 CryptoDevBackendVhost
*
64 cryptodev_vhost_user_get_vhost(
65 CryptoDevBackendClient
*cc
,
69 CryptoDevBackendVhostUser
*s
=
70 CRYPTODEV_BACKEND_VHOST_USER(b
);
71 assert(cc
->type
== CRYPTODEV_BACKEND_TYPE_VHOST_USER
);
72 assert(queue
< MAX_CRYPTO_QUEUE_NUM
);
74 return s
->vhost_crypto
[queue
];
77 static void cryptodev_vhost_user_stop(int queues
,
78 CryptoDevBackendVhostUser
*s
)
82 for (i
= 0; i
< queues
; i
++) {
83 if (!cryptodev_vhost_user_running(s
->vhost_crypto
[i
])) {
87 cryptodev_vhost_cleanup(s
->vhost_crypto
[i
]);
88 s
->vhost_crypto
[i
] = NULL
;
93 cryptodev_vhost_user_start(int queues
,
94 CryptoDevBackendVhostUser
*s
)
96 CryptoDevBackendVhostOptions options
;
97 CryptoDevBackend
*b
= CRYPTODEV_BACKEND(s
);
101 for (i
= 0; i
< queues
; i
++) {
102 if (cryptodev_vhost_user_running(s
->vhost_crypto
[i
])) {
106 options
.opaque
= &s
->vhost_user
;
107 options
.backend_type
= VHOST_BACKEND_TYPE_USER
;
108 options
.cc
= b
->conf
.peers
.ccs
[i
];
109 s
->vhost_crypto
[i
] = cryptodev_vhost_init(&options
);
110 if (!s
->vhost_crypto
[i
]) {
111 error_report("failed to init vhost_crypto for queue %zu", i
);
117 cryptodev_vhost_get_max_queues(s
->vhost_crypto
[i
]);
118 if (queues
> max_queues
) {
119 error_report("you are asking more queues than supported: %d",
129 cryptodev_vhost_user_stop(i
+ 1, s
);
134 cryptodev_vhost_claim_chardev(CryptoDevBackendVhostUser
*s
,
139 if (s
->chr_name
== NULL
) {
140 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
141 "chardev", "a valid character device");
145 chr
= qemu_chr_find(s
->chr_name
);
147 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
148 "Device '%s' not found", s
->chr_name
);
155 static void cryptodev_vhost_user_event(void *opaque
, QEMUChrEvent event
)
157 CryptoDevBackendVhostUser
*s
= opaque
;
158 CryptoDevBackend
*b
= CRYPTODEV_BACKEND(s
);
159 int queues
= b
->conf
.peers
.queues
;
161 assert(queues
< MAX_CRYPTO_QUEUE_NUM
);
164 case CHR_EVENT_OPENED
:
165 if (cryptodev_vhost_user_start(queues
, s
) < 0) {
170 case CHR_EVENT_CLOSED
:
172 cryptodev_vhost_user_stop(queues
, s
);
174 case CHR_EVENT_BREAK
:
175 case CHR_EVENT_MUX_IN
:
176 case CHR_EVENT_MUX_OUT
:
182 static void cryptodev_vhost_user_init(
183 CryptoDevBackend
*backend
, Error
**errp
)
185 int queues
= backend
->conf
.peers
.queues
;
187 Error
*local_err
= NULL
;
189 CryptoDevBackendClient
*cc
;
190 CryptoDevBackendVhostUser
*s
=
191 CRYPTODEV_BACKEND_VHOST_USER(backend
);
193 chr
= cryptodev_vhost_claim_chardev(s
, &local_err
);
195 error_propagate(errp
, local_err
);
201 for (i
= 0; i
< queues
; i
++) {
202 cc
= cryptodev_backend_new_client(
203 "cryptodev-vhost-user", NULL
);
204 cc
->info_str
= g_strdup_printf("cryptodev-vhost-user%zu to %s ",
207 cc
->type
= CRYPTODEV_BACKEND_TYPE_VHOST_USER
;
209 backend
->conf
.peers
.ccs
[i
] = cc
;
212 if (!qemu_chr_fe_init(&s
->chr
, chr
, &local_err
)) {
213 error_propagate(errp
, local_err
);
219 if (!vhost_user_init(&s
->vhost_user
, &s
->chr
, errp
)) {
223 qemu_chr_fe_set_handlers(&s
->chr
, NULL
, NULL
,
224 cryptodev_vhost_user_event
, NULL
, s
, NULL
, true);
226 backend
->conf
.crypto_services
=
227 1u << VIRTIO_CRYPTO_SERVICE_CIPHER
|
228 1u << VIRTIO_CRYPTO_SERVICE_HASH
|
229 1u << VIRTIO_CRYPTO_SERVICE_MAC
;
230 backend
->conf
.cipher_algo_l
= 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC
;
231 backend
->conf
.hash_algo
= 1u << VIRTIO_CRYPTO_HASH_SHA1
;
233 backend
->conf
.max_size
= UINT64_MAX
;
234 backend
->conf
.max_cipher_key_len
= VHOST_USER_MAX_CIPHER_KEY_LEN
;
235 backend
->conf
.max_auth_key_len
= VHOST_USER_MAX_AUTH_KEY_LEN
;
238 static int64_t cryptodev_vhost_user_sym_create_session(
239 CryptoDevBackend
*backend
,
240 CryptoDevBackendSymSessionInfo
*sess_info
,
241 uint32_t queue_index
, Error
**errp
)
243 CryptoDevBackendClient
*cc
=
244 backend
->conf
.peers
.ccs
[queue_index
];
245 CryptoDevBackendVhost
*vhost_crypto
;
246 uint64_t session_id
= 0;
249 vhost_crypto
= cryptodev_vhost_user_get_vhost(cc
, backend
, queue_index
);
251 struct vhost_dev
*dev
= &(vhost_crypto
->dev
);
252 ret
= dev
->vhost_ops
->vhost_crypto_create_session(dev
,
264 static int cryptodev_vhost_user_sym_close_session(
265 CryptoDevBackend
*backend
,
267 uint32_t queue_index
, Error
**errp
)
269 CryptoDevBackendClient
*cc
=
270 backend
->conf
.peers
.ccs
[queue_index
];
271 CryptoDevBackendVhost
*vhost_crypto
;
274 vhost_crypto
= cryptodev_vhost_user_get_vhost(cc
, backend
, queue_index
);
276 struct vhost_dev
*dev
= &(vhost_crypto
->dev
);
277 ret
= dev
->vhost_ops
->vhost_crypto_close_session(dev
,
288 static void cryptodev_vhost_user_cleanup(
289 CryptoDevBackend
*backend
,
292 CryptoDevBackendVhostUser
*s
=
293 CRYPTODEV_BACKEND_VHOST_USER(backend
);
295 int queues
= backend
->conf
.peers
.queues
;
296 CryptoDevBackendClient
*cc
;
298 cryptodev_vhost_user_stop(queues
, s
);
300 for (i
= 0; i
< queues
; i
++) {
301 cc
= backend
->conf
.peers
.ccs
[i
];
303 cryptodev_backend_free_client(cc
);
304 backend
->conf
.peers
.ccs
[i
] = NULL
;
308 vhost_user_cleanup(&s
->vhost_user
);
311 static void cryptodev_vhost_user_set_chardev(Object
*obj
,
312 const char *value
, Error
**errp
)
314 CryptoDevBackendVhostUser
*s
=
315 CRYPTODEV_BACKEND_VHOST_USER(obj
);
318 error_setg(errp
, QERR_PERMISSION_DENIED
);
321 s
->chr_name
= g_strdup(value
);
326 cryptodev_vhost_user_get_chardev(Object
*obj
, Error
**errp
)
328 CryptoDevBackendVhostUser
*s
=
329 CRYPTODEV_BACKEND_VHOST_USER(obj
);
330 Chardev
*chr
= qemu_chr_fe_get_driver(&s
->chr
);
332 if (chr
&& chr
->label
) {
333 return g_strdup(chr
->label
);
339 static void cryptodev_vhost_user_instance_int(Object
*obj
)
341 object_property_add_str(obj
, "chardev",
342 cryptodev_vhost_user_get_chardev
,
343 cryptodev_vhost_user_set_chardev
);
346 static void cryptodev_vhost_user_finalize(Object
*obj
)
348 CryptoDevBackendVhostUser
*s
=
349 CRYPTODEV_BACKEND_VHOST_USER(obj
);
351 qemu_chr_fe_deinit(&s
->chr
, false);
357 cryptodev_vhost_user_class_init(ObjectClass
*oc
, void *data
)
359 CryptoDevBackendClass
*bc
= CRYPTODEV_BACKEND_CLASS(oc
);
361 bc
->init
= cryptodev_vhost_user_init
;
362 bc
->cleanup
= cryptodev_vhost_user_cleanup
;
363 bc
->create_session
= cryptodev_vhost_user_sym_create_session
;
364 bc
->close_session
= cryptodev_vhost_user_sym_close_session
;
365 bc
->do_sym_op
= NULL
;
368 static const TypeInfo cryptodev_vhost_user_info
= {
369 .name
= TYPE_CRYPTODEV_BACKEND_VHOST_USER
,
370 .parent
= TYPE_CRYPTODEV_BACKEND
,
371 .class_init
= cryptodev_vhost_user_class_init
,
372 .instance_init
= cryptodev_vhost_user_instance_int
,
373 .instance_finalize
= cryptodev_vhost_user_finalize
,
374 .instance_size
= sizeof(CryptoDevBackendVhostUser
),
378 cryptodev_vhost_user_register_types(void)
380 type_register_static(&cryptodev_vhost_user_info
);
383 type_init(cryptodev_vhost_user_register_types
);