2 * QEMU Random Number Generator Backend
4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
13 #include "sysemu/rng.h"
14 #include "sysemu/char.h"
15 #include "qapi/qmp/qerror.h"
16 #include "hw/qdev.h" /* just for DEFINE_PROP_CHR */
18 #define TYPE_RNG_EGD "rng-egd"
19 #define RNG_EGD(obj) OBJECT_CHECK(RngEgd, (obj), TYPE_RNG_EGD)
31 typedef struct RngRequest
33 EntropyReceiveFunc
*receive_entropy
;
40 static void rng_egd_request_entropy(RngBackend
*b
, size_t size
,
41 EntropyReceiveFunc
*receive_entropy
,
44 RngEgd
*s
= RNG_EGD(b
);
47 req
= g_malloc(sizeof(*req
));
51 req
->receive_entropy
= receive_entropy
;
53 req
->data
= g_malloc(req
->size
);
57 uint8_t len
= MIN(size
, 255);
59 /* synchronous entropy request */
63 qemu_chr_fe_write(s
->chr
, header
, sizeof(header
));
68 s
->requests
= g_slist_append(s
->requests
, req
);
71 static void rng_egd_free_request(RngRequest
*req
)
77 static int rng_egd_chr_can_read(void *opaque
)
79 RngEgd
*s
= RNG_EGD(opaque
);
83 for (i
= s
->requests
; i
; i
= i
->next
) {
84 RngRequest
*req
= i
->data
;
85 size
+= req
->size
- req
->offset
;
91 static void rng_egd_chr_read(void *opaque
, const uint8_t *buf
, int size
)
93 RngEgd
*s
= RNG_EGD(opaque
);
94 size_t buf_offset
= 0;
96 while (size
> 0 && s
->requests
) {
97 RngRequest
*req
= s
->requests
->data
;
98 int len
= MIN(size
, req
->size
- req
->offset
);
100 memcpy(req
->data
+ req
->offset
, buf
+ buf_offset
, len
);
105 if (req
->offset
== req
->size
) {
106 s
->requests
= g_slist_remove_link(s
->requests
, s
->requests
);
108 req
->receive_entropy(req
->opaque
, req
->data
, req
->size
);
110 rng_egd_free_request(req
);
115 static void rng_egd_free_requests(RngEgd
*s
)
119 for (i
= s
->requests
; i
; i
= i
->next
) {
120 rng_egd_free_request(i
->data
);
123 g_slist_free(s
->requests
);
127 static void rng_egd_cancel_requests(RngBackend
*b
)
129 RngEgd
*s
= RNG_EGD(b
);
131 /* We simply delete the list of pending requests. If there is data in the
132 * queue waiting to be read, this is okay, because there will always be
133 * more data than we requested originally
135 rng_egd_free_requests(s
);
138 static void rng_egd_opened(RngBackend
*b
, Error
**errp
)
140 RngEgd
*s
= RNG_EGD(b
);
142 if (s
->chr_name
== NULL
) {
143 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
144 "chardev", "a valid character device");
148 s
->chr
= qemu_chr_find(s
->chr_name
);
149 if (s
->chr
== NULL
) {
150 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
151 "Device '%s' not found", s
->chr_name
);
155 if (qemu_chr_fe_claim(s
->chr
) != 0) {
156 error_setg(errp
, QERR_DEVICE_IN_USE
, s
->chr_name
);
160 /* FIXME we should resubmit pending requests when the CDS reconnects. */
161 qemu_chr_add_handlers(s
->chr
, rng_egd_chr_can_read
, rng_egd_chr_read
,
165 static void rng_egd_set_chardev(Object
*obj
, const char *value
, Error
**errp
)
167 RngBackend
*b
= RNG_BACKEND(obj
);
168 RngEgd
*s
= RNG_EGD(b
);
171 error_setg(errp
, QERR_PERMISSION_DENIED
);
174 s
->chr_name
= g_strdup(value
);
178 static char *rng_egd_get_chardev(Object
*obj
, Error
**errp
)
180 RngEgd
*s
= RNG_EGD(obj
);
182 if (s
->chr
&& s
->chr
->label
) {
183 return g_strdup(s
->chr
->label
);
189 static void rng_egd_init(Object
*obj
)
191 object_property_add_str(obj
, "chardev",
192 rng_egd_get_chardev
, rng_egd_set_chardev
,
196 static void rng_egd_finalize(Object
*obj
)
198 RngEgd
*s
= RNG_EGD(obj
);
201 qemu_chr_add_handlers(s
->chr
, NULL
, NULL
, NULL
, NULL
);
202 qemu_chr_fe_release(s
->chr
);
207 rng_egd_free_requests(s
);
210 static void rng_egd_class_init(ObjectClass
*klass
, void *data
)
212 RngBackendClass
*rbc
= RNG_BACKEND_CLASS(klass
);
214 rbc
->request_entropy
= rng_egd_request_entropy
;
215 rbc
->cancel_requests
= rng_egd_cancel_requests
;
216 rbc
->opened
= rng_egd_opened
;
219 static const TypeInfo rng_egd_info
= {
220 .name
= TYPE_RNG_EGD
,
221 .parent
= TYPE_RNG_BACKEND
,
222 .instance_size
= sizeof(RngEgd
),
223 .class_init
= rng_egd_class_init
,
224 .instance_init
= rng_egd_init
,
225 .instance_finalize
= rng_egd_finalize
,
228 static void register_types(void)
230 type_register_static(&rng_egd_info
);
233 type_init(register_types
);