2 * A virtio device implementing a hardware random number generator.
4 * Copyright 2012 Red Hat, Inc.
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
15 #include "virtio-rng.h"
18 typedef struct VirtIORNG {
23 /* Only one vq - guest puts buffer(s) on it when it needs entropy */
25 VirtQueueElement elem;
27 /* Config data for the device -- currently only chardev */
30 /* Whether we've popped a vq element into 'elem' above */
35 /* We purposefully don't migrate this state. The quota will reset on the
36 * destination as a result. Rate limiting is host state, not guest state.
38 QEMUTimer *rate_limit_timer;
39 int64_t quota_remaining;
42 static bool is_guest_ready(VirtIORNG *vrng)
44 if (virtio_queue_ready(vrng->vq)
45 && (vrng->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
51 static size_t pop_an_elem(VirtIORNG *vrng)
55 if (!vrng->popped && !virtqueue_pop(vrng->vq, &vrng->elem)) {
60 size = iov_size(vrng->elem.in_sg, vrng->elem.in_num);
64 static void virtio_rng_process(VirtIORNG *vrng);
66 /* Send data from a char device over to the guest */
67 static void chr_read(void *opaque, const void *buf, size_t size)
69 VirtIORNG *vrng = opaque;
73 if (!is_guest_ready(vrng)) {
77 vrng->quota_remaining -= size;
80 while (offset < size) {
81 if (!pop_an_elem(vrng)) {
84 len = iov_from_buf(vrng->elem.in_sg, vrng->elem.in_num,
85 0, buf + offset, size - offset);
88 virtqueue_push(vrng->vq, &vrng->elem, len);
91 virtio_notify(&vrng->vdev, vrng->vq);
94 * Lastly, if we had multiple elems queued by the guest, and we
95 * didn't have enough data to fill them all, indicate we want more
98 virtio_rng_process(vrng);
101 static void virtio_rng_process(VirtIORNG *vrng)
105 if (!is_guest_ready(vrng)) {
109 size = pop_an_elem(vrng);
110 size = MIN(vrng->quota_remaining, size);
113 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
118 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
120 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
121 virtio_rng_process(vrng);
124 static uint32_t get_features(VirtIODevice *vdev, uint32_t f)
129 static void virtio_rng_save(QEMUFile *f, void *opaque)
131 VirtIORNG *vrng = opaque;
133 virtio_save(&vrng->vdev, f);
135 qemu_put_byte(f, vrng->popped);
139 qemu_put_be32(f, vrng->elem.index);
141 qemu_put_be32(f, vrng->elem.in_num);
142 for (i = 0; i < vrng->elem.in_num; i++) {
143 qemu_put_be64(f, vrng->elem.in_addr[i]);
146 qemu_put_be32(f, vrng->elem.out_num);
147 for (i = 0; i < vrng->elem.out_num; i++) {
148 qemu_put_be64(f, vrng->elem.out_addr[i]);
153 static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id)
155 VirtIORNG *vrng = opaque;
157 if (version_id != 1) {
160 virtio_load(&vrng->vdev, f);
162 vrng->popped = qemu_get_byte(f);
166 vrng->elem.index = qemu_get_be32(f);
168 vrng->elem.in_num = qemu_get_be32(f);
169 g_assert(vrng->elem.in_num < VIRTQUEUE_MAX_SIZE);
170 for (i = 0; i < vrng->elem.in_num; i++) {
171 vrng->elem.in_addr[i] = qemu_get_be64(f);
174 vrng->elem.out_num = qemu_get_be32(f);
175 g_assert(vrng->elem.out_num < VIRTQUEUE_MAX_SIZE);
176 for (i = 0; i < vrng->elem.out_num; i++) {
177 vrng->elem.out_addr[i] = qemu_get_be64(f);
180 virtqueue_map_sg(vrng->elem.in_sg, vrng->elem.in_addr,
181 vrng->elem.in_num, 1);
182 virtqueue_map_sg(vrng->elem.out_sg, vrng->elem.out_addr,
183 vrng->elem.out_num, 0);
186 /* We may have an element ready but couldn't process it due to a quota
187 limit. Make sure to try again after live migration when the quota may
190 virtio_rng_process(vrng);
195 static void check_rate_limit(void *opaque)
197 VirtIORNG *s = opaque;
199 s->quota_remaining = s->conf->max_bytes;
200 virtio_rng_process(s);
201 qemu_mod_timer(s->rate_limit_timer,
202 qemu_get_clock_ms(vm_clock) + s->conf->period_ms);
206 VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf)
210 Error *local_err = NULL;
212 vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0,
215 vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
217 vrng->rng = conf->rng;
218 if (vrng->rng == NULL) {
219 qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
223 rng_backend_open(vrng->rng, &local_err);
225 qerror_report_err(local_err);
226 error_free(local_err);
230 vrng->vq = virtio_add_queue(vdev, 8, handle_input);
231 vrng->vdev.get_features = get_features;
235 vrng->popped = false;
236 vrng->quota_remaining = vrng->conf->max_bytes;
238 g_assert_cmpint(vrng->conf->max_bytes, <=, INT64_MAX);
240 vrng->rate_limit_timer = qemu_new_timer_ms(vm_clock,
241 check_rate_limit, vrng);
243 qemu_mod_timer(vrng->rate_limit_timer,
244 qemu_get_clock_ms(vm_clock) + vrng->conf->period_ms);
246 register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
247 virtio_rng_load, vrng);
252 void virtio_rng_exit(VirtIODevice *vdev)
254 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
256 unregister_savevm(vrng->qdev, "virtio-rng", vrng);
257 virtio_cleanup(vdev);