]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/virtio-rng.c
Open 2.2 development tree
[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
1de7afc9 12#include "qemu/iov.h"
83c9f4ca 13#include "hw/qdev.h"
b4a42f81 14#include "qapi/qmp/qerror.h"
0d09e41a
PB
15#include "hw/virtio/virtio.h"
16#include "hw/virtio/virtio-rng.h"
dccfcd0e 17#include "sysemu/rng.h"
57d3e1b3 18#include "qom/object_interfaces.h"
16c915ba 19
16c915ba
AS
20static bool is_guest_ready(VirtIORNG *vrng)
21{
611aa333 22 VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
16c915ba 23 if (virtio_queue_ready(vrng->vq)
611aa333 24 && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
16c915ba
AS
25 return true;
26 }
27 return false;
28}
29
e1f7b481 30static size_t get_request_size(VirtQueue *vq, unsigned quota)
16c915ba 31{
14417039 32 unsigned int in, out;
16c915ba 33
e1f7b481 34 virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
14417039 35 return in;
16c915ba
AS
36}
37
904d6f58
AL
38static void virtio_rng_process(VirtIORNG *vrng);
39
16c915ba
AS
40/* Send data from a char device over to the guest */
41static void chr_read(void *opaque, const void *buf, size_t size)
42{
43 VirtIORNG *vrng = opaque;
611aa333 44 VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
14417039 45 VirtQueueElement elem;
16c915ba
AS
46 size_t len;
47 int offset;
48
49 if (!is_guest_ready(vrng)) {
50 return;
51 }
52
904d6f58
AL
53 vrng->quota_remaining -= size;
54
16c915ba
AS
55 offset = 0;
56 while (offset < size) {
14417039 57 if (!virtqueue_pop(vrng->vq, &elem)) {
16c915ba
AS
58 break;
59 }
14417039 60 len = iov_from_buf(elem.in_sg, elem.in_num,
16c915ba
AS
61 0, buf + offset, size - offset);
62 offset += len;
63
14417039 64 virtqueue_push(vrng->vq, &elem, len);
16c915ba 65 }
611aa333 66 virtio_notify(vdev, vrng->vq);
16c915ba
AS
67}
68
904d6f58 69static void virtio_rng_process(VirtIORNG *vrng)
16c915ba 70{
14417039 71 size_t size;
e1f7b481 72 unsigned quota;
904d6f58
AL
73
74 if (!is_guest_ready(vrng)) {
75 return;
76 }
16c915ba 77
e1f7b481
MT
78 if (vrng->quota_remaining < 0) {
79 quota = 0;
80 } else {
81 quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
82 }
83 size = get_request_size(vrng->vq, quota);
904d6f58 84 size = MIN(vrng->quota_remaining, size);
14417039 85 if (size) {
16c915ba
AS
86 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
87 }
88}
89
904d6f58
AL
90static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
91{
611aa333 92 VirtIORNG *vrng = VIRTIO_RNG(vdev);
904d6f58
AL
93 virtio_rng_process(vrng);
94}
95
16c915ba
AS
96static uint32_t get_features(VirtIODevice *vdev, uint32_t f)
97{
98 return f;
99}
100
101static void virtio_rng_save(QEMUFile *f, void *opaque)
102{
611aa333 103 VirtIODevice *vdev = opaque;
16c915ba 104
611aa333 105 virtio_save(vdev, f);
16c915ba
AS
106}
107
108static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id)
109{
16c915ba
AS
110 if (version_id != 1) {
111 return -EINVAL;
112 }
3902d49e
GK
113 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
114}
16c915ba 115
3902d49e
GK
116static int virtio_rng_load_device(VirtIODevice *vdev, QEMUFile *f,
117 int version_id)
118{
904d6f58 119 /* We may have an element ready but couldn't process it due to a quota
42015c9a
AS
120 * limit. Make sure to try again after live migration when the quota may
121 * have been reset.
122 */
3902d49e 123 virtio_rng_process(VIRTIO_RNG(vdev));
904d6f58 124
16c915ba
AS
125 return 0;
126}
127
904d6f58
AL
128static void check_rate_limit(void *opaque)
129{
611aa333 130 VirtIORNG *vrng = opaque;
904d6f58 131
611aa333
FK
132 vrng->quota_remaining = vrng->conf.max_bytes;
133 virtio_rng_process(vrng);
bc72ad67
AB
134 timer_mod(vrng->rate_limit_timer,
135 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
904d6f58
AL
136}
137
a8d57dfb 138static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
16c915ba 139{
a8d57dfb 140 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
af7671fd 141 VirtIORNG *vrng = VIRTIO_RNG(dev);
16c915ba
AS
142 Error *local_err = NULL;
143
d44bb860 144 if (!vrng->conf.period_ms > 0) {
a8d57dfb
AF
145 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "period",
146 "a positive number");
147 return;
d44bb860
AK
148 }
149
46a5a89d
FK
150 if (vrng->conf.rng == NULL) {
151 vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
152
57d3e1b3
IM
153 user_creatable_complete(OBJECT(vrng->conf.default_backend),
154 &local_err);
155 if (local_err) {
156 error_propagate(errp, local_err);
157 object_unref(OBJECT(vrng->conf.default_backend));
158 return;
159 }
160
af7671fd 161 object_property_add_child(OBJECT(dev),
46a5a89d
FK
162 "default-backend",
163 OBJECT(vrng->conf.default_backend),
164 NULL);
165
abdffd1f
SH
166 /* The child property took a reference, we can safely drop ours now */
167 object_unref(OBJECT(vrng->conf.default_backend));
168
af7671fd 169 object_property_set_link(OBJECT(dev),
46a5a89d
FK
170 OBJECT(vrng->conf.default_backend),
171 "rng", NULL);
6eac8aec 172 }
16c915ba 173
46a5a89d
FK
174 virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0);
175
176 vrng->rng = vrng->conf.rng;
16c915ba 177 if (vrng->rng == NULL) {
a8d57dfb
AF
178 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
179 return;
16c915ba
AS
180 }
181
16c915ba 182 vrng->vq = virtio_add_queue(vdev, 8, handle_input);
6eac8aec 183
713e8a10
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) {
187 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "max-bytes",
188 "a non-negative integer below 2^63");
189 return;
190 }
af1a8ad6 191 vrng->quota_remaining = vrng->conf.max_bytes;
904d6f58 192
bc72ad67 193 vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
904d6f58
AL
194 check_rate_limit, vrng);
195
bc72ad67
AB
196 timer_mod(vrng->rate_limit_timer,
197 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
904d6f58 198
af7671fd 199 register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
16c915ba 200 virtio_rng_load, vrng);
6eac8aec
FK
201}
202
306ec6c3 203static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp)
6eac8aec 204{
306ec6c3
AF
205 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
206 VirtIORNG *vrng = VIRTIO_RNG(dev);
6eac8aec 207
bc72ad67
AB
208 timer_del(vrng->rate_limit_timer);
209 timer_free(vrng->rate_limit_timer);
306ec6c3 210 unregister_savevm(dev, "virtio-rng", vrng);
6a1a8cc7 211 virtio_cleanup(vdev);
6eac8aec
FK
212}
213
214static Property virtio_rng_properties[] = {
215 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORNG, conf),
216 DEFINE_PROP_END_OF_LIST(),
217};
218
219static void virtio_rng_class_init(ObjectClass *klass, void *data)
220{
221 DeviceClass *dc = DEVICE_CLASS(klass);
222 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
a8d57dfb 223
6eac8aec 224 dc->props = virtio_rng_properties;
125ee0ed 225 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
a8d57dfb 226 vdc->realize = virtio_rng_device_realize;
306ec6c3 227 vdc->unrealize = virtio_rng_device_unrealize;
6eac8aec 228 vdc->get_features = get_features;
3902d49e 229 vdc->load = virtio_rng_load_device;
6eac8aec
FK
230}
231
232static void virtio_rng_initfn(Object *obj)
233{
234 VirtIORNG *vrng = VIRTIO_RNG(obj);
235
236 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
9561fda8 237 (Object **)&vrng->conf.rng,
39f72ef9 238 qdev_prop_allow_set_link_before_realize,
9561fda8 239 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
6eac8aec
FK
240}
241
242static const TypeInfo virtio_rng_info = {
243 .name = TYPE_VIRTIO_RNG,
244 .parent = TYPE_VIRTIO_DEVICE,
245 .instance_size = sizeof(VirtIORNG),
246 .instance_init = virtio_rng_initfn,
247 .class_init = virtio_rng_class_init,
248};
249
250static void virtio_register_types(void)
251{
252 type_register_static(&virtio_rng_info);
253}
254
255type_init(virtio_register_types)