public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 2/7] iov, checksum: Replace csum_iov() with csum_iov_tail()
Date: Wed, 27 Nov 2024 14:54:05 +1100	[thread overview]
Message-ID: <20241127035410.1167773-3-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20241127035410.1167773-1-david@gibson.dropbear.id.au>

We usually want to checksum only the tail part of a frame, excluding at
least some headers.  csum_iov() does that for a frame represented as an
IO vector, not actually summing the entire IO vector.  We now have struct
iov_tail to explicitly represent this construct, so replace csum_iov()
with csum_iov_tail() taking that representation rather than 3 parameters.

We propagate the same change to csum_udp4() and csum_udp6() which take
similar parameters.  This slightly simplifies the code, and will allow some
further simplifications as struct iov_tail is more widely used.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 checksum.c | 58 ++++++++++++++++++++++--------------------------------
 checksum.h |  8 ++++----
 iov.c      |  1 -
 tap.c      |  6 ++++--
 tcp.c      |  6 ++++--
 udp.c      |  7 ++++---
 udp_vu.c   |  9 +++++----
 7 files changed, 44 insertions(+), 51 deletions(-)

diff --git a/checksum.c b/checksum.c
index c673993..1c4354d 100644
--- a/checksum.c
+++ b/checksum.c
@@ -166,24 +166,22 @@ uint32_t proto_ipv4_header_psum(uint16_t l4len, uint8_t protocol,
  * @udp4hr:	UDP header, initialised apart from checksum
  * @saddr:	IPv4 source address
  * @daddr:	IPv4 destination address
- * @iov:	Pointer to the array of IO vectors
- * @iov_cnt:	Length of the array
- * @offset:	UDP payload offset in the iovec array
+ * @data:	UDP payload (as IO vector tail)
  */
 void csum_udp4(struct udphdr *udp4hr,
 	       struct in_addr saddr, struct in_addr daddr,
-	       const struct iovec *iov, int iov_cnt, size_t offset)
+	       struct iov_tail *data)
 {
 	/* UDP checksums are optional, so don't bother */
 	udp4hr->check = 0;
 
 	if (UDP4_REAL_CHECKSUMS) {
-		uint16_t l4len = iov_size(iov, iov_cnt) - offset +
-				 sizeof(struct udphdr);
+		uint16_t l4len = iov_tail_size(data) + sizeof(struct udphdr);
 		uint32_t psum = proto_ipv4_header_psum(l4len, IPPROTO_UDP,
 						       saddr, daddr);
+
 		psum = csum_unfolded(udp4hr, sizeof(struct udphdr), psum);
-		udp4hr->check = csum_iov(iov, iov_cnt, offset, psum);
+		udp4hr->check = csum_iov_tail(data, psum);
 	}
 }
 
@@ -231,22 +229,20 @@ uint32_t proto_ipv6_header_psum(uint16_t payload_len, uint8_t protocol,
  * @udp6hr:	UDP header, initialised apart from checksum
  * @saddr:	Source address
  * @daddr:	Destination address
- * @iov:	Pointer to the array of IO vectors
- * @iov_cnt:	Length of the array
- * @offset:	UDP payload offset in the iovec array
+ * @data:	UDP payload (as IO vector tail)
  */
 void csum_udp6(struct udphdr *udp6hr,
 	       const struct in6_addr *saddr, const struct in6_addr *daddr,
-	       const struct iovec *iov, int iov_cnt, size_t offset)
+	       struct iov_tail *data)
 {
-	uint16_t l4len = iov_size(iov, iov_cnt) - offset +
-			 sizeof(struct udphdr);
+	uint16_t l4len = iov_tail_size(data) + sizeof(struct udphdr);
 	uint32_t psum = proto_ipv6_header_psum(l4len, IPPROTO_UDP,
 					       saddr, daddr);
+
 	udp6hr->check = 0;
 
 	psum = csum_unfolded(udp6hr, sizeof(struct udphdr), psum);
-	udp6hr->check = csum_iov(iov, iov_cnt, offset, psum);
+	udp6hr->check = csum_iov_tail(data, psum);
 }
 
 /**
@@ -501,31 +497,23 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
 }
 
 /**
- * csum_iov() - Calculates the unfolded checksum over an array of IO vectors
- *
- * @iov		Pointer to the array of IO vectors
- * @n		Length of the array
- * @offset:	Offset of the data to checksum within the full data length
+ * csum_iov_tail() - Calculate unfolded checksum for the tail of an IO vector
+ * @tail:	IO vector tail to checksum
  * @init	Initial 32-bit checksum, 0 for no pre-computed checksum
  *
  * Return: 16-bit folded, complemented checksum
  */
-uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
-		  uint32_t init)
+uint16_t csum_iov_tail(struct iov_tail *tail, uint32_t init)
 {
-	unsigned int i;
-	size_t first;
-
-	i = iov_skip_bytes(iov, n, offset, &first);
-	if (i >= n)
-		return (uint16_t)~csum_fold(init);
-
-	init = csum_unfolded((char *)iov[i].iov_base + first,
-			     iov[i].iov_len - first, init);
-	i++;
-
-	for (; i < n; i++)
-		init = csum_unfolded(iov[i].iov_base, iov[i].iov_len, init);
-
+	if (iov_tail_prune(tail)) {
+		size_t i;
+
+		init = csum_unfolded((char *)tail->iov[0].iov_base + tail->off,
+				     tail->iov[0].iov_len - tail->off, init);
+		for (i = 1; i < tail->cnt; i++) {
+			const struct iovec *iov = &tail->iov[i];
+			init = csum_unfolded(iov->iov_base, iov->iov_len, init);
+		}
+	}
 	return (uint16_t)~csum_fold(init);
 }
diff --git a/checksum.h b/checksum.h
index 31ba322..e243c97 100644
--- a/checksum.h
+++ b/checksum.h
@@ -9,6 +9,7 @@
 struct udphdr;
 struct icmphdr;
 struct icmp6hdr;
+struct iov_tail;
 
 uint32_t sum_16b(const void *buf, size_t len);
 uint16_t csum_fold(uint32_t sum);
@@ -19,20 +20,19 @@ uint32_t proto_ipv4_header_psum(uint16_t l4len, uint8_t protocol,
 				struct in_addr saddr, struct in_addr daddr);
 void csum_udp4(struct udphdr *udp4hr,
 	       struct in_addr saddr, struct in_addr daddr,
-	       const struct iovec *iov, int iov_cnt, size_t offset);
+	       struct iov_tail *data);
 void csum_icmp4(struct icmphdr *icmp4hr, const void *payload, size_t dlen);
 uint32_t proto_ipv6_header_psum(uint16_t payload_len, uint8_t protocol,
 				const struct in6_addr *saddr,
 				const struct in6_addr *daddr);
 void csum_udp6(struct udphdr *udp6hr,
 	       const struct in6_addr *saddr, const struct in6_addr *daddr,
-	       const struct iovec *iov, int iov_cnt, size_t offset);
+	       struct iov_tail *data);
 void csum_icmp6(struct icmp6hdr *icmp6hr,
 		const struct in6_addr *saddr, const struct in6_addr *daddr,
 		const void *payload, size_t dlen);
 uint32_t csum_unfolded(const void *buf, size_t len, uint32_t init);
 uint16_t csum(const void *buf, size_t len, uint32_t init);
-uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
-		  uint32_t init);
+uint16_t csum_iov_tail(struct iov_tail *tail, uint32_t init);
 
 #endif /* CHECKSUM_H */
diff --git a/iov.c b/iov.c
index 4c6416c..2f7be15 100644
--- a/iov.c
+++ b/iov.c
@@ -185,7 +185,6 @@ bool iov_tail_prune(struct iov_tail *tail)
  *
  * Returns:    The total size in bytes.
  */
