blob: 9cd06c4b9e7d0ac032ef7e5688c84764df054f99 [file] [log] [blame]
Gonglei042cea22018-03-01 21:46:28 +08001/*
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"
25#include "hw/boards.h"
26#include "qapi/error.h"
27#include "qapi/qmp/qerror.h"
28#include "qemu/error-report.h"
29#include "standard-headers/linux/virtio_crypto.h"
30#include "sysemu/cryptodev-vhost.h"
31#include "chardev/char-fe.h"
Gonglei5da73da2018-03-01 21:46:29 +080032#include "sysemu/cryptodev-vhost-user.h"
Gonglei042cea22018-03-01 21:46:28 +080033
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
49 CharBackend chr;
50 char *chr_name;
51 bool opened;
52 CryptoDevBackendVhost *vhost_crypto[MAX_CRYPTO_QUEUE_NUM];
53} CryptoDevBackendVhostUser;
54
55static int
56cryptodev_vhost_user_running(
57 CryptoDevBackendVhost *crypto)
58{
59 return crypto ? 1 : 0;
60}
61
Gonglei5da73da2018-03-01 21:46:29 +080062CryptoDevBackendVhost *
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);
70 assert(cc->type == CRYPTODEV_BACKEND_TYPE_VHOST_USER);
71 assert(queue < MAX_CRYPTO_QUEUE_NUM);
72
73 return s->vhost_crypto[queue];
74}
75
Gonglei042cea22018-03-01 21:46:28 +080076static 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
105 options.opaque = &s->chr;
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
154static void cryptodev_vhost_user_event(void *opaque, int event)
155{
156 CryptoDevBackendVhostUser *s = opaque;
157 CryptoDevBackend *b = CRYPTODEV_BACKEND(s);
158 Error *err = NULL;
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;
174 }
175
176 if (err) {
177 error_report_err(err);
178 }
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++) {
201 cc = cryptodev_backend_new_client(
202 "cryptodev-vhost-user", NULL);
203 cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
204 i, chr->label);
205 cc->queue_index = i;
Gonglei5da73da2018-03-01 21:46:29 +0800206 cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
Gonglei042cea22018-03-01 21:46:28 +0800207
208 backend->conf.peers.ccs[i] = cc;
209
210 if (i == 0) {
211 if (!qemu_chr_fe_init(&s->chr, chr, &local_err)) {
212 error_propagate(errp, local_err);
213 return;
214 }
215 }
216 }
217
218 qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
219 cryptodev_vhost_user_event, NULL, s, NULL, true);
220
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;
227}
228
229static int64_t cryptodev_vhost_user_sym_create_session(
230 CryptoDevBackend *backend,
231 CryptoDevBackendSymSessionInfo *sess_info,
232 uint32_t queue_index, Error **errp)
233{
Gongleiefbfeb82018-03-01 21:46:30 +0800234 CryptoDevBackendClient *cc =
235 backend->conf.peers.ccs[queue_index];
236 CryptoDevBackendVhost *vhost_crypto;
237 uint64_t session_id = 0;
238 int ret;
239
240 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
241 if (vhost_crypto) {
242 struct vhost_dev *dev = &(vhost_crypto->dev);
243 ret = dev->vhost_ops->vhost_crypto_create_session(dev,
244 sess_info,
245 &session_id);
246 if (ret < 0) {
247 return -1;
248 } else {
249 return session_id;
250 }
251 }
252 return -1;
Gonglei042cea22018-03-01 21:46:28 +0800253}
254
255static int cryptodev_vhost_user_sym_close_session(
256 CryptoDevBackend *backend,
257 uint64_t session_id,
258 uint32_t queue_index, Error **errp)
259{
Gongleiefbfeb82018-03-01 21:46:30 +0800260 CryptoDevBackendClient *cc =
261 backend->conf.peers.ccs[queue_index];
262 CryptoDevBackendVhost *vhost_crypto;
263 int ret;
Gonglei042cea22018-03-01 21:46:28 +0800264
Gongleiefbfeb82018-03-01 21:46:30 +0800265 vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
266 if (vhost_crypto) {
267 struct vhost_dev *dev = &(vhost_crypto->dev);
268 ret = dev->vhost_ops->vhost_crypto_close_session(dev,
269 session_id);
270 if (ret < 0) {
271 return -1;
272 } else {
273 return 0;
274 }
275 }
276 return -1;
Gonglei042cea22018-03-01 21:46:28 +0800277}
278
279static void cryptodev_vhost_user_cleanup(
280 CryptoDevBackend *backend,
281 Error **errp)
282{
283 CryptoDevBackendVhostUser *s =
284 CRYPTODEV_BACKEND_VHOST_USER(backend);
285 size_t i;
286 int queues = backend->conf.peers.queues;
287 CryptoDevBackendClient *cc;
288
289 cryptodev_vhost_user_stop(queues, s);
290
291 for (i = 0; i < queues; i++) {
292 cc = backend->conf.peers.ccs[i];
293 if (cc) {
294 cryptodev_backend_free_client(cc);
295 backend->conf.peers.ccs[i] = NULL;
296 }
297 }
298}
299
300static void cryptodev_vhost_user_set_chardev(Object *obj,
301 const char *value, Error **errp)
302{
303 CryptoDevBackendVhostUser *s =
304 CRYPTODEV_BACKEND_VHOST_USER(obj);
305
306 if (s->opened) {
307 error_setg(errp, QERR_PERMISSION_DENIED);
308 } else {
309 g_free(s->chr_name);
310 s->chr_name = g_strdup(value);
311 }
312}
313
314static char *
315cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
316{
317 CryptoDevBackendVhostUser *s =
318 CRYPTODEV_BACKEND_VHOST_USER(obj);
319 Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
320
321 if (chr && chr->label) {
322 return g_strdup(chr->label);
323 }
324
325 return NULL;
326}
327
328static void cryptodev_vhost_user_instance_int(Object *obj)
329{
330 object_property_add_str(obj, "chardev",
331 cryptodev_vhost_user_get_chardev,
332 cryptodev_vhost_user_set_chardev,
333 NULL);
334}
335
336static void cryptodev_vhost_user_finalize(Object *obj)
337{
338 CryptoDevBackendVhostUser *s =
339 CRYPTODEV_BACKEND_VHOST_USER(obj);
340
341 qemu_chr_fe_deinit(&s->chr, false);
342
343 g_free(s->chr_name);
344}
345
346static void
347cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
348{
349 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
350
351 bc->init = cryptodev_vhost_user_init;
352 bc->cleanup = cryptodev_vhost_user_cleanup;
353 bc->create_session = cryptodev_vhost_user_sym_create_session;
354 bc->close_session = cryptodev_vhost_user_sym_close_session;
Gongleiefbfeb82018-03-01 21:46:30 +0800355 bc->do_sym_op = NULL;
Gonglei042cea22018-03-01 21:46:28 +0800356}
357
358static const TypeInfo cryptodev_vhost_user_info = {
359 .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
360 .parent = TYPE_CRYPTODEV_BACKEND,
361 .class_init = cryptodev_vhost_user_class_init,
362 .instance_init = cryptodev_vhost_user_instance_int,
363 .instance_finalize = cryptodev_vhost_user_finalize,
364 .instance_size = sizeof(CryptoDevBackendVhostUser),
365};
366
367static void
368cryptodev_vhost_user_register_types(void)
369{
370 type_register_static(&cryptodev_vhost_user_info);
371}
372
373type_init(cryptodev_vhost_user_register_types);