vhost-user: rework enabling vrings

We call the handler almost the same way in three places:

 - cryptodev-vhost.c
 - vhost_net.c
 - vhost.c

The only difference, is that in vhost.c we don't try to call the handler
for old vhost-user (when VHOST_USER_F_PROTOCOL_FEATURES is not supported).

cryptodev-vhost and vhost_net code will just fail in this case. Probably
they were developed only for newer vhost-user. Anyway, it doesn't seem
correct to rely on this error path, if these devices want to check,
that they don't communicate to old vhost-user protocol, they should
do that earlier.

Let's create the common helper, to call .vhost_set_vring_enable and
use in all three places. For vhost-user let's just always skip
enable/disable if it's unsupported.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Reviewed-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
Reviewed-by: Raphael Norwitz <raphael.s.norwitz@gmail.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Tested-by: Lei Yang <leiyang@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Message-Id: <20260420200339.708640-2-vsementsov@yandex-team.ru>
This commit is contained in:
Vladimir Sementsov-Ogievskiy
2026-04-20 23:03:16 +03:00
committed by Michael S. Tsirkin
parent c549dca2f2
commit 44c8f42d58
5 changed files with 17 additions and 35 deletions

View File

@@ -152,7 +152,6 @@ vhost_set_vring_enable(CryptoDevBackendClient *cc,
{
CryptoDevBackendVhost *crypto =
cryptodev_get_vhost(cc, b, queue);
const VhostOps *vhost_ops;
cc->vring_enable = enable;
@@ -160,12 +159,7 @@ vhost_set_vring_enable(CryptoDevBackendClient *cc,
return 0;
}
vhost_ops = crypto->dev.vhost_ops;
if (vhost_ops->vhost_set_vring_enable) {
return vhost_ops->vhost_set_vring_enable(&crypto->dev, enable);
}
return 0;
return vhost_dev_set_vring_enable(&crypto->dev, enable);
}
int cryptodev_vhost_start(VirtIODevice *dev, int total_queues)

View File

@@ -587,7 +587,6 @@ VHostNetState *get_vhost_net(NetClientState *nc)
int vhost_net_set_vring_enable(NetClientState *nc, int enable)
{
VHostNetState *net = get_vhost_net(nc);
const VhostOps *vhost_ops = net->dev.vhost_ops;
/*
* vhost-vdpa network devices need to enable dataplane virtqueues after
@@ -601,11 +600,7 @@ int vhost_net_set_vring_enable(NetClientState *nc, int enable)
nc->vring_enable = enable;
if (vhost_ops && vhost_ops->vhost_set_vring_enable) {
return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
}
return 0;
return vhost_dev_set_vring_enable(&net->dev, enable);
}
int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)

View File

@@ -1230,7 +1230,12 @@ static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable)
int i;
if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) {
return -EINVAL;
/*
* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not
* been negotiated, the rings start directly in the enabled state,
* and can't be disabled.
*/
return 0;
}
for (i = 0; i < dev->nvqs; ++i) {

View File

@@ -2078,27 +2078,6 @@ int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
return 0;
}
static int vhost_dev_set_vring_enable(struct vhost_dev *hdev, int enable)
{
if (!hdev->vhost_ops->vhost_set_vring_enable) {
return 0;
}
/*
* For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not
* been negotiated, the rings start directly in the enabled state, and
* .vhost_set_vring_enable callback will fail since
* VHOST_USER_SET_VRING_ENABLE is not supported.
*/
if (hdev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER &&
!virtio_has_feature(hdev->backend_features,
VHOST_USER_F_PROTOCOL_FEATURES)) {
return 0;
}
return hdev->vhost_ops->vhost_set_vring_enable(hdev, enable);
}
/*
* Host notifiers must be enabled at this point.
*

View File

@@ -215,6 +215,15 @@ static inline bool vhost_dev_is_started(struct vhost_dev *hdev)
return hdev->started;
}
static inline int vhost_dev_set_vring_enable(struct vhost_dev *hdev, int enable)
{
if (!hdev->vhost_ops->vhost_set_vring_enable) {
return 0;
}
return hdev->vhost_ops->vhost_set_vring_enable(hdev, enable);
}
/**
* vhost_dev_start() - start the vhost device
* @hdev: common vhost_dev structure