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=202410 header.b=XG7OLz3z; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 6E0A65A061F for ; Mon, 28 Oct 2024 10:41:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202410; t=1730108451; bh=VkLdV1GbmCvqe3IvgJ0t3ku7JgVoJAd8gi1pFJfXTEM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XG7OLz3zZ7OFG+ud/HtM68fjwzm40pn1TpOFocHSrWDoD5NUvMpETg2Ui5O+21VwT CQLjHOELS/K66xSqg4nWuMVSFE6nkkOltYPRFAhyAI/mw/i0EknTNjUVl94DnDUDkR kq9FLX+9KxL5+fkR/NKUJIHj2P5BKalX9QwhjZDhrXFUeMAjYv8lLuAfBDKIQCrgY3 j6TdDeEiaE8vz6U85n8r9mXzD1P4qd0v0QTFp7i1YjyZkslXctjjAK3QoBGl7BtSUD FUOhqCnjNwkuo4pGFPrM1aRpG6OH1H87PW7OU14TmNj+16biGovMybPMr7vUUdCzcM WgCXdGfYPYFng== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4XcT0M2cPSz4x8T; Mon, 28 Oct 2024 20:40:51 +1100 (AEDT) From: David Gibson To: Stefano Brivio , Laurent Vivier , passt-dev@passt.top Subject: [PATCH 6/7] tcp: Merge tcp_update_check_tcp[46]() Date: Mon, 28 Oct 2024 20:40:49 +1100 Message-ID: <20241028094050.1609090-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241028094050.1609090-1-david@gibson.dropbear.id.au> References: <20241028094050.1609090-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: SHUB7KI2K3DKBVWB6DBHQSQTDDUNMS2T X-Message-ID-Hash: SHUB7KI2K3DKBVWB6DBHQSQTDDUNMS2T 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: David Gibson 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: The only reason we need separate functions for the IPv4 and IPv6 case is to calculate the checksum of the IP pseudo-header, which is different for the two cases. However, the caller already knows which path it's on and can access the values needed for the pseudo-header partial sum more easily than tcp_update_check_tcp[46]() can. So, merge these functions into a single tcp_update_csum() function that just takes the pseudo-header partial sum, calculated in the caller. Signed-off-by: David Gibson --- tcp.c | 65 ++++++++++++++++++++--------------------------------------- 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/tcp.c b/tcp.c index 787bc19..e9f62a4 100644 --- a/tcp.c +++ b/tcp.c @@ -751,50 +751,20 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s) } /** - * tcp_update_check_tcp4() - Calculate TCP checksum for IPv4 - * @iph: IPv4 header + * tcp_update_csum() - Calculate TCP checksum + * @psum: Unfolded partial checksum of the IPv4 or IPv6 pseudo-header * @th: TCP header (updated) * @iov: IO vector containing the TCP payload * @iov_cnt: Length of @iov * @doffset: TCP payload offset in @iov */ -static void tcp_update_check_tcp4(const struct iphdr *iph, struct tcphdr *th, - const struct iovec *iov, int iov_cnt, - size_t doffset) +static void tcp_update_csum(uint32_t psum, struct tcphdr *th, + const struct iovec *iov, int iov_cnt, + size_t doffset) { - uint16_t l4len = ntohs(iph->tot_len) - sizeof(struct iphdr); - struct in_addr saddr = { .s_addr = iph->saddr }; - struct in_addr daddr = { .s_addr = iph->daddr }; - uint32_t sum; - - sum = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr); - - th->check = 0; - sum = csum_unfolded(th, sizeof(*th), sum); - th->check = csum_iov(iov, iov_cnt, doffset, sum); -} - -/** - * tcp_update_check_tcp6() - Calculate TCP checksum for IPv6 - * @ip6h: IPv6 header - * @th: TCP header (updated) - * @iov: IO vector containing the TCP payload - * @iov_cnt: Length of @iov - * @doffset: TCP payload offset in @iov - */ -static void tcp_update_check_tcp6(const struct ipv6hdr *ip6h, struct tcphdr *th, - const struct iovec *iov, int iov_cnt, - size_t doffset) -{ - uint16_t l4len = ntohs(ip6h->payload_len); - uint32_t sum; - - sum = proto_ipv6_header_psum(l4len, IPPROTO_TCP, &ip6h->saddr, - &ip6h->daddr); - th->check = 0; - sum = csum_unfolded(th, sizeof(*th), sum); - th->check = csum_iov(iov, iov_cnt, doffset, sum); + psum = csum_unfolded(th, sizeof(*th), psum); + th->check = csum_iov(iov, iov_cnt, doffset, psum); } /** @@ -946,10 +916,14 @@ void tcp_fill_headers4(const struct tcp_tap_conn *conn, tcp_fill_header(th, conn, seq); - if (no_tcp_csum) + if (no_tcp_csum) { th->check = 0; - else - tcp_update_check_tcp4(iph, th, iov, iov_cnt, doffset); + } else { + uint32_t psum = proto_ipv4_header_psum(l4len, IPPROTO_TCP, + *src4, *dst4); + + tcp_update_csum(psum, th, iov, iov_cnt, doffset); + } tap_hdr_update(taph, l3len + sizeof(struct ethhdr)); } @@ -991,10 +965,15 @@ void tcp_fill_headers6(const struct tcp_tap_conn *conn, tcp_fill_header(th, conn, seq); - if (no_tcp_csum) + if (no_tcp_csum) { th->check = 0; - else - tcp_update_check_tcp6(ip6h, th, iov, iov_cnt, doffset); + } else { + uint32_t psum = proto_ipv6_header_psum(l4len, IPPROTO_TCP, + &ip6h->saddr, + &ip6h->daddr); + + tcp_update_csum(psum, th, iov, iov_cnt, doffset); + } tap_hdr_update(taph, l4len + sizeof(*ip6h) + sizeof(struct ethhdr)); } -- 2.47.0