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=QU90Q8Wh; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 07B255A0262 for ; Mon, 13 Apr 2026 03:55:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1776045331; bh=SM3ccpNtBWMMST/nGsY6aZCYx1XetDKoRMyHNW+cPcA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=QU90Q8WhG1T54VPOipkJzEghwx0Tkb5OaPuGyJ7AsGRtFgPk6hxFxe7wvz4+xGrHo Ui8BYFRsfs1vnDULPag12tnYXutfjOXflk5cnjRF6KVxKAQd5Nbu13h/thRPaRag8k IUICpsC/mt/52WjUe5jwAMmbxZrO/KjrcRr1F7XF/8Ubj3OYwV+CgkTx/de49M5eNr xp7IQbVjd8T6BZIDIUTVpuqH/1HdI9dMyVnPFx1cDPxK/h8TyS39xVmNBys2CvdQ4R devmDz6Pm2WVvLUCpga/RI1rmf7r5NwQOCemkilMzGYX5BThbEH+OutSA4FA/MI5TZ FyAe1hGRuN2GQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fv9Tv1xsdz4wHr; Mon, 13 Apr 2026 11:55:31 +1000 (AEST) Date: Mon, 13 Apr 2026 11:55:26 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v7 3/3] udp: Pass iov_tail to udp_update_hdr4()/udp_update_hdr6() Message-ID: References: <20260403164241.3212528-1-lvivier@redhat.com> <20260403164241.3212528-4-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="AK1hxGuBPFsidLp1" Content-Disposition: inline In-Reply-To: <20260403164241.3212528-4-lvivier@redhat.com> Message-ID-Hash: X53K6DVEEZCM77YVNGTO5NY7R3FAMDYH X-Message-ID-Hash: X53K6DVEEZCM77YVNGTO5NY7R3FAMDYH 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: --AK1hxGuBPFsidLp1 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Apr 03, 2026 at 06:42:41PM +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 > --- > iov.c | 1 - > udp.c | 65 +++++++++++++++++++++------------------ > udp_internal.h | 4 +-- > udp_vu.c | 82 +++++++++++++++++++++++++++----------------------- > 4 files changed, 82 insertions(+), 70 deletions(-) >=20 > diff --git a/iov.c b/iov.c > index b1bcdc4649df..c0d9c6d21322 100644 > --- a/iov.c > +++ b/iov.c > @@ -368,7 +368,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 e113b26bc726..46eb199c74c9 100644 > --- a/udp.c > +++ b/udp.c > @@ -255,21 +255,22 @@ 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 with UDP payload to update To me "UDP payload" implies it doesn't include the UDP header, but I think from the code this *will* include the UDP header. > * @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) > */ > -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); > + size_t l4len =3D dlen + sizeof(struct udphdr); dlen + sizeof(uh) would break the reverse christmas tree a bit, but I think I'd still prefer it on balance. > size_t l3len =3D l4len + sizeof(*ip4h); > + struct udphdr uh; > =20 > assert(src && dst); > =20 > @@ -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); This is done unconditionally, so it could be an initializer for uh. > 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, l4len); > + struct iov_tail data =3D *payload; Clunky, but I can't see a nicer way around this. > + > + IOV_DROP_HEADER(&data, struct udphdr); > + csum_udp4(&uh, *src, *dst, &data, l4len); > } > + IOV_PUSH_HEADER(payload, uh); > =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 with UDP payload to update > * @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) > */ > -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) Similar comments to the IPv4 version. > { > - uint16_t l4len =3D dlen + sizeof(bp->uh); > + uint16_t l4len =3D dlen + sizeof(struct udphdr); > + struct udphdr uh; > =20 > ip6h->payload_len =3D htons(l4len); > ip6h->daddr =3D toside->eaddr.a6; > @@ -319,24 +320,23 @@ 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, > - l4len); > + struct iov_tail data =3D *payload; > + > + IOV_DROP_HEADER(&data, struct udphdr); > + csum_udp6(&uh, &toside->oaddr.a6, &toside->eaddr.a6, > + &data, l4len); > } > + IOV_PUSH_HEADER(payload, uh); > =20 > return l4len; > } > @@ -375,11 +375,18 @@ static void udp_tap_prepare(const struct mmsghdr *m= mh, > 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]; > + struct iovec iov[2]; > + struct iov_tail payload =3D IOV_TAIL(iov, ARRAY_SIZE(iov), 0); > size_t l4len, l2len; > =20 > + iov[0].iov_base =3D &bp->uh; > + iov[0].iov_len =3D sizeof(bp->uh); > + iov[1].iov_base =3D bp->data; > + iov[1].iov_len =3D mmh[idx].msg_len; > + > 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, > + l4len =3D udp_update_hdr6(&bm->ip6h, &payload, toside, > mmh[idx].msg_len, no_udp_csum); > =20 > l2len =3D MAX(l4len + sizeof(bm->ip6h) + ETH_HLEN, ETH_ZLEN); > @@ -388,7 +395,7 @@ 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, > + l4len =3D udp_update_hdr4(&bm->ip4h, &payload, toside, > mmh[idx].msg_len, no_udp_csum); > =20 > l2len =3D MAX(l4len + sizeof(bm->ip4h) + ETH_HLEN, ETH_ZLEN); > 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 16086e6c0c03..989751deafe0 100644 > --- a/udp_vu.c > +++ b/udp_vu.c > @@ -96,43 +96,52 @@ 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 frame, on return points to the L4 header This doesn't make it clear which headers @data includes on entry. > * @toside: Address information for one side of the flow > * @dlen: Packet data length > * > * Return: Layer-4 length > */ > -static size_t udp_vu_prepare(const struct ctx *c, const struct iovec *io= v, > - const struct flowside *toside, ssize_t dlen) > +static size_t 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; > size_t l4len; > =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); =46rom this, I'm assuming it does include the ethernet header, but not the vnet header. > =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); > + if (ipv4) { > + struct iov_tail udp_frame; > + struct iphdr iph; > =20 > - eh->h_proto =3D htons(ETH_P_IP); > + iph =3D (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP); This could be an initializer, since iph is a local now. > - *iph =3D (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP); > + udp_frame =3D *data; It's not strict, but I generally see the convention as being "frame" for the L2 object, "packet" for the L3 object and "datagram" for the L4 object. So "udp_frame" reads a bit oddly here, I'd call it "datagram". > + IOV_DROP_HEADER(&udp_frame, struct iphdr); > + l4len =3D udp_update_hdr4(&iph, &udp_frame, toside, dlen, true); > =20 > - l4len =3D 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); > + struct iov_tail udp_frame; > + struct ipv6hdr ip6h; > =20 > - eh->h_proto =3D htons(ETH_P_IPV6); > + ip6h =3D (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP); > =20 > - *ip6h =3D (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP); > + udp_frame =3D *data; > + IOV_DROP_HEADER(&udp_frame, struct ipv6hdr); > + l4len =3D udp_update_hdr6(&ip6h, &udp_frame, toside, dlen, true); > =20 > - l4len =3D udp_update_hdr6(ip6h, bp, toside, dlen, true); > + IOV_PUSH_HEADER(data, ip6h); > } > =20 > return l4len; > @@ -141,29 +150,25 @@ static size_t udp_vu_prepare(const struct ctx *c, c= onst struct iovec *iov, > /** > * 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 for the L4 frame (point to the UDP header) > * @l4len: L4 length > */ > -static void udp_vu_csum(const struct flowside *toside, const struct iove= c *iov, > - size_t cnt, size_t l4len) > +static void udp_vu_csum(const struct flowside *toside, struct iov_tail *= data, > + size_t l4len) > { > 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, l4len); > - } 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, > - l4len); > - } > + struct udphdr *uh, uh_storage; > + bool ipv4 =3D src4 && dst4; > + > + uh =3D IOV_REMOVE_HEADER(data, uh_storage); > + if (!uh) > + return; > + > + if (ipv4) > + csum_udp4(uh, *src4, *dst4, data, l4len); This is still implicitly requiring a contiguous UDP header, since you're writing to the pointer from IOV_REMOVE_HEADER. Which makes me think: maybe we should alter iov_peek_header() to return a const pointer to catch cases like that. > + else > + csum_udp6(uh, &toside->oaddr.a6, &toside->eaddr.a6, data, l4len); > } > =20 > /** > @@ -230,9 +235,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) { > - size_t l4len =3D udp_vu_prepare(c, iov_vu, toside, dlen); > + struct iov_tail data =3D IOV_TAIL(iov_vu, iov_cnt, VNET_HLEN); > + size_t l4len =3D udp_vu_prepare(c, &data, toside, dlen); > if (*c->pcap) { > - udp_vu_csum(toside, iov_vu, iov_cnt, l4len); > + udp_vu_csum(toside, &data, l4len); > pcap_iov(iov_vu, iov_cnt, VNET_HLEN, > hdrlen + dlen - VNET_HLEN); > } > --=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 --AK1hxGuBPFsidLp1 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmncTQ0ACgkQzQJF27ox 2GfuCxAAitGMOFcoAb6lIfRgVYPVOyaqRIcZXZf9wW/5TCayFS+dUKNoUStozSRe 4lgJwORInQPMe121K0JQObIs6MGFkN7kNaa57msJESPfCMxasnDahC1KasmF1v36 d1nVdEJ0kP7rsU1LJdb2zWSA4JG46fnRgFTo61ghM96Eyj4xojORsvi6c6ad8vmu umGAss/AhpxTLyK1gC7d3hfmnIhWlYXziuBw4UyZkznkzvzkJtztJ7XAs0ZzW5wV n5wnUeeO3JnCGLOj2n0cKiLBaD0IbKcvUvIXnbHIm7QiOTvBsbev7DhPK4r1Ai+/ 0Uc9yXRqiMaRa8iPps4BA6bbFOzv8KulfNmWhe3VdcQnvBJqdH7unkHUjYZ9HIRD uru8snDgJc3upsomr6l7mmcu4kiqzkYOHNwVtQEet4LT1B8NvxYzNgBiMXFRL6ey 2SVYIqldiWOyNB9QJNNqO5dnzVTRivLM9xULN5NMqDTSPpYpx7tN76lV4mObpi3s 4o8oScDYGs6HfiQNQ4SLtjKROg8N9Pfw7+Lz3M3fDHTc7Ncorxu5QBjAJCQf9wi4 crRBoj+BGeKbjFHB5V28hJfW1RLu/yQgnDaD17XgB0zBCHJI22Tf73fjLivXBLZF rMtNlGXfK5w1OCw64SZkjBRByEw3sOhR0RgQs/Fk1MXdfO1mbR4= =M7rL -----END PGP SIGNATURE----- --AK1hxGuBPFsidLp1--