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