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