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