git: 9ca874cf740e - main - Add TCP LRO support for VLAN and VxLAN.

Hans Petter Selasky hselasky at FreeBSD.org
Tue Apr 20 11:41:41 UTC 2021


The branch main has been updated by hselasky:

URL: https://cgit.FreeBSD.org/src/commit/?id=9ca874cf740ee68c5742df8b5f9e20910085c011

commit 9ca874cf740ee68c5742df8b5f9e20910085c011
Author:     Hans Petter Selasky <hselasky at FreeBSD.org>
AuthorDate: 2021-03-31 10:36:36 +0000
Commit:     Hans Petter Selasky <hselasky at FreeBSD.org>
CommitDate: 2021-04-20 11:36:22 +0000

    Add TCP LRO support for VLAN and VxLAN.
    
    This change makes the TCP LRO code more generic and flexible with regards
    to supporting multiple different TCP encapsulation protocols and in general
    lays the ground for broader TCP LRO support. The main job of the TCP LRO code is
    to merge TCP packets for the same flow, to reduce the number of calls to upper
    layers. This reduces CPU and increases performance, due to being able to send
    larger TSO offloaded data chunks at a time. Basically the TCP LRO makes it
    possible to avoid per-packet interaction by the host CPU.
    
    Because the current TCP LRO code was tightly bound and optimized for TCP/IP
    over ethernet only, several larger changes were needed. Also a minor bug was
    fixed in the flushing mechanism for inactive entries, where the expire time,
    "le->mtime" was not always properly set.
    
    To avoid having to re-run time consuming regression tests for every change,
    it was chosen to squash the following list of changes into a single commit:
    - Refactor parsing of all address information into the "lro_parser" structure.
      This easily allows to reuse parsing code for inner headers.
    - Speedup header data comparison. Don't compare field by field, but
      instead use an unsigned long array, where the fields get packed.
    - Refactor the IPv4/TCP/UDP checksum computations, so that they may be computed
      recursivly, only applying deltas as the result of updating payload data.
    - Make smaller inline functions doing one operation at a time instead of
      big functions having repeated code.
    - Refactor the TCP ACK compression code to only execute once
      per TCP LRO flush. This gives a minor performance improvement and
      keeps the code simple.
    - Use sbintime() for all time-keeping. This change also fixes flushing
      of inactive entries.
    - Try to shrink the size of the LRO entry, because it is frequently zeroed.
    - Removed unused TCP LRO macros.
    - Cleanup unused TCP LRO statistics counters while at it.
    - Try to use __predict_true() and predict_false() to optimise CPU branch
      predictions.
    
    Bump the __FreeBSD_version due to changing the "lro_ctrl" structure.
    
    Tested by:      Netflix
    Reviewed by:    rrs (transport)
    Differential Revision:  https://reviews.freebsd.org/D29564
    MFC after:      2 week
    Sponsored by:   Mellanox Technologies // NVIDIA Networking
---
 sys/netinet/tcp_lro.c  | 2282 ++++++++++++++++++++++--------------------------
 sys/netinet/tcp_lro.h  |  124 +--
 sys/netinet/tcp_subr.c |    5 -
 sys/netinet/tcp_var.h  |    5 -
 sys/sys/mbuf.h         |    7 +-
 sys/sys/param.h        |    2 +-
 6 files changed, 1107 insertions(+), 1318 deletions(-)

diff --git a/sys/netinet/tcp_lro.c b/sys/netinet/tcp_lro.c
index 62f6ad57c0f5..61c6f218e513 100644
--- a/sys/netinet/tcp_lro.c
+++ b/sys/netinet/tcp_lro.c
@@ -4,7 +4,7 @@
  * Copyright (c) 2007, Myricom Inc.
  * Copyright (c) 2008, Intel Corporation.
  * Copyright (c) 2012 The FreeBSD Foundation
- * Copyright (c) 2016 Mellanox Technologies.
+ * Copyright (c) 2016-2021 Mellanox Technologies.
  * All rights reserved.
  *
  * Portions of this software were developed by Bjoern Zeeb
@@ -68,39 +68,39 @@ __FBSDID("$FreeBSD$");
 #include <netinet/tcpip.h>
 #include <netinet/tcp_hpts.h>
 #include <netinet/tcp_log_buf.h>
+#include <netinet/udp.h>
 #include <netinet6/ip6_var.h>
 
 #include <machine/in_cksum.h>
 
 static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
 
