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