Commit 0b99f224 authored by Marc-André Lureau's avatar Marc-André Lureau Committed by Michael S. Tsirkin
Browse files

vhost-user: simplify vhost_user_init/vhost_user_cleanup



Take a VhostUserState* that can be pre-allocated, and initialize it
with the associated chardev.

Signed-off-by: default avatarMarc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: default avatarTiwei Bie <tiwei.bie@intel.com>
Message-Id: <20190308140454.32437-4-marcandre.lureau@redhat.com>
Reviewed-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 482580a6
Loading
Loading
Loading
Loading
+4 −14
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@
typedef struct CryptoDevBackendVhostUser {
    CryptoDevBackend parent_obj;

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

        options.opaque = s->vhost_user;
        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);
@@ -182,7 +182,6 @@ 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);
@@ -213,15 +212,10 @@ static void cryptodev_vhost_user_init(
        }
    }

    user = vhost_user_init();
    if (!user) {
        error_setg(errp, "Failed to init vhost_user");
    if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
        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);

@@ -307,11 +301,7 @@ static void cryptodev_vhost_user_cleanup(
        }
    }

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

static void cryptodev_vhost_user_set_chardev(Object *obj,
+4 −18
Original line number Diff line number Diff line
@@ -253,7 +253,6 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
{
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
    VHostUserBlk *s = VHOST_USER_BLK(vdev);
    VhostUserState *user;
    struct vhost_virtqueue *vqs = NULL;
    int i, ret;

@@ -272,15 +271,10 @@ 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");
    if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
        return;
    }

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

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

@@ -297,7 +291,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->vhost_user, 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));
@@ -322,10 +316,7 @@ vhost_err:
virtio_err:
    g_free(vqs);
    virtio_cleanup(vdev);

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

static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
@@ -338,12 +329,7 @@ static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
    vhost_dev_cleanup(&s->dev);
    g_free(vqs);
    virtio_cleanup(vdev);

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

static void vhost_user_blk_instance_init(Object *obj)
+4 −16
Original line number Diff line number Diff line
@@ -69,7 +69,6 @@ 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;

@@ -86,30 +85,24 @@ 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");
    if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
        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, user,
    ret = vhost_dev_init(&vsc->dev, &s->vhost_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);
        vhost_user_cleanup(&s->vhost_user);
        return;
    }

    s->vhost_user = user;

    /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
    vsc->channel = 0;
    vsc->lun = 0;
@@ -130,12 +123,7 @@ static void vhost_user_scsi_unrealize(DeviceState *dev, Error **errp)
    g_free(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;
    }
    vhost_user_cleanup(&s->vhost_user);
}

static Property vhost_user_scsi_properties[] = {
+2 −2
Original line number Diff line number Diff line
@@ -7,9 +7,9 @@ bool vhost_has_free_slot(void)
    return true;
}

VhostUserState *vhost_user_init(void)
bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
{
    return NULL;
    return false;
}

void vhost_user_cleanup(VhostUserState *user)
+12 −4
Original line number Diff line number Diff line
@@ -1750,17 +1750,24 @@ static bool vhost_user_mem_section_filter(struct vhost_dev *dev,
    return result;
}

VhostUserState *vhost_user_init(void)
bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
{
    VhostUserState *user = g_new0(struct VhostUserState, 1);

    return user;
    if (user->chr) {
        error_setg(errp, "Cannot initialize vhost-user state");
        return false;
    }
    user->chr = chr;
    return true;
}

void vhost_user_cleanup(VhostUserState *user)
{
    int i;

    if (!user->chr) {
        return;
    }

    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        if (user->notifier[i].addr) {
            object_unparent(OBJECT(&user->notifier[i].mr));
@@ -1768,6 +1775,7 @@ void vhost_user_cleanup(VhostUserState *user)
            user->notifier[i].addr = NULL;
        }
    }
    user->chr = NULL;
}

const VhostOps user_ops = {
Loading