xref: /qemu/backends/cryptodev.c (revision b1019999)
1 /*
2  * QEMU Crypto Device Implementation
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.1 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 "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"
36 
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"
41 
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"
50 
51 typedef struct StatsArgs {
52     union StatsResultsType {
53         StatsResultList **stats;
54         StatsSchemaList **schema;
55     } result;
56     strList *names;
57     Error **errp;
58 } StatsArgs;
59 
60 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
61 
qmp_query_cryptodev_foreach(Object * obj,void * data)62 static int qmp_query_cryptodev_foreach(Object *obj, void *data)
63 {
64     CryptoDevBackend *backend;
65     QCryptodevInfoList **infolist = data;
66     uint32_t services, i;
67 
68     if (!object_dynamic_cast(obj, TYPE_CRYPTODEV_BACKEND)) {
69         return 0;
70     }
71 
72     QCryptodevInfo *info = g_new0(QCryptodevInfo, 1);
73     info->id = g_strdup(object_get_canonical_path_component(obj));
74 
75     backend = CRYPTODEV_BACKEND(obj);
76     services = backend->conf.crypto_services;
77     for (i = 0; i < QCRYPTODEV_BACKEND_SERVICE_TYPE__MAX; i++) {
78         if (services & (1 << i)) {
79             QAPI_LIST_PREPEND(info->service, i);
80         }
81     }
82 
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);
86 
87         client->queue = cc->queue_index;
88         client->type = cc->type;
89         QAPI_LIST_PREPEND(info->client, client);
90     }
91 
92     QAPI_LIST_PREPEND(*infolist, info);
93 
94     return 0;
95 }
96 
qmp_query_cryptodev(Error ** errp)97 QCryptodevInfoList *qmp_query_cryptodev(Error **errp)
98 {
99     QCryptodevInfoList *list = NULL;
100     Object *objs = container_get(object_get_root(), "/objects");
101 
102     object_child_foreach(objs, qmp_query_cryptodev_foreach, &list);
103 
104     return list;
105 }
106 
cryptodev_backend_new_client(void)107 CryptoDevBackendClient *cryptodev_backend_new_client(void)
108 {
109     CryptoDevBackendClient *cc;
110 
111     cc = g_new0(CryptoDevBackendClient, 1);
112     QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
113 
114     return cc;
115 }
116 
cryptodev_backend_free_client(CryptoDevBackendClient * cc)117 void cryptodev_backend_free_client(
118                   CryptoDevBackendClient *cc)
119 {
120     QTAILQ_REMOVE(&crypto_clients, cc, next);
121     g_free(cc->info_str);
122     g_free(cc);
123 }
124 
cryptodev_backend_cleanup(CryptoDevBackend * backend,Error ** errp)125 void cryptodev_backend_cleanup(
126              CryptoDevBackend *backend,
127              Error **errp)
128 {
129     CryptoDevBackendClass *bc =
130                   CRYPTODEV_BACKEND_GET_CLASS(backend);
131 
132     if (bc->cleanup) {
133         bc->cleanup(backend, errp);
134     }
135 
136     g_free(backend->sym_stat);
137     g_free(backend->asym_stat);
138 }
139 
cryptodev_backend_create_session(CryptoDevBackend * backend,CryptoDevBackendSessionInfo * sess_info,uint32_t queue_index,CryptoDevCompletionFunc cb,void * opaque)140 int cryptodev_backend_create_session(
141            CryptoDevBackend *backend,
142            CryptoDevBackendSessionInfo *sess_info,
143            uint32_t queue_index,
144            CryptoDevCompletionFunc cb,
145            void *opaque)
146 {
147     CryptoDevBackendClass *bc =
148                       CRYPTODEV_BACKEND_GET_CLASS(backend);
149 
150     if (bc->create_session) {
151         return bc->create_session(backend, sess_info, queue_index, cb, opaque);
152     }
153     return -VIRTIO_CRYPTO_NOTSUPP;
154 }
155 
cryptodev_backend_close_session(CryptoDevBackend * backend,uint64_t session_id,uint32_t queue_index,CryptoDevCompletionFunc cb,void * opaque)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)
162 {
163     CryptoDevBackendClass *bc =
164                       CRYPTODEV_BACKEND_GET_CLASS(backend);
165 
166     if (bc->close_session) {
167         return bc->close_session(backend, session_id, queue_index, cb, opaque);
168     }
169     return -VIRTIO_CRYPTO_NOTSUPP;
170 }
171 
cryptodev_backend_operation(CryptoDevBackend * backend,CryptoDevBackendOpInfo * op_info)172 static int cryptodev_backend_operation(
173                  CryptoDevBackend *backend,
174                  CryptoDevBackendOpInfo *op_info)
175 {
176     CryptoDevBackendClass *bc =
177                       CRYPTODEV_BACKEND_GET_CLASS(backend);
178 
179     if (bc->do_op) {
180         return bc->do_op(backend, op_info);
181     }
182     return -VIRTIO_CRYPTO_NOTSUPP;
183 }
184 
cryptodev_backend_account(CryptoDevBackend * backend,CryptoDevBackendOpInfo * op_info)185 static int cryptodev_backend_account(CryptoDevBackend *backend,
186                  CryptoDevBackendOpInfo *op_info)
187 {
188     enum QCryptodevBackendAlgoType algtype = op_info->algtype;
189     int len;
190 
191     if (algtype == QCRYPTODEV_BACKEND_ALGO_TYPE_ASYM) {
192         CryptoDevBackendAsymOpInfo *asym_op_info = op_info->u.asym_op_info;
193         len = asym_op_info->src_len;
194 
195         if (unlikely(!backend->asym_stat)) {
196             error_report("cryptodev: Unexpected asym operation");
197             return -VIRTIO_CRYPTO_NOTSUPP;
198         }
199         switch (op_info->op_code) {
200         case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
201             CryptodevAsymStatIncEncrypt(backend, len);
202             break;
203         case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
204             CryptodevAsymStatIncDecrypt(backend, len);
205             break;
206         case VIRTIO_CRYPTO_AKCIPHER_SIGN:
207             CryptodevAsymStatIncSign(backend, len);
208             break;
209         case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
210             CryptodevAsymStatIncVerify(backend, len);
211             break;
212         default:
213             return -VIRTIO_CRYPTO_NOTSUPP;
214         }
215     } else if (algtype == QCRYPTODEV_BACKEND_ALGO_TYPE_SYM) {
216         CryptoDevBackendSymOpInfo *sym_op_info = op_info->u.sym_op_info;
217         len = sym_op_info->src_len;
218 
219         if (unlikely(!backend->sym_stat)) {
220             error_report("cryptodev: Unexpected sym operation");
221             return -VIRTIO_CRYPTO_NOTSUPP;
222         }
223         switch (op_info->op_code) {
224         case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
225             CryptodevSymStatIncEncrypt(backend, len);
226             break;
227         case VIRTIO_CRYPTO_CIPHER_DECRYPT:
228             CryptodevSymStatIncDecrypt(backend, len);
229             break;
230         default:
231             return -VIRTIO_CRYPTO_NOTSUPP;
232         }
233     } else {
234         error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
235         return -VIRTIO_CRYPTO_NOTSUPP;
236     }
237 
238     return len;
239 }
240 
cryptodev_backend_throttle_timer_cb(void * opaque)241 static void cryptodev_backend_throttle_timer_cb(void *opaque)
242 {
243     CryptoDevBackend *backend = (CryptoDevBackend *)opaque;
244     CryptoDevBackendOpInfo *op_info, *tmpop;
245     int ret;
246 
247     QTAILQ_FOREACH_SAFE(op_info, &backend->opinfos, next, tmpop) {
248         QTAILQ_REMOVE(&backend->opinfos, op_info, next);
249         ret = cryptodev_backend_account(backend, op_info);
250         if (ret < 0) {
251             op_info->cb(op_info->opaque, ret);
252             continue;
253         }
254 
255         throttle_account(&backend->ts, THROTTLE_WRITE, ret);
256         cryptodev_backend_operation(backend, op_info);
257         if (throttle_enabled(&backend->tc) &&
258             throttle_schedule_timer(&backend->ts, &backend->tt,
259                                     THROTTLE_WRITE)) {
260             break;
261         }
262     }
263 }
264 
cryptodev_backend_crypto_operation(CryptoDevBackend * backend,CryptoDevBackendOpInfo * op_info)265 int cryptodev_backend_crypto_operation(
266                  CryptoDevBackend *backend,
267                  CryptoDevBackendOpInfo *op_info)
268 {
269     int ret;
270 
271     if (!throttle_enabled(&backend->tc)) {
272         goto do_account;
273     }
274 
275     if (throttle_schedule_timer(&backend->ts, &backend->tt, THROTTLE_WRITE) ||
276         !QTAILQ_EMPTY(&backend->opinfos)) {
277         QTAILQ_INSERT_TAIL(&backend->opinfos, op_info, next);
278         return 0;
279     }
280 
281 do_account:
282     ret = cryptodev_backend_account(backend, op_info);
283     if (ret < 0) {
284         return ret;
285     }
286 
287     throttle_account(&backend->ts, THROTTLE_WRITE, ret);
288 
289     return cryptodev_backend_operation(backend, op_info);
290 }
291 
292 static void
cryptodev_backend_get_queues(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)293 cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
294                              void *opaque, Error **errp)
295 {
296     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
297     uint32_t value = backend->conf.peers.queues;
298 
299     visit_type_uint32(v, name, &value, errp);
300 }
301 
302 static void
cryptodev_backend_set_queues(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)303 cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
304                              void *opaque, Error **errp)
305 {
306     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
307     uint32_t value;
308 
309     if (!visit_type_uint32(v, name, &value, errp)) {
310         return;
311     }
312     if (!value) {
313         error_setg(errp, "Property '%s.%s' doesn't take value '%" PRIu32 "'",
314                    object_get_typename(obj), name, value);
315         return;
316     }
317     backend->conf.peers.queues = value;
318 }
319 
cryptodev_backend_set_throttle(CryptoDevBackend * backend,int field,uint64_t value,Error ** errp)320 static void cryptodev_backend_set_throttle(CryptoDevBackend *backend, int field,
321                                            uint64_t value, Error **errp)
322 {
323     uint64_t orig = backend->tc.buckets[field].avg;
324     bool enabled = throttle_enabled(&backend->tc);
325 
326     if (orig == value) {
327         return;
328     }
329 
330     backend->tc.buckets[field].avg = value;
331     if (!throttle_enabled(&backend->tc)) {
332         throttle_timers_destroy(&backend->tt);
333         cryptodev_backend_throttle_timer_cb(backend); /* drain opinfos */
334         return;
335     }
336 
337     if (!throttle_is_valid(&backend->tc, errp)) {
338         backend->tc.buckets[field].avg = orig; /* revert change */
339         return;
340     }
341 
342     if (!enabled) {
343         throttle_init(&backend->ts);
344         throttle_timers_init(&backend->tt, qemu_get_aio_context(),
345                              QEMU_CLOCK_REALTIME, NULL,
346                              cryptodev_backend_throttle_timer_cb, backend);
347     }
348 
349     throttle_config(&backend->ts, QEMU_CLOCK_REALTIME, &backend->tc);
350 }
351 
cryptodev_backend_get_bps(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)352 static void cryptodev_backend_get_bps(Object *obj, Visitor *v,
353                                       const char *name, void *opaque,
354                                       Error **errp)
355 {
356     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
357     uint64_t value = backend->tc.buckets[THROTTLE_BPS_TOTAL].avg;
358 
359     visit_type_uint64(v, name, &value, errp);
360 }
361 
cryptodev_backend_set_bps(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)362 static void cryptodev_backend_set_bps(Object *obj, Visitor *v, const char *name,
363                                       void *opaque, Error **errp)
364 {
365     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
366     uint64_t value;
367 
368     if (!visit_type_uint64(v, name, &value, errp)) {
369         return;
370     }
371 
372     cryptodev_backend_set_throttle(backend, THROTTLE_BPS_TOTAL, value, errp);
373 }
374 
cryptodev_backend_get_ops(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)375 static void cryptodev_backend_get_ops(Object *obj, Visitor *v, const char *name,
376                                       void *opaque, Error **errp)
377 {
378     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
379     uint64_t value = backend->tc.buckets[THROTTLE_OPS_TOTAL].avg;
380 
381     visit_type_uint64(v, name, &value, errp);
382 }
383 
cryptodev_backend_set_ops(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)384 static void cryptodev_backend_set_ops(Object *obj, Visitor *v,
385                                        const char *name, void *opaque,
386                                        Error **errp)
387 {
388     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
389     uint64_t value;
390 
391     if (!visit_type_uint64(v, name, &value, errp)) {
392         return;
393     }
394 
395     cryptodev_backend_set_throttle(backend, THROTTLE_OPS_TOTAL, value, errp);
396 }
397 
398 static void
cryptodev_backend_complete(UserCreatable * uc,Error ** errp)399 cryptodev_backend_complete(UserCreatable *uc, Error **errp)
400 {
401     ERRP_GUARD();
402     CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
403     CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
404     uint32_t services;
405     uint64_t value;
406 
407     QTAILQ_INIT(&backend->opinfos);
408     value = backend->tc.buckets[THROTTLE_OPS_TOTAL].avg;
409     cryptodev_backend_set_throttle(backend, THROTTLE_OPS_TOTAL, value, errp);
410     if (*errp) {
411         return;
412     }
413     value = backend->tc.buckets[THROTTLE_BPS_TOTAL].avg;
414     cryptodev_backend_set_throttle(backend, THROTTLE_BPS_TOTAL, value, errp);
415     if (*errp) {
416         return;
417     }
418 
419     if (bc->init) {
420         bc->init(backend, errp);
421         if (*errp) {
422             return;
423         }
424     }
425 
426     services = backend->conf.crypto_services;
427     if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_TYPE_CIPHER)) {
428         backend->sym_stat = g_new0(CryptodevBackendSymStat, 1);
429     }
430 
431     if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_TYPE_AKCIPHER)) {
432         backend->asym_stat = g_new0(CryptodevBackendAsymStat, 1);
433     }
434 }
435 
cryptodev_backend_set_used(CryptoDevBackend * backend,bool used)436 void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
437 {
438     backend->is_used = used;
439 }
440 
cryptodev_backend_is_used(CryptoDevBackend * backend)441 bool cryptodev_backend_is_used(CryptoDevBackend *backend)
442 {
443     return backend->is_used;
444 }
445 
cryptodev_backend_set_ready(CryptoDevBackend * backend,bool ready)446 void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
447 {
448     backend->ready = ready;
449 }
450 
cryptodev_backend_is_ready(CryptoDevBackend * backend)451 bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
452 {
453     return backend->ready;
454 }
455 
456 static bool
cryptodev_backend_can_be_deleted(UserCreatable * uc)457 cryptodev_backend_can_be_deleted(UserCreatable *uc)
458 {
459     return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
460 }
461 
cryptodev_backend_instance_init(Object * obj)462 static void cryptodev_backend_instance_init(Object *obj)
463 {
464     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
465 
466     /* Initialize devices' queues property to 1 */
467     object_property_set_int(obj, "queues", 1, NULL);
468 
469     throttle_config_init(&backend->tc);
470 }
471 
cryptodev_backend_finalize(Object * obj)472 static void cryptodev_backend_finalize(Object *obj)
473 {
474     CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
475 
476     cryptodev_backend_cleanup(backend, NULL);
477     if (throttle_enabled(&backend->tc)) {
478         throttle_timers_destroy(&backend->tt);
479     }
480 }
481 
cryptodev_backend_stats_add(const char * name,int64_t * val,StatsList * stats_list)482 static StatsList *cryptodev_backend_stats_add(const char *name, int64_t *val,
483                                               StatsList *stats_list)
484 {
485     Stats *stats = g_new0(Stats, 1);
486 
487     stats->name = g_strdup(name);
488     stats->value = g_new0(StatsValue, 1);
489     stats->value->type = QTYPE_QNUM;
490     stats->value->u.scalar = *val;
491 
492     QAPI_LIST_PREPEND(stats_list, stats);
493     return stats_list;
494 }
495 
cryptodev_backend_stats_query(Object * obj,void * data)496 static int cryptodev_backend_stats_query(Object *obj, void *data)
497 {
498     StatsArgs *stats_args = data;
499     StatsResultList **stats_results = stats_args->result.stats;
500     StatsList *stats_list = NULL;
501     StatsResult *entry;
502     CryptoDevBackend *backend;
503     CryptodevBackendSymStat *sym_stat;
504     CryptodevBackendAsymStat *asym_stat;
505 
506     if (!object_dynamic_cast(obj, TYPE_CRYPTODEV_BACKEND)) {
507         return 0;
508     }
509 
510     backend = CRYPTODEV_BACKEND(obj);
511     sym_stat = backend->sym_stat;
512     if (sym_stat) {
513         stats_list = cryptodev_backend_stats_add(SYM_ENCRYPT_OPS_STR,
514                          &sym_stat->encrypt_ops, stats_list);
515         stats_list = cryptodev_backend_stats_add(SYM_DECRYPT_OPS_STR,
516                          &sym_stat->decrypt_ops, stats_list);
517         stats_list = cryptodev_backend_stats_add(SYM_ENCRYPT_BYTES_STR,
518                          &sym_stat->encrypt_bytes, stats_list);
519         stats_list = cryptodev_backend_stats_add(SYM_DECRYPT_BYTES_STR,
520                          &sym_stat->decrypt_bytes, stats_list);
521     }
522 
523     asym_stat = backend->asym_stat;
524     if (asym_stat) {
525         stats_list = cryptodev_backend_stats_add(ASYM_ENCRYPT_OPS_STR,
526                          &asym_stat->encrypt_ops, stats_list);
527         stats_list = cryptodev_backend_stats_add(ASYM_DECRYPT_OPS_STR,
528                          &asym_stat->decrypt_ops, stats_list);
529         stats_list = cryptodev_backend_stats_add(ASYM_SIGN_OPS_STR,
530                          &asym_stat->sign_ops, stats_list);
531         stats_list = cryptodev_backend_stats_add(ASYM_VERIFY_OPS_STR,
532                          &asym_stat->verify_ops, stats_list);
533         stats_list = cryptodev_backend_stats_add(ASYM_ENCRYPT_BYTES_STR,
534                          &asym_stat->encrypt_bytes, stats_list);
535         stats_list = cryptodev_backend_stats_add(ASYM_DECRYPT_BYTES_STR,
536                          &asym_stat->decrypt_bytes, stats_list);
537         stats_list = cryptodev_backend_stats_add(ASYM_SIGN_BYTES_STR,
538                          &asym_stat->sign_bytes, stats_list);
539         stats_list = cryptodev_backend_stats_add(ASYM_VERIFY_BYTES_STR,
540                          &asym_stat->verify_bytes, stats_list);
541     }
542 
543     entry = g_new0(StatsResult, 1);
544     entry->provider = STATS_PROVIDER_CRYPTODEV;
545     entry->qom_path = object_get_canonical_path(obj);
546     entry->stats = stats_list;
547     QAPI_LIST_PREPEND(*stats_results, entry);
548 
549     return 0;
550 }
551 
cryptodev_backend_stats_cb(StatsResultList ** result,StatsTarget target,strList * names,strList * targets,Error ** errp)552 static void cryptodev_backend_stats_cb(StatsResultList **result,
553                                        StatsTarget target,
554                                        strList *names, strList *targets,
555                                        Error **errp)
556 {
557     switch (target) {
558     case STATS_TARGET_CRYPTODEV:
559     {
560         Object *objs = container_get(object_get_root(), "/objects");
561         StatsArgs stats_args;
562         stats_args.result.stats = result;
563         stats_args.names = names;
564         stats_args.errp = errp;
565 
566         object_child_foreach(objs, cryptodev_backend_stats_query, &stats_args);
567         break;
568     }
569     default:
570         break;
571     }
572 }
573 
cryptodev_backend_schemas_add(const char * name,StatsSchemaValueList * list)574 static StatsSchemaValueList *cryptodev_backend_schemas_add(const char *name,
575                                  StatsSchemaValueList *list)
576 {
577     StatsSchemaValueList *schema_entry = g_new0(StatsSchemaValueList, 1);
578 
579     schema_entry->value = g_new0(StatsSchemaValue, 1);
580     schema_entry->value->type = STATS_TYPE_CUMULATIVE;
581     schema_entry->value->name = g_strdup(name);
582     schema_entry->next = list;
583 
584     return schema_entry;
585 }
586 
cryptodev_backend_schemas_cb(StatsSchemaList ** result,Error ** errp)587 static void cryptodev_backend_schemas_cb(StatsSchemaList **result,
588                                          Error **errp)
589 {
590     StatsSchemaValueList *stats_list = NULL;
591     const char *sym_stats[] = { SYM_ENCRYPT_OPS_STR, SYM_DECRYPT_OPS_STR,
592                                 SYM_ENCRYPT_BYTES_STR, SYM_DECRYPT_BYTES_STR };
593     const char *asym_stats[] = { ASYM_ENCRYPT_OPS_STR, ASYM_DECRYPT_OPS_STR,
594                                  ASYM_SIGN_OPS_STR, ASYM_VERIFY_OPS_STR,
595                                  ASYM_ENCRYPT_BYTES_STR, ASYM_DECRYPT_BYTES_STR,
596                                  ASYM_SIGN_BYTES_STR, ASYM_VERIFY_BYTES_STR };
597 
598     for (int i = 0; i < ARRAY_SIZE(sym_stats); i++) {
599         stats_list = cryptodev_backend_schemas_add(sym_stats[i], stats_list);
600     }
601 
602     for (int i = 0; i < ARRAY_SIZE(asym_stats); i++) {
603         stats_list = cryptodev_backend_schemas_add(asym_stats[i], stats_list);
604     }
605 
606     add_stats_schema(result, STATS_PROVIDER_CRYPTODEV, STATS_TARGET_CRYPTODEV,
607                      stats_list);
608 }
609 
610 static void
cryptodev_backend_class_init(ObjectClass * oc,void * data)611 cryptodev_backend_class_init(ObjectClass *oc, void *data)
612 {
613     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
614 
615     ucc->complete = cryptodev_backend_complete;
616     ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
617 
618     QTAILQ_INIT(&crypto_clients);
619     object_class_property_add(oc, "queues", "uint32",
620                               cryptodev_backend_get_queues,
621                               cryptodev_backend_set_queues,
622                               NULL, NULL);
623     object_class_property_add(oc, "throttle-bps", "uint64",
624                               cryptodev_backend_get_bps,
625                               cryptodev_backend_set_bps,
626                               NULL, NULL);
627     object_class_property_add(oc, "throttle-ops", "uint64",
628                               cryptodev_backend_get_ops,
629                               cryptodev_backend_set_ops,
630                               NULL, NULL);
631 
632     add_stats_callbacks(STATS_PROVIDER_CRYPTODEV, cryptodev_backend_stats_cb,
633                         cryptodev_backend_schemas_cb);
634 }
635 
636 static const TypeInfo cryptodev_backend_info = {
637     .name = TYPE_CRYPTODEV_BACKEND,
638     .parent = TYPE_OBJECT,
639     .instance_size = sizeof(CryptoDevBackend),
640     .instance_init = cryptodev_backend_instance_init,
641     .instance_finalize = cryptodev_backend_finalize,
642     .class_size = sizeof(CryptoDevBackendClass),
643     .class_init = cryptodev_backend_class_init,
644     .interfaces = (InterfaceInfo[]) {
645         { TYPE_USER_CREATABLE },
646         { }
647     }
648 };
649 
650 static void
cryptodev_backend_register_types(void)651 cryptodev_backend_register_types(void)
652 {
653     type_register_static(&cryptodev_backend_info);
654 }
655 
656 type_init(cryptodev_backend_register_types);
657