migration: move global_state.optional out
[qemu/armbru.git] / hw / virtio / virtio-rng.c
bloba6ee5010516a350cba5ac5c31190b4a1b7051f45
1 /*
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
9 * top-level directory.
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu/iov.h"
15 #include "hw/qdev.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"
20 #include "trace.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)) {
27 return true;
29 trace_virtio_rng_guest_not_ready(vrng);
30 return false;
33 static size_t get_request_size(VirtQueue *vq, unsigned quota)
35 unsigned int in, out;
37 virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
38 return in;
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;
49 size_t len;
50 int offset;
52 if (!is_guest_ready(vrng)) {
53 return;
56 /* we can't modify the virtqueue until
57 * our state is fully synced
60 if (!runstate_check(RUN_STATE_RUNNING)) {
61 trace_virtio_rng_cpu_is_stopped(vrng, size);
62 return;
65 vrng->quota_remaining -= size;
67 offset = 0;
68 while (offset < size) {
69 elem = virtqueue_pop(vrng->vq, sizeof(VirtQueueElement));
70 if (!elem) {
71 break;
73 trace_virtio_rng_popped(vrng);
74 len = iov_from_buf(elem->in_sg, elem->in_num,
75 0, buf + offset, size - offset);
76 offset += len;
78 virtqueue_push(vrng->vq, elem, len);
79 trace_virtio_rng_pushed(vrng, len);
80 g_free(elem);
82 virtio_notify(vdev, vrng->vq);
84 if (!virtio_queue_empty(vrng->vq)) {
85 /* If we didn't drain the queue, call virtio_rng_process
86 * to take care of asking for more data as appropriate.
88 virtio_rng_process(vrng);
92 static void virtio_rng_process(VirtIORNG *vrng)
94 size_t size;
95 unsigned quota;
97 if (!is_guest_ready(vrng)) {
98 return;
101 if (vrng->activate_timer) {
102 timer_mod(vrng->rate_limit_timer,
103 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
104 vrng->activate_timer = false;
107 if (vrng->quota_remaining < 0) {
108 quota = 0;
109 } else {
110 quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
112 size = get_request_size(vrng->vq, quota);
114 trace_virtio_rng_request(vrng, size, quota);
116 size = MIN(vrng->quota_remaining, size);
117 if (size) {
118 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
122 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
124 VirtIORNG *vrng = VIRTIO_RNG(vdev);
125 virtio_rng_process(vrng);
128 static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp)
130 return f;
133 static void virtio_rng_vm_state_change(void *opaque, int running,
134 RunState state)
136 VirtIORNG *vrng = opaque;
138 trace_virtio_rng_vm_state_change(vrng, running, state);
140 /* We may have an element ready but couldn't process it due to a quota
141 * limit or because CPU was stopped. Make sure to try again when the
142 * CPU restart.
145 if (running && is_guest_ready(vrng)) {
146 virtio_rng_process(vrng);
150 static void check_rate_limit(void *opaque)
152 VirtIORNG *vrng = opaque;
154 vrng->quota_remaining = vrng->conf.max_bytes;
155 virtio_rng_process(vrng);
156 vrng->activate_timer = true;
159 static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
161 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
162 VirtIORNG *vrng = VIRTIO_RNG(dev);
163 Error *local_err = NULL;
165 if (vrng->conf.period_ms <= 0) {
166 error_setg(errp, "'period' parameter expects a positive integer");
167 return;
170 /* Workaround: Property parsing does not enforce unsigned integers,
171 * So this is a hack to reject such numbers. */
172 if (vrng->conf.max_bytes > INT64_MAX) {
173 error_setg(errp, "'max-bytes' parameter must be non-negative, "
174 "and less than 2^63");
175 return;
178 if (vrng->conf.rng == NULL) {
179 vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
181 user_creatable_complete(OBJECT(vrng->conf.default_backend),
182 &local_err);
183 if (local_err) {
184 error_propagate(errp, local_err);
185 object_unref(OBJECT(vrng->conf.default_backend));
186 return;
189 object_property_add_child(OBJECT(dev),
190 "default-backend",
191 OBJECT(vrng->conf.default_backend),
192 NULL);
194 /* The child property took a reference, we can safely drop ours now */
195 object_unref(OBJECT(vrng->conf.default_backend));
197 object_property_set_link(OBJECT(dev),
198 OBJECT(vrng->conf.default_backend),
199 "rng", NULL);
202 vrng->rng = vrng->conf.rng;
203 if (vrng->rng == NULL) {
204 error_setg(errp, "'rng' parameter expects a valid object");
205 return;
208 virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0);
210 vrng->vq = virtio_add_queue(vdev, 8, handle_input);
211 vrng->quota_remaining = vrng->conf.max_bytes;
212 vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
213 check_rate_limit, vrng);
214 vrng->activate_timer = true;
216 vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change,
217 vrng);
220 static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp)
222 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
223 VirtIORNG *vrng = VIRTIO_RNG(dev);
225 qemu_del_vm_change_state_handler(vrng->vmstate);
226 timer_del(vrng->rate_limit_timer);
227 timer_free(vrng->rate_limit_timer);
228 virtio_cleanup(vdev);
231 static const VMStateDescription vmstate_virtio_rng = {
232 .name = "virtio-rng",
233 .minimum_version_id = 1,
234 .version_id = 1,
235 .fields = (VMStateField[]) {
236 VMSTATE_VIRTIO_DEVICE,
237 VMSTATE_END_OF_LIST()
241 static Property virtio_rng_properties[] = {
242 /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
243 * you have an entropy source capable of generating more entropy than this
244 * and you can pass it through via virtio-rng, then hats off to you. Until
245 * then, this is unlimited for all practical purposes.
247 DEFINE_PROP_UINT64("max-bytes", VirtIORNG, conf.max_bytes, INT64_MAX),
248 DEFINE_PROP_UINT32("period", VirtIORNG, conf.period_ms, 1 << 16),
249 DEFINE_PROP_END_OF_LIST(),
252 static void virtio_rng_class_init(ObjectClass *klass, void *data)
254 DeviceClass *dc = DEVICE_CLASS(klass);
255 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
257 dc->props = virtio_rng_properties;
258 dc->vmsd = &vmstate_virtio_rng;
259 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
260 vdc->realize = virtio_rng_device_realize;
261 vdc->unrealize = virtio_rng_device_unrealize;
262 vdc->get_features = get_features;
265 static void virtio_rng_initfn(Object *obj)
267 VirtIORNG *vrng = VIRTIO_RNG(obj);
269 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
270 (Object **)&vrng->conf.rng,
271 qdev_prop_allow_set_link_before_realize,
272 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
275 static const TypeInfo virtio_rng_info = {
276 .name = TYPE_VIRTIO_RNG,
277 .parent = TYPE_VIRTIO_DEVICE,
278 .instance_size = sizeof(VirtIORNG),
279 .instance_init = virtio_rng_initfn,
280 .class_init = virtio_rng_class_init,
283 static void virtio_register_types(void)
285 type_register_static(&virtio_rng_info);
288 type_init(virtio_register_types)