From nobody Mon Feb 9 03:13:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1648666733012619.911753799729; Wed, 30 Mar 2022 11:58:53 -0700 (PDT) Received: from localhost ([::1]:35604 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nZdWt-00042W-S1 for importer@patchew.org; Wed, 30 Mar 2022 14:58:51 -0400 Received: from eggs.gnu.org ([209.51.188.92]:49628) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nZd7F-0006nY-Nm for qemu-devel@nongnu.org; Wed, 30 Mar 2022 14:32:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:56525) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nZd7A-0006ca-34 for qemu-devel@nongnu.org; Wed, 30 Mar 2022 14:32:19 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-633-ROZshcqENRiOJYaQAIMwOw-1; Wed, 30 Mar 2022 14:32:11 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 60A68185A7BA; Wed, 30 Mar 2022 18:32:11 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.195.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9CC3257ECF2; Wed, 30 Mar 2022 18:32:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648665135; 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=9A+sHtG8pHBJO54T8z69caFIuWr/WFYIUUCcELZ6RAU=; b=ToIzKdvwZJQW3M+FN1juWyzZRqHAC2lvaSTOokLFfQVXczhA79i6PfXKE3/gJbWM5HzbbG vnGVRb80K5AXLL/eU713ziFLaSs7GaKbSHme0xdlvI+qyDR3ftteuDv/HPBQYzk3VUGpg5 VP+S9Lybl8rRsHgbWOzSrIhKkOR5bn4= X-MC-Unique: ROZshcqENRiOJYaQAIMwOw-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [RFC PATCH v3 14/19] vhost: Add vhost_svq_inject Date: Wed, 30 Mar 2022 20:31:11 +0200 Message-Id: <20220330183116.358598-15-eperezma@redhat.com> In-Reply-To: <20220330183116.358598-1-eperezma@redhat.com> References: <20220330183116.358598-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.9 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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.129.124; envelope-from=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 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_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: , Cc: Laurent Vivier , Parav Pandit , Cindy Lu , "Michael S. Tsirkin" , Jason Wang , Cornelia Huck , Markus Armbruster , Gautam Dawar , Harpreet Singh Anand , Peter Xu , Eli Cohen , Paolo Bonzini , Zhu Lingshan , Eric Blake , Liuxiangdong Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1648666735185100001 This allows qemu to inject packets to the device without guest's notice. This will be use to inject net CVQ messages to restore status in the destin= ation Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-shadow-virtqueue.h | 5 + hw/virtio/vhost-shadow-virtqueue.c | 179 +++++++++++++++++++++++++---- 2 files changed, 160 insertions(+), 24 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h index 6e61d9bfef..d82a64d566 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -17,6 +17,9 @@ =20 typedef struct SVQElement { VirtQueueElement elem; + hwaddr in_iova; + hwaddr out_iova; + bool not_from_guest; } SVQElement; =20 typedef void (*VirtQueueElementCallback)(VirtIODevice *vdev, @@ -100,6 +103,8 @@ typedef struct VhostShadowVirtqueue { =20 bool vhost_svq_valid_features(uint64_t features, Error **errp); =20 +bool vhost_svq_inject(VhostShadowVirtqueue *svq, const struct iovec *iov, + size_t out_num, size_t in_num); void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd); void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd); void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index 714c820698..dc2f194e24 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -16,6 +16,7 @@ #include "qemu/log.h" #include "qemu/memalign.h" #include "linux-headers/linux/vhost.h" +#include "qemu/iov.h" =20 /** * Validate the transport device features that both guests can use with th= e SVQ @@ -122,7 +123,8 @@ static bool vhost_svq_translate_addr(const VhostShadowV= irtqueue *svq, return true; } =20 -static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr = *sg, +static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, + SVQElement *svq_elem, hwaddr *sg, const struct iovec *iovec, size_t = num, bool more_descs, bool write) { @@ -130,15 +132,39 @@ static bool vhost_svq_vring_write_descs(VhostShadowVi= rtqueue *svq, hwaddr *sg, unsigned n; uint16_t flags =3D write ? cpu_to_le16(VRING_DESC_F_WRITE) : 0; vring_desc_t *descs =3D svq->vring.desc; - bool ok; =20 if (num =3D=3D 0) { return true; } =20 - ok =3D vhost_svq_translate_addr(svq, sg, iovec, num); - if (unlikely(!ok)) { - return false; + if (svq_elem->not_from_guest) { + DMAMap map =3D { + .translated_addr =3D (hwaddr)iovec->iov_base, + .size =3D ROUND_UP(iovec->iov_len, 4096) - 1, + .perm =3D write ? IOMMU_RW : IOMMU_RO, + }; + int r; + + if (unlikely(num !=3D 1)) { + error_report("Unexpected chain of element injected"); + return false; + } + r =3D vhost_iova_tree_map_alloc(svq->iova_tree, &map); + if (unlikely(r !=3D IOVA_OK)) { + error_report("Cannot map injected element"); + return false; + } + + r =3D svq->map_ops->map(map.iova, map.size + 1, + (void *)map.translated_addr, !write, + svq->map_ops_opaque); + assert(r =3D=3D 0); + sg[0] =3D map.iova; + } else { + bool ok =3D vhost_svq_translate_addr(svq, sg, iovec, num); + if (unlikely(!ok)) { + return false; + } } =20 for (n =3D 0; n < num; n++) { @@ -165,7 +191,8 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *s= vq, SVQElement *svq_elem, unsigned avail_idx; vring_avail_t *avail =3D svq->vring.avail; bool ok; - g_autofree hwaddr *sgs =3D g_new(hwaddr, MAX(elem->out_num, elem->in_n= um)); + g_autofree hwaddr *sgs =3D NULL; + hwaddr *in_sgs, *out_sgs; =20 *head =3D svq->free_head; =20 @@ -176,15 +203,23 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue = *svq, SVQElement *svq_elem, return false; } =20 - ok =3D vhost_svq_vring_write_descs(svq, sgs, elem->out_sg, elem->out_n= um, - elem->in_num > 0, false); + if (!svq_elem->not_from_guest) { + sgs =3D g_new(hwaddr, MAX(elem->out_num, elem->in_num)); + in_sgs =3D out_sgs =3D sgs; + } else { + in_sgs =3D &svq_elem->in_iova; + out_sgs =3D &svq_elem->out_iova; + } + ok =3D vhost_svq_vring_write_descs(svq, svq_elem, out_sgs, elem->out_s= g, + elem->out_num, elem->in_num > 0, fals= e); if (unlikely(!ok)) { return false; } =20 - ok =3D vhost_svq_vring_write_descs(svq, sgs, elem->in_sg, elem->in_num= , false, - true); + ok =3D vhost_svq_vring_write_descs(svq, svq_elem, in_sgs, elem->in_sg, + elem->in_num, false, true); if (unlikely(!ok)) { + /* TODO unwind out_sg */ return false; } =20 @@ -229,6 +264,43 @@ static void vhost_svq_kick(VhostShadowVirtqueue *svq) event_notifier_set(&svq->hdev_kick); } =20 +bool vhost_svq_inject(VhostShadowVirtqueue *svq, const struct iovec *iov, + size_t out_num, size_t in_num) +{ + size_t out_size =3D iov_size(iov, out_num); + size_t out_buf_size =3D ROUND_UP(out_size, 4096); + size_t in_size =3D iov_size(iov + out_num, in_num); + size_t in_buf_size =3D ROUND_UP(in_size, 4096); + SVQElement *svq_elem; + uint16_t num_slots =3D (in_num ? 1 : 0) + (out_num ? 1 : 0); + + if (unlikely(num_slots =3D=3D 0 || svq->next_guest_avail_elem || + vhost_svq_available_slots(svq) < num_slots)) { + return false; + } + + svq_elem =3D virtqueue_alloc_element(sizeof(SVQElement), 1, 1); + if (out_num) { + void *out =3D qemu_memalign(4096, out_buf_size); + svq_elem->elem.out_sg[0].iov_base =3D out; + svq_elem->elem.out_sg[0].iov_len =3D out_size; + iov_to_buf(iov, out_num, 0, out, out_size); + memset(out + out_size, 0, out_buf_size - out_size); + } + if (in_num) { + void *in =3D qemu_memalign(4096, in_buf_size); + svq_elem->elem.in_sg[0].iov_base =3D in; + svq_elem->elem.in_sg[0].iov_len =3D in_size; + memset(in, 0, in_buf_size); + } + + svq_elem->not_from_guest =3D true; + vhost_svq_add(svq, svq_elem); + vhost_svq_kick(svq); + + return true; +} + /** * Forward available buffers. * @@ -266,6 +338,7 @@ static void vhost_handle_guest_kick(VhostShadowVirtqueu= e *svq) break; } =20 + svq_elem->not_from_guest =3D false; elem =3D &svq_elem->elem; if (elem->out_num + elem->in_num > vhost_svq_available_slots(s= vq)) { /* @@ -378,6 +451,31 @@ static SVQElement *vhost_svq_get_buf(VhostShadowVirtqu= eue *svq, uint32_t *len) return g_steal_pointer(&svq->ring_id_maps[used_elem.id]); } =20 +static int vhost_svq_unmap(VhostShadowVirtqueue *svq, hwaddr iova, size_t = size) +{ + DMAMap needle =3D { + .iova =3D iova, + .size =3D size, + }; + const DMAMap *overlap; + + while ((overlap =3D vhost_iova_tree_find(svq->iova_tree, &needle))) { + DMAMap needle =3D *overlap; + + if (svq->map_ops->unmap) { + int r =3D svq->map_ops->unmap(overlap->iova, overlap->size + 1, + svq->map_ops_opaque); + if (unlikely(r !=3D 0)) { + return r; + } + } + qemu_vfree((void *)overlap->translated_addr); + vhost_iova_tree_remove(svq->iova_tree, &needle); + } + + return 0; +} + static void vhost_svq_flush(VhostShadowVirtqueue *svq, bool check_for_avail_queue) { @@ -397,23 +495,56 @@ static void vhost_svq_flush(VhostShadowVirtqueue *svq, } =20 elem =3D &svq_elem->elem; - if (unlikely(i >=3D svq->vring.num)) { - qemu_log_mask(LOG_GUEST_ERROR, - "More than %u used buffers obtained in a %u size = SVQ", - i, svq->vring.num); - virtqueue_fill(vq, elem, len, i); - virtqueue_flush(vq, i); - return; - } - virtqueue_fill(vq, elem, len, i++); - if (svq->ops && svq->ops->used_elem_handler) { svq->ops->used_elem_handler(svq->vdev, elem); } + + if (svq_elem->not_from_guest) { + if (unlikely(!elem->out_num && elem->out_num !=3D 1)) { + error_report("Unexpected out_num > 1"); + return; + } + + if (elem->out_num) { + int r =3D vhost_svq_unmap(svq, svq_elem->out_iova, + elem->out_sg[0].iov_len); + if (unlikely(r !=3D 0)) { + error_report("Cannot unmap out buffer"); + return; + } + } + + if (unlikely(!elem->in_num && elem->in_num !=3D 1)) { + error_report("Unexpected in_num > 1"); + return; + } + + if (elem->in_num) { + int r =3D vhost_svq_unmap(svq, svq_elem->in_iova, + elem->in_sg[0].iov_len); + if (unlikely(r !=3D 0)) { + error_report("Cannot unmap out buffer"); + return; + } + } + } else { + if (unlikely(i >=3D svq->vring.num)) { + qemu_log_mask( + LOG_GUEST_ERROR, + "More than %u used buffers obtained in a %u size S= VQ", + i, svq->vring.num); + virtqueue_fill(vq, elem, len, i); + virtqueue_flush(vq, i); + return; + } + virtqueue_fill(vq, elem, len, i++); + } } =20 - virtqueue_flush(vq, i); - event_notifier_set(&svq->svq_call); + if (i > 0) { + virtqueue_flush(vq, i); + event_notifier_set(&svq->svq_call); + } =20 if (check_for_avail_queue && svq->next_guest_avail_elem) { /* @@ -576,13 +707,13 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq) for (unsigned i =3D 0; i < svq->vring.num; ++i) { g_autofree SVQElement *svq_elem =3D NULL; svq_elem =3D g_steal_pointer(&svq->ring_id_maps[i]); - if (svq_elem) { + if (svq_elem && !svq_elem->not_from_guest) { virtqueue_detach_element(svq->vq, &svq_elem->elem, 0); } } =20 next_avail_elem =3D g_steal_pointer(&svq->next_guest_avail_elem); - if (next_avail_elem) { + if (next_avail_elem && !next_avail_elem->not_from_guest) { virtqueue_detach_element(svq->vq, &next_avail_elem->elem, 0); } svq->vq =3D NULL; --=20 2.27.0