From nobody Sat Nov 15 16:36:06 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1750236116; cv=none; d=zohomail.com; s=zohoarc; b=ZF7WkbXBOLCZvCrNVXfWUzSNyAJZOcAQLLsqPDAoWTi+arra97zNa+jorS7SqCz4UcDRO+Kt5+cqeQXS6Ba4ov9po5QUm62RR3FPRdufHP7cwJreALW1Ryw1WvoPb0uoDgcduwMFtey0h5Yo+CFwqh1GDGyyJ9tIBEfUYarJO08= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750236116; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=JpoESKiiI1Pf52xnsFbE2a8bgaW+GSdR3SgxO0yJUas=; b=FyEz0mNMiuXAifi5q/p9w5yhvZM0mSvBq7jS3SzTItu9yNXn89KppLX4nk+Mmm98B9q6sDb90ylVXhy9eeT16BpCSs1mDnIFganCcw7yvySUGLaBiSbNrxg9LkY8dPPbzIYY2pE6kkwUbLubJ8BmKmuu3ytLNkS/KVGINekKzDQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1750236116957384.1075070219897; Wed, 18 Jun 2025 01:41:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uRoML-0005nw-Uw; Wed, 18 Jun 2025 04:41:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uRoM5-0005kC-GI for qemu-devel@nongnu.org; Wed, 18 Jun 2025 04:41:14 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uRoM2-0002aK-2k for qemu-devel@nongnu.org; Wed, 18 Jun 2025 04:41:13 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-281-RucAslPeOmiQ4nPvyDdAwQ-1; Wed, 18 Jun 2025 04:39:52 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0FE5018002A0; Wed, 18 Jun 2025 08:39:51 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.44.33.123]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C82FC19560A3; Wed, 18 Jun 2025 08:39:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750236061; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JpoESKiiI1Pf52xnsFbE2a8bgaW+GSdR3SgxO0yJUas=; b=ZK4WPwsfxUq6efNdT9Pcz0E1MTYt8s2hCQYtXgmx3hNrGCWOmVu+uLJ8gr5MAhFSF6dfZT ZIyvK3X8KWfajr3uLb0gRSibnlWk0rDxQTLMM/Eug5ikwWWRNF4cl27m/OlZheYxeWKHAT Bg19XopowjIUEgkcSB+btPQMPG0RZRs= X-MC-Unique: RucAslPeOmiQ4nPvyDdAwQ-1 X-Mimecast-MFC-AGG-ID: RucAslPeOmiQ4nPvyDdAwQ_1750235991 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Paolo Bonzini , "Michael S. Tsirkin" , Eric Blake , Stefano Garzarella , Stefan Weil , Jason Wang , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Dr. David Alan Gilbert" , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Laurent Vivier Subject: [PATCH 3/8] net: Introduce helper to identify vhost-user clients Date: Wed, 18 Jun 2025 10:39:25 +0200 Message-ID: <20250618083930.451313-4-lvivier@redhat.com> In-Reply-To: <20250618083930.451313-1-lvivier@redhat.com> References: <20250618083930.451313-1-lvivier@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=lvivier@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -39 X-Spam_score: -4.0 X-Spam_bar: ---- X-Spam_report: (-4.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.89, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1750236118106116600 Content-Type: text/plain; charset="utf-8" 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 --- 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[] =3D { =20 static const int *vhost_net_get_feature_bits(struct vhost_net *net) { - const int *feature_bits =3D 0; + if (net->nc->info->type =3D=3D NET_CLIENT_DRIVER_TAP) { + return kernel_feature_bits; + } + + if (qemu_is_vhost_user(net->nc)) { + return user_feature_bits; + } =20 - switch (net->nc->info->type) { - case NET_CLIENT_DRIVER_TAP: - feature_bits =3D kernel_feature_bits; - break; - case NET_CLIENT_DRIVER_VHOST_USER: - feature_bits =3D user_feature_bits; - break; #ifdef CONFIG_VHOST_NET_VDPA - case NET_CLIENT_DRIVER_VHOST_VDPA: - feature_bits =3D 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 =3D=3D NET_CLIENT_DRIVER_VHOST_VDPA) { + return vdpa_feature_bits; } +#endif =20 - return feature_bits; + error_report("Feature bits not defined for this type: %d", + net->nc->info->type); + + return 0; } =20 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 =3D=3D NET_CLIENT_DRIVER_VHOST_USER) { + if (qemu_is_vhost_user(net->nc)) { dev->use_guest_notifier_mask =3D false; } } diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 221252e00a50..abb5e5a9860d 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; } =20 - 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 =3D=3D NET_CLIENT_DRIVER_VHOST_VDPA) { + return VIRTQUEUE_MAX_SIZE; + } + + return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE; } =20 static int peer_attach(VirtIONet *n, int index) @@ -696,7 +698,7 @@ static int peer_attach(VirtIONet *n, int index) return 0; } =20 - if (nc->peer->info->type =3D=3D NET_CLIENT_DRIVER_VHOST_USER) { + if (qemu_is_vhost_user(nc->peer)) { vhost_set_vring_enable(nc->peer, 1); } =20 @@ -719,7 +721,7 @@ static int peer_detach(VirtIONet *n, int index) return 0; } =20 - if (nc->peer->info->type =3D=3D NET_CLIENT_DRIVER_VHOST_USER) { + if (qemu_is_vhost_user(nc->peer)) { vhost_set_vring_enable(nc->peer, 0); } =20 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 *r= s); typedef void (NetAnnounce)(NetClientState *); typedef bool (SetSteeringEBPF)(NetClientState *, int); typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **); +typedef bool (IsVHostUser)(NetClientState *); =20 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; =20 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 } =20 +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; } =20 +static bool vhost_user_is_vhost_user(NetClientState *nc) +{ + assert(nc->info->type =3D=3D NET_CLIENT_DRIVER_VHOST_USER); + + return true; +} + static NetClientInfo net_vhost_user_info =3D { .type =3D NET_CLIENT_DRIVER_VHOST_USER, .size =3D sizeof(NetVhostUserState), @@ -231,6 +238,7 @@ static NetClientInfo net_vhost_user_info =3D { .set_vnet_be =3D vhost_user_set_vnet_endianness, .set_vnet_le =3D vhost_user_set_vnet_endianness, .check_peer_type =3D vhost_user_check_peer_type, + .is_vhost_user =3D vhost_user_is_vhost_user, }; =20 static gboolean net_vhost_user_watch(void *do_not_use, GIOCondition cond, --=20 2.49.0