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"
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 if (!virtqueue_pop(vrng
->vq
, &elem
)) {
62 len
= iov_from_buf(elem
.in_sg
, elem
.in_num
,
63 0, buf
+ offset
, size
- offset
);
66 virtqueue_push(vrng
->vq
, &elem
, len
);
67 trace_virtio_rng_pushed(vrng
, len
);
69 virtio_notify(vdev
, vrng
->vq
);
72 static void virtio_rng_process(VirtIORNG
*vrng
)
77 if (!is_guest_ready(vrng
)) {
81 if (vrng
->quota_remaining
< 0) {
84 quota
= MIN((uint64_t)vrng
->quota_remaining
, (uint64_t)UINT32_MAX
);
86 size
= get_request_size(vrng
->vq
, quota
);
88 trace_virtio_rng_request(vrng
, size
, quota
);
90 size
= MIN(vrng
->quota_remaining
, size
);
92 rng_backend_request_entropy(vrng
->rng
, size
, chr_read
, vrng
);
96 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
98 VirtIORNG
*vrng
= VIRTIO_RNG(vdev
);
99 virtio_rng_process(vrng
);
102 static uint32_t get_features(VirtIODevice
*vdev
, uint32_t f
)
107 static void virtio_rng_save(QEMUFile
*f
, void *opaque
)
109 VirtIODevice
*vdev
= opaque
;
111 virtio_save(vdev
, f
);
114 static int virtio_rng_load(QEMUFile
*f
, void *opaque
, int version_id
)
116 VirtIORNG
*vrng
= opaque
;
119 if (version_id
!= 1) {
122 ret
= virtio_load(VIRTIO_DEVICE(vrng
), f
, version_id
);
127 /* We may have an element ready but couldn't process it due to a quota
128 * limit. Make sure to try again after live migration when the quota may
131 virtio_rng_process(vrng
);
136 static void check_rate_limit(void *opaque
)
138 VirtIORNG
*vrng
= opaque
;
140 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
141 virtio_rng_process(vrng
);
142 timer_mod(vrng
->rate_limit_timer
,
143 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vrng
->conf
.period_ms
);
146 static void virtio_rng_device_realize(DeviceState
*dev
, Error
**errp
)
148 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
149 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
150 Error
*local_err
= NULL
;
152 if (vrng
->conf
.period_ms
<= 0) {
153 error_setg(errp
, "'period' parameter expects a positive integer");
157 /* Workaround: Property parsing does not enforce unsigned integers,
158 * So this is a hack to reject such numbers. */
159 if (vrng
->conf
.max_bytes
> INT64_MAX
) {
160 error_setg(errp
, "'max-bytes' parameter must be non-negative, "
161 "and less than 2^63");
165 if (vrng
->conf
.rng
== NULL
) {
166 vrng
->conf
.default_backend
= RNG_RANDOM(object_new(TYPE_RNG_RANDOM
));
168 user_creatable_complete(OBJECT(vrng
->conf
.default_backend
),
171 error_propagate(errp
, local_err
);
172 object_unref(OBJECT(vrng
->conf
.default_backend
));
176 object_property_add_child(OBJECT(dev
),
178 OBJECT(vrng
->conf
.default_backend
),
181 /* The child property took a reference, we can safely drop ours now */
182 object_unref(OBJECT(vrng
->conf
.default_backend
));
184 object_property_set_link(OBJECT(dev
),
185 OBJECT(vrng
->conf
.default_backend
),
189 vrng
->rng
= vrng
->conf
.rng
;
190 if (vrng
->rng
== NULL
) {
191 error_setg(errp
, "'rng' parameter expects a valid object");
195 virtio_init(vdev
, "virtio-rng", VIRTIO_ID_RNG
, 0);
197 vrng
->vq
= virtio_add_queue(vdev
, 8, handle_input
);
198 vrng
->quota_remaining
= vrng
->conf
.max_bytes
;
200 vrng
->rate_limit_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
201 check_rate_limit
, vrng
);
203 timer_mod(vrng
->rate_limit_timer
,
204 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + vrng
->conf
.period_ms
);
206 register_savevm(dev
, "virtio-rng", -1, 1, virtio_rng_save
,
207 virtio_rng_load
, vrng
);
210 static void virtio_rng_device_unrealize(DeviceState
*dev
, Error
**errp
)
212 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
213 VirtIORNG
*vrng
= VIRTIO_RNG(dev
);
215 timer_del(vrng
->rate_limit_timer
);
216 timer_free(vrng
->rate_limit_timer
);
217 unregister_savevm(dev
, "virtio-rng", vrng
);
218 virtio_cleanup(vdev
);
221 static Property virtio_rng_properties
[] = {
222 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORNG
, conf
),
223 DEFINE_PROP_END_OF_LIST(),
226 static void virtio_rng_class_init(ObjectClass
*klass
, void *data
)
228 DeviceClass
*dc
= DEVICE_CLASS(klass
);
229 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
231 dc
->props
= virtio_rng_properties
;
232 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
233 vdc
->realize
= virtio_rng_device_realize
;
234 vdc
->unrealize
= virtio_rng_device_unrealize
;
235 vdc
->get_features
= get_features
;
238 static void virtio_rng_initfn(Object
*obj
)
240 VirtIORNG
*vrng
= VIRTIO_RNG(obj
);
242 object_property_add_link(obj
, "rng", TYPE_RNG_BACKEND
,
243 (Object
**)&vrng
->conf
.rng
,
244 qdev_prop_allow_set_link_before_realize
,
245 OBJ_PROP_LINK_UNREF_ON_RELEASE
, NULL
);
248 static const TypeInfo virtio_rng_info
= {
249 .name
= TYPE_VIRTIO_RNG
,
250 .parent
= TYPE_VIRTIO_DEVICE
,
251 .instance_size
= sizeof(VirtIORNG
),
252 .instance_init
= virtio_rng_initfn
,
253 .class_init
= virtio_rng_class_init
,
256 static void virtio_register_types(void)
258 type_register_static(&virtio_rng_info
);
261 type_init(virtio_register_types
)