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