]> git.proxmox.com Git - mirror_qemu.git/blame - backends/cryptodev-vhost-user.c
Delete duplicate QOM typedefs
[mirror_qemu.git] / backends / cryptodev-vhost-user.c
CommitLineData
042cea27
GA
1/*
2 * QEMU Cryptodev backend for QEMU cipher APIs
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
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"
042cea27
GA
25#include "qapi/error.h"
26#include "qapi/qmp/qerror.h"
27#include "qemu/error-report.h"
4d0cf552 28#include "hw/virtio/vhost-user.h"
042cea27
GA
29#include "standard-headers/linux/virtio_crypto.h"
30#include "sysemu/cryptodev-vhost.h"
31#include "chardev/char-fe.h"
5da73dab 32#include "sysemu/cryptodev-vhost-user.h"
042cea27
GA
33
34
35/**
36 * @TYPE_CRYPTODEV_BACKEND_VHOST_USER:
37 * name of backend that uses vhost user server
38 */
39#define TYPE_CRYPTODEV_BACKEND_VHOST_USER "cryptodev-vhost-user"
40
41#define CRYPTODEV_BACKEND_VHOST_USER(obj) \
42 OBJECT_CHECK(CryptoDevBackendVhostUser, \
43 (obj), TYPE_CRYPTODEV_BACKEND_VHOST_USER)
44
45
46typedef struct CryptoDevBackendVhostUser {
47 CryptoDevBackend parent_obj;
48
0b99f224 49 VhostUserState vhost_user;
042cea27
GA
50 CharBackend chr;
51 char *chr_name;
52 bool opened;
53 CryptoDevBackendVhost *vhost_crypto[MAX_CRYPTO_QUEUE_NUM];
54} CryptoDevBackendVhostUser;
55
56static int
57cryptodev_vhost_user_running(
58 CryptoDevBackendVhost *crypto)
59{
60 return crypto ? 1 : 0;
61}
62
5da73dab
GA
63CryptoDevBackendVhost *
64cryptodev_vhost_user_get_vhost(
65 CryptoDevBackendClient *cc,
66 CryptoDevBackend *b,
67 uint16_t queue)
68{
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);
73
74 return s->vhost_crypto[queue];
75}
76
042cea27
GA
77static void cryptodev_vhost_user_stop(int queues,
78 CryptoDevBackendVhostUser *s)
79{
80 size_t i;
81
82 for (i = 0; i < queues; i++) {
83 if (!cryptodev_vhost_user_running(s->vhost_crypto[i])) {
84 continue;
85 }
86
87 cryptodev_vhost_cleanup(s->vhost_crypto[i]);
88 s->vhost_crypto[i] = NULL;
89 }
90}
91
92static int
93cryptodev_vhost_user_start(int queues,
94 CryptoDevBackendVhostUser *s)
95{
96 CryptoDevBackendVhostOptions options;
97 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
98 int max_queues;
99 size_t i;
100
101 for (i = 0; i < queues; i++) {
102 if (cryptodev_vhost_user_running(s->vhost_crypto[i])) {
103 continue;
104 }
105
0b99f224 106 options.opaque = &s->vhost_user;
042cea27
GA
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);
112 goto err;
113 }
114
115 if (i == 0) {
116 max_queues =
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",
120 max_queues);
121 goto err;
122 }
123 }
124 }
125
126 return 0;
127
128err:
129 cryptodev_vhost_user_stop(i + 1, s);
130 return -1;
131}
132
133static Chardev *
134cryptodev_vhost_claim_chardev(CryptoDevBackendVhostUser *s,
135 Error **errp)
136{
137 Chardev *chr;
138
139 if (s->chr_name == NULL) {
140 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
141 "chardev", "a valid character device");
142 return NULL;
143 }
144
145 chr = qemu_chr_find(s->chr_name);
146 if (chr == NULL) {
147 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
148 "Device '%s' not found", s->chr_name);
149 return NULL;
150 }
151
152 return chr;
153}
154
083b266f 155static void cryptodev_vhost_user_event(void *opaque, QEMUChrEvent event)
042cea27
GA
156{
157 CryptoDevBackendVhostUser *s = opaque;
158 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
042cea27
GA
159 int queues = b->conf.peers.queues;
160
161 assert(queues < MAX_CRYPTO_QUEUE_NUM);
162
163 switch (event) {
164 case CHR_EVENT_OPENED:
165 if (cryptodev_vhost_user_start(queues, s) < 0) {
166 exit(1);
167 }
168 b->ready = true;
169 break;
170 case CHR_EVENT_CLOSED:
171 b->ready = false;
172 cryptodev_vhost_user_stop(queues, s);
173 break;
5b082922
PMD
174 case CHR_EVENT_BREAK:
175 case CHR_EVENT_MUX_IN:
176 case CHR_EVENT_MUX_OUT:
177 /* Ignore */
178 break;
042cea27 179 }
042cea27
GA
180}
181
182static void cryptodev_vhost_user_init(
183 CryptoDevBackend *backend, Error **errp)
184{
185 int queues = backend->conf.peers.queues;
186 size_t i;
187 Error *local_err = NULL;
188 Chardev *chr;
189 CryptoDevBackendClient *cc;
190 CryptoDevBackendVhostUser *s =
191 CRYPTODEV_BACKEND_VHOST_USER(backend);
192
193 chr = cryptodev_vhost_claim_chardev(s, &local_err);
194 if (local_err) {
195 error_propagate(errp, local_err);
196 return;
197 }
198
199 s->opened = true;
200
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 ",
205 i, chr->label);
206 cc->queue_index = i;
5da73dab 207 cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
042cea27
GA
208
209 backend->conf.peers.ccs[i] = cc;
210
211 if (i == 0) {
668f62ec 212 if (!qemu_chr_fe_init(&s->chr, chr, errp)) {
042cea27
GA
213 return;
214 }
215 }
216 }
217
0b99f224 218 if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
4d0cf552
TB
219 return;
220 }
221
042cea27
GA
222 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
223 cryptodev_vhost_user_event, NULL, s, NULL, true);
224
225 backend->conf.crypto_services =
226 1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
227 1u << VIRTIO_CRYPTO_SERVICE_HASH |
228 1u << VIRTIO_CRYPTO_SERVICE_MAC;
229 backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
230 backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
0a9b9be9
GA
231
232 backend->conf.max_size = UINT64_MAX;
233 backend->conf.max_cipher_key_len = VHOST_USER_MAX_CIPHER_KEY_LEN;
234 backend->conf.max_auth_key_len = VHOST_USER_MAX_AUTH_KEY_LEN;
042cea27
GA
235}
236
237static int64_t cryptodev_vhost_user_sym_create_session(
238 CryptoDevBackend *backend,
239 CryptoDevBackendSymSessionInfo *sess_info,
240 uint32_t queue_index, Error **errp)
241{
efbfeb81
GA
242 CryptoDevBackendClient *cc =
243 backend->conf.peers.ccs[queue_index];
244 CryptoDevBackendVhost *vhost_crypto;
245 uint64_t session_id = 0;
246 int ret;
247
248 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
249 if (vhost_crypto) {
250 struct vhost_dev *dev = &(vhost_crypto->dev);
251 ret = dev->vhost_ops->vhost_crypto_create_session(dev,
252 sess_info,
253 &session_id);
254 if (ret < 0) {
255 return -1;
256 } else {
257 return session_id;
258 }
259 }
260 return -1;
042cea27
GA
261}
262
263static int cryptodev_vhost_user_sym_close_session(
264 CryptoDevBackend *backend,
265 uint64_t session_id,
266 uint32_t queue_index, Error **errp)
267{
efbfeb81
GA
268 CryptoDevBackendClient *cc =
269 backend->conf.peers.ccs[queue_index];
270 CryptoDevBackendVhost *vhost_crypto;
271 int ret;
272
273 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
274 if (vhost_crypto) {
275 struct vhost_dev *dev = &(vhost_crypto->dev);
276 ret = dev->vhost_ops->vhost_crypto_close_session(dev,
277 session_id);
278 if (ret < 0) {
279 return -1;
280 } else {
281 return 0;
282 }
283 }
284 return -1;
042cea27
GA
285}
286
287static void cryptodev_vhost_user_cleanup(
288 CryptoDevBackend *backend,
289 Error **errp)
290{
291 CryptoDevBackendVhostUser *s =
292 CRYPTODEV_BACKEND_VHOST_USER(backend);
293 size_t i;
294 int queues = backend->conf.peers.queues;
295 CryptoDevBackendClient *cc;
296
297 cryptodev_vhost_user_stop(queues, s);
298
299 for (i = 0; i < queues; i++) {
300 cc = backend->conf.peers.ccs[i];
301 if (cc) {
302 cryptodev_backend_free_client(cc);
303 backend->conf.peers.ccs[i] = NULL;
304 }
305 }
4d0cf552 306
0b99f224 307 vhost_user_cleanup(&s->vhost_user);
042cea27
GA
308}
309
310static void cryptodev_vhost_user_set_chardev(Object *obj,
311 const char *value, Error **errp)
312{
313 CryptoDevBackendVhostUser *s =
314 CRYPTODEV_BACKEND_VHOST_USER(obj);
315
316 if (s->opened) {
317 error_setg(errp, QERR_PERMISSION_DENIED);
318 } else {
319 g_free(s->chr_name);
320 s->chr_name = g_strdup(value);
321 }
322}
323
324static char *
325cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
326{
327 CryptoDevBackendVhostUser *s =
328 CRYPTODEV_BACKEND_VHOST_USER(obj);
329 Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
330
331 if (chr && chr->label) {
332 return g_strdup(chr->label);
333 }
334
335 return NULL;
336}
337
338static void cryptodev_vhost_user_instance_int(Object *obj)
339{
340 object_property_add_str(obj, "chardev",
341 cryptodev_vhost_user_get_chardev,
d2623129 342 cryptodev_vhost_user_set_chardev);
042cea27
GA
343}
344
345static void cryptodev_vhost_user_finalize(Object *obj)
346{
347 CryptoDevBackendVhostUser *s =
348 CRYPTODEV_BACKEND_VHOST_USER(obj);
349
350 qemu_chr_fe_deinit(&s->chr, false);
351
352 g_free(s->chr_name);
353}
354
355static void
356cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
357{
358 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
359
360 bc->init = cryptodev_vhost_user_init;
361 bc->cleanup = cryptodev_vhost_user_cleanup;
362 bc->create_session = cryptodev_vhost_user_sym_create_session;
363 bc->close_session = cryptodev_vhost_user_sym_close_session;
efbfeb81 364 bc->do_sym_op = NULL;
042cea27
GA
365}
366
367static const TypeInfo cryptodev_vhost_user_info = {
368 .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
369 .parent = TYPE_CRYPTODEV_BACKEND,
370 .class_init = cryptodev_vhost_user_class_init,
371 .instance_init = cryptodev_vhost_user_instance_int,
372 .instance_finalize = cryptodev_vhost_user_finalize,
373 .instance_size = sizeof(CryptoDevBackendVhostUser),
374};
375
376static void
377cryptodev_vhost_user_register_types(void)
378{
379 type_register_static(&cryptodev_vhost_user_info);
380}
381
382type_init(cryptodev_vhost_user_register_types);