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