virtio-9p-device.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Virtio 9p backend
  3. *
  4. * Copyright IBM, Corp. 2010
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. /*
  14. * Not so fast! You might want to read the 9p developer docs first:
  15. * https://wiki.qemu.org/Documentation/9p
  16. */
  17. #include "qemu/osdep.h"
  18. #include "hw/virtio/virtio.h"
  19. #include "qemu/sockets.h"
  20. #include "virtio-9p.h"
  21. #include "fsdev/qemu-fsdev.h"
  22. #include "coth.h"
  23. #include "hw/qdev-properties.h"
  24. #include "hw/virtio/virtio-access.h"
  25. #include "qemu/iov.h"
  26. #include "qemu/module.h"
  27. #include "system/qtest.h"
  28. static void virtio_9p_push_and_notify(V9fsPDU *pdu)
  29. {
  30. V9fsState *s = pdu->s;
  31. V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
  32. VirtQueueElement *elem = v->elems[pdu->idx];
  33. /* push onto queue and notify */
  34. virtqueue_push(v->vq, elem, pdu->size);
  35. g_free(elem);
  36. v->elems[pdu->idx] = NULL;
  37. /* FIXME: we should batch these completions */
  38. virtio_notify(VIRTIO_DEVICE(v), v->vq);
  39. }
  40. static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
  41. {
  42. V9fsVirtioState *v = (V9fsVirtioState *)vdev;
  43. V9fsState *s = &v->state;
  44. V9fsPDU *pdu;
  45. ssize_t len;
  46. VirtQueueElement *elem;
  47. while ((pdu = pdu_alloc(s))) {
  48. P9MsgHeader out;
  49. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  50. if (!elem) {
  51. goto out_free_pdu;
  52. }
  53. if (iov_size(elem->in_sg, elem->in_num) < 7) {
  54. virtio_error(vdev,
  55. "The guest sent a VirtFS request without space for "
  56. "the reply");
  57. goto out_free_req;
  58. }
  59. len = iov_to_buf(elem->out_sg, elem->out_num, 0, &out, 7);
  60. if (len != 7) {
  61. virtio_error(vdev, "The guest sent a malformed VirtFS request: "
  62. "header size is %zd, should be 7", len);
  63. goto out_free_req;
  64. }
  65. v->elems[pdu->idx] = elem;
  66. pdu_submit(pdu, &out);
  67. }
  68. return;
  69. out_free_req:
  70. virtqueue_detach_element(vq, elem, 0);
  71. g_free(elem);
  72. out_free_pdu:
  73. pdu_free(pdu);
  74. }
  75. static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features,
  76. Error **errp)
  77. {
  78. virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG);
  79. return features;
  80. }
  81. static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config)
  82. {
  83. int len;
  84. struct virtio_9p_config *cfg;
  85. V9fsVirtioState *v = VIRTIO_9P(vdev);
  86. V9fsState *s = &v->state;
  87. len = strlen(s->tag);
  88. cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
  89. virtio_stw_p(vdev, &cfg->tag_len, len);
  90. /* We don't copy the terminating null to config space */
  91. memcpy(cfg->tag, s->tag, len);
  92. memcpy(config, cfg, v->config_size);
  93. g_free(cfg);
  94. }
  95. static void virtio_9p_reset(VirtIODevice *vdev)
  96. {
  97. V9fsVirtioState *v = (V9fsVirtioState *)vdev;
  98. v9fs_reset(&v->state);
  99. }
  100. static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset,
  101. const char *fmt, va_list ap)
  102. {
  103. V9fsState *s = pdu->s;
  104. V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
  105. VirtQueueElement *elem = v->elems[pdu->idx];
  106. ssize_t ret;
  107. ret = v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap);
  108. if (ret < 0) {
  109. VirtIODevice *vdev = VIRTIO_DEVICE(v);
  110. virtio_error(vdev, "Failed to encode VirtFS reply type %d",
  111. pdu->id + 1);
  112. }
  113. return ret;
  114. }
  115. static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset,
  116. const char *fmt, va_list ap)
  117. {
  118. V9fsState *s = pdu->s;
  119. V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
  120. VirtQueueElement *elem = v->elems[pdu->idx];
  121. ssize_t ret;
  122. ret = v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap);
  123. if (ret < 0) {
  124. VirtIODevice *vdev = VIRTIO_DEVICE(v);
  125. virtio_error(vdev, "Failed to decode VirtFS request type %d", pdu->id);
  126. }
  127. return ret;
  128. }
  129. static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
  130. unsigned int *pniov, size_t size)
  131. {
  132. V9fsState *s = pdu->s;
  133. V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
  134. VirtQueueElement *elem = v->elems[pdu->idx];
  135. size_t buf_size = iov_size(elem->in_sg, elem->in_num);
  136. if (buf_size < size) {
  137. VirtIODevice *vdev = VIRTIO_DEVICE(v);
  138. virtio_error(vdev,
  139. "VirtFS reply type %d needs %zu bytes, buffer has %zu",
  140. pdu->id + 1, size, buf_size);
  141. }
  142. *piov = elem->in_sg;
  143. *pniov = elem->in_num;
  144. }
  145. static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov,
  146. unsigned int *pniov, size_t size)
  147. {
  148. V9fsState *s = pdu->s;
  149. V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
  150. VirtQueueElement *elem = v->elems[pdu->idx];
  151. size_t buf_size = iov_size(elem->out_sg, elem->out_num);
  152. if (buf_size < size) {
  153. VirtIODevice *vdev = VIRTIO_DEVICE(v);
  154. virtio_error(vdev,
  155. "VirtFS request type %d needs %zu bytes, buffer has %zu",
  156. pdu->id, size, buf_size);
  157. }
  158. *piov = elem->out_sg;
  159. *pniov = elem->out_num;
  160. }
  161. static const V9fsTransport virtio_9p_transport = {
  162. .pdu_vmarshal = virtio_pdu_vmarshal,
  163. .pdu_vunmarshal = virtio_pdu_vunmarshal,
  164. .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu,
  165. .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu,
  166. .push_and_notify = virtio_9p_push_and_notify,
  167. };
  168. static void virtio_9p_device_realize(DeviceState *dev, Error **errp)
  169. {
  170. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  171. V9fsVirtioState *v = VIRTIO_9P(dev);
  172. V9fsState *s = &v->state;
  173. FsDriverEntry *fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
  174. if (qtest_enabled() && fse) {
  175. fse->export_flags |= V9FS_NO_PERF_WARN;
  176. }
  177. if (v9fs_device_realize_common(s, &virtio_9p_transport, errp)) {
  178. return;
  179. }
  180. v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag);
  181. virtio_init(vdev, VIRTIO_ID_9P, v->config_size);
  182. v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output);
  183. }
  184. static void virtio_9p_device_unrealize(DeviceState *dev)
  185. {
  186. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  187. V9fsVirtioState *v = VIRTIO_9P(dev);
  188. V9fsState *s = &v->state;
  189. virtio_delete_queue(v->vq);
  190. virtio_cleanup(vdev);
  191. v9fs_device_unrealize_common(s);
  192. }
  193. /* virtio-9p device */
  194. static const VMStateDescription vmstate_virtio_9p = {
  195. .name = "virtio-9p",
  196. .minimum_version_id = 1,
  197. .version_id = 1,
  198. .fields = (const VMStateField[]) {
  199. VMSTATE_VIRTIO_DEVICE,
  200. VMSTATE_END_OF_LIST()
  201. },
  202. };
  203. static const Property virtio_9p_properties[] = {
  204. DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag),
  205. DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id),
  206. };
  207. static void virtio_9p_class_init(ObjectClass *klass, void *data)
  208. {
  209. DeviceClass *dc = DEVICE_CLASS(klass);
  210. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  211. device_class_set_props(dc, virtio_9p_properties);
  212. dc->vmsd = &vmstate_virtio_9p;
  213. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  214. vdc->realize = virtio_9p_device_realize;
  215. vdc->unrealize = virtio_9p_device_unrealize;
  216. vdc->get_features = virtio_9p_get_features;
  217. vdc->get_config = virtio_9p_get_config;
  218. vdc->reset = virtio_9p_reset;
  219. }
  220. static const TypeInfo virtio_device_info = {
  221. .name = TYPE_VIRTIO_9P,
  222. .parent = TYPE_VIRTIO_DEVICE,
  223. .instance_size = sizeof(V9fsVirtioState),
  224. .class_init = virtio_9p_class_init,
  225. };
  226. static void virtio_9p_register_types(void)
  227. {
  228. type_register_static(&virtio_device_info);
  229. }
  230. type_init(virtio_9p_register_types)