]> git.proxmox.com Git - mirror_qemu.git/blame - backends/rng.c
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2016-03-18' into staging
[mirror_qemu.git] / backends / rng.c
CommitLineData
a9b7b2ad
AL
1/*
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
9c058332 13#include "qemu/osdep.h"
dccfcd0e 14#include "sysemu/rng.h"
7b1b5d19 15#include "qapi/qmp/qerror.h"
269e09f3 16#include "qom/object_interfaces.h"
a9b7b2ad
AL
17
18void rng_backend_request_entropy(RngBackend *s, size_t size,
19 EntropyReceiveFunc *receive_entropy,
20 void *opaque)
21{
22 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
60253ed1 23 RngRequest *req;
a9b7b2ad
AL
24
25 if (k->request_entropy) {
60253ed1
LP
26 req = g_malloc(sizeof(*req));
27
28 req->offset = 0;
29 req->size = size;
30 req->receive_entropy = receive_entropy;
31 req->opaque = opaque;
32 req->data = g_malloc(req->size);
33
34 k->request_entropy(s, req);
35
443590c2 36 QSIMPLEQ_INSERT_TAIL(&s->requests, req, next);
a9b7b2ad
AL
37 }
38}
39
a9b7b2ad
AL
40static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
41{
42 RngBackend *s = RNG_BACKEND(obj);
43
44 return s->opened;
45}
46
57d3e1b3 47static void rng_backend_complete(UserCreatable *uc, Error **errp)
a9b7b2ad 48{
57d3e1b3 49 object_property_set_bool(OBJECT(uc), true, "opened", errp);
a9b7b2ad
AL
50}
51
52static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
53{
54 RngBackend *s = RNG_BACKEND(obj);
55 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
65cd9064 56 Error *local_err = NULL;
a9b7b2ad
AL
57
58 if (value == s->opened) {
59 return;
60 }
61
62 if (!value && s->opened) {
c6bd8c70 63 error_setg(errp, QERR_PERMISSION_DENIED);
a9b7b2ad
AL
64 return;
65 }
66
67 if (k->opened) {
65cd9064
MA
68 k->opened(s, &local_err);
69 if (local_err) {
70 error_propagate(errp, local_err);
71 return;
72 }
a9b7b2ad
AL
73 }
74
65cd9064 75 s->opened = true;
a9b7b2ad
AL
76}
77
9f14b0ad
LP
78static void rng_backend_free_request(RngRequest *req)
79{
80 g_free(req->data);
81 g_free(req);
82}
83
84static void rng_backend_free_requests(RngBackend *s)
85{
443590c2 86 RngRequest *req, *next;
9f14b0ad 87
443590c2
LP
88 QSIMPLEQ_FOREACH_SAFE(req, &s->requests, next, next) {
89 rng_backend_free_request(req);
9f14b0ad
LP
90 }
91
443590c2 92 QSIMPLEQ_INIT(&s->requests);
9f14b0ad
LP
93}
94
95void rng_backend_finalize_request(RngBackend *s, RngRequest *req)
96{
443590c2 97 QSIMPLEQ_REMOVE(&s->requests, req, RngRequest, next);
9f14b0ad
LP
98 rng_backend_free_request(req);
99}
100
a9b7b2ad
AL
101static void rng_backend_init(Object *obj)
102{
443590c2
LP
103 RngBackend *s = RNG_BACKEND(obj);
104
105 QSIMPLEQ_INIT(&s->requests);
106
a9b7b2ad
AL
107 object_property_add_bool(obj, "opened",
108 rng_backend_prop_get_opened,
109 rng_backend_prop_set_opened,
110 NULL);
111}
112
9f14b0ad
LP
113static void rng_backend_finalize(Object *obj)
114{
115 RngBackend *s = RNG_BACKEND(obj);
116
117 rng_backend_free_requests(s);
118}
119
57d3e1b3
IM
120static void rng_backend_class_init(ObjectClass *oc, void *data)
121{
122 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
123
124 ucc->complete = rng_backend_complete;
125}
126
8c43a6f0 127static const TypeInfo rng_backend_info = {
a9b7b2ad
AL
128 .name = TYPE_RNG_BACKEND,
129 .parent = TYPE_OBJECT,
130 .instance_size = sizeof(RngBackend),
131 .instance_init = rng_backend_init,
9f14b0ad 132 .instance_finalize = rng_backend_finalize,
a9b7b2ad 133 .class_size = sizeof(RngBackendClass),
57d3e1b3 134 .class_init = rng_backend_class_init,
a9b7b2ad 135 .abstract = true,
269e09f3
IM
136 .interfaces = (InterfaceInfo[]) {
137 { TYPE_USER_CREATABLE },
138 { }
139 }
a9b7b2ad
AL
140};
141
142static void register_types(void)
143{
144 type_register_static(&rng_backend_info);
145}
146
147type_init(register_types);