]>
Commit | Line | Data |
---|---|---|
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" |
da34e65c | 15 | #include "qapi/error.h" |
7b1b5d19 | 16 | #include "qapi/qmp/qerror.h" |
0b8fa32f | 17 | #include "qemu/module.h" |
269e09f3 | 18 | #include "qom/object_interfaces.h" |
a9b7b2ad AL |
19 | |
20 | void rng_backend_request_entropy(RngBackend *s, size_t size, | |
21 | EntropyReceiveFunc *receive_entropy, | |
22 | void *opaque) | |
23 | { | |
24 | RngBackendClass *k = RNG_BACKEND_GET_CLASS(s); | |
60253ed1 | 25 | RngRequest *req; |
a9b7b2ad AL |
26 | |
27 | if (k->request_entropy) { | |
60253ed1 LP |
28 | req = g_malloc(sizeof(*req)); |
29 | ||
30 | req->offset = 0; | |
31 | req->size = size; | |
32 | req->receive_entropy = receive_entropy; | |
33 | req->opaque = opaque; | |
34 | req->data = g_malloc(req->size); | |
35 | ||
36 | k->request_entropy(s, req); | |
37 | ||
443590c2 | 38 | QSIMPLEQ_INSERT_TAIL(&s->requests, req, next); |
a9b7b2ad AL |
39 | } |
40 | } | |
41 | ||
a9b7b2ad AL |
42 | static bool rng_backend_prop_get_opened(Object *obj, Error **errp) |
43 | { | |
44 | RngBackend *s = RNG_BACKEND(obj); | |
45 | ||
46 | return s->opened; | |
47 | } | |
48 | ||
57d3e1b3 | 49 | static void rng_backend_complete(UserCreatable *uc, Error **errp) |
a9b7b2ad | 50 | { |
6e577937 | 51 | RngBackend *s = RNG_BACKEND(uc); |
a9b7b2ad | 52 | RngBackendClass *k = RNG_BACKEND_GET_CLASS(s); |
65cd9064 | 53 | Error *local_err = NULL; |
a9b7b2ad | 54 | |
a9b7b2ad | 55 | if (k->opened) { |
65cd9064 MA |
56 | k->opened(s, &local_err); |
57 | if (local_err) { | |
58 | error_propagate(errp, local_err); | |
59 | return; | |
60 | } | |
a9b7b2ad AL |
61 | } |
62 | ||
65cd9064 | 63 | s->opened = true; |
a9b7b2ad AL |
64 | } |
65 | ||
9f14b0ad LP |
66 | static void rng_backend_free_request(RngRequest *req) |
67 | { | |
68 | g_free(req->data); | |
69 | g_free(req); | |
70 | } | |
71 | ||
72 | static void rng_backend_free_requests(RngBackend *s) | |
73 | { | |
443590c2 | 74 | RngRequest *req, *next; |
9f14b0ad | 75 | |
443590c2 LP |
76 | QSIMPLEQ_FOREACH_SAFE(req, &s->requests, next, next) { |
77 | rng_backend_free_request(req); | |
9f14b0ad LP |
78 | } |
79 | ||
443590c2 | 80 | QSIMPLEQ_INIT(&s->requests); |
9f14b0ad LP |
81 | } |
82 | ||
83 | void rng_backend_finalize_request(RngBackend *s, RngRequest *req) | |
84 | { | |
443590c2 | 85 | QSIMPLEQ_REMOVE(&s->requests, req, RngRequest, next); |
9f14b0ad LP |
86 | rng_backend_free_request(req); |
87 | } | |
88 | ||
a9b7b2ad AL |
89 | static void rng_backend_init(Object *obj) |
90 | { | |
443590c2 LP |
91 | RngBackend *s = RNG_BACKEND(obj); |
92 | ||
93 | QSIMPLEQ_INIT(&s->requests); | |
a9b7b2ad AL |
94 | } |
95 | ||
9f14b0ad LP |
96 | static void rng_backend_finalize(Object *obj) |
97 | { | |
98 | RngBackend *s = RNG_BACKEND(obj); | |
99 | ||
100 | rng_backend_free_requests(s); | |
101 | } | |
102 | ||
57d3e1b3 IM |
103 | static void rng_backend_class_init(ObjectClass *oc, void *data) |
104 | { | |
105 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); | |
106 | ||
107 | ucc->complete = rng_backend_complete; | |
29ee2a18 EH |
108 | |
109 | object_class_property_add_bool(oc, "opened", | |
110 | rng_backend_prop_get_opened, | |
6e577937 | 111 | NULL); |
57d3e1b3 IM |
112 | } |
113 | ||
8c43a6f0 | 114 | static const TypeInfo rng_backend_info = { |
a9b7b2ad AL |
115 | .name = TYPE_RNG_BACKEND, |
116 | .parent = TYPE_OBJECT, | |
117 | .instance_size = sizeof(RngBackend), | |
118 | .instance_init = rng_backend_init, | |
9f14b0ad | 119 | .instance_finalize = rng_backend_finalize, |
a9b7b2ad | 120 | .class_size = sizeof(RngBackendClass), |
57d3e1b3 | 121 | .class_init = rng_backend_class_init, |
a9b7b2ad | 122 | .abstract = true, |
269e09f3 IM |
123 | .interfaces = (InterfaceInfo[]) { |
124 | { TYPE_USER_CREATABLE }, | |
125 | { } | |
126 | } | |
a9b7b2ad AL |
127 | }; |
128 | ||
129 | static void register_types(void) | |
130 | { | |
131 | type_register_static(&rng_backend_info); | |
132 | } | |
133 | ||
134 | type_init(register_types); |