aboutgitcodebugslistschat
path: root/checksum.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-11-27 14:54:05 +1100
committerStefano Brivio <sbrivio@redhat.com>2024-11-28 14:03:16 +0100
commit67151090bc349d9eec5a0b303d0cb3347b755251 (patch)
tree4e03fce4394192bb30056e9a91921eb7c5e3beba /checksum.c
parentf9311031713ab8f18e9c872a42a8f6a9935954ec (diff)
downloadpasst-67151090bc349d9eec5a0b303d0cb3347b755251.tar
passt-67151090bc349d9eec5a0b303d0cb3347b755251.tar.gz
passt-67151090bc349d9eec5a0b303d0cb3347b755251.tar.bz2
passt-67151090bc349d9eec5a0b303d0cb3347b755251.tar.lz
passt-67151090bc349d9eec5a0b303d0cb3347b755251.tar.xz
passt-67151090bc349d9eec5a0b303d0cb3347b755251.tar.zst
passt-67151090bc349d9eec5a0b303d0cb3347b755251.zip
iov, checksum: Replace csum_iov() with csum_iov_tail()
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> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'checksum.c')
-rw-r--r--checksum.c58
1 files changed, 23 insertions, 35 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);
}