]> git.proxmox.com Git - mirror_qemu.git/blame - backends/cryptodev-vhost-user.c
Merge tag 'migration-20231020-pull-request' of https://gitlab.com/juan.quintela/qemu...
[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
0dda001b 12 * version 2.1 of the License, or (at your option) any later version.
042cea27
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"
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"
db1015e9 33#include "qom/object.h"
042cea27
GA
34
35
36/**
37 * @TYPE_CRYPTODEV_BACKEND_VHOST_USER:
38 * name of backend that uses vhost user server
39 */
40#define TYPE_CRYPTODEV_BACKEND_VHOST_USER "cryptodev-vhost-user"
41
8063396b 42OBJECT_DECLARE_SIMPLE_TYPE(CryptoDevBackendVhostUser, CRYPTODEV_BACKEND_VHOST_USER)
042cea27
GA
43
44
db1015e9 45struct CryptoDevBackendVhostUser {
042cea27
GA
46 CryptoDevBackend parent_obj;
47
0b99f224 48 VhostUserState vhost_user;
042cea27
GA
49 CharBackend chr;
50 char *chr_name;
51 bool opened;
52 CryptoDevBackendVhost *vhost_crypto[MAX_CRYPTO_QUEUE_NUM];
db1015e9 53};
042cea27
GA
54
55static int
56cryptodev_vhost_user_running(
57 CryptoDevBackendVhost *crypto)
58{
59 return crypto ? 1 : 0;
60}
61
5da73dab
GA
62CryptoDevBackendVhost *
63cryptodev_vhost_user_get_vhost(
64 CryptoDevBackendClient *cc,
65 CryptoDevBackend *b,
66 uint16_t queue)
67{
68 CryptoDevBackendVhostUser *s =
69 CRYPTODEV_BACKEND_VHOST_USER(b);
14c9fd16 70 assert(cc->type == QCRYPTODEV_BACKEND_TYPE_VHOST_USER);
5da73dab
GA
71 assert(queue < MAX_CRYPTO_QUEUE_NUM);
72
73 return s->vhost_crypto[queue];
74}
75
042cea27
GA
76static void cryptodev_vhost_user_stop(int queues,
77 CryptoDevBackendVhostUser *s)
78{
79 size_t i;
80
81 for (i = 0; i < queues; i++) {
82 if (!cryptodev_vhost_user_running(s->vhost_crypto[i])) {
83 continue;
84 }
85
86 cryptodev_vhost_cleanup(s->vhost_crypto[i]);
87 s->vhost_crypto[i] = NULL;
88 }
89}
90
91static int
92cryptodev_vhost_user_start(int queues,
93 CryptoDevBackendVhostUser *s)
94{
95 CryptoDevBackendVhostOptions options;
96 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
97 int max_queues;
98 size_t i;
99
100 for (i = 0; i < queues; i++) {
101 if (cryptodev_vhost_user_running(s->vhost_crypto[i])) {
102 continue;
103 }
104
0b99f224 105 options.opaque = &s->vhost_user;
042cea27
GA
106 options.backend_type = VHOST_BACKEND_TYPE_USER;
107 options.cc = b->conf.peers.ccs[i];
108 s->vhost_crypto[i] = cryptodev_vhost_init(&options);
109 if (!s->vhost_crypto[i]) {
110 error_report("failed to init vhost_crypto for queue %zu", i);
111 goto err;
112 }
113
114 if (i == 0) {
115 max_queues =
116 cryptodev_vhost_get_max_queues(s->vhost_crypto[i]);
117 if (queues > max_queues) {
118 error_report("you are asking more queues than supported: %d",
119 max_queues);
120 goto err;
121 }
122 }
123 }
124
125 return 0;
126
127err:
128 cryptodev_vhost_user_stop(i + 1, s);
129 return -1;
130}
131
132static Chardev *
133cryptodev_vhost_claim_chardev(CryptoDevBackendVhostUser *s,
134 Error **errp)
135{
136 Chardev *chr;
137
138 if (s->chr_name == NULL) {
139 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
140 "chardev", "a valid character device");
141 return NULL;
142 }
143
144 chr = qemu_chr_find(s->chr_name);
145 if (chr == NULL) {
146 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
147 "Device '%s' not found", s->chr_name);
148 return NULL;
149 }
150
151 return chr;
152}
153
083b266f 154static void cryptodev_vhost_user_event(void *opaque, QEMUChrEvent event)
042cea27
GA
155{
156 CryptoDevBackendVhostUser *s = opaque;
157 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
042cea27
GA
158 int queues = b->conf.peers.queues;
159
160 assert(queues < MAX_CRYPTO_QUEUE_NUM);
161
162 switch (event) {
163 case CHR_EVENT_OPENED:
164 if (cryptodev_vhost_user_start(queues, s) < 0) {
165 exit(1);
166 }
167 b->ready = true;
168 break;
169 case CHR_EVENT_CLOSED:
170 b->ready = false;
171 cryptodev_vhost_user_stop(queues, s);
172 break;
5b082922
PMD
173 case CHR_EVENT_BREAK:
174 case CHR_EVENT_MUX_IN:
175 case CHR_EVENT_MUX_OUT:
176 /* Ignore */
177 break;
042cea27 178 }
042cea27
GA
179}
180
181static void cryptodev_vhost_user_init(
182 CryptoDevBackend *backend, Error **errp)
183{
184 int queues = backend->conf.peers.queues;
185 size_t i;
186 Error *local_err = NULL;
187 Chardev *chr;
188 CryptoDevBackendClient *cc;
189 CryptoDevBackendVhostUser *s =
190 CRYPTODEV_BACKEND_VHOST_USER(backend);
191
192 chr = cryptodev_vhost_claim_chardev(s, &local_err);
193 if (local_err) {
194 error_propagate(errp, local_err);
195 return;
196 }
197
198 s->opened = true;
199
200 for (i = 0; i < queues; i++) {
3f478371 201 cc = cryptodev_backend_new_client();
042cea27
GA
202 cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
203 i, chr->label);
204 cc->queue_index = i;
14c9fd16 205 cc->type = QCRYPTODEV_BACKEND_TYPE_VHOST_USER;
042cea27
GA
206
207 backend->conf.peers.ccs[i] = cc;
208
209 if (i == 0) {
668f62ec 210 if (!qemu_chr_fe_init(&s->chr, chr, errp)) {
042cea27
GA
211 return;
212 }
213 }
214 }
215
0b99f224 216 if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
4d0cf552
TB
217 return;
218 }
219
042cea27
GA
220 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
221 cryptodev_vhost_user_event, NULL, s, NULL, true);
222
223 backend->conf.crypto_services =
bc304a64
ZP
224 1u << QCRYPTODEV_BACKEND_SERVICE_CIPHER |
225 1u << QCRYPTODEV_BACKEND_SERVICE_HASH |
226 1u << QCRYPTODEV_BACKEND_SERVICE_MAC;
042cea27
GA
227 backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
228 backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
0a9b9be9
GA
229
230 backend->conf.max_size = UINT64_MAX;
231 backend->conf.max_cipher_key_len = VHOST_USER_MAX_CIPHER_KEY_LEN;
232 backend->conf.max_auth_key_len = VHOST_USER_MAX_AUTH_KEY_LEN;
042cea27
GA
233}
234
5c33f978 235static int64_t cryptodev_vhost_user_crypto_create_session(
042cea27 236 CryptoDevBackend *backend,
5c33f978 237 CryptoDevBackendSessionInfo *sess_info,
042cea27
GA
238 uint32_t queue_index, Error **errp)
239{
efbfeb81
GA
240 CryptoDevBackendClient *cc =
241 backend->conf.peers.ccs[queue_index];
242 CryptoDevBackendVhost *vhost_crypto;
243 uint64_t session_id = 0;
244 int ret;
245
246 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
247 if (vhost_crypto) {
248 struct vhost_dev *dev = &(vhost_crypto->dev);
249 ret = dev->vhost_ops->vhost_crypto_create_session(dev,
250 sess_info,
251 &session_id);
252 if (ret < 0) {
253 return -1;
254 } else {
255 return session_id;
256 }
257 }
258 return -1;
042cea27
GA
259}
260
2fda101d 261static int cryptodev_vhost_user_create_session(
0e660a6f
ZP
262 CryptoDevBackend *backend,
263 CryptoDevBackendSessionInfo *sess_info,
2fda101d
LH
264 uint32_t queue_index,
265 CryptoDevCompletionFunc cb,
266 void *opaque)
0e660a6f
ZP
267{
268 uint32_t op_code = sess_info->op_code;
2fda101d
LH
269 int64_t ret;
270 Error *local_error = NULL;
271 int status;
0e660a6f
ZP
272
273 switch (op_code) {
274 case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION:
5c33f978 275 case VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION:
0e660a6f
ZP
276 case VIRTIO_CRYPTO_HASH_CREATE_SESSION:
277 case VIRTIO_CRYPTO_MAC_CREATE_SESSION:
278 case VIRTIO_CRYPTO_AEAD_CREATE_SESSION:
5c33f978 279 ret = cryptodev_vhost_user_crypto_create_session(backend, sess_info,
2fda101d
LH
280 queue_index, &local_error);
281 break;
282
0e660a6f 283 default:
2fda101d 284 error_setg(&local_error, "Unsupported opcode :%" PRIu32 "",
0e660a6f 285 sess_info->op_code);
2fda101d 286 return -VIRTIO_CRYPTO_NOTSUPP;
0e660a6f
ZP
287 }
288
2fda101d
LH
289 if (local_error) {
290 error_report_err(local_error);
291 }
292 if (ret < 0) {
293 status = -VIRTIO_CRYPTO_ERR;
294 } else {
295 sess_info->session_id = ret;
296 status = VIRTIO_CRYPTO_OK;
297 }
298 if (cb) {
299 cb(opaque, status);
300 }
301 return 0;
0e660a6f
ZP
302}
303
304static int cryptodev_vhost_user_close_session(
042cea27
GA
305 CryptoDevBackend *backend,
306 uint64_t session_id,
2fda101d
LH
307 uint32_t queue_index,
308 CryptoDevCompletionFunc cb,
309 void *opaque)
042cea27 310{
efbfeb81
GA
311 CryptoDevBackendClient *cc =
312 backend->conf.peers.ccs[queue_index];
313 CryptoDevBackendVhost *vhost_crypto;
2fda101d 314 int ret = -1, status;
efbfeb81
GA
315
316 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
317 if (vhost_crypto) {
318 struct vhost_dev *dev = &(vhost_crypto->dev);
319 ret = dev->vhost_ops->vhost_crypto_close_session(dev,
320 session_id);
321 if (ret < 0) {
2fda101d 322 status = -VIRTIO_CRYPTO_ERR;
efbfeb81 323 } else {
2fda101d 324 status = VIRTIO_CRYPTO_OK;
efbfeb81 325 }
2fda101d
LH
326 } else {
327 status = -VIRTIO_CRYPTO_NOTSUPP;
efbfeb81 328 }
2fda101d
LH
329 if (cb) {
330 cb(opaque, status);
331 }
332 return 0;
042cea27
GA
333}
334
335static void cryptodev_vhost_user_cleanup(
336 CryptoDevBackend *backend,
337 Error **errp)
338{
339 CryptoDevBackendVhostUser *s =
340 CRYPTODEV_BACKEND_VHOST_USER(backend);
341 size_t i;
342 int queues = backend->conf.peers.queues;
343 CryptoDevBackendClient *cc;
344
345 cryptodev_vhost_user_stop(queues, s);
346
347 for (i = 0; i < queues; i++) {
348 cc = backend->conf.peers.ccs[i];
349 if (cc) {
350 cryptodev_backend_free_client(cc);
351 backend->conf.peers.ccs[i] = NULL;
352 }
353 }
4d0cf552 354
0b99f224 355 vhost_user_cleanup(&s->vhost_user);
042cea27
GA
356}
357
358static void cryptodev_vhost_user_set_chardev(Object *obj,
359 const char *value, Error **errp)
360{
361 CryptoDevBackendVhostUser *s =
362 CRYPTODEV_BACKEND_VHOST_USER(obj);
363
364 if (s->opened) {
ff924448 365 error_setg(errp, "Property 'chardev' can no longer be set");
042cea27
GA
366 } else {
367 g_free(s->chr_name);
368 s->chr_name = g_strdup(value);
369 }
370}
371
372static char *
373cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
374{
375 CryptoDevBackendVhostUser *s =
376 CRYPTODEV_BACKEND_VHOST_USER(obj);
377 Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
378
379 if (chr && chr->label) {
380 return g_strdup(chr->label);
381 }
382
383 return NULL;
384}
385
042cea27
GA
386static void cryptodev_vhost_user_finalize(Object *obj)
387{
388 CryptoDevBackendVhostUser *s =
389 CRYPTODEV_BACKEND_VHOST_USER(obj);
390
391 qemu_chr_fe_deinit(&s->chr, false);
392
393 g_free(s->chr_name);
394}
395
396static void
397cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
398{
399 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
400
401 bc->init = cryptodev_vhost_user_init;
402 bc->cleanup = cryptodev_vhost_user_cleanup;
0e660a6f
ZP
403 bc->create_session = cryptodev_vhost_user_create_session;
404 bc->close_session = cryptodev_vhost_user_close_session;
405 bc->do_op = NULL;
07b0db0e
EH
406
407 object_class_property_add_str(oc, "chardev",
408 cryptodev_vhost_user_get_chardev,
409 cryptodev_vhost_user_set_chardev);
410
042cea27
GA
411}
412
413static const TypeInfo cryptodev_vhost_user_info = {
414 .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
415 .parent = TYPE_CRYPTODEV_BACKEND,
416 .class_init = cryptodev_vhost_user_class_init,
042cea27
GA
417 .instance_finalize = cryptodev_vhost_user_finalize,
418 .instance_size = sizeof(CryptoDevBackendVhostUser),
419};
420
421static void
422cryptodev_vhost_user_register_types(void)
423{
424 type_register_static(&cryptodev_vhost_user_info);
425}
426
427type_init(cryptodev_vhost_user_register_types);