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: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Tiwei Bie <tiwei.bie@intel.com>
Message-Id: <20190308140454.32437-4-marcandre.lureau@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Marc-André Lureau 2019-03-08 15:04:45 +01:00 committed by Michael S. Tsirkin
parent 482580a658
commit 0b99f22461
9 changed files with 33 additions and 66 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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[] = {

View File

@ -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)

View File

@ -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 = {

View File

@ -36,7 +36,7 @@ typedef struct VHostUserBlk {
uint32_t queue_size;
uint32_t config_wce;
struct vhost_dev dev;
VhostUserState *vhost_user;
VhostUserState vhost_user;
} VHostUserBlk;
#endif

View File

@ -30,7 +30,7 @@
typedef struct VHostUserSCSI {
VHostSCSICommon parent_obj;
VhostUserState *vhost_user;
VhostUserState vhost_user;
} VHostUserSCSI;
#endif /* VHOST_USER_SCSI_H */

View File

@ -22,7 +22,7 @@ typedef struct VhostUserState {
VhostUserHostNotifier notifier[VIRTIO_QUEUE_MAX];
} VhostUserState;
VhostUserState *vhost_user_init(void);
bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp);
void vhost_user_cleanup(VhostUserState *user);
#endif

View File

@ -304,19 +304,14 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
{
Error *err = NULL;
NetClientState *nc, *nc0 = NULL;
VhostUserState *user = NULL;
NetVhostUserState *s = NULL;
VhostUserState *user;
int i;
assert(name);
assert(queues > 0);
user = vhost_user_init();
if (!user) {
error_report("failed to init vhost_user");
goto err;
}
user = g_new0(struct VhostUserState, 1);
for (i = 0; i < queues; i++) {
nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
@ -325,11 +320,11 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
if (!nc0) {
nc0 = nc;
s = DO_UPCAST(NetVhostUserState, nc, nc);
if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
if (!qemu_chr_fe_init(&s->chr, chr, &err) ||
!vhost_user_init(user, &s->chr, &err)) {
error_report_err(err);
goto err;
}
user->chr = &s->chr;
}
s = DO_UPCAST(NetVhostUserState, nc, nc);
s->vhost_user = user;