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