-#define	TCP_LRO_UPDATE_CSUM	1
-#ifndef	TCP_LRO_UPDATE_CSUM
-#define	TCP_LRO_INVALID_CSUM	0x0000
-#endif
+#define	TCP_LRO_TS_OPTION \
+    ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
+	  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
 
 static void	tcp_lro_rx_done(struct lro_ctrl *lc);
-static int	tcp_lro_rx2(struct lro_ctrl *lc, struct mbuf *m,
-		    uint32_t csum, int use_hash);
+static int	tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m,
+		    uint32_t csum, bool use_hash);
+
+#ifdef TCPHPTS
+static bool	do_bpf_strip_and_compress(struct inpcb *, struct lro_ctrl *,
+		struct lro_entry *, struct mbuf **, struct mbuf **, struct mbuf **, bool *, bool);
+
+#endif
 
 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro,  CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
     "TCP LRO");
 
-static long tcplro_stacks_wanting_mbufq = 0;
+static long tcplro_stacks_wanting_mbufq;
 counter_u64_t tcp_inp_lro_direct_queue;
 counter_u64_t tcp_inp_lro_wokeup_queue;
 counter_u64_t tcp_inp_lro_compressed;
-counter_u64_t tcp_inp_lro_single_push;
 counter_u64_t tcp_inp_lro_locks_taken;
-counter_u64_t tcp_inp_lro_sack_wake;
 counter_u64_t tcp_extra_mbuf;
 counter_u64_t tcp_would_have_but;
 counter_u64_t tcp_comp_total;
 counter_u64_t tcp_uncomp_total;
-counter_u64_t tcp_csum_hardware;
-counter_u64_t tcp_csum_hardware_w_ph;
-counter_u64_t tcp_csum_software;
-
 
 static unsigned	tcp_lro_entries = TCP_LRO_ENTRIES;
 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
@@ -113,28 +113,16 @@ SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD,
     &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts");
 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD,
     &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport");
-SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, single, CTLFLAG_RD,
-    &tcp_inp_lro_single_push, "Number of lro's sent with single segment");
 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
     &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
-SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, sackwakeups, CTLFLAG_RD,
-    &tcp_inp_lro_sack_wake, "Number of wakeups caused by sack/fin");
 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD,
     &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp");
 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD,
-    &tcp_would_have_but, "Number of times we would have had an extra compressed but out of room");
+    &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed");
 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD,
     &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set");
 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD,
     &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP");
-SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, csum_hw, CTLFLAG_RD,
-    &tcp_csum_hardware, "Number of checksums processed in hardware");
-SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, csum_hw_ph, CTLFLAG_RD,
-    &tcp_csum_hardware_w_ph, "Number of checksums processed in hardware with pseudo header");
-SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, csum_sw, CTLFLAG_RD,
-    &tcp_csum_software, "Number of checksums processed in software");
-
-
 
 void
 tcp_lro_reg_mbufq(void)
@@ -226,34 +214,230 @@ tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
 	return (0);
 }
 
-static struct tcphdr *
-tcp_lro_get_th(struct lro_entry *le, struct mbuf *m)
+struct vxlan_header {
+	uint32_t	vxlh_flags;
+	uint32_t	vxlh_vni;
+};
+
+static inline void *
+tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan)
 {
-	struct ether_header *eh;
-	struct tcphdr *th = NULL;
-#ifdef INET6
-	struct ip6_hdr *ip6 = NULL;	/* Keep compiler happy. */
-#endif
+	const struct ether_vlan_header *eh;
+	void *old;
+	uint16_t eth_type;
+
+	if (update_data)
+		memset(parser, 0, sizeof(*parser));
+
+	old = ptr;
+
+	if (is_vxlan) {
+		const struct vxlan_header *vxh;
+		vxh = ptr;
+		ptr = (uint8_t *)ptr + sizeof(*vxh);
+		if (update_data) {
+			parser->data.vxlan_vni =
+			    vxh->vxlh_vni & htonl(0xffffff00);
+		}
+	}
+
+	eh = ptr;
+	if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) {
+		eth_type = eh->evl_proto;
+		if (update_data) {
+			/* strip priority and keep VLAN ID only */
+			parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK);
+		}
+		/* advance to next header */
+		ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
+	} else {
+		eth_type = eh->evl_encap_proto;
+		/* advance to next header */
+		ptr = (uint8_t *)ptr + ETHER_HDR_LEN;
+	}
+
+	switch (eth_type) {
 #ifdef INET
-	struct ip *ip4 = NULL;		/* Keep compiler happy. */
+	case htons(ETHERTYPE_IP):
+		parser->ip4 = ptr;
+		/* Ensure there are no IPv4 options. */
+		if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4))
+			break;
+		/* .. and the packet is not fragmented. */
+		if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK))
+			break;
+		ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2);
+		if (update_data) {
+			parser->data.s_addr.v4 = parser->ip4->ip_src;
+			parser->data.d_addr.v4 = parser->ip4->ip_dst;
+		}
+		switch (parser->ip4->ip_p) {
+		case IPPROTO_UDP:
+			parser->udp = ptr;
+			if (update_data) {
+				parser->data.lro_type = LRO_TYPE_IPV4_UDP;
+				parser->data.s_port = parser->udp->uh_sport;
+				parser->data.d_port = parser->udp->uh_dport;
+			} else {
+				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP);
+			}
+			ptr = ((uint8_t *)ptr + sizeof(*parser->udp));
+			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
+			return (ptr);
+		case IPPROTO_TCP:
+			parser->tcp = ptr;
+			if (update_data) {
+				parser->data.lro_type = LRO_TYPE_IPV4_TCP;
+				parser->data.s_port = parser->tcp->th_sport;
+				parser->data.d_port = parser->tcp->th_dport;
+			} else {
+				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP);
+			}
+			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
+			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
+			return (ptr);
+		default:
+			break;
+		}
+		break;
 #endif
