block/nvme: add support for write zeros
[qemu/ar7.git] / backends / cryptodev-vhost-user.c
blobb34428394087ecf149624167f3ca4fe04cd1b2ac
1 /*
2 * QEMU Cryptodev backend for QEMU cipher APIs
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 "qapi/error.h"
26 #include "qapi/qmp/qerror.h"
27 #include "qemu/error-report.h"
28 #include "hw/virtio/vhost-user.h"
29 #include "standard-headers/linux/virtio_crypto.h"
30 #include "sysemu/cryptodev-vhost.h"
31 #include "chardev/char-fe.h"
32 #include "sysemu/cryptodev-vhost-user.h"
35 /**
36 * @TYPE_CRYPTODEV_BACKEND_VHOST_USER:
37 * name of backend that uses vhost user server
39 #define TYPE_CRYPTODEV_BACKEND_VHOST_USER "cryptodev-vhost-user"
41 #define CRYPTODEV_BACKEND_VHOST_USER(obj) \
42 OBJECT_CHECK(CryptoDevBackendVhostUser, \
43 (obj), TYPE_CRYPTODEV_BACKEND_VHOST_USER)
46 typedef struct CryptoDevBackendVhostUser {
47 CryptoDevBackend parent_obj;
49 VhostUserState vhost_user;
50 CharBackend chr;
51 char *chr_name;
52 bool opened;
53 CryptoDevBackendVhost *vhost_crypto[MAX_CRYPTO_QUEUE_NUM];
54 } CryptoDevBackendVhostUser;
56 static int
57 cryptodev_vhost_user_running(
58 CryptoDevBackendVhost *crypto)
60 return crypto ? 1 : 0;
63 CryptoDevBackendVhost *
64 cryptodev_vhost_user_get_vhost(
65 CryptoDevBackendClient *cc,
66 CryptoDevBackend *b,
67 uint16_t queue)
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);
74 return s->vhost_crypto[queue];
77 static void cryptodev_vhost_user_stop(int queues,
78 CryptoDevBackendVhostUser *s)
80 size_t i;
82 for (i = 0; i < queues; i++) {
83 if (!cryptodev_vhost_user_running(s->vhost_crypto[i])) {
84 continue;
87 cryptodev_vhost_cleanup(s->vhost_crypto[i]);
88 s->vhost_crypto[i] = NULL;
92 static int
93 cryptodev_vhost_user_start(int queues,
94 CryptoDevBackendVhostUser *s)
96 CryptoDevBackendVhostOptions options;
97 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
98 int max_queues;
99 size_t i;
101 for (i = 0; i < queues; i++) {
102 if (cryptodev_vhost_user_running(s->vhost_crypto[i])) {
103 continue;
106 options.opaque = &s->vhost_user;
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;
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;
126 return 0;
128 err:
129 cryptodev_vhost_user_stop(i + 1, s);
130 return -1;
133 static Chardev *
134 cryptodev_vhost_claim_chardev(CryptoDevBackendVhostUser *s,
135 Error **errp)
137 Chardev *chr;
139 if (s->chr_name == NULL) {
140 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
141 "chardev", "a valid character device");
142 return NULL;
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;
152 return chr;
155 static void cryptodev_vhost_user_event(void *opaque, int event)
157 CryptoDevBackendVhostUser *s = opaque;
158 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
159 int queues = b->conf.peers.queues;
161 assert(queues < MAX_CRYPTO_QUEUE_NUM);
163 switch (event) {
164 case CHR_EVENT_OPENED:
165 if (cryptodev_vhost_user_start(queues, s) < 0) {
166 exit(1);
168 b->ready = true;
169 break;
170 case CHR_EVENT_CLOSED:
171 b->ready = false;
172 cryptodev_vhost_user_stop(queues, s);
173 break;
177 static void cryptodev_vhost_user_init(
178 CryptoDevBackend *backend, Error **errp)
180 int queues = backend->conf.peers.queues;
181 size_t i;
182 Error *local_err = NULL;
183 Chardev *chr;
184 CryptoDevBackendClient *cc;
185 CryptoDevBackendVhostUser *s =
186 CRYPTODEV_BACKEND_VHOST_USER(backend);
188 chr = cryptodev_vhost_claim_chardev(s, &local_err);
189 if (local_err) {
190 error_propagate(errp, local_err);
191 return;
194 s->opened = true;
196 for (i = 0; i < queues; i++) {
197 cc = cryptodev_backend_new_client(
198 "cryptodev-vhost-user", NULL);
199 cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
200 i, chr->label);
201 cc->queue_index = i;
202 cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
204 backend->conf.peers.ccs[i] = cc;
206 if (i == 0) {
207 if (!qemu_chr_fe_init(&s->chr, chr, &local_err)) {
208 error_propagate(errp, local_err);
209 return;
214 if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
215 return;
218 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
219 cryptodev_vhost_user_event, NULL, s, NULL, true);
221 backend->conf.crypto_services =
222 1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
223 1u << VIRTIO_CRYPTO_SERVICE_HASH |
224 1u << VIRTIO_CRYPTO_SERVICE_MAC;
225 backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
226 backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
228 backend->conf.max_size = UINT64_MAX;
229 backend->conf.max_cipher_key_len = VHOST_USER_MAX_CIPHER_KEY_LEN;
230 backend->conf.max_auth_key_len = VHOST_USER_MAX_AUTH_KEY_LEN;
233 static int64_t cryptodev_vhost_user_sym_create_session(
234 CryptoDevBackend *backend,
235 CryptoDevBackendSymSessionInfo *sess_info,
236 uint32_t queue_index, Error **errp)
238 CryptoDevBackendClient *cc =
239 backend->conf.peers.ccs[queue_index];
240 CryptoDevBackendVhost *vhost_crypto;
241 uint64_t session_id = 0;
242 int ret;
244 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
245 if (vhost_crypto) {
246 struct vhost_dev *dev = &(vhost_crypto->dev);
247 ret = dev->vhost_ops->vhost_crypto_create_session(dev,
248 sess_info,
249 &session_id);
250 if (ret < 0) {
251 return -1;
252 } else {
253 return session_id;
256 return -1;
259 static int cryptodev_vhost_user_sym_close_session(
260 CryptoDevBackend *backend,
261 uint64_t session_id,
262 uint32_t queue_index, Error **errp)
264 CryptoDevBackendClient *cc =
265 backend->conf.peers.ccs[queue_index];
266 CryptoDevBackendVhost *vhost_crypto;
267 int ret;
269 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
270 if (vhost_crypto) {
271 struct vhost_dev *dev = &(vhost_crypto->dev);
272 ret = dev->vhost_ops->vhost_crypto_close_session(dev,
273 session_id);
274 if (ret < 0) {
275 return -1;
276 } else {
277 return 0;
280 return -1;
283 static void cryptodev_vhost_user_cleanup(
284 CryptoDevBackend *backend,
285 Error **errp)
287 CryptoDevBackendVhostUser *s =
288 CRYPTODEV_BACKEND_VHOST_USER(backend);
289 size_t i;
290 int queues = backend->conf.peers.queues;
291 CryptoDevBackendClient *cc;
293 cryptodev_vhost_user_stop(queues, s);
295 for (i = 0; i < queues; i++) {
296 cc = backend->conf.peers.ccs[i];
297 if (cc) {
298 cryptodev_backend_free_client(cc);
299 backend->conf.peers.ccs[i] = NULL;
303 vhost_user_cleanup(&s->vhost_user);
306 static void cryptodev_vhost_user_set_chardev(Object *obj,
307 const char *value, Error **errp)
309 CryptoDevBackendVhostUser *s =
310 CRYPTODEV_BACKEND_VHOST_USER(obj);
312 if (s->opened) {
313 error_setg(errp, QERR_PERMISSION_DENIED);
314 } else {
315 g_free(s->chr_name);
316 s->chr_name = g_strdup(value);
320 static char *
321 cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
323 CryptoDevBackendVhostUser *s =
324 CRYPTODEV_BACKEND_VHOST_USER(obj);
325 Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
327 if (chr && chr->label) {
328 return g_strdup(chr->label);
331 return NULL;
334 static void cryptodev_vhost_user_instance_int(Object *obj)
336 object_property_add_str(obj, "chardev",
337 cryptodev_vhost_user_get_chardev,
338 cryptodev_vhost_user_set_chardev,
339 NULL);
342 static void cryptodev_vhost_user_finalize(Object *obj)
344 CryptoDevBackendVhostUser *s =
345 CRYPTODEV_BACKEND_VHOST_USER(obj);
347 qemu_chr_fe_deinit(&s->chr, false);
349 g_free(s->chr_name);
352 static void
353 cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
355 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
357 bc->init = cryptodev_vhost_user_init;
358 bc->cleanup = cryptodev_vhost_user_cleanup;
359 bc->create_session = cryptodev_vhost_user_sym_create_session;
360 bc->close_session = cryptodev_vhost_user_sym_close_session;
361 bc->do_sym_op = NULL;
364 static const TypeInfo cryptodev_vhost_user_info = {
365 .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
366 .parent = TYPE_CRYPTODEV_BACKEND,
367 .class_init = cryptodev_vhost_user_class_init,
368 .instance_init = cryptodev_vhost_user_instance_int,
369 .instance_finalize = cryptodev_vhost_user_finalize,
370 .instance_size = sizeof(CryptoDevBackendVhostUser),
373 static void
374 cryptodev_vhost_user_register_types(void)
376 type_register_static(&cryptodev_vhost_user_info);
379 type_init(cryptodev_vhost_user_register_types);