Commit 4d0cf552 authored by Tiwei Bie's avatar Tiwei Bie Committed by Michael S. Tsirkin
Browse files

vhost-user: introduce shared vhost-user state



When multi queue is enabled e.g. for a virtio-net device,
each queue pair will have a vhost_dev, and the only thing
shared between vhost devs currently is the chardev. This
patch introduces a vhost-user state structure which will
be shared by all vhost devs of the same virtio device.

Signed-off-by: default avatarTiwei Bie <tiwei.bie@intel.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Reviewed-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 5f57fbea
Loading
Loading
Loading
Loading
+19 −1
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#include "qapi/error.h"
#include "qapi/qmp/qerror.h"
#include "qemu/error-report.h"
#include "hw/virtio/vhost-user.h"
#include "standard-headers/linux/virtio_crypto.h"
#include "sysemu/cryptodev-vhost.h"
#include "chardev/char-fe.h"
@@ -46,6 +47,7 @@
typedef struct CryptoDevBackendVhostUser {
    CryptoDevBackend parent_obj;

    VhostUserState *vhost_user;
    CharBackend chr;
    char *chr_name;
    bool opened;
@@ -102,7 +104,7 @@ cryptodev_vhost_user_start(int queues,
            continue;
        }

        options.opaque = &s->chr;
        options.opaque = s->vhost_user;
        options.backend_type = VHOST_BACKEND_TYPE_USER;
        options.cc = b->conf.peers.ccs[i];
        s->vhost_crypto[i] = cryptodev_vhost_init(&options);
@@ -185,6 +187,7 @@ static void cryptodev_vhost_user_init(
    size_t i;
    Error *local_err = NULL;
    Chardev *chr;
    VhostUserState *user;
    CryptoDevBackendClient *cc;
    CryptoDevBackendVhostUser *s =
                      CRYPTODEV_BACKEND_VHOST_USER(backend);
@@ -215,6 +218,15 @@ static void cryptodev_vhost_user_init(
        }
    }

    user = vhost_user_init();
    if (!user) {
        error_setg(errp, "Failed to init vhost_user");
        return;
    }

    user->chr = &s->chr;
    s->vhost_user = user;

    qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
                     cryptodev_vhost_user_event, NULL, s, NULL, true);

@@ -299,6 +311,12 @@ static void cryptodev_vhost_user_cleanup(
            backend->conf.peers.ccs[i] = NULL;
        }
    }

    if (s->vhost_user) {
        vhost_user_cleanup(s->vhost_user);
        g_free(s->vhost_user);
        s->vhost_user = NULL;
    }
}

static void cryptodev_vhost_user_set_chardev(Object *obj,
+21 −1
Original line number Diff line number Diff line
@@ -226,6 +226,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
{
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
    VHostUserBlk *s = VHOST_USER_BLK(vdev);
    VhostUserState *user;
    int i, ret;

    if (!s->chardev.chr) {
@@ -243,6 +244,15 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
        return;
    }

    user = vhost_user_init();
    if (!user) {
        error_setg(errp, "vhost-user-blk: failed to init vhost_user");
        return;
    }

    user->chr = &s->chardev;
    s->vhost_user = user;

    virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
                sizeof(struct virtio_blk_config));

@@ -258,7 +268,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)

    vhost_dev_set_config_notifier(&s->dev, &blk_ops);

    ret = vhost_dev_init(&s->dev, &s->chardev, VHOST_BACKEND_TYPE_USER, 0);
    ret = vhost_dev_init(&s->dev, s->vhost_user, VHOST_BACKEND_TYPE_USER, 0);
    if (ret < 0) {
        error_setg(errp, "vhost-user-blk: vhost initialization failed: %s",
                   strerror(-ret));
@@ -283,6 +293,10 @@ vhost_err:
virtio_err:
    g_free(s->dev.vqs);
    virtio_cleanup(vdev);

    vhost_user_cleanup(user);
    g_free(user);
    s->vhost_user = NULL;
}

static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
@@ -294,6 +308,12 @@ static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
    vhost_dev_cleanup(&s->dev);
    g_free(s->dev.vqs);
    virtio_cleanup(vdev);

    if (s->vhost_user) {
        vhost_user_cleanup(s->vhost_user);
        g_free(s->vhost_user);
        s->vhost_user = NULL;
    }
}

static void vhost_user_blk_instance_init(Object *obj)
+19 −1
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
    VHostUserSCSI *s = VHOST_USER_SCSI(dev);
    VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
    VhostUserState *user;
    Error *err = NULL;
    int ret;

@@ -85,19 +86,30 @@ static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
        return;
    }

    user = vhost_user_init();
    if (!user) {
        error_setg(errp, "vhost-user-scsi: failed to init vhost_user");
        return;
    }
    user->chr = &vs->conf.chardev;

    vsc->dev.nvqs = 2 + vs->conf.num_queues;
    vsc->dev.vqs = g_new(struct vhost_virtqueue, vsc->dev.nvqs);
    vsc->dev.vq_index = 0;
    vsc->dev.backend_features = 0;

    ret = vhost_dev_init(&vsc->dev, (void *)&vs->conf.chardev,
    ret = vhost_dev_init(&vsc->dev, user,
                         VHOST_BACKEND_TYPE_USER, 0);
    if (ret < 0) {
        error_setg(errp, "vhost-user-scsi: vhost initialization failed: %s",
                   strerror(-ret));
        vhost_user_cleanup(user);
        g_free(user);
        return;
    }

    s->vhost_user = user;

    /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
    vsc->channel = 0;
    vsc->lun = 0;
@@ -117,6 +129,12 @@ static void vhost_user_scsi_unrealize(DeviceState *dev, Error **errp)
    g_free(vsc->dev.vqs);

    virtio_scsi_common_unrealize(dev, errp);

    if (s->vhost_user) {
        vhost_user_cleanup(s->vhost_user);
        g_free(s->vhost_user);
        s->vhost_user = NULL;
    }
}

static uint64_t vhost_user_scsi_get_features(VirtIODevice *vdev,
+1 −1
Original line number Diff line number Diff line
@@ -11,5 +11,5 @@ obj-y += virtio-crypto.o
obj-$(CONFIG_VIRTIO_PCI) += virtio-crypto-pci.o
endif

common-obj-$(call lnot,$(CONFIG_LINUX)) += vhost-stub.o
common-obj-$(call lnot,$(call land,$(CONFIG_VIRTIO),$(CONFIG_LINUX))) += vhost-stub.o
common-obj-$(CONFIG_ALL) += vhost-stub.o
+10 −0
Original line number Diff line number Diff line
#include "qemu/osdep.h"
#include "hw/virtio/vhost.h"
#include "hw/virtio/vhost-user.h"

bool vhost_has_free_slot(void)
{
    return true;
}

VhostUserState *vhost_user_init(void)
{
    return NULL;
}

void vhost_user_cleanup(VhostUserState *user)
{
}
Loading