-/* cppcheck-suppress unusedFunction */
 size_t iov_tail_size(struct iov_tail *tail)
 {
 	iov_tail_prune(tail);
diff --git a/tap.c b/tap.c
index 386f0bc..3035610 100644
--- a/tap.c
+++ b/tap.c
@@ -184,11 +184,12 @@ void tap_udp4_send(const struct ctx *c, struct in_addr src, in_port_t sport,
 		.iov_base = (void *)in,
 		.iov_len = dlen
 	};
+	struct iov_tail payload = IOV_TAIL(&iov, 1, 0);
 
 	uh->source = htons(sport);
 	uh->dest = htons(dport);
 	uh->len = htons(l4len);
-	csum_udp4(uh, src, dst, &iov, 1, 0);
+	csum_udp4(uh, src, dst, &payload);
 	memcpy(data, in, dlen);
 
 	tap_send_single(c, buf, dlen + (data - buf));
@@ -271,11 +272,12 @@ void tap_udp6_send(const struct ctx *c,
 		.iov_base = in,
 		.iov_len = dlen
 	};
+	struct iov_tail payload = IOV_TAIL(&iov, 1, 0);
 
 	uh->source = htons(sport);
 	uh->dest = htons(dport);
 	uh->len = htons(l4len);
-	csum_udp6(uh, src, dst, &iov, 1, 0);
+	csum_udp6(uh, src, dst, &payload);
 	memcpy(data, in, dlen);
 
 	tap_send_single(c, buf, dlen + (data - buf));
diff --git a/tcp.c b/tcp.c
index 61c12a5..f334ca5 100644
--- a/tcp.c
+++ b/tcp.c
@@ -764,6 +764,7 @@ void tcp_update_check_tcp4(const struct iphdr *iph,
 			   size_t l4offset)
 {
 	uint16_t l4len = ntohs(iph->tot_len) - sizeof(struct iphdr);
+	struct iov_tail l4 = IOV_TAIL(iov, iov_cnt, l4offset);
 	struct in_addr saddr = { .s_addr = iph->saddr };
 	struct in_addr daddr = { .s_addr = iph->daddr };
 	size_t check_ofs;
@@ -801,7 +802,7 @@ void tcp_update_check_tcp4(const struct iphdr *iph,
 	check = (uint16_t *)ptr;
 
 	*check = 0;
-	*check = csum_iov(iov, iov_cnt, l4offset, sum);
+	*check = csum_iov_tail(&l4, sum);
 }
 
 /**
@@ -815,6 +816,7 @@ void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
 			   const struct iovec *iov, int iov_cnt,
 			   size_t l4offset)
 {
+	struct iov_tail l4 = IOV_TAIL(iov, iov_cnt, l4offset);
 	uint16_t l4len = ntohs(ip6h->payload_len);
 	size_t check_ofs;
 	uint16_t *check;
@@ -852,7 +854,7 @@ void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
 	check = (uint16_t *)ptr;
 
 	*check = 0;
-	*check = csum_iov(iov, iov_cnt, l4offset, sum);
+	*check = csum_iov_tail(&l4, sum);
 }
 
 /**
diff --git a/udp.c b/udp.c
index 5b0093a..c89f031 100644
--- a/udp.c
+++ b/udp.c
@@ -316,7 +316,8 @@ size_t udp_update_hdr4(struct iphdr *ip4h, struct udp_payload_t *bp,
 			.iov_base = bp->data,
 			.iov_len = dlen
 		};
-		csum_udp4(&bp->uh, *src, *dst, &iov, 1, 0);
+		struct iov_tail data = IOV_TAIL(&iov, 1, 0);
+		csum_udp4(&bp->uh, *src, *dst, &data);
 	}
 
 	return l4len;
@@ -360,8 +361,8 @@ size_t udp_update_hdr6(struct ipv6hdr *ip6h, struct udp_payload_t *bp,
 			.iov_base = bp->data,
 			.iov_len = dlen
 		};
-		csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6,
-			  &iov, 1, 0);
+		struct iov_tail data = IOV_TAIL(&iov, 1, 0);
+		csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6, &data);
 	}
 
 	return l4len;
diff --git a/udp_vu.c b/udp_vu.c
index c911022..9c697f3 100644
--- a/udp_vu.c
+++ b/udp_vu.c
@@ -199,15 +199,16 @@ static void udp_vu_csum(const struct flowside *toside, int iov_used)
 	const struct in_addr *dst4 = inany_v4(&toside->eaddr);
 	char *base = iov_vu[0].iov_base;
 	struct udp_payload_t *bp;
+	struct iov_tail data;
 
 	if (src4 && dst4) {
 		bp = vu_payloadv4(base);
-		csum_udp4(&bp->uh, *src4, *dst4, iov_vu, iov_used,
-			  (char *)&bp->data - base);
+		data = IOV_TAIL(iov_vu, iov_used, (char *)&bp->data - base);
+		csum_udp4(&bp->uh, *src4, *dst4, &data);
 	} else {
 		bp = vu_payloadv6(base);
-		csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6,
-			  iov_vu, iov_used, (char *)&bp->data - base);
+		data = IOV_TAIL(iov_vu, iov_used, (char *)&bp->data - base);
+		csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6, &data);
 	}
 }
 
-- 
@@ -199,15 +199,16 @@ static void udp_vu_csum(const struct flowside *toside, int iov_used)
 	const struct in_addr *dst4 = inany_v4(&toside->eaddr);
 	char *base = iov_vu[0].iov_base;
 	struct udp_payload_t *bp;
+	struct iov_tail data;
 
 	if (src4 && dst4) {
 		bp = vu_payloadv4(base);
-		csum_udp4(&bp->uh, *src4, *dst4, iov_vu, iov_used,
-			  (char *)&bp->data - base);
+		data = IOV_TAIL(iov_vu, iov_used, (char *)&bp->data - base);
+		csum_udp4(&bp->uh, *src4, *dst4, &data);
 	} else {
 		bp = vu_payloadv6(base);
-		csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6,
-			  iov_vu, iov_used, (char *)&bp->data - base);
+		data = IOV_TAIL(iov_vu, iov_used, (char *)&bp->data - base);
+		csum_udp6(&bp->uh, &toside->oaddr.a6, &toside->eaddr.a6, &data);
 	}
 }
 
-- 
2.47.0


  parent reply	other threads:[~2024-11-27  3:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-27  3:54 [PATCH 0/7] TCP buffer handling cleanups (including vhost) David Gibson
2024-11-27  3:54 ` [PATCH 1/7] iov: iov tail helpers David Gibson
2024-11-27  3:54 ` David Gibson [this message]
2024-11-27  3:54 ` [PATCH 3/7] tcp: Pass TCP header and payload separately to tcp_update_check_tcp[46]() David Gibson
2024-11-27  3:54 ` [PATCH 4/7] tcp: Pass TCP header and payload separately to tcp_fill_headers[46]() David Gibson
2024-11-27  3:54 ` [PATCH 5/7] tcp: Merge tcp_update_check_tcp[46]() David Gibson
2024-11-27  3:54 ` [PATCH 6/7] tcp: Merge tcp_fill_headers[46]() with each other David Gibson
2024-11-27  3:54 ` [PATCH 7/7] tcp_vu: Remove unnecessary tcp_vu_update_check() function David Gibson

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=20241127035410.1167773-3-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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).