From: David Gibson <david@gibson.dropbear.id.au>
To: Laurent Vivier <lvivier@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v2 07/13] udp: Pass iov_tail to udp_update_hdr4()/udp_update_hdr6()
Date: Thu, 12 Mar 2026 15:16:39 +1100 [thread overview]
Message-ID: <abI-J3SmLCc6ESWX@zatzit> (raw)
In-Reply-To: <20260309094744.1907754-8-lvivier@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 9098 bytes --]
On Mon, Mar 09, 2026 at 10:47:38AM +0100, Laurent Vivier wrote:
> Change udp_update_hdr4() and udp_update_hdr6() to take a separate
> struct udphdr pointer and an iov_tail for the payload, instead of a
> struct udp_payload_t pointer and an explicit data length.
>
> This decouples the header update functions from the udp_payload_t memory
> layout, which assumes all headers and data sit in a single contiguous
> buffer. The vhost-user path uses virtqueue-provided scatter-gather
> buffers where this assumption does not hold; passing an iov_tail lets
> both the tap path and the vhost-user path share the same functions
> without casting through layout-specific helpers.
>
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
> udp.c | 72 +++++++++++++++++++++++---------------------------
> udp_internal.h | 10 ++++---
> udp_vu.c | 14 ++++++++--
> 3 files changed, 51 insertions(+), 45 deletions(-)
>
> diff --git a/udp.c b/udp.c
> index 464aa09365cc..6840e8843e68 100644
> --- a/udp.c
> +++ b/udp.c
> @@ -255,20 +255,20 @@ 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
> + * @uh: UDP header to update
> + * @payload: UDP payload
> * @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,
> - const struct flowside *toside, size_t dlen,
> - bool no_udp_csum)
> +size_t udp_update_hdr4(struct iphdr *ip4h, struct udphdr *uh,
> + struct iov_tail *payload,
> + const struct flowside *toside, bool no_udp_csum)
> {
> const struct in_addr *src = inany_v4(&toside->oaddr);
> const struct in_addr *dst = inany_v4(&toside->eaddr);
> - size_t l4len = dlen + sizeof(bp->uh);
> + size_t l4len = iov_tail_size(payload) + sizeof(*uh);
> size_t l3len = l4len + sizeof(*ip4h);
>
> ASSERT(src && dst);
> @@ -278,19 +278,13 @@ size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
> ip4h->saddr = src->s_addr;
> ip4h->check = csum_ip4_header(l3len, IPPROTO_UDP, *src, *dst);
>
> - bp->uh.source = htons(toside->oport);
> - bp->uh.dest = htons(toside->eport);
> - bp->uh.len = htons(l4len);
> - if (no_udp_csum) {
> - bp->uh.check = 0;
> - } else {
> - const struct iovec iov = {
> - .iov_base = bp->data,
> - .iov_len = dlen
> - };
> - struct iov_tail data = IOV_TAIL(&iov, 1, 0);
> - csum_udp4(&bp->uh, *src, *dst, &data);
> - }
> + uh->source = htons(toside->oport);
> + uh->dest = htons(toside->eport);
> + uh->len = htons(l4len);
> + if (no_udp_csum)
> + uh->check = 0;
> + else
> + csum_udp4(uh, *src, *dst, payload);
>
> return l4len;
> }
> @@ -299,18 +293,18 @@ size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_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
> + * @uh: UDP header to update
> + * @payload: UDP payload
> * @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,
> - const struct flowside *toside, size_t dlen,
> - bool no_udp_csum)
> +size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udphdr *uh,
> + struct iov_tail *payload,
> + const struct flowside *toside, bool no_udp_csum)
> {
> - uint16_t l4len = dlen + sizeof(bp->uh);
> + uint16_t l4len = iov_tail_size(payload) + sizeof(*uh);
>
> ip6h->payload_len = htons(l4len);
> ip6h->daddr = toside->eaddr.a6;
> @@ -319,22 +313,17 @@ size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp,
> ip6h->nexthdr = IPPROTO_UDP;
> ip6h->hop_limit = 255;
>
> - bp->uh.source = htons(toside->oport);
> - bp->uh.dest = htons(toside->eport);
> - bp->uh.len = ip6h->payload_len;
> + uh->source = htons(toside->oport);
> + uh->dest = htons(toside->eport);
> + uh->len = ip6h->payload_len;
> 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 = 0xffff;
> + uh->check = 0xffff;
> } else {
> - const struct iovec iov = {
> - .iov_base = bp->data,
> - .iov_len = dlen
> - };
> - struct iov_tail data = IOV_TAIL(&iov, 1, 0);
> - csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6, &data);
> + csum_udp6(uh, &toside->oaddr.a6, &toside->eaddr.a6, payload);
> }
>
> return l4len;
> @@ -374,12 +363,17 @@ static void udp_tap_prepare(const struct mmsghdr *mmh,
> struct ethhdr *eh = (*tap_iov)[UDP_IOV_ETH].iov_base;
> struct udp_payload_t *bp = &udp_payload[idx];
> struct udp_meta_t *bm = &udp_meta[idx];
> + const struct iovec iov = {
> + .iov_base = bp->data,
> + .iov_len = mmh[idx].msg_len,
> + };
> + struct iov_tail payload = IOV_TAIL(&iov, 1, 0);
> size_t l4len, l2len;
>
> eth_update_mac(eh, NULL, tap_omac);
> if (!inany_v4(&toside->eaddr) || !inany_v4(&toside->oaddr)) {
> - l4len = udp_update_hdr6(&bm->ip6h, bp, toside,
> - mmh[idx].msg_len, no_udp_csum);
> + l4len = udp_update_hdr6(&bm->ip6h, &bp->uh, &payload, toside,
> + no_udp_csum);
>
> l2len = MAX(l4len + sizeof(bm->ip6h) + ETH_HLEN, ETH_ZLEN);
> tap_hdr_update(&bm->taph, l2len);
> @@ -387,8 +381,8 @@ static void udp_tap_prepare(const struct mmsghdr *mmh,
> eh->h_proto = htons_constant(ETH_P_IPV6);
> (*tap_iov)[UDP_IOV_IP] = IOV_OF_LVALUE(bm->ip6h);
> } else {
> - l4len = udp_update_hdr4(&bm->ip4h, bp, toside,
> - mmh[idx].msg_len, no_udp_csum);
> + l4len = udp_update_hdr4(&bm->ip4h, &bp->uh, &payload, toside,
> + no_udp_csum);
>
> l2len = MAX(l4len + sizeof(bm->ip4h) + ETH_HLEN, ETH_ZLEN);
> tap_hdr_update(&bm->taph, l2len);
> diff --git a/udp_internal.h b/udp_internal.h
> index 64e457748324..fba5ef33cf99 100644
> --- a/udp_internal.h
> +++ b/udp_internal.h
> @@ -25,11 +25,13 @@ struct udp_payload_t {
> } __attribute__ ((packed, aligned(__alignof__(unsigned int))));
> #endif
>
> -size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
> - const struct flowside *toside, size_t dlen,
> +size_t udp_update_hdr4(struct iphdr *ip4h, struct udphdr *uh,
> + struct iov_tail *payload,
> + const struct flowside *toside,
> bool no_udp_csum);
> -size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp,
> - const struct flowside *toside, size_t dlen,
> +size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udphdr *uh,
> + struct iov_tail *payload,
> + const struct flowside *toside,
> bool no_udp_csum);
> void udp_sock_fwd(const struct ctx *c, int s, int rule_hint,
> uint8_t frompif, in_port_t port, const struct timespec *now);
> diff --git a/udp_vu.c b/udp_vu.c
> index 27ae93de4420..2a5d3f822bf6 100644
> --- a/udp_vu.c
> +++ b/udp_vu.c
> @@ -122,21 +122,31 @@ static size_t udp_vu_prepare(const struct ctx *c, const struct iov_tail *data,
> if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) {
> struct iphdr *iph = vu_ip(iov[0].iov_base);
> struct udp_payload_t *bp = vu_payloadv4(iov[0].iov_base);
> + const struct iovec payload_iov = {
> + .iov_base = bp->data,
> + .iov_len = dlen,
> + };
> + struct iov_tail payload = IOV_TAIL(&payload_iov, 1, 0);
>
> eh->h_proto = htons(ETH_P_IP);
>
> *iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP);
>
> - l4len = udp_update_hdr4(iph, bp, toside, dlen, true);
> + l4len = udp_update_hdr4(iph, &bp->uh, &payload, toside, true);
> } else {
> struct ipv6hdr *ip6h = vu_ip(iov[0].iov_base);
> struct udp_payload_t *bp = vu_payloadv6(iov[0].iov_base);
> + const struct iovec payload_iov = {
> + .iov_base = bp->data,
> + .iov_len = dlen,
> + };
> + struct iov_tail payload = IOV_TAIL(&payload_iov, 1, 0);
>
> eh->h_proto = htons(ETH_P_IPV6);
>
> *ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP);
>
> - l4len = udp_update_hdr6(ip6h, bp, toside, dlen, true);
> + l4len = udp_update_hdr6(ip6h, &bp->uh, &payload, toside, true);
> }
>
> return l4len;
> --
> 2.53.0
>
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2026-03-12 4:30 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-09 9:47 [PATCH v2 00/13] vhost-user,udp: Handle multiple iovec entries per virtqueue element Laurent Vivier
2026-03-09 9:47 ` [PATCH v2 01/13] iov: Add iov_truncate() helper and use it in vu handlers Laurent Vivier
2026-03-09 9:47 ` [PATCH v2 02/13] vhost-user: Centralise 802.3 frame padding in vu_collect() and vu_flush() Laurent Vivier
2026-03-12 2:05 ` David Gibson
2026-03-09 9:47 ` [PATCH v2 03/13] vhost-user: Use ARRAY_SIZE(elem) instead of VIRTQUEUE_MAX_SIZE Laurent Vivier
2026-03-09 9:47 ` [PATCH v2 04/13] udp_vu: Use iov_tail to manage virtqueue buffers Laurent Vivier
2026-03-12 2:38 ` David Gibson
2026-03-09 9:47 ` [PATCH v2 05/13] udp_vu: Move virtqueue management from udp_vu_sock_recv() to its caller Laurent Vivier
2026-03-12 3:44 ` David Gibson
2026-03-09 9:47 ` [PATCH v2 06/13] iov: Add IOV_PUT_HEADER() to write header data back to iov_tail Laurent Vivier
2026-03-12 4:12 ` David Gibson
2026-03-12 8:20 ` Laurent Vivier
2026-03-09 9:47 ` [PATCH v2 07/13] udp: Pass iov_tail to udp_update_hdr4()/udp_update_hdr6() Laurent Vivier
2026-03-12 4:16 ` David Gibson [this message]
2026-03-09 9:47 ` [PATCH v2 08/13] udp_vu: Use iov_tail in udp_vu_prepare() Laurent Vivier
2026-03-12 4:30 ` David Gibson
2026-03-12 8:19 ` Laurent Vivier
2026-03-12 9:51 ` David Gibson
2026-03-09 9:47 ` [PATCH v2 09/13] vu_common: Pass iov_tail to vu_set_vnethdr() Laurent Vivier
2026-03-12 4:34 ` David Gibson
2026-03-09 9:47 ` [PATCH v2 10/13] vu_common: Accept explicit iovec counts in vu_set_element() Laurent Vivier
2026-03-09 9:47 ` [PATCH v2 11/13] vu_common: Accept explicit iovec count per element in vu_init_elem() Laurent Vivier
2026-03-09 9:47 ` [PATCH v2 12/13] vu_common: Prepare to use multibuffer with guest RX Laurent Vivier
2026-03-09 9:47 ` [PATCH v2 13/13] vhost-user,udp: Use 2 iovec entries per element Laurent Vivier
2026-03-12 4:39 ` David Gibson
2026-03-12 8:08 ` Laurent Vivier
2026-03-12 9:47 ` David Gibson
2026-03-12 10:42 ` Laurent Vivier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=abI-J3SmLCc6ESWX@zatzit \
--to=david@gibson.dropbear.id.au \
--cc=lvivier@redhat.com \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).