]> git.proxmox.com Git - qemu.git/blame - hw/virtio-rng.c
qemu-tech.texi: update implemented xtensa features list
[qemu.git] / hw / virtio-rng.c
CommitLineData
16c915ba
AS
1/*
2 * A virtio device implementing a hardware random number generator.
3 *
4 * Copyright 2012 Red Hat, Inc.
5 * Copyright 2012 Amit Shah <amit.shah@redhat.com>
6 *
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.
10 */
11
12#include "iov.h"
13#include "qdev.h"
14#include "virtio.h"
15#include "virtio-rng.h"
16#include "qemu/rng.h"
17
18typedef struct VirtIORNG {
19 VirtIODevice vdev;
20
21 DeviceState *qdev;
22
23 /* Only one vq - guest puts buffer(s) on it when it needs entropy */
24 VirtQueue *vq;
16c915ba 25
16c915ba
AS
26 VirtIORNGConf *conf;
27
16c915ba 28 RngBackend *rng;
904d6f58
AL
29
30 /* We purposefully don't migrate this state. The quota will reset on the
31 * destination as a result. Rate limiting is host state, not guest state.
32 */
33 QEMUTimer *rate_limit_timer;
34 int64_t quota_remaining;
16c915ba
AS
35} VirtIORNG;
36
37static bool is_guest_ready(VirtIORNG *vrng)
38{
39 if (virtio_queue_ready(vrng->vq)
40 && (vrng->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
41 return true;
42 }
43 return false;
44}
45
14417039 46static size_t get_request_size(VirtQueue *vq)
16c915ba 47{
14417039 48 unsigned int in, out;
16c915ba 49
14417039
AS
50 virtqueue_get_avail_bytes(vq, &in, &out);
51 return in;
16c915ba
AS
52}
53
904d6f58
AL
54static void virtio_rng_process(VirtIORNG *vrng);
55
16c915ba
AS
56/* Send data from a char device over to the guest */
57static void chr_read(void *opaque, const void *buf, size_t size)
58{
59 VirtIORNG *vrng = opaque;
14417039 60 VirtQueueElement elem;
16c915ba
AS
61 size_t len;
62 int offset;
63
64 if (!is_guest_ready(vrng)) {
65 return;
66 }
67
904d6f58
AL
68 vrng->quota_remaining -= size;
69
16c915ba
AS
70 offset = 0;
71 while (offset < size) {
14417039 72 if (!virtqueue_pop(vrng->vq, &elem)) {
16c915ba
AS
73 break;
74 }
14417039 75 len = iov_from_buf(elem.in_sg, elem.in_num,
16c915ba
AS
76 0, buf + offset, size - offset);
77 offset += len;
78
14417039 79 virtqueue_push(vrng->vq, &elem, len);
16c915ba
AS
80 }
81 virtio_notify(&vrng->vdev, vrng->vq);
16c915ba
AS
82}
83
904d6f58 84static void virtio_rng_process(VirtIORNG *vrng)
16c915ba 85{
14417039 86 size_t size;
904d6f58
AL
87
88 if (!is_guest_ready(vrng)) {
89 return;
90 }
16c915ba 91
14417039 92 size = get_request_size(vrng->vq);
904d6f58 93 size = MIN(vrng->quota_remaining, size);
14417039 94 if (size) {
16c915ba
AS
95 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
96 }
97}
98
904d6f58
AL
99static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
100{
101 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
102 virtio_rng_process(vrng);
103}
104
16c915ba
AS
105static uint32_t get_features(VirtIODevice *vdev, uint32_t f)
106{
107 return f;
108}
109
110static void virtio_rng_save(QEMUFile *f, void *opaque)
111{
112 VirtIORNG *vrng = opaque;
113
114 virtio_save(&vrng->vdev, f);
16c915ba
AS
115}
116
117static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id)
118{
119 VirtIORNG *vrng = opaque;
120
121 if (version_id != 1) {
122 return -EINVAL;
123 }
124 virtio_load(&vrng->vdev, f);
125
904d6f58 126 /* We may have an element ready but couldn't process it due to a quota
42015c9a
AS
127 * limit. Make sure to try again after live migration when the quota may
128 * have been reset.
129 */
904d6f58
AL
130 virtio_rng_process(vrng);
131
16c915ba
AS
132 return 0;
133}
134
904d6f58
AL
135static void check_rate_limit(void *opaque)
136{
137 VirtIORNG *s = opaque;
138
139 s->quota_remaining = s->conf->max_bytes;
140 virtio_rng_process(s);
141 qemu_mod_timer(s->rate_limit_timer,
142 qemu_get_clock_ms(vm_clock) + s->conf->period_ms);
143}
144
145
16c915ba
AS
146VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf)
147{
148 VirtIORNG *vrng;
149 VirtIODevice *vdev;
150 Error *local_err = NULL;
151
152 vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0,
153 sizeof(VirtIORNG));
154
155 vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
156
157 vrng->rng = conf->rng;
158 if (vrng->rng == NULL) {
159 qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
160 return NULL;
161 }
162
163 rng_backend_open(vrng->rng, &local_err);
164 if (local_err) {
165 qerror_report_err(local_err);
166 error_free(local_err);
167 return NULL;
168 }
169
170 vrng->vq = virtio_add_queue(vdev, 8, handle_input);
171 vrng->vdev.get_features = get_features;
172
173 vrng->qdev = dev;
174 vrng->conf = conf;
14417039 175
03a36f17 176 assert(vrng->conf->max_bytes <= INT64_MAX);
904d6f58
AL
177 vrng->quota_remaining = vrng->conf->max_bytes;
178
904d6f58
AL
179 vrng->rate_limit_timer = qemu_new_timer_ms(vm_clock,
180 check_rate_limit, vrng);
181
182 qemu_mod_timer(vrng->rate_limit_timer,
183 qemu_get_clock_ms(vm_clock) + vrng->conf->period_ms);
184
16c915ba
AS
185 register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
186 virtio_rng_load, vrng);
187
188 return vdev;
189}
190
191void virtio_rng_exit(VirtIODevice *vdev)
192{
193 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
194
8cc67743
AS
195 qemu_del_timer(vrng->rate_limit_timer);
196 qemu_free_timer(vrng->rate_limit_timer);
16c915ba
AS
197 unregister_savevm(vrng->qdev, "virtio-rng", vrng);
198 virtio_cleanup(vdev);
199}