Currently, several parts of the codebase check if a network client is a
vhost-user backend by directly comparing its type against the
NET_CLIENT_DRIVER_VHOST_USER enum. This creates a tight coupling
between virtio/vhost-net drivers and the internal implementation
details of the vhost-user net client.
To improve abstraction and reduce this coupling, this patch introduces a
new helper function, qemu_is_vhost_user(). This function allows
callers to query if a client is of the vhost-user type without
needing to know about the specific enum value.
The mechanism uses a new is_vhost_user function pointer in the
NetClientInfo structure, which is implemented by the vhost-user
backend.
All existing direct checks are replaced with calls to the new helper.
This simplifies the logic in vhost_net.c and virtio-net.c,
making the code cleaner and more maintainable.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
hw/net/vhost_net.c | 33 +++++++++++++++------------------
hw/net/virtio-net.c | 18 ++++++++++--------
include/net/net.h | 3 +++
net/net.c | 9 +++++++++
net/vhost-user.c | 8 ++++++++
5 files changed, 45 insertions(+), 26 deletions(-)
diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c
index 891f235a0a6e..41d21e687ed9 100644
--- a/hw/net/vhost_net.c
+++ b/hw/net/vhost_net.c
@@ -98,27 +98,24 @@ static const int user_feature_bits[] = {
static const int *vhost_net_get_feature_bits(struct vhost_net *net)
{
- const int *feature_bits = 0;
+ if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
+ return kernel_feature_bits;
+ }
+
+ if (qemu_is_vhost_user(net->nc)) {
+ return user_feature_bits;
+ }
- switch (net->nc->info->type) {
- case NET_CLIENT_DRIVER_TAP:
- feature_bits = kernel_feature_bits;
- break;
- case NET_CLIENT_DRIVER_VHOST_USER:
- feature_bits = user_feature_bits;
- break;
#ifdef CONFIG_VHOST_NET_VDPA
- case NET_CLIENT_DRIVER_VHOST_VDPA:
- feature_bits = vdpa_feature_bits;
- break;
-#endif
- default:
- error_report("Feature bits not defined for this type: %d",
- net->nc->info->type);
- break;
+ if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+ return vdpa_feature_bits;
}
+#endif
- return feature_bits;
+ error_report("Feature bits not defined for this type: %d",
+ net->nc->info->type);
+
+ return 0;
}
uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
@@ -525,7 +522,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
* because vhost user doesn't interrupt masking/unmasking
* properly.
*/
- if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
+ if (qemu_is_vhost_user(net->nc)) {
dev->use_guest_notifier_mask = false;
}
}
diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index eb93607b8c76..1367d2b581b1 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -679,13 +679,15 @@ static int virtio_net_max_tx_queue_size(VirtIONet *n)
return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
}
- switch(peer->info->type) {
- case NET_CLIENT_DRIVER_VHOST_USER:
- case NET_CLIENT_DRIVER_VHOST_VDPA:
+ if (qemu_is_vhost_user(peer)) {
return VIRTQUEUE_MAX_SIZE;
- default:
- return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
- };
+ }
+
+ if (peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+ return VIRTQUEUE_MAX_SIZE;
+ }
+
+ return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
}
static int peer_attach(VirtIONet *n, int index)
@@ -696,7 +698,7 @@ static int peer_attach(VirtIONet *n, int index)
return 0;
}
- if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
+ if (qemu_is_vhost_user(nc->peer)) {
vhost_set_vring_enable(nc->peer, 1);
}
@@ -719,7 +721,7 @@ static int peer_detach(VirtIONet *n, int index)
return 0;
}
- if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
+ if (qemu_is_vhost_user(nc->peer)) {
vhost_set_vring_enable(nc->peer, 0);
}
diff --git a/include/net/net.h b/include/net/net.h
index ac59b593ba48..2c3b22f564b6 100644
--- a/include/net/net.h
+++ b/include/net/net.h
@@ -67,6 +67,7 @@ typedef void (SocketReadStateFinalize)(SocketReadState *rs);
typedef void (NetAnnounce)(NetClientState *);
typedef bool (SetSteeringEBPF)(NetClientState *, int);
typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **);
+typedef bool (IsVHostUser)(NetClientState *);
typedef struct NetClientInfo {
NetClientDriver type;
@@ -92,6 +93,7 @@ typedef struct NetClientInfo {
NetAnnounce *announce;
SetSteeringEBPF *set_steering_ebpf;
NetCheckPeerType *check_peer_type;
+ IsVHostUser *is_vhost_user;
} NetClientInfo;
struct NetClientState {
@@ -191,6 +193,7 @@ int qemu_get_vnet_hdr_len(NetClientState *nc);
void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
int qemu_set_vnet_le(NetClientState *nc, bool is_le);
int qemu_set_vnet_be(NetClientState *nc, bool is_be);
+bool qemu_is_vhost_user(NetClientState *nc);
void qemu_macaddr_default_if_unset(MACAddr *macaddr);
/**
* qemu_find_nic_info: Obtain NIC configuration information
diff --git a/net/net.c b/net/net.c
index cfa2d8e95827..8726c59915a3 100644
--- a/net/net.c
+++ b/net/net.c
@@ -586,6 +586,15 @@ int qemu_set_vnet_le(NetClientState *nc, bool is_le)
#endif
}
+bool qemu_is_vhost_user(NetClientState *nc)
+{
+ if (nc->info->is_vhost_user) {
+ return nc->info->is_vhost_user(nc);
+ }
+
+ return false;
+}
+
int qemu_set_vnet_be(NetClientState *nc, bool is_be)
{
#if HOST_BIG_ENDIAN
diff --git a/net/vhost-user.c b/net/vhost-user.c
index 10ac8dc0b3d7..4b4d02c56740 100644
--- a/net/vhost-user.c
+++ b/net/vhost-user.c
@@ -221,6 +221,13 @@ static bool vhost_user_check_peer_type(NetClientState *nc, ObjectClass *oc,
return true;
}
+static bool vhost_user_is_vhost_user(NetClientState *nc)
+{
+ assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
+
+ return true;
+}
+
static NetClientInfo net_vhost_user_info = {
.type = NET_CLIENT_DRIVER_VHOST_USER,
.size = sizeof(NetVhostUserState),
@@ -231,6 +238,7 @@ static NetClientInfo net_vhost_user_info = {
.set_vnet_be = vhost_user_set_vnet_endianness,
.set_vnet_le = vhost_user_set_vnet_endianness,
.check_peer_type = vhost_user_check_peer_type,
+ .is_vhost_user = vhost_user_is_vhost_user,
};
static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
--
2.49.0