virtio-rng.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. if (vrng->conf.period_ms <= 0) {
  149. error_setg(errp, "'period' parameter expects a positive integer");
  150. return;
  151. }
  152. /* Workaround: Property parsing does not enforce unsigned integers,
  153. * So this is a hack to reject such numbers. */
  154. if (vrng->conf.max_bytes > INT64_MAX) {
  155. error_setg(errp, "'max-bytes' parameter must be non-negative, "
  156. "and less than 2^63");
  157. return;
  158. }
  159. if (vrng->conf.rng == NULL) {
  160. Object *default_backend = object_new(TYPE_RNG_BUILTIN);
  161. if (!user_creatable_complete(USER_CREATABLE(default_backend),
  162. errp)) {
  163. object_unref(default_backend);
  164. return;
  165. }
  166. object_property_add_child(OBJECT(dev), "default-backend",
  167. default_backend);
  168. /* The child property took a reference, we can safely drop ours now */
  169. object_unref(default_backend);
  170. object_property_set_link(OBJECT(dev), "rng", default_backend,
  171. &error_abort);
  172. }
  173. vrng->rng = vrng->conf.rng;
  174. if (vrng->rng == NULL) {
  175. error_setg(errp, "'rng' parameter expects a valid object");
  176. return;
  177. }
  178. virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0);
  179. vrng->vq = virtio_add_queue(vdev, 8, handle_input);
  180. vrng->quota_remaining = vrng->conf.max_bytes;
  181. vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
  182. check_rate_limit, vrng);
  183. vrng->activate_timer = true;
  184. vrng->vmstate = qemu_add_vm_change_state_handler(virtio_rng_vm_state_change,
  185. vrng);
  186. }
  187. static void virtio_rng_device_unrealize(DeviceState *dev)
  188. {
  189. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  190. VirtIORNG *vrng = VIRTIO_RNG(dev);
  191. qemu_del_vm_change_state_handler(vrng->vmstate);
  192. timer_del(vrng->rate_limit_timer);
  193. timer_free(vrng->rate_limit_timer);
  194. virtio_del_queue(vdev, 0);
  195. virtio_cleanup(vdev);
  196. }
  197. static const VMStateDescription vmstate_virtio_rng = {
  198. .name = "virtio-rng",
  199. .minimum_version_id = 1,
  200. .version_id = 1,
  201. .fields = (VMStateField[]) {
  202. VMSTATE_VIRTIO_DEVICE,
  203. VMSTATE_END_OF_LIST()
  204. },
  205. };
  206. static Property virtio_rng_properties[] = {
  207. /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
  208. * you have an entropy source capable of generating more entropy than this
  209. * and you can pass it through via virtio-rng, then hats off to you. Until
  210. * then, this is unlimited for all practical purposes.
  211. */
  212. DEFINE_PROP_UINT64("max-bytes", VirtIORNG, conf.max_bytes, INT64_MAX),
  213. DEFINE_PROP_UINT32("period", VirtIORNG, conf.period_ms, 1 << 16),
  214. DEFINE_PROP_LINK("rng", VirtIORNG, conf.rng, TYPE_RNG_BACKEND, RngBackend *),
  215. DEFINE_PROP_END_OF_LIST(),
  216. };
  217. static void virtio_rng_class_init(ObjectClass *klass, void *data)
  218. {
  219. DeviceClass *dc = DEVICE_CLASS(klass);
  220. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  221. device_class_set_props(dc, virtio_rng_properties);
  222. dc->vmsd = &vmstate_virtio_rng;
  223. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  224. vdc->realize = virtio_rng_device_realize;
  225. vdc->unrealize = virtio_rng_device_unrealize;
  226. vdc->get_features = get_features;
  227. vdc->set_status = virtio_rng_set_status;
  228. }
  229. static const TypeInfo virtio_rng_info = {
  230. .name = TYPE_VIRTIO_RNG,
  231. .parent = TYPE_VIRTIO_DEVICE,
  232. .instance_size = sizeof(VirtIORNG),
  233. .class_init = virtio_rng_class_init,
  234. };
  235. static void virtio_register_types(void)
  236. {
  237. type_register_static(&virtio_rng_info);
  238. }
  239. type_init(virtio_register_types)