-
-	eh = mtod(m, struct ether_header *);
-	switch (le->eh_type) {
 #ifdef INET6
-	case ETHERTYPE_IPV6:
-		ip6 = (struct ip6_hdr *)(eh + 1);
-		th = (struct tcphdr *)(ip6 + 1);
+	case htons(ETHERTYPE_IPV6):
+		parser->ip6 = ptr;
+		ptr = (uint8_t *)ptr + sizeof(*parser->ip6);
+		if (update_data) {
+			parser->data.s_addr.v6 = parser->ip6->ip6_src;
+			parser->data.d_addr.v6 = parser->ip6->ip6_dst;
+		}
+		switch (parser->ip6->ip6_nxt) {
+		case IPPROTO_UDP:
+			parser->udp = ptr;
+			if (update_data) {
+				parser->data.lro_type = LRO_TYPE_IPV6_UDP;
+				parser->data.s_port = parser->udp->uh_sport;
+				parser->data.d_port = parser->udp->uh_dport;
+			} else {
+				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP);
+			}
+			ptr = (uint8_t *)ptr + sizeof(*parser->udp);
+			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
+			return (ptr);
+		case IPPROTO_TCP:
+			parser->tcp = ptr;
+			if (update_data) {
+				parser->data.lro_type = LRO_TYPE_IPV6_TCP;
+				parser->data.s_port = parser->tcp->th_sport;
+				parser->data.d_port = parser->tcp->th_dport;
+			} else {
+				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP);
+			}
+			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
+			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
+			return (ptr);
+		default:
+			break;
+		}
 		break;
 #endif
+	default:
+		break;
+	}
+	/* Invalid packet - cannot parse */
+	return (NULL);
+}
+
+static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
+    CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID;
+
+static inline struct lro_parser *
+tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data)
+{
+	void *data_ptr;
+
+	/* Try to parse outer headers first. */
+	data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false);
+	if (data_ptr == NULL || po->total_hdr_len > m->m_len)
+		return (NULL);
+
+	if (update_data) {
+		/* Store VLAN ID, if any. */
+		if (__predict_false(m->m_flags & M_VLANTAG)) {
+			po->data.vlan_id =
+			    htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK);
+		}
+	}
+
+	switch (po->data.lro_type) {
+	case LRO_TYPE_IPV4_UDP:
+	case LRO_TYPE_IPV6_UDP:
+		/* Check for VXLAN headers. */
+		if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum)
+			break;
+
+		/* Try to parse inner headers. */
+		data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true);
+		if (data_ptr == NULL || pi->total_hdr_len > m->m_len)
+			break;
+
+		/* Verify supported header types. */
+		switch (pi->data.lro_type) {
+		case LRO_TYPE_IPV4_TCP:
+		case LRO_TYPE_IPV6_TCP:
+			return (pi);
+		default:
+			break;
+		}
+		break;
+	case LRO_TYPE_IPV4_TCP:
+	case LRO_TYPE_IPV6_TCP:
+		if (update_data)
+			memset(pi, 0, sizeof(*pi));
+		return (po);
+	default:
+		break;
+	}
+	return (NULL);
+}
+
+static inline int
+tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po)
+{
+	int len;
+
+	switch (po->data.lro_type) {
 #ifdef INET
-	case ETHERTYPE_IP:
-		ip4 = (struct ip *)(eh + 1);
-		th = (struct tcphdr *)(ip4 + 1);
+	case LRO_TYPE_IPV4_TCP:
+		len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) +
+		    ntohs(po->ip4->ip_len);
 		break;
 #endif
+#ifdef INET6
+	case LRO_TYPE_IPV6_TCP:
+		len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) +
+		    ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6);
+		break;
+#endif
+	default:
+		return (TCP_LRO_CANNOT);
+	}
+
+	/*
+	 * If the frame is padded beyond the end of the IP packet,
+	 * then trim the extra bytes off:
+	 */
+	if (__predict_true(m->m_pkthdr.len == len)) {
+		return (0);
+	} else if (m->m_pkthdr.len > len) {
+		m_adj(m, len - m->m_pkthdr.len);
+		return (0);
 	}
