]> git.proxmox.com Git - qemu.git/blame - hw/virtio/virtio-rng.c
sysemu: avoid proliferation of include/ subdirectories
[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 <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"
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"
16c915ba 18
16c915ba
AS
19static bool is_guest_ready(VirtIORNG *vrng)
20{
21 if (virtio_queue_ready(vrng->vq)
22 && (vrng->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
23 return true;
24 }
25 return false;
26}
27
e1f7b481 28static size_t get_request_size(VirtQueue *vq, unsigned quota)
16c915ba 29{
14417039 30 unsigned int in, out;
16c915ba 31
e1f7b481 32 virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
14417039 33 return in;
16c915ba
AS
34}
35
904d6f58
AL
36static void virtio_rng_process(VirtIORNG *vrng);
37
16c915ba
AS
38/* Send data from a char device over to the guest */
39static void chr_read(void *opaque, const void *buf, size_t size)
40{
41 VirtIORNG *vrng = opaque;
14417039 42 VirtQueueElement elem;
16c915ba
AS
43 size_t len;
44 int offset;
45
46 if (!is_guest_ready(vrng)) {
47 return;
48 }
49
904d6f58
AL
50 vrng->quota_remaining -= size;
51
16c915ba
AS
52 offset = 0;
53 while (offset < size) {
14417039 54 if (!virtqueue_pop(vrng->vq, &elem)) {
16c915ba
AS
55 break;
56 }
14417039 57 len = iov_from_buf(elem.in_sg, elem.in_num,
16c915ba
AS
58 0, buf + offset, size - offset);
59 offset += len;
60
14417039 61 virtqueue_push(vrng->vq, &elem, len);
16c915ba
AS
62 }
63 virtio_notify(&vrng->vdev, vrng->vq);
16c915ba
AS
64}
65
904d6f58 66static void virtio_rng_process(VirtIORNG *vrng)
16c915ba 67{
14417039 68 size_t size;
e1f7b481 69 unsigned quota;
904d6f58
AL
70
71 if (!is_guest_ready(vrng)) {
72 return;
73 }
16c915ba 74
e1f7b481
MT
75 if (vrng->quota_remaining < 0) {
76 quota = 0;
77 } else {
78 quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
79 }
80 size = get_request_size(vrng->vq, quota);
904d6f58 81 size = MIN(vrng->quota_remaining, size);
14417039 82 if (size) {
16c915ba
AS
83 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
84 }
85}
86
904d6f58
AL
87static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
88{
89 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
90 virtio_rng_process(vrng);
91}
92
16c915ba
AS
93static uint32_t get_features(VirtIODevice *vdev, uint32_t f)
94{
95 return f;
96}
97
98static void virtio_rng_save(QEMUFile *f, void *opaque)
99{
100 VirtIORNG *vrng = opaque;
101
102 virtio_save(&vrng->vdev, f);
16c915ba
AS
103}
104
105static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id)
106{
107 VirtIORNG *vrng = opaque;
108
109 if (version_id != 1) {
110 return -EINVAL;
111 }
112 virtio_load(&vrng->vdev, f);
113
904d6f58 114 /* We may have an element ready but couldn't process it due to a quota
42015c9a
AS
115 * limit. Make sure to try again after live migration when the quota may
116 * have been reset.
117 */
904d6f58
AL
118 virtio_rng_process(vrng);
119
16c915ba
AS
120 return 0;
121}
122
904d6f58
AL
123static void check_rate_limit(void *opaque)
124{
125 VirtIORNG *s = opaque;
126
127 s->quota_remaining = s->conf->max_bytes;
128 virtio_rng_process(s);
129 qemu_mod_timer(s->rate_limit_timer,
130 qemu_get_clock_ms(vm_clock) + s->conf->period_ms);
131}
132
133
16c915ba
AS
134VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf)
135{
136 VirtIORNG *vrng;
137 VirtIODevice *vdev;
138 Error *local_err = NULL;
139
140 vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0,
141 sizeof(VirtIORNG));
142
143 vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
144
145 vrng->rng = conf->rng;
146 if (vrng->rng == NULL) {
147 qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
148 return NULL;
149 }
150
151 rng_backend_open(vrng->rng, &local_err);
152 if (local_err) {
153 qerror_report_err(local_err);
154 error_free(local_err);
155 return NULL;
156 }
157
158 vrng->vq = virtio_add_queue(vdev, 8, handle_input);
159 vrng->vdev.get_features = get_features;
160
161 vrng->qdev = dev;
162 vrng->conf = conf;
14417039 163
03a36f17 164 assert(vrng->conf->max_bytes <= INT64_MAX);
904d6f58
AL
165 vrng->quota_remaining = vrng->conf->max_bytes;
166
904d6f58
AL
167 vrng->rate_limit_timer = qemu_new_timer_ms(vm_clock,
168 check_rate_limit, vrng);
169
170 qemu_mod_timer(vrng->rate_limit_timer,
171 qemu_get_clock_ms(vm_clock) + vrng->conf->period_ms);
172
16c915ba
AS
173 register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
174 virtio_rng_load, vrng);
175
176 return vdev;
177}
178
179void virtio_rng_exit(VirtIODevice *vdev)
180{
181 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
182
8cc67743
AS
183 qemu_del_timer(vrng->rate_limit_timer);
184 qemu_free_timer(vrng->rate_limit_timer);
16c915ba
AS
185 unregister_savevm(vrng->qdev, "virtio-rng", vrng);
186 virtio_cleanup(vdev);
187}