]>
Commit | Line | Data |
---|---|---|
d0ee7a13 GA |
1 | /* |
2 | * QEMU Crypto Device Implementation | |
3 | * | |
4 | * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. | |
5 | * | |
6 | * Authors: | |
7 | * Gonglei <arei.gonglei@huawei.com> | |
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 | |
0dda001b | 12 | * version 2.1 of the License, or (at your option) any later version. |
d0ee7a13 GA |
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" | |
d0ee7a13 | 26 | #include "qapi/error.h" |
5dcb0198 | 27 | #include "qapi/qapi-commands-cryptodev.h" |
d0ee7a13 | 28 | #include "qapi/visitor.h" |
d0ee7a13 | 29 | #include "qemu/config-file.h" |
2fda101d | 30 | #include "qemu/error-report.h" |
d0ee7a13 | 31 | #include "qom/object_interfaces.h" |
d6634ac0 GA |
32 | #include "hw/virtio/virtio-crypto.h" |
33 | ||
d0ee7a13 GA |
34 | |
35 | static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients; | |
36 | ||
5dcb0198 ZP |
37 | static int qmp_query_cryptodev_foreach(Object *obj, void *data) |
38 | { | |
39 | CryptoDevBackend *backend; | |
40 | QCryptodevInfoList **infolist = data; | |
41 | uint32_t services, i; | |
42 | ||
43 | if (!object_dynamic_cast(obj, TYPE_CRYPTODEV_BACKEND)) { | |
44 | return 0; | |
45 | } | |
46 | ||
47 | QCryptodevInfo *info = g_new0(QCryptodevInfo, 1); | |
48 | info->id = g_strdup(object_get_canonical_path_component(obj)); | |
49 | ||
50 | backend = CRYPTODEV_BACKEND(obj); | |
51 | services = backend->conf.crypto_services; | |
52 | for (i = 0; i < QCRYPTODEV_BACKEND_SERVICE__MAX; i++) { | |
53 | if (services & (1 << i)) { | |
54 | QAPI_LIST_PREPEND(info->service, i); | |
55 | } | |
56 | } | |
57 | ||
58 | for (i = 0; i < backend->conf.peers.queues; i++) { | |
59 | CryptoDevBackendClient *cc = backend->conf.peers.ccs[i]; | |
60 | QCryptodevBackendClient *client = g_new0(QCryptodevBackendClient, 1); | |
61 | ||
62 | client->queue = cc->queue_index; | |
63 | client->type = cc->type; | |
64 | QAPI_LIST_PREPEND(info->client, client); | |
65 | } | |
66 | ||
67 | QAPI_LIST_PREPEND(*infolist, info); | |
68 | ||
69 | return 0; | |
70 | } | |
71 | ||
72 | QCryptodevInfoList *qmp_query_cryptodev(Error **errp) | |
73 | { | |
74 | QCryptodevInfoList *list = NULL; | |
75 | Object *objs = container_get(object_get_root(), "/objects"); | |
76 | ||
77 | object_child_foreach(objs, qmp_query_cryptodev_foreach, &list); | |
78 | ||
79 | return list; | |
80 | } | |
d0ee7a13 | 81 | |
3f478371 | 82 | CryptoDevBackendClient *cryptodev_backend_new_client(void) |
d0ee7a13 GA |
83 | { |
84 | CryptoDevBackendClient *cc; | |
85 | ||
b21e2380 | 86 | cc = g_new0(CryptoDevBackendClient, 1); |
d0ee7a13 GA |
87 | QTAILQ_INSERT_TAIL(&crypto_clients, cc, next); |
88 | ||
89 | return cc; | |
90 | } | |
91 | ||
92 | void cryptodev_backend_free_client( | |
93 | CryptoDevBackendClient *cc) | |
94 | { | |
95 | QTAILQ_REMOVE(&crypto_clients, cc, next); | |
d0ee7a13 GA |
96 | g_free(cc->info_str); |
97 | g_free(cc); | |
98 | } | |
99 | ||
100 | void cryptodev_backend_cleanup( | |
101 | CryptoDevBackend *backend, | |
102 | Error **errp) | |
103 | { | |
104 | CryptoDevBackendClass *bc = | |
105 | CRYPTODEV_BACKEND_GET_CLASS(backend); | |
106 | ||
107 | if (bc->cleanup) { | |
108 | bc->cleanup(backend, errp); | |
109 | } | |
d0ee7a13 GA |
110 | } |
111 | ||
2fda101d | 112 | int cryptodev_backend_create_session( |
9e4f86a8 | 113 | CryptoDevBackend *backend, |
0e660a6f | 114 | CryptoDevBackendSessionInfo *sess_info, |
2fda101d LH |
115 | uint32_t queue_index, |
116 | CryptoDevCompletionFunc cb, | |
117 | void *opaque) | |
9e4f86a8 GA |
118 | { |
119 | CryptoDevBackendClass *bc = | |
120 | CRYPTODEV_BACKEND_GET_CLASS(backend); | |
121 | ||
122 | if (bc->create_session) { | |
2fda101d | 123 | return bc->create_session(backend, sess_info, queue_index, cb, opaque); |
9e4f86a8 | 124 | } |
2fda101d | 125 | return -VIRTIO_CRYPTO_NOTSUPP; |
9e4f86a8 GA |
126 | } |
127 | ||
0e660a6f | 128 | int cryptodev_backend_close_session( |
9e4f86a8 GA |
129 | CryptoDevBackend *backend, |
130 | uint64_t session_id, | |
2fda101d LH |
131 | uint32_t queue_index, |
132 | CryptoDevCompletionFunc cb, | |
133 | void *opaque) | |
9e4f86a8 GA |
134 | { |
135 | CryptoDevBackendClass *bc = | |
136 | CRYPTODEV_BACKEND_GET_CLASS(backend); | |
137 | ||
138 | if (bc->close_session) { | |
2fda101d | 139 | return bc->close_session(backend, session_id, queue_index, cb, opaque); |
9e4f86a8 | 140 | } |
2fda101d | 141 | return -VIRTIO_CRYPTO_NOTSUPP; |
9e4f86a8 GA |
142 | } |
143 | ||
0e660a6f | 144 | static int cryptodev_backend_operation( |
9e4f86a8 | 145 | CryptoDevBackend *backend, |
2cb06927 | 146 | CryptoDevBackendOpInfo *op_info) |
9e4f86a8 GA |
147 | { |
148 | CryptoDevBackendClass *bc = | |
149 | CRYPTODEV_BACKEND_GET_CLASS(backend); | |
150 | ||
0e660a6f | 151 | if (bc->do_op) { |
2cb06927 | 152 | return bc->do_op(backend, op_info); |
9e4f86a8 | 153 | } |
2fda101d | 154 | return -VIRTIO_CRYPTO_NOTSUPP; |
d6634ac0 GA |
155 | } |
156 | ||
157 | int cryptodev_backend_crypto_operation( | |
158 | CryptoDevBackend *backend, | |
2cb06927 | 159 | CryptoDevBackendOpInfo *op_info) |
d6634ac0 | 160 | { |
2cb06927 | 161 | QCryptodevBackendAlgType algtype = op_info->algtype; |
d6634ac0 | 162 | |
999c789f ZP |
163 | if ((algtype != QCRYPTODEV_BACKEND_ALG_SYM) |
164 | && (algtype != QCRYPTODEV_BACKEND_ALG_ASYM)) { | |
2fda101d | 165 | error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype); |
0e660a6f | 166 | return -VIRTIO_CRYPTO_NOTSUPP; |
d6634ac0 GA |
167 | } |
168 | ||
2cb06927 | 169 | return cryptodev_backend_operation(backend, op_info); |
9e4f86a8 GA |
170 | } |
171 | ||
d0ee7a13 GA |
172 | static void |
173 | cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name, | |
174 | void *opaque, Error **errp) | |
175 | { | |
176 | CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj); | |
177 | uint32_t value = backend->conf.peers.queues; | |
178 | ||
179 | visit_type_uint32(v, name, &value, errp); | |
180 | } | |
181 | ||
182 | static void | |
183 | cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name, | |
184 | void *opaque, Error **errp) | |
185 | { | |
186 | CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj); | |
d0ee7a13 GA |
187 | uint32_t value; |
188 | ||
668f62ec | 189 | if (!visit_type_uint32(v, name, &value, errp)) { |
dcfe4805 | 190 | return; |
d0ee7a13 GA |
191 | } |
192 | if (!value) { | |
dcfe4805 MA |
193 | error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu32 "'", |
194 | object_get_typename(obj), name, value); | |
195 | return; | |
d0ee7a13 GA |
196 | } |
197 | backend->conf.peers.queues = value; | |
d0ee7a13 GA |
198 | } |
199 | ||
200 | static void | |
201 | cryptodev_backend_complete(UserCreatable *uc, Error **errp) | |
202 | { | |
203 | CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc); | |
204 | CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc); | |
d0ee7a13 GA |
205 | |
206 | if (bc->init) { | |
7dc75edb | 207 | bc->init(backend, errp); |
d0ee7a13 | 208 | } |
d0ee7a13 GA |
209 | } |
210 | ||
46fd1705 GA |
211 | void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used) |
212 | { | |
213 | backend->is_used = used; | |
214 | } | |
215 | ||
216 | bool cryptodev_backend_is_used(CryptoDevBackend *backend) | |
217 | { | |
218 | return backend->is_used; | |
219 | } | |
220 | ||
6138dbda GA |
221 | void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready) |
222 | { | |
223 | backend->ready = ready; | |
224 | } | |
225 | ||
226 | bool cryptodev_backend_is_ready(CryptoDevBackend *backend) | |
227 | { | |
228 | return backend->ready; | |
229 | } | |
230 | ||
46fd1705 | 231 | static bool |
3beacfb9 | 232 | cryptodev_backend_can_be_deleted(UserCreatable *uc) |
46fd1705 GA |
233 | { |
234 | return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc)); | |
235 | } | |
236 | ||
d0ee7a13 GA |
237 | static void cryptodev_backend_instance_init(Object *obj) |
238 | { | |
d0ee7a13 | 239 | /* Initialize devices' queues property to 1 */ |
5325cc34 | 240 | object_property_set_int(obj, "queues", 1, NULL); |
d0ee7a13 GA |
241 | } |
242 | ||
243 | static void cryptodev_backend_finalize(Object *obj) | |
244 | { | |
46fd1705 | 245 | CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj); |
d0ee7a13 | 246 | |
46fd1705 | 247 | cryptodev_backend_cleanup(backend, NULL); |
d0ee7a13 GA |
248 | } |
249 | ||
250 | static void | |
251 | cryptodev_backend_class_init(ObjectClass *oc, void *data) | |
252 | { | |
253 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); | |
254 | ||
255 | ucc->complete = cryptodev_backend_complete; | |
46fd1705 | 256 | ucc->can_be_deleted = cryptodev_backend_can_be_deleted; |
d0ee7a13 GA |
257 | |
258 | QTAILQ_INIT(&crypto_clients); | |
1f14e388 EH |
259 | object_class_property_add(oc, "queues", "uint32", |
260 | cryptodev_backend_get_queues, | |
261 | cryptodev_backend_set_queues, | |
262 | NULL, NULL); | |
d0ee7a13 GA |
263 | } |
264 | ||
265 | static const TypeInfo cryptodev_backend_info = { | |
266 | .name = TYPE_CRYPTODEV_BACKEND, | |
267 | .parent = TYPE_OBJECT, | |
268 | .instance_size = sizeof(CryptoDevBackend), | |
269 | .instance_init = cryptodev_backend_instance_init, | |
270 | .instance_finalize = cryptodev_backend_finalize, | |
271 | .class_size = sizeof(CryptoDevBackendClass), | |
272 | .class_init = cryptodev_backend_class_init, | |
273 | .interfaces = (InterfaceInfo[]) { | |
274 | { TYPE_USER_CREATABLE }, | |
275 | { } | |
276 | } | |
277 | }; | |
278 | ||
279 | static void | |
280 | cryptodev_backend_register_types(void) | |
281 | { | |
282 | type_register_static(&cryptodev_backend_info); | |
283 | } | |
284 | ||
285 | type_init(cryptodev_backend_register_types); |