virtio-rng.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/iov.h"
  12. #include "qdev.h"
  13. #include "virtio.h"
  14. #include "virtio-rng.h"
  15. #include "qemu/rng.h"
  16. typedef struct VirtIORNG {
  17. VirtIODevice vdev;
  18. DeviceState *qdev;
  19. /* Only one vq - guest puts buffer(s) on it when it needs entropy */
  20. VirtQueue *vq;
  21. VirtIORNGConf *conf;
  22. RngBackend *rng;
  23. /* We purposefully don't migrate this state. The quota will reset on the
  24. * destination as a result. Rate limiting is host state, not guest state.
  25. */
  26. QEMUTimer *rate_limit_timer;
  27. int64_t quota_remaining;
  28. } VirtIORNG;
  29. static bool is_guest_ready(VirtIORNG *vrng)
  30. {
  31. if (virtio_queue_ready(vrng->vq)
  32. && (vrng->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. static size_t get_request_size(VirtQueue *vq, unsigned quota)
  38. {
  39. unsigned int in, out;
  40. virtqueue_get_avail_bytes(vq, &in, &out, quota, 0);
  41. return in;
  42. }
  43. static void virtio_rng_process(VirtIORNG *vrng);
  44. /* Send data from a char device over to the guest */
  45. static void chr_read(void *opaque, const void *buf, size_t size)
  46. {
  47. VirtIORNG *vrng = opaque;
  48. VirtQueueElement elem;
  49. size_t len;
  50. int offset;
  51. if (!is_guest_ready(vrng)) {
  52. return;
  53. }
  54. vrng->quota_remaining -= size;
  55. offset = 0;
  56. while (offset < size) {
  57. if (!virtqueue_pop(vrng->vq, &elem)) {
  58. break;
  59. }
  60. len = iov_from_buf(elem.in_sg, elem.in_num,
  61. 0, buf + offset, size - offset);
  62. offset += len;
  63. virtqueue_push(vrng->vq, &elem, len);
  64. }
  65. virtio_notify(&vrng->vdev, vrng->vq);
  66. }
  67. static void virtio_rng_process(VirtIORNG *vrng)
  68. {
  69. size_t size;
  70. unsigned quota;
  71. if (!is_guest_ready(vrng)) {
  72. return;
  73. }
  74. if (vrng->quota_remaining < 0) {
  75. quota = 0;
  76. } else {
  77. quota = MIN((uint64_t)vrng->quota_remaining, (uint64_t)UINT32_MAX);
  78. }
  79. size = get_request_size(vrng->vq, quota);
  80. size = MIN(vrng->quota_remaining, size);
  81. if (size) {
  82. rng_backend_request_entropy(vrng->rng, size, chr_read, vrng);
  83. }
  84. }
  85. static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
  86. {
  87. VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
  88. virtio_rng_process(vrng);
  89. }
  90. static uint32_t get_features(VirtIODevice *vdev, uint32_t f)
  91. {
  92. return f;
  93. }
  94. static void virtio_rng_save(QEMUFile *f, void *opaque)
  95. {
  96. VirtIORNG *vrng = opaque;
  97. virtio_save(&vrng->vdev, f);
  98. }
  99. static int virtio_rng_load(QEMUFile *f, void *opaque, int version_id)
  100. {
  101. VirtIORNG *vrng = opaque;
  102. if (version_id != 1) {
  103. return -EINVAL;
  104. }
  105. virtio_load(&vrng->vdev, f);
  106. /* We may have an element ready but couldn't process it due to a quota
  107. * limit. Make sure to try again after live migration when the quota may
  108. * have been reset.
  109. */
  110. virtio_rng_process(vrng);
  111. return 0;
  112. }
  113. static void check_rate_limit(void *opaque)
  114. {
  115. VirtIORNG *s = opaque;
  116. s->quota_remaining = s->conf->max_bytes;
  117. virtio_rng_process(s);
  118. qemu_mod_timer(s->rate_limit_timer,
  119. qemu_get_clock_ms(vm_clock) + s->conf->period_ms);
  120. }
  121. VirtIODevice *virtio_rng_init(DeviceState *dev, VirtIORNGConf *conf)
  122. {
  123. VirtIORNG *vrng;
  124. VirtIODevice *vdev;
  125. Error *local_err = NULL;
  126. vdev = virtio_common_init("virtio-rng", VIRTIO_ID_RNG, 0,
  127. sizeof(VirtIORNG));
  128. vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
  129. vrng->rng = conf->rng;
  130. if (vrng->rng == NULL) {
  131. qerror_report(QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object");
  132. return NULL;
  133. }
  134. rng_backend_open(vrng->rng, &local_err);
  135. if (local_err) {
  136. qerror_report_err(local_err);
  137. error_free(local_err);
  138. return NULL;
  139. }
  140. vrng->vq = virtio_add_queue(vdev, 8, handle_input);
  141. vrng->vdev.get_features = get_features;
  142. vrng->qdev = dev;
  143. vrng->conf = conf;
  144. assert(vrng->conf->max_bytes <= INT64_MAX);
  145. vrng->quota_remaining = vrng->conf->max_bytes;
  146. vrng->rate_limit_timer = qemu_new_timer_ms(vm_clock,
  147. check_rate_limit, vrng);
  148. qemu_mod_timer(vrng->rate_limit_timer,
  149. qemu_get_clock_ms(vm_clock) + vrng->conf->period_ms);
  150. register_savevm(dev, "virtio-rng", -1, 1, virtio_rng_save,
  151. virtio_rng_load, vrng);
  152. return vdev;
  153. }
  154. void virtio_rng_exit(VirtIODevice *vdev)
  155. {
  156. VirtIORNG *vrng = DO_UPCAST(VirtIORNG, vdev, vdev);
  157. qemu_del_timer(vrng->rate_limit_timer);
  158. qemu_free_timer(vrng->rate_limit_timer);
  159. unregister_savevm(vrng->qdev, "virtio-rng", vrng);
  160. virtio_cleanup(vdev);
  161. }