2
0

vhost-user.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "system/vhost-user-backend.h"
  17. #include "system/kvm.h"
  18. #include "io/channel-command.h"
  19. #include "hw/virtio/virtio-bus.h"
  20. int
  21. vhost_user_backend_dev_init(VhostUserBackend *b, VirtIODevice *vdev,
  22. unsigned nvqs, Error **errp)
  23. {
  24. int ret;
  25. assert(!b->vdev && vdev);
  26. if (!vhost_user_init(&b->vhost_user, &b->chr, errp)) {
  27. return -1;
  28. }
  29. b->vdev = vdev;
  30. b->dev.nvqs = nvqs;
  31. b->dev.vqs = g_new0(struct vhost_virtqueue, nvqs);
  32. ret = vhost_dev_init(&b->dev, &b->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
  33. errp);
  34. if (ret < 0) {
  35. return -1;
  36. }
  37. return 0;
  38. }
  39. void
  40. vhost_user_backend_start(VhostUserBackend *b)
  41. {
  42. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(b->vdev)));
  43. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  44. int ret, i ;
  45. if (b->started) {
  46. return;
  47. }
  48. if (!k->set_guest_notifiers) {
  49. error_report("binding does not support guest notifiers");
  50. return;
  51. }
  52. ret = vhost_dev_enable_notifiers(&b->dev, b->vdev);
  53. if (ret < 0) {
  54. return;
  55. }
  56. ret = k->set_guest_notifiers(qbus->parent, b->dev.nvqs, true);
  57. if (ret < 0) {
  58. error_report("Error binding guest notifier");
  59. goto err_host_notifiers;
  60. }
  61. b->dev.acked_features = b->vdev->guest_features;
  62. ret = vhost_dev_start(&b->dev, b->vdev, true);
  63. if (ret < 0) {
  64. error_report("Error start vhost dev");
  65. goto err_guest_notifiers;
  66. }
  67. /* guest_notifier_mask/pending not used yet, so just unmask
  68. * everything here. virtio-pci will do the right thing by
  69. * enabling/disabling irqfd.
  70. */
  71. for (i = 0; i < b->dev.nvqs; i++) {
  72. vhost_virtqueue_mask(&b->dev, b->vdev,
  73. b->dev.vq_index + i, false);
  74. }
  75. b->started = true;
  76. return;
  77. err_guest_notifiers:
  78. k->set_guest_notifiers(qbus->parent, b->dev.nvqs, false);
  79. err_host_notifiers:
  80. vhost_dev_disable_notifiers(&b->dev, b->vdev);
  81. }
  82. void
  83. vhost_user_backend_stop(VhostUserBackend *b)
  84. {
  85. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(b->vdev)));
  86. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  87. int ret = 0;
  88. if (!b->started) {
  89. return;
  90. }
  91. vhost_dev_stop(&b->dev, b->vdev, true);
  92. if (k->set_guest_notifiers) {
  93. ret = k->set_guest_notifiers(qbus->parent,
  94. b->dev.nvqs, false);
  95. if (ret < 0) {
  96. error_report("vhost guest notifier cleanup failed: %d", ret);
  97. }
  98. }
  99. assert(ret >= 0);
  100. vhost_dev_disable_notifiers(&b->dev, b->vdev);
  101. b->started = false;
  102. }
  103. static void set_chardev(Object *obj, const char *value, Error **errp)
  104. {
  105. VhostUserBackend *b = VHOST_USER_BACKEND(obj);
  106. Chardev *chr;
  107. if (b->completed) {
  108. error_setg(errp, "Property 'chardev' can no longer be set");
  109. return;
  110. }
  111. g_free(b->chr_name);
  112. b->chr_name = g_strdup(value);
  113. chr = qemu_chr_find(b->chr_name);
  114. if (chr == NULL) {
  115. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  116. "Chardev '%s' not found", b->chr_name);
  117. return;
  118. }
  119. if (!qemu_chr_fe_init(&b->chr, chr, errp)) {
  120. return;
  121. }
  122. b->completed = true;
  123. /* could call vhost_dev_init() so early message can be exchanged */
  124. }
  125. static char *get_chardev(Object *obj, Error **errp)
  126. {
  127. VhostUserBackend *b = VHOST_USER_BACKEND(obj);
  128. Chardev *chr = qemu_chr_fe_get_driver(&b->chr);
  129. if (chr && chr->label) {
  130. return g_strdup(chr->label);
  131. }
  132. return NULL;
  133. }
  134. static void vhost_user_backend_class_init(ObjectClass *oc, void *data)
  135. {
  136. object_class_property_add_str(oc, "chardev", get_chardev, set_chardev);
  137. }
  138. static void vhost_user_backend_finalize(Object *obj)
  139. {
  140. VhostUserBackend *b = VHOST_USER_BACKEND(obj);
  141. g_free(b->dev.vqs);
  142. g_free(b->chr_name);
  143. vhost_user_cleanup(&b->vhost_user);
  144. qemu_chr_fe_deinit(&b->chr, true);
  145. }
  146. static const TypeInfo vhost_user_backend_info = {
  147. .name = TYPE_VHOST_USER_BACKEND,
  148. .parent = TYPE_OBJECT,
  149. .instance_size = sizeof(VhostUserBackend),
  150. .class_init = vhost_user_backend_class_init,
  151. .instance_finalize = vhost_user_backend_finalize,
  152. };
  153. static void register_types(void)
  154. {
  155. type_register_static(&vhost_user_backend_info);
  156. }
  157. type_init(register_types);