xref: /openbmc/qemu/hw/virtio/virtio-rng.c (revision 1b111dc1)
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 
12 #include "qemu/iov.h"
13 #include "hw/qdev.h"
14 #include "qapi/qmp/qerror.h"
15 #include "hw/virtio/virtio.h"
16 #include "hw/virtio/virtio-rng.h"
17 #include "sysemu/rng.h"
18 
19 static bool is_guest_ready(VirtIORNG *vrng)
20 {
21     VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
22     if (virtio_queue_ready(vrng->vq)
23         && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
24         return true;
25     }
26     return false;
27 }
28 
29 static size_t get_request_size(VirtQueue *vq, unsigned quota)
30 {
31     unsigned int in, out;
32 
33     virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
34     return in;
35 }
36 
37 static void virtio_rng_process(VirtIORNG *vrng);
38 
39 /* Send data from a char device over to the guest */
40 static void chr_read(void *opaque, const void *buf, size_t size)
41 {
42     VirtIORNG *vrng = opaque;
43     VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
44     VirtQueueElement elem;
45     size_t len;
46     int offset;
47 
48     if (!is_guest_ready(vrng)) {
49         return;
50     }
51 
52     vrng->quota_remaining -= size;
53 
54     offset = 0;
55     while (offset < size) {
56         if (!virtqueue_pop(vrng->vq, &elem)) {
57             break;
58         }
59         len = iov_from_buf(elem.in_sg, elem.in_num,
60                            0, buf + offset, size - offset);
61         offset += len;
62 
63         virtqueue_push(vrng->vq, &elem, len);
64     }
65     virtio_notify(vdev, vrng->vq);
66 }
67 
68 static void virtio_rng_process(VirtIORNG *vrng)
69 {
70     size_t size;
71     unsigned quota;
72 
73     if (!is_guest_ready(vrng)) {
74         return;
75     }
76 
77     if (vrng->quota_remaining < 0) {
78         quota = 0;
79     } else {
80         quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
81     }
82     size = get_request_size(vrng->vq, quota);
83     size = MIN(vrng->quota_remaining, size);
84     if (size) {
85         rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
86     }
87 }
88 
89 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
90 {
91     VirtIORNG *vrng = VIRTIO_RNG(vdev);
92     virtio_rng_process(vrng);
93 }
94 
95 static uint32_t get_features(VirtIODevice *vdev, uint32_t f)
96 {
97     return f;
98 }
99 
100 static void virtio_rng_save(QEMUFile *f, void *opaque)
101 {
102     VirtIODevice *vdev = opaque;
103 
104     virtio_save(vdev, f);
105 }
106 
107 static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id)
108 {
109     VirtIORNG *vrng = opaque;
110     VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
111 
112     if (version_id != 1) {
113         return -EINVAL;
114     }
115     virtio_load(vdev, f);
116 
117     /* We may have an element ready but couldn't process it due to a quota
118      * limit.  Make sure to try again after live migration when the quota may
119      * have been reset.
120      */
121     virtio_rng_process(vrng);
122 
123     return 0;
124 }
125 
126 static void check_rate_limit(void *opaque)
127 {
128     VirtIORNG *vrng = opaque;
129 
130     vrng->quota_remaining = vrng->conf.max_bytes;
131     virtio_rng_process(vrng);
132     timer_mod(vrng->rate_limit_timer,
133                    qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
134 }
135 
136 static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
137 {
138     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
139     VirtIORNG *vrng = VIRTIO_RNG(dev);
140     Error *local_err = NULL;
141 
142     if (!vrng->conf.period_ms > 0) {
143         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "period",
144                   "a positive number");
145         return;
146     }
147 
148     if (vrng->conf.rng == NULL) {
149         vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
150 
151         object_property_add_child(OBJECT(dev),
152                                   "default-backend",
153                                   OBJECT(vrng->conf.default_backend),
154                                   NULL);
155 
156         object_property_set_link(OBJECT(dev),
157                                  OBJECT(vrng->conf.default_backend),
158                                  "rng", NULL);
159     }
160 
161     virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0);
162 
163     vrng->rng = vrng->conf.rng;
164     if (vrng->rng == NULL) {
165         error_set(errp, QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
166         return;
167     }
168 
169     rng_backend_open(vrng->rng, &local_err);
170     if (local_err) {
171         error_propagate(errp, local_err);
172         return;
173     }
174 
175     vrng->vq = virtio_add_queue(vdev, 8, handle_input);
176 
177     assert(vrng->conf.max_bytes <= INT64_MAX);
178     vrng->quota_remaining = vrng->conf.max_bytes;
179 
180     vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
181                                                check_rate_limit, vrng);
182 
183     timer_mod(vrng->rate_limit_timer,
184                    qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
185 
186     register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
187                     virtio_rng_load, vrng);
188 }
189 
190 static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp)
191 {
192     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
193     VirtIORNG *vrng = VIRTIO_RNG(dev);
194 
195     timer_del(vrng->rate_limit_timer);
196     timer_free(vrng->rate_limit_timer);
197     unregister_savevm(dev, "virtio-rng", vrng);
198     virtio_cleanup(vdev);
199 }
200 
201 static Property virtio_rng_properties[] = {
202     DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORNG, conf),
203     DEFINE_PROP_END_OF_LIST(),
204 };
205 
206 static void virtio_rng_class_init(ObjectClass *klass, void *data)
207 {
208     DeviceClass *dc = DEVICE_CLASS(klass);
209     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
210 
211     dc->props = virtio_rng_properties;
212     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
213     vdc->realize = virtio_rng_device_realize;
214     vdc->unrealize = virtio_rng_device_unrealize;
215     vdc->get_features = get_features;
216 }
217 
218 static void virtio_rng_initfn(Object *obj)
219 {
220     VirtIORNG *vrng = VIRTIO_RNG(obj);
221 
222     object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
223                              (Object **)&vrng->conf.rng, NULL);
224 }
225 
226 static const TypeInfo virtio_rng_info = {
227     .name = TYPE_VIRTIO_RNG,
228     .parent = TYPE_VIRTIO_DEVICE,
229     .instance_size = sizeof(VirtIORNG),
230     .instance_init = virtio_rng_initfn,
231     .class_init = virtio_rng_class_init,
232 };
233 
234 static void virtio_register_types(void)
235 {
236     type_register_static(&virtio_rng_info);
237 }
238 
239 type_init(virtio_register_types)
240