hw: ccid-card-emulated: cleanup resource when realize in error path
[qemu/ar7.git] / backends / cryptodev-vhost-user.c
blobd539f14d59f490e09d5c70cb9875e499ec4f645f
1 /*
2 * QEMU Cryptodev backend for QEMU cipher APIs
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6 * Authors:
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 "hw/boards.h"
26 #include "qapi/error.h"
27 #include "qapi/qmp/qerror.h"
28 #include "qemu/error-report.h"
29 #include "hw/virtio/vhost-user.h"
30 #include "standard-headers/linux/virtio_crypto.h"
31 #include "sysemu/cryptodev-vhost.h"
32 #include "chardev/char-fe.h"
33 #include "sysemu/cryptodev-vhost-user.h"
36 /**
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 #define CRYPTODEV_BACKEND_VHOST_USER(obj) \
43 OBJECT_CHECK(CryptoDevBackendVhostUser, \
44 (obj), TYPE_CRYPTODEV_BACKEND_VHOST_USER)
47 typedef struct CryptoDevBackendVhostUser {
48 CryptoDevBackend parent_obj;
50 VhostUserState *vhost_user;
51 CharBackend chr;
52 char *chr_name;
53 bool opened;
54 CryptoDevBackendVhost *vhost_crypto[MAX_CRYPTO_QUEUE_NUM];
55 } CryptoDevBackendVhostUser;
57 static int
58 cryptodev_vhost_user_running(
59 CryptoDevBackendVhost *crypto)
61 return crypto ? 1 : 0;
64 CryptoDevBackendVhost *
65 cryptodev_vhost_user_get_vhost(
66 CryptoDevBackendClient *cc,
67 CryptoDevBackend *b,
68 uint16_t queue)
70 CryptoDevBackendVhostUser *s =
71 CRYPTODEV_BACKEND_VHOST_USER(b);
72 assert(cc->type == CRYPTODEV_BACKEND_TYPE_VHOST_USER);
73 assert(queue < MAX_CRYPTO_QUEUE_NUM);
75 return s->vhost_crypto[queue];
78 static void cryptodev_vhost_user_stop(int queues,
79 CryptoDevBackendVhostUser *s)
81 size_t i;
83 for (i = 0; i < queues; i++) {
84 if (!cryptodev_vhost_user_running(s->vhost_crypto[i])) {
85 continue;
88 cryptodev_vhost_cleanup(s->vhost_crypto[i]);
89 s->vhost_crypto[i] = NULL;
93 static int
94 cryptodev_vhost_user_start(int queues,
95 CryptoDevBackendVhostUser *s)
97 CryptoDevBackendVhostOptions options;
98 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
99 int max_queues;
100 size_t i;
102 for (i = 0; i < queues; i++) {
103 if (cryptodev_vhost_user_running(s->vhost_crypto[i])) {
104 continue;
107 options.opaque = s->vhost_user;
108 options.backend_type = VHOST_BACKEND_TYPE_USER;
109 options.cc = b->conf.peers.ccs[i];
110 s->vhost_crypto[i] = cryptodev_vhost_init(&options);
111 if (!s->vhost_crypto[i]) {
112 error_report("failed to init vhost_crypto for queue %zu", i);
113 goto err;
116 if (i == 0) {
117 max_queues =
118 cryptodev_vhost_get_max_queues(s->vhost_crypto[i]);
119 if (queues > max_queues) {
120 error_report("you are asking more queues than supported: %d",
121 max_queues);
122 goto err;
127 return 0;
129 err:
130 cryptodev_vhost_user_stop(i + 1, s);
131 return -1;
134 static Chardev *
135 cryptodev_vhost_claim_chardev(CryptoDevBackendVhostUser *s,
136 Error **errp)
138 Chardev *chr;
140 if (s->chr_name == NULL) {
141 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
142 "chardev", "a valid character device");
143 return NULL;
146 chr = qemu_chr_find(s->chr_name);
147 if (chr == NULL) {
148 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
149 "Device '%s' not found", s->chr_name);
150 return NULL;
153 return chr;
156 static void cryptodev_vhost_user_event(void *opaque, int event)
158 CryptoDevBackendVhostUser *s = opaque;
159 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
160 int queues = b->conf.peers.queues;
162 assert(queues < MAX_CRYPTO_QUEUE_NUM);
164 switch (event) {
165 case CHR_EVENT_OPENED:
166 if (cryptodev_vhost_user_start(queues, s) < 0) {
167 exit(1);
169 b->ready = true;
170 break;
171 case CHR_EVENT_CLOSED:
172 b->ready = false;
173 cryptodev_vhost_user_stop(queues, s);
174 break;
178 static void cryptodev_vhost_user_init(
179 CryptoDevBackend *backend, Error **errp)
181 int queues = backend->conf.peers.queues;
182 size_t i;
183 Error *local_err = NULL;
184 Chardev *chr;
185 VhostUserState *user;
186 CryptoDevBackendClient *cc;
187 CryptoDevBackendVhostUser *s =
188 CRYPTODEV_BACKEND_VHOST_USER(backend);
190 chr = cryptodev_vhost_claim_chardev(s, &local_err);
191 if (local_err) {
192 error_propagate(errp, local_err);
193 return;
196 s->opened = true;
198 for (i = 0; i < queues; i++) {
199 cc = cryptodev_backend_new_client(
200 "cryptodev-vhost-user", NULL);
201 cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
202 i, chr->label);
203 cc->queue_index = i;
204 cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
206 backend->conf.peers.ccs[i] = cc;
208 if (i == 0) {
209 if (!qemu_chr_fe_init(&s->chr, chr, &local_err)) {
210 error_propagate(errp, local_err);
211 return;
216 user = vhost_user_init();
217 if (!user) {
218 error_setg(errp, "Failed to init vhost_user");
219 return;
222 user->chr = &s->chr;
223 s->vhost_user = user;
225 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
226 cryptodev_vhost_user_event, NULL, s, NULL, true);
228 backend->conf.crypto_services =
229 1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
230 1u << VIRTIO_CRYPTO_SERVICE_HASH |
231 1u << VIRTIO_CRYPTO_SERVICE_MAC;
232 backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
233 backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
235 backend->conf.max_size = UINT64_MAX;
236 backend->conf.max_cipher_key_len = VHOST_USER_MAX_CIPHER_KEY_LEN;
237 backend->conf.max_auth_key_len = VHOST_USER_MAX_AUTH_KEY_LEN;
240 static int64_t cryptodev_vhost_user_sym_create_session(
241 CryptoDevBackend *backend,
242 CryptoDevBackendSymSessionInfo *sess_info,
243 uint32_t queue_index, Error **errp)
245 CryptoDevBackendClient *cc =
246 backend->conf.peers.ccs[queue_index];
247 CryptoDevBackendVhost *vhost_crypto;
248 uint64_t session_id = 0;
249 int ret;
251 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
252 if (vhost_crypto) {
253 struct vhost_dev *dev = &(vhost_crypto->dev);
254 ret = dev->vhost_ops->vhost_crypto_create_session(dev,
255 sess_info,
256 &session_id);
257 if (ret < 0) {
258 return -1;
259 } else {
260 return session_id;
263 return -1;
266 static int cryptodev_vhost_user_sym_close_session(
267 CryptoDevBackend *backend,
268 uint64_t session_id,
269 uint32_t queue_index, Error **errp)
271 CryptoDevBackendClient *cc =
272 backend->conf.peers.ccs[queue_index];
273 CryptoDevBackendVhost *vhost_crypto;
274 int ret;
276 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
277 if (vhost_crypto) {
278 struct vhost_dev *dev = &(vhost_crypto->dev);
279 ret = dev->vhost_ops->vhost_crypto_close_session(dev,
280 session_id);
281 if (ret < 0) {
282 return -1;
283 } else {
284 return 0;
287 return -1;
290 static void cryptodev_vhost_user_cleanup(
291 CryptoDevBackend *backend,
292 Error **errp)
294 CryptoDevBackendVhostUser *s =
295 CRYPTODEV_BACKEND_VHOST_USER(backend);
296 size_t i;
297 int queues = backend->conf.peers.queues;
298 CryptoDevBackendClient *cc;
300 cryptodev_vhost_user_stop(queues, s);
302 for (i = 0; i < queues; i++) {
303 cc = backend->conf.peers.ccs[i];
304 if (cc) {
305 cryptodev_backend_free_client(cc);
306 backend->conf.peers.ccs[i] = NULL;
310 if (s->vhost_user) {
311 vhost_user_cleanup(s->vhost_user);
312 g_free(s->vhost_user);
313 s->vhost_user = NULL;
317 static void cryptodev_vhost_user_set_chardev(Object *obj,
318 const char *value, Error **errp)
320 CryptoDevBackendVhostUser *s =
321 CRYPTODEV_BACKEND_VHOST_USER(obj);
323 if (s->opened) {
324 error_setg(errp, QERR_PERMISSION_DENIED);
325 } else {
326 g_free(s->chr_name);
327 s->chr_name = g_strdup(value);
331 static char *
332 cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
334 CryptoDevBackendVhostUser *s =
335 CRYPTODEV_BACKEND_VHOST_USER(obj);
336 Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
338 if (chr && chr->label) {
339 return g_strdup(chr->label);
342 return NULL;
345 static void cryptodev_vhost_user_instance_int(Object *obj)
347 object_property_add_str(obj, "chardev",
348 cryptodev_vhost_user_get_chardev,
349 cryptodev_vhost_user_set_chardev,
350 NULL);
353 static void cryptodev_vhost_user_finalize(Object *obj)
355 CryptoDevBackendVhostUser *s =
356 CRYPTODEV_BACKEND_VHOST_USER(obj);
358 qemu_chr_fe_deinit(&s->chr, false);
360 g_free(s->chr_name);
363 static void
364 cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
366 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
368 bc->init = cryptodev_vhost_user_init;
369 bc->cleanup = cryptodev_vhost_user_cleanup;
370 bc->create_session = cryptodev_vhost_user_sym_create_session;
371 bc->close_session = cryptodev_vhost_user_sym_close_session;
372 bc->do_sym_op = NULL;
375 static const TypeInfo cryptodev_vhost_user_info = {
376 .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
377 .parent = TYPE_CRYPTODEV_BACKEND,
378 .class_init = cryptodev_vhost_user_class_init,
379 .instance_init = cryptodev_vhost_user_instance_int,
380 .instance_finalize = cryptodev_vhost_user_finalize,
381 .instance_size = sizeof(CryptoDevBackendVhostUser),
384 static void
385 cryptodev_vhost_user_register_types(void)
387 type_register_static(&cryptodev_vhost_user_info);
390 type_init(cryptodev_vhost_user_register_types);