2
0

vhost-vsock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <sys/ioctl.h>
  15. #include "standard-headers/linux/virtio_vsock.h"
  16. #include "qapi/error.h"
  17. #include "hw/virtio/virtio-bus.h"
  18. #include "hw/virtio/virtio-access.h"
  19. #include "qemu/error-report.h"
  20. #include "hw/qdev-properties.h"
  21. #include "hw/virtio/vhost-vsock.h"
  22. #include "qemu/iov.h"
  23. #include "qemu/module.h"
  24. #include "monitor/monitor.h"
  25. enum {
  26. VHOST_VSOCK_SAVEVM_VERSION = 0,
  27. VHOST_VSOCK_QUEUE_SIZE = 128,
  28. };
  29. static void vhost_vsock_get_config(VirtIODevice *vdev, uint8_t *config)
  30. {
  31. VHostVSock *vsock = VHOST_VSOCK(vdev);
  32. struct virtio_vsock_config vsockcfg = {};
  33. virtio_stq_p(vdev, &vsockcfg.guest_cid, vsock->conf.guest_cid);
  34. memcpy(config, &vsockcfg, sizeof(vsockcfg));
  35. }
  36. static int vhost_vsock_set_guest_cid(VHostVSock *vsock)
  37. {
  38. const VhostOps *vhost_ops = vsock->vhost_dev.vhost_ops;
  39. int ret;
  40. if (!vhost_ops->vhost_vsock_set_guest_cid) {
  41. return -ENOSYS;
  42. }
  43. ret = vhost_ops->vhost_vsock_set_guest_cid(&vsock->vhost_dev,
  44. vsock->conf.guest_cid);
  45. if (ret < 0) {
  46. return -errno;
  47. }
  48. return 0;
  49. }
  50. static int vhost_vsock_set_running(VHostVSock *vsock, int start)
  51. {
  52. const VhostOps *vhost_ops = vsock->vhost_dev.vhost_ops;
  53. int ret;
  54. if (!vhost_ops->vhost_vsock_set_running) {
  55. return -ENOSYS;
  56. }
  57. ret = vhost_ops->vhost_vsock_set_running(&vsock->vhost_dev, start);
  58. if (ret < 0) {
  59. return -errno;
  60. }
  61. return 0;
  62. }
  63. static void vhost_vsock_start(VirtIODevice *vdev)
  64. {
  65. VHostVSock *vsock = VHOST_VSOCK(vdev);
  66. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  67. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  68. int ret;
  69. int i;
  70. if (!k->set_guest_notifiers) {
  71. error_report("binding does not support guest notifiers");
  72. return;
  73. }
  74. ret = vhost_dev_enable_notifiers(&vsock->vhost_dev, vdev);
  75. if (ret < 0) {
  76. error_report("Error enabling host notifiers: %d", -ret);
  77. return;
  78. }
  79. ret = k->set_guest_notifiers(qbus->parent, vsock->vhost_dev.nvqs, true);
  80. if (ret < 0) {
  81. error_report("Error binding guest notifier: %d", -ret);
  82. goto err_host_notifiers;
  83. }
  84. vsock->vhost_dev.acked_features = vdev->guest_features;
  85. ret = vhost_dev_start(&vsock->vhost_dev, vdev);
  86. if (ret < 0) {
  87. error_report("Error starting vhost: %d", -ret);
  88. goto err_guest_notifiers;
  89. }
  90. ret = vhost_vsock_set_running(vsock, 1);
  91. if (ret < 0) {
  92. error_report("Error starting vhost vsock: %d", -ret);
  93. goto err_dev_start;
  94. }
  95. /* guest_notifier_mask/pending not used yet, so just unmask
  96. * everything here. virtio-pci will do the right thing by
  97. * enabling/disabling irqfd.
  98. */
  99. for (i = 0; i < vsock->vhost_dev.nvqs; i++) {
  100. vhost_virtqueue_mask(&vsock->vhost_dev, vdev, i, false);
  101. }
  102. return;
  103. err_dev_start:
  104. vhost_dev_stop(&vsock->vhost_dev, vdev);
  105. err_guest_notifiers:
  106. k->set_guest_notifiers(qbus->parent, vsock->vhost_dev.nvqs, false);
  107. err_host_notifiers:
  108. vhost_dev_disable_notifiers(&vsock->vhost_dev, vdev);
  109. }
  110. static void vhost_vsock_stop(VirtIODevice *vdev)
  111. {
  112. VHostVSock *vsock = VHOST_VSOCK(vdev);
  113. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
  114. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
  115. int ret;
  116. if (!k->set_guest_notifiers) {
  117. return;
  118. }
  119. ret = vhost_vsock_set_running(vsock, 0);
  120. if (ret < 0) {
  121. error_report("vhost vsock set running failed: %d", ret);
  122. return;
  123. }
  124. vhost_dev_stop(&vsock->vhost_dev, vdev);
  125. ret = k->set_guest_notifiers(qbus->parent, vsock->vhost_dev.nvqs, false);
  126. if (ret < 0) {
  127. error_report("vhost guest notifier cleanup failed: %d", ret);
  128. return;
  129. }
  130. vhost_dev_disable_notifiers(&vsock->vhost_dev, vdev);
  131. }
  132. static void vhost_vsock_set_status(VirtIODevice *vdev, uint8_t status)
  133. {
  134. VHostVSock *vsock = VHOST_VSOCK(vdev);
  135. bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
  136. if (!vdev->vm_running) {
  137. should_start = false;
  138. }
  139. if (vsock->vhost_dev.started == should_start) {
  140. return;
  141. }
  142. if (should_start) {
  143. vhost_vsock_start(vdev);
  144. } else {
  145. vhost_vsock_stop(vdev);
  146. }
  147. }
  148. static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
  149. uint64_t requested_features,
  150. Error **errp)
  151. {
  152. /* No feature bits used yet */
  153. return requested_features;
  154. }
  155. static void vhost_vsock_handle_output(VirtIODevice *vdev, VirtQueue *vq)
  156. {
  157. /* Do nothing */
  158. }
  159. static void vhost_vsock_guest_notifier_mask(VirtIODevice *vdev, int idx,
  160. bool mask)
  161. {
  162. VHostVSock *vsock = VHOST_VSOCK(vdev);
  163. vhost_virtqueue_mask(&vsock->vhost_dev, vdev, idx, mask);
  164. }
  165. static bool vhost_vsock_guest_notifier_pending(VirtIODevice *vdev, int idx)
  166. {
  167. VHostVSock *vsock = VHOST_VSOCK(vdev);
  168. return vhost_virtqueue_pending(&vsock->vhost_dev, idx);
  169. }
  170. static void vhost_vsock_send_transport_reset(VHostVSock *vsock)
  171. {
  172. VirtQueueElement *elem;
  173. VirtQueue *vq = vsock->event_vq;
  174. struct virtio_vsock_event event = {
  175. .id = cpu_to_le32(VIRTIO_VSOCK_EVENT_TRANSPORT_RESET),
  176. };
  177. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  178. if (!elem) {
  179. error_report("vhost-vsock missed transport reset event");
  180. return;
  181. }
  182. if (elem->out_num) {
  183. error_report("invalid vhost-vsock event virtqueue element with "
  184. "out buffers");
  185. goto out;
  186. }
  187. if (iov_from_buf(elem->in_sg, elem->in_num, 0,
  188. &event, sizeof(event)) != sizeof(event)) {
  189. error_report("vhost-vsock event virtqueue element is too short");
  190. goto out;
  191. }
  192. virtqueue_push(vq, elem, sizeof(event));
  193. virtio_notify(VIRTIO_DEVICE(vsock), vq);
  194. out:
  195. g_free(elem);
  196. }
  197. static void vhost_vsock_post_load_timer_cleanup(VHostVSock *vsock)
  198. {
  199. if (!vsock->post_load_timer) {
  200. return;
  201. }
  202. timer_del(vsock->post_load_timer);
  203. timer_free(vsock->post_load_timer);
  204. vsock->post_load_timer = NULL;
  205. }
  206. static void vhost_vsock_post_load_timer_cb(void *opaque)
  207. {
  208. VHostVSock *vsock = opaque;
  209. vhost_vsock_post_load_timer_cleanup(vsock);
  210. vhost_vsock_send_transport_reset(vsock);
  211. }
  212. static int vhost_vsock_pre_save(void *opaque)
  213. {
  214. VHostVSock *vsock = opaque;
  215. /* At this point, backend must be stopped, otherwise
  216. * it might keep writing to memory. */
  217. assert(!vsock->vhost_dev.started);
  218. return 0;
  219. }
  220. static int vhost_vsock_post_load(void *opaque, int version_id)
  221. {
  222. VHostVSock *vsock = opaque;
  223. VirtIODevice *vdev = VIRTIO_DEVICE(vsock);
  224. if (virtio_queue_get_addr(vdev, 2)) {
  225. /* Defer transport reset event to a vm clock timer so that virtqueue
  226. * changes happen after migration has completed.
  227. */
  228. assert(!vsock->post_load_timer);
  229. vsock->post_load_timer =
  230. timer_new_ns(QEMU_CLOCK_VIRTUAL,
  231. vhost_vsock_post_load_timer_cb,
  232. vsock);
  233. timer_mod(vsock->post_load_timer, 1);
  234. }
  235. return 0;
  236. }
  237. static const VMStateDescription vmstate_virtio_vhost_vsock = {
  238. .name = "virtio-vhost_vsock",
  239. .minimum_version_id = VHOST_VSOCK_SAVEVM_VERSION,
  240. .version_id = VHOST_VSOCK_SAVEVM_VERSION,
  241. .fields = (VMStateField[]) {
  242. VMSTATE_VIRTIO_DEVICE,
  243. VMSTATE_END_OF_LIST()
  244. },
  245. .pre_save = vhost_vsock_pre_save,
  246. .post_load = vhost_vsock_post_load,
  247. };
  248. static void vhost_vsock_device_realize(DeviceState *dev, Error **errp)
  249. {
  250. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  251. VHostVSock *vsock = VHOST_VSOCK(dev);
  252. int vhostfd;
  253. int ret;
  254. /* Refuse to use reserved CID numbers */
  255. if (vsock->conf.guest_cid <= 2) {
  256. error_setg(errp, "guest-cid property must be greater than 2");
  257. return;
  258. }
  259. if (vsock->conf.guest_cid > UINT32_MAX) {
  260. error_setg(errp, "guest-cid property must be a 32-bit number");
  261. return;
  262. }
  263. if (vsock->conf.vhostfd) {
  264. vhostfd = monitor_fd_param(cur_mon, vsock->conf.vhostfd, errp);
  265. if (vhostfd == -1) {
  266. error_prepend(errp, "vhost-vsock: unable to parse vhostfd: ");
  267. return;
  268. }
  269. } else {
  270. vhostfd = open("/dev/vhost-vsock", O_RDWR);
  271. if (vhostfd < 0) {
  272. error_setg_errno(errp, -errno,
  273. "vhost-vsock: failed to open vhost device");
  274. return;
  275. }
  276. }
  277. virtio_init(vdev, "vhost-vsock", VIRTIO_ID_VSOCK,
  278. sizeof(struct virtio_vsock_config));
  279. /* Receive and transmit queues belong to vhost */
  280. virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE, vhost_vsock_handle_output);
  281. virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE, vhost_vsock_handle_output);
  282. /* The event queue belongs to QEMU */
  283. vsock->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
  284. vhost_vsock_handle_output);
  285. vsock->vhost_dev.nvqs = ARRAY_SIZE(vsock->vhost_vqs);
  286. vsock->vhost_dev.vqs = vsock->vhost_vqs;
  287. ret = vhost_dev_init(&vsock->vhost_dev, (void *)(uintptr_t)vhostfd,
  288. VHOST_BACKEND_TYPE_KERNEL, 0);
  289. if (ret < 0) {
  290. error_setg_errno(errp, -ret, "vhost-vsock: vhost_dev_init failed");
  291. goto err_virtio;
  292. }
  293. ret = vhost_vsock_set_guest_cid(vsock);
  294. if (ret < 0) {
  295. error_setg_errno(errp, -ret, "vhost-vsock: unable to set guest cid");
  296. goto err_vhost_dev;
  297. }
  298. vsock->post_load_timer = NULL;
  299. return;
  300. err_vhost_dev:
  301. vhost_dev_cleanup(&vsock->vhost_dev);
  302. err_virtio:
  303. virtio_cleanup(vdev);
  304. close(vhostfd);
  305. return;
  306. }
  307. static void vhost_vsock_device_unrealize(DeviceState *dev, Error **errp)
  308. {
  309. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  310. VHostVSock *vsock = VHOST_VSOCK(dev);
  311. vhost_vsock_post_load_timer_cleanup(vsock);
  312. /* This will stop vhost backend if appropriate. */
  313. vhost_vsock_set_status(vdev, 0);
  314. vhost_dev_cleanup(&vsock->vhost_dev);
  315. virtio_cleanup(vdev);
  316. }
  317. static Property vhost_vsock_properties[] = {
  318. DEFINE_PROP_UINT64("guest-cid", VHostVSock, conf.guest_cid, 0),
  319. DEFINE_PROP_STRING("vhostfd", VHostVSock, conf.vhostfd),
  320. DEFINE_PROP_END_OF_LIST(),
  321. };
  322. static void vhost_vsock_class_init(ObjectClass *klass, void *data)
  323. {
  324. DeviceClass *dc = DEVICE_CLASS(klass);
  325. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  326. dc->props = vhost_vsock_properties;
  327. dc->vmsd = &vmstate_virtio_vhost_vsock;
  328. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  329. vdc->realize = vhost_vsock_device_realize;
  330. vdc->unrealize = vhost_vsock_device_unrealize;
  331. vdc->get_features = vhost_vsock_get_features;
  332. vdc->get_config = vhost_vsock_get_config;
  333. vdc->set_status = vhost_vsock_set_status;
  334. vdc->guest_notifier_mask = vhost_vsock_guest_notifier_mask;
  335. vdc->guest_notifier_pending = vhost_vsock_guest_notifier_pending;
  336. }
  337. static const TypeInfo vhost_vsock_info = {
  338. .name = TYPE_VHOST_VSOCK,
  339. .parent = TYPE_VIRTIO_DEVICE,
  340. .instance_size = sizeof(VHostVSock),
  341. .class_init = vhost_vsock_class_init,
  342. };
  343. static void vhost_vsock_register_types(void)
  344. {
  345. type_register_static(&vhost_vsock_info);
  346. }
  347. type_init(vhost_vsock_register_types)