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