Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 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" |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 29 | #include "hw/virtio/vhost-user.h" |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 30 | #include "standard-headers/linux/virtio_crypto.h" |
| 31 | #include "sysemu/cryptodev-vhost.h" |
| 32 | #include "chardev/char-fe.h" |
Gonglei | 5da73da | 2018-03-01 21:46:29 +0800 | [diff] [blame] | 33 | #include "sysemu/cryptodev-vhost-user.h" |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 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 | |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 50 | VhostUserState *vhost_user; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 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 | |
Gonglei | 5da73da | 2018-03-01 21:46:29 +0800 | [diff] [blame] | 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 | |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 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 | |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 107 | options.opaque = s->vhost_user; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 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 | Error *err = NULL; |
| 161 | int queues = b->conf.peers.queues; |
| 162 | |
| 163 | assert(queues < MAX_CRYPTO_QUEUE_NUM); |
| 164 | |
| 165 | switch (event) { |
| 166 | case CHR_EVENT_OPENED: |
| 167 | if (cryptodev_vhost_user_start(queues, s) < 0) { |
| 168 | exit(1); |
| 169 | } |
| 170 | b->ready = true; |
| 171 | break; |
| 172 | case CHR_EVENT_CLOSED: |
| 173 | b->ready = false; |
| 174 | cryptodev_vhost_user_stop(queues, s); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | if (err) { |
| 179 | error_report_err(err); |
| 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; |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 190 | VhostUserState *user; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 191 | CryptoDevBackendClient *cc; |
| 192 | CryptoDevBackendVhostUser *s = |
| 193 | CRYPTODEV_BACKEND_VHOST_USER(backend); |
| 194 | |
| 195 | chr = cryptodev_vhost_claim_chardev(s, &local_err); |
| 196 | if (local_err) { |
| 197 | error_propagate(errp, local_err); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | s->opened = true; |
| 202 | |
| 203 | for (i = 0; i < queues; i++) { |
| 204 | cc = cryptodev_backend_new_client( |
| 205 | "cryptodev-vhost-user", NULL); |
| 206 | cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ", |
| 207 | i, chr->label); |
| 208 | cc->queue_index = i; |
Gonglei | 5da73da | 2018-03-01 21:46:29 +0800 | [diff] [blame] | 209 | cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 210 | |
| 211 | backend->conf.peers.ccs[i] = cc; |
| 212 | |
| 213 | if (i == 0) { |
| 214 | if (!qemu_chr_fe_init(&s->chr, chr, &local_err)) { |
| 215 | error_propagate(errp, local_err); |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 221 | user = vhost_user_init(); |
| 222 | if (!user) { |
| 223 | error_setg(errp, "Failed to init vhost_user"); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | user->chr = &s->chr; |
| 228 | s->vhost_user = user; |
| 229 | |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 230 | qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, |
| 231 | cryptodev_vhost_user_event, NULL, s, NULL, true); |
| 232 | |
| 233 | backend->conf.crypto_services = |
| 234 | 1u << VIRTIO_CRYPTO_SERVICE_CIPHER | |
| 235 | 1u << VIRTIO_CRYPTO_SERVICE_HASH | |
| 236 | 1u << VIRTIO_CRYPTO_SERVICE_MAC; |
| 237 | backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC; |
| 238 | backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1; |
Gonglei | 0a9b9be | 2018-03-01 21:46:31 +0800 | [diff] [blame] | 239 | |
| 240 | backend->conf.max_size = UINT64_MAX; |
| 241 | backend->conf.max_cipher_key_len = VHOST_USER_MAX_CIPHER_KEY_LEN; |
| 242 | backend->conf.max_auth_key_len = VHOST_USER_MAX_AUTH_KEY_LEN; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static int64_t cryptodev_vhost_user_sym_create_session( |
| 246 | CryptoDevBackend *backend, |
| 247 | CryptoDevBackendSymSessionInfo *sess_info, |
| 248 | uint32_t queue_index, Error **errp) |
| 249 | { |
Gonglei | efbfeb8 | 2018-03-01 21:46:30 +0800 | [diff] [blame] | 250 | CryptoDevBackendClient *cc = |
| 251 | backend->conf.peers.ccs[queue_index]; |
| 252 | CryptoDevBackendVhost *vhost_crypto; |
| 253 | uint64_t session_id = 0; |
| 254 | int ret; |
| 255 | |
| 256 | vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index); |
| 257 | if (vhost_crypto) { |
| 258 | struct vhost_dev *dev = &(vhost_crypto->dev); |
| 259 | ret = dev->vhost_ops->vhost_crypto_create_session(dev, |
| 260 | sess_info, |
| 261 | &session_id); |
| 262 | if (ret < 0) { |
| 263 | return -1; |
| 264 | } else { |
| 265 | return session_id; |
| 266 | } |
| 267 | } |
| 268 | return -1; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int cryptodev_vhost_user_sym_close_session( |
| 272 | CryptoDevBackend *backend, |
| 273 | uint64_t session_id, |
| 274 | uint32_t queue_index, Error **errp) |
| 275 | { |
Gonglei | efbfeb8 | 2018-03-01 21:46:30 +0800 | [diff] [blame] | 276 | CryptoDevBackendClient *cc = |
| 277 | backend->conf.peers.ccs[queue_index]; |
| 278 | CryptoDevBackendVhost *vhost_crypto; |
| 279 | int ret; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 280 | |
Gonglei | efbfeb8 | 2018-03-01 21:46:30 +0800 | [diff] [blame] | 281 | vhost_crypto = cryptodev_vhost_user_get_vhost(cc, backend, queue_index); |
| 282 | if (vhost_crypto) { |
| 283 | struct vhost_dev *dev = &(vhost_crypto->dev); |
| 284 | ret = dev->vhost_ops->vhost_crypto_close_session(dev, |
| 285 | session_id); |
| 286 | if (ret < 0) { |
| 287 | return -1; |
| 288 | } else { |
| 289 | return 0; |
| 290 | } |
| 291 | } |
| 292 | return -1; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void cryptodev_vhost_user_cleanup( |
| 296 | CryptoDevBackend *backend, |
| 297 | Error **errp) |
| 298 | { |
| 299 | CryptoDevBackendVhostUser *s = |
| 300 | CRYPTODEV_BACKEND_VHOST_USER(backend); |
| 301 | size_t i; |
| 302 | int queues = backend->conf.peers.queues; |
| 303 | CryptoDevBackendClient *cc; |
| 304 | |
| 305 | cryptodev_vhost_user_stop(queues, s); |
| 306 | |
| 307 | for (i = 0; i < queues; i++) { |
| 308 | cc = backend->conf.peers.ccs[i]; |
| 309 | if (cc) { |
| 310 | cryptodev_backend_free_client(cc); |
| 311 | backend->conf.peers.ccs[i] = NULL; |
| 312 | } |
| 313 | } |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 314 | |
| 315 | if (s->vhost_user) { |
| 316 | vhost_user_cleanup(s->vhost_user); |
| 317 | g_free(s->vhost_user); |
| 318 | s->vhost_user = NULL; |
| 319 | } |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void cryptodev_vhost_user_set_chardev(Object *obj, |
| 323 | const char *value, Error **errp) |
| 324 | { |
| 325 | CryptoDevBackendVhostUser *s = |
| 326 | CRYPTODEV_BACKEND_VHOST_USER(obj); |
| 327 | |
| 328 | if (s->opened) { |
| 329 | error_setg(errp, QERR_PERMISSION_DENIED); |
| 330 | } else { |
| 331 | g_free(s->chr_name); |
| 332 | s->chr_name = g_strdup(value); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static char * |
| 337 | cryptodev_vhost_user_get_chardev(Object *obj, Error **errp) |
| 338 | { |
| 339 | CryptoDevBackendVhostUser *s = |
| 340 | CRYPTODEV_BACKEND_VHOST_USER(obj); |
| 341 | Chardev *chr = qemu_chr_fe_get_driver(&s->chr); |
| 342 | |
| 343 | if (chr && chr->label) { |
| 344 | return g_strdup(chr->label); |
| 345 | } |
| 346 | |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | static void cryptodev_vhost_user_instance_int(Object *obj) |
| 351 | { |
| 352 | object_property_add_str(obj, "chardev", |
| 353 | cryptodev_vhost_user_get_chardev, |
| 354 | cryptodev_vhost_user_set_chardev, |
| 355 | NULL); |
| 356 | } |
| 357 | |
| 358 | static void cryptodev_vhost_user_finalize(Object *obj) |
| 359 | { |
| 360 | CryptoDevBackendVhostUser *s = |
| 361 | CRYPTODEV_BACKEND_VHOST_USER(obj); |
| 362 | |
| 363 | qemu_chr_fe_deinit(&s->chr, false); |
| 364 | |
| 365 | g_free(s->chr_name); |
| 366 | } |
| 367 | |
| 368 | static void |
| 369 | cryptodev_vhost_user_class_init(ObjectClass *oc, void *data) |
| 370 | { |
| 371 | CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc); |
| 372 | |
| 373 | bc->init = cryptodev_vhost_user_init; |
| 374 | bc->cleanup = cryptodev_vhost_user_cleanup; |
| 375 | bc->create_session = cryptodev_vhost_user_sym_create_session; |
| 376 | bc->close_session = cryptodev_vhost_user_sym_close_session; |
Gonglei | efbfeb8 | 2018-03-01 21:46:30 +0800 | [diff] [blame] | 377 | bc->do_sym_op = NULL; |
Gonglei | 042cea2 | 2018-03-01 21:46:28 +0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static const TypeInfo cryptodev_vhost_user_info = { |
| 381 | .name = TYPE_CRYPTODEV_BACKEND_VHOST_USER, |
| 382 | .parent = TYPE_CRYPTODEV_BACKEND, |
| 383 | .class_init = cryptodev_vhost_user_class_init, |
| 384 | .instance_init = cryptodev_vhost_user_instance_int, |
| 385 | .instance_finalize = cryptodev_vhost_user_finalize, |
| 386 | .instance_size = sizeof(CryptoDevBackendVhostUser), |
| 387 | }; |
| 388 | |
| 389 | static void |
| 390 | cryptodev_vhost_user_register_types(void) |
| 391 | { |
| 392 | type_register_static(&cryptodev_vhost_user_info); |
| 393 | } |
| 394 | |
| 395 | type_init(cryptodev_vhost_user_register_types); |