vhost-user.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * QEMU vhost-user backend
  3. *
  4. * Copyright (C) 2018 Red Hat Inc
  5. *
  6. * Authors:
  7. * Marc-André Lureau <marcandre.lureau@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qapi/error.h"
  14. #include "qemu/error-report.h"
  15. #include "qom/object_interfaces.h"
  16. #include "sysemu/vhost-user-backend.h"
  17. #include "sysemu/kvm.h"
  18. #include "io/channel-command.h"
  19. #include "hw/virtio/virtio-bus.h"
  20. static bool
  21. ioeventfd_enabled(void)
  22. {
  23. return kvm_enabled() && kvm_eventfds_enabled();
  24. }
  25. int
  26. vhost_user_backend_dev_init(VhostUserBackend *b, VirtIODevice *vdev,
  27. unsigned nvqs, Error **errp)
  28. {
  29. int ret;
  30. assert(!b->vdev && vdev);
  31. if (!ioeventfd_enabled()) {
  32. error_setg(errp, "vhost initialization failed: requires kvm");
  33. return -1;
  34. }
  35. if (!vhost_user_init(&b->vhost_user, &b->chr, errp)) {
  36. return -1;
  37. }
  38. b->vdev = vdev;
  39. b->dev.nvqs = nvqs;
  40. b->dev.vqs = g_new0(struct vhost_virtqueue, nvqs);
  41. ret = vhost_dev_init(&b->dev, &b->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
  42. errp);
  43. if (ret < 0) {
  44. return -1;
  45. }
  46. return 0;
  47. }
  48. void
  49. vhost_user_backend_start(VhostUserBackend *b)
  50. {
  51. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(b->vdev)));
  52. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  53. int ret, i ;
  54. if (b->started) {
  55. return;
  56. }
  57. if (!k->set_guest_notifiers) {
  58. error_report("binding does not support guest notifiers");
  59. return;
  60. }
  61. ret = vhost_dev_enable_notifiers(&b->dev, b->vdev);
  62. if (ret < 0) {
  63. return;
  64. }
  65. ret = k->set_guest_notifiers(qbus->parent, b->dev.nvqs, true);
  66. if (ret < 0) {
  67. error_report("Error binding guest notifier");
  68. goto err_host_notifiers;
  69. }
  70. b->dev.acked_features = b->vdev->guest_features;
  71. ret = vhost_dev_start(&b->dev, b->vdev, true);
  72. if (ret < 0) {
  73. error_report("Error start vhost dev");
  74. goto err_guest_notifiers;
  75. }
  76. /* guest_notifier_mask/pending not used yet, so just unmask
  77. * everything here. virtio-pci will do the right thing by
  78. * enabling/disabling irqfd.
  79. */
  80. for (i = 0; i < b->dev.nvqs; i++) {
  81. vhost_virtqueue_mask(&b->dev, b->vdev,
  82. b->dev.vq_index + i, false);
  83. }
  84. b->started = true;
  85. return;
  86. err_guest_notifiers:
  87. k->set_guest_notifiers(qbus->parent, b->dev.nvqs, false);
  88. err_host_notifiers:
  89. vhost_dev_disable_notifiers(&b->dev, b->vdev);
  90. }
  91. void
  92. vhost_user_backend_stop(VhostUserBackend *b)
  93. {
  94. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(b->vdev)));
  95. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  96. int ret = 0;
  97. if (!b->started) {
  98. return;
  99. }
  100. vhost_dev_stop(&b->dev, b->vdev, true);
  101. if (k->set_guest_notifiers) {
  102. ret = k->set_guest_notifiers(qbus->parent,
  103. b->dev.nvqs, false);
  104. if (ret < 0) {
  105. error_report("vhost guest notifier cleanup failed: %d", ret);
  106. }
  107. }
  108. assert(ret >= 0);
  109. vhost_dev_disable_notifiers(&b->dev, b->vdev);
  110. b->started = false;
  111. }
  112. static void set_chardev(Object *obj, const char *value, Error **errp)
  113. {
  114. VhostUserBackend *b = VHOST_USER_BACKEND(obj);
  115. Chardev *chr;
  116. if (b->completed) {
  117. error_setg(errp, "Property 'chardev' can no longer be set");
  118. return;
  119. }
  120. g_free(b->chr_name);
  121. b->chr_name = g_strdup(value);
  122. chr = qemu_chr_find(b->chr_name);
  123. if (chr == NULL) {
  124. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  125. "Chardev '%s' not found", b->chr_name);
  126. return;
  127. }
  128. if (!qemu_chr_fe_init(&b->chr, chr, errp)) {
  129. return;
  130. }
  131. b->completed = true;
  132. /* could call vhost_dev_init() so early message can be exchanged */
  133. }
  134. static char *get_chardev(Object *obj, Error **errp)
  135. {
  136. VhostUserBackend *b = VHOST_USER_BACKEND(obj);
  137. Chardev *chr = qemu_chr_fe_get_driver(&b->chr);
  138. if (chr && chr->label) {
  139. return g_strdup(chr->label);
  140. }
  141. return NULL;
  142. }
  143. static void vhost_user_backend_class_init(ObjectClass *oc, void *data)
  144. {
  145. object_class_property_add_str(oc, "chardev", get_chardev, set_chardev);
  146. }
  147. static void vhost_user_backend_finalize(Object *obj)
  148. {
  149. VhostUserBackend *b = VHOST_USER_BACKEND(obj);
  150. g_free(b->dev.vqs);
  151. g_free(b->chr_name);
  152. vhost_user_cleanup(&b->vhost_user);
  153. qemu_chr_fe_deinit(&b->chr, true);
  154. }
  155. static const TypeInfo vhost_user_backend_info = {
  156. .name = TYPE_VHOST_USER_BACKEND,
  157. .parent = TYPE_OBJECT,
  158. .instance_size = sizeof(VhostUserBackend),
  159. .class_init = vhost_user_backend_class_init,
  160. .instance_finalize = vhost_user_backend_finalize,
  161. };
  162. static void register_types(void)
  163. {
  164. type_register_static(&vhost_user_backend_info);
  165. }
  166. type_init(register_types);