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