-	return (th);
+	return (TCP_LRO_CANNOT);
+}
+
+static struct tcphdr *
+tcp_lro_get_th(struct mbuf *m)
+{
+	return ((struct tcphdr *)((uint8_t *)m->m_data + m->m_pkthdr.lro_tcp_h_off));
 }
 
 static void
@@ -300,86 +484,67 @@ tcp_lro_free(struct lro_ctrl *lc)
 }
 
 static uint16_t
-tcp_lro_csum_th(struct tcphdr *th)
+tcp_lro_rx_csum_tcphdr(const struct tcphdr *th)
 {
-	uint32_t ch;
-	uint16_t *p, l;
-
-	ch = th->th_sum = 0x0000;
-	l = th->th_off;
-	p = (uint16_t *)th;
-	while (l > 0) {
-		ch += *p;
-		p++;
-		ch += *p;
-		p++;
-		l--;
+	const uint16_t *ptr;
+	uint32_t csum;
+	uint16_t len;
+
+	csum = -th->th_sum;	/* exclude checksum field */
+	len = th->th_off;
+	ptr = (const uint16_t *)th;
+	while (len--) {
+		csum += *ptr;
+		ptr++;
+		csum += *ptr;
+		ptr++;
 	}
-	while (ch > 0xffff)
-		ch = (ch >> 16) + (ch & 0xffff);
+	while (csum > 0xffff)
+		csum = (csum >> 16) + (csum & 0xffff);
 
-	return (ch & 0xffff);
+	return (csum);
 }
 
 static uint16_t
-tcp_lro_rx_csum_fixup(struct lro_entry *le, void *l3hdr, struct tcphdr *th,
-    uint16_t tcp_data_len, uint16_t csum)
+tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum)
 {
 	uint32_t c;
 	uint16_t cs;
 
-	c = csum;
+	c = tcp_csum;
 
-	/* Remove length from checksum. */
-	switch (le->eh_type) {
+	switch (pa->data.lro_type) {
 #ifdef INET6
-	case ETHERTYPE_IPV6:
-	{
-		struct ip6_hdr *ip6;
-
-		ip6 = (struct ip6_hdr *)l3hdr;
-		if (le->append_cnt == 0)
-			cs = ip6->ip6_plen;
-		else {
-			uint32_t cx;
-
-			cx = ntohs(ip6->ip6_plen);
-			cs = in6_cksum_pseudo(ip6, cx, ip6->ip6_nxt, 0);
-		}
+	case LRO_TYPE_IPV6_TCP:
+		/* Compute full pseudo IPv6 header checksum. */
+		cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0);
 		break;
-	}
 #endif
 #ifdef INET
-	case ETHERTYPE_IP:
-	{
-		struct ip *ip4;
-
-		ip4 = (struct ip *)l3hdr;
-		if (le->append_cnt == 0)
-			cs = ip4->ip_len;
-		else {
-			cs = in_addword(ntohs(ip4->ip_len) - sizeof(*ip4),
-			    IPPROTO_TCP);
-			cs = in_pseudo(ip4->ip_src.s_addr, ip4->ip_dst.s_addr,
-			    htons(cs));
-		}
+	case LRO_TYPE_IPV4_TCP:
+		/* Compute full pseudo IPv4 header checsum. */
+		cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP);
+		cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs));
 		break;
-	}
 #endif
 	default:
 		cs = 0;		/* Keep compiler happy. */
+		break;
 	}
 
+	/* Complement checksum. */
 	cs = ~cs;
 	c += cs;
 
-	/* Remove TCP header csum. */
-	cs = ~tcp_lro_csum_th(th);
+	/* Remove TCP header checksum. */
+	cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp);
 	c += cs;
