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"
16 #include "hw/virtio/virtio.h"
17 #include "hw/virtio/virtio-rng.h"
18 #include "sysemu/rng.h"
19 #include "qom/object_interfaces.h"
22 static bool is_guest_ready(VirtIORNG
*vrng
)
24 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
25 if (virtio_queue_ready(vrng
->vq
)
26 && (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
29 trace_virtio_rng_guest_not_ready(vrng
);
33 static size_t get_request_size(VirtQueue
*vq
, unsigned quota
)
37 virtqueue_get_avail_bytes(vq
, &in
, &out
, quota
, 0);
41 static void virtio_rng_process(VirtIORNG
*vrng
);
43 /* Send data from a char device over to the guest */
44 static void chr_read(void *opaque
, const void *buf
, size_t size
)
46 VirtIORNG
*vrng
= opaque
;
47 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
48 VirtQueueElement
*elem
;
52 if (!is_guest_ready(vrng
)) {
56 vrng
->quota_remaining
-= size
;
59 while (offset
< size
) {
60 elem
= virtqueue_pop(vrng
->vq
, sizeof(VirtQueueElement
));
64 len
= iov_from_buf(elem
->in_sg
, elem
->in_num
,
65 0, buf
+ offset
, size
- offset
);
68 virtqueue_push(vrng
->vq
, elem
, len
);
69 trace_virtio_rng_pushed(vrng
, len
);
72 virtio_notify(vdev
, vrng
->vq
);
74 if (!virtio_queue_empty(vrng
->vq
)) {
75 /* If we didn't drain the queue, call virtio_rng_process
76 * to take care of asking for more data as appropriate.
78 virtio_rng_process(vrng
);
82 static void virtio_rng_process(VirtIORNG
*vrng
)
87 if (!is_guest_ready(vrng
)) {
91 if (vrng
->activate_timer
) {
92 timer_mod(vrng
->rate_limit_timer
,
93 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vrng
->conf
.period_ms
);
94 vrng
->activate_timer
= false;
97 if (vrng
->quota_remaining
< 0) {
100 quota
= MIN((uint64_t)vrng
->quota_remaining
, (uint64_t)UINT32_MAX
);
102 size
= get_request_size(vrng
->vq
, quota
);
104 trace_virtio_rng_request(vrng
, size
, quota
);
106 size
= MIN(vrng
->quota_remaining
, size
);
108 rng_backend_request_entropy(vrng
->rng
, size
, chr_read
, vrng
);
112 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
114 VirtIORNG
*vrng
= VIRTIO_RNG(vdev
);
115 virtio_rng_process(vrng
);
118 static uint64_t get_features(VirtIODevice
*vdev
, uint64_t f
, Error
**errp
)
123 static void virtio_rng_save(QEMUFile
*f
, void *opaque
)
125 VirtIODevice
*vdev
= opaque
;
127 virtio_save(vdev
, f
);
130 static int virtio_rng_load(QEMUFile
*f
, void *opaque
, int version_id
)
132 VirtIORNG
*vrng
= opaque
;
135 if (version_id
!= 1) {
138 ret
= virtio_load(VIRTIO_DEVICE(vrng
), f
, version_id
);
143 /* We may have an element ready but couldn't process it due to a quota
144 * limit. Make sure to try again after live migration when the quota may
147 virtio_rng_process(vrng
);
152 static void check_rate_limit(void *opaque
)
154 VirtIORNG
*vrng
= opaque
;
156 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
157 virtio_rng_process(vrng
);
158 vrng
->activate_timer
= true;
161 static void virtio_rng_device_realize(DeviceState
*dev
, Error
**errp
)
163 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
164 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
165 Error
*local_err
= NULL
;
167 if (vrng
->conf
.period_ms
<= 0) {
168 error_setg(errp
, "'period' parameter expects a positive integer");
172 /* Workaround: Property parsing does not enforce unsigned integers,
173 * So this is a hack to reject such numbers. */
174 if (vrng
->conf
.max_bytes
> INT64_MAX
) {
175 error_setg(errp
, "'max-bytes' parameter must be non-negative, "
176 "and less than 2^63");
180 if (vrng
->conf
.rng
== NULL
) {
181 vrng
->conf
.default_backend
= RNG_RANDOM(object_new(TYPE_RNG_RANDOM
));
183 user_creatable_complete(OBJECT(vrng
->conf
.default_backend
),
186 error_propagate(errp
, local_err
);
187 object_unref(OBJECT(vrng
->conf
.default_backend
));
191 object_property_add_child(OBJECT(dev
),
193 OBJECT(vrng
->conf
.default_backend
),
196 /* The child property took a reference, we can safely drop ours now */
197 object_unref(OBJECT(vrng
->conf
.default_backend
));
199 object_property_set_link(OBJECT(dev
),
200 OBJECT(vrng
->conf
.default_backend
),
204 vrng
->rng
= vrng
->conf
.rng
;
205 if (vrng
->rng
== NULL
) {
206 error_setg(errp
, "'rng' parameter expects a valid object");
210 virtio_init(vdev
, "virtio-rng", VIRTIO_ID_RNG
, 0);
212 vrng
->vq
= virtio_add_queue(vdev
, 8, handle_input
);
213 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
214 vrng
->rate_limit_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
215 check_rate_limit
, vrng
);
216 vrng
->activate_timer
= true;
217 register_savevm(dev
, "virtio-rng", -1, 1, virtio_rng_save
,
218 virtio_rng_load
, vrng
);
221 static void virtio_rng_device_unrealize(DeviceState
*dev
, Error
**errp
)
223 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
224 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
226 timer_del(vrng
->rate_limit_timer
);
227 timer_free(vrng
->rate_limit_timer
);
228 unregister_savevm(dev
, "virtio-rng", vrng
);
229 virtio_cleanup(vdev
);
232 static Property virtio_rng_properties
[] = {
233 /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
234 * you have an entropy source capable of generating more entropy than this
235 * and you can pass it through via virtio-rng, then hats off to you. Until
236 * then, this is unlimited for all practical purposes.
238 DEFINE_PROP_UINT64("max-bytes", VirtIORNG
, conf
.max_bytes
, INT64_MAX
),
239 DEFINE_PROP_UINT32("period", VirtIORNG
, conf
.period_ms
, 1 << 16),
240 DEFINE_PROP_END_OF_LIST(),
243 static void virtio_rng_class_init(ObjectClass
*klass
, void *data
)
245 DeviceClass
*dc
= DEVICE_CLASS(klass
);
246 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
248 dc
->props
= virtio_rng_properties
;
249 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
250 vdc
->realize
= virtio_rng_device_realize
;
251 vdc
->unrealize
= virtio_rng_device_unrealize
;
252 vdc
->get_features
= get_features
;
255 static void virtio_rng_initfn(Object
*obj
)
257 VirtIORNG
*vrng
= VIRTIO_RNG(obj
);
259 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
260 (Object
**)&vrng
->conf
.rng
,
261 qdev_prop_allow_set_link_before_realize
,
262 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
265 static const TypeInfo virtio_rng_info
= {
266 .name
= TYPE_VIRTIO_RNG
,
267 .parent
= TYPE_VIRTIO_DEVICE
,
268 .instance_size
= sizeof(VirtIORNG
),
269 .instance_init
= virtio_rng_initfn
,
270 .class_init
= virtio_rng_class_init
,
273 static void virtio_register_types(void)
275 type_register_static(&virtio_rng_info
);
278 type_init(virtio_register_types
)