Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 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 | |
Peter Maydell | 9b8bfe2 | 2016-01-26 18:17:07 +0000 | [diff] [blame] | 12 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 13 | #include "qapi/error.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 14 | #include "qemu/iov.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 15 | #include "qemu/module.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 16 | #include "qemu/timer.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 17 | #include "hw/virtio/virtio.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 18 | #include "hw/qdev-properties.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 19 | #include "hw/virtio/virtio-rng.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 20 | #include "sysemu/rng.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 21 | #include "sysemu/runstate.h" |
Igor Mammedov | 57d3e1b | 2014-01-16 17:34:39 +0100 | [diff] [blame] | 22 | #include "qom/object_interfaces.h" |
Amit Shah | 4ac4458 | 2014-08-04 16:22:44 +0530 | [diff] [blame] | 23 | #include "trace.h" |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 24 | |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 25 | static bool is_guest_ready(VirtIORNG *vrng) |
| 26 | { |
KONRAD Frederic | 611aa33 | 2013-04-24 10:08:01 +0200 | [diff] [blame] | 27 | VirtIODevice *vdev = VIRTIO_DEVICE(vrng); |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 28 | if (virtio_queue_ready(vrng->vq) |
KONRAD Frederic | 611aa33 | 2013-04-24 10:08:01 +0200 | [diff] [blame] | 29 | && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 30 | return true; |
| 31 | } |
Amit Shah | 4ac4458 | 2014-08-04 16:22:44 +0530 | [diff] [blame] | 32 | trace_virtio_rng_guest_not_ready(vrng); |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 33 | return false; |
| 34 | } |
| 35 | |
Michael S. Tsirkin | e1f7b48 | 2012-11-30 00:02:56 +0200 | [diff] [blame] | 36 | static size_t get_request_size(VirtQueue *vq, unsigned quota) |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 37 | { |
Amit Shah | 1441703 | 2012-11-21 11:21:18 +0530 | [diff] [blame] | 38 | unsigned int in, out; |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 39 | |
Michael S. Tsirkin | e1f7b48 | 2012-11-30 00:02:56 +0200 | [diff] [blame] | 40 | virtqueue_get_avail_bytes(vq, &in, &out, quota, 0); |
Amit Shah | 1441703 | 2012-11-21 11:21:18 +0530 | [diff] [blame] | 41 | return in; |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 42 | } |
| 43 | |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 44 | static void virtio_rng_process(VirtIORNG *vrng); |
| 45 | |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 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; |
KONRAD Frederic | 611aa33 | 2013-04-24 10:08:01 +0200 | [diff] [blame] | 50 | VirtIODevice *vdev = VIRTIO_DEVICE(vrng); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 51 | VirtQueueElement *elem; |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 52 | size_t len; |
| 53 | int offset; |
| 54 | |
| 55 | if (!is_guest_ready(vrng)) { |
| 56 | return; |
| 57 | } |
| 58 | |
Laurent Vivier | a23a6d1 | 2017-04-12 15:53:12 +0200 | [diff] [blame] | 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 | |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 68 | vrng->quota_remaining -= size; |
| 69 | |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 70 | offset = 0; |
| 71 | while (offset < size) { |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 72 | elem = virtqueue_pop(vrng->vq, sizeof(VirtQueueElement)); |
| 73 | if (!elem) { |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 74 | break; |
| 75 | } |
Laurent Vivier | a23a6d1 | 2017-04-12 15:53:12 +0200 | [diff] [blame] | 76 | trace_virtio_rng_popped(vrng); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 77 | len = iov_from_buf(elem->in_sg, elem->in_num, |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 78 | 0, buf + offset, size - offset); |
| 79 | offset += len; |
| 80 | |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 81 | virtqueue_push(vrng->vq, elem, len); |
Amit Shah | 4ac4458 | 2014-08-04 16:22:44 +0530 | [diff] [blame] | 82 | trace_virtio_rng_pushed(vrng, len); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 83 | g_free(elem); |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 84 | } |
KONRAD Frederic | 611aa33 | 2013-04-24 10:08:01 +0200 | [diff] [blame] | 85 | virtio_notify(vdev, vrng->vq); |
Ladi Prosek | f8693c2 | 2016-03-03 10:48:34 +0100 | [diff] [blame] | 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 | } |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void virtio_rng_process(VirtIORNG *vrng) |
| 96 | { |
Amit Shah | 1441703 | 2012-11-21 11:21:18 +0530 | [diff] [blame] | 97 | size_t size; |
Michael S. Tsirkin | e1f7b48 | 2012-11-30 00:02:56 +0200 | [diff] [blame] | 98 | unsigned quota; |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 99 | |
| 100 | if (!is_guest_ready(vrng)) { |
| 101 | return; |
| 102 | } |
| 103 | |
Pankaj Gupta | 621a20e | 2015-07-15 17:46:47 +0530 | [diff] [blame] | 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 | |
Michael S. Tsirkin | e1f7b48 | 2012-11-30 00:02:56 +0200 | [diff] [blame] | 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); |
Amit Shah | 4ac4458 | 2014-08-04 16:22:44 +0530 | [diff] [blame] | 116 | |
| 117 | trace_virtio_rng_request(vrng, size, quota); |
| 118 | |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 119 | size = MIN(vrng->quota_remaining, size); |
Amit Shah | 1441703 | 2012-11-21 11:21:18 +0530 | [diff] [blame] | 120 | if (size) { |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 121 | rng_backend_request_entropy(vrng->rng, size, chr_read, vrng); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void handle_input(VirtIODevice *vdev, VirtQueue *vq) |
| 126 | { |
KONRAD Frederic | 611aa33 | 2013-04-24 10:08:01 +0200 | [diff] [blame] | 127 | VirtIORNG *vrng = VIRTIO_RNG(vdev); |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 128 | virtio_rng_process(vrng); |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 129 | } |
| 130 | |
Jason Wang | 9d5b731 | 2015-07-27 17:49:19 +0800 | [diff] [blame] | 131 | static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp) |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 132 | { |
| 133 | return f; |
| 134 | } |
| 135 | |
Philippe Mathieu-Daudé | 538f049 | 2021-01-11 16:20:20 +0100 | [diff] [blame] | 136 | static void virtio_rng_vm_state_change(void *opaque, bool running, |
Laurent Vivier | a23a6d1 | 2017-04-12 15:53:12 +0200 | [diff] [blame] | 137 | RunState state) |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 138 | { |
David Gibson | db12451 | 2014-11-27 16:48:10 +1100 | [diff] [blame] | 139 | VirtIORNG *vrng = opaque; |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 140 | |
Laurent Vivier | a23a6d1 | 2017-04-12 15:53:12 +0200 | [diff] [blame] | 141 | trace_virtio_rng_vm_state_change(vrng, running, state); |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 142 | |
Laurent Vivier | a23a6d1 | 2017-04-12 15:53:12 +0200 | [diff] [blame] | 143 | /* We may have an element ready but couldn't process it due to a quota |
| 144 | * limit or because CPU was stopped. Make sure to try again when the |
| 145 | * CPU restart. |
| 146 | */ |
| 147 | |
| 148 | if (running && is_guest_ready(vrng)) { |
| 149 | virtio_rng_process(vrng); |
| 150 | } |
Amit Shah | 16c915b | 2012-06-20 12:29:32 +0530 | [diff] [blame] | 151 | } |
| 152 | |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 153 | static void check_rate_limit(void *opaque) |
| 154 | { |
KONRAD Frederic | 611aa33 | 2013-04-24 10:08:01 +0200 | [diff] [blame] | 155 | VirtIORNG *vrng = opaque; |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 156 | |
KONRAD Frederic | 611aa33 | 2013-04-24 10:08:01 +0200 | [diff] [blame] | 157 | vrng->quota_remaining = vrng->conf.max_bytes; |
| 158 | virtio_rng_process(vrng); |
Pankaj Gupta | 621a20e | 2015-07-15 17:46:47 +0530 | [diff] [blame] | 159 | vrng->activate_timer = true; |
Anthony Liguori | 904d6f5 | 2012-10-30 17:45:05 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Pankaj Gupta | 5d9c9ea | 2018-06-27 16:55:20 +0530 | [diff] [blame] | 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 | |
Andreas Färber | a8d57df | 2013-07-30 02:57:37 +0200 | [diff] [blame] | 175 | static void virtio_rng_device_realize(DeviceState *dev, Error **errp) |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 176 | { |
Andreas Färber | a8d57df | 2013-07-30 02:57:37 +0200 | [diff] [blame] | 177 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
Andreas Färber | af7671f | 2013-07-30 05:12:47 +0200 | [diff] [blame] | 178 | VirtIORNG *vrng = VIRTIO_RNG(dev); |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 179 | |
Amit Shah | a3a292c | 2014-12-11 13:17:42 +0530 | [diff] [blame] | 180 | if (vrng->conf.period_ms <= 0) { |
John Snow | c617dd3 | 2014-07-29 19:28:58 -0400 | [diff] [blame] | 181 | error_setg(errp, "'period' parameter expects a positive integer"); |
Andreas Färber | a8d57df | 2013-07-30 02:57:37 +0200 | [diff] [blame] | 182 | return; |
Amos Kong | d44bb86 | 2013-11-21 18:53:23 +0800 | [diff] [blame] | 183 | } |
| 184 | |
John Snow | 1efd6e0 | 2014-07-29 19:28:57 -0400 | [diff] [blame] | 185 | /* Workaround: Property parsing does not enforce unsigned integers, |
| 186 | * So this is a hack to reject such numbers. */ |
Michael S. Tsirkin | 024d046 | 2024-07-24 06:48:59 -0400 | [diff] [blame] | 187 | if (vrng->conf.max_bytes == 0 || |
| 188 | vrng->conf.max_bytes > INT64_MAX) { |
| 189 | error_setg(errp, "'max-bytes' parameter must be positive, " |
John Snow | c617dd3 | 2014-07-29 19:28:58 -0400 | [diff] [blame] | 190 | "and less than 2^63"); |
John Snow | 1efd6e0 | 2014-07-29 19:28:57 -0400 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 194 | if (vrng->conf.rng == NULL) { |
Laurent Vivier | 0198c26 | 2019-08-20 18:06:15 +0200 | [diff] [blame] | 195 | Object *default_backend = object_new(TYPE_RNG_BUILTIN); |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 196 | |
Markus Armbruster | 778a2dc | 2020-07-07 18:05:56 +0200 | [diff] [blame] | 197 | if (!user_creatable_complete(USER_CREATABLE(default_backend), |
Markus Armbruster | 668f62e | 2020-07-07 18:06:02 +0200 | [diff] [blame] | 198 | errp)) { |
Markus Armbruster | 5f7655f | 2019-08-20 18:06:14 +0200 | [diff] [blame] | 199 | object_unref(default_backend); |
Igor Mammedov | 57d3e1b | 2014-01-16 17:34:39 +0100 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
Markus Armbruster | 5f7655f | 2019-08-20 18:06:14 +0200 | [diff] [blame] | 203 | object_property_add_child(OBJECT(dev), "default-backend", |
Markus Armbruster | d262312 | 2020-05-05 17:29:22 +0200 | [diff] [blame] | 204 | default_backend); |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 205 | |
Stefan Hajnoczi | abdffd1 | 2014-03-19 08:58:57 +0100 | [diff] [blame] | 206 | /* The child property took a reference, we can safely drop ours now */ |
Markus Armbruster | 5f7655f | 2019-08-20 18:06:14 +0200 | [diff] [blame] | 207 | object_unref(default_backend); |
Stefan Hajnoczi | abdffd1 | 2014-03-19 08:58:57 +0100 | [diff] [blame] | 208 | |
Markus Armbruster | 5325cc3 | 2020-07-07 18:05:54 +0200 | [diff] [blame] | 209 | object_property_set_link(OBJECT(dev), "rng", default_backend, |
| 210 | &error_abort); |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 211 | } |
| 212 | |
KONRAD Frederic | 46a5a89 | 2013-04-24 10:07:59 +0200 | [diff] [blame] | 213 | vrng->rng = vrng->conf.rng; |
| 214 | if (vrng->rng == NULL) { |
John Snow | c617dd3 | 2014-07-29 19:28:58 -0400 | [diff] [blame] | 215 | error_setg(errp, "'rng' parameter expects a valid object"); |
Andreas Färber | a8d57df | 2013-07-30 02:57:37 +0200 | [diff] [blame] | 216 | return; |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 217 | } |
KONRAD Frederic | 46a5a89 | 2013-04-24 10:07:59 +0200 | [diff] [blame] | 218 | |
Jonah Palmer | 3857cd5 | 2022-04-01 09:23:18 -0400 | [diff] [blame] | 219 | virtio_init(vdev, VIRTIO_ID_RNG, 0); |
KONRAD Frederic | 46a5a89 | 2013-04-24 10:07:59 +0200 | [diff] [blame] | 220 | |
John Snow | 1efd6e0 | 2014-07-29 19:28:57 -0400 | [diff] [blame] | 221 | vrng->vq = virtio_add_queue(vdev, 8, handle_input); |
KONRAD Frederic | 46a5a89 | 2013-04-24 10:07:59 +0200 | [diff] [blame] | 222 | vrng->quota_remaining = vrng->conf.max_bytes; |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 223 | vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, |
KONRAD Frederic | 46a5a89 | 2013-04-24 10:07:59 +0200 | [diff] [blame] | 224 | check_rate_limit, vrng); |
Pankaj Gupta | 621a20e | 2015-07-15 17:46:47 +0530 | [diff] [blame] | 225 | vrng->activate_timer = true; |
Laurent Vivier | a23a6d1 | 2017-04-12 15:53:12 +0200 | [diff] [blame] | 226 | |
| 227 | vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change, |
| 228 | vrng); |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 231 | static void virtio_rng_device_unrealize(DeviceState *dev) |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 232 | { |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 233 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 234 | VirtIORNG *vrng = VIRTIO_RNG(dev); |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 235 | |
Laurent Vivier | a23a6d1 | 2017-04-12 15:53:12 +0200 | [diff] [blame] | 236 | qemu_del_vm_change_state_handler(vrng->vmstate); |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 237 | timer_free(vrng->rate_limit_timer); |
Eugenio Pérez | 522bbb1 | 2019-10-25 10:35:23 +0200 | [diff] [blame] | 238 | virtio_del_queue(vdev, 0); |
KONRAD Frederic | 6a1a8cc | 2013-04-24 10:21:22 +0200 | [diff] [blame] | 239 | virtio_cleanup(vdev); |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Halil Pasic | b7de81f | 2016-10-06 14:55:48 +0200 | [diff] [blame] | 242 | static const VMStateDescription vmstate_virtio_rng = { |
| 243 | .name = "virtio-rng", |
| 244 | .minimum_version_id = 1, |
| 245 | .version_id = 1, |
Richard Henderson | ca02a17 | 2023-12-21 14:16:41 +1100 | [diff] [blame] | 246 | .fields = (const VMStateField[]) { |
Halil Pasic | b7de81f | 2016-10-06 14:55:48 +0200 | [diff] [blame] | 247 | VMSTATE_VIRTIO_DEVICE, |
| 248 | VMSTATE_END_OF_LIST() |
| 249 | }, |
Halil Pasic | b7de81f | 2016-10-06 14:55:48 +0200 | [diff] [blame] | 250 | }; |
Dr. David Alan Gilbert | b607579 | 2016-07-14 18:22:48 +0100 | [diff] [blame] | 251 | |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 252 | static Property virtio_rng_properties[] = { |
Shannon Zhao | fe70480 | 2015-06-10 23:04:33 +0800 | [diff] [blame] | 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), |
Fam Zheng | d1fd7f7 | 2017-07-14 10:14:57 +0800 | [diff] [blame] | 260 | DEFINE_PROP_LINK("rng", VirtIORNG, conf.rng, TYPE_RNG_BACKEND, RngBackend *), |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 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); |
Andreas Färber | a8d57df | 2013-07-30 02:57:37 +0200 | [diff] [blame] | 268 | |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 269 | device_class_set_props(dc, virtio_rng_properties); |
Dr. David Alan Gilbert | b607579 | 2016-07-14 18:22:48 +0100 | [diff] [blame] | 270 | dc->vmsd = &vmstate_virtio_rng; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 271 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
Andreas Färber | a8d57df | 2013-07-30 02:57:37 +0200 | [diff] [blame] | 272 | vdc->realize = virtio_rng_device_realize; |
Andreas Färber | 306ec6c | 2013-07-30 03:50:44 +0200 | [diff] [blame] | 273 | vdc->unrealize = virtio_rng_device_unrealize; |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 274 | vdc->get_features = get_features; |
Pankaj Gupta | 5d9c9ea | 2018-06-27 16:55:20 +0530 | [diff] [blame] | 275 | vdc->set_status = virtio_rng_set_status; |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 276 | } |
| 277 | |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 278 | static const TypeInfo virtio_rng_info = { |
| 279 | .name = TYPE_VIRTIO_RNG, |
| 280 | .parent = TYPE_VIRTIO_DEVICE, |
| 281 | .instance_size = sizeof(VirtIORNG), |
KONRAD Frederic | 6eac8ae | 2013-04-24 10:07:55 +0200 | [diff] [blame] | 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) |