git: 3c5bf8d9d935 - stable/14 - mlx5: recalculate tcp checksum for ipv4 hw lro coalesced packet

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Thu, 20 Mar 2025 03:14:06 UTC
The branch stable/14 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=3c5bf8d9d9352d38393aac222451f638d6791499

commit 3c5bf8d9d9352d38393aac222451f638d6791499
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2025-02-08 07:21:55 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2025-03-20 03:13:37 +0000

    mlx5: recalculate tcp checksum for ipv4 hw lro coalesced packet
    
    (cherry picked from commit 3eb6d4b4a25a13b9ea9d825ee44c641b91669d06)
---
 sys/dev/mlx5/mlx5_en/mlx5_en_rx.c | 42 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/sys/dev/mlx5/mlx5_en/mlx5_en_rx.c b/sys/dev/mlx5/mlx5_en/mlx5_en_rx.c
index 73ac24f55f45..266e8e804259 100644
--- a/sys/dev/mlx5/mlx5_en/mlx5_en_rx.c
+++ b/sys/dev/mlx5/mlx5_en/mlx5_en_rx.c
@@ -27,6 +27,7 @@
 #include "opt_ratelimit.h"
 
 #include <dev/mlx5/mlx5_en/en.h>
+#include <netinet/ip_var.h>
 #include <machine/in_cksum.h>
 
 static inline int
@@ -124,6 +125,27 @@ mlx5e_post_rx_wqes(struct mlx5e_rq *rq)
 	mlx5_wq_ll_update_db_record(&rq->wq);
 }
 
+static uint32_t
+csum_reduce(uint32_t val)
+{
+	while (val > 0xffff)
+		val = (val >> 16) + (val & 0xffff);
+	return (val);
+}
+
+static u_short
+csum_buf(uint32_t val, void *buf, int len)
+{
+	u_short x;
+
+	MPASS(len % 2 == 0);
+	for (int i = 0; i < len; i += 2) {
+		bcopy((char *)buf + i, &x, 2);
+		val = csum_reduce(val + x);
+	}
+	return (val);
+}
+
 static void
 mlx5e_lro_update_hdr(struct mbuf *mb, struct mlx5_cqe64 *cqe)
 {
@@ -135,6 +157,7 @@ mlx5e_lro_update_hdr(struct mbuf *mb, struct mlx5_cqe64 *cqe)
 	struct ip *ip4 = NULL;
 	struct tcphdr *th;
 	uint32_t *ts_ptr;
+	uint32_t tcp_csum;
 	uint8_t l4_hdr_type;
 	int tcp_ack;
 
@@ -196,17 +219,34 @@ mlx5e_lro_update_hdr(struct mbuf *mb, struct mlx5_cqe64 *cqe)
 		}
 	}
 	if (ip4) {
+		struct ipovly io;
+
 		ip4->ip_ttl = cqe->lro_min_ttl;
 		ip4->ip_len = cpu_to_be16(tot_len);
 		ip4->ip_sum = 0;
 		ip4->ip_sum = in_cksum_skip(mb, (ip4->ip_hl << 2) +
 		    ETHER_HDR_LEN, ETHER_HDR_LEN);
+
+		/* TCP checksum: data */
+		tcp_csum = cqe->check_sum;
+
+		/* TCP checksum: IP pseudoheader */
+		bzero(io.ih_x1, sizeof(io.ih_x1));
+		io.ih_pr = IPPROTO_TCP;
+		io.ih_len = htons(ntohs(ip4->ip_len) - sizeof(*ip4));
+		io.ih_src = ip4->ip_src;
+		io.ih_dst = ip4->ip_dst;
+		tcp_csum = csum_buf(tcp_csum, &io, sizeof(io));
+
+		/* TCP checksum: TCP header */
+		th->th_sum = 0;
+		tcp_csum = csum_buf(tcp_csum, th, th->th_off * 4);
+		th->th_sum = ~tcp_csum & 0xffff;
 	} else {
 		ip6->ip6_hlim = cqe->lro_min_ttl;
 		ip6->ip6_plen = cpu_to_be16(tot_len -
 		    sizeof(struct ip6_hdr));
 	}
-	/* TODO: handle tcp checksum */
 }
 
 static uint64_t