qemu/hw/block/vhost-user-blk.c
<<
>>
Prefs
   1/*
   2 * vhost-user-blk host device
   3 *
   4 * Copyright(C) 2017 Intel Corporation.
   5 *
   6 * Authors:
   7 *  Changpeng Liu <changpeng.liu@intel.com>
   8 *
   9 * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by:
  10 * Felipe Franciosi <felipe@nutanix.com>
  11 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
  12 * Nicholas Bellinger <nab@risingtidesystems.com>
  13 *
  14 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
  15 * See the COPYING.LIB file in the top-level directory.
  16 *
  17 */
  18
  19#include "qemu/osdep.h"
  20#include "qapi/error.h"
  21#include "qemu/error-report.h"
  22#include "qemu/cutils.h"
  23#include "qom/object.h"
  24#include "hw/qdev-core.h"
  25#include "hw/virtio/vhost.h"
  26#include "hw/virtio/vhost-user-blk.h"
  27#include "hw/virtio/virtio.h"
  28#include "hw/virtio/virtio-bus.h"
  29#include "hw/virtio/virtio-access.h"
  30
  31static const int user_feature_bits[] = {
  32    VIRTIO_BLK_F_SIZE_MAX,
  33    VIRTIO_BLK_F_SEG_MAX,
  34    VIRTIO_BLK_F_GEOMETRY,
  35    VIRTIO_BLK_F_BLK_SIZE,
  36    VIRTIO_BLK_F_TOPOLOGY,
  37    VIRTIO_BLK_F_MQ,
  38    VIRTIO_BLK_F_RO,
  39    VIRTIO_BLK_F_FLUSH,
  40    VIRTIO_BLK_F_CONFIG_WCE,
  41    VIRTIO_F_VERSION_1,
  42    VIRTIO_RING_F_INDIRECT_DESC,
  43    VIRTIO_RING_F_EVENT_IDX,
  44    VIRTIO_F_NOTIFY_ON_EMPTY,
  45    VHOST_INVALID_FEATURE_BIT
  46};
  47
  48static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config)
  49{
  50    VHostUserBlk *s = VHOST_USER_BLK(vdev);
  51
  52    memcpy(config, &s->blkcfg, sizeof(struct virtio_blk_config));
  53}
  54
  55static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
  56{
  57    VHostUserBlk *s = VHOST_USER_BLK(vdev);
  58    struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
  59    int ret;
  60
  61    if (blkcfg->wce == s->blkcfg.wce) {
  62        return;
  63    }
  64
  65    ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
  66                               offsetof(struct virtio_blk_config, wce),
  67                               sizeof(blkcfg->wce),
  68                               VHOST_SET_CONFIG_TYPE_MASTER);
  69    if (ret) {
  70        error_report("set device config space failed");
  71        return;
  72    }
  73
  74    s->blkcfg.wce = blkcfg->wce;
  75}
  76
  77static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
  78{
  79    int ret;
  80    struct virtio_blk_config blkcfg;
  81    VHostUserBlk *s = VHOST_USER_BLK(dev->vdev);
  82
  83    ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg,
  84                               sizeof(struct virtio_blk_config));
  85    if (ret < 0) {
  86        error_report("get config space failed");
  87        return -1;
  88    }
  89
  90    /* valid for resize only */
  91    if (blkcfg.capacity != s->blkcfg.capacity) {
  92        s->blkcfg.capacity = blkcfg.capacity;
  93        memcpy(dev->vdev->config, &s->blkcfg, sizeof(struct virtio_blk_config));
  94        virtio_notify_config(dev->vdev);
  95    }
  96
  97    return 0;
  98}
  99
 100const VhostDevConfigOps blk_ops = {
 101    .vhost_dev_config_notifier = vhost_user_blk_handle_config_change,
 102};
 103
 104static void vhost_user_blk_start(VirtIODevice *vdev)
 105{
 106    VHostUserBlk *s = VHOST_USER_BLK(vdev);
 107    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
 108    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
 109    int i, ret;
 110
 111    if (!k->set_guest_notifiers) {
 112        error_report("binding does not support guest notifiers");
 113        return;
 114    }
 115
 116    ret = vhost_dev_enable_notifiers(&s->dev, vdev);
 117    if (ret < 0) {
 118        error_report("Error enabling host notifiers: %d", -ret);
 119        return;
 120    }
 121
 122    ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
 123    if (ret < 0) {
 124        error_report("Error binding guest notifier: %d", -ret);
 125        goto err_host_notifiers;
 126    }
 127
 128    s->dev.acked_features = vdev->guest_features;
 129    ret = vhost_dev_start(&s->dev, vdev);
 130    if (ret < 0) {
 131        error_report("Error starting vhost: %d", -ret);
 132        goto err_guest_notifiers;
 133    }
 134
 135    /* guest_notifier_mask/pending not used yet, so just unmask
 136     * everything here. virtio-pci will do the right thing by
 137     * enabling/disabling irqfd.
 138     */
 139    for (i = 0; i < s->dev.nvqs; i++) {
 140        vhost_virtqueue_mask(&s->dev, vdev, i, false);
 141    }
 142
 143    return;
 144
 145err_guest_notifiers:
 146    k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
 147err_host_notifiers:
 148    vhost_dev_disable_notifiers(&s->dev, vdev);
 149}
 150
 151static void vhost_user_blk_stop(VirtIODevice *vdev)
 152{
 153    VHostUserBlk *s = VHOST_USER_BLK(vdev);
 154    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
 155    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
 156    int ret;
 157
 158    if (!k->set_guest_notifiers) {
 159        return;
 160    }
 161
 162    vhost_dev_stop(&s->dev, vdev);
 163
 164    ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
 165    if (ret < 0) {
 166        error_report("vhost guest notifier cleanup failed: %d", ret);
 167        return;
 168    }
 169
 170    vhost_dev_disable_notifiers(&s->dev, vdev);
 171}
 172
 173static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status)
 174{
 175    VHostUserBlk *s = VHOST_USER_BLK(vdev);
 176    bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
 177
 178    if (!vdev->vm_running) {
 179        should_start = false;
 180    }
 181
 182    if (s->dev.started == should_start) {
 183        return;
 184    }
 185
 186    if (should_start) {
 187        vhost_user_blk_start(vdev);
 188    } else {
 189        vhost_user_blk_stop(vdev);
 190    }
 191
 192}
 193
 194static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
 195                                            uint64_t features,
 196                                            Error **errp)
 197{
 198    VHostUserBlk *s = VHOST_USER_BLK(vdev);
 199
 200    /* Turn on pre-defined features */
 201    virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
 202    virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
 203    virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
 204    virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
 205    virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH);
 206    virtio_add_feature(&features, VIRTIO_BLK_F_RO);
 207
 208    if (s->config_wce) {
 209        virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
 210    }
 211    if (s->num_queues > 1) {
 212        virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
 213    }
 214
 215    return vhost_get_features(&s->dev, user_feature_bits, features);
 216}
 217
 218static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 219{
 220    VHostUserBlk *s = VHOST_USER_BLK(vdev);
 221    int i;
 222
 223    if (!(virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1) &&
 224        !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1))) {
 225        return;
 226    }
 227
 228    if (s->dev.started) {
 229        return;
 230    }
 231
 232    /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
 233     * vhost here instead of waiting for .set_status().
 234     */
 235    vhost_user_blk_start(vdev);
 236
 237    /* Kick right away to begin processing requests already in vring */
 238    for (i = 0; i < s->dev.nvqs; i++) {
 239        VirtQueue *kick_vq = virtio_get_queue(vdev, i);
 240
 241        if (!virtio_queue_get_desc_addr(vdev, i)) {
 242            continue;
 243        }
 244        event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
 245    }
 246}
 247
 248static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
 249{
 250    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
 251    VHostUserBlk *s = VHOST_USER_BLK(vdev);
 252    VhostUserState *user;
 253    int i, ret;
 254
 255    if (!s->chardev.chr) {
 256        error_setg(errp, "vhost-user-blk: chardev is mandatory");
 257        return;
 258    }
 259
 260    if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) {
 261        error_setg(errp, "vhost-user-blk: invalid number of IO queues");
 262        return;
 263    }
 264
 265    if (!s->queue_size) {
 266        error_setg(errp, "vhost-user-blk: queue size must be non-zero");
 267        return;
 268    }
 269
 270    user = vhost_user_init();
 271    if (!user) {
 272        error_setg(errp, "vhost-user-blk: failed to init vhost_user");
 273        return;
 274    }
 275
 276    user->chr = &s->chardev;
 277    s->vhost_user = user;
 278
 279    virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
 280                sizeof(struct virtio_blk_config));
 281
 282    for (i = 0; i < s->num_queues; i++) {
 283        virtio_add_queue(vdev, s->queue_size,
 284                         vhost_user_blk_handle_output);
 285    }
 286
 287    s->dev.nvqs = s->num_queues;
 288    s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
 289    s->dev.vq_index = 0;
 290    s->dev.backend_features = 0;
 291
 292    vhost_dev_set_config_notifier(&s->dev, &blk_ops);
 293
 294    ret = vhost_dev_init(&s->dev, s->vhost_user, VHOST_BACKEND_TYPE_USER, 0);
 295    if (ret < 0) {
 296        error_setg(errp, "vhost-user-blk: vhost initialization failed: %s",
 297                   strerror(-ret));
 298        goto virtio_err;
 299    }
 300
 301    ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
 302                              sizeof(struct virtio_blk_config));
 303    if (ret < 0) {
 304        error_setg(errp, "vhost-user-blk: get block config failed");
 305        goto vhost_err;
 306    }
 307
 308    if (s->blkcfg.num_queues != s->num_queues) {
 309        s->blkcfg.num_queues = s->num_queues;
 310    }
 311
 312    return;
 313
 314vhost_err:
 315    vhost_dev_cleanup(&s->dev);
 316virtio_err:
 317    g_free(s->dev.vqs);
 318    virtio_cleanup(vdev);
 319
 320    vhost_user_cleanup(user);
 321    g_free(user);
 322    s->vhost_user = NULL;
 323}
 324
 325static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
 326{
 327    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
 328    VHostUserBlk *s = VHOST_USER_BLK(dev);
 329
 330    vhost_user_blk_set_status(vdev, 0);
 331    vhost_dev_cleanup(&s->dev);
 332    g_free(s->dev.vqs);
 333    virtio_cleanup(vdev);
 334
 335    if (s->vhost_user) {
 336        vhost_user_cleanup(s->vhost_user);
 337        g_free(s->vhost_user);
 338        s->vhost_user = NULL;
 339    }
 340}
 341
 342static void vhost_user_blk_instance_init(Object *obj)
 343{
 344    VHostUserBlk *s = VHOST_USER_BLK(obj);
 345
 346    device_add_bootindex_property(obj, &s->bootindex, "bootindex",
 347                                  "/disk@0,0", DEVICE(obj), NULL);
 348}
 349
 350static const VMStateDescription vmstate_vhost_user_blk = {
 351    .name = "vhost-user-blk",
 352    .minimum_version_id = 1,
 353    .version_id = 1,
 354    .fields = (VMStateField[]) {
 355        VMSTATE_VIRTIO_DEVICE,
 356        VMSTATE_END_OF_LIST()
 357    },
 358};
 359
 360static Property vhost_user_blk_properties[] = {
 361    DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev),
 362    DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues, 1),
 363    DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128),
 364    DEFINE_PROP_BIT("config-wce", VHostUserBlk, config_wce, 0, true),
 365    DEFINE_PROP_END_OF_LIST(),
 366};
 367
 368static void vhost_user_blk_class_init(ObjectClass *klass, void *data)
 369{
 370    DeviceClass *dc = DEVICE_CLASS(klass);
 371    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
 372
 373    dc->props = vhost_user_blk_properties;
 374    dc->vmsd = &vmstate_vhost_user_blk;
 375    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
 376    vdc->realize = vhost_user_blk_device_realize;
 377    vdc->unrealize = vhost_user_blk_device_unrealize;
 378    vdc->get_config = vhost_user_blk_update_config;
 379    vdc->set_config = vhost_user_blk_set_config;
 380    vdc->get_features = vhost_user_blk_get_features;
 381    vdc->set_status = vhost_user_blk_set_status;
 382}
 383
 384static const TypeInfo vhost_user_blk_info = {
 385    .name = TYPE_VHOST_USER_BLK,
 386    .parent = TYPE_VIRTIO_DEVICE,
 387    .instance_size = sizeof(VHostUserBlk),
 388    .instance_init = vhost_user_blk_instance_init,
 389    .class_init = vhost_user_blk_class_init,
 390};
 391
 392static void virtio_register_types(void)
 393{
 394    type_register_static(&vhost_user_blk_info);
 395}
 396
 397type_init(virtio_register_types)
 398