ui: add trace events related to VNC client throttling
[qemu/ar7.git] / backends / cryptodev.c
blob67edfa53289bc8966c3aadf11169174563973051
1 /*
2 * QEMU Crypto Device Implementation
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 "sysemu/cryptodev.h"
26 #include "hw/boards.h"
27 #include "qapi/error.h"
28 #include "qapi/visitor.h"
29 #include "qapi-types.h"
30 #include "qapi-visit.h"
31 #include "qemu/config-file.h"
32 #include "qom/object_interfaces.h"
33 #include "hw/virtio/virtio-crypto.h"
36 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
39 CryptoDevBackendClient *
40 cryptodev_backend_new_client(const char *model,
41 const char *name)
43 CryptoDevBackendClient *cc;
45 cc = g_malloc0(sizeof(CryptoDevBackendClient));
46 cc->model = g_strdup(model);
47 if (name) {
48 cc->name = g_strdup(name);
51 QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
53 return cc;
56 void cryptodev_backend_free_client(
57 CryptoDevBackendClient *cc)
59 QTAILQ_REMOVE(&crypto_clients, cc, next);
60 g_free(cc->name);
61 g_free(cc->model);
62 g_free(cc->info_str);
63 g_free(cc);
66 void cryptodev_backend_cleanup(
67 CryptoDevBackend *backend,
68 Error **errp)
70 CryptoDevBackendClass *bc =
71 CRYPTODEV_BACKEND_GET_CLASS(backend);
73 if (bc->cleanup) {
74 bc->cleanup(backend, errp);
78 int64_t cryptodev_backend_sym_create_session(
79 CryptoDevBackend *backend,
80 CryptoDevBackendSymSessionInfo *sess_info,
81 uint32_t queue_index, Error **errp)
83 CryptoDevBackendClass *bc =
84 CRYPTODEV_BACKEND_GET_CLASS(backend);
86 if (bc->create_session) {
87 return bc->create_session(backend, sess_info, queue_index, errp);
90 return -1;
93 int cryptodev_backend_sym_close_session(
94 CryptoDevBackend *backend,
95 uint64_t session_id,
96 uint32_t queue_index, Error **errp)
98 CryptoDevBackendClass *bc =
99 CRYPTODEV_BACKEND_GET_CLASS(backend);
101 if (bc->close_session) {
102 return bc->close_session(backend, session_id, queue_index, errp);
105 return -1;
108 static int cryptodev_backend_sym_operation(
109 CryptoDevBackend *backend,
110 CryptoDevBackendSymOpInfo *op_info,
111 uint32_t queue_index, Error **errp)
113 CryptoDevBackendClass *bc =
114 CRYPTODEV_BACKEND_GET_CLASS(backend);
116 if (bc->do_sym_op) {
117 return bc->do_sym_op(backend, op_info, queue_index, errp);
120 return -VIRTIO_CRYPTO_ERR;
123 int cryptodev_backend_crypto_operation(
124 CryptoDevBackend *backend,
125 void *opaque,
126 uint32_t queue_index, Error **errp)
128 VirtIOCryptoReq *req = opaque;
130 if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
131 CryptoDevBackendSymOpInfo *op_info;
132 op_info = req->u.sym_op_info;
134 return cryptodev_backend_sym_operation(backend,
135 op_info, queue_index, errp);
136 } else {
137 error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
138 req->flags);
139 return -VIRTIO_CRYPTO_NOTSUPP;
142 return -VIRTIO_CRYPTO_ERR;
145 static void
146 cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
147 void *opaque, Error **errp)
149 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
150 uint32_t value = backend->conf.peers.queues;
152 visit_type_uint32(v, name, &value, errp);
155 static void
156 cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
157 void *opaque, Error **errp)
159 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
160 Error *local_err = NULL;
161 uint32_t value;
163 visit_type_uint32(v, name, &value, &local_err);
164 if (local_err) {
165 goto out;
167 if (!value) {
168 error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
169 PRIu32 "'", object_get_typename(obj), name, value);
170 goto out;
172 backend->conf.peers.queues = value;
173 out:
174 error_propagate(errp, local_err);
177 static void
178 cryptodev_backend_complete(UserCreatable *uc, Error **errp)
180 CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
181 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
182 Error *local_err = NULL;
184 if (bc->init) {
185 bc->init(backend, &local_err);
186 if (local_err) {
187 goto out;
191 return;
193 out:
194 error_propagate(errp, local_err);
197 void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
199 backend->is_used = used;
202 bool cryptodev_backend_is_used(CryptoDevBackend *backend)
204 return backend->is_used;
207 void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
209 backend->ready = ready;
212 bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
214 return backend->ready;
217 static bool
218 cryptodev_backend_can_be_deleted(UserCreatable *uc)
220 return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
223 static void cryptodev_backend_instance_init(Object *obj)
225 object_property_add(obj, "queues", "uint32",
226 cryptodev_backend_get_queues,
227 cryptodev_backend_set_queues,
228 NULL, NULL, NULL);
229 /* Initialize devices' queues property to 1 */
230 object_property_set_int(obj, 1, "queues", NULL);
233 static void cryptodev_backend_finalize(Object *obj)
235 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
237 cryptodev_backend_cleanup(backend, NULL);
240 static void
241 cryptodev_backend_class_init(ObjectClass *oc, void *data)
243 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
245 ucc->complete = cryptodev_backend_complete;
246 ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
248 QTAILQ_INIT(&crypto_clients);
251 static const TypeInfo cryptodev_backend_info = {
252 .name = TYPE_CRYPTODEV_BACKEND,
253 .parent = TYPE_OBJECT,
254 .instance_size = sizeof(CryptoDevBackend),
255 .instance_init = cryptodev_backend_instance_init,
256 .instance_finalize = cryptodev_backend_finalize,
257 .class_size = sizeof(CryptoDevBackendClass),
258 .class_init = cryptodev_backend_class_init,
259 .interfaces = (InterfaceInfo[]) {
260 { TYPE_USER_CREATABLE },
265 static void
266 cryptodev_backend_register_types(void)
268 type_register_static(&cryptodev_backend_info);
271 type_init(cryptodev_backend_register_types);