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
14 #include "qapi/qmp/qerror.h"
16 #include "virtio-rng.h"
19 typedef struct VirtIORNG {
24 /* Only one vq - guest puts buffer(s) on it when it needs entropy */
31 /* We purposefully don't migrate this state. The quota will reset on the
32 * destination as a result. Rate limiting is host state, not guest state.
34 QEMUTimer *rate_limit_timer;
35 int64_t quota_remaining;
38 static bool is_guest_ready(VirtIORNG *vrng)
40 if (virtio_queue_ready(vrng->vq)
41 && (vrng->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
47 static size_t get_request_size(VirtQueue *vq, unsigned quota)
51 virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
55 static void virtio_rng_process(VirtIORNG *vrng);
57 /* Send data from a char device over to the guest */
58 static void chr_read(void *opaque, const void *buf, size_t size)
60 VirtIORNG *vrng = opaque;
61 VirtQueueElement elem;
65 if (!is_guest_ready(vrng)) {
69 vrng->quota_remaining -= size;
72 while (offset < size) {
73 if (!virtqueue_pop(vrng->vq, &elem)) {
76 len = iov_from_buf(elem.in_sg, elem.in_num,
77 0, buf + offset, size - offset);
80 virtqueue_push(vrng->vq, &elem, len);
82 virtio_notify(&vrng->vdev, vrng->vq);
85 static void virtio_rng_process(VirtIORNG *vrng)
90 if (!is_guest_ready(vrng)) {
94 if (vrng->quota_remaining < 0) {
97 quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
99 size = get_request_size(vrng->vq, quota);
100 size = MIN(vrng->quota_remaining, size);
102 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
106 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
108 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
109 virtio_rng_process(vrng);
112 static uint32_t get_features(VirtIODevice *vdev, uint32_t f)
117 static void virtio_rng_save(QEMUFile *f, void *opaque)
119 VirtIORNG *vrng = opaque;
121 virtio_save(&vrng->vdev, f);
124 static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id)
126 VirtIORNG *vrng = opaque;
128 if (version_id != 1) {
131 virtio_load(&vrng->vdev, f);
133 /* We may have an element ready but couldn't process it due to a quota
134 * limit. Make sure to try again after live migration when the quota may
137 virtio_rng_process(vrng);
142 static void check_rate_limit(void *opaque)
144 VirtIORNG *s = opaque;
146 s->quota_remaining = s->conf->max_bytes;
147 virtio_rng_process(s);
148 qemu_mod_timer(s->rate_limit_timer,
149 qemu_get_clock_ms(vm_clock) + s->conf->period_ms);
153 VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf)
157 Error *local_err = NULL;
159 vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0,
162 vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
164 vrng->rng = conf->rng;
165 if (vrng->rng == NULL) {
166 qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
170 rng_backend_open(vrng->rng, &local_err);
172 qerror_report_err(local_err);
173 error_free(local_err);
177 vrng->vq = virtio_add_queue(vdev, 8, handle_input);
178 vrng->vdev.get_features = get_features;
183 assert(vrng->conf->max_bytes <= INT64_MAX);
184 vrng->quota_remaining = vrng->conf->max_bytes;
186 vrng->rate_limit_timer = qemu_new_timer_ms(vm_clock,
187 check_rate_limit, vrng);
189 qemu_mod_timer(vrng->rate_limit_timer,
190 qemu_get_clock_ms(vm_clock) + vrng->conf->period_ms);
192 register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
193 virtio_rng_load, vrng);
198 void virtio_rng_exit(VirtIODevice *vdev)
200 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
202 qemu_del_timer(vrng->rate_limit_timer);
203 qemu_free_timer(vrng->rate_limit_timer);
204 unregister_savevm(vrng->qdev, "virtio-rng", vrng);
205 virtio_cleanup(vdev);