From: Laurent Vivier <lvivier@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Stefan Weil" <sw@weilnetz.de>,
"Stefano Garzarella" <sgarzare@redhat.com>,
"Jason Wang" <jasowang@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Dr. David Alan Gilbert" <dave@treblig.org>,
"Eric Blake" <eblake@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>
Subject: [PATCH v2 05/10] net: Consolidate vhost feature bits into NetClientInfo
Date: Wed, 18 Jun 2025 17:57:13 +0200 [thread overview]
Message-ID: <20250618155718.550968-6-lvivier@redhat.com> (raw)
In-Reply-To: <20250618155718.550968-1-lvivier@redhat.com>
Previously, the vhost_net_get_feature_bits() function in
hw/net/vhost_net.c used a large switch statement to determine
the appropriate feature bits based on the NetClientDriver type.
This created unnecessary coupling between the generic vhost layer
and specific network backends (like TAP, vhost-user, and
vhost-vdpa).
This patch moves the definition of vhost feature bits directly into the
NetClientInfo structure for each relevant network client.
This refactoring centralizes feature bit definitions where they're
needed, making code easier to add new vhost-enabled network backends
in the future without modifying core vhost logic.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
hw/net/vhost_net.c | 80 ++--------------------------------------
include/net/net.h | 1 +
include/net/vhost-vdpa.h | 2 -
net/tap.c | 19 ++++++++++
net/vhost-user.c | 43 +++++++++++++++++++++
net/vhost-vdpa.c | 4 +-
6 files changed, 70 insertions(+), 79 deletions(-)
diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c
index 16dadd022e75..3dff819d2dbd 100644
--- a/hw/net/vhost_net.c
+++ b/hw/net/vhost_net.c
@@ -36,86 +36,14 @@
#include "hw/virtio/virtio-bus.h"
#include "linux-headers/linux/vhost.h"
-
-/* Features supported by host kernel. */
-static const int kernel_feature_bits[] = {
- VIRTIO_F_NOTIFY_ON_EMPTY,
- VIRTIO_RING_F_INDIRECT_DESC,
- VIRTIO_RING_F_EVENT_IDX,
- VIRTIO_NET_F_MRG_RXBUF,
- VIRTIO_F_VERSION_1,
- VIRTIO_NET_F_MTU,
- VIRTIO_F_IOMMU_PLATFORM,
- VIRTIO_F_RING_PACKED,
- VIRTIO_F_RING_RESET,
- VIRTIO_F_IN_ORDER,
- VIRTIO_F_NOTIFICATION_DATA,
- VIRTIO_NET_F_RSC_EXT,
- VIRTIO_NET_F_HASH_REPORT,
- VHOST_INVALID_FEATURE_BIT
-};
-
-/* Features supported by others. */
-static const int user_feature_bits[] = {
- VIRTIO_F_NOTIFY_ON_EMPTY,
- VIRTIO_F_NOTIFICATION_DATA,
- VIRTIO_RING_F_INDIRECT_DESC,
- VIRTIO_RING_F_EVENT_IDX,
-
- VIRTIO_F_ANY_LAYOUT,
- VIRTIO_F_VERSION_1,
- VIRTIO_NET_F_CSUM,
- VIRTIO_NET_F_GUEST_CSUM,
- VIRTIO_NET_F_GSO,
- VIRTIO_NET_F_GUEST_TSO4,
- VIRTIO_NET_F_GUEST_TSO6,
- VIRTIO_NET_F_GUEST_ECN,
- VIRTIO_NET_F_GUEST_UFO,
- VIRTIO_NET_F_HOST_TSO4,
- VIRTIO_NET_F_HOST_TSO6,
- VIRTIO_NET_F_HOST_ECN,
- VIRTIO_NET_F_HOST_UFO,
- VIRTIO_NET_F_MRG_RXBUF,
- VIRTIO_NET_F_MTU,
- VIRTIO_F_IOMMU_PLATFORM,
- VIRTIO_F_RING_PACKED,
- VIRTIO_F_RING_RESET,
- VIRTIO_F_IN_ORDER,
- VIRTIO_NET_F_RSS,
- VIRTIO_NET_F_RSC_EXT,
- VIRTIO_NET_F_HASH_REPORT,
- VIRTIO_NET_F_GUEST_USO4,
- VIRTIO_NET_F_GUEST_USO6,
- VIRTIO_NET_F_HOST_USO,
-
- /* This bit implies RARP isn't sent by QEMU out of band */
- VIRTIO_NET_F_GUEST_ANNOUNCE,
-
- VIRTIO_NET_F_MQ,
-
- VHOST_INVALID_FEATURE_BIT
-};
-
static const int *vhost_net_get_feature_bits(struct vhost_net *net)
{
- if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
- return kernel_feature_bits;
- }
-
- if (qemu_is_vhost_user(net->nc)) {
- return user_feature_bits;
+ if (net->nc->info->vhost_feature_bits == NULL) {
+ error_report("Feature bits not defined for this type: %d",
+ net->nc->info->type);
}
-#ifdef CONFIG_VHOST_NET_VDPA
- if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
- return vdpa_feature_bits;
- }
-#endif
-
- error_report("Feature bits not defined for this type: %d",
- net->nc->info->type);
-
- return 0;
+ return net->nc->info->vhost_feature_bits;
}
uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
diff --git a/include/net/net.h b/include/net/net.h
index 8a62cd6e8aab..dd11be11a39f 100644
--- a/include/net/net.h
+++ b/include/net/net.h
@@ -94,6 +94,7 @@ typedef struct NetClientInfo {
NetAnnounce *announce;
SetSteeringEBPF *set_steering_ebpf;
NetCheckPeerType *check_peer_type;
+ const int *vhost_feature_bits;
IsVHostUser *is_vhost_user;
GetVHostNet *get_vhost_net;
} NetClientInfo;
diff --git a/include/net/vhost-vdpa.h b/include/net/vhost-vdpa.h
index 916ead3793d9..f8d7d6c9045b 100644
--- a/include/net/vhost-vdpa.h
+++ b/include/net/vhost-vdpa.h
@@ -14,6 +14,4 @@
#define TYPE_VHOST_VDPA "vhost-vdpa"
-extern const int vdpa_feature_bits[];
-
#endif /* VHOST_VDPA_H */
diff --git a/net/tap.c b/net/tap.c
index 4beba6d7a784..9e5b1a02d8d3 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -42,11 +42,29 @@
#include "qemu/error-report.h"
#include "qemu/main-loop.h"
#include "qemu/sockets.h"
+#include "hw/virtio/vhost.h"
#include "net/tap.h"
#include "net/vhost_net.h"
+static const int kernel_feature_bits[] = {
+ VIRTIO_F_NOTIFY_ON_EMPTY,
+ VIRTIO_RING_F_INDIRECT_DESC,
+ VIRTIO_RING_F_EVENT_IDX,
+ VIRTIO_NET_F_MRG_RXBUF,
+ VIRTIO_F_VERSION_1,
+ VIRTIO_NET_F_MTU,
+ VIRTIO_F_IOMMU_PLATFORM,
+ VIRTIO_F_RING_PACKED,
+ VIRTIO_F_RING_RESET,
+ VIRTIO_F_IN_ORDER,
+ VIRTIO_F_NOTIFICATION_DATA,
+ VIRTIO_NET_F_RSC_EXT,
+ VIRTIO_NET_F_HASH_REPORT,
+ VHOST_INVALID_FEATURE_BIT
+};
+
typedef struct TAPState {
NetClientState nc;
int fd;
@@ -360,6 +378,7 @@ static NetClientInfo net_tap_info = {
.set_vnet_be = tap_set_vnet_be,
.set_steering_ebpf = tap_set_steering_ebpf,
.get_vhost_net = tap_get_vhost_net,
+ .vhost_feature_bits = kernel_feature_bits,
};
static TAPState *net_tap_fd_init(NetClientState *peer,
diff --git a/net/vhost-user.c b/net/vhost-user.c
index 2caa7e56f7a0..d8fef801de5a 100644
--- a/net/vhost-user.c
+++ b/net/vhost-user.c
@@ -12,7 +12,9 @@
#include "clients.h"
#include "net/vhost_net.h"
#include "net/vhost-user.h"
+#include "hw/virtio/vhost.h"
#include "hw/virtio/vhost-user.h"
+#include "standard-headers/linux/virtio_net.h"
#include "chardev/char-fe.h"
#include "qapi/error.h"
#include "qapi/qapi-commands-net.h"
@@ -22,6 +24,46 @@
#include "qemu/option.h"
#include "trace.h"
+static const int user_feature_bits[] = {
+ VIRTIO_F_NOTIFY_ON_EMPTY,
+ VIRTIO_F_NOTIFICATION_DATA,
+ VIRTIO_RING_F_INDIRECT_DESC,
+ VIRTIO_RING_F_EVENT_IDX,
+
+ VIRTIO_F_ANY_LAYOUT,
+ VIRTIO_F_VERSION_1,
+ VIRTIO_NET_F_CSUM,
+ VIRTIO_NET_F_GUEST_CSUM,
+ VIRTIO_NET_F_GSO,
+ VIRTIO_NET_F_GUEST_TSO4,
+ VIRTIO_NET_F_GUEST_TSO6,
+ VIRTIO_NET_F_GUEST_ECN,
+ VIRTIO_NET_F_GUEST_UFO,
+ VIRTIO_NET_F_HOST_TSO4,
+ VIRTIO_NET_F_HOST_TSO6,
+ VIRTIO_NET_F_HOST_ECN,
+ VIRTIO_NET_F_HOST_UFO,
+ VIRTIO_NET_F_MRG_RXBUF,
+ VIRTIO_NET_F_MTU,
+ VIRTIO_F_IOMMU_PLATFORM,
+ VIRTIO_F_RING_PACKED,
+ VIRTIO_F_RING_RESET,
+ VIRTIO_F_IN_ORDER,
+ VIRTIO_NET_F_RSS,
+ VIRTIO_NET_F_RSC_EXT,
+ VIRTIO_NET_F_HASH_REPORT,
+ VIRTIO_NET_F_GUEST_USO4,
+ VIRTIO_NET_F_GUEST_USO6,
+ VIRTIO_NET_F_HOST_USO,
+
+ /* This bit implies RARP isn't sent by QEMU out of band */
+ VIRTIO_NET_F_GUEST_ANNOUNCE,
+
+ VIRTIO_NET_F_MQ,
+
+ VHOST_INVALID_FEATURE_BIT
+};
+
typedef struct NetVhostUserState {
NetClientState nc;
CharBackend chr; /* only queue index 0 */
@@ -240,6 +282,7 @@ static NetClientInfo net_vhost_user_info = {
.check_peer_type = vhost_user_check_peer_type,
.is_vhost_user = vhost_user_is_vhost_user,
.get_vhost_net = vhost_user_get_vhost_net,
+ .vhost_feature_bits = user_feature_bits,
};
static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond,
diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
index 0b86c917ed68..bd699066a3ab 100644
--- a/net/vhost-vdpa.c
+++ b/net/vhost-vdpa.c
@@ -55,7 +55,7 @@ typedef struct VhostVDPAState {
* with the exception of VHOST_INVALID_FEATURE_BIT,
* which should always be the last entry.
*/
-const int vdpa_feature_bits[] = {
+static const int vdpa_feature_bits[] = {
VIRTIO_F_ANY_LAYOUT,
VIRTIO_F_IOMMU_PLATFORM,
VIRTIO_F_NOTIFY_ON_EMPTY,
@@ -433,6 +433,7 @@ static NetClientInfo net_vhost_vdpa_info = {
.check_peer_type = vhost_vdpa_check_peer_type,
.set_steering_ebpf = vhost_vdpa_set_steering_ebpf,
.get_vhost_net = vhost_vdpa_get_vhost_net,
+ .vhost_feature_bits = vdpa_feature_bits,
};
static int64_t vhost_vdpa_get_vring_group(int device_fd, unsigned vq_index,
@@ -1289,6 +1290,7 @@ static NetClientInfo net_vhost_vdpa_cvq_info = {
.check_peer_type = vhost_vdpa_check_peer_type,
.set_steering_ebpf = vhost_vdpa_set_steering_ebpf,
.get_vhost_net = vhost_vdpa_get_vhost_net,
+ .vhost_feature_bits = vdpa_feature_bits,
};
/*
--
2.49.0
next prev parent reply other threads:[~2025-06-18 15:58 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-18 15:57 [PATCH v2 00/10] net: Add passt netdev backend Laurent Vivier
2025-06-18 15:57 ` [PATCH v2 01/10] net: Refactor stream logic for reuse in '-net passt' Laurent Vivier
2025-06-18 15:57 ` [PATCH v2 02/10] net: Define net_client_set_link() Laurent Vivier
2025-06-18 15:57 ` [PATCH v2 03/10] net: Introduce helper to identify vhost-user clients Laurent Vivier
2025-07-01 1:36 ` Jason Wang
2025-06-18 15:57 ` [PATCH v2 04/10] net: Add get_vhost_net callback to NetClientInfo Laurent Vivier
2025-06-18 15:57 ` Laurent Vivier [this message]
2025-07-01 1:39 ` [PATCH v2 05/10] net: Consolidate vhost feature bits into NetClientInfo Jason Wang
2025-06-18 15:57 ` [PATCH v2 06/10] net: Add get_acked_features callback to NetClientInfo Laurent Vivier
2025-06-18 15:57 ` [PATCH v2 07/10] net: Add save_acked_features " Laurent Vivier
2025-06-18 15:57 ` [PATCH v2 08/10] net: Allow network backends to advertise max TX queue size Laurent Vivier
2025-06-18 15:57 ` [PATCH v2 09/10] net: Add passt network backend Laurent Vivier
2025-06-24 8:16 ` Markus Armbruster
2025-06-24 8:37 ` Laurent Vivier
2025-06-24 11:55 ` Markus Armbruster
2025-06-24 12:03 ` Daniel P. Berrangé
2025-06-24 12:47 ` Laurent Vivier
2025-06-25 6:57 ` Markus Armbruster
2025-06-30 14:22 ` Stefano Brivio
2025-07-01 1:46 ` Jason Wang
2025-07-01 13:00 ` Laurent Vivier
2025-07-03 10:27 ` Stefano Brivio
2025-06-18 15:57 ` [PATCH v2 10/10] net/passt: Implement vhost-user backend support Laurent Vivier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250618155718.550968-6-lvivier@redhat.com \
--to=lvivier@redhat.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=dave@treblig.org \
--cc=eblake@redhat.com \
--cc=jasowang@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=sgarzare@redhat.com \
--cc=sw@weilnetz.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).