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:
parent
482580a658
commit
0b99f22461
|
@ -47,7 +47,7 @@
|
||||||
typedef struct CryptoDevBackendVhostUser {
|
typedef struct CryptoDevBackendVhostUser {
|
||||||
CryptoDevBackend parent_obj;
|
CryptoDevBackend parent_obj;
|
||||||
|
|
||||||
VhostUserState *vhost_user;
|
VhostUserState vhost_user;
|
||||||
CharBackend chr;
|
CharBackend chr;
|
||||||
char *chr_name;
|
char *chr_name;
|
||||||
bool opened;
|
bool opened;
|
||||||
|
@ -104,7 +104,7 @@ cryptodev_vhost_user_start(int queues,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
options.opaque = s->vhost_user;
|
options.opaque = &s->vhost_user;
|
||||||
options.backend_type = VHOST_BACKEND_TYPE_USER;
|
options.backend_type = VHOST_BACKEND_TYPE_USER;
|
||||||
options.cc = b->conf.peers.ccs[i];
|
options.cc = b->conf.peers.ccs[i];
|
||||||
s->vhost_crypto[i] = cryptodev_vhost_init(&options);
|
s->vhost_crypto[i] = cryptodev_vhost_init(&options);
|
||||||
|
@ -182,7 +182,6 @@ static void cryptodev_vhost_user_init(
|
||||||
size_t i;
|
size_t i;
|
||||||
Error *local_err = NULL;
|
Error *local_err = NULL;
|
||||||
Chardev *chr;
|
Chardev *chr;
|
||||||
VhostUserState *user;
|
|
||||||
CryptoDevBackendClient *cc;
|
CryptoDevBackendClient *cc;
|
||||||
CryptoDevBackendVhostUser *s =
|
CryptoDevBackendVhostUser *s =
|
||||||
CRYPTODEV_BACKEND_VHOST_USER(backend);
|
CRYPTODEV_BACKEND_VHOST_USER(backend);
|
||||||
|
@ -213,15 +212,10 @@ static void cryptodev_vhost_user_init(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
user = vhost_user_init();
|
if (!vhost_user_init(&s->vhost_user, &s->chr, errp)) {
|
||||||
if (!user) {
|
|
||||||
error_setg(errp, "Failed to init vhost_user");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
user->chr = &s->chr;
|
|
||||||
s->vhost_user = user;
|
|
||||||
|
|
||||||
qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
|
qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
|
||||||
cryptodev_vhost_user_event, NULL, s, NULL, true);
|
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);
|
||||||
vhost_user_cleanup(s->vhost_user);
|
|
||||||
g_free(s->vhost_user);
|
|
||||||
s->vhost_user = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cryptodev_vhost_user_set_chardev(Object *obj,
|
static void cryptodev_vhost_user_set_chardev(Object *obj,
|
||||||
|
|
|
@ -253,7 +253,6 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
|
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
|
||||||
VHostUserBlk *s = VHOST_USER_BLK(vdev);
|
VHostUserBlk *s = VHOST_USER_BLK(vdev);
|
||||||
VhostUserState *user;
|
|
||||||
struct vhost_virtqueue *vqs = NULL;
|
struct vhost_virtqueue *vqs = NULL;
|
||||||
int i, ret;
|
int i, ret;
|
||||||
|
|
||||||
|
@ -272,15 +271,10 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
user = vhost_user_init();
|
if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
|
||||||
if (!user) {
|
|
||||||
error_setg(errp, "vhost-user-blk: failed to init vhost_user");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
user->chr = &s->chardev;
|
|
||||||
s->vhost_user = user;
|
|
||||||
|
|
||||||
virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
|
virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
|
||||||
sizeof(struct virtio_blk_config));
|
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);
|
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) {
|
if (ret < 0) {
|
||||||
error_setg(errp, "vhost-user-blk: vhost initialization failed: %s",
|
error_setg(errp, "vhost-user-blk: vhost initialization failed: %s",
|
||||||
strerror(-ret));
|
strerror(-ret));
|
||||||
|
@ -322,10 +316,7 @@ vhost_err:
|
||||||
virtio_err:
|
virtio_err:
|
||||||
g_free(vqs);
|
g_free(vqs);
|
||||||
virtio_cleanup(vdev);
|
virtio_cleanup(vdev);
|
||||||
|
vhost_user_cleanup(&s->vhost_user);
|
||||||
vhost_user_cleanup(user);
|
|
||||||
g_free(user);
|
|
||||||
s->vhost_user = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
|
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);
|
vhost_dev_cleanup(&s->dev);
|
||||||
g_free(vqs);
|
g_free(vqs);
|
||||||
virtio_cleanup(vdev);
|
virtio_cleanup(vdev);
|
||||||
|
vhost_user_cleanup(&s->vhost_user);
|
||||||
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)
|
static void vhost_user_blk_instance_init(Object *obj)
|
||||||
|
|
|
@ -69,7 +69,6 @@ static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
|
||||||
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
|
VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
|
||||||
VHostUserSCSI *s = VHOST_USER_SCSI(dev);
|
VHostUserSCSI *s = VHOST_USER_SCSI(dev);
|
||||||
VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
|
VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
|
||||||
VhostUserState *user;
|
|
||||||
Error *err = NULL;
|
Error *err = NULL;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
@ -86,30 +85,24 @@ static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
user = vhost_user_init();
|
if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
|
||||||
if (!user) {
|
|
||||||
error_setg(errp, "vhost-user-scsi: failed to init vhost_user");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
user->chr = &vs->conf.chardev;
|
|
||||||
|
|
||||||
vsc->dev.nvqs = 2 + vs->conf.num_queues;
|
vsc->dev.nvqs = 2 + vs->conf.num_queues;
|
||||||
vsc->dev.vqs = g_new(struct vhost_virtqueue, vsc->dev.nvqs);
|
vsc->dev.vqs = g_new(struct vhost_virtqueue, vsc->dev.nvqs);
|
||||||
vsc->dev.vq_index = 0;
|
vsc->dev.vq_index = 0;
|
||||||
vsc->dev.backend_features = 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);
|
VHOST_BACKEND_TYPE_USER, 0);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg(errp, "vhost-user-scsi: vhost initialization failed: %s",
|
error_setg(errp, "vhost-user-scsi: vhost initialization failed: %s",
|
||||||
strerror(-ret));
|
strerror(-ret));
|
||||||
vhost_user_cleanup(user);
|
vhost_user_cleanup(&s->vhost_user);
|
||||||
g_free(user);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->vhost_user = user;
|
|
||||||
|
|
||||||
/* Channel and lun both are 0 for bootable vhost-user-scsi disk */
|
/* Channel and lun both are 0 for bootable vhost-user-scsi disk */
|
||||||
vsc->channel = 0;
|
vsc->channel = 0;
|
||||||
vsc->lun = 0;
|
vsc->lun = 0;
|
||||||
|
@ -130,12 +123,7 @@ static void vhost_user_scsi_unrealize(DeviceState *dev, Error **errp)
|
||||||
g_free(vqs);
|
g_free(vqs);
|
||||||
|
|
||||||
virtio_scsi_common_unrealize(dev, errp);
|
virtio_scsi_common_unrealize(dev, errp);
|
||||||
|
vhost_user_cleanup(&s->vhost_user);
|
||||||
if (s->vhost_user) {
|
|
||||||
vhost_user_cleanup(s->vhost_user);
|
|
||||||
g_free(s->vhost_user);
|
|
||||||
s->vhost_user = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Property vhost_user_scsi_properties[] = {
|
static Property vhost_user_scsi_properties[] = {
|
||||||
|
|
|
@ -7,9 +7,9 @@ bool vhost_has_free_slot(void)
|
||||||
return true;
|
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)
|
void vhost_user_cleanup(VhostUserState *user)
|
||||||
|
|
|
@ -1750,17 +1750,24 @@ static bool vhost_user_mem_section_filter(struct vhost_dev *dev,
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
VhostUserState *vhost_user_init(void)
|
bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
|
||||||
{
|
{
|
||||||
VhostUserState *user = g_new0(struct VhostUserState, 1);
|
if (user->chr) {
|
||||||
|
error_setg(errp, "Cannot initialize vhost-user state");
|
||||||
return user;
|
return false;
|
||||||
|
}
|
||||||
|
user->chr = chr;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vhost_user_cleanup(VhostUserState *user)
|
void vhost_user_cleanup(VhostUserState *user)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (!user->chr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
|
for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
|
||||||
if (user->notifier[i].addr) {
|
if (user->notifier[i].addr) {
|
||||||
object_unparent(OBJECT(&user->notifier[i].mr));
|
object_unparent(OBJECT(&user->notifier[i].mr));
|
||||||
|
@ -1768,6 +1775,7 @@ void vhost_user_cleanup(VhostUserState *user)
|
||||||
user->notifier[i].addr = NULL;
|
user->notifier[i].addr = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
user->chr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const VhostOps user_ops = {
|
const VhostOps user_ops = {
|
||||||
|
|
|
@ -36,7 +36,7 @@ typedef struct VHostUserBlk {
|
||||||
uint32_t queue_size;
|
uint32_t queue_size;
|
||||||
uint32_t config_wce;
|
uint32_t config_wce;
|
||||||
struct vhost_dev dev;
|
struct vhost_dev dev;
|
||||||
VhostUserState *vhost_user;
|
VhostUserState vhost_user;
|
||||||
} VHostUserBlk;
|
} VHostUserBlk;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
typedef struct VHostUserSCSI {
|
typedef struct VHostUserSCSI {
|
||||||
VHostSCSICommon parent_obj;
|
VHostSCSICommon parent_obj;
|
||||||
VhostUserState *vhost_user;
|
VhostUserState vhost_user;
|
||||||
} VHostUserSCSI;
|
} VHostUserSCSI;
|
||||||
|
|
||||||
#endif /* VHOST_USER_SCSI_H */
|
#endif /* VHOST_USER_SCSI_H */
|
||||||
|
|
|
@ -22,7 +22,7 @@ typedef struct VhostUserState {
|
||||||
VhostUserHostNotifier notifier[VIRTIO_QUEUE_MAX];
|
VhostUserHostNotifier notifier[VIRTIO_QUEUE_MAX];
|
||||||
} VhostUserState;
|
} VhostUserState;
|
||||||
|
|
||||||
VhostUserState *vhost_user_init(void);
|
bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp);
|
||||||
void vhost_user_cleanup(VhostUserState *user);
|
void vhost_user_cleanup(VhostUserState *user);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -304,19 +304,14 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
|
||||||
{
|
{
|
||||||
Error *err = NULL;
|
Error *err = NULL;
|
||||||
NetClientState *nc, *nc0 = NULL;
|
NetClientState *nc, *nc0 = NULL;
|
||||||
VhostUserState *user = NULL;
|
|
||||||
NetVhostUserState *s = NULL;
|
NetVhostUserState *s = NULL;
|
||||||
|
VhostUserState *user;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
assert(name);
|
assert(name);
|
||||||
assert(queues > 0);
|
assert(queues > 0);
|
||||||
|
|
||||||
user = vhost_user_init();
|
user = g_new0(struct VhostUserState, 1);
|
||||||
if (!user) {
|
|
||||||
error_report("failed to init vhost_user");
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < queues; i++) {
|
for (i = 0; i < queues; i++) {
|
||||||
nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
|
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",
|
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) {
|
if (!nc0) {
|
||||||
nc0 = nc;
|
nc0 = nc;
|
||||||
s = DO_UPCAST(NetVhostUserState, nc, 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);
|
error_report_err(err);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
user->chr = &s->chr;
|
|
||||||
}
|
}
|
||||||
s = DO_UPCAST(NetVhostUserState, nc, nc);
|
s = DO_UPCAST(NetVhostUserState, nc, nc);
|
||||||
s->vhost_user = user;
|
s->vhost_user = user;
|
||||||
|
|
Loading…
Reference in New Issue