blob: 85cb83f5e1ea9025b23b1596a5c05d4451e89218 [file] [log] [blame]
Anthony Liguoria9b7b2a2012-06-25 10:03:47 -05001/*
2 * QEMU Random Number Generator Backend
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 */
12
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020013#include "sysemu/rng.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010014#include "qapi/qmp/qerror.h"
Anthony Liguoria9b7b2a2012-06-25 10:03:47 -050015
16void rng_backend_request_entropy(RngBackend *s, size_t size,
17 EntropyReceiveFunc *receive_entropy,
18 void *opaque)
19{
20 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
21
22 if (k->request_entropy) {
23 k->request_entropy(s, size, receive_entropy, opaque);
24 }
25}
26
27void rng_backend_cancel_requests(RngBackend *s)
28{
29 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
30
31 if (k->cancel_requests) {
32 k->cancel_requests(s);
33 }
34}
35
36static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
37{
38 RngBackend *s = RNG_BACKEND(obj);
39
40 return s->opened;
41}
42
43void rng_backend_open(RngBackend *s, Error **errp)
44{
45 object_property_set_bool(OBJECT(s), true, "opened", errp);
46}
47
48static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
49{
50 RngBackend *s = RNG_BACKEND(obj);
51 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
52
53 if (value == s->opened) {
54 return;
55 }
56
57 if (!value && s->opened) {
58 error_set(errp, QERR_PERMISSION_DENIED);
59 return;
60 }
61
62 if (k->opened) {
63 k->opened(s, errp);
64 }
65
66 if (!error_is_set(errp)) {
67 s->opened = value;
68 }
69}
70
71static void rng_backend_init(Object *obj)
72{
73 object_property_add_bool(obj, "opened",
74 rng_backend_prop_get_opened,
75 rng_backend_prop_set_opened,
76 NULL);
77}
78
Andreas Färber8c43a6f2013-01-10 16:19:07 +010079static const TypeInfo rng_backend_info = {
Anthony Liguoria9b7b2a2012-06-25 10:03:47 -050080 .name = TYPE_RNG_BACKEND,
81 .parent = TYPE_OBJECT,
82 .instance_size = sizeof(RngBackend),
83 .instance_init = rng_backend_init,
84 .class_size = sizeof(RngBackendClass),
85 .abstract = true,
86};
87
88static void register_types(void)
89{
90 type_register_static(&rng_backend_info);
91}
92
93type_init(register_types);