virtio-rng.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "qemu/osdep.h"
  12. #include "qapi/error.h"
  13. #include "qemu/iov.h"
  14. #include "qemu/module.h"
  15. #include "qemu/timer.h"
  16. #include "hw/virtio/virtio.h"
  17. #include "hw/qdev-properties.h"
  18. #include "hw/virtio/virtio-rng.h"
  19. #include "sysemu/rng.h"
  20. #include "sysemu/runstate.h"
  21. #include "qom/object_interfaces.h"
  22. #include "trace.h"
  23. static bool is_guest_ready(VirtIORNG *vrng)
  24. {
  25. VirtIODevice *vdev = VIRTIO_DEVICE(vrng);
  26. if (virtio_queue_ready(vrng->vq)
  27. && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  28. return true;
  29. }
  30. trace_virtio_rng_guest_not_ready(vrng);
  31. return false;
  32. }
  33. static size_t get_request_size(VirtQueue *vq, unsigned quota)
  34. {
  35. unsigned int in, out;
  36. virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
  37. return in;
  38. }
  39. static void virtio_rng_process(VirtIORNG *vrng);
  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. if (!is_guest_ready(vrng)) {
  49. return;
  50. }
  51. /* we can't modify the virtqueue until
  52. * our state is fully synced
  53. */
  54. if (!runstate_check(RUN_STATE_RUNNING)) {
  55. trace_virtio_rng_cpu_is_stopped(vrng, size);
  56. return;
  57. }
  58. vrng->quota_remaining -= size;
  59. offset = 0;
  60. while (offset < size) {
  61. elem = virtqueue_pop(vrng->vq, sizeof(VirtQueueElement));
  62. if (!elem) {
  63. break;
  64. }
  65. trace_virtio_rng_popped(vrng);
  66. len = iov_from_buf(elem->in_sg, elem->in_num,
  67. 0, buf + offset, size - offset);
  68. offset += len;
  69. virtqueue_push(vrng->vq, elem, len);
  70. trace_virtio_rng_pushed(vrng, len);
  71. g_free(elem);
  72. }
  73. virtio_notify(vdev, vrng->vq);
  74. if (!virtio_queue_empty(vrng->vq)) {
  75. /* If we didn't drain the queue, call virtio_rng_process
  76. * to take care of asking for more data as appropriate.
  77. */
  78. virtio_rng_process(vrng);
  79. }
  80. }
  81. static void virtio_rng_process(VirtIORNG *vrng)
  82. {
  83. size_t size;
  84. unsigned quota;
  85. if (!is_guest_ready(vrng)) {
  86. return;
  87. }
  88. if (vrng->activate_timer) {
  89. timer_mod(vrng->rate_limit_timer,
  90. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vrng->conf.period_ms);
  91. vrng->activate_timer = false;
  92. }
  93. if (vrng->quota_remaining < 0) {
  94. quota = 0;
  95. } else {
  96. quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
  97. }
  98. size = get_request_size(vrng->vq, quota);
  99. trace_virtio_rng_request(vrng, size, quota);
  100. size = MIN(vrng->quota_remaining, size);
  101. if (size) {
  102. rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
  103. }
  104. }
  105. static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
  106. {
  107. VirtIORNG *vrng = VIRTIO_RNG(vdev);
  108. virtio_rng_process(vrng);
  109. }
  110. static uint64_t get_features(VirtIODevice *vdev, uint64_t f, Error **errp)
  111. {
  112. return f;
  113. }
  114. static void virtio_rng_vm_state_change(void *opaque, int running,
  115. RunState state)
  116. {
  117. VirtIORNG *vrng = opaque;
  118. trace_virtio_rng_vm_state_change(vrng, running, state);
  119. /* We may have an element ready but couldn't process it due to a quota
  120. * limit or because CPU was stopped. Make sure to try again when the
  121. * CPU restart.
  122. */
  123. if (running && is_guest_ready(vrng)) {
  124. virtio_rng_process(vrng);
  125. }
  126. }
  127. static void check_rate_limit(void *opaque)
  128. {
  129. VirtIORNG *vrng = opaque;
  130. vrng->quota_remaining = vrng->conf.max_bytes;
  131. virtio_rng_process(vrng);
  132. vrng->activate_timer = true;
  133. }
  134. static void virtio_rng_set_status(VirtIODevice *vdev, uint8_t status)
  135. {
  136. VirtIORNG *vrng = VIRTIO_RNG(vdev);
  137. if (!vdev->vm_running) {
  138. return;
  139. }
  140. vdev->status = status;
  141. /* Something changed, try to process buffers */
  142. virtio_rng_process(vrng);
  143. }
  144. static void virtio_rng_device_realize(DeviceState *dev, Error **errp)
  145. {
  146. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  147. VirtIORNG *vrng = VIRTIO_RNG(dev);
  148. Error *local_err = NULL;
  149. if (vrng->conf.period_ms <= 0) {
  150. error_setg(errp, "'period' parameter expects a positive integer");
  151. return;
  152. }
  153. /* Workaround: Property parsing does not enforce unsigned integers,
  154. * So this is a hack to reject such numbers. */
  155. if (vrng->conf.max_bytes > INT64_MAX) {
  156. error_setg(errp, "'max-bytes' parameter must be non-negative, "
  157. "and less than 2^63");
  158. return;
  159. }
  160. if (vrng->conf.rng == NULL) {
  161. Object *default_backend = object_new(TYPE_RNG_BUILTIN);
  162. user_creatable_complete(USER_CREATABLE(default_backend),
  163. &local_err);
  164. if (local_err) {
  165. error_propagate(errp, local_err);
  166. object_unref(default_backend);
  167. return;
  168. }
  169. object_property_add_child(OBJECT(dev), "default-backend",
  170. default_backend, &error_abort);
  171. /* The child property took a reference, we can safely drop ours now */
  172. object_unref(default_backend);
  173. object_property_set_link(OBJECT(dev), default_backend,
  174. "rng", &error_abort);
  175. }
  176. vrng->rng = vrng->conf.rng;
  177. if (vrng->rng == NULL) {
  178. error_setg(errp, "'rng' parameter expects a valid object");
  179. return;
  180. }
  181. virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0);
  182. vrng->vq = virtio_add_queue(vdev, 8, handle_input);
  183. vrng->quota_remaining = vrng->conf.max_bytes;
  184. vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
  185. check_rate_limit, vrng);
  186. vrng->activate_timer = true;
  187. vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change,
  188. vrng);
  189. }
  190. static void virtio_rng_device_unrealize(DeviceState *dev, Error **errp)
  191. {
  192. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  193. VirtIORNG *vrng = VIRTIO_RNG(dev);
  194. qemu_del_vm_change_state_handler(vrng->vmstate);
  195. timer_del(vrng->rate_limit_timer);
  196. timer_free(vrng->rate_limit_timer);
  197. virtio_del_queue(vdev, 0);
  198. virtio_cleanup(vdev);
  199. }
  200. static const VMStateDescription vmstate_virtio_rng = {
  201. .name = "virtio-rng",
  202. .minimum_version_id = 1,
  203. .version_id = 1,
  204. .fields = (VMStateField[]) {
  205. VMSTATE_VIRTIO_DEVICE,
  206. VMSTATE_END_OF_LIST()
  207. },
  208. };
  209. static Property virtio_rng_properties[] = {
  210. /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
  211. * you have an entropy source capable of generating more entropy than this
  212. * and you can pass it through via virtio-rng, then hats off to you. Until
  213. * then, this is unlimited for all practical purposes.
  214. */
  215. DEFINE_PROP_UINT64("max-bytes", VirtIORNG, conf.max_bytes, INT64_MAX),
  216. DEFINE_PROP_UINT32("period", VirtIORNG, conf.period_ms, 1 << 16),
  217. DEFINE_PROP_LINK("rng", VirtIORNG, conf.rng, TYPE_RNG_BACKEND, RngBackend *),
  218. DEFINE_PROP_END_OF_LIST(),
  219. };
  220. static void virtio_rng_class_init(ObjectClass *klass, void *data)
  221. {
  222. DeviceClass *dc = DEVICE_CLASS(klass);
  223. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  224. dc->props = virtio_rng_properties;
  225. dc->vmsd = &vmstate_virtio_rng;
  226. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  227. vdc->realize = virtio_rng_device_realize;
  228. vdc->unrealize = virtio_rng_device_unrealize;
  229. vdc->get_features = get_features;
  230. vdc->set_status = virtio_rng_set_status;
  231. }
  232. static const TypeInfo virtio_rng_info = {
  233. .name = TYPE_VIRTIO_RNG,
  234. .parent = TYPE_VIRTIO_DEVICE,
  235. .instance_size = sizeof(VirtIORNG),
  236. .class_init = virtio_rng_class_init,
  237. };
  238. static void virtio_register_types(void)
  239. {
  240. type_register_static(&virtio_rng_info);
  241. }
  242. type_init(virtio_register_types)