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