qapi/block: Tidy up block-latency-histogram-set documentation
[qemu/ar7.git] / backends / cryptodev.c
blob7d295178433be5c2b3d53d9ebaee4d4513196a0d
1 /*
2 * QEMU Crypto Device Implementation
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6 * Authors:
7 * Gonglei <arei.gonglei@huawei.com>
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.1 of the License, or (at your option) any later version.
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.
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/>.
24 #include "qemu/osdep.h"
25 #include "sysemu/cryptodev.h"
26 #include "sysemu/stats.h"
27 #include "qapi/error.h"
28 #include "qapi/qapi-commands-cryptodev.h"
29 #include "qapi/qapi-types-stats.h"
30 #include "qapi/visitor.h"
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/main-loop.h"
34 #include "qom/object_interfaces.h"
35 #include "hw/virtio/virtio-crypto.h"
37 #define SYM_ENCRYPT_OPS_STR "sym-encrypt-ops"
38 #define SYM_DECRYPT_OPS_STR "sym-decrypt-ops"
39 #define SYM_ENCRYPT_BYTES_STR "sym-encrypt-bytes"
40 #define SYM_DECRYPT_BYTES_STR "sym-decrypt-bytes"
42 #define ASYM_ENCRYPT_OPS_STR "asym-encrypt-ops"
43 #define ASYM_DECRYPT_OPS_STR "asym-decrypt-ops"
44 #define ASYM_SIGN_OPS_STR "asym-sign-ops"
45 #define ASYM_VERIFY_OPS_STR "asym-verify-ops"
46 #define ASYM_ENCRYPT_BYTES_STR "asym-encrypt-bytes"
47 #define ASYM_DECRYPT_BYTES_STR "asym-decrypt-bytes"
48 #define ASYM_SIGN_BYTES_STR "asym-sign-bytes"
49 #define ASYM_VERIFY_BYTES_STR "asym-verify-bytes"
51 typedef struct StatsArgs {
52 union StatsResultsType {
53 StatsResultList **stats;
54 StatsSchemaList **schema;
55 } result;
56 strList *names;
57 Error **errp;
58 } StatsArgs;
60 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
62 static int qmp_query_cryptodev_foreach(Object *obj, void *data)
64 CryptoDevBackend *backend;
65 QCryptodevInfoList **infolist = data;
66 uint32_t services, i;
68 if (!object_dynamic_cast(obj, TYPE_CRYPTODEV_BACKEND)) {
69 return 0;
72 QCryptodevInfo *info = g_new0(QCryptodevInfo, 1);
73 info->id = g_strdup(object_get_canonical_path_component(obj));
75 backend = CRYPTODEV_BACKEND(obj);
76 services = backend->conf.crypto_services;
77 for (i = 0; i < QCRYPTODEV_BACKEND_SERVICE__MAX; i++) {
78 if (services & (1 << i)) {
79 QAPI_LIST_PREPEND(info->service, i);
83 for (i = 0; i < backend->conf.peers.queues; i++) {
84 CryptoDevBackendClient *cc = backend->conf.peers.ccs[i];
85 QCryptodevBackendClient *client = g_new0(QCryptodevBackendClient, 1);
87 client->queue = cc->queue_index;
88 client->type = cc->type;
89 QAPI_LIST_PREPEND(info->client, client);
92 QAPI_LIST_PREPEND(*infolist, info);
94 return 0;
97 QCryptodevInfoList *qmp_query_cryptodev(Error **errp)
99 QCryptodevInfoList *list = NULL;
100 Object *objs = container_get(object_get_root(), "/objects");
102 object_child_foreach(objs, qmp_query_cryptodev_foreach, &list);
104 return list;
107 CryptoDevBackendClient *cryptodev_backend_new_client(void)
109 CryptoDevBackendClient *cc;
111 cc = g_new0(CryptoDevBackendClient, 1);
112 QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
114 return cc;
117 void cryptodev_backend_free_client(
118 CryptoDevBackendClient *cc)
120 QTAILQ_REMOVE(&crypto_clients, cc, next);
121 g_free(cc->info_str);
122 g_free(cc);
125 void cryptodev_backend_cleanup(
126 CryptoDevBackend *backend,
127 Error **errp)
129 CryptoDevBackendClass *bc =
130 CRYPTODEV_BACKEND_GET_CLASS(backend);
132 if (bc->cleanup) {
133 bc->cleanup(backend, errp);
136 g_free(backend->sym_stat);
137 g_free(backend->asym_stat);
140 int cryptodev_backend_create_session(
141 CryptoDevBackend *backend,
142 CryptoDevBackendSessionInfo *sess_info,
143 uint32_t queue_index,
144 CryptoDevCompletionFunc cb,
145 void *opaque)
147 CryptoDevBackendClass *bc =
148 CRYPTODEV_BACKEND_GET_CLASS(backend);
150 if (bc->create_session) {
151 return bc->create_session(backend, sess_info, queue_index, cb, opaque);
153 return -VIRTIO_CRYPTO_NOTSUPP;
156 int cryptodev_backend_close_session(
157 CryptoDevBackend *backend,
158 uint64_t session_id,
159 uint32_t queue_index,
160 CryptoDevCompletionFunc cb,
161 void *opaque)
163 CryptoDevBackendClass *bc =
164 CRYPTODEV_BACKEND_GET_CLASS(backend);
166 if (bc->close_session) {
167 return bc->close_session(backend, session_id, queue_index, cb, opaque);
169 return -VIRTIO_CRYPTO_NOTSUPP;
172 static int cryptodev_backend_operation(
173 CryptoDevBackend *backend,
174 CryptoDevBackendOpInfo *op_info)
176 CryptoDevBackendClass *bc =
177 CRYPTODEV_BACKEND_GET_CLASS(backend);
179 if (bc->do_op) {
180 return bc->do_op(backend, op_info);
182 return -VIRTIO_CRYPTO_NOTSUPP;
185 static int cryptodev_backend_account(CryptoDevBackend *backend,
186 CryptoDevBackendOpInfo *op_info)
188 enum QCryptodevBackendAlgType algtype = op_info->algtype;
189 int len;
191 if (algtype == QCRYPTODEV_BACKEND_ALG_ASYM) {
192 CryptoDevBackendAsymOpInfo *asym_op_info = op_info->u.asym_op_info;
193 len = asym_op_info->src_len;
194 switch (op_info->op_code) {
195 case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
196 CryptodevAsymStatIncEncrypt(backend, len);
197 break;
198 case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
199 CryptodevAsymStatIncDecrypt(backend, len);
200 break;
201 case VIRTIO_CRYPTO_AKCIPHER_SIGN:
202 CryptodevAsymStatIncSign(backend, len);
203 break;
204 case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
205 CryptodevAsymStatIncVerify(backend, len);
206 break;
207 default:
208 return -VIRTIO_CRYPTO_NOTSUPP;
210 } else if (algtype == QCRYPTODEV_BACKEND_ALG_SYM) {
211 CryptoDevBackendSymOpInfo *sym_op_info = op_info->u.sym_op_info;
212 len = sym_op_info->src_len;
213 switch (op_info->op_code) {
214 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
215 CryptodevSymStatIncEncrypt(backend, len);
216 break;
217 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
218 CryptodevSymStatIncDecrypt(backend, len);
219 break;
220 default:
221 return -VIRTIO_CRYPTO_NOTSUPP;
223 } else {
224 error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
225 return -VIRTIO_CRYPTO_NOTSUPP;
228 return len;
231 static void cryptodev_backend_throttle_timer_cb(void *opaque)
233 CryptoDevBackend *backend = (CryptoDevBackend *)opaque;
234 CryptoDevBackendOpInfo *op_info, *tmpop;
235 int ret;
237 QTAILQ_FOREACH_SAFE(op_info, &backend->opinfos, next, tmpop) {
238 QTAILQ_REMOVE(&backend->opinfos, op_info, next);
239 ret = cryptodev_backend_account(backend, op_info);
240 if (ret < 0) {
241 op_info->cb(op_info->opaque, ret);
242 continue;
245 throttle_account(&backend->ts, true, ret);
246 cryptodev_backend_operation(backend, op_info);
247 if (throttle_enabled(&backend->tc) &&
248 throttle_schedule_timer(&backend->ts, &backend->tt, true)) {
249 break;
254 int cryptodev_backend_crypto_operation(
255 CryptoDevBackend *backend,
256 CryptoDevBackendOpInfo *op_info)
258 int ret;
260 if (!throttle_enabled(&backend->tc)) {
261 goto do_account;
264 if (throttle_schedule_timer(&backend->ts, &backend->tt, true) ||
265 !QTAILQ_EMPTY(&backend->opinfos)) {
266 QTAILQ_INSERT_TAIL(&backend->opinfos, op_info, next);
267 return 0;
270 do_account:
271 ret = cryptodev_backend_account(backend, op_info);
272 if (ret < 0) {
273 return ret;
276 throttle_account(&backend->ts, true, ret);
278 return cryptodev_backend_operation(backend, op_info);
281 static void
282 cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
283 void *opaque, Error **errp)
285 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
286 uint32_t value = backend->conf.peers.queues;
288 visit_type_uint32(v, name, &value, errp);
291 static void
292 cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
293 void *opaque, Error **errp)
295 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
296 uint32_t value;
298 if (!visit_type_uint32(v, name, &value, errp)) {
299 return;
301 if (!value) {
302 error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu32 "'",
303 object_get_typename(obj), name, value);
304 return;
306 backend->conf.peers.queues = value;
309 static void cryptodev_backend_set_throttle(CryptoDevBackend *backend, int field,
310 uint64_t value, Error **errp)
312 uint64_t orig = backend->tc.buckets[field].avg;
313 bool enabled = throttle_enabled(&backend->tc);
315 if (orig == value) {
316 return;
319 backend->tc.buckets[field].avg = value;
320 if (!throttle_enabled(&backend->tc)) {
321 throttle_timers_destroy(&backend->tt);
322 cryptodev_backend_throttle_timer_cb(backend); /* drain opinfos */
323 return;
326 if (!throttle_is_valid(&backend->tc, errp)) {
327 backend->tc.buckets[field].avg = orig; /* revert change */
328 return;
331 if (!enabled) {
332 throttle_init(&backend->ts);
333 throttle_timers_init(&backend->tt, qemu_get_aio_context(),
334 QEMU_CLOCK_REALTIME,
335 cryptodev_backend_throttle_timer_cb, /* FIXME */
336 cryptodev_backend_throttle_timer_cb, backend);
339 throttle_config(&backend->ts, QEMU_CLOCK_REALTIME, &backend->tc);
342 static void cryptodev_backend_get_bps(Object *obj, Visitor *v,
343 const char *name, void *opaque,
344 Error **errp)
346 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
347 uint64_t value = backend->tc.buckets[THROTTLE_BPS_TOTAL].avg;
349 visit_type_uint64(v, name, &value, errp);
352 static void cryptodev_backend_set_bps(Object *obj, Visitor *v, const char *name,
353 void *opaque, Error **errp)
355 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
356 uint64_t value;
358 if (!visit_type_uint64(v, name, &value, errp)) {
359 return;
362 cryptodev_backend_set_throttle(backend, THROTTLE_BPS_TOTAL, value, errp);
365 static void cryptodev_backend_get_ops(Object *obj, Visitor *v, const char *name,
366 void *opaque, Error **errp)
368 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
369 uint64_t value = backend->tc.buckets[THROTTLE_OPS_TOTAL].avg;
371 visit_type_uint64(v, name, &value, errp);
374 static void cryptodev_backend_set_ops(Object *obj, Visitor *v,
375 const char *name, void *opaque,
376 Error **errp)
378 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
379 uint64_t value;
381 if (!visit_type_uint64(v, name, &value, errp)) {
382 return;
385 cryptodev_backend_set_throttle(backend, THROTTLE_OPS_TOTAL, value, errp);
388 static void
389 cryptodev_backend_complete(UserCreatable *uc, Error **errp)
391 CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
392 CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
393 uint32_t services;
394 uint64_t value;
396 QTAILQ_INIT(&backend->opinfos);
397 value = backend->tc.buckets[THROTTLE_OPS_TOTAL].avg;
398 cryptodev_backend_set_throttle(backend, THROTTLE_OPS_TOTAL, value, errp);
399 value = backend->tc.buckets[THROTTLE_BPS_TOTAL].avg;
400 cryptodev_backend_set_throttle(backend, THROTTLE_BPS_TOTAL, value, errp);
402 if (bc->init) {
403 bc->init(backend, errp);
406 services = backend->conf.crypto_services;
407 if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_CIPHER)) {
408 backend->sym_stat = g_new0(CryptodevBackendSymStat, 1);
411 if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER)) {
412 backend->asym_stat = g_new0(CryptodevBackendAsymStat, 1);
416 void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
418 backend->is_used = used;
421 bool cryptodev_backend_is_used(CryptoDevBackend *backend)
423 return backend->is_used;
426 void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
428 backend->ready = ready;
431 bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
433 return backend->ready;
436 static bool
437 cryptodev_backend_can_be_deleted(UserCreatable *uc)
439 return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
442 static void cryptodev_backend_instance_init(Object *obj)
444 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
446 /* Initialize devices' queues property to 1 */
447 object_property_set_int(obj, "queues", 1, NULL);
449 throttle_config_init(&backend->tc);
452 static void cryptodev_backend_finalize(Object *obj)
454 CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
456 cryptodev_backend_cleanup(backend, NULL);
457 if (throttle_enabled(&backend->tc)) {
458 throttle_timers_destroy(&backend->tt);
462 static StatsList *cryptodev_backend_stats_add(const char *name, int64_t *val,
463 StatsList *stats_list)
465 Stats *stats = g_new0(Stats, 1);
467 stats->name = g_strdup(name);
468 stats->value = g_new0(StatsValue, 1);
469 stats->value->type = QTYPE_QNUM;
470 stats->value->u.scalar = *val;
472 QAPI_LIST_PREPEND(stats_list, stats);
473 return stats_list;
476 static int cryptodev_backend_stats_query(Object *obj, void *data)
478 StatsArgs *stats_args = data;
479 StatsResultList **stats_results = stats_args->result.stats;
480 StatsList *stats_list = NULL;
481 StatsResult *entry;
482 CryptoDevBackend *backend;
483 CryptodevBackendSymStat *sym_stat;
484 CryptodevBackendAsymStat *asym_stat;
486 if (!object_dynamic_cast(obj, TYPE_CRYPTODEV_BACKEND)) {
487 return 0;
490 backend = CRYPTODEV_BACKEND(obj);
491 sym_stat = backend->sym_stat;
492 if (sym_stat) {
493 stats_list = cryptodev_backend_stats_add(SYM_ENCRYPT_OPS_STR,
494 &sym_stat->encrypt_ops, stats_list);
495 stats_list = cryptodev_backend_stats_add(SYM_DECRYPT_OPS_STR,
496 &sym_stat->decrypt_ops, stats_list);
497 stats_list = cryptodev_backend_stats_add(SYM_ENCRYPT_BYTES_STR,
498 &sym_stat->encrypt_bytes, stats_list);
499 stats_list = cryptodev_backend_stats_add(SYM_DECRYPT_BYTES_STR,
500 &sym_stat->decrypt_bytes, stats_list);
503 asym_stat = backend->asym_stat;
504 if (asym_stat) {
505 stats_list = cryptodev_backend_stats_add(ASYM_ENCRYPT_OPS_STR,
506 &asym_stat->encrypt_ops, stats_list);
507 stats_list = cryptodev_backend_stats_add(ASYM_DECRYPT_OPS_STR,
508 &asym_stat->decrypt_ops, stats_list);
509 stats_list = cryptodev_backend_stats_add(ASYM_SIGN_OPS_STR,
510 &asym_stat->sign_ops, stats_list);
511 stats_list = cryptodev_backend_stats_add(ASYM_VERIFY_OPS_STR,
512 &asym_stat->verify_ops, stats_list);
513 stats_list = cryptodev_backend_stats_add(ASYM_ENCRYPT_BYTES_STR,
514 &asym_stat->encrypt_bytes, stats_list);
515 stats_list = cryptodev_backend_stats_add(ASYM_DECRYPT_BYTES_STR,
516 &asym_stat->decrypt_bytes, stats_list);
517 stats_list = cryptodev_backend_stats_add(ASYM_SIGN_BYTES_STR,
518 &asym_stat->sign_bytes, stats_list);
519 stats_list = cryptodev_backend_stats_add(ASYM_VERIFY_BYTES_STR,
520 &asym_stat->verify_bytes, stats_list);
523 entry = g_new0(StatsResult, 1);
524 entry->provider = STATS_PROVIDER_CRYPTODEV;
525 entry->qom_path = object_get_canonical_path(obj);
526 entry->stats = stats_list;
527 QAPI_LIST_PREPEND(*stats_results, entry);
529 return 0;
532 static void cryptodev_backend_stats_cb(StatsResultList **result,
533 StatsTarget target,
534 strList *names, strList *targets,
535 Error **errp)
537 switch (target) {
538 case STATS_TARGET_CRYPTODEV:
540 Object *objs = container_get(object_get_root(), "/objects");
541 StatsArgs stats_args;
542 stats_args.result.stats = result;
543 stats_args.names = names;
544 stats_args.errp = errp;
546 object_child_foreach(objs, cryptodev_backend_stats_query, &stats_args);
547 break;
549 default:
550 break;
554 static StatsSchemaValueList *cryptodev_backend_schemas_add(const char *name,
555 StatsSchemaValueList *list)
557 StatsSchemaValueList *schema_entry = g_new0(StatsSchemaValueList, 1);
559 schema_entry->value = g_new0(StatsSchemaValue, 1);
560 schema_entry->value->type = STATS_TYPE_CUMULATIVE;
561 schema_entry->value->name = g_strdup(name);
562 schema_entry->next = list;
564 return schema_entry;
567 static void cryptodev_backend_schemas_cb(StatsSchemaList **result,
568 Error **errp)
570 StatsSchemaValueList *stats_list = NULL;
571 const char *sym_stats[] = { SYM_ENCRYPT_OPS_STR, SYM_DECRYPT_OPS_STR,
572 SYM_ENCRYPT_BYTES_STR, SYM_DECRYPT_BYTES_STR };
573 const char *asym_stats[] = { ASYM_ENCRYPT_OPS_STR, ASYM_DECRYPT_OPS_STR,
574 ASYM_SIGN_OPS_STR, ASYM_VERIFY_OPS_STR,
575 ASYM_ENCRYPT_BYTES_STR, ASYM_DECRYPT_BYTES_STR,
576 ASYM_SIGN_BYTES_STR, ASYM_VERIFY_BYTES_STR };
578 for (int i = 0; i < ARRAY_SIZE(sym_stats); i++) {
579 stats_list = cryptodev_backend_schemas_add(sym_stats[i], stats_list);
582 for (int i = 0; i < ARRAY_SIZE(asym_stats); i++) {
583 stats_list = cryptodev_backend_schemas_add(asym_stats[i], stats_list);
586 add_stats_schema(result, STATS_PROVIDER_CRYPTODEV, STATS_TARGET_CRYPTODEV,
587 stats_list);
590 static void
591 cryptodev_backend_class_init(ObjectClass *oc, void *data)
593 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
595 ucc->complete = cryptodev_backend_complete;
596 ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
598 QTAILQ_INIT(&crypto_clients);
599 object_class_property_add(oc, "queues", "uint32",
600 cryptodev_backend_get_queues,
601 cryptodev_backend_set_queues,
602 NULL, NULL);
603 object_class_property_add(oc, "throttle-bps", "uint64",
604 cryptodev_backend_get_bps,
605 cryptodev_backend_set_bps,
606 NULL, NULL);
607 object_class_property_add(oc, "throttle-ops", "uint64",
608 cryptodev_backend_get_ops,
609 cryptodev_backend_set_ops,
610 NULL, NULL);
612 add_stats_callbacks(STATS_PROVIDER_CRYPTODEV, cryptodev_backend_stats_cb,
613 cryptodev_backend_schemas_cb);
616 static const TypeInfo cryptodev_backend_info = {
617 .name = TYPE_CRYPTODEV_BACKEND,
618 .parent = TYPE_OBJECT,
619 .instance_size = sizeof(CryptoDevBackend),
620 .instance_init = cryptodev_backend_instance_init,
621 .instance_finalize = cryptodev_backend_finalize,
622 .class_size = sizeof(CryptoDevBackendClass),
623 .class_init = cryptodev_backend_class_init,
624 .interfaces = (InterfaceInfo[]) {
625 { TYPE_USER_CREATABLE },
630 static void
631 cryptodev_backend_register_types(void)
633 type_register_static(&cryptodev_backend_info);
636 type_init(cryptodev_backend_register_types);