vhost-user-scsi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * vhost-user-scsi host device
  3. *
  4. * Copyright (c) 2016 Nutanix Inc. All rights reserved.
  5. *
  6. * Author:
  7. * Felipe Franciosi <felipe@nutanix.com>
  8. *
  9. * This work is largely based on the "vhost-scsi" implementation by:
  10. * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  11. * Nicholas Bellinger <nab@risingtidesystems.com>
  12. *
  13. * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  14. * See the COPYING.LIB file in the top-level directory.
  15. *
  16. */
  17. #include "qemu/osdep.h"
  18. #include "qapi/error.h"
  19. #include "qemu/error-report.h"
  20. #include "hw/fw-path-provider.h"
  21. #include "hw/qdev-core.h"
  22. #include "hw/qdev-properties.h"
  23. #include "hw/qdev-properties-system.h"
  24. #include "hw/virtio/vhost.h"
  25. #include "hw/virtio/vhost-backend.h"
  26. #include "hw/virtio/vhost-user-scsi.h"
  27. #include "hw/virtio/virtio.h"
  28. #include "chardev/char-fe.h"
  29. #include "system/system.h"
  30. /* Features supported by the host application */
  31. static const int user_feature_bits[] = {
  32. VIRTIO_F_NOTIFY_ON_EMPTY,
  33. VIRTIO_RING_F_INDIRECT_DESC,
  34. VIRTIO_RING_F_EVENT_IDX,
  35. VIRTIO_SCSI_F_HOTPLUG,
  36. VIRTIO_F_RING_RESET,
  37. VIRTIO_F_IN_ORDER,
  38. VIRTIO_F_NOTIFICATION_DATA,
  39. VHOST_INVALID_FEATURE_BIT
  40. };
  41. static int vhost_user_scsi_start(VHostUserSCSI *s, Error **errp)
  42. {
  43. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  44. int ret;
  45. ret = vhost_scsi_common_start(vsc, errp);
  46. s->started_vu = !(ret < 0);
  47. return ret;
  48. }
  49. static void vhost_user_scsi_stop(VHostUserSCSI *s)
  50. {
  51. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  52. if (!s->started_vu) {
  53. return;
  54. }
  55. s->started_vu = false;
  56. vhost_scsi_common_stop(vsc);
  57. }
  58. static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status)
  59. {
  60. VHostUserSCSI *s = (VHostUserSCSI *)vdev;
  61. DeviceState *dev = DEVICE(vdev);
  62. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  63. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  64. bool should_start = virtio_device_should_start(vdev, status);
  65. Error *local_err = NULL;
  66. int ret;
  67. if (!s->connected) {
  68. return;
  69. }
  70. if (vhost_dev_is_started(&vsc->dev) == should_start) {
  71. return;
  72. }
  73. if (should_start) {
  74. ret = vhost_user_scsi_start(s, &local_err);
  75. if (ret < 0) {
  76. error_reportf_err(local_err,
  77. "unable to start vhost-user-scsi: %s: ",
  78. strerror(-ret));
  79. qemu_chr_fe_disconnect(&vs->conf.chardev);
  80. }
  81. } else {
  82. vhost_user_scsi_stop(s);
  83. }
  84. }
  85. static void vhost_user_scsi_handle_output(VirtIODevice *vdev, VirtQueue *vq)
  86. {
  87. VHostUserSCSI *s = (VHostUserSCSI *)vdev;
  88. DeviceState *dev = DEVICE(vdev);
  89. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  90. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  91. Error *local_err = NULL;
  92. int i, ret;
  93. if (!vdev->start_on_kick) {
  94. return;
  95. }
  96. if (!s->connected) {
  97. return;
  98. }
  99. if (vhost_dev_is_started(&vsc->dev)) {
  100. return;
  101. }
  102. /*
  103. * Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
  104. * vhost here instead of waiting for .set_status().
  105. */
  106. ret = vhost_user_scsi_start(s, &local_err);
  107. if (ret < 0) {
  108. error_reportf_err(local_err, "vhost-user-scsi: vhost start failed: ");
  109. qemu_chr_fe_disconnect(&vs->conf.chardev);
  110. return;
  111. }
  112. /* Kick right away to begin processing requests already in vring */
  113. for (i = 0; i < vsc->dev.nvqs; i++) {
  114. VirtQueue *kick_vq = virtio_get_queue(vdev, i);
  115. if (!virtio_queue_get_desc_addr(vdev, i)) {
  116. continue;
  117. }
  118. event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
  119. }
  120. }
  121. static int vhost_user_scsi_connect(DeviceState *dev, Error **errp)
  122. {
  123. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  124. VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
  125. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  126. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  127. int ret = 0;
  128. if (s->connected) {
  129. return 0;
  130. }
  131. vsc->dev.num_queues = vs->conf.num_queues;
  132. vsc->dev.nvqs = VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
  133. vsc->dev.vqs = s->vhost_vqs;
  134. vsc->dev.vq_index = 0;
  135. vsc->dev.backend_features = 0;
  136. ret = vhost_dev_init(&vsc->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
  137. errp);
  138. if (ret < 0) {
  139. return ret;
  140. }
  141. s->connected = true;
  142. /* restore vhost state */
  143. if (virtio_device_started(vdev, vdev->status)) {
  144. ret = vhost_user_scsi_start(s, errp);
  145. }
  146. return ret;
  147. }
  148. static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event);
  149. static void vhost_user_scsi_disconnect(DeviceState *dev)
  150. {
  151. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  152. VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
  153. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  154. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  155. if (!s->connected) {
  156. goto done;
  157. }
  158. s->connected = false;
  159. vhost_user_scsi_stop(s);
  160. vhost_dev_cleanup(&vsc->dev);
  161. done:
  162. /* Re-instate the event handler for new connections */
  163. qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL,
  164. vhost_user_scsi_event, NULL, dev, NULL, true);
  165. }
  166. static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event)
  167. {
  168. DeviceState *dev = opaque;
  169. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  170. VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
  171. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  172. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  173. Error *local_err = NULL;
  174. switch (event) {
  175. case CHR_EVENT_OPENED:
  176. if (vhost_user_scsi_connect(dev, &local_err) < 0) {
  177. error_report_err(local_err);
  178. qemu_chr_fe_disconnect(&vs->conf.chardev);
  179. return;
  180. }
  181. break;
  182. case CHR_EVENT_CLOSED:
  183. /* defer close until later to avoid circular close */
  184. vhost_user_async_close(dev, &vs->conf.chardev, &vsc->dev,
  185. vhost_user_scsi_disconnect);
  186. break;
  187. case CHR_EVENT_BREAK:
  188. case CHR_EVENT_MUX_IN:
  189. case CHR_EVENT_MUX_OUT:
  190. /* Ignore */
  191. break;
  192. }
  193. }
  194. static int vhost_user_scsi_realize_connect(VHostUserSCSI *s, Error **errp)
  195. {
  196. DeviceState *dev = DEVICE(s);
  197. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  198. int ret;
  199. s->connected = false;
  200. ret = qemu_chr_fe_wait_connected(&vs->conf.chardev, errp);
  201. if (ret < 0) {
  202. return ret;
  203. }
  204. ret = vhost_user_scsi_connect(dev, errp);
  205. if (ret < 0) {
  206. qemu_chr_fe_disconnect(&vs->conf.chardev);
  207. return ret;
  208. }
  209. assert(s->connected);
  210. return 0;
  211. }
  212. static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
  213. {
  214. ERRP_GUARD();
  215. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  216. VHostUserSCSI *s = VHOST_USER_SCSI(dev);
  217. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  218. Error *err = NULL;
  219. int ret;
  220. int retries = VU_REALIZE_CONN_RETRIES;
  221. if (!vs->conf.chardev.chr) {
  222. error_setg(errp, "vhost-user-scsi: missing chardev");
  223. return;
  224. }
  225. virtio_scsi_common_realize(dev, vhost_user_scsi_handle_output,
  226. vhost_user_scsi_handle_output,
  227. vhost_user_scsi_handle_output, &err);
  228. if (err != NULL) {
  229. error_propagate(errp, err);
  230. return;
  231. }
  232. if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
  233. goto free_virtio;
  234. }
  235. vsc->inflight = g_new0(struct vhost_inflight, 1);
  236. s->vhost_vqs = g_new0(struct vhost_virtqueue,
  237. VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues);
  238. assert(!*errp);
  239. do {
  240. if (*errp) {
  241. error_prepend(errp, "Reconnecting after error: ");
  242. error_report_err(*errp);
  243. *errp = NULL;
  244. }
  245. ret = vhost_user_scsi_realize_connect(s, errp);
  246. } while (ret < 0 && retries--);
  247. if (ret < 0) {
  248. goto free_vhost;
  249. }
  250. /* we're fully initialized, now we can operate, so add the handler */
  251. qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL,
  252. vhost_user_scsi_event, NULL, (void *)dev,
  253. NULL, true);
  254. /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
  255. vsc->channel = 0;
  256. vsc->lun = 0;
  257. vsc->target = vs->conf.boot_tpgt;
  258. return;
  259. free_vhost:
  260. g_free(s->vhost_vqs);
  261. s->vhost_vqs = NULL;
  262. g_free(vsc->inflight);
  263. vsc->inflight = NULL;
  264. vhost_user_cleanup(&s->vhost_user);
  265. free_virtio:
  266. virtio_scsi_common_unrealize(dev);
  267. }
  268. static void vhost_user_scsi_unrealize(DeviceState *dev)
  269. {
  270. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  271. VHostUserSCSI *s = VHOST_USER_SCSI(dev);
  272. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  273. VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
  274. /* This will stop the vhost backend. */
  275. vhost_user_scsi_set_status(vdev, 0);
  276. qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, NULL, NULL, NULL,
  277. NULL, false);
  278. vhost_dev_cleanup(&vsc->dev);
  279. g_free(s->vhost_vqs);
  280. s->vhost_vqs = NULL;
  281. vhost_dev_free_inflight(vsc->inflight);
  282. g_free(vsc->inflight);
  283. vsc->inflight = NULL;
  284. vhost_user_cleanup(&s->vhost_user);
  285. virtio_scsi_common_unrealize(dev);
  286. }
  287. static const Property vhost_user_scsi_properties[] = {
  288. DEFINE_PROP_CHR("chardev", VirtIOSCSICommon, conf.chardev),
  289. DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
  290. DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues,
  291. VIRTIO_SCSI_AUTO_NUM_QUEUES),
  292. DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
  293. 128),
  294. DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
  295. 0xFFFF),
  296. DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
  297. DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features,
  298. VIRTIO_SCSI_F_HOTPLUG,
  299. true),
  300. DEFINE_PROP_BIT64("param_change", VHostSCSICommon, host_features,
  301. VIRTIO_SCSI_F_CHANGE,
  302. true),
  303. DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
  304. VIRTIO_SCSI_F_T10_PI,
  305. false),
  306. };
  307. static void vhost_user_scsi_reset(VirtIODevice *vdev)
  308. {
  309. VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
  310. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
  311. vhost_dev_free_inflight(vsc->inflight);
  312. }
  313. static struct vhost_dev *vhost_user_scsi_get_vhost(VirtIODevice *vdev)
  314. {
  315. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
  316. return &vsc->dev;
  317. }
  318. static const VMStateDescription vmstate_vhost_scsi = {
  319. .name = "virtio-scsi",
  320. .minimum_version_id = 1,
  321. .version_id = 1,
  322. .fields = (const VMStateField[]) {
  323. VMSTATE_VIRTIO_DEVICE,
  324. VMSTATE_END_OF_LIST()
  325. },
  326. };
  327. static void vhost_user_scsi_class_init(ObjectClass *klass, void *data)
  328. {
  329. DeviceClass *dc = DEVICE_CLASS(klass);
  330. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  331. FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
  332. device_class_set_props(dc, vhost_user_scsi_properties);
  333. dc->vmsd = &vmstate_vhost_scsi;
  334. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  335. vdc->realize = vhost_user_scsi_realize;
  336. vdc->unrealize = vhost_user_scsi_unrealize;
  337. vdc->get_features = vhost_scsi_common_get_features;
  338. vdc->set_config = vhost_scsi_common_set_config;
  339. vdc->set_status = vhost_user_scsi_set_status;
  340. fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
  341. vdc->reset = vhost_user_scsi_reset;
  342. vdc->get_vhost = vhost_user_scsi_get_vhost;
  343. }
  344. static void vhost_user_scsi_instance_init(Object *obj)
  345. {
  346. VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
  347. vsc->feature_bits = user_feature_bits;
  348. /* Add the bootindex property for this object */
  349. device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
  350. DEVICE(vsc));
  351. }
  352. static const TypeInfo vhost_user_scsi_info = {
  353. .name = TYPE_VHOST_USER_SCSI,
  354. .parent = TYPE_VHOST_SCSI_COMMON,
  355. .instance_size = sizeof(VHostUserSCSI),
  356. .class_init = vhost_user_scsi_class_init,
  357. .instance_init = vhost_user_scsi_instance_init,
  358. .interfaces = (InterfaceInfo[]) {
  359. { TYPE_FW_PATH_PROVIDER },
  360. { }
  361. },
  362. };
  363. static void virtio_register_types(void)
  364. {
  365. type_register_static(&vhost_user_scsi_info);
  366. }
  367. type_init(virtio_register_types)