]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/virtio-rng.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[mirror_qemu.git] / hw / virtio / 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
9b8bfe21 12#include "qemu/osdep.h"
da34e65c 13#include "qapi/error.h"
1de7afc9 14#include "qemu/iov.h"
0b8fa32f 15#include "qemu/module.h"
83c9f4ca 16#include "hw/qdev.h"
0d09e41a
PB
17#include "hw/virtio/virtio.h"
18#include "hw/virtio/virtio-rng.h"
dccfcd0e 19#include "sysemu/rng.h"
57d3e1b3 20#include "qom/object_interfaces.h"
4ac44580 21#include "trace.h"
16c915ba 22
16c915ba
AS
23static bool is_guest_ready(VirtIORNG *vrng)
24{
611aa333 25 VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
16c915ba 26 if (virtio_queue_ready(vrng->vq)
611aa333 27 && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
16c915ba
AS
28 return true;
29 }
4ac44580 30 trace_virtio_rng_guest_not_ready(vrng);
16c915ba
AS
31 return false;
32}
33
e1f7b481 34static size_t get_request_size(VirtQueue *vq, unsigned quota)
16c915ba 35{
14417039 36 unsigned int in, out;
16c915ba 37
e1f7b481 38 virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
14417039 39 return in;
16c915ba
AS
40}
41
904d6f58
AL
42static void virtio_rng_process(VirtIORNG *vrng);
43
16c915ba
AS
44/* Send data from a char device over to the guest */
45static void chr_read(void *opaque, const void *buf, size_t size)
46{
47 VirtIORNG *vrng = opaque;
611aa333 48 VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
51b19ebe 49 VirtQueueElement *elem;
16c915ba
AS
50 size_t len;
51 int offset;
52
53 if (!is_guest_ready(vrng)) {
54 return;
55 }
56
a23a6d18
LV
57 /* we can't modify the virtqueue until
58 * our state is fully synced
59 */
60
61 if (!runstate_check(RUN_STATE_RUNNING)) {
62 trace_virtio_rng_cpu_is_stopped(vrng, size);
63 return;
64 }
65
904d6f58
AL
66 vrng->quota_remaining -= size;
67
16c915ba
AS
68 offset = 0;
69 while (offset < size) {
51b19ebe
PB
70 elem = virtqueue_pop(vrng->vq, sizeof(VirtQueueElement));
71 if (!elem) {
16c915ba
AS
72 break;
73 }
a23a6d18 74 trace_virtio_rng_popped(vrng);
51b19ebe 75 len = iov_from_buf(elem->in_sg, elem->in_num,
16c915ba
AS
76 0, buf + offset, size - offset);
77 offset += len;
78
51b19ebe 79 virtqueue_push(vrng->vq, elem, len);
4ac44580 80 trace_virtio_rng_pushed(vrng, len);
51b19ebe 81 g_free(elem);
16c915ba 82 }
611aa333 83 virtio_notify(vdev, vrng->vq);
f8693c2c
LP
84
85 if (!virtio_queue_empty(vrng->vq)) {
86 /* If we didn't drain the queue, call virtio_rng_process
87 * to take care of asking for more data as appropriate.
88 */
89 virtio_rng_process(vrng);
90 }
16c915ba
AS
91}
92
904d6f58 93static void virtio_rng_process(VirtIORNG *vrng)
16c915ba 94{
14417039 95 size_t size;
e1f7b481 96 unsigned quota;
904d6f58
AL
97
98 if (!is_guest_ready(vrng)) {
99 return;
100 }
16c915ba 101
621a20e0
PG
102 if (vrng->activate_timer) {
103 timer_mod(vrng->rate_limit_timer,
104 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
105 vrng->activate_timer = false;
106 }
107
e1f7b481
MT
108 if (vrng->quota_remaining < 0) {
109 quota = 0;
110 } else {
111 quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
112 }
113 size = get_request_size(vrng->vq, quota);
4ac44580
AS
114
115 trace_virtio_rng_request(vrng, size, quota);
116
904d6f58 117 size = MIN(vrng->quota_remaining, size);
14417039 118 if (size) {
16c915ba
AS
119 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
120 }
121}
122
904d6f58
AL
123static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
124{
611aa333 125 VirtIORNG *vrng = VIRTIO_RNG(vdev);
904d6f58
AL
126 virtio_rng_process(vrng);
127}
128
9d5b731d 129static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp)
16c915ba
AS
130{
131 return f;
132}
133
a23a6d18
LV
134static void virtio_rng_vm_state_change(void *opaque, int running,
135 RunState state)
16c915ba 136{
db12451d 137 VirtIORNG *vrng = opaque;
16c915ba 138
a23a6d18
LV
139 trace_virtio_rng_vm_state_change(vrng, running, state);
140
904d6f58 141 /* We may have an element ready but couldn't process it due to a quota
a23a6d18
LV
142 * limit or because CPU was stopped. Make sure to try again when the
143 * CPU restart.
42015c9a 144 */
904d6f58 145
a23a6d18
LV
146 if (running && is_guest_ready(vrng)) {
147 virtio_rng_process(vrng);
148 }
16c915ba
AS
149}
150
904d6f58
AL
151static void check_rate_limit(void *opaque)
152{
611aa333 153 VirtIORNG *vrng = opaque;
904d6f58 154
611aa333
FK
155 vrng->quota_remaining = vrng->conf.max_bytes;
156 virtio_rng_process(vrng);
621a20e0 157 vrng->activate_timer = true;
904d6f58
AL
158}
159
5d9c9ea2
PG
160static void virtio_rng_set_status(VirtIODevice *vdev, uint8_t status)
161{
162 VirtIORNG *vrng = VIRTIO_RNG(vdev);
163
164 if (!vdev->vm_running) {
165 return;
166 }
167 vdev->status = status;
168
169 /* Something changed, try to process buffers */
170 virtio_rng_process(vrng);
171}
172
a8d57dfb 173static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
16c915ba 174{
a8d57dfb 175 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
af7671fd 176 VirtIORNG *vrng = VIRTIO_RNG(dev);
16c915ba
AS
177 Error *local_err = NULL;
178
a3a292c4 179 if (vrng->conf.period_ms <= 0) {
c617dd3b 180 error_setg(errp, "'period' parameter expects a positive integer");
a8d57dfb 181 return;
d44bb860
AK
182 }
183
1efd6e07
JS
184 /* Workaround: Property parsing does not enforce unsigned integers,
185 * So this is a hack to reject such numbers. */
186 if (vrng->conf.max_bytes > INT64_MAX) {
c617dd3b
JS
187 error_setg(errp, "'max-bytes' parameter must be non-negative, "
188 "and less than 2^63");
1efd6e07
JS
189 return;
190 }
191
46a5a89d
FK
192 if (vrng->conf.rng == NULL) {
193 vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
194
3650b2de 195 user_creatable_complete(USER_CREATABLE(vrng->conf.default_backend),
57d3e1b3
IM
196 &local_err);
197 if (local_err) {
198 error_propagate(errp, local_err);
199 object_unref(OBJECT(vrng->conf.default_backend));
200 return;
201 }
202
af7671fd 203 object_property_add_child(OBJECT(dev),
46a5a89d
FK
204 "default-backend",
205 OBJECT(vrng->conf.default_backend),
206 NULL);
207
abdffd1f
SH
208 /* The child property took a reference, we can safely drop ours now */
209 object_unref(OBJECT(vrng->conf.default_backend));
210
af7671fd 211 object_property_set_link(OBJECT(dev),
46a5a89d
FK
212 OBJECT(vrng->conf.default_backend),
213 "rng", NULL);
6eac8aec 214 }
16c915ba 215
46a5a89d 216 vrng->rng = vrng->conf.rng;
16c915ba 217 if (vrng->rng == NULL) {
c617dd3b 218 error_setg(errp, "'rng' parameter expects a valid object");
a8d57dfb 219 return;
16c915ba
AS
220 }
221
1efd6e07 222 virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0);
6eac8aec 223
1efd6e07 224 vrng->vq = virtio_add_queue(vdev, 8, handle_input);
af1a8ad6 225 vrng->quota_remaining = vrng->conf.max_bytes;
bc72ad67 226 vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
904d6f58 227 check_rate_limit, vrng);
621a20e0 228 vrng->activate_timer = true;
a23a6d18
LV
229
230 vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change,
231 vrng);
6eac8aec
FK
232}
233
306ec6c3 234static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp)
6eac8aec 235{
306ec6c3
AF
236 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
237 VirtIORNG *vrng = VIRTIO_RNG(dev);
6eac8aec 238
a23a6d18 239 qemu_del_vm_change_state_handler(vrng->vmstate);
bc72ad67
AB
240 timer_del(vrng->rate_limit_timer);
241 timer_free(vrng->rate_limit_timer);
6a1a8cc7 242 virtio_cleanup(vdev);
6eac8aec
FK
243}
244
b7de81f6
HP
245static const VMStateDescription vmstate_virtio_rng = {
246 .name = "virtio-rng",
247 .minimum_version_id = 1,
248 .version_id = 1,
249 .fields = (VMStateField[]) {
250 VMSTATE_VIRTIO_DEVICE,
251 VMSTATE_END_OF_LIST()
252 },
b7de81f6 253};
b6075793 254
6eac8aec 255static Property virtio_rng_properties[] = {
fe704809
SZ
256 /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
257 * you have an entropy source capable of generating more entropy than this
258 * and you can pass it through via virtio-rng, then hats off to you. Until
259 * then, this is unlimited for all practical purposes.
260 */
261 DEFINE_PROP_UINT64("max-bytes", VirtIORNG, conf.max_bytes, INT64_MAX),
262 DEFINE_PROP_UINT32("period", VirtIORNG, conf.period_ms, 1 << 16),
d1fd7f77 263 DEFINE_PROP_LINK("rng", VirtIORNG, conf.rng, TYPE_RNG_BACKEND, RngBackend *),
6eac8aec
FK
264 DEFINE_PROP_END_OF_LIST(),
265};
266
267static void virtio_rng_class_init(ObjectClass *klass, void *data)
268{
269 DeviceClass *dc = DEVICE_CLASS(klass);
270 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
a8d57dfb 271
6eac8aec 272 dc->props = virtio_rng_properties;
b6075793 273 dc->vmsd = &vmstate_virtio_rng;
125ee0ed 274 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
a8d57dfb 275 vdc->realize = virtio_rng_device_realize;
306ec6c3 276 vdc->unrealize = virtio_rng_device_unrealize;
6eac8aec 277 vdc->get_features = get_features;
5d9c9ea2 278 vdc->set_status = virtio_rng_set_status;
6eac8aec
FK
279}
280
6eac8aec
FK
281static const TypeInfo virtio_rng_info = {
282 .name = TYPE_VIRTIO_RNG,
283 .parent = TYPE_VIRTIO_DEVICE,
284 .instance_size = sizeof(VirtIORNG),
6eac8aec
FK
285 .class_init = virtio_rng_class_init,
286};
287
288static void virtio_register_types(void)
289{
290 type_register_static(&virtio_rng_info);
291}
292
293type_init(virtio_register_types)