2
0

virtio-9p-device.c 7.5 KB

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