]> Git Repo - qemu.git/blob - backends/cryptodev.c
cryptodev: add vhost-user as a new cryptodev backend
[qemu.git] / backends / cryptodev.c
1 /*
2  * QEMU Crypto Device Implementation
3  *
4  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5  *
6  * Authors:
7  *    Gonglei <[email protected]>
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  */
23
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-visit.h"
30 #include "qemu/config-file.h"
31 #include "qom/object_interfaces.h"
32 #include "hw/virtio/virtio-crypto.h"
33
34
35 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
36
37
38 CryptoDevBackendClient *
39 cryptodev_backend_new_client(const char *model,
40                                     const char *name)
41 {
42     CryptoDevBackendClient *cc;
43
44     cc = g_malloc0(sizeof(CryptoDevBackendClient));
45     cc->model = g_strdup(model);
46     if (name) {
47         cc->name = g_strdup(name);
48     }
49
50     QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
51
52     return cc;
53 }
54
55 void cryptodev_backend_free_client(
56                   CryptoDevBackendClient *cc)
57 {
58     QTAILQ_REMOVE(&crypto_clients, cc, next);
59     g_free(cc->name);
60     g_free(cc->model);
61     g_free(cc->info_str);
62     g_free(cc);
63 }
64
65 void cryptodev_backend_cleanup(
66              CryptoDevBackend *backend,
67              Error **errp)
68 {
69     CryptoDevBackendClass *bc =
70                   CRYPTODEV_BACKEND_GET_CLASS(backend);
71
72     if (bc->cleanup) {
73         bc->cleanup(backend, errp);
74     }
75 }
76
77 int64_t cryptodev_backend_sym_create_session(
78            CryptoDevBackend *backend,
79            CryptoDevBackendSymSessionInfo *sess_info,
80            uint32_t queue_index, Error **errp)
81 {
82     CryptoDevBackendClass *bc =
83                       CRYPTODEV_BACKEND_GET_CLASS(backend);
84
85     if (bc->create_session) {
86         return bc->create_session(backend, sess_info, queue_index, errp);
87     }
88
89     return -1;
90 }
91
92 int cryptodev_backend_sym_close_session(
93            CryptoDevBackend *backend,
94            uint64_t session_id,
95            uint32_t queue_index, Error **errp)
96 {
97     CryptoDevBackendClass *bc =
98                       CRYPTODEV_BACKEND_GET_CLASS(backend);
99
100     if (bc->close_session) {
101         return bc->close_session(backend, session_id, queue_index, errp);
102     }
103
104     return -1;
105 }
106
107 static int cryptodev_backend_sym_operation(
108                  CryptoDevBackend *backend,
109                  CryptoDevBackendSymOpInfo *op_info,
110                  uint32_t queue_index, Error **errp)
111 {
112     CryptoDevBackendClass *bc =
113                       CRYPTODEV_BACKEND_GET_CLASS(backend);
114
115     if (bc->do_sym_op) {
116         return bc->do_sym_op(backend, op_info, queue_index, errp);
117     }
118
119     return -VIRTIO_CRYPTO_ERR;
120 }
121
122 int cryptodev_backend_crypto_operation(
123                  CryptoDevBackend *backend,
124                  void *opaque,
125                  uint32_t queue_index, Error **errp)
126 {
127     VirtIOCryptoReq *req = opaque;
128
129     if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
130         CryptoDevBackendSymOpInfo *op_info;
131         op_info = req->u.sym_op_info;
132
133         return cryptodev_backend_sym_operation(backend,
134                          op_info, queue_index, errp);
135     } else {
136         error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
137                    req->flags);
138        return -VIRTIO_CRYPTO_NOTSUPP;
139     }
140
141     return -VIRTIO_CRYPTO_ERR;
142 }
143
144 static void
145 cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
146                              void *opaque, Error **errp)
147 {
148     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
149     uint32_t value = backend->conf.peers.queues;
150
151     visit_type_uint32(v, name, &value, errp);
152 }
153
154 static void
155 cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
156                              void *opaque, Error **errp)
157 {
158     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
159     Error *local_err = NULL;
160     uint32_t value;
161
162     visit_type_uint32(v, name, &value, &local_err);
163     if (local_err) {
164         goto out;
165     }
166     if (!value) {
167         error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
168                    PRIu32 "'", object_get_typename(obj), name, value);
169         goto out;
170     }
171     backend->conf.peers.queues = value;
172 out:
173     error_propagate(errp, local_err);
174 }
175
176 static void
177 cryptodev_backend_complete(UserCreatable *uc, Error **errp)
178 {
179     CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
180     CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
181     Error *local_err = NULL;
182
183     if (bc->init) {
184         bc->init(backend, &local_err);
185         if (local_err) {
186             goto out;
187         }
188     }
189
190     return;
191
192 out:
193     error_propagate(errp, local_err);
194 }
195
196 void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
197 {
198     backend->is_used = used;
199 }
200
201 bool cryptodev_backend_is_used(CryptoDevBackend *backend)
202 {
203     return backend->is_used;
204 }
205
206 void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
207 {
208     backend->ready = ready;
209 }
210
211 bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
212 {
213     return backend->ready;
214 }
215
216 static bool
217 cryptodev_backend_can_be_deleted(UserCreatable *uc)
218 {
219     return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
220 }
221
222 static void cryptodev_backend_instance_init(Object *obj)
223 {
224     object_property_add(obj, "queues", "uint32",
225                           cryptodev_backend_get_queues,
226                           cryptodev_backend_set_queues,
227                           NULL, NULL, NULL);
228     /* Initialize devices' queues property to 1 */
229     object_property_set_int(obj, 1, "queues", NULL);
230 }
231
232 static void cryptodev_backend_finalize(Object *obj)
233 {
234     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
235
236     cryptodev_backend_cleanup(backend, NULL);
237 }
238
239 static void
240 cryptodev_backend_class_init(ObjectClass *oc, void *data)
241 {
242     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
243
244     ucc->complete = cryptodev_backend_complete;
245     ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
246
247     QTAILQ_INIT(&crypto_clients);
248 }
249
250 static const TypeInfo cryptodev_backend_info = {
251     .name = TYPE_CRYPTODEV_BACKEND,
252     .parent = TYPE_OBJECT,
253     .instance_size = sizeof(CryptoDevBackend),
254     .instance_init = cryptodev_backend_instance_init,
255     .instance_finalize = cryptodev_backend_finalize,
256     .class_size = sizeof(CryptoDevBackendClass),
257     .class_init = cryptodev_backend_class_init,
258     .interfaces = (InterfaceInfo[]) {
259         { TYPE_USER_CREATABLE },
260         { }
261     }
262 };
263
264 static void
265 cryptodev_backend_register_types(void)
266 {
267     type_register_static(&cryptodev_backend_info);
268 }
269
270 type_init(cryptodev_backend_register_types);
This page took 0.038083 seconds and 4 git commands to generate.