* [PATCH v2 0/4] tcp: use csum_iov() in tcp_update_check_tcp[4|6]()
@ 2024-09-25 8:11 Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 1/4] tcp: Use tcp_payload_t rather than tcphdr Laurent Vivier
` (3 more replies)
0 siblings, 4 replies; 13+ messages in thread
From: Laurent Vivier @ 2024-09-25 8:11 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
For vhost-user, we will need to spread TCP payload over
several buffers. To re-use tcp_update_check_tcp[4|6](),
provide an iovec rather than a pointer to a buffer.
This series updates also csum_iov() and pcap_iov() to
add an offset of bytes to skip in the iovec array.
It's based on top of "tcp: Use tcp_payload_t rather than tcphdr" that
is added in the series for convenience.
Laurent Vivier (4):
tcp: Use tcp_payload_t rather than tcphdr
pcap: Add an offset argument in pcap_iov()
checksum: Add an offset argument in csum_iov()
tcp: Update TCP checksum using an iovec array
checksum.c | 17 +++++--
checksum.h | 3 +-
pcap.c | 5 +-
pcap.h | 2 +-
tcp.c | 134 ++++++++++++++++++++++++++++++++++++-------------
tcp_buf.c | 29 -----------
tcp_internal.h | 29 +++++++++++
7 files changed, 147 insertions(+), 72 deletions(-)
--
2.46.0
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 1/4] tcp: Use tcp_payload_t rather than tcphdr
2024-09-25 8:11 [PATCH v2 0/4] tcp: use csum_iov() in tcp_update_check_tcp[4|6]() Laurent Vivier
@ 2024-09-25 8:11 ` Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 2/4] pcap: Add an offset argument in pcap_iov() Laurent Vivier
` (2 subsequent siblings)
3 siblings, 0 replies; 13+ messages in thread
From: Laurent Vivier @ 2024-09-25 8:11 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
As tcp_update_check_tcp4() and tcp_update_check_tcp6() compute the
checksum using the TCP header and the TCP payload, it is clearer
to use a pointer to tcp_payload_t that includes tcphdr and payload
rather than a pointer to tcphdr (and guessing TCP header is
followed by the payload).
Move tcp_payload_t and tcp_flags_t to tcp_internal.h.
(They will be used also by vhost-user).
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
tcp.c | 42 ++++++++++++++++++++++--------------------
tcp_buf.c | 29 -----------------------------
tcp_internal.h | 29 +++++++++++++++++++++++++++++
3 files changed, 51 insertions(+), 49 deletions(-)
diff --git a/tcp.c b/tcp.c
index 1962fcc469ed..c9472d905520 100644
--- a/tcp.c
+++ b/tcp.c
@@ -757,32 +757,34 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
/**
* tcp_update_check_tcp4() - Update TCP checksum from stored one
* @iph: IPv4 header
- * @th: TCP header followed by TCP payload
+ * @bp: TCP header followed by TCP payload
*/
-static void tcp_update_check_tcp4(const struct iphdr *iph, struct tcphdr *th)
+static void tcp_update_check_tcp4(const struct iphdr *iph,
+ struct tcp_payload_t *bp)
{
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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
- th->check = 0;
- th->check = csum(th, l4len, sum);
+ bp->th.check = 0;
+ bp->th.check = csum(bp, l4len, sum);
}
/**
* tcp_update_check_tcp6() - Calculate TCP checksum for IPv6
* @ip6h: IPv6 header
- * @th: TCP header followed by TCP payload
+ * @bp: TCP header followed by TCP payload
*/
-static void tcp_update_check_tcp6(struct ipv6hdr *ip6h, struct tcphdr *th)
+static void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
+ struct tcp_payload_t *bp)
{
uint16_t l4len = ntohs(ip6h->payload_len);
uint32_t sum = proto_ipv6_header_psum(l4len, IPPROTO_TCP,
&ip6h->saddr, &ip6h->daddr);
- th->check = 0;
- th->check = csum(th, l4len, sum);
+ bp->th.check = 0;
+ bp->th.check = csum(bp, l4len, sum);
}
/**
@@ -902,7 +904,7 @@ static void tcp_fill_header(struct tcphdr *th,
* @conn: Connection pointer
* @taph: tap backend specific header
* @iph: Pointer to IPv4 header
- * @th: Pointer to TCP header
+ * @bp: Pointer to TCP header followed by TCP payload
* @dlen: TCP payload length
* @check: Checksum, if already known
* @seq: Sequence number for this segment
@@ -912,14 +914,14 @@ static void tcp_fill_header(struct tcphdr *th,
*/
static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
struct tap_hdr *taph,
- struct iphdr *iph, struct tcphdr *th,
+ struct iphdr *iph, struct tcp_payload_t *bp,
size_t dlen, const uint16_t *check,
uint32_t seq, bool no_tcp_csum)
{
const struct flowside *tapside = TAPFLOW(conn);
const struct in_addr *src4 = inany_v4(&tapside->oaddr);
const struct in_addr *dst4 = inany_v4(&tapside->eaddr);
- size_t l4len = dlen + sizeof(*th);
+ size_t l4len = dlen + sizeof(bp->th);
size_t l3len = l4len + sizeof(*iph);
ASSERT(src4 && dst4);
@@ -931,12 +933,12 @@ static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
iph->check = check ? *check :
csum_ip4_header(l3len, IPPROTO_TCP, *src4, *dst4);
- tcp_fill_header(th, conn, seq);
+ tcp_fill_header(&bp->th, conn, seq);
if (no_tcp_csum)
- th->check = 0;
+ bp->th.check = 0;
else
- tcp_update_check_tcp4(iph, th);
+ tcp_update_check_tcp4(iph, bp);
tap_hdr_update(taph, l3len + sizeof(struct ethhdr));
@@ -948,7 +950,7 @@ static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
* @conn: Connection pointer
* @taph: tap backend specific header
* @ip6h: Pointer to IPv6 header
- * @th: Pointer to TCP header
+ * @bp: Pointer to TCP header followed by TCP payload
* @dlen: TCP payload length
* @check: Checksum, if already known
* @seq: Sequence number for this segment
@@ -958,11 +960,11 @@ static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
*/
static size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
struct tap_hdr *taph,
- struct ipv6hdr *ip6h, struct tcphdr *th,
+ struct ipv6hdr *ip6h, struct tcp_payload_t *bp,
size_t dlen, uint32_t seq, bool no_tcp_csum)
{
const struct flowside *tapside = TAPFLOW(conn);
- size_t l4len = dlen + sizeof(*th);
+ size_t l4len = dlen + sizeof(bp->th);
ip6h->payload_len = htons(l4len);
ip6h->saddr = tapside->oaddr.a6;
@@ -976,12 +978,12 @@ static size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
ip6h->flow_lbl[1] = (conn->sock >> 8) & 0xff;
ip6h->flow_lbl[2] = (conn->sock >> 0) & 0xff;
- tcp_fill_header(th, conn, seq);
+ tcp_fill_header(&bp->th, conn, seq);
if (no_tcp_csum)
- th->check = 0;
+ bp->th.check = 0;
else
- tcp_update_check_tcp6(ip6h, th);
+ tcp_update_check_tcp6(ip6h, bp);
tap_hdr_update(taph, l4len + sizeof(*ip6h) + sizeof(struct ethhdr));
diff --git a/tcp_buf.c b/tcp_buf.c
index ffbff5e4b485..238827b01d90 100644
--- a/tcp_buf.c
+++ b/tcp_buf.c
@@ -38,35 +38,6 @@
(c->mode == MODE_PASTA ? 1 : TCP_FRAMES_MEM)
/* Static buffers */
-/**
- * struct tcp_payload_t - TCP header and data to send segments with payload
- * @th: TCP header
- * @data: TCP data
- */
-struct tcp_payload_t {
- struct tcphdr th;
- uint8_t data[IP_MAX_MTU - sizeof(struct tcphdr)];
-#ifdef __AVX2__
-} __attribute__ ((packed, aligned(32))); /* For AVX2 checksum routines */
-#else
-} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
-#endif
-
-/**
- * struct tcp_flags_t - TCP header and data to send zero-length
- * segments (flags)
- * @th: TCP header
- * @opts TCP options
- */
-struct tcp_flags_t {
- struct tcphdr th;
- char opts[OPT_MSS_LEN + OPT_WS_LEN + 1];
-#ifdef __AVX2__
-} __attribute__ ((packed, aligned(32)));
-#else
-} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
-#endif
-
/* Ethernet header for IPv4 frames */
static struct ethhdr tcp4_eth_src;
diff --git a/tcp_internal.h b/tcp_internal.h
index de06db1438d6..2f74ffeff8f3 100644
--- a/tcp_internal.h
+++ b/tcp_internal.h
@@ -63,6 +63,35 @@ enum tcp_iov_parts {
TCP_NUM_IOVS
};
+/**
+ * struct tcp_payload_t - TCP header and data to send segments with payload
+ * @th: TCP header
+ * @data: TCP data
+ */
+struct tcp_payload_t {
+ struct tcphdr th;
+ uint8_t data[IP_MAX_MTU - sizeof(struct tcphdr)];
+#ifdef __AVX2__
+} __attribute__ ((packed, aligned(32))); /* For AVX2 checksum routines */
+#else
+} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
+#endif
+
+/**
+ * struct tcp_flags_t - TCP header and data to send zero-length
+ * segments (flags)
+ * @th: TCP header
+ * @opts TCP options
+ */
+struct tcp_flags_t {
+ struct tcphdr th;
+ char opts[OPT_MSS_LEN + OPT_WS_LEN + 1];
+#ifdef __AVX2__
+} __attribute__ ((packed, aligned(32)));
+#else
+} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
+#endif
+
extern char tcp_buf_discard [MAX_WINDOW];
void conn_flag_do(const struct ctx *c, struct tcp_tap_conn *conn,
--
@@ -63,6 +63,35 @@ enum tcp_iov_parts {
TCP_NUM_IOVS
};
+/**
+ * struct tcp_payload_t - TCP header and data to send segments with payload
+ * @th: TCP header
+ * @data: TCP data
+ */
+struct tcp_payload_t {
+ struct tcphdr th;
+ uint8_t data[IP_MAX_MTU - sizeof(struct tcphdr)];
+#ifdef __AVX2__
+} __attribute__ ((packed, aligned(32))); /* For AVX2 checksum routines */
+#else
+} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
+#endif
+
+/**
+ * struct tcp_flags_t - TCP header and data to send zero-length
+ * segments (flags)
+ * @th: TCP header
+ * @opts TCP options
+ */
+struct tcp_flags_t {
+ struct tcphdr th;
+ char opts[OPT_MSS_LEN + OPT_WS_LEN + 1];
+#ifdef __AVX2__
+} __attribute__ ((packed, aligned(32)));
+#else
+} __attribute__ ((packed, aligned(__alignof__(unsigned int))));
+#endif
+
extern char tcp_buf_discard [MAX_WINDOW];
void conn_flag_do(const struct ctx *c, struct tcp_tap_conn *conn,
--
2.46.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v2 2/4] pcap: Add an offset argument in pcap_iov()
2024-09-25 8:11 [PATCH v2 0/4] tcp: use csum_iov() in tcp_update_check_tcp[4|6]() Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 1/4] tcp: Use tcp_payload_t rather than tcphdr Laurent Vivier
@ 2024-09-25 8:11 ` Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 3/4] checksum: Add an offset argument in csum_iov() Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array Laurent Vivier
3 siblings, 0 replies; 13+ messages in thread
From: Laurent Vivier @ 2024-09-25 8:11 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
The offset is passed directly to pcap_frame() and allows
any headers that are not part of the frame to
capture to be skipped.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
pcap.c | 5 +++--
pcap.h | 2 +-
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/pcap.c b/pcap.c
index e6b5ced4a9f8..6ee6cdfd261a 100644
--- a/pcap.c
+++ b/pcap.c
@@ -138,9 +138,10 @@ void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
* @iov: Pointer to the array of struct iovec describing the I/O vector
* containing packet data to write, including L2 header
* @iovcnt: Number of buffers (@iov entries)
+ * @offset: Offset of the L2 frame within the full data length
*/
/* cppcheck-suppress unusedFunction */
-void pcap_iov(const struct iovec *iov, size_t iovcnt)
+void pcap_iov(const struct iovec *iov, size_t iovcnt, size_t offset)
{
struct timespec now;
@@ -148,7 +149,7 @@ void pcap_iov(const struct iovec *iov, size_t iovcnt)
return;
clock_gettime(CLOCK_REALTIME, &now);
- pcap_frame(iov, iovcnt, 0, &now);
+ pcap_frame(iov, iovcnt, offset, &now);
}
/**
diff --git a/pcap.h b/pcap.h
index 533923749222..9795f2e8adc5 100644
--- a/pcap.h
+++ b/pcap.h
@@ -9,7 +9,7 @@
void pcap(const char *pkt, size_t l2len);
void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
size_t offset);
-void pcap_iov(const struct iovec *iov, size_t iovcnt);
+void pcap_iov(const struct iovec *iov, size_t iovcnt, size_t offset);
void pcap_init(struct ctx *c);
#endif /* PCAP_H */
--
@@ -9,7 +9,7 @@
void pcap(const char *pkt, size_t l2len);
void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n,
size_t offset);
-void pcap_iov(const struct iovec *iov, size_t iovcnt);
+void pcap_iov(const struct iovec *iov, size_t iovcnt, size_t offset);
void pcap_init(struct ctx *c);
#endif /* PCAP_H */
--
2.46.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v2 3/4] checksum: Add an offset argument in csum_iov()
2024-09-25 8:11 [PATCH v2 0/4] tcp: use csum_iov() in tcp_update_check_tcp[4|6]() Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 1/4] tcp: Use tcp_payload_t rather than tcphdr Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 2/4] pcap: Add an offset argument in pcap_iov() Laurent Vivier
@ 2024-09-25 8:11 ` Laurent Vivier
2024-09-25 17:39 ` Stefano Brivio
2024-09-26 1:31 ` David Gibson
2024-09-25 8:11 ` [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array Laurent Vivier
3 siblings, 2 replies; 13+ messages in thread
From: Laurent Vivier @ 2024-09-25 8:11 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
The offset allows any headers are that are not part
of the data to checksum to be skipped.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
Notes:
v2:
- check iov_skip_bytes() return value
checksum.c | 16 ++++++++++++++--
checksum.h | 3 ++-
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/checksum.c b/checksum.c
index 006614fcbb28..68ffaddb5bb0 100644
--- a/checksum.c
+++ b/checksum.c
@@ -59,6 +59,7 @@
#include "util.h"
#include "ip.h"
#include "checksum.h"
+#include "iov.h"
/* Checksums are optional for UDP over IPv4, so we usually just set
* them to 0. Change this to 1 to calculate real UDP over IPv4
@@ -498,15 +499,26 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
* @iov Pointer to the array of IO vectors
* @n Length of the array
* @init Initial 32-bit checksum, 0 for no pre-computed checksum
+ * @offset: Offset of the data to checksum within the full data length
*
* Return: 16-bit folded, complemented checksum
*/
/* cppcheck-suppress unusedFunction */
-uint16_t csum_iov(const struct iovec *iov, size_t n, uint32_t init)
+uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
+ uint32_t init)
{
unsigned int i;
+ size_t first;
- for (i = 0; i < n; i++)
+ 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, init);
+ i++;
+
+ for (; i < n; i++)
init = csum_unfolded(iov[i].iov_base, iov[i].iov_len, init);
return (uint16_t)~csum_fold(init);
diff --git a/checksum.h b/checksum.h
index c5964ac78921..49f7472dd1b6 100644
--- a/checksum.h
+++ b/checksum.h
@@ -32,6 +32,7 @@ void csum_icmp6(struct icmp6hdr *icmp6hr,
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, uint32_t init);
+uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
+ uint32_t init);
#endif /* CHECKSUM_H */
--
@@ -32,6 +32,7 @@ void csum_icmp6(struct icmp6hdr *icmp6hr,
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, uint32_t init);
+uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
+ uint32_t init);
#endif /* CHECKSUM_H */
--
2.46.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array
2024-09-25 8:11 [PATCH v2 0/4] tcp: use csum_iov() in tcp_update_check_tcp[4|6]() Laurent Vivier
` (2 preceding siblings ...)
2024-09-25 8:11 ` [PATCH v2 3/4] checksum: Add an offset argument in csum_iov() Laurent Vivier
@ 2024-09-25 8:11 ` Laurent Vivier
2024-09-25 17:39 ` Stefano Brivio
2024-09-26 1:45 ` David Gibson
3 siblings, 2 replies; 13+ messages in thread
From: Laurent Vivier @ 2024-09-25 8:11 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
TCP header and payload are supposed to be in the same buffer,
and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute
the checksum from the base address of the header using the
length of the IP payload.
In the future (for vhost-user) we need to dispatch the TCP header and
the TCP payload through several buffers. To be able to manage that, we
provide an iovec array that points to the data of the TCP frame.
We provide also an offset to be able to provide an array that contains
the TCP frame embedded in an lower level frame, and this offset points
to the TCP header inside the iovec array.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
Notes:
v2:
- s/payload_offset/l4offset/
- check memory address of the checksum (alignment, iovec boundaries)
checksum.c | 1 -
tcp.c | 116 ++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 88 insertions(+), 29 deletions(-)
diff --git a/checksum.c b/checksum.c
index 68ffaddb5bb0..4854c1937c39 100644
--- a/checksum.c
+++ b/checksum.c
@@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
*
* Return: 16-bit folded, complemented checksum
*/
-/* cppcheck-suppress unusedFunction */
uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
uint32_t init)
{
diff --git a/tcp.c b/tcp.c
index c9472d905520..f0a6f7a507a7 100644
--- a/tcp.c
+++ b/tcp.c
@@ -755,36 +755,81 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
}
/**
- * tcp_update_check_tcp4() - Update TCP checksum from stored one
- * @iph: IPv4 header
- * @bp: TCP header followed by TCP payload
- */
-static void tcp_update_check_tcp4(const struct iphdr *iph,
- struct tcp_payload_t *bp)
+ * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6
+ * @src: IPv4 source address
+ * @dst: IPv4 destination address
+ * @iov: Pointer to the array of IO vectors
+ * @iov_cnt: Length of the array
+ * @l4offset: IPv4 payload offset in the iovec array
+ */
+void tcp_update_check_tcp4(struct in_addr src,
+ struct in_addr dst,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset)
{
- 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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
+ size_t check_ofs;
+ __sum16 *check;
+ int check_idx;
+ uint32_t sum;
+
+ sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset,
+ IPPROTO_TCP, src, dst);
+
+ check_idx = iov_skip_bytes(iov, iov_cnt,
+ l4offset + offsetof(struct tcphdr, check),
+ &check_ofs);
+
+ if (check_idx >= iov_cnt)
+ die("TCP4 buffer is too small");
+ if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
+ die("TCP4 checksum field memory is not contiguous");
+
+ check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs);
- bp->th.check = 0;
- bp->th.check = csum(bp, l4len, sum);
+ if ((uintptr_t)check & (__alignof__(*check) - 1))
+ die("TCP4 checksum field is not correctly aligned in memory");
+
+ *check = 0;
+ *check = csum_iov(iov, iov_cnt, l4offset, sum);
}
/**
* tcp_update_check_tcp6() - Calculate TCP checksum for IPv6
- * @ip6h: IPv6 header
- * @bp: TCP header followed by TCP payload
- */
-static void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
- struct tcp_payload_t *bp)
+ * @src: IPv6 source address
+ * @dst: IPv6 destination address
+ * @iov: Pointer to the array of IO vectors
+ * @iov_cnt: Length of the array
+ * @l4offset: IPv6 payload offset in the iovec array
+ */
+void tcp_update_check_tcp6(const struct in6_addr *src,
+ const struct in6_addr *dst,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset)
{
- uint16_t l4len = ntohs(ip6h->payload_len);
- uint32_t sum = proto_ipv6_header_psum(l4len, IPPROTO_TCP,
- &ip6h->saddr, &ip6h->daddr);
+ size_t check_ofs;
+ __sum16 *check;
+ int check_idx;
+ uint32_t sum;
+
+ sum = proto_ipv6_header_psum(iov_size(iov, iov_cnt) - l4offset,
+ IPPROTO_TCP, src, dst);
+
+ check_idx = iov_skip_bytes(iov, iov_cnt,
+ l4offset + offsetof(struct tcphdr, check),
+ &check_ofs);
+
+ if (check_idx >= iov_cnt)
+ die("TCP6 buffer is too small");
+ if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
+ die("TCP6 checksum field memory is not contiguous");
- bp->th.check = 0;
- bp->th.check = csum(bp, l4len, sum);
+ check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs);
+
+ if ((uintptr_t)check & (__alignof__(*check) - 1))
+ die("TCP6 checksum field is not correctly aligned in memory");
+
+ *check = 0;
+ *check = csum_iov(iov, iov_cnt, l4offset, sum);
}
/**
@@ -935,10 +980,18 @@ static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
tcp_fill_header(&bp->th, conn, seq);
- if (no_tcp_csum)
+ if (no_tcp_csum) {
bp->th.check = 0;
- else
- tcp_update_check_tcp4(iph, bp);
+ } else {
+ const struct iovec iov = {
+ .iov_base = bp,
+ .iov_len = ntohs(iph->tot_len) - sizeof(struct iphdr),
+ };
+ struct in_addr saddr = { .s_addr = iph->saddr };
+ struct in_addr daddr = { .s_addr = iph->daddr };
+
+ tcp_update_check_tcp4(saddr, daddr, &iov, 1, 0);
+ }
tap_hdr_update(taph, l3len + sizeof(struct ethhdr));
@@ -980,10 +1033,17 @@ static size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
tcp_fill_header(&bp->th, conn, seq);
- if (no_tcp_csum)
+ if (no_tcp_csum) {
bp->th.check = 0;
- else
- tcp_update_check_tcp6(ip6h, bp);
+ } else {
+ const struct iovec iov = {
+ .iov_base = bp,
+ .iov_len = ntohs(ip6h->payload_len)
+ };
+
+ tcp_update_check_tcp6(&ip6h->saddr, &ip6h->daddr,
+ &iov, 1, 0);
+ }
tap_hdr_update(taph, l4len + sizeof(*ip6h) + sizeof(struct ethhdr));
--
@@ -755,36 +755,81 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
}
/**
- * tcp_update_check_tcp4() - Update TCP checksum from stored one
- * @iph: IPv4 header
- * @bp: TCP header followed by TCP payload
- */
-static void tcp_update_check_tcp4(const struct iphdr *iph,
- struct tcp_payload_t *bp)
+ * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6
+ * @src: IPv4 source address
+ * @dst: IPv4 destination address
+ * @iov: Pointer to the array of IO vectors
+ * @iov_cnt: Length of the array
+ * @l4offset: IPv4 payload offset in the iovec array
+ */
+void tcp_update_check_tcp4(struct in_addr src,
+ struct in_addr dst,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset)
{
- 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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
+ size_t check_ofs;
+ __sum16 *check;
+ int check_idx;
+ uint32_t sum;
+
+ sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset,
+ IPPROTO_TCP, src, dst);
+
+ check_idx = iov_skip_bytes(iov, iov_cnt,
+ l4offset + offsetof(struct tcphdr, check),
+ &check_ofs);
+
+ if (check_idx >= iov_cnt)
+ die("TCP4 buffer is too small");
+ if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
+ die("TCP4 checksum field memory is not contiguous");
+
+ check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs);
- bp->th.check = 0;
- bp->th.check = csum(bp, l4len, sum);
+ if ((uintptr_t)check & (__alignof__(*check) - 1))
+ die("TCP4 checksum field is not correctly aligned in memory");
+
+ *check = 0;
+ *check = csum_iov(iov, iov_cnt, l4offset, sum);
}
/**
* tcp_update_check_tcp6() - Calculate TCP checksum for IPv6
- * @ip6h: IPv6 header
- * @bp: TCP header followed by TCP payload
- */
-static void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
- struct tcp_payload_t *bp)
+ * @src: IPv6 source address
+ * @dst: IPv6 destination address
+ * @iov: Pointer to the array of IO vectors
+ * @iov_cnt: Length of the array
+ * @l4offset: IPv6 payload offset in the iovec array
+ */
+void tcp_update_check_tcp6(const struct in6_addr *src,
+ const struct in6_addr *dst,
+ const struct iovec *iov, int iov_cnt,
+ size_t l4offset)
{
- uint16_t l4len = ntohs(ip6h->payload_len);
- uint32_t sum = proto_ipv6_header_psum(l4len, IPPROTO_TCP,
- &ip6h->saddr, &ip6h->daddr);
+ size_t check_ofs;
+ __sum16 *check;
+ int check_idx;
+ uint32_t sum;
+
+ sum = proto_ipv6_header_psum(iov_size(iov, iov_cnt) - l4offset,
+ IPPROTO_TCP, src, dst);
+
+ check_idx = iov_skip_bytes(iov, iov_cnt,
+ l4offset + offsetof(struct tcphdr, check),
+ &check_ofs);
+
+ if (check_idx >= iov_cnt)
+ die("TCP6 buffer is too small");
+ if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
+ die("TCP6 checksum field memory is not contiguous");
- bp->th.check = 0;
- bp->th.check = csum(bp, l4len, sum);
+ check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs);
+
+ if ((uintptr_t)check & (__alignof__(*check) - 1))
+ die("TCP6 checksum field is not correctly aligned in memory");
+
+ *check = 0;
+ *check = csum_iov(iov, iov_cnt, l4offset, sum);
}
/**
@@ -935,10 +980,18 @@ static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
tcp_fill_header(&bp->th, conn, seq);
- if (no_tcp_csum)
+ if (no_tcp_csum) {
bp->th.check = 0;
- else
- tcp_update_check_tcp4(iph, bp);
+ } else {
+ const struct iovec iov = {
+ .iov_base = bp,
+ .iov_len = ntohs(iph->tot_len) - sizeof(struct iphdr),
+ };
+ struct in_addr saddr = { .s_addr = iph->saddr };
+ struct in_addr daddr = { .s_addr = iph->daddr };
+
+ tcp_update_check_tcp4(saddr, daddr, &iov, 1, 0);
+ }
tap_hdr_update(taph, l3len + sizeof(struct ethhdr));
@@ -980,10 +1033,17 @@ static size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
tcp_fill_header(&bp->th, conn, seq);
- if (no_tcp_csum)
+ if (no_tcp_csum) {
bp->th.check = 0;
- else
- tcp_update_check_tcp6(ip6h, bp);
+ } else {
+ const struct iovec iov = {
+ .iov_base = bp,
+ .iov_len = ntohs(ip6h->payload_len)
+ };
+
+ tcp_update_check_tcp6(&ip6h->saddr, &ip6h->daddr,
+ &iov, 1, 0);
+ }
tap_hdr_update(taph, l4len + sizeof(*ip6h) + sizeof(struct ethhdr));
--
2.46.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array
2024-09-25 8:11 ` [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array Laurent Vivier
@ 2024-09-25 17:39 ` Stefano Brivio
2024-09-26 1:56 ` David Gibson
2024-09-26 1:45 ` David Gibson
1 sibling, 1 reply; 13+ messages in thread
From: Stefano Brivio @ 2024-09-25 17:39 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
On Wed, 25 Sep 2024 10:11:25 +0200
Laurent Vivier <lvivier@redhat.com> wrote:
> TCP header and payload are supposed to be in the same buffer,
> and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute
> the checksum from the base address of the header using the
> length of the IP payload.
>
> In the future (for vhost-user) we need to dispatch the TCP header and
> the TCP payload through several buffers. To be able to manage that, we
> provide an iovec array that points to the data of the TCP frame.
> We provide also an offset to be able to provide an array that contains
> the TCP frame embedded in an lower level frame, and this offset points
> to the TCP header inside the iovec array.
>
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---
>
> Notes:
> v2:
> - s/payload_offset/l4offset/
> - check memory address of the checksum (alignment, iovec boundaries)
>
> checksum.c | 1 -
> tcp.c | 116 ++++++++++++++++++++++++++++++++++++++++-------------
> 2 files changed, 88 insertions(+), 29 deletions(-)
>
> diff --git a/checksum.c b/checksum.c
> index 68ffaddb5bb0..4854c1937c39 100644
> --- a/checksum.c
> +++ b/checksum.c
> @@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
> *
> * Return: 16-bit folded, complemented checksum
> */
> -/* cppcheck-suppress unusedFunction */
> uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
> uint32_t init)
> {
> diff --git a/tcp.c b/tcp.c
> index c9472d905520..f0a6f7a507a7 100644
> --- a/tcp.c
> +++ b/tcp.c
> @@ -755,36 +755,81 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
> }
>
> /**
> - * tcp_update_check_tcp4() - Update TCP checksum from stored one
> - * @iph: IPv4 header
> - * @bp: TCP header followed by TCP payload
> - */
> -static void tcp_update_check_tcp4(const struct iphdr *iph,
> - struct tcp_payload_t *bp)
> + * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6
> + * @src: IPv4 source address
> + * @dst: IPv4 destination address
> + * @iov: Pointer to the array of IO vectors
> + * @iov_cnt: Length of the array
> + * @l4offset: IPv4 payload offset in the iovec array
> + */
> +void tcp_update_check_tcp4(struct in_addr src,
> + struct in_addr dst,
> + const struct iovec *iov, int iov_cnt,
> + size_t l4offset)
> {
> - 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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
> + size_t check_ofs;
> + __sum16 *check;
> + int check_idx;
> + uint32_t sum;
> +
> + sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset,
> + IPPROTO_TCP, src, dst);
> +
> + check_idx = iov_skip_bytes(iov, iov_cnt,
> + l4offset + offsetof(struct tcphdr, check),
> + &check_ofs);
> +
> + if (check_idx >= iov_cnt)
> + die("TCP4 buffer is too small");
> + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
> + die("TCP4 checksum field memory is not contiguous");
I'm not really fond of those die() calls. First off, they should report
a couple more details (at least check_idx, iov_cnt).
Second, we could fail gracefully (hence, we should) instead of aborting
the whole thing: those could be err() calls.
If we fail to calculate checksums, we can leave them as zero, and the
receiver will drop those frames anyway, if you don't want to add
complexity (propagation of return values) for something that should
never happen.
The rest looks good to me, but I didn't go through David's comments so
I'll wait for his review before applying.
--
Stefano
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 3/4] checksum: Add an offset argument in csum_iov()
2024-09-25 8:11 ` [PATCH v2 3/4] checksum: Add an offset argument in csum_iov() Laurent Vivier
@ 2024-09-25 17:39 ` Stefano Brivio
2024-09-26 1:31 ` David Gibson
1 sibling, 0 replies; 13+ messages in thread
From: Stefano Brivio @ 2024-09-25 17:39 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
On Wed, 25 Sep 2024 10:11:24 +0200
Laurent Vivier <lvivier@redhat.com> wrote:
> The offset allows any headers are that are not part
> of the data to checksum to be skipped.
>
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---
>
> Notes:
> v2:
> - check iov_skip_bytes() return value
>
> checksum.c | 16 ++++++++++++++--
> checksum.h | 3 ++-
> 2 files changed, 16 insertions(+), 3 deletions(-)
>
> diff --git a/checksum.c b/checksum.c
> index 006614fcbb28..68ffaddb5bb0 100644
> --- a/checksum.c
> +++ b/checksum.c
> @@ -59,6 +59,7 @@
> #include "util.h"
> #include "ip.h"
> #include "checksum.h"
> +#include "iov.h"
>
> /* Checksums are optional for UDP over IPv4, so we usually just set
> * them to 0. Change this to 1 to calculate real UDP over IPv4
> @@ -498,15 +499,26 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
> * @iov Pointer to the array of IO vectors
> * @n Length of the array
> * @init Initial 32-bit checksum, 0 for no pre-computed checksum
> + * @offset: Offset of the data to checksum within the full data length
Nit, which I can fix up on merge: @init and @offset are swapped here.
> *
> * Return: 16-bit folded, complemented checksum
> */
> /* cppcheck-suppress unusedFunction */
> -uint16_t csum_iov(const struct iovec *iov, size_t n, uint32_t init)
> +uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
> + uint32_t init)
> {
> unsigned int i;
> + size_t first;
>
> - for (i = 0; i < n; i++)
> + 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, init);
> + i++;
> +
> + for (; i < n; i++)
> init = csum_unfolded(iov[i].iov_base, iov[i].iov_len, init);
I wonder if this could become:
for (i = iov_skip_bytes(iov, n, offset, &start); i < n; i++) {
init = csum_unfolded((char *)iov[i].iov_base + start,
iov[i].iov_len, init);
start = 0;
}
return (uint16_t)~csum_fold(init);
...but I haven't tested it and didn't really think it through. I'm fine
with the original version too.
--
Stefano
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 3/4] checksum: Add an offset argument in csum_iov()
2024-09-25 8:11 ` [PATCH v2 3/4] checksum: Add an offset argument in csum_iov() Laurent Vivier
2024-09-25 17:39 ` Stefano Brivio
@ 2024-09-26 1:31 ` David Gibson
1 sibling, 0 replies; 13+ messages in thread
From: David Gibson @ 2024-09-26 1:31 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
[-- Attachment #1: Type: text/plain, Size: 2652 bytes --]
On Wed, Sep 25, 2024 at 10:11:24AM +0200, Laurent Vivier wrote:
> The offset allows any headers are that are not part
> of the data to checksum to be skipped.
>
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
LGTM, with the exception of the nits that Stefano already noted.
> ---
>
> Notes:
> v2:
> - check iov_skip_bytes() return value
>
> checksum.c | 16 ++++++++++++++--
> checksum.h | 3 ++-
> 2 files changed, 16 insertions(+), 3 deletions(-)
>
> diff --git a/checksum.c b/checksum.c
> index 006614fcbb28..68ffaddb5bb0 100644
> --- a/checksum.c
> +++ b/checksum.c
> @@ -59,6 +59,7 @@
> #include "util.h"
> #include "ip.h"
> #include "checksum.h"
> +#include "iov.h"
>
> /* Checksums are optional for UDP over IPv4, so we usually just set
> * them to 0. Change this to 1 to calculate real UDP over IPv4
> @@ -498,15 +499,26 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
> * @iov Pointer to the array of IO vectors
> * @n Length of the array
> * @init Initial 32-bit checksum, 0 for no pre-computed checksum
> + * @offset: Offset of the data to checksum within the full data length
> *
> * Return: 16-bit folded, complemented checksum
> */
> /* cppcheck-suppress unusedFunction */
> -uint16_t csum_iov(const struct iovec *iov, size_t n, uint32_t init)
> +uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
> + uint32_t init)
> {
> unsigned int i;
> + size_t first;
>
> - for (i = 0; i < n; i++)
> + 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, init);
> + i++;
> +
> + for (; i < n; i++)
> init = csum_unfolded(iov[i].iov_base, iov[i].iov_len, init);
>
> return (uint16_t)~csum_fold(init);
> diff --git a/checksum.h b/checksum.h
> index c5964ac78921..49f7472dd1b6 100644
> --- a/checksum.h
> +++ b/checksum.h
> @@ -32,6 +32,7 @@ void csum_icmp6(struct icmp6hdr *icmp6hr,
> 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, uint32_t init);
> +uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
> + uint32_t init);
>
> #endif /* CHECKSUM_H */
--
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 --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array
2024-09-25 8:11 ` [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array Laurent Vivier
2024-09-25 17:39 ` Stefano Brivio
@ 2024-09-26 1:45 ` David Gibson
2024-09-27 13:49 ` Laurent Vivier
1 sibling, 1 reply; 13+ messages in thread
From: David Gibson @ 2024-09-26 1:45 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
[-- Attachment #1: Type: text/plain, Size: 7604 bytes --]
On Wed, Sep 25, 2024 at 10:11:25AM +0200, Laurent Vivier wrote:
> TCP header and payload are supposed to be in the same buffer,
> and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute
> the checksum from the base address of the header using the
> length of the IP payload.
>
> In the future (for vhost-user) we need to dispatch the TCP header and
> the TCP payload through several buffers. To be able to manage that, we
> provide an iovec array that points to the data of the TCP frame.
> We provide also an offset to be able to provide an array that contains
> the TCP frame embedded in an lower level frame, and this offset points
> to the TCP header inside the iovec array.
>
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---
>
> Notes:
> v2:
> - s/payload_offset/l4offset/
> - check memory address of the checksum (alignment, iovec boundaries)
>
> checksum.c | 1 -
> tcp.c | 116 ++++++++++++++++++++++++++++++++++++++++-------------
> 2 files changed, 88 insertions(+), 29 deletions(-)
>
> diff --git a/checksum.c b/checksum.c
> index 68ffaddb5bb0..4854c1937c39 100644
> --- a/checksum.c
> +++ b/checksum.c
> @@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
> *
> * Return: 16-bit folded, complemented checksum
> */
> -/* cppcheck-suppress unusedFunction */
> uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
> uint32_t init)
> {
> diff --git a/tcp.c b/tcp.c
> index c9472d905520..f0a6f7a507a7 100644
> --- a/tcp.c
> +++ b/tcp.c
> @@ -755,36 +755,81 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
> }
>
> /**
> - * tcp_update_check_tcp4() - Update TCP checksum from stored one
> - * @iph: IPv4 header
> - * @bp: TCP header followed by TCP payload
> - */
> -static void tcp_update_check_tcp4(const struct iphdr *iph,
> - struct tcp_payload_t *bp)
> + * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6
Nit: s/IPv6/IPv4/
> + * @src: IPv4 source address
> + * @dst: IPv4 destination address
> + * @iov: Pointer to the array of IO vectors
> + * @iov_cnt: Length of the array
> + * @l4offset: IPv4 payload offset in the iovec array
> + */
> +void tcp_update_check_tcp4(struct in_addr src,
> + struct in_addr dst,
> + const struct iovec *iov, int iov_cnt,
> + size_t l4offset)
> {
> - 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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
> + size_t check_ofs;
> + __sum16 *check;
> + int check_idx;
> + uint32_t sum;
> +
> + sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset,
> + IPPROTO_TCP, src, dst);
Previously, we took the size from the IP header, which we'd previously
calculated. It seems a shame to replace that with a call to
iov_size() which will make another pass through the whole vector.
> +
> + check_idx = iov_skip_bytes(iov, iov_cnt,
> + l4offset + offsetof(struct tcphdr, check),
> + &check_ofs);
> +
> + if (check_idx >= iov_cnt)
> + die("TCP4 buffer is too small");
> + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
> + die("TCP4 checksum field memory is not contiguous");
> +
> + check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs);
Strictly speaking, it's UB to even *create* an improperly aligned
pointer, even if you never dereference it. So the alignment check
should go before casting to (__sum16 *).
> - bp->th.check = 0;
> - bp->th.check = csum(bp, l4len, sum);
> + if ((uintptr_t)check & (__alignof__(*check) - 1))
> + die("TCP4 checksum field is not correctly aligned in memory");
I really think it would be worth packaging this logic (skip_bytes +
contiguous check + alignment check + pointer cast) into another helper
(iov_field()?). I strongly suspect we'll have further use for it down
the line.
> +
> + *check = 0;
> + *check = csum_iov(iov, iov_cnt, l4offset, sum);
> }
>
> /**
> * tcp_update_check_tcp6() - Calculate TCP checksum for IPv6
> - * @ip6h: IPv6 header
> - * @bp: TCP header followed by TCP payload
> - */
> -static void tcp_update_check_tcp6(const struct ipv6hdr *ip6h,
> - struct tcp_payload_t *bp)
> + * @src: IPv6 source address
> + * @dst: IPv6 destination address
> + * @iov: Pointer to the array of IO vectors
> + * @iov_cnt: Length of the array
> + * @l4offset: IPv6 payload offset in the iovec array
> + */
> +void tcp_update_check_tcp6(const struct in6_addr *src,
> + const struct in6_addr *dst,
> + const struct iovec *iov, int iov_cnt,
> + size_t l4offset)
Same comments as the IPv4 version here apply here too.
> {
> - uint16_t l4len = ntohs(ip6h->payload_len);
> - uint32_t sum = proto_ipv6_header_psum(l4len, IPPROTO_TCP,
> - &ip6h->saddr, &ip6h->daddr);
> + size_t check_ofs;
> + __sum16 *check;
> + int check_idx;
> + uint32_t sum;
> +
> + sum = proto_ipv6_header_psum(iov_size(iov, iov_cnt) - l4offset,
> + IPPROTO_TCP, src, dst);
> +
> + check_idx = iov_skip_bytes(iov, iov_cnt,
> + l4offset + offsetof(struct tcphdr, check),
> + &check_ofs);
> +
> + if (check_idx >= iov_cnt)
> + die("TCP6 buffer is too small");
Alternatively, you could check the relevant offset against the total
size, which you've already calculated. Or indeed, it might make sense
to check the total IOV size against the minimum size for a TCP packet
somewhere earlier in the call graph. In that case this could become
an ASSERT().
> + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
> + die("TCP6 checksum field memory is not contiguous");
>
> - bp->th.check = 0;
> - bp->th.check = csum(bp, l4len, sum);
> + check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs);
> +
> + if ((uintptr_t)check & (__alignof__(*check) - 1))
> + die("TCP6 checksum field is not correctly aligned in memory");
> +
> + *check = 0;
> + *check = csum_iov(iov, iov_cnt, l4offset, sum);
> }
>
> /**
> @@ -935,10 +980,18 @@ static size_t tcp_fill_headers4(const struct tcp_tap_conn *conn,
>
> tcp_fill_header(&bp->th, conn, seq);
>
> - if (no_tcp_csum)
> + if (no_tcp_csum) {
> bp->th.check = 0;
> - else
> - tcp_update_check_tcp4(iph, bp);
> + } else {
> + const struct iovec iov = {
> + .iov_base = bp,
> + .iov_len = ntohs(iph->tot_len) - sizeof(struct iphdr),
> + };
> + struct in_addr saddr = { .s_addr = iph->saddr };
> + struct in_addr daddr = { .s_addr = iph->daddr };
> +
> + tcp_update_check_tcp4(saddr, daddr, &iov, 1, 0);
> + }
>
> tap_hdr_update(taph, l3len + sizeof(struct ethhdr));
>
> @@ -980,10 +1033,17 @@ static size_t tcp_fill_headers6(const struct tcp_tap_conn *conn,
>
> tcp_fill_header(&bp->th, conn, seq);
>
> - if (no_tcp_csum)
> + if (no_tcp_csum) {
> bp->th.check = 0;
> - else
> - tcp_update_check_tcp6(ip6h, bp);
> + } else {
> + const struct iovec iov = {
> + .iov_base = bp,
> + .iov_len = ntohs(ip6h->payload_len)
> + };
> +
> + tcp_update_check_tcp6(&ip6h->saddr, &ip6h->daddr,
> + &iov, 1, 0);
> + }
>
> tap_hdr_update(taph, l4len + sizeof(*ip6h) + sizeof(struct ethhdr));
>
--
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 --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array
2024-09-25 17:39 ` Stefano Brivio
@ 2024-09-26 1:56 ` David Gibson
2024-09-26 3:54 ` Stefano Brivio
0 siblings, 1 reply; 13+ messages in thread
From: David Gibson @ 2024-09-26 1:56 UTC (permalink / raw)
To: Stefano Brivio; +Cc: Laurent Vivier, passt-dev
[-- Attachment #1: Type: text/plain, Size: 4745 bytes --]
On Wed, Sep 25, 2024 at 07:39:19PM +0200, Stefano Brivio wrote:
> On Wed, 25 Sep 2024 10:11:25 +0200
> Laurent Vivier <lvivier@redhat.com> wrote:
>
> > TCP header and payload are supposed to be in the same buffer,
> > and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute
> > the checksum from the base address of the header using the
> > length of the IP payload.
> >
> > In the future (for vhost-user) we need to dispatch the TCP header and
> > the TCP payload through several buffers. To be able to manage that, we
> > provide an iovec array that points to the data of the TCP frame.
> > We provide also an offset to be able to provide an array that contains
> > the TCP frame embedded in an lower level frame, and this offset points
> > to the TCP header inside the iovec array.
> >
> > Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> > ---
> >
> > Notes:
> > v2:
> > - s/payload_offset/l4offset/
> > - check memory address of the checksum (alignment, iovec boundaries)
> >
> > checksum.c | 1 -
> > tcp.c | 116 ++++++++++++++++++++++++++++++++++++++++-------------
> > 2 files changed, 88 insertions(+), 29 deletions(-)
> >
> > diff --git a/checksum.c b/checksum.c
> > index 68ffaddb5bb0..4854c1937c39 100644
> > --- a/checksum.c
> > +++ b/checksum.c
> > @@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
> > *
> > * Return: 16-bit folded, complemented checksum
> > */
> > -/* cppcheck-suppress unusedFunction */
> > uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
> > uint32_t init)
> > {
> > diff --git a/tcp.c b/tcp.c
> > index c9472d905520..f0a6f7a507a7 100644
> > --- a/tcp.c
> > +++ b/tcp.c
> > @@ -755,36 +755,81 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
> > }
> >
> > /**
> > - * tcp_update_check_tcp4() - Update TCP checksum from stored one
> > - * @iph: IPv4 header
> > - * @bp: TCP header followed by TCP payload
> > - */
> > -static void tcp_update_check_tcp4(const struct iphdr *iph,
> > - struct tcp_payload_t *bp)
> > + * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6
> > + * @src: IPv4 source address
> > + * @dst: IPv4 destination address
> > + * @iov: Pointer to the array of IO vectors
> > + * @iov_cnt: Length of the array
> > + * @l4offset: IPv4 payload offset in the iovec array
> > + */
> > +void tcp_update_check_tcp4(struct in_addr src,
> > + struct in_addr dst,
> > + const struct iovec *iov, int iov_cnt,
> > + size_t l4offset)
> > {
> > - 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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
> > + size_t check_ofs;
> > + __sum16 *check;
> > + int check_idx;
> > + uint32_t sum;
> > +
> > + sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset,
> > + IPPROTO_TCP, src, dst);
> > +
> > + check_idx = iov_skip_bytes(iov, iov_cnt,
> > + l4offset + offsetof(struct tcphdr, check),
> > + &check_ofs);
> > +
> > + if (check_idx >= iov_cnt)
> > + die("TCP4 buffer is too small");
> > + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
> > + die("TCP4 checksum field memory is not contiguous");
>
> I'm not really fond of those die() calls. First off, they should report
> a couple more details (at least check_idx, iov_cnt).
>
> Second, we could fail gracefully (hence, we should) instead of aborting
> the whole thing: those could be err() calls.
It's a question of how plausible a graceful recovery is at this point.
If we hit this, the guest has given us buffers that aren't even 2-byte
aligned. Is it reasonable to keep trying to working with a guest that
does that?
> If we fail to calculate checksums, we can leave them as zero, and the
> receiver will drop those frames anyway, if you don't want to add
> complexity (propagation of return values) for something that should
> never happen.
I think the checksum is (in the general vhost-user case)
uninitialised, not zero, at this point. To set it to zero, we'd still
need to get a usable pointer to it. Not that that really changes what
would happen - 0 has the same chance of being right by accident as an
uninitialised value.
> The rest looks good to me, but I didn't go through David's comments so
> I'll wait for his review before applying.
--
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 --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array
2024-09-26 1:56 ` David Gibson
@ 2024-09-26 3:54 ` Stefano Brivio
0 siblings, 0 replies; 13+ messages in thread
From: Stefano Brivio @ 2024-09-26 3:54 UTC (permalink / raw)
To: David Gibson; +Cc: Laurent Vivier, passt-dev
On Thu, 26 Sep 2024 11:56:49 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:
> On Wed, Sep 25, 2024 at 07:39:19PM +0200, Stefano Brivio wrote:
> > On Wed, 25 Sep 2024 10:11:25 +0200
> > Laurent Vivier <lvivier@redhat.com> wrote:
> >
> > > TCP header and payload are supposed to be in the same buffer,
> > > and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute
> > > the checksum from the base address of the header using the
> > > length of the IP payload.
> > >
> > > In the future (for vhost-user) we need to dispatch the TCP header and
> > > the TCP payload through several buffers. To be able to manage that, we
> > > provide an iovec array that points to the data of the TCP frame.
> > > We provide also an offset to be able to provide an array that contains
> > > the TCP frame embedded in an lower level frame, and this offset points
> > > to the TCP header inside the iovec array.
> > >
> > > Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> > > ---
> > >
> > > Notes:
> > > v2:
> > > - s/payload_offset/l4offset/
> > > - check memory address of the checksum (alignment, iovec boundaries)
> > >
> > > checksum.c | 1 -
> > > tcp.c | 116 ++++++++++++++++++++++++++++++++++++++++-------------
> > > 2 files changed, 88 insertions(+), 29 deletions(-)
> > >
> > > diff --git a/checksum.c b/checksum.c
> > > index 68ffaddb5bb0..4854c1937c39 100644
> > > --- a/checksum.c
> > > +++ b/checksum.c
> > > @@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
> > > *
> > > * Return: 16-bit folded, complemented checksum
> > > */
> > > -/* cppcheck-suppress unusedFunction */
> > > uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
> > > uint32_t init)
> > > {
> > > diff --git a/tcp.c b/tcp.c
> > > index c9472d905520..f0a6f7a507a7 100644
> > > --- a/tcp.c
> > > +++ b/tcp.c
> > > @@ -755,36 +755,81 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
> > > }
> > >
> > > /**
> > > - * tcp_update_check_tcp4() - Update TCP checksum from stored one
> > > - * @iph: IPv4 header
> > > - * @bp: TCP header followed by TCP payload
> > > - */
> > > -static void tcp_update_check_tcp4(const struct iphdr *iph,
> > > - struct tcp_payload_t *bp)
> > > + * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6
> > > + * @src: IPv4 source address
> > > + * @dst: IPv4 destination address
> > > + * @iov: Pointer to the array of IO vectors
> > > + * @iov_cnt: Length of the array
> > > + * @l4offset: IPv4 payload offset in the iovec array
> > > + */
> > > +void tcp_update_check_tcp4(struct in_addr src,
> > > + struct in_addr dst,
> > > + const struct iovec *iov, int iov_cnt,
> > > + size_t l4offset)
> > > {
> > > - 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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
> > > + size_t check_ofs;
> > > + __sum16 *check;
> > > + int check_idx;
> > > + uint32_t sum;
> > > +
> > > + sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset,
> > > + IPPROTO_TCP, src, dst);
> > > +
> > > + check_idx = iov_skip_bytes(iov, iov_cnt,
> > > + l4offset + offsetof(struct tcphdr, check),
> > > + &check_ofs);
> > > +
> > > + if (check_idx >= iov_cnt)
> > > + die("TCP4 buffer is too small");
> > > + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
> > > + die("TCP4 checksum field memory is not contiguous");
> >
> > I'm not really fond of those die() calls. First off, they should report
> > a couple more details (at least check_idx, iov_cnt).
> >
> > Second, we could fail gracefully (hence, we should) instead of aborting
> > the whole thing: those could be err() calls.
>
> It's a question of how plausible a graceful recovery is at this point.
> If we hit this, the guest has given us buffers that aren't even 2-byte
> aligned. Is it reasonable to keep trying to working with a guest that
> does that?
Could it happen for a single buffer, if the hypervisor has some issue?
Or, say, for every second buffer? If that's the case, then we could
kind of work with it.
Looking at it again, true, that would mean there's some fundamental
issue with it and it doesn't make a lot of sense to try to recover, I
still think we probably should but it's not a strong preference.
> > If we fail to calculate checksums, we can leave them as zero, and the
> > receiver will drop those frames anyway, if you don't want to add
> > complexity (propagation of return values) for something that should
> > never happen.
>
> I think the checksum is (in the general vhost-user case)
> uninitialised, not zero, at this point. To set it to zero, we'd still
> need to get a usable pointer to it. Not that that really changes what
> would happen - 0 has the same chance of being right by accident as an
> uninitialised value.
Ah, right. Well, we could just leave it uninitialised then.
--
Stefano
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array
2024-09-26 1:45 ` David Gibson
@ 2024-09-27 13:49 ` Laurent Vivier
2024-09-30 2:59 ` David Gibson
0 siblings, 1 reply; 13+ messages in thread
From: Laurent Vivier @ 2024-09-27 13:49 UTC (permalink / raw)
To: David Gibson; +Cc: passt-dev
On 26/09/2024 03:45, David Gibson wrote:
> On Wed, Sep 25, 2024 at 10:11:25AM +0200, Laurent Vivier wrote:
>> TCP header and payload are supposed to be in the same buffer,
>> and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute
>> the checksum from the base address of the header using the
>> length of the IP payload.
>>
>> In the future (for vhost-user) we need to dispatch the TCP header and
>> the TCP payload through several buffers. To be able to manage that, we
>> provide an iovec array that points to the data of the TCP frame.
>> We provide also an offset to be able to provide an array that contains
>> the TCP frame embedded in an lower level frame, and this offset points
>> to the TCP header inside the iovec array.
>>
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> ---
>>
>> Notes:
>> v2:
>> - s/payload_offset/l4offset/
>> - check memory address of the checksum (alignment, iovec boundaries)
>>
>> checksum.c | 1 -
>> tcp.c | 116 ++++++++++++++++++++++++++++++++++++++++-------------
>> 2 files changed, 88 insertions(+), 29 deletions(-)
>>
>> diff --git a/checksum.c b/checksum.c
>> index 68ffaddb5bb0..4854c1937c39 100644
>> --- a/checksum.c
>> +++ b/checksum.c
>> @@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
>> *
>> * Return: 16-bit folded, complemented checksum
>> */
>> -/* cppcheck-suppress unusedFunction */
>> uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
>> uint32_t init)
>> {
>> diff --git a/tcp.c b/tcp.c
>> index c9472d905520..f0a6f7a507a7 100644
>> --- a/tcp.c
>> +++ b/tcp.c
>> @@ -755,36 +755,81 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
>> }
>>
>> /**
>> - * tcp_update_check_tcp4() - Update TCP checksum from stored one
>> - * @iph: IPv4 header
>> - * @bp: TCP header followed by TCP payload
>> - */
>> -static void tcp_update_check_tcp4(const struct iphdr *iph,
>> - struct tcp_payload_t *bp)
>> + * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6
>
> Nit: s/IPv6/IPv4/
>
>> + * @src: IPv4 source address
>> + * @dst: IPv4 destination address
>> + * @iov: Pointer to the array of IO vectors
>> + * @iov_cnt: Length of the array
>> + * @l4offset: IPv4 payload offset in the iovec array
>> + */
>> +void tcp_update_check_tcp4(struct in_addr src,
>> + struct in_addr dst,
>> + const struct iovec *iov, int iov_cnt,
>> + size_t l4offset)
>> {
>> - 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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
>> + size_t check_ofs;
>> + __sum16 *check;
>> + int check_idx;
>> + uint32_t sum;
>> +
>> + sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset,
>> + IPPROTO_TCP, src, dst);
>
> Previously, we took the size from the IP header, which we'd previously
> calculated. It seems a shame to replace that with a call to
> iov_size() which will make another pass through the whole vector.
>
>> +
>> + check_idx = iov_skip_bytes(iov, iov_cnt,
>> + l4offset + offsetof(struct tcphdr, check),
>> + &check_ofs);
>> +
>> + if (check_idx >= iov_cnt)
>> + die("TCP4 buffer is too small");
>> + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
>> + die("TCP4 checksum field memory is not contiguous");
>> +
>> + check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs);
>
> Strictly speaking, it's UB to even *create* an improperly aligned
> pointer, even if you never dereference it. So the alignment check
> should go before casting to (__sum16 *).
>
>> - bp->th.check = 0;
>> - bp->th.check = csum(bp, l4len, sum);
>> + if ((uintptr_t)check & (__alignof__(*check) - 1))
>> + die("TCP4 checksum field is not correctly aligned in memory");
>
> I really think it would be worth packaging this logic (skip_bytes +
> contiguous check + alignment check + pointer cast) into another helper
> (iov_field()?). I strongly suspect we'll have further use for it down
> the line.
>
I'm addressing all your other comments but I don't really have the time to write a generic
and clean function to do that. I prefer to duplicate the code for the moment, we will be
able to cleanup this in the future.
Thanks,
Laurent
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array
2024-09-27 13:49 ` Laurent Vivier
@ 2024-09-30 2:59 ` David Gibson
0 siblings, 0 replies; 13+ messages in thread
From: David Gibson @ 2024-09-30 2:59 UTC (permalink / raw)
To: Laurent Vivier; +Cc: passt-dev
[-- Attachment #1: Type: text/plain, Size: 5122 bytes --]
On Fri, Sep 27, 2024 at 03:49:50PM +0200, Laurent Vivier wrote:
> On 26/09/2024 03:45, David Gibson wrote:
> > On Wed, Sep 25, 2024 at 10:11:25AM +0200, Laurent Vivier wrote:
> > > TCP header and payload are supposed to be in the same buffer,
> > > and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute
> > > the checksum from the base address of the header using the
> > > length of the IP payload.
> > >
> > > In the future (for vhost-user) we need to dispatch the TCP header and
> > > the TCP payload through several buffers. To be able to manage that, we
> > > provide an iovec array that points to the data of the TCP frame.
> > > We provide also an offset to be able to provide an array that contains
> > > the TCP frame embedded in an lower level frame, and this offset points
> > > to the TCP header inside the iovec array.
> > >
> > > Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> > > ---
> > >
> > > Notes:
> > > v2:
> > > - s/payload_offset/l4offset/
> > > - check memory address of the checksum (alignment, iovec boundaries)
> > >
> > > checksum.c | 1 -
> > > tcp.c | 116 ++++++++++++++++++++++++++++++++++++++++-------------
> > > 2 files changed, 88 insertions(+), 29 deletions(-)
> > >
> > > diff --git a/checksum.c b/checksum.c
> > > index 68ffaddb5bb0..4854c1937c39 100644
> > > --- a/checksum.c
> > > +++ b/checksum.c
> > > @@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t init)
> > > *
> > > * Return: 16-bit folded, complemented checksum
> > > */
> > > -/* cppcheck-suppress unusedFunction */
> > > uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset,
> > > uint32_t init)
> > > {
> > > diff --git a/tcp.c b/tcp.c
> > > index c9472d905520..f0a6f7a507a7 100644
> > > --- a/tcp.c
> > > +++ b/tcp.c
> > > @@ -755,36 +755,81 @@ static void tcp_sock_set_bufsize(const struct ctx *c, int s)
> > > }
> > > /**
> > > - * tcp_update_check_tcp4() - Update TCP checksum from stored one
> > > - * @iph: IPv4 header
> > > - * @bp: TCP header followed by TCP payload
> > > - */
> > > -static void tcp_update_check_tcp4(const struct iphdr *iph,
> > > - struct tcp_payload_t *bp)
> > > + * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6
> >
> > Nit: s/IPv6/IPv4/
> >
> > > + * @src: IPv4 source address
> > > + * @dst: IPv4 destination address
> > > + * @iov: Pointer to the array of IO vectors
> > > + * @iov_cnt: Length of the array
> > > + * @l4offset: IPv4 payload offset in the iovec array
> > > + */
> > > +void tcp_update_check_tcp4(struct in_addr src,
> > > + struct in_addr dst,
> > > + const struct iovec *iov, int iov_cnt,
> > > + size_t l4offset)
> > > {
> > > - 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 = proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, daddr);
> > > + size_t check_ofs;
> > > + __sum16 *check;
> > > + int check_idx;
> > > + uint32_t sum;
> > > +
> > > + sum = proto_ipv4_header_psum(iov_size(iov, iov_cnt) - l4offset,
> > > + IPPROTO_TCP, src, dst);
> >
> > Previously, we took the size from the IP header, which we'd previously
> > calculated. It seems a shame to replace that with a call to
> > iov_size() which will make another pass through the whole vector.
> >
> > > +
> > > + check_idx = iov_skip_bytes(iov, iov_cnt,
> > > + l4offset + offsetof(struct tcphdr, check),
> > > + &check_ofs);
> > > +
> > > + if (check_idx >= iov_cnt)
> > > + die("TCP4 buffer is too small");
> > > + if (check_ofs + sizeof(*check) > iov[check_idx].iov_len)
> > > + die("TCP4 checksum field memory is not contiguous");
> > > +
> > > + check = (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs);
> >
> > Strictly speaking, it's UB to even *create* an improperly aligned
> > pointer, even if you never dereference it. So the alignment check
> > should go before casting to (__sum16 *).
> >
> > > - bp->th.check = 0;
> > > - bp->th.check = csum(bp, l4len, sum);
> > > + if ((uintptr_t)check & (__alignof__(*check) - 1))
> > > + die("TCP4 checksum field is not correctly aligned in memory");
> >
> > I really think it would be worth packaging this logic (skip_bytes +
> > contiguous check + alignment check + pointer cast) into another helper
> > (iov_field()?). I strongly suspect we'll have further use for it down
> > the line.
> >
>
> I'm addressing all your other comments but I don't really have the time to
> write a generic and clean function to do that. I prefer to duplicate the
> code for the moment, we will be able to cleanup this in the future.
Ok, fair enough. If I get a chance, I might take a crack at writing a
suitable helper.
--
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 --]
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2024-09-30 4:24 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-25 8:11 [PATCH v2 0/4] tcp: use csum_iov() in tcp_update_check_tcp[4|6]() Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 1/4] tcp: Use tcp_payload_t rather than tcphdr Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 2/4] pcap: Add an offset argument in pcap_iov() Laurent Vivier
2024-09-25 8:11 ` [PATCH v2 3/4] checksum: Add an offset argument in csum_iov() Laurent Vivier
2024-09-25 17:39 ` Stefano Brivio
2024-09-26 1:31 ` David Gibson
2024-09-25 8:11 ` [PATCH v2 4/4] tcp: Update TCP checksum using an iovec array Laurent Vivier
2024-09-25 17:39 ` Stefano Brivio
2024-09-26 1:56 ` David Gibson
2024-09-26 3:54 ` Stefano Brivio
2024-09-26 1:45 ` David Gibson
2024-09-27 13:49 ` Laurent Vivier
2024-09-30 2:59 ` David Gibson
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).