From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202602 header.b=k8w/waO6; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 68B155A0262 for ; Tue, 24 Mar 2026 03:41:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1774320083; bh=giycO6cRVeOlCx4PugbIVPfIxkxsyyFBreNciMlTw1Y=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=k8w/waO6OYkCM4VjdxkQ3XT6UCNgFCQOCfHxx3IV8mwFhq3xtQ3tAuI6Y2J27zHbg L0KFQMFK7yWr8YXUpg7w9jA9XlHwwSl73EqXc0di/Xr8TOsvYbtMBc7Hk+T8+f0B3l oxfOe7j/YNdqLVjlRevUWAbZ2KtT87aqTPMLkEnouTKcwYQW2uDsLlfh0ICErDoHd6 K66uHnkokX2ihbFx4mlhy/jU0qq5mvQY8aNFzw+IpJDa6Up+cUwbaHfTx1WedrY50c gSV3ASXeK54jj71tPnrRnqN50ul2ww26nuwdExvC3F9JhHV8e4nafroIUzP5WadcMS ZTY0SS6+7wZBg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4ffvS30gk8z4w9T; Tue, 24 Mar 2026 13:41:23 +1100 (AEDT) Date: Tue, 24 Mar 2026 13:37:31 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v4 3/5] udp_vu: Move virtqueue management from udp_vu_sock_recv() to its caller Message-ID: References: <20260323143151.538673-1-lvivier@redhat.com> <20260323143151.538673-4-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="a6AmyODj2mnA+w3t" Content-Disposition: inline In-Reply-To: <20260323143151.538673-4-lvivier@redhat.com> Message-ID-Hash: 5RUPHPRSSBLKB6GLDED6HPTS473HXTP3 X-Message-ID-Hash: 5RUPHPRSSBLKB6GLDED6HPTS473HXTP3 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --a6AmyODj2mnA+w3t Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Mar 23, 2026 at 03:31:49PM +0100, Laurent Vivier wrote: > udp_vu_sock_recv() currently mixes two concerns: receiving data from the > socket and managing virtqueue buffers (collecting, rewinding, releasing). > This makes the function harder to reason about and couples socket I/O > with virtqueue state. >=20 > Move all virtqueue operations, vu_collect(), vu_init_elem(), > vu_queue_rewind(), vu_set_vnethdr(), and the queue-readiness check, into > udp_vu_sock_to_tap(), which is the only caller. This turns > udp_vu_sock_recv() into a pure socket receive function that simply reads > into the provided iov array and adjusts its length. >=20 > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson > --- > udp_vu.c | 110 +++++++++++++++++++++++++------------------------------ > 1 file changed, 49 insertions(+), 61 deletions(-) >=20 > diff --git a/udp_vu.c b/udp_vu.c > index 6638cb0ddc5f..dc949e30a793 100644 > --- a/udp_vu.c > +++ b/udp_vu.c > @@ -33,9 +33,6 @@ > #include "udp_vu.h" > #include "vu_common.h" > =20 > -static struct iovec iov_vu [VIRTQUEUE_MAX_SIZE]; > -static struct vu_virtq_element elem [VIRTQUEUE_MAX_SIZE]; > - > /** > * udp_vu_hdrlen() - Sum size of all headers, from UDP to virtio-net > * @v6: Set for IPv6 packet > @@ -58,78 +55,35 @@ static size_t udp_vu_hdrlen(bool v6) > =20 > /** > * udp_vu_sock_recv() - Receive datagrams from socket into vhost-user bu= ffers > - * @c: Execution context > * @iov: IO vector for the frame (in/out) > * @cnt: Number of IO vector entries (in/out) > - * @vq: virtqueue to use to receive data > * @s: Socket to receive from > * @v6: Set for IPv6 connections > * > - * Return: size of received data, 0 if the datagram > - * was discarded because the virtqueue is not ready, -1 on error > + * Return: size of received data, -1 on error > */ > -static ssize_t udp_vu_sock_recv(const struct ctx *c, struct iovec *iov, > - size_t *cnt, unsigned *elem_used, > - struct vu_virtq *vq, int s, bool v6) > +static ssize_t udp_vu_sock_recv(struct iovec *iov, size_t *cnt, int s, b= ool v6) > { > - const struct vu_dev *vdev =3D c->vdev; > - struct msghdr msg =3D { 0 }; > + struct iovec msg_iov[*cnt]; > + struct msghdr msg =3D { 0 }; > struct iov_tail payload; > - size_t hdrlen, iov_used; > - unsigned elem_cnt; > - unsigned i, j; > + size_t hdrlen; > ssize_t dlen; > =20 > - assert(!c->no_udp); > - > - if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) { > - debug("Got UDP packet, but RX virtqueue not usable yet"); > - > - if (recvmsg(s, &msg, MSG_DONTWAIT) < 0) > - debug_perror("Failed to discard datagram"); > - > - *cnt =3D 0; > - return 0; > - } > - > /* compute L2 header length */ > hdrlen =3D udp_vu_hdrlen(v6); > =20 > - elem_cnt =3D vu_collect(vdev, vq, elem, ARRAY_SIZE(elem), > - iov, *cnt, &iov_used, > - IP_MAX_MTU + ETH_HLEN + VNET_HLEN, NULL); > - if (elem_cnt =3D=3D 0) > - return -1; > - > - assert((size_t)elem_cnt =3D=3D iov_used); /* one iovec per element */ > - > - payload =3D IOV_TAIL(iov, iov_used, hdrlen); > + payload =3D IOV_TAIL(iov, *cnt, hdrlen); > =20 > - struct iovec msg_iov[payload.cnt]; > msg.msg_iov =3D msg_iov; > msg.msg_iovlen =3D iov_tail_clone(msg.msg_iov, payload.cnt, &payload); > =20 > /* read data from the socket */ > dlen =3D recvmsg(s, &msg, 0); > - if (dlen < 0) { > - vu_queue_rewind(vq, elem_cnt); > + if (dlen < 0) > return -1; > - } > - > - *cnt =3D vu_pad(iov, iov_used, 0, dlen + hdrlen); > - > - *elem_used =3D 0; > - for (i =3D 0, j =3D 0; j < *cnt && i < elem_cnt; i++) { > - if (j + elem[i].in_num > *cnt) > - elem[i].in_num =3D *cnt - j; > - j +=3D elem[i].in_num; > - (*elem_used)++; > - } > =20 > - vu_set_vnethdr(iov[0].iov_base, *elem_used); > - > - /* release unused buffers */ > - vu_queue_rewind(vq, elem_cnt - *elem_used); > + *cnt =3D vu_pad(iov, *cnt, 0, dlen + hdrlen); > =20 > return dlen; > } > @@ -217,26 +171,60 @@ static void udp_vu_csum(const struct flowside *tosi= de, > */ > void udp_vu_sock_to_tap(const struct ctx *c, int s, int n, flow_sidx_t t= osidx) > { > + static struct iovec iov_vu [VIRTQUEUE_MAX_SIZE]; > + static struct vu_virtq_element elem [VIRTQUEUE_MAX_SIZE]; > const struct flowside *toside =3D flowside_at_sidx(tosidx); > bool v6 =3D !(inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)); > struct vu_dev *vdev =3D c->vdev; > struct vu_virtq *vq =3D &vdev->vq[VHOST_USER_RX_QUEUE]; > - struct iov_tail data; > int i; > =20 > + assert(!c->no_udp); > + > + if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) { > + struct msghdr msg =3D { 0 }; > + > + debug("Got UDP packet, but RX virtqueue not usable yet"); > + > + for (i =3D 0; i < n; i++) { > + if (recvmsg(s, &msg, MSG_DONTWAIT) < 0) > + debug_perror("Failed to discard datagram"); > + } > + > + return; > + } > + > for (i =3D 0; i < n; i++) { > - unsigned elem_used; > + unsigned elem_used, elem_cnt, j, k; > size_t iov_cnt; > ssize_t dlen; > =20 > - iov_cnt =3D ARRAY_SIZE(iov_vu); > - dlen =3D udp_vu_sock_recv(c, iov_vu, &iov_cnt, &elem_used, vq, > - s, v6); > - if (dlen < 0) > + elem_cnt =3D vu_collect(vdev, vq, elem, ARRAY_SIZE(elem), > + iov_vu, ARRAY_SIZE(iov_vu), &iov_cnt, > + IP_MAX_MTU + ETH_HLEN + VNET_HLEN, NULL); > + if (elem_cnt =3D=3D 0) > + break; > + > + dlen =3D udp_vu_sock_recv(iov_vu, &iov_cnt, s, v6); > + if (dlen < 0) { > + vu_queue_rewind(vq, elem_cnt); > break; > + } > + > + elem_used =3D 0; > + for (j =3D 0, k =3D 0; k < iov_cnt && j < elem_cnt; j++) { > + if (k + elem[j].in_num > iov_cnt) > + elem[j].in_num =3D iov_cnt - k; > + k +=3D elem[j].in_num; > + elem_used++; > + } > + > + /* release unused buffers */ > + vu_queue_rewind(vq, elem_cnt - elem_used); > =20 > if (iov_cnt > 0) { > - data =3D IOV_TAIL(iov_vu, iov_cnt, 0); > + struct iov_tail data =3D IOV_TAIL(iov_vu, iov_cnt, 0); > + vu_set_vnethdr(iov_vu[0].iov_base, elem_used); > udp_vu_prepare(c, &data, toside, dlen); > if (*c->pcap) { > udp_vu_csum(toside, &data); > --=20 > 2.53.0 >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --a6AmyODj2mnA+w3t Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmnB+OoACgkQzQJF27ox 2GeGng/9EaMc+7uut9mu5BJPcROGMMtCbf4Mz3O6jQyhuGtEZrjzYsrSs2A5lr/S oDh/Ci2jgxHZCN1OXldV1sjFWujLvEMV7w7sfTMiR5VJsfjLaRmc1kVCoyuYtl8T 7pAmWHNyO7YLUL0X4+tWO/UM5v2AaDpapdwktNEyH59ikP1gHxFDsp6aGeB4tNvY nMFu+rEF9lv/Ps+pA0hAMXXAGbt12RNaWYfWgmN546QQ5v6o51XOk8A3ZieS/pnO EzmT2DyeHCtcMHL3BvzFMWi/JWcwJhFiO3Ta3YzReg7tA8CJ1Is4uhby300wQsGe Bv/VoA9oTHM/bJbMkBVDUsPKsohfduCuQYGaMSYDWK/nj+YMYw7JTir43WSLlShJ geRa6cvqMx5EDMnf0MCSJSJ/Xrfaqgqe9m0C7isYaHhkSevXqhrx76x+X0B/xOA2 SCSH76wtyuIAoCsPhbyfhswkL88nTH8RWXEParxKFNYoFIvug+Fdd5ksvOJqIr3q vOje8ylzDJTR692UaVpW985UoOJwi3uuLlrG+EmlvgEirxQQjl0RygB3dfg68jZv LU6h+ZyJcQs3+thb8MyY6bkuV+it43bdyt921LPyFFG1J8i+XPmCmZ0q+Ymv3ZCE 99lIKsMdfEbho0U+w9ugqyiT3yySVo3fMvaiZZdMtS+wPUb51sE= =gSEV -----END PGP SIGNATURE----- --a6AmyODj2mnA+w3t--