+
+	/* Compute checksum remainder. */
 	while (c > 0xffff)
 		c = (c >> 16) + (c & 0xffff);
 
-	return (c & 0xffff);
+	return (c);
 }
 
 static void
@@ -397,84 +562,51 @@ void
 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
 {
 	struct lro_entry *le, *le_tmp;
-	struct timeval tv;
+	sbintime_t sbt;
 
 	if (LIST_EMPTY(&lc->lro_active))
 		return;
 
-	getmicrouptime(&tv);
-	timevalsub(&tv, timeout);
+	/* get timeout time */
+	sbt = getsbinuptime() - tvtosbt(*timeout);
+
 	LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
-		if (timevalcmp(&tv, &le->mtime, >=)) {
+		if (sbt >= le->alloc_time) {
 			tcp_lro_active_remove(le);
 			tcp_lro_flush(lc, le);
 		}
 	}
 }
 
-#ifdef INET6
-static int
-tcp_lro_rx_ipv6(struct lro_ctrl *lc, struct mbuf *m, struct ip6_hdr *ip6,
-    struct tcphdr **th)
-{
-
-	/* XXX-BZ we should check the flow-label. */
-
-	/* XXX-BZ We do not yet support ext. hdrs. */
-	if (ip6->ip6_nxt != IPPROTO_TCP)
-		return (TCP_LRO_NOT_SUPPORTED);
-
-	/* Find the TCP header. */
-	*th = (struct tcphdr *)(ip6 + 1);
-
-	return (0);
-}
-#endif
-
 #ifdef INET
 static int
-tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4,
-    struct tcphdr **th)
+tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4)
 {
-	int csum_flags;
 	uint16_t csum;
 
-	if (ip4->ip_p != IPPROTO_TCP)
-		return (TCP_LRO_NOT_SUPPORTED);
-
-	/* Ensure there are no options. */
-	if ((ip4->ip_hl << 2) != sizeof (*ip4))
-		return (TCP_LRO_CANNOT);
-
-	/* .. and the packet is not fragmented. */
-	if (ip4->ip_off & htons(IP_MF|IP_OFFMASK))
-		return (TCP_LRO_CANNOT);
-
 	/* Legacy IP has a header checksum that needs to be correct. */
-	csum_flags = m->m_pkthdr.csum_flags;
-	if (csum_flags & CSUM_IP_CHECKED) {
-		if (__predict_false((csum_flags & CSUM_IP_VALID) == 0)) {
+	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
+		if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) {
 			lc->lro_bad_csum++;
 			return (TCP_LRO_CANNOT);
 		}
 	} else {
 		csum = in_cksum_hdr(ip4);
-		if (__predict_false((csum) != 0)) {
+		if (__predict_false(csum != 0)) {
 			lc->lro_bad_csum++;
 			return (TCP_LRO_CANNOT);
 		}
 	}
-	/* Find the TCP header (we assured there are no IP options). */
-	*th = (struct tcphdr *)(ip4 + 1);
 	return (0);
 }
 #endif
 
 #ifdef TCPHPTS
 static void
-tcp_lro_log(struct tcpcb *tp, struct lro_ctrl *lc,
-	    struct lro_entry *le, struct mbuf *m, int frm, int32_t tcp_data_len,
-	    uint32_t th_seq , uint32_t th_ack, uint16_t th_win)
+tcp_lro_log(struct tcpcb *tp, const struct lro_ctrl *lc,
+    const struct lro_entry *le, const struct mbuf *m,
+    int frm, int32_t tcp_data_len, uint32_t th_seq,
+    uint32_t th_ack, uint16_t th_win)
 {
 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
 		union tcp_log_stackspecific log;
@@ -489,8 +621,8 @@ tcp_lro_log(struct tcpcb *tp, struct lro_ctrl *lc,
 			log.u_bbr.flex2 = m->m_pkthdr.len;
 		else
 			log.u_bbr.flex2 = 0;
-		log.u_bbr.flex3 = le->append_cnt;
-		log.u_bbr.flex4 = le->p_len;
+		log.u_bbr.flex3 = le->m_head->m_pkthdr.lro_nsegs;
+		log.u_bbr.flex4 = le->m_head->m_pkthdr.lro_tcp_d_len;
 		if (le->m_head) {
 			log.u_bbr.flex5 = le->m_head->m_pkthdr.len;
 			log.u_bbr.delRate = le->m_head->m_flags;
@@ -505,11 +637,9 @@ tcp_lro_log(struct tcpcb *tp, struct lro_ctrl *lc,
 		log.u_bbr.cwnd_gain = le->window;
 		log.u_bbr.cur_del_rate = (uintptr_t)m;
 		log.u_bbr.bw_inuse = (uintptr_t)le->m_head;
-		log.u_bbr.pkts_out = le->mbuf_cnt;	/* Total mbufs added */
-		log.u_bbr.applimited = le->ulp_csum;
-		log.u_bbr.lost = le->mbuf_appended;
-		log.u_bbr.pkt_epoch = le->cmp_ack_cnt;
-		log.u_bbr.flex6 = tcp_tv_to_usectick(&lc->lro_last_flush);
+		log.u_bbr.flex6 = sbttous(lc->lro_last_queue_time);
+		log.u_bbr.flex7 = le->compressed;
+		log.u_bbr.pacing_gain = le->uncompressed;
 		if (in_epoch(net_epoch_preempt))
 			log.u_bbr.inhpts = 1;
 		else
@@ -523,198 +653,292 @@ tcp_lro_log(struct tcpcb *tp, struct lro_ctrl *lc,
 }
 #endif
 
-static void
-tcp_flush_out_le(struct tcpcb *tp, struct lro_ctrl *lc, struct lro_entry *le)
+static inline void
+tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum)
 {
-	if (le->append_cnt > 1) {
-		struct tcphdr *th;
-		uint16_t p_len;
+	uint32_t csum;
 
-		p_len = htons(le->p_len);
-		switch (le->eh_type) {
-#ifdef INET6
-		case ETHERTYPE_IPV6:
-		{
-			struct ip6_hdr *ip6;
+	csum = 0xffff - *ptr + value;
+	while (csum > 0xffff)
+		csum = (csum >> 16) + (csum & 0xffff);
+	*ptr = value;
+	*psum = csum;
+}
+
+static uint16_t
+tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le,
+    uint16_t payload_len, uint16_t delta_sum)
+{
+	uint32_t csum;
+	uint16_t tlen;
+	uint16_t temp[5] = {};
+
+	switch (pa->data.lro_type) {
+	case LRO_TYPE_IPV4_TCP:
+		/* Compute new IPv4 length. */
+		tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len;
+		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
+
+		/* Subtract delta from current IPv4 checksum. */
+		csum = pa->ip4->ip_sum + 0xffff - temp[0];
+		while (csum > 0xffff)
+			csum = (csum >> 16) + (csum & 0xffff);
+		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
+		goto update_tcp_header;
+
+	case LRO_TYPE_IPV6_TCP:
+		/* Compute new IPv6 length. */
+		tlen = (pa->tcp->th_off << 2) + payload_len;
+		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
+		goto update_tcp_header;
+
+	case LRO_TYPE_IPV4_UDP:
+		/* Compute new IPv4 length. */
+		tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len;
+		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
+
+		/* Subtract delta from current IPv4 checksum. */
+		csum = pa->ip4->ip_sum + 0xffff - temp[0];
+		while (csum > 0xffff)
+			csum = (csum >> 16) + (csum & 0xffff);
+		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
+		goto update_udp_header;
+
+	case LRO_TYPE_IPV6_UDP:
+		/* Compute new IPv6 length. */
+		tlen = sizeof(*pa->udp) + payload_len;
+		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
+		goto update_udp_header;
+
+	default:
+		return (0);
+	}
+
+update_tcp_header:
+	/* Compute current TCP header checksum. */
+	temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp);
+
+	/* Incorporate the latest ACK into the TCP header. */
+	pa->tcp->th_ack = le->ack_seq;
+	pa->tcp->th_win = le->window;
+
+	/* Incorporate latest timestamp into the TCP header. */
+	if (le->timestamp != 0) {
+		uint32_t *ts_ptr;
+
+		ts_ptr = (uint32_t *)(pa->tcp + 1);
+		ts_ptr[1] = htonl(le->tsval);
+		ts_ptr[2] = le->tsecr;
+	}
+
+	/* Compute new TCP header checksum. */
+	temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp);
+
+	/* Compute new TCP checksum. */
+	csum = pa->tcp->th_sum + 0xffff - delta_sum +
+	    0xffff - temp[0] + 0xffff - temp[3] + temp[2];
+	while (csum > 0xffff)
+		csum = (csum >> 16) + (csum & 0xffff);
+
+	/* Assign new TCP checksum. */
+	tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]);
+
+	/* Compute all modififications affecting next checksum. */
+	csum = temp[0] + temp[1] + 0xffff - temp[2] +
+	    temp[3] + temp[4] + delta_sum;
+	while (csum > 0xffff)
+		csum = (csum >> 16) + (csum & 0xffff);
+
+	/* Return delta checksum to next stage, if any. */
+	return (csum);
+
+update_udp_header:
+	tlen = sizeof(*pa->udp) + payload_len;
+	/* Assign new UDP length and compute checksum delta. */
+	tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]);
+
+	/* Check if there is a UDP checksum. */
+	if (__predict_false(pa->udp->uh_sum != 0)) {
+		/* Compute new UDP checksum. */
+		csum = pa->udp->uh_sum + 0xffff - delta_sum +
+		    0xffff - temp[0] + 0xffff - temp[2];
+		while (csum > 0xffff)
+			csum = (csum >> 16) + (csum & 0xffff);
+		/* Assign new UDP checksum. */
+		tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]);
+	}
 
