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