git: 126a30da9cff - stable/12 - ena: properly handle IPv6 L4 checksum offload

From: Marcin Wojtas <mw_at_FreeBSD.org>
Date: Thu, 24 Feb 2022 13:04:25 UTC
The branch stable/12 has been updated by mw:

URL: https://cgit.FreeBSD.org/src/commit/?id=126a30da9cfffb5c8df029297a0219bf56b6fb53

commit 126a30da9cfffb5c8df029297a0219bf56b6fb53
Author:     Dawid Gorecki <dgr@semihalf.com>
AuthorDate: 2022-01-03 13:49:58 +0000
Commit:     Marcin Wojtas <mw@FreeBSD.org>
CommitDate: 2022-02-24 13:04:04 +0000

    ena: properly handle IPv6 L4 checksum offload
    
    ena_tx_csum function did not check if IPv6 checksum offload was
    requested it only checked checksum offloading flags for IPv4 packets.
    Because of that, when encountering CSUM_IP6_* flags, the function simply
    returned without actually setting checksum offloading in ena_ctx.
    Check CUSM_IP6_* flags to enable IPv6 checksum offload.
    
    Additionally, only IPv4 header was being parsed regardless of EtherType
    field, because of that, value of L4 protocol read when actually trying
    to send IPv6 packets was wrong. Use ip6_lasthdr function to get length
    of all IPv6 headers and payload protocol.
    
    Set the DF flag to 1 in order to allow the device to offload the IPv6
    checksum calculation and achieve optimal performance.
    
    Add CSUM6_OFFLOAD and CSUM_OFFLOAD definitions into ena_datapath.h.
    
    Submitted by: Dawid Gorecki <dgr@semihalf.com>
    Obtained from: Semihalf
    MFC after: 2 weeks
    Sponsored by: Amazon, Inc.
    
    (cherry picked from commit 2bbef9d95dc10a69a3c5813a517f6e8fe583539a)
---
 sys/dev/ena/ena_datapath.c | 44 ++++++++++++++++++++++++++++----------------
 sys/dev/ena/ena_datapath.h |  1 +
 2 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/sys/dev/ena/ena_datapath.c b/sys/dev/ena/ena_datapath.c
index 0a7dc51b1cdc..023a0d54693e 100644
--- a/sys/dev/ena/ena_datapath.c
+++ b/sys/dev/ena/ena_datapath.c
@@ -41,6 +41,8 @@ __FBSDID("$FreeBSD$");
 #include <net/rss_config.h>
 #endif /* RSS */
 
+#include <netinet6/ip6_var.h>
+
 /*********************************************************************
  *  Static functions prototypes
  *********************************************************************/
@@ -713,6 +715,7 @@ ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf,
 	uint16_t etype;
 	int ehdrlen;
 	struct ip *ip;
+	int ipproto;
 	int iphlen;
 	struct tcphdr *th;
 	int offset;
@@ -730,6 +733,9 @@ ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf,
 	if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
 		offload = true;
 
+	if ((mbuf->m_pkthdr.csum_flags & CSUM6_OFFLOAD) != 0)
+		offload = true;
+
 	if (!offload) {
 		if (disable_meta_caching) {
 			memset(ena_meta, 0, sizeof(*ena_meta));
@@ -751,41 +757,47 @@ ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf,
 	}
 
 	mbuf_next = m_getptr(mbuf, ehdrlen, &offset);
-	ip = (struct ip *)(mtodo(mbuf_next, offset));
-	iphlen = ip->ip_hl << 2;
-
-	mbuf_next = m_getptr(mbuf, iphlen + ehdrlen, &offset);
-	th = (struct tcphdr *)(mtodo(mbuf_next, offset));
-
-	if ((mbuf->m_pkthdr.csum_flags & CSUM_IP) != 0) {
-		ena_tx_ctx->l3_csum_enable = 1;
-	}
-	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
-		ena_tx_ctx->tso_enable = 1;
-		ena_meta->l4_hdr_len = (th->th_off);
-	}
 
 	switch (etype) {
 	case ETHERTYPE_IP:
+		ip = (struct ip *)(mtodo(mbuf_next, offset));
+		iphlen = ip->ip_hl << 2;
+		ipproto = ip->ip_p;
 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
 		if ((ip->ip_off & htons(IP_DF)) != 0)
 			ena_tx_ctx->df = 1;
 		break;
 	case ETHERTYPE_IPV6:
 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
-
+		iphlen = ip6_lasthdr(mbuf, ehdrlen, IPPROTO_IPV6, &ipproto);
+		iphlen -= ehdrlen;
+		ena_tx_ctx->df = 1;
+		break;
 	default:
+		iphlen = 0;
+		ipproto = 0;
 		break;
 	}
 
-	if (ip->ip_p == IPPROTO_TCP) {
+	mbuf_next = m_getptr(mbuf, iphlen + ehdrlen, &offset);
+	th = (struct tcphdr *)(mtodo(mbuf_next, offset));
+
+	if ((mbuf->m_pkthdr.csum_flags & CSUM_IP) != 0) {
+		ena_tx_ctx->l3_csum_enable = 1;
+	}
+	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
+		ena_tx_ctx->tso_enable = 1;
+		ena_meta->l4_hdr_len = (th->th_off);
+	}
+
+	if (ipproto == IPPROTO_TCP) {
 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
 		if ((mbuf->m_pkthdr.csum_flags &
 		    (CSUM_IP_TCP | CSUM_IP6_TCP)) != 0)
 			ena_tx_ctx->l4_csum_enable = 1;
 		else
 			ena_tx_ctx->l4_csum_enable = 0;
-	} else if (ip->ip_p == IPPROTO_UDP) {
+	} else if (ipproto == IPPROTO_UDP) {
 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
 		if ((mbuf->m_pkthdr.csum_flags &
 		    (CSUM_IP_UDP | CSUM_IP6_UDP)) != 0)
diff --git a/sys/dev/ena/ena_datapath.h b/sys/dev/ena/ena_datapath.h
index 8da6a2a0edc9..f3b721359c72 100644
--- a/sys/dev/ena/ena_datapath.h
+++ b/sys/dev/ena/ena_datapath.h
@@ -40,5 +40,6 @@ int	ena_mq_start(if_t ifp, struct mbuf *m);
 void	ena_deferred_mq_start(void *arg, int pending);
 
 #define CSUM_OFFLOAD 	(CSUM_IP|CSUM_TCP|CSUM_UDP)
+#define CSUM6_OFFLOAD	(CSUM_IP6_UDP|CSUM_IP6_TCP)
 
 #endif /* ENA_TXRX_H */