-			ip6 = le->le_ip6;
-			ip6->ip6_plen = p_len;
-			th = (struct tcphdr *)(ip6 + 1);
+	/* Compute all modififications affecting next checksum. */
+	csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum;
+	while (csum > 0xffff)
+		csum = (csum >> 16) + (csum & 0xffff);
+
+	/* Return delta checksum to next stage, if any. */
+	return (csum);
+}
+
+static void
+tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le)
+{
+	/* Check if we need to recompute any checksums. */
+	if (le->m_head->m_pkthdr.lro_nsegs > 1) {
+		uint16_t csum;
+
+		switch (le->inner.data.lro_type) {
+		case LRO_TYPE_IPV4_TCP:
+			csum = tcp_lro_update_checksum(&le->inner, le,
+			    le->m_head->m_pkthdr.lro_tcp_d_len,
+			    le->m_head->m_pkthdr.lro_tcp_d_csum);
+			csum = tcp_lro_update_checksum(&le->outer, NULL,
+			    le->m_head->m_pkthdr.lro_tcp_d_len +
+			    le->inner.total_hdr_len, csum);
 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
-			    CSUM_PSEUDO_HDR;
-			le->p_len += ETHER_HDR_LEN + sizeof(*ip6);
+			    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
+			le->m_head->m_pkthdr.csum_data = 0xffff;
 			break;
-		}
-#endif
-#ifdef INET
-		case ETHERTYPE_IP:
-		{
-			struct ip *ip4;
-			uint32_t cl;
-			uint16_t c;
-
-			ip4 = le->le_ip4;
-			/* Fix IP header checksum for new length. */
-			c = ~ip4->ip_sum;
-			cl = c;
-			c = ~ip4->ip_len;
-			cl += c + p_len;
-			while (cl > 0xffff)
-				cl = (cl >> 16) + (cl & 0xffff);
-			c = cl;
-			ip4->ip_sum = ~c;
-			ip4->ip_len = p_len;
-			th = (struct tcphdr *)(ip4 + 1);
+		case LRO_TYPE_IPV6_TCP:
+			csum = tcp_lro_update_checksum(&le->inner, le,
+			    le->m_head->m_pkthdr.lro_tcp_d_len,
+			    le->m_head->m_pkthdr.lro_tcp_d_csum);
+			csum = tcp_lro_update_checksum(&le->outer, NULL,
+			    le->m_head->m_pkthdr.lro_tcp_d_len +
+			    le->inner.total_hdr_len, csum);
 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
-			    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
-			le->p_len += ETHER_HDR_LEN;
+			    CSUM_PSEUDO_HDR;
+			le->m_head->m_pkthdr.csum_data = 0xffff;
+			break;
+		case LRO_TYPE_NONE:
+			switch (le->outer.data.lro_type) {
+			case LRO_TYPE_IPV4_TCP:
+				csum = tcp_lro_update_checksum(&le->outer, le,
+				    le->m_head->m_pkthdr.lro_tcp_d_len,
+				    le->m_head->m_pkthdr.lro_tcp_d_csum);
+				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
+				    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
+				le->m_head->m_pkthdr.csum_data = 0xffff;
+				break;
+			case LRO_TYPE_IPV6_TCP:
+				csum = tcp_lro_update_checksum(&le->outer, le,
+				    le->m_head->m_pkthdr.lro_tcp_d_len,
+				    le->m_head->m_pkthdr.lro_tcp_d_csum);
+				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
+				    CSUM_PSEUDO_HDR;
+				le->m_head->m_pkthdr.csum_data = 0xffff;
+				break;
+			default:
+				break;
+			}
 			break;
-		}
-#endif
 		default:
-			th = NULL;	/* Keep compiler happy. */
-		}
-		le->m_head->m_pkthdr.csum_data = 0xffff;
-		le->m_head->m_pkthdr.len = le->p_len;
-
-		/* Incorporate the latest ACK into the TCP header. */
-		th->th_ack = le->ack_seq;
-		th->th_win = le->window;
-		/* Incorporate latest timestamp into the TCP header. */
-		if (le->timestamp != 0) {
-			uint32_t *ts_ptr;
-
-			ts_ptr = (uint32_t *)(th + 1);
-			ts_ptr[1] = htonl(le->tsval);
-			ts_ptr[2] = le->tsecr;
+			break;
 		}
-		/* Update the TCP header checksum. */
-		le->ulp_csum += p_len;
-		le->ulp_csum += tcp_lro_csum_th(th);
-		while (le->ulp_csum > 0xffff)
-			le->ulp_csum = (le->ulp_csum >> 16) +
-			    (le->ulp_csum & 0xffff);
-		th->th_sum = (le->ulp_csum & 0xffff);
-		th->th_sum = ~th->th_sum;
 	}
