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=V/mqfp/U; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 27DFC5A0262 for ; Fri, 10 Apr 2026 09:28:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1775806115; bh=9OlQY3gi9r2WC0Xki+W1zomTvZWwAx+3hkgcO1NIVB4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=V/mqfp/U0Mq9VlKXOZt4fTrhIW9/8Yx1AH6Gmrd2rHO6QtupXzFdeed+qv3ppwR2Y ETd6RSc/G9G6pcMKSv2gcPPnIZW/PmUejOIrSeu/5UCabBSuuvU6qk34sCDy8+nlC3 5+rQbzmIkQFeDihALQd0US5/lNSiEqhUr/zo/rF4KqUmdRTnZDjvgen469qWd6R1jR pdwG82sTkA+wYLV2G+RfY/iORDVCKjbkrzWqoQJh6dRcHQ7OGVcEHEtTr5W5rebKXI MVsn7rDhVRmGRjGt0ENB8y1NMLzVx7xmgQ2nx8Wvj65u7QpVB1HepoMQqUr+Px+Ks+ vkccXYPoN7nKA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fsT1b33KTz4wM1; Fri, 10 Apr 2026 17:28:35 +1000 (AEST) Date: Fri, 10 Apr 2026 17:28:32 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v2 10/10] vhost-user: Centralise Ethernet frame padding in vu_collect() and vu_pad() Message-ID: References: <20260403163811.3209635-1-lvivier@redhat.com> <20260403163811.3209635-11-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="D/iQaDrshH9N5vch" Content-Disposition: inline In-Reply-To: <20260403163811.3209635-11-lvivier@redhat.com> Message-ID-Hash: WTDP55N64MEND7ZIHGE6DWN5SP6OQYLT X-Message-ID-Hash: WTDP55N64MEND7ZIHGE6DWN5SP6OQYLT 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: --D/iQaDrshH9N5vch Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Apr 03, 2026 at 06:38:11PM +0200, Laurent Vivier wrote: > The previous per-protocol padding done by vu_pad() in tcp_vu.c and > udp_vu.c was only correct for single-buffer frames: it assumed the > padding area always fell within the first iov, writing past its end > with a plain memset(). >=20 > It also required each caller to compute MAX(..., ETH_ZLEN + VNET_HLEN) > for vu_collect() and to call vu_pad() at the right point, duplicating > the minimum-size logic across protocols. >=20 > Move the Ethernet minimum size enforcement into vu_collect() itself, so > that enough buffer space is always reserved for padding regardless of > the requested frame size. >=20 > Rewrite vu_pad() to take a full iovec array and use iov_memset(), > making it safe for multi-buffer (mergeable rx buffer) frames. >=20 > In tcp_vu_sock_recv(), replace iov_truncate() with iov_skip_bytes(): > now that all consumers receive explicit data lengths, truncating the > iovecs is no longer needed. In tcp_vu_data_from_sock(), cap each > frame's data length against the remaining bytes actually received from > the socket, so that the last partial frame gets correct headers and > sequence number advancement. >=20 > Signed-off-by: Laurent Vivier > --- > iov.c | 1 - > tcp_vu.c | 34 ++++++++++++++++++---------------- > udp_vu.c | 14 ++++++++------ > vu_common.c | 31 +++++++++++++++---------------- > vu_common.h | 2 +- > 5 files changed, 42 insertions(+), 40 deletions(-) >=20 > diff --git a/iov.c b/iov.c > index dabc4f1ceea3..28c6d40d2986 100644 > --- a/iov.c > +++ b/iov.c > @@ -180,7 +180,6 @@ size_t iov_truncate(struct iovec *iov, size_t iov_cnt= , size_t size) > * Will write less than @length bytes if it runs out of space in > * the iov > */ > -/* cppcheck-suppress unusedFunction */ > void iov_memset(const struct iovec *iov, size_t iov_cnt, size_t offset, = int c, > size_t length) > { > diff --git a/tcp_vu.c b/tcp_vu.c > index 8c1894dca7fe..2dfe14485eee 100644 > --- a/tcp_vu.c > +++ b/tcp_vu.c > @@ -88,7 +88,7 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_ta= p_conn *conn, int flags) > =20 > elem_cnt =3D vu_collect(vdev, vq, &flags_elem[0], 1, > &flags_iov[0], 1, NULL, > - MAX(hdrlen + sizeof(*opts), ETH_ZLEN + VNET_HLEN), NULL); > + hdrlen + sizeof(*opts), NULL); > if (elem_cnt !=3D 1) > return -1; > =20 > @@ -128,8 +128,6 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_= tap_conn *conn, int flags) > return ret; > } > =20 > - l2len =3D hdrlen + optlen - VNET_HLEN; > - iov_truncate(&flags_iov[0], 1, l2len + VNET_HLEN); > payload =3D IOV_TAIL(flags_elem[0].in_sg, 1, hdrlen); > =20 > if (flags & KEEPALIVE) > @@ -138,17 +136,17 @@ int tcp_vu_send_flag(const struct ctx *c, struct tc= p_tap_conn *conn, int flags) > tcp_fill_headers(c, conn, eh, ip4h, ip6h, th, &payload, > optlen, NULL, seq, !*c->pcap); > =20 > - vu_pad(&flags_elem[0].in_sg[0], l2len); > - > + vu_pad(flags_elem[0].in_sg, 1, hdrlen + optlen); Is there a reason not to fold vu_pad() into vu_flush()? > vu_flush(vdev, vq, flags_elem, 1, hdrlen + optlen); > =20 > + l2len =3D hdrlen + optlen - VNET_HLEN; > if (*c->pcap) > pcap_iov(&flags_elem[0].in_sg[0], 1, VNET_HLEN, l2len); > =20 > if (flags & DUP_ACK) { > elem_cnt =3D vu_collect(vdev, vq, &flags_elem[1], 1, > &flags_iov[1], 1, NULL, > - flags_elem[0].in_sg[0].iov_len, NULL); > + hdrlen + optlen, NULL); > if (elem_cnt =3D=3D 1 && > flags_elem[1].in_sg[0].iov_len >=3D > flags_elem[0].in_sg[0].iov_len) { > @@ -213,7 +211,7 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c, = struct vu_virtq *vq, > ARRAY_SIZE(elem) - elem_cnt, > &iov_vu[DISCARD_IOV_NUM + iov_used], > VIRTQUEUE_MAX_SIZE - iov_used, &in_total, > - MAX(MIN(mss, fillsize) + hdrlen, ETH_ZLEN + VNET_HLEN), > + MIN(mss, fillsize) + hdrlen, > &frame_size); > if (cnt =3D=3D 0) > break; > @@ -249,8 +247,11 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c,= struct vu_virtq *vq, > if (!peek_offset_cap) > ret -=3D already_sent; > =20 > - /* adjust iov number and length of the last iov */ > - i =3D iov_truncate(&iov_vu[DISCARD_IOV_NUM], iov_used, ret); > + i =3D iov_skip_bytes(&iov_vu[DISCARD_IOV_NUM], iov_used, > + MAX(hdrlen + ret, VNET_HLEN + ETH_ZLEN), > + NULL); > + if ((size_t)i < iov_used) > + i++; > =20 > /* adjust head count */ > while (*head_cnt > 0 && head[*head_cnt - 1] >=3D i) > @@ -447,11 +448,13 @@ int tcp_vu_data_from_sock(const struct ctx *c, stru= ct tcp_tap_conn *conn) > size_t frame_size =3D iov_size(iov, buf_cnt); > bool push =3D i =3D=3D head_cnt - 1; > ssize_t dlen; > - size_t l2len; > =20 > assert(frame_size >=3D hdrlen); > =20 > dlen =3D frame_size - hdrlen; > + if (dlen > len) > + dlen =3D len; > + len -=3D dlen; > =20 > /* The IPv4 header checksum varies only with dlen */ > if (previous_dlen !=3D dlen) > @@ -460,14 +463,13 @@ int tcp_vu_data_from_sock(const struct ctx *c, stru= ct tcp_tap_conn *conn) > =20 > tcp_vu_prepare(c, conn, iov, buf_cnt, dlen, &check, !*c->pcap, push); > =20 > - /* Pad first/single buffer only, it's at least ETH_ZLEN long */ > - l2len =3D dlen + hdrlen - VNET_HLEN; > - vu_pad(iov, l2len); > - > + vu_pad(elem[head[i]].in_sg, buf_cnt, dlen + hdrlen); > vu_flush(vdev, vq, &elem[head[i]], buf_cnt, dlen + hdrlen); > =20 > - if (*c->pcap) > - pcap_iov(iov, buf_cnt, VNET_HLEN, l2len); > + if (*c->pcap) { > + pcap_iov(iov, buf_cnt, VNET_HLEN, > + dlen + hdrlen - VNET_HLEN); > + } > =20 > conn->seq_to_tap +=3D dlen; > } > diff --git a/udp_vu.c b/udp_vu.c > index 4641f42eb5c4..30af64034516 100644 > --- a/udp_vu.c > +++ b/udp_vu.c > @@ -65,7 +65,7 @@ static size_t udp_vu_hdrlen(bool v6) > static ssize_t udp_vu_sock_recv(struct iovec *iov, size_t *cnt, int s, b= ool v6) > { > struct msghdr msg =3D { 0 }; > - size_t hdrlen, l2len; > + size_t hdrlen, iov_used; > ssize_t dlen; > =20 > /* compute L2 header length */ > @@ -88,11 +88,12 @@ static ssize_t udp_vu_sock_recv(struct iovec *iov, si= ze_t *cnt, int s, bool v6) > iov[0].iov_base =3D (char *)iov[0].iov_base - hdrlen; > iov[0].iov_len +=3D hdrlen; > =20 > - *cnt =3D iov_truncate(iov, *cnt, dlen + hdrlen); > - > - /* pad frame to 60 bytes: first buffer is at least ETH_ZLEN long */ > - l2len =3D dlen + hdrlen - VNET_HLEN; > - vu_pad(&iov[0], l2len); > + iov_used =3D iov_skip_bytes(iov, *cnt, > + MAX(dlen + hdrlen, VNET_HLEN + ETH_ZLEN), > + NULL); > + if (iov_used < *cnt) > + iov_used++; > + *cnt =3D iov_used; /* one iovec per element */ > =20 > return dlen; > } > @@ -234,6 +235,7 @@ void udp_vu_sock_to_tap(const struct ctx *c, int s, i= nt n, flow_sidx_t tosidx) > pcap_iov(iov_vu, iov_cnt, VNET_HLEN, > hdrlen + dlen - VNET_HLEN); > } > + vu_pad(iov_vu, iov_cnt, hdrlen + dlen); > vu_flush(vdev, vq, elem, elem_used, hdrlen + dlen); > vu_queue_notify(vdev, vq); > } > diff --git a/vu_common.c b/vu_common.c > index 704e908aa02c..d07f584f228a 100644 > --- a/vu_common.c > +++ b/vu_common.c > @@ -74,6 +74,7 @@ int vu_collect(const struct vu_dev *vdev, struct vu_vir= tq *vq, > size_t current_iov =3D 0; > int elem_cnt =3D 0; > =20 > + size =3D MAX(size, ETH_ZLEN /* Ethernet minimum size */ + VNET_HLEN); > while (current_size < size && elem_cnt < max_elem && > current_iov < max_in_sg) { > int ret; > @@ -261,29 +262,27 @@ int vu_send_single(const struct ctx *c, const void = *buf, size_t size) > return -1; > } > =20 > - size +=3D VNET_HLEN; > elem_cnt =3D vu_collect(vdev, vq, elem, ARRAY_SIZE(elem), in_sg, > - ARRAY_SIZE(in_sg), &in_total, size, &total); > - if (elem_cnt =3D=3D 0 || total < size) { > + ARRAY_SIZE(in_sg), &in_total, VNET_HLEN + size, &total); > + if (elem_cnt =3D=3D 0 || total < VNET_HLEN + size) { > debug("vu_send_single: no space to send the data " > "elem_cnt %d size %zu", elem_cnt, total); > goto err; > } > =20 > - total -=3D VNET_HLEN; > - > /* copy data from the buffer to the iovec */ > - iov_from_buf(in_sg, in_total, VNET_HLEN, buf, total); > + iov_from_buf(in_sg, in_total, VNET_HLEN, buf, size); > =20 > if (*c->pcap) > pcap_iov(in_sg, in_total, VNET_HLEN, size); > =20 > + vu_pad(in_sg, in_total, VNET_HLEN + size); > vu_flush(vdev, vq, elem, elem_cnt, VNET_HLEN + size); > vu_queue_notify(vdev, vq); > =20 > - trace("vhost-user sent %zu", total); > + trace("vhost-user sent %zu", size); > =20 > - return total; > + return size; > err: > for (i =3D 0; i < elem_cnt; i++) > vu_queue_detach_element(vq); > @@ -292,15 +291,15 @@ err: > } > =20 > /** > - * vu_pad() - Pad 802.3 frame to minimum length (60 bytes) if needed > - * @iov: Buffer in iovec array where end of 802.3 frame is stored > - * @l2len: Layer-2 length already filled in frame > + * vu_pad() - Pad short frames to minimum Ethernet length and truncate i= ovec > + * @iov: Pointer to iovec array > + * @cnt: Number of entries in @iov > + * @frame_len: Data length in @iov (including virtio-net header) > */ > -void vu_pad(struct iovec *iov, size_t l2len) > +void vu_pad(const struct iovec *iov, size_t cnt, size_t frame_len) > { > - if (l2len >=3D ETH_ZLEN) > - return; > + size_t min_frame_len =3D ETH_ZLEN + VNET_HLEN; > =20 > - memset((char *)iov->iov_base + iov->iov_len, 0, ETH_ZLEN - l2len); > - iov->iov_len +=3D ETH_ZLEN - l2len; > + if (frame_len < min_frame_len) > + iov_memset(iov, cnt, frame_len, 0, min_frame_len - frame_len); > } > diff --git a/vu_common.h b/vu_common.h > index 77d1849e6115..51f70084a7cb 100644 > --- a/vu_common.h > +++ b/vu_common.h > @@ -44,6 +44,6 @@ void vu_flush(const struct vu_dev *vdev, struct vu_virt= q *vq, > void vu_kick_cb(struct vu_dev *vdev, union epoll_ref ref, > const struct timespec *now); > int vu_send_single(const struct ctx *c, const void *buf, size_t size); > -void vu_pad(struct iovec *iov, size_t l2len); > +void vu_pad(const struct iovec *iov, size_t cnt, size_t frame_len); > =20 > #endif /* VU_COMMON_H */ > --=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 --D/iQaDrshH9N5vch Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmnYpp8ACgkQzQJF27ox 2GeFhA/+OO3KRQhJ1GcydoxOaESSds70rnW+oa3Uksb6NlgzmFL+z2/M9AUc64kf RLZ+Bs8kuExSRBTsYl47N0DZpatoryaQcJ58aRf7nnhdSNml958JQsB8alJ8rkjl fFYv6e0FqGKl8SazNMN9xbU5dCUj0WxTkL7o+9Ueg2kJRgWRI6+XdExeLtgl+CkY xqR5KRhlOhIZ2CtjGhh+RtZJqLTaJHpDceF1LlT2BZDUOi3eJWlSM323Oml3g0Os 4g9eM+jJTCUu4Q6msMhU9X55DCg0QzvNq6wcpscjaTLIiEH+LYLKuQd7E9KqQjg/ 3NaFqgRUFuadnt5G+Tol7xcOXA5sGzClH9mm3/01gITnkLoPR53h/mzGmMp5WLKd hJJbe/aOmLbiKKxqZWYlySM5L55BfDkD41nlEmhWxyRYU7plQyHjm7HBy8bB0LNa HKXdY7vVDplsXY65uo5/nSQaK7WI8C+SxrVRwRrm2jaLuPmr3uSkLopat+ynhuLS yCRB/8vf01gcNKcQcUtnKR7XIdhehSAc9JXp0bls9dR0BkIxELy3E3GYYm/UWvx9 ZDetAaLT9MUKsZ9AjjCdev0RnfATEmsEZQg7+qJ3NMt0wvsFDzusBS4d3liQqQnu ESqA7taty8dq+N9T3vm0oK+glEbS46SGII+s7k08jy76UwxJyoE= =C+J1 -----END PGP SIGNATURE----- --D/iQaDrshH9N5vch--