2 * A virtio device implementing a hardware random number generator.
4 * Copyright 2012 Red Hat, Inc.
5 * Copyright 2012 Amit Shah <amit.shah@redhat.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or
8 * (at your option) any later version. See the COPYING file in the
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
15 #include "qemu/module.h"
17 #include "hw/virtio/virtio.h"
18 #include "hw/virtio/virtio-rng.h"
19 #include "sysemu/rng.h"
20 #include "qom/object_interfaces.h"
23 static bool is_guest_ready(VirtIORNG
*vrng
)
25 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
26 if (virtio_queue_ready(vrng
->vq
)
27 && (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
30 trace_virtio_rng_guest_not_ready(vrng
);
34 static size_t get_request_size(VirtQueue
*vq
, unsigned quota
)
38 virtqueue_get_avail_bytes(vq
, &in
, &out
, quota
, 0);
42 static void virtio_rng_process(VirtIORNG
*vrng
);
44 /* Send data from a char device over to the guest */
45 static void chr_read(void *opaque
, const void *buf
, size_t size
)
47 VirtIORNG
*vrng
= opaque
;
48 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
49 VirtQueueElement
*elem
;
53 if (!is_guest_ready(vrng
)) {
57 /* we can't modify the virtqueue until
58 * our state is fully synced
61 if (!runstate_check(RUN_STATE_RUNNING
)) {
62 trace_virtio_rng_cpu_is_stopped(vrng
, size
);
66 vrng
->quota_remaining
-= size
;
69 while (offset
< size
) {
70 elem
= virtqueue_pop(vrng
->vq
, sizeof(VirtQueueElement
));
74 trace_virtio_rng_popped(vrng
);
75 len
= iov_from_buf(elem
->in_sg
, elem
->in_num
,
76 0, buf
+ offset
, size
- offset
);
79 virtqueue_push(vrng
->vq
, elem
, len
);
80 trace_virtio_rng_pushed(vrng
, len
);
83 virtio_notify(vdev
, vrng
->vq
);
85 if (!virtio_queue_empty(vrng
->vq
)) {
86 /* If we didn't drain the queue, call virtio_rng_process
87 * to take care of asking for more data as appropriate.
89 virtio_rng_process(vrng
);
93 static void virtio_rng_process(VirtIORNG
*vrng
)
98 if (!is_guest_ready(vrng
)) {
102 if (vrng
->activate_timer
) {
103 timer_mod(vrng
->rate_limit_timer
,
104 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vrng
->conf
.period_ms
);
105 vrng
->activate_timer
= false;
108 if (vrng
->quota_remaining
< 0) {
111 quota
= MIN((uint64_t)vrng
->quota_remaining
, (uint64_t)UINT32_MAX
);
113 size
= get_request_size(vrng
->vq
, quota
);
115 trace_virtio_rng_request(vrng
, size
, quota
);
117 size
= MIN(vrng
->quota_remaining
, size
);
119 rng_backend_request_entropy(vrng
->rng
, size
, chr_read
, vrng
);
123 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
125 VirtIORNG
*vrng
= VIRTIO_RNG(vdev
);
126 virtio_rng_process(vrng
);
129 static uint64_t get_features(VirtIODevice
*vdev
, uint64_t f
, Error
**errp
)
134 static void virtio_rng_vm_state_change(void *opaque
, int running
,
137 VirtIORNG
*vrng
= opaque
;
139 trace_virtio_rng_vm_state_change(vrng
, running
, state
);
141 /* We may have an element ready but couldn't process it due to a quota
142 * limit or because CPU was stopped. Make sure to try again when the
146 if (running
&& is_guest_ready(vrng
)) {
147 virtio_rng_process(vrng
);
151 static void check_rate_limit(void *opaque
)
153 VirtIORNG
*vrng
= opaque
;
155 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
156 virtio_rng_process(vrng
);
157 vrng
->activate_timer
= true;
160 static void virtio_rng_set_status(VirtIODevice
*vdev
, uint8_t status
)
162 VirtIORNG
*vrng
= VIRTIO_RNG(vdev
);
164 if (!vdev
->vm_running
) {
167 vdev
->status
= status
;
169 /* Something changed, try to process buffers */
170 virtio_rng_process(vrng
);
173 static void virtio_rng_device_realize(DeviceState
*dev
, Error
**errp
)
175 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
176 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
177 Error
*local_err
= NULL
;
179 if (vrng
->conf
.period_ms
<= 0) {
180 error_setg(errp
, "'period' parameter expects a positive integer");
184 /* Workaround: Property parsing does not enforce unsigned integers,
185 * So this is a hack to reject such numbers. */
186 if (vrng
->conf
.max_bytes
> INT64_MAX
) {
187 error_setg(errp
, "'max-bytes' parameter must be non-negative, "
188 "and less than 2^63");
192 if (vrng
->conf
.rng
== NULL
) {
193 vrng
->conf
.default_backend
= RNG_RANDOM(object_new(TYPE_RNG_RANDOM
));
195 user_creatable_complete(USER_CREATABLE(vrng
->conf
.default_backend
),
198 error_propagate(errp
, local_err
);
199 object_unref(OBJECT(vrng
->conf
.default_backend
));
203 object_property_add_child(OBJECT(dev
),
205 OBJECT(vrng
->conf
.default_backend
),
208 /* The child property took a reference, we can safely drop ours now */
209 object_unref(OBJECT(vrng
->conf
.default_backend
));
211 object_property_set_link(OBJECT(dev
),
212 OBJECT(vrng
->conf
.default_backend
),
216 vrng
->rng
= vrng
->conf
.rng
;
217 if (vrng
->rng
== NULL
) {
218 error_setg(errp
, "'rng' parameter expects a valid object");
222 virtio_init(vdev
, "virtio-rng", VIRTIO_ID_RNG
, 0);
224 vrng
->vq
= virtio_add_queue(vdev
, 8, handle_input
);
225 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
226 vrng
->rate_limit_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
227 check_rate_limit
, vrng
);
228 vrng
->activate_timer
= true;
230 vrng
->vmstate
= qemu_add_vm_change_state_handler(virtio_rng_vm_state_change
,
234 static void virtio_rng_device_unrealize(DeviceState
*dev
, Error
**errp
)
236 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
237 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
239 qemu_del_vm_change_state_handler(vrng
->vmstate
);
240 timer_del(vrng
->rate_limit_timer
);
241 timer_free(vrng
->rate_limit_timer
);
242 virtio_cleanup(vdev
);
245 static const VMStateDescription vmstate_virtio_rng
= {
246 .name
= "virtio-rng",
247 .minimum_version_id
= 1,
249 .fields
= (VMStateField
[]) {
250 VMSTATE_VIRTIO_DEVICE
,
251 VMSTATE_END_OF_LIST()
255 static Property virtio_rng_properties
[] = {
256 /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
257 * you have an entropy source capable of generating more entropy than this
258 * and you can pass it through via virtio-rng, then hats off to you. Until
259 * then, this is unlimited for all practical purposes.
261 DEFINE_PROP_UINT64("max-bytes", VirtIORNG
, conf
.max_bytes
, INT64_MAX
),
262 DEFINE_PROP_UINT32("period", VirtIORNG
, conf
.period_ms
, 1 << 16),
263 DEFINE_PROP_LINK("rng", VirtIORNG
, conf
.rng
, TYPE_RNG_BACKEND
, RngBackend
*),
264 DEFINE_PROP_END_OF_LIST(),
267 static void virtio_rng_class_init(ObjectClass
*klass
, void *data
)
269 DeviceClass
*dc
= DEVICE_CLASS(klass
);
270 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
272 dc
->props
= virtio_rng_properties
;
273 dc
->vmsd
= &vmstate_virtio_rng
;
274 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
275 vdc
->realize
= virtio_rng_device_realize
;
276 vdc
->unrealize
= virtio_rng_device_unrealize
;
277 vdc
->get_features
= get_features
;
278 vdc
->set_status
= virtio_rng_set_status
;
281 static const TypeInfo virtio_rng_info
= {
282 .name
= TYPE_VIRTIO_RNG
,
283 .parent
= TYPE_VIRTIO_DEVICE
,
284 .instance_size
= sizeof(VirtIORNG
),
285 .class_init
= virtio_rng_class_init
,
288 static void virtio_register_types(void)
290 type_register_static(&virtio_rng_info
);
293 type_init(virtio_register_types
)