1f4f61d27SAneesh Kumar K.V /* 2f4f61d27SAneesh Kumar K.V * Virtio 9p backend 3f4f61d27SAneesh Kumar K.V * 4f4f61d27SAneesh Kumar K.V * Copyright IBM, Corp. 2010 5f4f61d27SAneesh Kumar K.V * 6f4f61d27SAneesh Kumar K.V * Authors: 7f4f61d27SAneesh Kumar K.V * Anthony Liguori <aliguori@us.ibm.com> 8f4f61d27SAneesh Kumar K.V * 9f4f61d27SAneesh Kumar K.V * This work is licensed under the terms of the GNU GPL, version 2. See 10f4f61d27SAneesh Kumar K.V * the COPYING file in the top-level directory. 11f4f61d27SAneesh Kumar K.V * 12f4f61d27SAneesh Kumar K.V */ 13f4f61d27SAneesh Kumar K.V 146f569084SChristian Schoenebeck /* 156f569084SChristian Schoenebeck * Not so fast! You might want to read the 9p developer docs first: 166f569084SChristian Schoenebeck * https://wiki.qemu.org/Documentation/9p 176f569084SChristian Schoenebeck */ 186f569084SChristian Schoenebeck 199b8bfe21SPeter Maydell #include "qemu/osdep.h" 200d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h" 211de7afc9SPaolo Bonzini #include "qemu/sockets.h" 22f4f61d27SAneesh Kumar K.V #include "virtio-9p.h" 23f4f61d27SAneesh Kumar K.V #include "fsdev/qemu-fsdev.h" 24fe52840cSWei Liu #include "coth.h" 25a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 26d64ccb91SGreg Kurz #include "hw/virtio/virtio-access.h" 270192cc5dSWei Liu #include "qemu/iov.h" 280b8fa32fSMarkus Armbruster #include "qemu/module.h" 29*32cad1ffSPhilippe Mathieu-Daudé #include "system/qtest.h" 30f4f61d27SAneesh Kumar K.V 31ea83441cSStefano Stabellini static void virtio_9p_push_and_notify(V9fsPDU *pdu) 320d3716b4SWei Liu { 330d3716b4SWei Liu V9fsState *s = pdu->s; 3400588a0aSWei Liu V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 3551b19ebeSPaolo Bonzini VirtQueueElement *elem = v->elems[pdu->idx]; 360d3716b4SWei Liu 370d3716b4SWei Liu /* push onto queue and notify */ 3800588a0aSWei Liu virtqueue_push(v->vq, elem, pdu->size); 3951b19ebeSPaolo Bonzini g_free(elem); 4051b19ebeSPaolo Bonzini v->elems[pdu->idx] = NULL; 410d3716b4SWei Liu 420d3716b4SWei Liu /* FIXME: we should batch these completions */ 4300588a0aSWei Liu virtio_notify(VIRTIO_DEVICE(v), v->vq); 440d3716b4SWei Liu } 450d3716b4SWei Liu 460192cc5dSWei Liu static void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq) 470192cc5dSWei Liu { 4800588a0aSWei Liu V9fsVirtioState *v = (V9fsVirtioState *)vdev; 4900588a0aSWei Liu V9fsState *s = &v->state; 500192cc5dSWei Liu V9fsPDU *pdu; 510192cc5dSWei Liu ssize_t len; 52d3d74d6fSGreg Kurz VirtQueueElement *elem; 530192cc5dSWei Liu 5400588a0aSWei Liu while ((pdu = pdu_alloc(s))) { 55c9fb47e7SStefano Stabellini P9MsgHeader out; 560192cc5dSWei Liu 5751b19ebeSPaolo Bonzini elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 5851b19ebeSPaolo Bonzini if (!elem) { 59d3d74d6fSGreg Kurz goto out_free_pdu; 6000588a0aSWei Liu } 6100588a0aSWei Liu 62a4d99854SGreg Kurz if (iov_size(elem->in_sg, elem->in_num) < 7) { 63d3d74d6fSGreg Kurz virtio_error(vdev, 64d3d74d6fSGreg Kurz "The guest sent a VirtFS request without space for " 65d3d74d6fSGreg Kurz "the reply"); 66d3d74d6fSGreg Kurz goto out_free_req; 67d3d74d6fSGreg Kurz } 680192cc5dSWei Liu 69a4d99854SGreg Kurz len = iov_to_buf(elem->out_sg, elem->out_num, 0, &out, 7); 70a4d99854SGreg Kurz if (len != 7) { 71d3d74d6fSGreg Kurz virtio_error(vdev, "The guest sent a malformed VirtFS request: " 72d3d74d6fSGreg Kurz "header size is %zd, should be 7", len); 73d3d74d6fSGreg Kurz goto out_free_req; 74d3d74d6fSGreg Kurz } 750192cc5dSWei Liu 763a21fb2aSGreg Kurz v->elems[pdu->idx] = elem; 773a21fb2aSGreg Kurz 78506f3275SGreg Kurz pdu_submit(pdu, &out); 790192cc5dSWei Liu } 80d3d74d6fSGreg Kurz 81d3d74d6fSGreg Kurz return; 82d3d74d6fSGreg Kurz 83d3d74d6fSGreg Kurz out_free_req: 84d3d74d6fSGreg Kurz virtqueue_detach_element(vq, elem, 0); 85d3d74d6fSGreg Kurz g_free(elem); 86d3d74d6fSGreg Kurz out_free_pdu: 87d3d74d6fSGreg Kurz pdu_free(pdu); 880192cc5dSWei Liu } 890192cc5dSWei Liu 909d5b731dSJason Wang static uint64_t virtio_9p_get_features(VirtIODevice *vdev, uint64_t features, 919d5b731dSJason Wang Error **errp) 92f4f61d27SAneesh Kumar K.V { 930cd09c3aSCornelia Huck virtio_add_feature(&features, VIRTIO_9P_MOUNT_TAG); 94f4f61d27SAneesh Kumar K.V return features; 95f4f61d27SAneesh Kumar K.V } 96f4f61d27SAneesh Kumar K.V 97f4f61d27SAneesh Kumar K.V static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 98f4f61d27SAneesh Kumar K.V { 99e9a0152bSAneesh Kumar K.V int len; 100f4f61d27SAneesh Kumar K.V struct virtio_9p_config *cfg; 10100588a0aSWei Liu V9fsVirtioState *v = VIRTIO_9P(vdev); 10200588a0aSWei Liu V9fsState *s = &v->state; 103f4f61d27SAneesh Kumar K.V 104e9a0152bSAneesh Kumar K.V len = strlen(s->tag); 105e9a0152bSAneesh Kumar K.V cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); 106d64ccb91SGreg Kurz virtio_stw_p(vdev, &cfg->tag_len, len); 107e9a0152bSAneesh Kumar K.V /* We don't copy the terminating null to config space */ 108e9a0152bSAneesh Kumar K.V memcpy(cfg->tag, s->tag, len); 10900588a0aSWei Liu memcpy(config, cfg, v->config_size); 1107267c094SAnthony Liguori g_free(cfg); 111f4f61d27SAneesh Kumar K.V } 112f4f61d27SAneesh Kumar K.V 1130e44a0fdSGreg Kurz static void virtio_9p_reset(VirtIODevice *vdev) 1140e44a0fdSGreg Kurz { 1150e44a0fdSGreg Kurz V9fsVirtioState *v = (V9fsVirtioState *)vdev; 1160e44a0fdSGreg Kurz 1170e44a0fdSGreg Kurz v9fs_reset(&v->state); 1180e44a0fdSGreg Kurz } 1190e44a0fdSGreg Kurz 120ea83441cSStefano Stabellini static ssize_t virtio_pdu_vmarshal(V9fsPDU *pdu, size_t offset, 121fe9fa96dSWei Liu const char *fmt, va_list ap) 122fe9fa96dSWei Liu { 12300588a0aSWei Liu V9fsState *s = pdu->s; 12400588a0aSWei Liu V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 12551b19ebeSPaolo Bonzini VirtQueueElement *elem = v->elems[pdu->idx]; 1268d37de41SGreg Kurz ssize_t ret; 12700588a0aSWei Liu 1288d37de41SGreg Kurz ret = v9fs_iov_vmarshal(elem->in_sg, elem->in_num, offset, 1, fmt, ap); 1298d37de41SGreg Kurz if (ret < 0) { 1308d37de41SGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(v); 1318d37de41SGreg Kurz 1328d37de41SGreg Kurz virtio_error(vdev, "Failed to encode VirtFS reply type %d", 1338d37de41SGreg Kurz pdu->id + 1); 1348d37de41SGreg Kurz } 1358d37de41SGreg Kurz return ret; 136fe9fa96dSWei Liu } 137fe9fa96dSWei Liu 138ea83441cSStefano Stabellini static ssize_t virtio_pdu_vunmarshal(V9fsPDU *pdu, size_t offset, 139fe9fa96dSWei Liu const char *fmt, va_list ap) 140fe9fa96dSWei Liu { 14100588a0aSWei Liu V9fsState *s = pdu->s; 14200588a0aSWei Liu V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 14351b19ebeSPaolo Bonzini VirtQueueElement *elem = v->elems[pdu->idx]; 1448d37de41SGreg Kurz ssize_t ret; 14500588a0aSWei Liu 1468d37de41SGreg Kurz ret = v9fs_iov_vunmarshal(elem->out_sg, elem->out_num, offset, 1, fmt, ap); 1478d37de41SGreg Kurz if (ret < 0) { 1488d37de41SGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(v); 1498d37de41SGreg Kurz 1508d37de41SGreg Kurz virtio_error(vdev, "Failed to decode VirtFS request type %d", pdu->id); 1518d37de41SGreg Kurz } 1528d37de41SGreg Kurz return ret; 153fe9fa96dSWei Liu } 154fe9fa96dSWei Liu 15588da0b03SStefano Stabellini static void virtio_init_in_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, 156cf45183bSStefano Stabellini unsigned int *pniov, size_t size) 157592707afSWei Liu { 15800588a0aSWei Liu V9fsState *s = pdu->s; 15900588a0aSWei Liu V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 16051b19ebeSPaolo Bonzini VirtQueueElement *elem = v->elems[pdu->idx]; 1618d37de41SGreg Kurz size_t buf_size = iov_size(elem->in_sg, elem->in_num); 1628d37de41SGreg Kurz 163cf45183bSStefano Stabellini if (buf_size < size) { 1648d37de41SGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(v); 1658d37de41SGreg Kurz 1668d37de41SGreg Kurz virtio_error(vdev, 167cf45183bSStefano Stabellini "VirtFS reply type %d needs %zu bytes, buffer has %zu", 168cf45183bSStefano Stabellini pdu->id + 1, size, buf_size); 1698d37de41SGreg Kurz } 17000588a0aSWei Liu 17100588a0aSWei Liu *piov = elem->in_sg; 17200588a0aSWei Liu *pniov = elem->in_num; 173592707afSWei Liu } 17488da0b03SStefano Stabellini 17588da0b03SStefano Stabellini static void virtio_init_out_iov_from_pdu(V9fsPDU *pdu, struct iovec **piov, 1768d37de41SGreg Kurz unsigned int *pniov, size_t size) 17788da0b03SStefano Stabellini { 17888da0b03SStefano Stabellini V9fsState *s = pdu->s; 17988da0b03SStefano Stabellini V9fsVirtioState *v = container_of(s, V9fsVirtioState, state); 18088da0b03SStefano Stabellini VirtQueueElement *elem = v->elems[pdu->idx]; 1818d37de41SGreg Kurz size_t buf_size = iov_size(elem->out_sg, elem->out_num); 1828d37de41SGreg Kurz 1838d37de41SGreg Kurz if (buf_size < size) { 1848d37de41SGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(v); 1858d37de41SGreg Kurz 1868d37de41SGreg Kurz virtio_error(vdev, 1878d37de41SGreg Kurz "VirtFS request type %d needs %zu bytes, buffer has %zu", 1888d37de41SGreg Kurz pdu->id, size, buf_size); 1898d37de41SGreg Kurz } 19088da0b03SStefano Stabellini 19188da0b03SStefano Stabellini *piov = elem->out_sg; 19288da0b03SStefano Stabellini *pniov = elem->out_num; 193592707afSWei Liu } 194592707afSWei Liu 1958e71b96cSGreg Kurz static const V9fsTransport virtio_9p_transport = { 196ea83441cSStefano Stabellini .pdu_vmarshal = virtio_pdu_vmarshal, 197ea83441cSStefano Stabellini .pdu_vunmarshal = virtio_pdu_vunmarshal, 19888da0b03SStefano Stabellini .init_in_iov_from_pdu = virtio_init_in_iov_from_pdu, 19988da0b03SStefano Stabellini .init_out_iov_from_pdu = virtio_init_out_iov_from_pdu, 200ea83441cSStefano Stabellini .push_and_notify = virtio_9p_push_and_notify, 201ea83441cSStefano Stabellini }; 202ea83441cSStefano Stabellini 203bd3be4dbSGreg Kurz static void virtio_9p_device_realize(DeviceState *dev, Error **errp) 204bd3be4dbSGreg Kurz { 205bd3be4dbSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(dev); 206bd3be4dbSGreg Kurz V9fsVirtioState *v = VIRTIO_9P(dev); 207bd3be4dbSGreg Kurz V9fsState *s = &v->state; 208b036d9acSChristian Schoenebeck FsDriverEntry *fse = get_fsdev_fsentry(s->fsconf.fsdev_id); 209b036d9acSChristian Schoenebeck 210b036d9acSChristian Schoenebeck if (qtest_enabled() && fse) { 211b036d9acSChristian Schoenebeck fse->export_flags |= V9FS_NO_PERF_WARN; 212b036d9acSChristian Schoenebeck } 213bd3be4dbSGreg Kurz 214066eb006SGreg Kurz if (v9fs_device_realize_common(s, &virtio_9p_transport, errp)) { 215066eb006SGreg Kurz return; 216bd3be4dbSGreg Kurz } 217bd3be4dbSGreg Kurz 218bd3be4dbSGreg Kurz v->config_size = sizeof(struct virtio_9p_config) + strlen(s->fsconf.tag); 2193857cd5cSJonah Palmer virtio_init(vdev, VIRTIO_ID_9P, v->config_size); 220bd3be4dbSGreg Kurz v->vq = virtio_add_queue(vdev, MAX_REQ, handle_9p_output); 221bd3be4dbSGreg Kurz } 222bd3be4dbSGreg Kurz 223b69c3c21SMarkus Armbruster static void virtio_9p_device_unrealize(DeviceState *dev) 224bd3be4dbSGreg Kurz { 225bd3be4dbSGreg Kurz VirtIODevice *vdev = VIRTIO_DEVICE(dev); 226bd3be4dbSGreg Kurz V9fsVirtioState *v = VIRTIO_9P(dev); 227bd3be4dbSGreg Kurz V9fsState *s = &v->state; 228bd3be4dbSGreg Kurz 229ad30a9e9SPan Nengyuan virtio_delete_queue(v->vq); 230bd3be4dbSGreg Kurz virtio_cleanup(vdev); 231b69c3c21SMarkus Armbruster v9fs_device_unrealize_common(s); 232bd3be4dbSGreg Kurz } 233bd3be4dbSGreg Kurz 234e7303c43SKONRAD Frederic /* virtio-9p device */ 235e7303c43SKONRAD Frederic 236dcaf8ddaSHalil Pasic static const VMStateDescription vmstate_virtio_9p = { 237dcaf8ddaSHalil Pasic .name = "virtio-9p", 238dcaf8ddaSHalil Pasic .minimum_version_id = 1, 239dcaf8ddaSHalil Pasic .version_id = 1, 240d19630d2SRichard Henderson .fields = (const VMStateField[]) { 241dcaf8ddaSHalil Pasic VMSTATE_VIRTIO_DEVICE, 242dcaf8ddaSHalil Pasic VMSTATE_END_OF_LIST() 243dcaf8ddaSHalil Pasic }, 244dcaf8ddaSHalil Pasic }; 24518e0e5b2SDr. David Alan Gilbert 24646408f18SRichard Henderson static const Property virtio_9p_properties[] = { 24700588a0aSWei Liu DEFINE_PROP_STRING("mount_tag", V9fsVirtioState, state.fsconf.tag), 24800588a0aSWei Liu DEFINE_PROP_STRING("fsdev", V9fsVirtioState, state.fsconf.fsdev_id), 249e7303c43SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 250e7303c43SKONRAD Frederic }; 251e7303c43SKONRAD Frederic 252e7303c43SKONRAD Frederic static void virtio_9p_class_init(ObjectClass *klass, void *data) 253e7303c43SKONRAD Frederic { 254e7303c43SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 255e7303c43SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 25659be7522SAndreas Färber 2574f67d30bSMarc-André Lureau device_class_set_props(dc, virtio_9p_properties); 25818e0e5b2SDr. David Alan Gilbert dc->vmsd = &vmstate_virtio_9p; 259125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 26059be7522SAndreas Färber vdc->realize = virtio_9p_device_realize; 2616cecf093SGreg Kurz vdc->unrealize = virtio_9p_device_unrealize; 262e7303c43SKONRAD Frederic vdc->get_features = virtio_9p_get_features; 263e7303c43SKONRAD Frederic vdc->get_config = virtio_9p_get_config; 2640e44a0fdSGreg Kurz vdc->reset = virtio_9p_reset; 265e7303c43SKONRAD Frederic } 266e7303c43SKONRAD Frederic 267e7303c43SKONRAD Frederic static const TypeInfo virtio_device_info = { 268e7303c43SKONRAD Frederic .name = TYPE_VIRTIO_9P, 269e7303c43SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 27000588a0aSWei Liu .instance_size = sizeof(V9fsVirtioState), 271e7303c43SKONRAD Frederic .class_init = virtio_9p_class_init, 272e7303c43SKONRAD Frederic }; 273e7303c43SKONRAD Frederic 274e7303c43SKONRAD Frederic static void virtio_9p_register_types(void) 275e7303c43SKONRAD Frederic { 276e7303c43SKONRAD Frederic type_register_static(&virtio_device_info); 277e7303c43SKONRAD Frederic } 278e7303c43SKONRAD Frederic 279e7303c43SKONRAD Frederic type_init(virtio_9p_register_types) 280