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 "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"
33 #include "qom/object.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 typedef struct CryptoDevBackendVhostUser CryptoDevBackendVhostUser;
43 DECLARE_INSTANCE_CHECKER(CryptoDevBackendVhostUser, CRYPTODEV_BACKEND_VHOST_USER,
44                          TYPE_CRYPTODEV_BACKEND_VHOST_USER)
45 
46 
47 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 };
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, QEMUChrEvent 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     case CHR_EVENT_BREAK:
176     case CHR_EVENT_MUX_IN:
177     case CHR_EVENT_MUX_OUT:
178         /* Ignore */
179         break;
180     }
181 }
182 
183 static void cryptodev_vhost_user_init(
184              CryptoDevBackend *backend, Error **errp)
185 {
186     int queues = backend->conf.peers.queues;
187     size_t i;
188     Error *local_err = NULL;
189     Chardev *chr;
190     CryptoDevBackendClient *cc;
191     CryptoDevBackendVhostUser *s =
192                       CRYPTODEV_BACKEND_VHOST_USER(backend);
193 
194     chr = cryptodev_vhost_claim_chardev(s, &local_err);
195     if (local_err) {
196         error_propagate(errp, local_err);
197         return;
198     }
199 
200     s->opened = true;
201 
202     for (i = 0; i < queues; i++) {
203         cc = cryptodev_backend_new_client(
204                   "cryptodev-vhost-user", NULL);
205         cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
206                                        i, chr->label);
207         cc->queue_index = i;
208         cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
209 
210         backend->conf.peers.ccs[i] = cc;
211 
212         if (i == 0) {
213             if (!qemu_chr_fe_init(&s->chr, chr, errp)) {
214                 return;
215             }
216         }
217     }
218 
219     if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
220         return;
221     }
222 
223     qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
224                      cryptodev_vhost_user_event, NULL, s, NULL, true);
225 
226     backend->conf.crypto_services =
227                          1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
228                          1u << VIRTIO_CRYPTO_SERVICE_HASH |
229                          1u << VIRTIO_CRYPTO_SERVICE_MAC;
230     backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
231     backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
232 
233     backend->conf.max_size = UINT64_MAX;
234     backend->conf.max_cipher_key_len = VHOST_USER_MAX_CIPHER_KEY_LEN;
235     backend->conf.max_auth_key_len = VHOST_USER_MAX_AUTH_KEY_LEN;
236 }
237 
238 static int64_t cryptodev_vhost_user_sym_create_session(
239            CryptoDevBackend *backend,
240            CryptoDevBackendSymSessionInfo *sess_info,
241            uint32_t queue_index, Error **errp)
242 {
243     CryptoDevBackendClient *cc =
244                    backend->conf.peers.ccs[queue_index];
245     CryptoDevBackendVhost *vhost_crypto;
246     uint64_t session_id = 0;
247     int ret;
248 
249     vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
250     if (vhost_crypto) {
251         struct vhost_dev *dev = &(vhost_crypto->dev);
252         ret = dev->vhost_ops->vhost_crypto_create_session(dev,
253                                                           sess_info,
254                                                           &session_id);
255         if (ret < 0) {
256             return -1;
257         } else {
258             return session_id;
259         }
260     }
261     return -1;
262 }
263 
264 static int cryptodev_vhost_user_sym_close_session(
265            CryptoDevBackend *backend,
266            uint64_t session_id,
267            uint32_t queue_index, Error **errp)
268 {
269     CryptoDevBackendClient *cc =
270                   backend->conf.peers.ccs[queue_index];
271     CryptoDevBackendVhost *vhost_crypto;
272     int ret;
273 
274     vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index);
275     if (vhost_crypto) {
276         struct vhost_dev *dev = &(vhost_crypto->dev);
277         ret = dev->vhost_ops->vhost_crypto_close_session(dev,
278                                                          session_id);
279         if (ret < 0) {
280             return -1;
281         } else {
282             return 0;
283         }
284     }
285     return -1;
286 }
287 
288 static void cryptodev_vhost_user_cleanup(
289              CryptoDevBackend *backend,
290              Error **errp)
291 {
292     CryptoDevBackendVhostUser *s =
293                       CRYPTODEV_BACKEND_VHOST_USER(backend);
294     size_t i;
295     int queues = backend->conf.peers.queues;
296     CryptoDevBackendClient *cc;
297 
298     cryptodev_vhost_user_stop(queues, s);
299 
300     for (i = 0; i < queues; i++) {
301         cc = backend->conf.peers.ccs[i];
302         if (cc) {
303             cryptodev_backend_free_client(cc);
304             backend->conf.peers.ccs[i] = NULL;
305         }
306     }
307 
308     vhost_user_cleanup(&s->vhost_user);
309 }
310 
311 static void cryptodev_vhost_user_set_chardev(Object *obj,
312                                     const char *value, Error **errp)
313 {
314     CryptoDevBackendVhostUser *s =
315                       CRYPTODEV_BACKEND_VHOST_USER(obj);
316 
317     if (s->opened) {
318         error_setg(errp, QERR_PERMISSION_DENIED);
319     } else {
320         g_free(s->chr_name);
321         s->chr_name = g_strdup(value);
322     }
323 }
324 
325 static char *
326 cryptodev_vhost_user_get_chardev(Object *obj, Error **errp)
327 {
328     CryptoDevBackendVhostUser *s =
329                       CRYPTODEV_BACKEND_VHOST_USER(obj);
330     Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
331 
332     if (chr && chr->label) {
333         return g_strdup(chr->label);
334     }
335 
336     return NULL;
337 }
338 
339 static void cryptodev_vhost_user_instance_int(Object *obj)
340 {
341     object_property_add_str(obj, "chardev",
342                             cryptodev_vhost_user_get_chardev,
343                             cryptodev_vhost_user_set_chardev);
344 }
345 
346 static void cryptodev_vhost_user_finalize(Object *obj)
347 {
348     CryptoDevBackendVhostUser *s =
349                       CRYPTODEV_BACKEND_VHOST_USER(obj);
350 
351     qemu_chr_fe_deinit(&s->chr, false);
352 
353     g_free(s->chr_name);
354 }
355 
356 static void
357 cryptodev_vhost_user_class_init(ObjectClass *oc, void *data)
358 {
359     CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
360 
361     bc->init = cryptodev_vhost_user_init;
362     bc->cleanup = cryptodev_vhost_user_cleanup;
363     bc->create_session = cryptodev_vhost_user_sym_create_session;
364     bc->close_session = cryptodev_vhost_user_sym_close_session;
365     bc->do_sym_op = NULL;
366 }
367 
368 static const TypeInfo cryptodev_vhost_user_info = {
369     .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER,
370     .parent = TYPE_CRYPTODEV_BACKEND,
371     .class_init = cryptodev_vhost_user_class_init,
372     .instance_init = cryptodev_vhost_user_instance_int,
373     .instance_finalize = cryptodev_vhost_user_finalize,
374     .instance_size = sizeof(CryptoDevBackendVhostUser),
375 };
376 
377 static void
378 cryptodev_vhost_user_register_types(void)
379 {
380     type_register_static(&cryptodev_vhost_user_info);
381 }
382 
383 type_init(cryptodev_vhost_user_register_types);
384