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 "iov.h" 13 #include "qdev.h" 14 #include "virtio.h" 15 #include "virtio-rng.h" 16 #include "qemu/rng.h" 17 18 typedef struct VirtIORNG { 19 VirtIODevice vdev; 20 21 DeviceState *qdev; 22 23 /* Only one vq - guest puts buffer(s) on it when it needs entropy */ 24 VirtQueue *vq; 25 VirtQueueElement elem; 26 27 /* Config data for the device -- currently only chardev */ 28 VirtIORNGConf *conf; 29 30 /* Whether we've popped a vq element into 'elem' above */ 31 bool popped; 32 33 RngBackend *rng; 34 35 /* We purposefully don't migrate this state. The quota will reset on the 36 * destination as a result. Rate limiting is host state, not guest state. 37 */ 38 QEMUTimer *rate_limit_timer; 39 int64_t quota_remaining; 40 } VirtIORNG; 41 42 static bool is_guest_ready(VirtIORNG *vrng) 43 { 44 if (virtio_queue_ready(vrng->vq) 45 && (vrng->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) { 46 return true; 47 } 48 return false; 49 } 50 51 static size_t pop_an_elem(VirtIORNG *vrng) 52 { 53 size_t size; 54 55 if (!vrng->popped && !virtqueue_pop(vrng->vq, &vrng->elem)) { 56 return 0; 57 } 58 vrng->popped = true; 59 60 size = iov_size(vrng->elem.in_sg, vrng->elem.in_num); 61 return size; 62 } 63 64 static void virtio_rng_process(VirtIORNG *vrng); 65 66 /* Send data from a char device over to the guest */ 67 static void chr_read(void *opaque, const void *buf, size_t size) 68 { 69 VirtIORNG *vrng = opaque; 70 size_t len; 71 int offset; 72 73 if (!is_guest_ready(vrng)) { 74 return; 75 } 76 77 vrng->quota_remaining -= size; 78 79 offset = 0; 80 while (offset < size) { 81 if (!pop_an_elem(vrng)) { 82 break; 83 } 84 len = iov_from_buf(vrng->elem.in_sg, vrng->elem.in_num, 85 0, buf + offset, size - offset); 86 offset += len; 87 88 virtqueue_push(vrng->vq, &vrng->elem, len); 89 vrng->popped = false; 90 } 91 virtio_notify(&vrng->vdev, vrng->vq); 92 93 /* 94 * Lastly, if we had multiple elems queued by the guest, and we 95 * didn't have enough data to fill them all, indicate we want more 96 * data. 97 */ 98 virtio_rng_process(vrng); 99 } 100 101 static void virtio_rng_process(VirtIORNG *vrng) 102 { 103 ssize_t size; 104 105 if (!is_guest_ready(vrng)) { 106 return; 107 } 108 109 size = pop_an_elem(vrng); 110 size = MIN(vrng->quota_remaining, size); 111 112 if (size > 0) { 113 rng_backend_request_entropy(vrng->rng, size, chr_read, vrng); 114 } 115 } 116 117 118 static void handle_input(VirtIODevice *vdev, VirtQueue *vq) 119 { 120 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev); 121 virtio_rng_process(vrng); 122 } 123 124 static uint32_t get_features(VirtIODevice *vdev, uint32_t f) 125 { 126 return f; 127 } 128 129 static void virtio_rng_save(QEMUFile *f, void *opaque) 130 { 131 VirtIORNG *vrng = opaque; 132 133 virtio_save(&vrng->vdev, f); 134 135 qemu_put_byte(f, vrng->popped); 136 if (vrng->popped) { 137 int i; 138 139 qemu_put_be32(f, vrng->elem.index); 140 141 qemu_put_be32(f, vrng->elem.in_num); 142 for (i = 0; i < vrng->elem.in_num; i++) { 143 qemu_put_be64(f, vrng->elem.in_addr[i]); 144 } 145 146 qemu_put_be32(f, vrng->elem.out_num); 147 for (i = 0; i < vrng->elem.out_num; i++) { 148 qemu_put_be64(f, vrng->elem.out_addr[i]); 149 } 150 } 151 } 152 153 static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id) 154 { 155 VirtIORNG *vrng = opaque; 156 157 if (version_id != 1) { 158 return -EINVAL; 159 } 160 virtio_load(&vrng->vdev, f); 161 162 vrng->popped = qemu_get_byte(f); 163 if (vrng->popped) { 164 int i; 165 166 vrng->elem.index = qemu_get_be32(f); 167 168 vrng->elem.in_num = qemu_get_be32(f); 169 g_assert(vrng->elem.in_num < VIRTQUEUE_MAX_SIZE); 170 for (i = 0; i < vrng->elem.in_num; i++) { 171 vrng->elem.in_addr[i] = qemu_get_be64(f); 172 } 173 174 vrng->elem.out_num = qemu_get_be32(f); 175 g_assert(vrng->elem.out_num < VIRTQUEUE_MAX_SIZE); 176 for (i = 0; i < vrng->elem.out_num; i++) { 177 vrng->elem.out_addr[i] = qemu_get_be64(f); 178 } 179 180 virtqueue_map_sg(vrng->elem.in_sg, vrng->elem.in_addr, 181 vrng->elem.in_num, 1); 182 virtqueue_map_sg(vrng->elem.out_sg, vrng->elem.out_addr, 183 vrng->elem.out_num, 0); 184 } 185 186 /* We may have an element ready but couldn't process it due to a quota 187 limit. Make sure to try again after live migration when the quota may 188 have been reset. 189 */ 190 virtio_rng_process(vrng); 191 192 return 0; 193 } 194 195 static void check_rate_limit(void *opaque) 196 { 197 VirtIORNG *s = opaque; 198 199 s->quota_remaining = s->conf->max_bytes; 200 virtio_rng_process(s); 201 qemu_mod_timer(s->rate_limit_timer, 202 qemu_get_clock_ms(vm_clock) + s->conf->period_ms); 203 } 204 205 206 VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf) 207 { 208 VirtIORNG *vrng; 209 VirtIODevice *vdev; 210 Error *local_err = NULL; 211 212 vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0, 213 sizeof(VirtIORNG)); 214 215 vrng = DO_UPCAST(VirtIORNG, vdev, vdev); 216 217 vrng->rng = conf->rng; 218 if (vrng->rng == NULL) { 219 qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object"); 220 return NULL; 221 } 222 223 rng_backend_open(vrng->rng, &local_err); 224 if (local_err) { 225 qerror_report_err(local_err); 226 error_free(local_err); 227 return NULL; 228 } 229 230 vrng->vq = virtio_add_queue(vdev, 8, handle_input); 231 vrng->vdev.get_features = get_features; 232 233 vrng->qdev = dev; 234 vrng->conf = conf; 235 vrng->popped = false; 236 vrng->quota_remaining = vrng->conf->max_bytes; 237 238 g_assert_cmpint(vrng->conf->max_bytes, <=, INT64_MAX); 239 240 vrng->rate_limit_timer = qemu_new_timer_ms(vm_clock, 241 check_rate_limit, vrng); 242 243 qemu_mod_timer(vrng->rate_limit_timer, 244 qemu_get_clock_ms(vm_clock) + vrng->conf->period_ms); 245 246 register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save, 247 virtio_rng_load, vrng); 248 249 return vdev; 250 } 251 252 void virtio_rng_exit(VirtIODevice *vdev) 253 { 254 VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev); 255 256 unregister_savevm(vrng->qdev, "virtio-rng", vrng); 257 virtio_cleanup(vdev); 258 } 259