From nobody Mon Feb 9 08:11:46 2026 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=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1659547390; cv=none; d=zohomail.com; s=zohoarc; b=eLfldxZS6SAcUJJ7JfJMMzM/nhkg5V3TePVpmGDtA09pWzfuszoevzkS0io6K+eOsjnmKpViA9WTd+zJgkZ5m4uLHY+v9ugv5WWpqzAdRhuhOh0az+Z3MIIzAWy4rNZcAoRL+6Z5F69Ai8KD+mkrlbGooDp6eEzmwikr8CSF/qE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1659547390; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=FS8WOWx2Mlz/EPB9nn2iUK0MkKtiWEii0lW9lgeSWZc=; b=mFGSGxqRJkQEmdCQ7vFMc2PP6XODJ63M+d0QRo8rEoOx8BKF6Pr1By7GO01pk1ZxdEan3+8Vf6Z3AEEVRTCFoo6kW5i8G5MvMIvhCPkdTE4D8HjaPthyH2+h9rAANn6vcMzuO0nMB2CwHCbl97V9A8vlAobE6ReQ8ik7n8DXnTg= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1659547390778183.69651974507804; Wed, 3 Aug 2022 10:23:10 -0700 (PDT) Received: from localhost ([::1]:58962 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oJI5N-000139-NK for importer@patchew.org; Wed, 03 Aug 2022 13:23:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54044) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oJI1k-0004lL-AK for qemu-devel@nongnu.org; Wed, 03 Aug 2022 13:19:29 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:55491) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oJI1i-00047J-Bn for qemu-devel@nongnu.org; Wed, 03 Aug 2022 13:19:23 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-184-_Mgt6Ij6ORGrFasUXz0O6w-1; Wed, 03 Aug 2022 13:19:12 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8B5EA1C04B64; Wed, 3 Aug 2022 17:18:47 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.192.202]) by smtp.corp.redhat.com (Postfix) with ESMTP id D103C1121314; Wed, 3 Aug 2022 17:18:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659547157; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FS8WOWx2Mlz/EPB9nn2iUK0MkKtiWEii0lW9lgeSWZc=; b=hgQPa9NYTj/rVcaXi1ZOn59aJmR/+Ccp4tEJ1OW2PHvQ3+tQ5ckfHiCX/DN4tNBlEBBoXq KPDt3iuYb9HH4tnQiqkYaGm2rWg8vAD/fR2J4Wqr5CqD7FvR3Jlc+VSWVKB1ZX85WIp6nL 4yoOoZLJHhNcZTFXVegQ7sMZxqd8GNY= X-MC-Unique: _Mgt6Ij6ORGrFasUXz0O6w-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Cc: Jason Wang , Stefan Hajnoczi , Gautam Dawar , Cornelia Huck , Eli Cohen , Stefano Garzarella , Harpreet Singh Anand , Zhu Lingshan , Laurent Vivier , "Michael S. Tsirkin" , kvm@vger.kernel.org, Paolo Bonzini , Liuxiangdong , Parav Pandit , Cindy Lu , "Gonglei (Arei)" Subject: [PATCH v3 7/7] vdpa: Always start CVQ in SVQ mode Date: Wed, 3 Aug 2022 19:18:21 +0200 Message-Id: <20220803171821.481336-8-eperezma@redhat.com> In-Reply-To: <20220803171821.481336-1-eperezma@redhat.com> References: <20220803171821.481336-1-eperezma@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 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=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1659547392393100001 Isolate control virtqueue in its own group, allowing to intercept control commands but letting dataplane run totally passthrough to the guest. Signed-off-by: Eugenio P=C3=A9rez --- v3: * Make asid related queries print a warning instead of returning an error and stop the start of qemu. --- hw/virtio/vhost-vdpa.c | 3 +- net/vhost-vdpa.c | 144 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 142 insertions(+), 5 deletions(-) diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 131100841c..a4cb68862b 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -674,7 +674,8 @@ static int vhost_vdpa_set_backend_cap(struct vhost_dev = *dev) { uint64_t features; uint64_t f =3D 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 | - 0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH; + 0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH | + 0x1ULL << VHOST_BACKEND_F_IOTLB_ASID; int r; =20 if (vhost_vdpa_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) { diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c index e3b65ed546..5f39f0edb5 100644 --- a/net/vhost-vdpa.c +++ b/net/vhost-vdpa.c @@ -37,6 +37,9 @@ typedef struct VhostVDPAState { /* Control commands shadow buffers */ void *cvq_cmd_out_buffer, *cvq_cmd_in_buffer; =20 + /* Number of address spaces supported by the device */ + unsigned address_space_num; + /* The device always have SVQ enabled */ bool always_svq; bool started; @@ -100,6 +103,8 @@ static const uint64_t vdpa_svq_device_features =3D BIT_ULL(VIRTIO_NET_F_RSC_EXT) | BIT_ULL(VIRTIO_NET_F_STANDBY); =20 +#define VHOST_VDPA_NET_CVQ_ASID 1 + VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc) { VhostVDPAState *s =3D DO_UPCAST(VhostVDPAState, nc, nc); @@ -224,6 +229,101 @@ static NetClientInfo net_vhost_vdpa_info =3D { .check_peer_type =3D vhost_vdpa_check_peer_type, }; =20 +static void vhost_vdpa_get_vring_group(int device_fd, + struct vhost_vring_state *state) +{ + int r =3D ioctl(device_fd, VHOST_VDPA_GET_VRING_GROUP, state); + if (unlikely(r < 0)) { + /* + * Assume all groups are 0, the consequences are the same and we w= ill + * not abort device creation + */ + state->num =3D 0; + } +} + +/** + * Check if all the virtqueues of the virtio device are in a different vq = than + * the last vq. VQ group of last group passed in cvq_group. + */ +static bool vhost_vdpa_cvq_group_is_independent(struct vhost_vdpa *v, + struct vhost_vring_state cvq_g= roup) +{ + struct vhost_dev *dev =3D v->dev; + + for (int i =3D 0; i < (dev->vq_index_end - 1); ++i) { + struct vhost_vring_state vq_group =3D { + .index =3D i, + }; + + vhost_vdpa_get_vring_group(v->device_fd, &vq_group); + if (unlikely(vq_group.num =3D=3D cvq_group.num)) { + warn_report("CVQ %u group is the same as VQ %u one (%u)", + cvq_group.index, vq_group.index, cvq_group.num); + return false; + } + } + + return true; +} + +static int vhost_vdpa_set_address_space_id(struct vhost_vdpa *v, + unsigned vq_group, + unsigned asid_num) +{ + struct vhost_vring_state asid =3D { + .index =3D vq_group, + .num =3D asid_num, + }; + int ret; + + ret =3D ioctl(v->device_fd, VHOST_VDPA_SET_GROUP_ASID, &asid); + if (unlikely(ret < 0)) { + warn_report("Can't set vq group %u asid %u, errno=3D%d (%s)", + asid.index, asid.num, errno, g_strerror(errno)); + } + return ret; +} + +static void vhost_vdpa_net_prepare(NetClientState *nc) +{ + VhostVDPAState *s =3D DO_UPCAST(VhostVDPAState, nc, nc); + struct vhost_vdpa *v =3D &s->vhost_vdpa; + struct vhost_dev *dev =3D v->dev; + struct vhost_vring_state cvq_group =3D { + .index =3D v->dev->vq_index_end - 1, + }; + int r; + + assert(nc->info->type =3D=3D NET_CLIENT_DRIVER_VHOST_VDPA); + + if (dev->nvqs !=3D 1 || dev->vq_index + dev->nvqs !=3D dev->vq_index_e= nd) { + /* Only interested in CVQ */ + return; + } + + if (s->always_svq) { + /* SVQ is already enabled */ + return; + } + + if (s->address_space_num < 2) { + v->shadow_vqs_enabled =3D false; + return; + } + + vhost_vdpa_get_vring_group(v->device_fd, &cvq_group); + if (!vhost_vdpa_cvq_group_is_independent(v, cvq_group)) { + v->shadow_vqs_enabled =3D false; + return; + } + + r =3D vhost_vdpa_set_address_space_id(v, cvq_group.num, + VHOST_VDPA_NET_CVQ_ASID); + v->shadow_vqs_enabled =3D r =3D=3D 0; + s->vhost_vdpa.address_space_id =3D r =3D=3D 0 ? 1 : 0; +} + static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr) { VhostIOVATree *tree =3D v->iova_tree; @@ -431,6 +531,7 @@ static NetClientInfo net_vhost_vdpa_cvq_info =3D { .type =3D NET_CLIENT_DRIVER_VHOST_VDPA, .size =3D sizeof(VhostVDPAState), .receive =3D vhost_vdpa_receive, + .prepare =3D vhost_vdpa_net_prepare, .load =3D vhost_vdpa_net_load, .cleanup =3D vhost_vdpa_cleanup, .has_vnet_hdr =3D vhost_vdpa_has_vnet_hdr, @@ -541,12 +642,38 @@ static const VhostShadowVirtqueueOps vhost_vdpa_net_s= vq_ops =3D { .avail_handler =3D vhost_vdpa_net_handle_ctrl_avail, }; =20 +static uint32_t vhost_vdpa_get_as_num(int vdpa_device_fd) +{ + uint64_t features; + unsigned num_as; + int r; + + r =3D ioctl(vdpa_device_fd, VHOST_GET_BACKEND_FEATURES, &features); + if (unlikely(r < 0)) { + warn_report("Cannot get backend features"); + return 1; + } + + if (!(features & BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID))) { + return 1; + } + + r =3D ioctl(vdpa_device_fd, VHOST_VDPA_GET_AS_NUM, &num_as); + if (unlikely(r < 0)) { + warn_report("Cannot retrieve number of supported ASs"); + return 1; + } + + return num_as; +} + static NetClientState *net_vhost_vdpa_init(NetClientState *peer, const char *device, const char *name, int vdpa_device_fd, int queue_pair_index, int nvqs, + unsigned nas, bool is_datapath, bool svq, VhostIOVATree *iova_tree) @@ -565,6 +692,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientSta= te *peer, snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA); s =3D DO_UPCAST(VhostVDPAState, nc, nc); =20 + s->address_space_num =3D nas; s->vhost_vdpa.device_fd =3D vdpa_device_fd; s->vhost_vdpa.index =3D queue_pair_index; s->always_svq =3D svq; @@ -650,6 +778,8 @@ int net_init_vhost_vdpa(const Netdev *netdev, const cha= r *name, g_autoptr(VhostIOVATree) iova_tree =3D NULL; NetClientState *nc; int queue_pairs, r, i =3D 0, has_cvq =3D 0; + unsigned num_as =3D 1; + bool svq_cvq; =20 assert(netdev->type =3D=3D NET_CLIENT_DRIVER_VHOST_VDPA); opts =3D &netdev->u.vhost_vdpa; @@ -674,7 +804,13 @@ int net_init_vhost_vdpa(const Netdev *netdev, const ch= ar *name, goto err; } =20 - if (opts->x_svq) { + svq_cvq =3D opts->x_svq; + if (has_cvq && !opts->x_svq) { + num_as =3D vhost_vdpa_get_as_num(vdpa_device_fd); + svq_cvq =3D num_as > 1; + } + + if (opts->x_svq || svq_cvq) { struct vhost_vdpa_iova_range iova_range; =20 uint64_t invalid_dev_features =3D @@ -697,15 +833,15 @@ int net_init_vhost_vdpa(const Netdev *netdev, const c= har *name, =20 for (i =3D 0; i < queue_pairs; i++) { ncs[i] =3D net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, - vdpa_device_fd, i, 2, true, opts->x_s= vq, - iova_tree); + vdpa_device_fd, i, 2, num_as, true, + opts->x_svq, iova_tree); if (!ncs[i]) goto err; } =20 if (has_cvq) { nc =3D net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, - vdpa_device_fd, i, 1, false, + vdpa_device_fd, i, 1, num_as, false, opts->x_svq, iova_tree); if (!nc) goto err; --=20 2.31.1