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