vhost-vsock.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Virtio vsock device
  3. *
  4. * Copyright 2015 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 "standard-headers/linux/virtio_vsock.h"
  15. #include "qapi/error.h"
  16. #include "hw/virtio/virtio-access.h"
  17. #include "qemu/error-report.h"
  18. #include "hw/qdev-properties.h"
  19. #include "hw/virtio/vhost-vsock.h"
  20. #include "monitor/monitor.h"
  21. static void vhost_vsock_get_config(VirtIODevice *vdev, uint8_t *config)
  22. {
  23. VHostVSock *vsock = VHOST_VSOCK(vdev);
  24. struct virtio_vsock_config vsockcfg = {};
  25. virtio_stq_p(vdev, &vsockcfg.guest_cid, vsock->conf.guest_cid);
  26. memcpy(config, &vsockcfg, sizeof(vsockcfg));
  27. }
  28. static int vhost_vsock_set_guest_cid(VirtIODevice *vdev)
  29. {
  30. VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
  31. VHostVSock *vsock = VHOST_VSOCK(vdev);
  32. const VhostOps *vhost_ops = vvc->vhost_dev.vhost_ops;
  33. int ret;
  34. if (!vhost_ops->vhost_vsock_set_guest_cid) {
  35. return -ENOSYS;
  36. }
  37. ret = vhost_ops->vhost_vsock_set_guest_cid(&vvc->vhost_dev,
  38. vsock->conf.guest_cid);
  39. if (ret < 0) {
  40. return -errno;
  41. }
  42. return 0;
  43. }
  44. static int vhost_vsock_set_running(VirtIODevice *vdev, int start)
  45. {
  46. VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
  47. const VhostOps *vhost_ops = vvc->vhost_dev.vhost_ops;
  48. int ret;
  49. if (!vhost_ops->vhost_vsock_set_running) {
  50. return -ENOSYS;
  51. }
  52. ret = vhost_ops->vhost_vsock_set_running(&vvc->vhost_dev, start);
  53. if (ret < 0) {
  54. return -errno;
  55. }
  56. return 0;
  57. }
  58. static void vhost_vsock_set_status(VirtIODevice *vdev, uint8_t status)
  59. {
  60. VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
  61. bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
  62. int ret;
  63. if (!vdev->vm_running) {
  64. should_start = false;
  65. }
  66. if (vvc->vhost_dev.started == should_start) {
  67. return;
  68. }
  69. if (should_start) {
  70. ret = vhost_vsock_common_start(vdev);
  71. if (ret < 0) {
  72. return;
  73. }
  74. ret = vhost_vsock_set_running(vdev, 1);
  75. if (ret < 0) {
  76. vhost_vsock_common_stop(vdev);
  77. error_report("Error starting vhost vsock: %d", -ret);
  78. return;
  79. }
  80. } else {
  81. ret = vhost_vsock_set_running(vdev, 0);
  82. if (ret < 0) {
  83. error_report("vhost vsock set running failed: %d", ret);
  84. return;
  85. }
  86. vhost_vsock_common_stop(vdev);
  87. }
  88. }
  89. static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
  90. uint64_t requested_features,
  91. Error **errp)
  92. {
  93. /* No feature bits used yet */
  94. return requested_features;
  95. }
  96. static const VMStateDescription vmstate_virtio_vhost_vsock = {
  97. .name = "virtio-vhost_vsock",
  98. .minimum_version_id = VHOST_VSOCK_SAVEVM_VERSION,
  99. .version_id = VHOST_VSOCK_SAVEVM_VERSION,
  100. .fields = (VMStateField[]) {
  101. VMSTATE_VIRTIO_DEVICE,
  102. VMSTATE_END_OF_LIST()
  103. },
  104. .pre_save = vhost_vsock_common_pre_save,
  105. .post_load = vhost_vsock_common_post_load,
  106. };
  107. static void vhost_vsock_device_realize(DeviceState *dev, Error **errp)
  108. {
  109. VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(dev);
  110. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  111. VHostVSock *vsock = VHOST_VSOCK(dev);
  112. int vhostfd;
  113. int ret;
  114. /* Refuse to use reserved CID numbers */
  115. if (vsock->conf.guest_cid <= 2) {
  116. error_setg(errp, "guest-cid property must be greater than 2");
  117. return;
  118. }
  119. if (vsock->conf.guest_cid > UINT32_MAX) {
  120. error_setg(errp, "guest-cid property must be a 32-bit number");
  121. return;
  122. }
  123. if (vsock->conf.vhostfd) {
  124. vhostfd = monitor_fd_param(cur_mon, vsock->conf.vhostfd, errp);
  125. if (vhostfd == -1) {
  126. error_prepend(errp, "vhost-vsock: unable to parse vhostfd: ");
  127. return;
  128. }
  129. } else {
  130. vhostfd = open("/dev/vhost-vsock", O_RDWR);
  131. if (vhostfd < 0) {
  132. error_setg_errno(errp, errno,
  133. "vhost-vsock: failed to open vhost device");
  134. return;
  135. }
  136. }
  137. vhost_vsock_common_realize(vdev, "vhost-vsock");
  138. ret = vhost_dev_init(&vvc->vhost_dev, (void *)(uintptr_t)vhostfd,
  139. VHOST_BACKEND_TYPE_KERNEL, 0);
  140. if (ret < 0) {
  141. error_setg_errno(errp, -ret, "vhost-vsock: vhost_dev_init failed");
  142. goto err_virtio;
  143. }
  144. ret = vhost_vsock_set_guest_cid(vdev);
  145. if (ret < 0) {
  146. error_setg_errno(errp, -ret, "vhost-vsock: unable to set guest cid");
  147. goto err_vhost_dev;
  148. }
  149. return;
  150. err_vhost_dev:
  151. vhost_dev_cleanup(&vvc->vhost_dev);
  152. /* vhost_dev_cleanup() closes the vhostfd passed to vhost_dev_init() */
  153. vhostfd = -1;
  154. err_virtio:
  155. vhost_vsock_common_unrealize(vdev);
  156. if (vhostfd >= 0) {
  157. close(vhostfd);
  158. }
  159. return;
  160. }
  161. static void vhost_vsock_device_unrealize(DeviceState *dev)
  162. {
  163. VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(dev);
  164. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  165. /* This will stop vhost backend if appropriate. */
  166. vhost_vsock_set_status(vdev, 0);
  167. vhost_dev_cleanup(&vvc->vhost_dev);
  168. vhost_vsock_common_unrealize(vdev);
  169. }
  170. static Property vhost_vsock_properties[] = {
  171. DEFINE_PROP_UINT64("guest-cid", VHostVSock, conf.guest_cid, 0),
  172. DEFINE_PROP_STRING("vhostfd", VHostVSock, conf.vhostfd),
  173. DEFINE_PROP_END_OF_LIST(),
  174. };
  175. static void vhost_vsock_class_init(ObjectClass *klass, void *data)
  176. {
  177. DeviceClass *dc = DEVICE_CLASS(klass);
  178. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  179. device_class_set_props(dc, vhost_vsock_properties);
  180. dc->vmsd = &vmstate_virtio_vhost_vsock;
  181. vdc->realize = vhost_vsock_device_realize;
  182. vdc->unrealize = vhost_vsock_device_unrealize;
  183. vdc->get_features = vhost_vsock_get_features;
  184. vdc->get_config = vhost_vsock_get_config;
  185. vdc->set_status = vhost_vsock_set_status;
  186. }
  187. static const TypeInfo vhost_vsock_info = {
  188. .name = TYPE_VHOST_VSOCK,
  189. .parent = TYPE_VHOST_VSOCK_COMMON,
  190. .instance_size = sizeof(VHostVSock),
  191. .class_init = vhost_vsock_class_init,
  192. };
  193. static void vhost_vsock_register_types(void)
  194. {
  195. type_register_static(&vhost_vsock_info);
  196. }
  197. type_init(vhost_vsock_register_types)