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