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
14 #include "qapi/qmp/qerror.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"
20 static bool is_guest_ready(VirtIORNG
*vrng
)
22 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
23 if (virtio_queue_ready(vrng
->vq
)
24 && (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
30 static size_t get_request_size(VirtQueue
*vq
, unsigned quota
)
34 virtqueue_get_avail_bytes(vq
, &in
, &out
, quota
, 0);
38 static void virtio_rng_process(VirtIORNG
*vrng
);
40 /* Send data from a char device over to the guest */
41 static void chr_read(void *opaque
, const void *buf
, size_t size
)
43 VirtIORNG
*vrng
= opaque
;
44 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
45 VirtQueueElement elem
;
49 if (!is_guest_ready(vrng
)) {
53 vrng
->quota_remaining
-= size
;
56 while (offset
< size
) {
57 if (!virtqueue_pop(vrng
->vq
, &elem
)) {
60 len
= iov_from_buf(elem
.in_sg
, elem
.in_num
,
61 0, buf
+ offset
, size
- offset
);
64 virtqueue_push(vrng
->vq
, &elem
, len
);
66 virtio_notify(vdev
, vrng
->vq
);
69 static void virtio_rng_process(VirtIORNG
*vrng
)
74 if (!is_guest_ready(vrng
)) {
78 if (vrng
->quota_remaining
< 0) {
81 quota
= MIN((uint64_t)vrng
->quota_remaining
, (uint64_t)UINT32_MAX
);
83 size
= get_request_size(vrng
->vq
, quota
);
84 size
= MIN(vrng
->quota_remaining
, size
);
86 rng_backend_request_entropy(vrng
->rng
, size
, chr_read
, vrng
);
90 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
92 VirtIORNG
*vrng
= VIRTIO_RNG(vdev
);
93 virtio_rng_process(vrng
);
96 static uint32_t get_features(VirtIODevice
*vdev
, uint32_t f
)
101 static void virtio_rng_save(QEMUFile
*f
, void *opaque
)
103 VirtIODevice
*vdev
= opaque
;
105 virtio_save(vdev
, f
);
108 static int virtio_rng_load(QEMUFile
*f
, void *opaque
, int version_id
)
110 VirtIORNG
*vrng
= opaque
;
111 VirtIODevice
*vdev
= VIRTIO_DEVICE(vrng
);
113 if (version_id
!= 1) {
116 virtio_load(vdev
, f
);
118 /* We may have an element ready but couldn't process it due to a quota
119 * limit. Make sure to try again after live migration when the quota may
122 virtio_rng_process(vrng
);
127 static void check_rate_limit(void *opaque
)
129 VirtIORNG
*vrng
= opaque
;
131 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
132 virtio_rng_process(vrng
);
133 timer_mod(vrng
->rate_limit_timer
,
134 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vrng
->conf
.period_ms
);
137 static void virtio_rng_device_realize(DeviceState
*dev
, Error
**errp
)
139 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
140 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
141 Error
*local_err
= NULL
;
143 if (!vrng
->conf
.period_ms
> 0) {
144 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "period",
145 "a positive number");
149 if (vrng
->conf
.rng
== NULL
) {
150 vrng
->conf
.default_backend
= RNG_RANDOM(object_new(TYPE_RNG_RANDOM
));
152 user_creatable_complete(OBJECT(vrng
->conf
.default_backend
),
155 error_propagate(errp
, local_err
);
156 object_unref(OBJECT(vrng
->conf
.default_backend
));
160 object_property_add_child(OBJECT(dev
),
162 OBJECT(vrng
->conf
.default_backend
),
165 /* The child property took a reference, we can safely drop ours now */
166 object_unref(OBJECT(vrng
->conf
.default_backend
));
168 object_property_set_link(OBJECT(dev
),
169 OBJECT(vrng
->conf
.default_backend
),
173 virtio_init(vdev
, "virtio-rng", VIRTIO_ID_RNG
, 0);
175 vrng
->rng
= vrng
->conf
.rng
;
176 if (vrng
->rng
== NULL
) {
177 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "rng", "a valid object");
181 vrng
->vq
= virtio_add_queue(vdev
, 8, handle_input
);
183 assert(vrng
->conf
.max_bytes
<= INT64_MAX
);
184 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
186 vrng
->rate_limit_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
187 check_rate_limit
, vrng
);
189 timer_mod(vrng
->rate_limit_timer
,
190 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vrng
->conf
.period_ms
);
192 register_savevm(dev
, "virtio-rng", -1, 1, virtio_rng_save
,
193 virtio_rng_load
, vrng
);
196 static void virtio_rng_device_unrealize(DeviceState
*dev
, Error
**errp
)
198 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
199 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
201 timer_del(vrng
->rate_limit_timer
);
202 timer_free(vrng
->rate_limit_timer
);
203 unregister_savevm(dev
, "virtio-rng", vrng
);
204 virtio_cleanup(vdev
);
207 static Property virtio_rng_properties
[] = {
208 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORNG
, conf
),
209 DEFINE_PROP_END_OF_LIST(),
212 static void virtio_rng_class_init(ObjectClass
*klass
, void *data
)
214 DeviceClass
*dc
= DEVICE_CLASS(klass
);
215 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
217 dc
->props
= virtio_rng_properties
;
218 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
219 vdc
->realize
= virtio_rng_device_realize
;
220 vdc
->unrealize
= virtio_rng_device_unrealize
;
221 vdc
->get_features
= get_features
;
224 static void virtio_rng_initfn(Object
*obj
)
226 VirtIORNG
*vrng
= VIRTIO_RNG(obj
);
228 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
229 (Object
**)&vrng
->conf
.rng
,
230 qdev_prop_allow_set_link_before_realize
,
231 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
234 static const TypeInfo virtio_rng_info
= {
235 .name
= TYPE_VIRTIO_RNG
,
236 .parent
= TYPE_VIRTIO_DEVICE
,
237 .instance_size
= sizeof(VirtIORNG
),
238 .instance_init
= virtio_rng_initfn
,
239 .class_init
= virtio_rng_class_init
,
242 static void virtio_register_types(void)
244 type_register_static(&virtio_rng_info
);
247 type_init(virtio_register_types
)