+
 	/*
 	 * Break any chain, this is not set to NULL on the singleton
 	 * case m_nextpkt points to m_head. Other case set them
 	 * m_nextpkt to NULL in push_and_replace.
 	 */
 	le->m_head->m_nextpkt = NULL;
-	le->m_head->m_pkthdr.lro_nsegs = le->append_cnt;
+	lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs;
 	(*lc->ifp->if_input)(lc->ifp, le->m_head);
-	lc->lro_queued += le->append_cnt;
 }
 
 static void
-tcp_set_le_to_m(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
+tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le,
+    struct mbuf *m, struct tcphdr *th)
 {
-	struct ether_header *eh;
-	void *l3hdr = NULL;		/* Keep compiler happy. */
-	struct tcphdr *th;
-#ifdef INET6
-	struct ip6_hdr *ip6 = NULL;	/* Keep compiler happy. */
-#endif
-#ifdef INET
-	struct ip *ip4 = NULL;		/* Keep compiler happy. */
-#endif
 	uint32_t *ts_ptr;
-	int error, l, ts_failed = 0;
 	uint16_t tcp_data_len;
-	uint16_t csum;
+	uint16_t tcp_opt_len;
 
-	error = -1;
-	eh = mtod(m, struct ether_header *);
-	/*
-	 * We must reset the other pointers since the mbuf
-	 * we were pointing too is about to go away.
-	 */
-	switch (le->eh_type) {
-#ifdef INET6
-	case ETHERTYPE_IPV6:
-		l3hdr = ip6 = (struct ip6_hdr *)(eh + 1);
-		error = tcp_lro_rx_ipv6(lc, m, ip6, &th);
-		le->le_ip6 = ip6;
-		le->source_ip6 = ip6->ip6_src;
-		le->dest_ip6 = ip6->ip6_dst;
-		le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN - sizeof(*ip6);
-		break;
-#endif
-#ifdef INET
-	case ETHERTYPE_IP:
-		l3hdr = ip4 = (struct ip *)(eh + 1);
-		error = tcp_lro_rx_ipv4(lc, m, ip4, &th);
-		le->le_ip4 = ip4;
-		le->source_ip4 = ip4->ip_src.s_addr;
-		le->dest_ip4 = ip4->ip_dst.s_addr;
-		le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN;
-		break;
-#endif
-	}
-	KASSERT(error == 0, ("%s: le=%p tcp_lro_rx_xxx failed\n",
-				    __func__, le));
 	ts_ptr = (uint32_t *)(th + 1);
-	l = (th->th_off << 2);
-	l -= sizeof(*th);
-	if (l != 0 &&
-	    (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
-	     (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
-			       TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
-		/* We have failed to find a timestamp some other option? */
-		ts_failed = 1;
-	}
-	if ((l != 0) && (ts_failed == 0)) {
+	tcp_opt_len = (th->th_off << 2);
+	tcp_opt_len -= sizeof(*th);
+
+	/* Check if there is a timestamp option. */
+	if (tcp_opt_len == 0 ||
+	    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
+	    *ts_ptr != TCP_LRO_TS_OPTION)) {
+		/* We failed to find the timestamp option. */
+		le->timestamp = 0;
+	} else {
 		le->timestamp = 1;
 		le->tsval = ntohl(*(ts_ptr + 1));
 		le->tsecr = *(ts_ptr + 2);
-	} else
*** 2084 LINES SKIPPED ***


More information about the dev-commits-src-all mailing list