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"
15 #include "hw/virtio/virtio.h"
16 #include "hw/virtio/virtio-rng.h"
17 #include "sysemu/rng.h"
18 #include "qom/object_interfaces.h"
21 static bool is_guest_ready(VirtIORNG
*vrng
)
23 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
24 if (virtio_queue_ready(vrng
->vq
)
25 && (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
28 trace_virtio_rng_guest_not_ready(vrng
);
32 static size_t get_request_size(VirtQueue
*vq
, unsigned quota
)
36 virtqueue_get_avail_bytes(vq
, &in
, &out
, quota
, 0);
40 static void virtio_rng_process(VirtIORNG
*vrng
);
42 /* Send data from a char device over to the guest */
43 static void chr_read(void *opaque
, const void *buf
, size_t size
)
45 VirtIORNG
*vrng
= opaque
;
46 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
47 VirtQueueElement
*elem
;
51 if (!is_guest_ready(vrng
)) {
55 vrng
->quota_remaining
-= size
;
58 while (offset
< size
) {
59 elem
= virtqueue_pop(vrng
->vq
, sizeof(VirtQueueElement
));
63 len
= iov_from_buf(elem
->in_sg
, elem
->in_num
,
64 0, buf
+ offset
, size
- offset
);
67 virtqueue_push(vrng
->vq
, elem
, len
);
68 trace_virtio_rng_pushed(vrng
, len
);
71 virtio_notify(vdev
, vrng
->vq
);
74 static void virtio_rng_process(VirtIORNG
*vrng
)
79 if (!is_guest_ready(vrng
)) {
83 if (vrng
->activate_timer
) {
84 timer_mod(vrng
->rate_limit_timer
,
85 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vrng
->conf
.period_ms
);
86 vrng
->activate_timer
= false;
89 if (vrng
->quota_remaining
< 0) {
92 quota
= MIN((uint64_t)vrng
->quota_remaining
, (uint64_t)UINT32_MAX
);
94 size
= get_request_size(vrng
->vq
, quota
);
96 trace_virtio_rng_request(vrng
, size
, quota
);
98 size
= MIN(vrng
->quota_remaining
, size
);
100 rng_backend_request_entropy(vrng
->rng
, size
, chr_read
, vrng
);
104 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
106 VirtIORNG
*vrng
= VIRTIO_RNG(vdev
);
107 virtio_rng_process(vrng
);
110 static uint64_t get_features(VirtIODevice
*vdev
, uint64_t f
, Error
**errp
)
115 static void virtio_rng_save(QEMUFile
*f
, void *opaque
)
117 VirtIODevice
*vdev
= opaque
;
119 virtio_save(vdev
, f
);
122 static int virtio_rng_load(QEMUFile
*f
, void *opaque
, int version_id
)
124 VirtIORNG
*vrng
= opaque
;
127 if (version_id
!= 1) {
130 ret
= virtio_load(VIRTIO_DEVICE(vrng
), f
, version_id
);
135 /* We may have an element ready but couldn't process it due to a quota
136 * limit. Make sure to try again after live migration when the quota may
139 virtio_rng_process(vrng
);
144 static void check_rate_limit(void *opaque
)
146 VirtIORNG
*vrng
= opaque
;
148 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
149 virtio_rng_process(vrng
);
150 vrng
->activate_timer
= true;
153 static void virtio_rng_device_realize(DeviceState
*dev
, Error
**errp
)
155 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
156 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
157 Error
*local_err
= NULL
;
159 if (vrng
->conf
.period_ms
<= 0) {
160 error_setg(errp
, "'period' parameter expects a positive integer");
164 /* Workaround: Property parsing does not enforce unsigned integers,
165 * So this is a hack to reject such numbers. */
166 if (vrng
->conf
.max_bytes
> INT64_MAX
) {
167 error_setg(errp
, "'max-bytes' parameter must be non-negative, "
168 "and less than 2^63");
172 if (vrng
->conf
.rng
== NULL
) {
173 vrng
->conf
.default_backend
= RNG_RANDOM(object_new(TYPE_RNG_RANDOM
));
175 user_creatable_complete(OBJECT(vrng
->conf
.default_backend
),
178 error_propagate(errp
, local_err
);
179 object_unref(OBJECT(vrng
->conf
.default_backend
));
183 object_property_add_child(OBJECT(dev
),
185 OBJECT(vrng
->conf
.default_backend
),
188 /* The child property took a reference, we can safely drop ours now */
189 object_unref(OBJECT(vrng
->conf
.default_backend
));
191 object_property_set_link(OBJECT(dev
),
192 OBJECT(vrng
->conf
.default_backend
),
196 vrng
->rng
= vrng
->conf
.rng
;
197 if (vrng
->rng
== NULL
) {
198 error_setg(errp
, "'rng' parameter expects a valid object");
202 virtio_init(vdev
, "virtio-rng", VIRTIO_ID_RNG
, 0);
204 vrng
->vq
= virtio_add_queue(vdev
, 8, handle_input
);
205 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
206 vrng
->rate_limit_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
207 check_rate_limit
, vrng
);
208 vrng
->activate_timer
= true;
209 register_savevm(dev
, "virtio-rng", -1, 1, virtio_rng_save
,
210 virtio_rng_load
, vrng
);
213 static void virtio_rng_device_unrealize(DeviceState
*dev
, Error
**errp
)
215 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
216 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
218 timer_del(vrng
->rate_limit_timer
);
219 timer_free(vrng
->rate_limit_timer
);
220 unregister_savevm(dev
, "virtio-rng", vrng
);
221 virtio_cleanup(vdev
);
224 static Property virtio_rng_properties
[] = {
225 /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
226 * you have an entropy source capable of generating more entropy than this
227 * and you can pass it through via virtio-rng, then hats off to you. Until
228 * then, this is unlimited for all practical purposes.
230 DEFINE_PROP_UINT64("max-bytes", VirtIORNG
, conf
.max_bytes
, INT64_MAX
),
231 DEFINE_PROP_UINT32("period", VirtIORNG
, conf
.period_ms
, 1 << 16),
232 DEFINE_PROP_END_OF_LIST(),
235 static void virtio_rng_class_init(ObjectClass
*klass
, void *data
)
237 DeviceClass
*dc
= DEVICE_CLASS(klass
);
238 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
240 dc
->props
= virtio_rng_properties
;
241 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
242 vdc
->realize
= virtio_rng_device_realize
;
243 vdc
->unrealize
= virtio_rng_device_unrealize
;
244 vdc
->get_features
= get_features
;
247 static void virtio_rng_initfn(Object
*obj
)
249 VirtIORNG
*vrng
= VIRTIO_RNG(obj
);
251 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
252 (Object
**)&vrng
->conf
.rng
,
253 qdev_prop_allow_set_link_before_realize
,
254 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
257 static const TypeInfo virtio_rng_info
= {
258 .name
= TYPE_VIRTIO_RNG
,
259 .parent
= TYPE_VIRTIO_DEVICE
,
260 .instance_size
= sizeof(VirtIORNG
),
261 .instance_init
= virtio_rng_initfn
,
262 .class_init
= virtio_rng_class_init
,
265 static void virtio_register_types(void)
267 type_register_static(&virtio_rng_info
);
270 type_init(virtio_register_types
)