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=lSoEIAop; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 36B7B5A0262 for ; Tue, 19 May 2026 07:08:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1779167317; bh=Gr5sUMN8m4c0wh0neAkma1tpWp9gSyFybeHQRyC1lYM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=lSoEIAoputGZbK3azWyQphsnHAL5yPeYBtw2OozsY30osrBSLT3k2Bm9/8GZbxJe/ tVjXUXwIND1JdAG0jrJSftHM89GeALjD9FU1ImWpWoEhZ0bxd/MieEmvVYA5lIbqei QghffQBFSADqwRUu9Oy5uwpqe+wHzrcRh4IZQF/9XjF2AqBqWvD/r51HRKc37WCewY 0+mqW8TI8vgqulpVpiStXE+ca09EL7/SilhiPrftYjAqL3iYJkk0g101U1f3Ik7byd 8oAs12mgD6l9rNMcNpRel2CTCMJFVTL1xv5HG0Bjk4uepv5QvjCJKDAuEc8vu1ZC6Z wktYEmhKEFTsA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4gKN45670kz4xdl; Tue, 19 May 2026 15:08:37 +1000 (AEST) Date: Tue, 19 May 2026 15:08:27 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v9 3/3] udp: Pass iov_tail to udp_update_hdr4()/udp_update_hdr6() Message-ID: References: <20260518122625.2290034-1-lvivier@redhat.com> <20260518122625.2290034-4-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ZtGXeE50pM3J0sA5" Content-Disposition: inline In-Reply-To: <20260518122625.2290034-4-lvivier@redhat.com> Message-ID-Hash: XNXQD7TP3GRAGEJE7YCRAGWLYL3TYS4E X-Message-ID-Hash: XNXQD7TP3GRAGEJE7YCRAGWLYL3TYS4E 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: --ZtGXeE50pM3J0sA5 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, May 18, 2026 at 02:26:25PM +0200, Laurent Vivier wrote: > Change udp_update_hdr4() and udp_update_hdr6() to take an iov_tail > pointing at the UDP frame instead of a contiguous udp_payload_t buffer > and explicit data length. This lets vhost-user pass scatter-gather > virtqueue buffers directly without an intermediate copy. >=20 > The UDP header is built into a local struct udphdr and written back with > IOV_PUSH_HEADER(). On the tap side, udp_tap_prepare() wraps the > existing udp_payload_t in a two-element iov to match the new interface. >=20 > Signed-off-by: Laurent Vivier Alas, this still has a potentially aliased memcpy(), see below. > --- > iov.c | 1 - > udp.c | 73 ++++++++++++++++++++++--------------------- > udp_internal.h | 4 +-- > udp_vu.c | 85 ++++++++++++++++++++++++++------------------------ > 4 files changed, 85 insertions(+), 78 deletions(-) >=20 > diff --git a/iov.c b/iov.c > index 6a5d7d35b67f..9248ba95a9f2 100644 > --- a/iov.c > +++ b/iov.c > @@ -367,7 +367,6 @@ void *iov_peek_header_(struct iov_tail *tail, void *v= , size_t len, size_t align) > * > * Return: number of bytes written > */ > -/* cppcheck-suppress unusedFunction */ > size_t iov_push_header_(struct iov_tail *tail, const void *v, size_t len) > { > size_t l; > diff --git a/udp.c b/udp.c > index 66dc7766868c..8ea8ac848dab 100644 > --- a/udp.c > +++ b/udp.c > @@ -255,20 +255,21 @@ static void udp_iov_init(const struct ctx *c) > /** > * udp_update_hdr4() - Update headers for one IPv4 datagram > * @ip4h: Pre-filled IPv4 header (except for tot_len and saddr) > - * @bp: Pointer to udp_payload_t to update > + * @payload: iov_tail including UDP header on entry, excluding it on ex= it It's not necessarily essential to correctness, but I think things will be cleaner if you make this take @uh (as a pointer) and @payload (excluding the UDP header) as separate parameters. For starters @payload can then be const and won't cover different things on entry and exit. > * @toside: Flowside for destination side > * @dlen: Length of UDP payload > * @no_udp_csum: Do not set UDP checksum > * > - * Return: size of IPv4 payload (UDP header + data) > + * Return: size of datagram (UDP header + data) > */ > -size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp, > +size_t udp_update_hdr4(struct iphdr *ip4h, struct iov_tail *payload, > const struct flowside *toside, size_t dlen, > bool no_udp_csum) > { > const struct in_addr *src =3D inany_v4(&toside->oaddr); > const struct in_addr *dst =3D inany_v4(&toside->eaddr); > - size_t l4len =3D dlen + sizeof(bp->uh); > + struct udphdr uh; > + size_t l4len =3D dlen + sizeof(uh); > size_t l3len =3D l4len + sizeof(*ip4h); > =20 > assert(src && dst); > @@ -278,19 +279,18 @@ size_t udp_update_hdr4(struct iphdr *ip4h, struct u= dp_payload_t *bp, > ip4h->saddr =3D src->s_addr; > ip4h->check =3D csum_ip4_header(l3len, IPPROTO_UDP, *src, *dst); > =20 > - bp->uh.source =3D htons(toside->oport); > - bp->uh.dest =3D htons(toside->eport); > - bp->uh.len =3D htons(l4len); > + uh.source =3D htons(toside->oport); > + uh.dest =3D htons(toside->eport); > + uh.len =3D htons(l4len); > if (no_udp_csum) { > - bp->uh.check =3D 0; > + uh.check =3D 0; > } else { > - const struct iovec iov =3D { > - .iov_base =3D bp->data, > - .iov_len =3D dlen > - }; > - struct iov_tail data =3D IOV_TAIL(&iov, 1, 0); > - csum_udp4(&bp->uh, *src, *dst, &data, dlen); > + struct iov_tail data =3D *payload; > + > + IOV_DROP_HEADER(&data, struct udphdr); This is dropped. > + csum_udp4(&uh, *src, *dst, &data, dlen); > } > + IOV_PUSH_HEADER(payload, uh); As is this. > =20 > return l4len; > } > @@ -299,18 +299,19 @@ size_t udp_update_hdr4(struct iphdr *ip4h, struct u= dp_payload_t *bp, > * udp_update_hdr6() - Update headers for one IPv6 datagram > * @ip6h: Pre-filled IPv6 header (except for payload_len and > * addresses) > - * @bp: Pointer to udp_payload_t to update > + * @payload: iov_tail including UDP header on entry, excluding it on ex= it Same for IPv6. > * @toside: Flowside for destination side > * @dlen: Length of UDP payload > * @no_udp_csum: Do not set UDP checksum > * > - * Return: size of IPv6 payload (UDP header + data) > + * Return: size of datagram (UDP header + data) > */ > -size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp, > +size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct iov_tail *payload, > const struct flowside *toside, size_t dlen, > bool no_udp_csum) > { > - uint16_t l4len =3D dlen + sizeof(bp->uh); > + struct udphdr uh; > + uint16_t l4len =3D dlen + sizeof(uh); > =20 > ip6h->payload_len =3D htons(l4len); > ip6h->daddr =3D toside->eaddr.a6; > @@ -319,24 +320,24 @@ size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct= udp_payload_t *bp, > ip6h->nexthdr =3D IPPROTO_UDP; > ip6h->hop_limit =3D 255; > =20 > - bp->uh.source =3D htons(toside->oport); > - bp->uh.dest =3D htons(toside->eport); > - bp->uh.len =3D ip6h->payload_len; > + uh.source =3D htons(toside->oport); > + uh.dest =3D htons(toside->eport); > + uh.len =3D htons(l4len); > + > if (no_udp_csum) { > /* 0 is an invalid checksum for UDP IPv6 and dropped by > * the kernel stack, even if the checksum is disabled by virtio > * flags. We need to put any non-zero value here. > */ > - bp->uh.check =3D 0xffff; > + uh.check =3D 0xffff; > } else { > - const struct iovec iov =3D { > - .iov_base =3D bp->data, > - .iov_len =3D dlen > - }; > - struct iov_tail data =3D IOV_TAIL(&iov, 1, 0); > - csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6, &data, > - dlen); > + struct iov_tail data =3D *payload; > + > + IOV_DROP_HEADER(&data, struct udphdr); This is dropped. > + csum_udp6(&uh, &toside->oaddr.a6, &toside->eaddr.a6, > + &data, dlen); > } > + IOV_PUSH_HEADER(payload, uh); And this. > =20 > return l4len; > } > @@ -372,15 +373,18 @@ static void udp_tap_prepare(const struct mmsghdr *m= mh, > bool no_udp_csum) > { > struct iovec (*tap_iov)[UDP_NUM_IOVS] =3D &udp_l2_iov[idx]; > + struct iov_tail payload =3D IOV_TAIL(&(*tap_iov)[UDP_IOV_PAYLOAD], 1, 0= ); You can still construct an iov_tail with just the UDP payload here, either from tap_iov, or from mmh[idx].msg_hdr.msg_iov[]. Likewise you can obtain a suitable uh pointer easily. > struct ethhdr *eh =3D (*tap_iov)[UDP_IOV_ETH].iov_base; > - struct udp_payload_t *bp =3D &udp_payload[idx]; > struct udp_meta_t *bm =3D &udp_meta[idx]; > size_t l4len, l2len; > =20 > + l4len =3D sizeof(struct udphdr) + mmh[idx].msg_len; > + (*tap_iov)[UDP_IOV_PAYLOAD].iov_len =3D l4len; > + > eth_update_mac(eh, NULL, tap_omac); > if (!inany_v4(&toside->eaddr) || !inany_v4(&toside->oaddr)) { > - l4len =3D udp_update_hdr6(&bm->ip6h, bp, toside, > - mmh[idx].msg_len, no_udp_csum); > + udp_update_hdr6(&bm->ip6h, &payload, toside, mmh[idx].msg_len, > + no_udp_csum); So this will still work. > l2len =3D MAX(l4len + sizeof(bm->ip6h) + ETH_HLEN, ETH_ZLEN); > tap_hdr_update(&bm->taph, l2len); > @@ -388,8 +392,8 @@ static void udp_tap_prepare(const struct mmsghdr *mmh, > eh->h_proto =3D htons_constant(ETH_P_IPV6); > (*tap_iov)[UDP_IOV_IP] =3D IOV_OF_LVALUE(bm->ip6h); > } else { > - l4len =3D udp_update_hdr4(&bm->ip4h, bp, toside, > - mmh[idx].msg_len, no_udp_csum); > + udp_update_hdr4(&bm->ip4h, &payload, toside, mmh[idx].msg_len, > + no_udp_csum); As will this. > =20 > l2len =3D MAX(l4len + sizeof(bm->ip4h) + ETH_HLEN, ETH_ZLEN); > tap_hdr_update(&bm->taph, l2len); > @@ -397,7 +401,6 @@ static void udp_tap_prepare(const struct mmsghdr *mmh, > eh->h_proto =3D htons_constant(ETH_P_IP); > (*tap_iov)[UDP_IOV_IP] =3D IOV_OF_LVALUE(bm->ip4h); > } > - (*tap_iov)[UDP_IOV_PAYLOAD].iov_len =3D l4len; > =20 > udp_tap_pad(*tap_iov); > } > diff --git a/udp_internal.h b/udp_internal.h > index 64e457748324..e6cbaab79519 100644 > --- a/udp_internal.h > +++ b/udp_internal.h > @@ -25,10 +25,10 @@ struct udp_payload_t { > } __attribute__ ((packed, aligned(__alignof__(unsigned int)))); > #endif > =20 > -size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp, > +size_t udp_update_hdr4(struct iphdr *ip4h, struct iov_tail *payload, > const struct flowside *toside, size_t dlen, > bool no_udp_csum); > -size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp, > +size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct iov_tail *payload, > const struct flowside *toside, size_t dlen, > bool no_udp_csum); > void udp_sock_fwd(const struct ctx *c, int s, int rule_hint, > diff --git a/udp_vu.c b/udp_vu.c > index 74bf79d57969..36543f75638d 100644 > --- a/udp_vu.c > +++ b/udp_vu.c > @@ -98,69 +98,73 @@ static ssize_t udp_vu_sock_recv(struct iovec *iov, si= ze_t *cnt, int s, bool v6) > /** > * udp_vu_prepare() - Prepare the packet header > * @c: Execution context > - * @iov: IO vector for the frame (including vnet header) > + * @data: IO vector tail for the L2 frame, on return points to the L4 he= ader > * @toside: Address information for one side of the flow > * @dlen: Packet data length > */ > -static void udp_vu_prepare(const struct ctx *c, const struct iovec *iov, > - const struct flowside *toside, ssize_t dlen) > +static void udp_vu_prepare(const struct ctx *c, struct iov_tail *data, > + const struct flowside *toside, size_t dlen) > { > - struct ethhdr *eh; > + bool ipv4 =3D inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr); > + struct ethhdr eh; Create a local buffer for the UDP header. In addition to @data we'll want another iov_tail as a parameter. I'll, call it @payload since it will be const and contain only the UDP payload. You already built a suitable iov array for the recvmsg(), so @payload can be constructed from that. > =20 > /* ethernet header */ > - eh =3D vu_eth(iov[0].iov_base); > + memcpy(eh.h_dest, c->guest_mac, sizeof(eh.h_dest)); > + memcpy(eh.h_source, c->our_tap_mac, sizeof(eh.h_source)); > =20 > - memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest)); > - memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source)); > + if (ipv4) > + eh.h_proto =3D htons(ETH_P_IP); > + else > + eh.h_proto =3D htons(ETH_P_IPV6); > + IOV_PUSH_HEADER(data, eh); > =20 > /* initialize header */ > - if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) { > - struct iphdr *iph =3D vu_ip(iov[0].iov_base); > - struct udp_payload_t *bp =3D vu_payloadv4(iov[0].iov_base); > - > - eh->h_proto =3D htons(ETH_P_IP); > + if (ipv4) { > + struct iov_tail datagram; > + struct iphdr iph =3D (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP); > =20 > - *iph =3D (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP); > + datagram =3D *data; > + IOV_DROP_HEADER(&datagram, struct iphdr); > + udp_update_hdr4(&iph, &datagram, toside, dlen, true); At the moment this is rather odd, because we skip ahead, push the UDP header inside udp_update_hdr4(), then rewind to push the IP header. If udp_update_hdr4() takes uh and @payload separately, it's more straightforward. It will update both iph and uh, currently local buffers. Then we can push iph followed by uh to @data. > =20 > - udp_update_hdr4(iph, bp, toside, dlen, true); > + IOV_PUSH_HEADER(data, iph); > } else { > - struct ipv6hdr *ip6h =3D vu_ip(iov[0].iov_base); > - struct udp_payload_t *bp =3D vu_payloadv6(iov[0].iov_base); > - > - eh->h_proto =3D htons(ETH_P_IPV6); > + struct iov_tail datagram; > + struct ipv6hdr ip6h =3D (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP); > =20 > - *ip6h =3D (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP); > + datagram =3D *data; > + IOV_DROP_HEADER(&datagram, struct ipv6hdr); > + udp_update_hdr6(&ip6h, &datagram, toside, dlen, true); Similar for IPv6. > =20 > - udp_update_hdr6(ip6h, bp, toside, dlen, true); > + IOV_PUSH_HEADER(data, ip6h); > } > } > =20 > /** > * udp_vu_csum() - Calculate and set checksum for a UDP packet > * @toside: Address information for one side of the flow > - * @iov: IO vector for the frame > - * @cnt: Number of IO vector entries > + * @data: IO vector tail including UDP header on entry, excluding it on = exit > * @dlen: Data length > */ > -static void udp_vu_csum(const struct flowside *toside, const struct iove= c *iov, > - size_t cnt, size_t dlen) > +static void udp_vu_csum(const struct flowside *toside, struct iov_tail *= data, > + size_t dlen) > { > const struct in_addr *src4 =3D inany_v4(&toside->oaddr); > const struct in_addr *dst4 =3D inany_v4(&toside->eaddr); > - char *base =3D iov[0].iov_base; > - struct udp_payload_t *bp; > - struct iov_tail data; > - > - if (src4 && dst4) { > - bp =3D vu_payloadv4(base); > - data =3D IOV_TAIL(iov, cnt, (char *)&bp->data - base); > - csum_udp4(&bp->uh, *src4, *dst4, &data, dlen); > - } else { > - bp =3D vu_payloadv6(base); > - data =3D IOV_TAIL(iov, cnt, (char *)&bp->data - base); > - csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6, &data, > - dlen); > - } > + struct iov_tail current =3D *data; > + struct udphdr *uh, uh_storage; > + bool ipv4 =3D src4 && dst4; > + > + uh =3D IOV_REMOVE_HEADER(¤t, uh_storage); > + if (!uh) > + return; > + > + if (ipv4) > + csum_udp4(uh, *src4, *dst4, ¤t, dlen); > + else > + csum_udp6(uh, &toside->oaddr.a6, &toside->eaddr.a6, ¤t, dlen); > + > + IOV_PUSH_HEADER(data, *uh); Here's the aliased memcpy. uh points within data, and this will attempt to copy it back into the same place. Better to have uh be a local buffer. > } > =20 > /** > @@ -227,9 +231,10 @@ void udp_vu_sock_to_tap(const struct ctx *c, int s, = int n, flow_sidx_t tosidx) > vu_queue_rewind(vq, elem_cnt - elem_used); > =20 > if (iov_cnt > 0) { > - udp_vu_prepare(c, iov_vu, toside, dlen); > + struct iov_tail data =3D IOV_TAIL(iov_vu, iov_cnt, VNET_HLEN); > + udp_vu_prepare(c, &data, toside, dlen); > if (*c->pcap) { > - udp_vu_csum(toside, iov_vu, iov_cnt, dlen); > + udp_vu_csum(toside, &data, dlen); > pcap_iov(iov_vu, iov_cnt, VNET_HLEN, > hdrlen + dlen - VNET_HLEN); > } > --=20 > 2.54.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 --ZtGXeE50pM3J0sA5 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmoL8D4ACgkQzQJF27ox 2GcG5Q//V56d5UJn3ZAfO3lan3AOYhwNiYEtKzPczWZqX+cnFDwPrhOU2gAi1bj+ FishRPAXsrZqwkMUm/9PhIa0SM0OdDBB1uLVfanWIWQ/7YnTQiAjuznesQzCXZLH SXszn4o0BiydUUF9ct54QtISSMI+3weQ70JqQsBD5EMBGg94DQzC3moelicG4WCm sBRBnoA9sCsvON3xy9mi7m996sjEGPz2dpVY7wPSCvgk1rFVNbA6WKEgebPsDF4c 8ouuZJcYHRxjMPG7DOA2yeM5cl/xPyoXfMRnBLQT3yV2D9B7soKDFZxyJTMosRk7 GxlNOCliLcYDM8KjXAsHomxn5pVduWUz4rP6TQpA5kgZmqQeSt73J0fq2X6QVwJ4 cZlM8paZPoVG+Ciai7PiVEscYZtXyrV1H7XC374k624ZtqcPOlPsFI9BzEzrm5af gIPiJxGkx15ktRnZmOQ5axUcb/InmJkHrX15zs3LMYC9thoBVjbzAsKBjDIwLQB+ 82RGayedw2SIg2OYPBEmgZxYmxjBH+70tFxpUeZKf+lnRN1Tv2fI+Q9ZMiMSgZ63 0SCD3dBZXAyqygtbCQHqhZlHkUdfFErifB3m2ySSx9/eXbeuOeFt80WEw5Y5VuIV Y6HuVd8dPY3eweDM8PYvG35kadX723QwZdcTqADVBa18QqVMU0k= =LP8H -----END PGP SIGNATURE----- --ZtGXeE50pM3J0sA5--