2
0

vhost-user-fs.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Vhost-user filesystem virtio device
  3. *
  4. * Copyright 2018-2019 Red Hat, Inc.
  5. *
  6. * Authors:
  7. * Stefan Hajnoczi <stefanha@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or
  10. * (at your option) any later version. See the COPYING file in the
  11. * top-level directory.
  12. */
  13. #include "qemu/osdep.h"
  14. #include <sys/ioctl.h>
  15. #include "standard-headers/linux/virtio_fs.h"
  16. #include "qapi/error.h"
  17. #include "hw/qdev-properties.h"
  18. #include "hw/virtio/virtio-bus.h"
  19. #include "hw/virtio/virtio-access.h"
  20. #include "qemu/error-report.h"
  21. #include "hw/virtio/vhost-user-fs.h"
  22. #include "monitor/monitor.h"
  23. static void vuf_get_config(VirtIODevice *vdev, uint8_t *config)
  24. {
  25. VHostUserFS *fs = VHOST_USER_FS(vdev);
  26. struct virtio_fs_config fscfg = {};
  27. memcpy((char *)fscfg.tag, fs->conf.tag,
  28. MIN(strlen(fs->conf.tag) + 1, sizeof(fscfg.tag)));
  29. virtio_stl_p(vdev, &fscfg.num_request_queues, fs->conf.num_request_queues);
  30. memcpy(config, &fscfg, sizeof(fscfg));
  31. }
  32. static void vuf_start(VirtIODevice *vdev)
  33. {
  34. VHostUserFS *fs = VHOST_USER_FS(vdev);
  35. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  36. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  37. int ret;
  38. int i;
  39. if (!k->set_guest_notifiers) {
  40. error_report("binding does not support guest notifiers");
  41. return;
  42. }
  43. ret = vhost_dev_enable_notifiers(&fs->vhost_dev, vdev);
  44. if (ret < 0) {
  45. error_report("Error enabling host notifiers: %d", -ret);
  46. return;
  47. }
  48. ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, true);
  49. if (ret < 0) {
  50. error_report("Error binding guest notifier: %d", -ret);
  51. goto err_host_notifiers;
  52. }
  53. fs->vhost_dev.acked_features = vdev->guest_features;
  54. ret = vhost_dev_start(&fs->vhost_dev, vdev);
  55. if (ret < 0) {
  56. error_report("Error starting vhost: %d", -ret);
  57. goto err_guest_notifiers;
  58. }
  59. /*
  60. * guest_notifier_mask/pending not used yet, so just unmask
  61. * everything here. virtio-pci will do the right thing by
  62. * enabling/disabling irqfd.
  63. */
  64. for (i = 0; i < fs->vhost_dev.nvqs; i++) {
  65. vhost_virtqueue_mask(&fs->vhost_dev, vdev, i, false);
  66. }
  67. return;
  68. err_guest_notifiers:
  69. k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
  70. err_host_notifiers:
  71. vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
  72. }
  73. static void vuf_stop(VirtIODevice *vdev)
  74. {
  75. VHostUserFS *fs = VHOST_USER_FS(vdev);
  76. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  77. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  78. int ret;
  79. if (!k->set_guest_notifiers) {
  80. return;
  81. }
  82. vhost_dev_stop(&fs->vhost_dev, vdev);
  83. ret = k->set_guest_notifiers(qbus->parent, fs->vhost_dev.nvqs, false);
  84. if (ret < 0) {
  85. error_report("vhost guest notifier cleanup failed: %d", ret);
  86. return;
  87. }
  88. vhost_dev_disable_notifiers(&fs->vhost_dev, vdev);
  89. }
  90. static void vuf_set_status(VirtIODevice *vdev, uint8_t status)
  91. {
  92. VHostUserFS *fs = VHOST_USER_FS(vdev);
  93. bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
  94. if (!vdev->vm_running) {
  95. should_start = false;
  96. }
  97. if (fs->vhost_dev.started == should_start) {
  98. return;
  99. }
  100. if (should_start) {
  101. vuf_start(vdev);
  102. } else {
  103. vuf_stop(vdev);
  104. }
  105. }
  106. static uint64_t vuf_get_features(VirtIODevice *vdev,
  107. uint64_t requested_features,
  108. Error **errp)
  109. {
  110. /* No feature bits used yet */
  111. return requested_features;
  112. }
  113. static void vuf_handle_output(VirtIODevice *vdev, VirtQueue *vq)
  114. {
  115. /*
  116. * Not normally called; it's the daemon that handles the queue;
  117. * however virtio's cleanup path can call this.
  118. */
  119. }
  120. static void vuf_guest_notifier_mask(VirtIODevice *vdev, int idx,
  121. bool mask)
  122. {
  123. VHostUserFS *fs = VHOST_USER_FS(vdev);
  124. vhost_virtqueue_mask(&fs->vhost_dev, vdev, idx, mask);
  125. }
  126. static bool vuf_guest_notifier_pending(VirtIODevice *vdev, int idx)
  127. {
  128. VHostUserFS *fs = VHOST_USER_FS(vdev);
  129. return vhost_virtqueue_pending(&fs->vhost_dev, idx);
  130. }
  131. static void vuf_device_realize(DeviceState *dev, Error **errp)
  132. {
  133. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  134. VHostUserFS *fs = VHOST_USER_FS(dev);
  135. unsigned int i;
  136. size_t len;
  137. int ret;
  138. if (!fs->conf.chardev.chr) {
  139. error_setg(errp, "missing chardev");
  140. return;
  141. }
  142. if (!fs->conf.tag) {
  143. error_setg(errp, "missing tag property");
  144. return;
  145. }
  146. len = strlen(fs->conf.tag);
  147. if (len == 0) {
  148. error_setg(errp, "tag property cannot be empty");
  149. return;
  150. }
  151. if (len > sizeof_field(struct virtio_fs_config, tag)) {
  152. error_setg(errp, "tag property must be %zu bytes or less",
  153. sizeof_field(struct virtio_fs_config, tag));
  154. return;
  155. }
  156. if (fs->conf.num_request_queues == 0) {
  157. error_setg(errp, "num-request-queues property must be larger than 0");
  158. return;
  159. }
  160. if (!is_power_of_2(fs->conf.queue_size)) {
  161. error_setg(errp, "queue-size property must be a power of 2");
  162. return;
  163. }
  164. if (fs->conf.queue_size > VIRTQUEUE_MAX_SIZE) {
  165. error_setg(errp, "queue-size property must be %u or smaller",
  166. VIRTQUEUE_MAX_SIZE);
  167. return;
  168. }
  169. if (!vhost_user_init(&fs->vhost_user, &fs->conf.chardev, errp)) {
  170. return;
  171. }
  172. virtio_init(vdev, "vhost-user-fs", VIRTIO_ID_FS,
  173. sizeof(struct virtio_fs_config));
  174. /* Hiprio queue */
  175. virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
  176. /* Request queues */
  177. for (i = 0; i < fs->conf.num_request_queues; i++) {
  178. virtio_add_queue(vdev, fs->conf.queue_size, vuf_handle_output);
  179. }
  180. /* 1 high prio queue, plus the number configured */
  181. fs->vhost_dev.nvqs = 1 + fs->conf.num_request_queues;
  182. fs->vhost_dev.vqs = g_new0(struct vhost_virtqueue, fs->vhost_dev.nvqs);
  183. ret = vhost_dev_init(&fs->vhost_dev, &fs->vhost_user,
  184. VHOST_BACKEND_TYPE_USER, 0);
  185. if (ret < 0) {
  186. error_setg_errno(errp, -ret, "vhost_dev_init failed");
  187. goto err_virtio;
  188. }
  189. return;
  190. err_virtio:
  191. vhost_user_cleanup(&fs->vhost_user);
  192. virtio_cleanup(vdev);
  193. g_free(fs->vhost_dev.vqs);
  194. return;
  195. }
  196. static void vuf_device_unrealize(DeviceState *dev, Error **errp)
  197. {
  198. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  199. VHostUserFS *fs = VHOST_USER_FS(dev);
  200. /* This will stop vhost backend if appropriate. */
  201. vuf_set_status(vdev, 0);
  202. vhost_dev_cleanup(&fs->vhost_dev);
  203. vhost_user_cleanup(&fs->vhost_user);
  204. virtio_cleanup(vdev);
  205. g_free(fs->vhost_dev.vqs);
  206. fs->vhost_dev.vqs = NULL;
  207. }
  208. static const VMStateDescription vuf_vmstate = {
  209. .name = "vhost-user-fs",
  210. .unmigratable = 1,
  211. };
  212. static Property vuf_properties[] = {
  213. DEFINE_PROP_CHR("chardev", VHostUserFS, conf.chardev),
  214. DEFINE_PROP_STRING("tag", VHostUserFS, conf.tag),
  215. DEFINE_PROP_UINT16("num-request-queues", VHostUserFS,
  216. conf.num_request_queues, 1),
  217. DEFINE_PROP_UINT16("queue-size", VHostUserFS, conf.queue_size, 128),
  218. DEFINE_PROP_STRING("vhostfd", VHostUserFS, conf.vhostfd),
  219. DEFINE_PROP_END_OF_LIST(),
  220. };
  221. static void vuf_class_init(ObjectClass *klass, void *data)
  222. {
  223. DeviceClass *dc = DEVICE_CLASS(klass);
  224. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  225. dc->props = vuf_properties;
  226. dc->vmsd = &vuf_vmstate;
  227. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  228. vdc->realize = vuf_device_realize;
  229. vdc->unrealize = vuf_device_unrealize;
  230. vdc->get_features = vuf_get_features;
  231. vdc->get_config = vuf_get_config;
  232. vdc->set_status = vuf_set_status;
  233. vdc->guest_notifier_mask = vuf_guest_notifier_mask;
  234. vdc->guest_notifier_pending = vuf_guest_notifier_pending;
  235. }
  236. static const TypeInfo vuf_info = {
  237. .name = TYPE_VHOST_USER_FS,
  238. .parent = TYPE_VIRTIO_DEVICE,
  239. .instance_size = sizeof(VHostUserFS),
  240. .class_init = vuf_class_init,
  241. };
  242. static void vuf_register_types(void)
  243. {
  244. type_register_static(&vuf_info);
  245. }
  246. type_init(vuf_register_types)