diff options
author | Laurent Vivier <lvivier@redhat.com> | 2025-07-09 10:24:18 +0200 |
---|---|---|
committer | Jason Wang <jasowang@redhat.com> | 2025-07-14 13:27:09 +0800 |
commit | 8f6e5c620a5b21c070eed93721236cad48b6f9d7 (patch) | |
tree | 6665b71774b42435e4fa95d6c3cb7716d6c4765c /hw | |
parent | 7136352b40631b058dd0fe731a0d404e761e799f (diff) | |
download | qemu-8f6e5c620a5b21c070eed93721236cad48b6f9d7.zip qemu-8f6e5c620a5b21c070eed93721236cad48b6f9d7.tar.gz qemu-8f6e5c620a5b21c070eed93721236cad48b6f9d7.tar.bz2 |
net: Add get_vhost_net callback to NetClientInfo
The get_vhost_net() function previously contained a large switch
statement to find the VHostNetState pointer based on the net
client's type. This created a tight coupling, requiring the generic
vhost layer to be aware of every specific backend that supported
vhost, such as tap, vhost-user, and vhost-vdpa.
This approach is not scalable and requires modifying a central function
for any new backend. It also forced each backend to expose its internal
getter function in a public header file.
This patch refactors the logic by introducing a new get_vhost_net
function pointer to the NetClientInfo struct. The central
get_vhost_net() function is now a simple, generic dispatcher that
invokes the callback provided by the net client.
Each backend now implements its own private getter and registers it in
its NetClientInfo.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
Diffstat (limited to 'hw')
-rw-r--r-- | hw/net/vhost_net.c | 31 |
1 files changed, 4 insertions, 27 deletions
diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c index cb87056..db8b97b 100644 --- a/hw/net/vhost_net.c +++ b/hw/net/vhost_net.c @@ -649,41 +649,18 @@ void vhost_net_config_mask(VHostNetState *net, VirtIODevice *dev, bool mask) { vhost_config_mask(&net->dev, dev, mask); } + VHostNetState *get_vhost_net(NetClientState *nc) { - VHostNetState *vhost_net = 0; - if (!nc) { return 0; } - switch (nc->info->type) { - case NET_CLIENT_DRIVER_TAP: - vhost_net = tap_get_vhost_net(nc); - /* - * tap_get_vhost_net() can return NULL if a tap net-device backend is - * created with 'vhost=off' option, 'vhostforce=off' or no vhost or - * vhostforce or vhostfd options at all. Please see net_init_tap_one(). - * Hence, we omit the assertion here. - */ - break; -#ifdef CONFIG_VHOST_NET_USER - case NET_CLIENT_DRIVER_VHOST_USER: - vhost_net = vhost_user_get_vhost_net(nc); - assert(vhost_net); - break; -#endif -#ifdef CONFIG_VHOST_NET_VDPA - case NET_CLIENT_DRIVER_VHOST_VDPA: - vhost_net = vhost_vdpa_get_vhost_net(nc); - assert(vhost_net); - break; -#endif - default: - break; + if (nc->info->get_vhost_net) { + return nc->info->get_vhost_net(nc); } - return vhost_net; + return NULL; } int vhost_net_set_vring_enable(NetClientState *nc, int enable) |