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 if (virtio_queue_ready(vrng->vq) 22 && (vrng->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) { 23 return true; 24 } 25 return false; 26 } 27 28 static size_t get_request_size(VirtQueue *vq, unsigned quota) 29 { 30 unsigned int in, out; 31 32 virtqueue_get_avail_bytes(vq, &in, &out, quota, 0); 33 return in; 34 } 35 36 static void virtio_rng_process(VirtIORNG *vrng); 37 38 /* Send data from a char device over to the guest */ 39 static void chr_read(void *opaque, const void *buf, size_t size) 40 { 41 VirtIORNG *vrng = opaque; 42 VirtQueueElement elem; 43 size_t len; 44 int offset; 45 46 if (!is_guest_ready(vrng)) { 47 return; 48 } 49 50 vrng->quota_remaining -= size; 51 52 offset = 0; 53 while (offset < size) { 54 if (!virtqueue_pop(vrng->vq, &elem)) { 55 break; 56 } 57 len = iov_from_buf(elem.in_sg, elem.in_num, 58 0, buf + offset, size - offset); 59 offset += len; 60 61 virtqueue_push(vrng->vq, &elem, len); 62 } 63 virtio_notify(&vrng->vdev, vrng->vq); 64 } 65 66 static void virtio_rng_process(VirtIORNG *vrng) 67 { 68 size_t size; 69 unsigned quota; 70 71 if (!is_guest_ready(vrng)) { 72 return; 73 } 74 75 if (vrng->quota_remaining < 0) { 76 quota = 0; 77 } else { 78 quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX); 79 } 80 size = get_request_size(vrng->vq, quota); 81 size = MIN(vrng->quota_remaining, size); 82 if (size) { 83 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng); 84 } 85 } 86 87 static void handle_input(VirtIODevice *vdev, VirtQueue *vq) 88 { 89 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev); 90 virtio_rng_process(vrng); 91 } 92 93 static uint32_t get_features(VirtIODevice *vdev, uint32_t f) 94 { 95 return f; 96 } 97 98 static void virtio_rng_save(QEMUFile *f, void *opaque) 99 { 100 VirtIORNG *vrng = opaque; 101 102 virtio_save(&vrng->vdev, f); 103 } 104 105 static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id) 106 { 107 VirtIORNG *vrng = opaque; 108 109 if (version_id != 1) { 110 return -EINVAL; 111 } 112 virtio_load(&vrng->vdev, f); 113 114 /* We may have an element ready but couldn't process it due to a quota 115 * limit. Make sure to try again after live migration when the quota may 116 * have been reset. 117 */ 118 virtio_rng_process(vrng); 119 120 return 0; 121 } 122 123 static void check_rate_limit(void *opaque) 124 { 125 VirtIORNG *s = opaque; 126 127 s->quota_remaining = s->conf.max_bytes; 128 virtio_rng_process(s); 129 qemu_mod_timer(s->rate_limit_timer, 130 qemu_get_clock_ms(vm_clock) + s->conf.period_ms); 131 } 132 133 static VirtIODevice *virtio_rng_common_init(DeviceState *dev, 134 VirtIORNGConf *conf, 135 VirtIORNG **pvrng) 136 { 137 VirtIORNG *vrng = *pvrng; 138 VirtIODevice *vdev; 139 Error *local_err = NULL; 140 141 /* 142 * We have two cases here: the old virtio-rng-x device, and the 143 * refactored virtio-rng. 144 * This will disappear later in the serie. 145 */ 146 if (vrng == NULL) { 147 vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0, 148 sizeof(VirtIORNG)); 149 vrng = DO_UPCAST(VirtIORNG, vdev, vdev); 150 } else { 151 vdev = VIRTIO_DEVICE(vrng); 152 virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0); 153 } 154 155 vrng->rng = conf->rng; 156 if (vrng->rng == NULL) { 157 qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object"); 158 return NULL; 159 } 160 161 rng_backend_open(vrng->rng, &local_err); 162 if (local_err) { 163 qerror_report_err(local_err); 164 error_free(local_err); 165 return NULL; 166 } 167 168 vrng->vq = virtio_add_queue(vdev, 8, handle_input); 169 170 vrng->vdev.get_features = get_features; 171 172 vrng->qdev = dev; 173 memcpy(&(vrng->conf), conf, sizeof(struct VirtIORNGConf)); 174 175 assert(vrng->conf.max_bytes <= INT64_MAX); 176 vrng->quota_remaining = vrng->conf.max_bytes; 177 178 vrng->rate_limit_timer = qemu_new_timer_ms(vm_clock, 179 check_rate_limit, vrng); 180 181 qemu_mod_timer(vrng->rate_limit_timer, 182 qemu_get_clock_ms(vm_clock) + vrng->conf.period_ms); 183 184 register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save, 185 virtio_rng_load, vrng); 186 187 return vdev; 188 } 189 190 /* 191 * This two functions will be removed later in the serie. 192 */ 193 VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf) 194 { 195 VirtIORNG *vdev = NULL; 196 return virtio_rng_common_init(dev, conf, &vdev); 197 } 198 199 void virtio_rng_exit(VirtIODevice *vdev) 200 { 201 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev); 202 203 qemu_del_timer(vrng->rate_limit_timer); 204 qemu_free_timer(vrng->rate_limit_timer); 205 unregister_savevm(vrng->qdev, "virtio-rng", vrng); 206 virtio_cleanup(vdev); 207 } 208 209 static int virtio_rng_device_init(VirtIODevice *vdev) 210 { 211 DeviceState *qdev = DEVICE(vdev); 212 VirtIORNG *vrng = VIRTIO_RNG(vdev); 213 214 if (vrng->conf.rng == NULL) { 215 vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM)); 216 217 object_property_add_child(OBJECT(qdev), 218 "default-backend", 219 OBJECT(vrng->conf.default_backend), 220 NULL); 221 222 object_property_set_link(OBJECT(qdev), 223 OBJECT(vrng->conf.default_backend), 224 "rng", NULL); 225 } 226 227 if (virtio_rng_common_init(qdev, &(vrng->conf), &vrng) == NULL) { 228 return -1; 229 } 230 return 0; 231 } 232 233 static int virtio_rng_device_exit(DeviceState *qdev) 234 { 235 VirtIORNG *vrng = VIRTIO_RNG(qdev); 236 VirtIODevice *vdev = VIRTIO_DEVICE(qdev); 237 238 qemu_del_timer(vrng->rate_limit_timer); 239 qemu_free_timer(vrng->rate_limit_timer); 240 unregister_savevm(qdev, "virtio-rng", vrng); 241 virtio_common_cleanup(vdev); 242 return 0; 243 } 244 245 static Property virtio_rng_properties[] = { 246 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORNG, conf), 247 DEFINE_PROP_END_OF_LIST(), 248 }; 249 250 static void virtio_rng_class_init(ObjectClass *klass, void *data) 251 { 252 DeviceClass *dc = DEVICE_CLASS(klass); 253 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 254 dc->exit = virtio_rng_device_exit; 255 dc->props = virtio_rng_properties; 256 vdc->init = virtio_rng_device_init; 257 vdc->get_features = get_features; 258 } 259 260 static void virtio_rng_initfn(Object *obj) 261 { 262 VirtIORNG *vrng = VIRTIO_RNG(obj); 263 264 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND, 265 (Object **)&vrng->conf.rng, NULL); 266 } 267 268 static const TypeInfo virtio_rng_info = { 269 .name = TYPE_VIRTIO_RNG, 270 .parent = TYPE_VIRTIO_DEVICE, 271 .instance_size = sizeof(VirtIORNG), 272 .instance_init = virtio_rng_initfn, 273 .class_init = virtio_rng_class_init, 274 }; 275 276 static void virtio_register_types(void) 277 { 278 type_register_static(&virtio_rng_info); 279 } 280 281 type_init(virtio_register_types) 282