From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=VeA+Cz8g; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id 6DC1B5A0262 for ; Tue, 19 May 2026 10:03:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779177825; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=KKk1HNhu96lduka1JLwxw8yBj6lOZPmZ4Dz+mgafggg=; b=VeA+Cz8gqytCcgvjjVTdu95mbYnCYh9iveTRa1h97FuFrPGT7vublhE59tW3h8XzvVNMtE 5zGbx7KDvv6huu4CZ5IsgPkthcWN2f0wkSjaY0wTQsfJoE0l8kazHSgn8gLD9+btyvIMu9 thBX1m6aC01on9agQBCErjBI5xNL1Ak= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-647-SqXPmbE7NYWk5Cr61Y-_Qw-1; Tue, 19 May 2026 04:03:43 -0400 X-MC-Unique: SqXPmbE7NYWk5Cr61Y-_Qw-1 X-Mimecast-MFC-AGG-ID: SqXPmbE7NYWk5Cr61Y-_Qw_1779177823 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-43d7a5b9678so2143000f8f.2 for ; Tue, 19 May 2026 01:03:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779177822; x=1779782622; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=KKk1HNhu96lduka1JLwxw8yBj6lOZPmZ4Dz+mgafggg=; b=RQggQ65yDQcrb2w9fYis7iZftCOYrslSzK5+4ZmfyRx8yZNobGoGSnMAmly0IyL1eC A7hTUNoD+mdhZT3+Nsg1Dzk1YdVwEQby+9gNE4eiOsXloqaOdgOUxeZTCq6QK/AqK0sW 0m2pkx0MPR68v6/l+6Sddq4Sc5u5zemh24pgRKlUPm9GvQBKdQAFLMu4Nhv87PacGhYN +30SWysFQ50uahO7kQgFoPf2CEaCBIQ+XoF/pcQyKcZiWbQRnbA55qWRHI5+lXXaKTui Ait3RUOP52BR9paLiROIxYWhv6XZf+yGkZVrxwNasN1fI0+oAQQ25YYT3vdTxCA2A9b2 3bRg== X-Gm-Message-State: AOJu0Yy39vPQ/VuYFKzmVKe/pN3ANV9aZjwaWCGrrPHP15t8KcYfZNxB 8JiGLAnHIGu62G9e5W+a2BxV3p2O/tZNHauTFa3f1Ze2cZcg9PvdJct5UBaaB4luZQ165vEThLy 4cJEqIHKikfgYflJZocWFvtQoFK8QQ0yeUiUmfo7Qk+yXDntUoUxGpgA8mvtSaQ== X-Gm-Gg: Acq92OH7aXw/7YryBbXJEbPBC8S9BIsJiebKO+yQE96vessjfTB+pMLjzoyqBWlyUNA 5njLxYpChl1SG6WAP+JvoFOvJ138AoK4e5tW5v2A8/uJ6B1gz5VBRvS1xMyjyeAS9sYX7peKyxu AW2Nunbjx7n0NJKOZw/G4+YqWsM4VMPB4jOTQhlHT6ATGojlWwLNkImlvttNaFwDznMuTTpwMy2 QDZNK/185GL2L5Ll45khcW6O7wos+3nDvK5hbGWIefKaSKbQ9ev+vD4n+Lvp9KoIfna8QijaI3F afs72Z6XItDPeOwWgdvGEAphg1fQyD46Ku10uYXPXoT8ItvFcdMDRl8gDFFOY7Mf+A3WRBAGnRN jgv8CgXELdH36zePNmdiOK3UL5AM43gGw7ecei219s0SQ4EBuohm2xbA= X-Received: by 2002:a05:600c:4e47:b0:488:a977:8de with SMTP id 5b1f17b1804b1-48fe61ed3b3mr305570125e9.16.1779177822176; Tue, 19 May 2026 01:03:42 -0700 (PDT) X-Received: by 2002:a05:600c:4e47:b0:488:a977:8de with SMTP id 5b1f17b1804b1-48fe61ed3b3mr305569115e9.16.1779177821470; Tue, 19 May 2026 01:03:41 -0700 (PDT) Received: from [192.168.100.100] (82-64-211-94.subs.proxad.net. [82.64.211.94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48ff2cb4ae0sm140278355e9.0.2026.05.19.01.03.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 19 May 2026 01:03:40 -0700 (PDT) Message-ID: <933734ef-5d4c-40dd-9fee-bb8f182f0921@redhat.com> Date: Tue, 19 May 2026 10:03:39 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v9 3/3] udp: Pass iov_tail to udp_update_hdr4()/udp_update_hdr6() To: David Gibson References: <20260518122625.2290034-1-lvivier@redhat.com> <20260518122625.2290034-4-lvivier@redhat.com> From: Laurent Vivier Autocrypt: addr=lvivier@redhat.com; keydata= xsFNBFYFJhkBEAC2me7w2+RizYOKZM+vZCx69GTewOwqzHrrHSG07MUAxJ6AY29/+HYf6EY2 WoeuLWDmXE7A3oJoIsRecD6BXHTb0OYS20lS608anr3B0xn5g0BX7es9Mw+hV/pL+63EOCVm SUVTEQwbGQN62guOKnJJJfphbbv82glIC/Ei4Ky8BwZkUuXd7d5NFJKC9/GDrbWdj75cDNQx UZ9XXbXEKY9MHX83Uy7JFoiFDMOVHn55HnncflUncO0zDzY7CxFeQFwYRbsCXOUL9yBtqLer Ky8/yjBskIlNrp0uQSt9LMoMsdSjYLYhvk1StsNPg74+s4u0Q6z45+l8RAsgLw5OLtTa+ePM J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W98bXCBIuQgs2m+OflERv5c3Ojldp04/S4NEjXEYRWdiCxN7ca5iPml5gLtuvhJMSy36gl U6IW9kn30IWuSoBpTkgV7rLUEhh9Ms82VWW/h2TxL8enfx40PrfbDtWwqRID3WY8jLrjKfTd R3LW8BnUDNkG+c4FzvvGUs8AvuqxxyHbXAfDx9o/jXfPHVRmJVhSmd+hC3mcQ+4iX5bBPBPM oDqSoLt5w9GoQQ6gDVP2ZjTWqwSRMLzNr37rJjZ1pt0DCMMTbiYIUcrhX8eveCJtY7NGWNyx FCRkhxRuGcpwPmRVDwOl39MB3iTsRighiMnijkbLXiKoJ5CDVvX5yicNqYJPKh5MFXN1bvsB kmYiStMRbrD0HoY1kx5/VozBtc70OU0EB8Wrv9hZD+Ofp0T3KOr1RUHvCZoLURfFhSQ= In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: xEDORy4dQR6bdW5PX99_Qo_-ZgnIxSz9N-3JdLp2i0E_1779177823 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID-Hash: TC7JJPJDVX3NTYARP2TJ573JXNEHHJZS X-Message-ID-Hash: TC7JJPJDVX3NTYARP2TJ573JXNEHHJZS X-MailFrom: lvivier@redhat.com 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: On 5/19/26 07:08, David Gibson wrote: > 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. >> >> 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. >> >> Signed-off-by: Laurent Vivier > > Alas, this still has a potentially aliased memcpy(), see below. Did you see I updated patch 2 to avoid the memcpy() if the header is already in place? (I think I should have removed your R-b...) Thanks, Laurent > >> --- >> iov.c | 1 - >> udp.c | 73 ++++++++++++++++++++++--------------------- >> udp_internal.h | 4 +-- >> udp_vu.c | 85 ++++++++++++++++++++++++++------------------------ >> 4 files changed, 85 insertions(+), 78 deletions(-) >> >> 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 exit > > 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 = inany_v4(&toside->oaddr); >> const struct in_addr *dst = inany_v4(&toside->eaddr); >> - size_t l4len = dlen + sizeof(bp->uh); >> + struct udphdr uh; >> + size_t l4len = dlen + sizeof(uh); >> size_t l3len = l4len + sizeof(*ip4h); >> >> assert(src && dst); >> @@ -278,19 +279,18 @@ 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); >> + uh.source = htons(toside->oport); >> + uh.dest = htons(toside->eport); >> + uh.len = htons(l4len); >> if (no_udp_csum) { >> - bp->uh.check = 0; >> + 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, dlen); >> + struct iov_tail data = *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. > >> >> return l4len; >> } >> @@ -299,18 +299,19 @@ 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 >> + * @payload: iov_tail including UDP header on entry, excluding it on exit > > 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 = dlen + sizeof(bp->uh); >> + struct udphdr uh; >> + uint16_t l4len = dlen + sizeof(uh); >> >> ip6h->payload_len = htons(l4len); >> ip6h->daddr = toside->eaddr.a6; >> @@ -319,24 +320,24 @@ 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 = 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 = 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, >> - dlen); >> + struct iov_tail data = *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. > >> >> return l4len; >> } >> @@ -372,15 +373,18 @@ static void udp_tap_prepare(const struct mmsghdr *mmh, >> bool no_udp_csum) >> { >> struct iovec (*tap_iov)[UDP_NUM_IOVS] = &udp_l2_iov[idx]; >> + struct iov_tail payload = 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 = (*tap_iov)[UDP_IOV_ETH].iov_base; >> - struct udp_payload_t *bp = &udp_payload[idx]; >> struct udp_meta_t *bm = &udp_meta[idx]; >> size_t l4len, l2len; >> >> + l4len = sizeof(struct udphdr) + mmh[idx].msg_len; >> + (*tap_iov)[UDP_IOV_PAYLOAD].iov_len = l4len; >> + >> 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); >> + udp_update_hdr6(&bm->ip6h, &payload, toside, mmh[idx].msg_len, >> + no_udp_csum); > > So this will still work. > > >> l2len = 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 = 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); >> + udp_update_hdr4(&bm->ip4h, &payload, toside, mmh[idx].msg_len, >> + no_udp_csum); > > As will this. > >> >> l2len = 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 = htons_constant(ETH_P_IP); >> (*tap_iov)[UDP_IOV_IP] = IOV_OF_LVALUE(bm->ip4h); >> } >> - (*tap_iov)[UDP_IOV_PAYLOAD].iov_len = l4len; >> >> 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 >> >> -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, size_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 header >> * @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 = 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. > >> >> /* ethernet header */ >> - eh = 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)); >> >> - 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 = htons(ETH_P_IP); >> + else >> + eh.h_proto = htons(ETH_P_IPV6); >> + IOV_PUSH_HEADER(data, eh); >> >> /* initialize header */ >> - 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); >> - >> - eh->h_proto = htons(ETH_P_IP); >> + if (ipv4) { >> + struct iov_tail datagram; >> + struct iphdr iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP); >> >> - *iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP); >> + datagram = *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. > >> >> - udp_update_hdr4(iph, bp, toside, dlen, true); >> + IOV_PUSH_HEADER(data, iph); >> } else { >> - struct ipv6hdr *ip6h = vu_ip(iov[0].iov_base); >> - struct udp_payload_t *bp = vu_payloadv6(iov[0].iov_base); >> - >> - eh->h_proto = htons(ETH_P_IPV6); >> + struct iov_tail datagram; >> + struct ipv6hdr ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP); >> >> - *ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP); >> + datagram = *data; >> + IOV_DROP_HEADER(&datagram, struct ipv6hdr); >> + udp_update_hdr6(&ip6h, &datagram, toside, dlen, true); > > Similar for IPv6. > >> >> - udp_update_hdr6(ip6h, bp, toside, dlen, true); >> + IOV_PUSH_HEADER(data, ip6h); >> } >> } >> >> /** >> * 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 iovec *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 = inany_v4(&toside->oaddr); >> const struct in_addr *dst4 = inany_v4(&toside->eaddr); >> - char *base = iov[0].iov_base; >> - struct udp_payload_t *bp; >> - struct iov_tail data; >> - >> - if (src4 && dst4) { >> - bp = vu_payloadv4(base); >> - data = IOV_TAIL(iov, cnt, (char *)&bp->data - base); >> - csum_udp4(&bp->uh, *src4, *dst4, &data, dlen); >> - } else { >> - bp = vu_payloadv6(base); >> - data = IOV_TAIL(iov, cnt, (char *)&bp->data - base); >> - csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6, &data, >> - dlen); >> - } >> + struct iov_tail current = *data; >> + struct udphdr *uh, uh_storage; >> + bool ipv4 = src4 && dst4; >> + >> + uh = 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. > >> } >> >> /** >> @@ -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); >> >> if (iov_cnt > 0) { >> - udp_vu_prepare(c, iov_vu, toside, dlen); >> + struct iov_tail data = 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); >> } >> -- >> 2.54.0 >> >