xref: /freebsd/sys/netinet/tcp_lro.c (revision 2c6fc36a0ddd4d741e2c206855d2dff9b008005a)
127f190a3SBjoern A. Zeeb /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
427f190a3SBjoern A. Zeeb  * Copyright (c) 2007, Myricom Inc.
527f190a3SBjoern A. Zeeb  * Copyright (c) 2008, Intel Corporation.
662b5b6ecSBjoern A. Zeeb  * Copyright (c) 2012 The FreeBSD Foundation
79ca874cfSHans Petter Selasky  * Copyright (c) 2016-2021 Mellanox Technologies.
827f190a3SBjoern A. Zeeb  * All rights reserved.
927f190a3SBjoern A. Zeeb  *
1062b5b6ecSBjoern A. Zeeb  * Portions of this software were developed by Bjoern Zeeb
1162b5b6ecSBjoern A. Zeeb  * under sponsorship from the FreeBSD Foundation.
1262b5b6ecSBjoern A. Zeeb  *
1327f190a3SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
1427f190a3SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
1527f190a3SBjoern A. Zeeb  * are met:
1627f190a3SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
1727f190a3SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
1827f190a3SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
1927f190a3SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
2027f190a3SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
2127f190a3SBjoern A. Zeeb  *
2227f190a3SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2327f190a3SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2427f190a3SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2527f190a3SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2627f190a3SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2727f190a3SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2827f190a3SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2927f190a3SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3027f190a3SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3127f190a3SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3227f190a3SBjoern A. Zeeb  * SUCH DAMAGE.
3327f190a3SBjoern A. Zeeb  */
346c5087a8SJack F Vogel 
3562b5b6ecSBjoern A. Zeeb #include <sys/cdefs.h>
3662b5b6ecSBjoern A. Zeeb #include "opt_inet.h"
3762b5b6ecSBjoern A. Zeeb #include "opt_inet6.h"
3862b5b6ecSBjoern A. Zeeb 
396c5087a8SJack F Vogel #include <sys/param.h>
406c5087a8SJack F Vogel #include <sys/systm.h>
416c5087a8SJack F Vogel #include <sys/kernel.h>
428ec07310SGleb Smirnoff #include <sys/malloc.h>
438ec07310SGleb Smirnoff #include <sys/mbuf.h>
446c5087a8SJack F Vogel #include <sys/socket.h>
45e57b2d0eSRandall Stewart #include <sys/socketvar.h>
46e57b2d0eSRandall Stewart #include <sys/sockbuf.h>
478452c1b3SSepherosa Ziehau #include <sys/sysctl.h>
486c5087a8SJack F Vogel 
496c5087a8SJack F Vogel #include <net/if.h>
5062b5b6ecSBjoern A. Zeeb #include <net/if_var.h>
516c5087a8SJack F Vogel #include <net/ethernet.h>
5269a34e8dSRandall Stewart #include <net/bpf.h>
535fa2656eSBjoern A. Zeeb #include <net/vnet.h>
542c6ff1d6SAndrew Gallatin #include <net/if_dl.h>
552c6ff1d6SAndrew Gallatin #include <net/if_media.h>
563d0d5b21SJustin Hibbits #include <net/if_private.h>
572c6ff1d6SAndrew Gallatin #include <net/if_types.h>
582c6ff1d6SAndrew Gallatin #include <net/infiniband.h>
592c6ff1d6SAndrew Gallatin #include <net/if_lagg.h>
606c5087a8SJack F Vogel 
616c5087a8SJack F Vogel #include <netinet/in_systm.h>
626c5087a8SJack F Vogel #include <netinet/in.h>
6362b5b6ecSBjoern A. Zeeb #include <netinet/ip6.h>
646c5087a8SJack F Vogel #include <netinet/ip.h>
6531bfc56eSBjoern A. Zeeb #include <netinet/ip_var.h>
66e57b2d0eSRandall Stewart #include <netinet/in_pcb.h>
67e57b2d0eSRandall Stewart #include <netinet6/in6_pcb.h>
686c5087a8SJack F Vogel #include <netinet/tcp.h>
69d7fb35d1SSean Bruno #include <netinet/tcp_seq.h>
706c5087a8SJack F Vogel #include <netinet/tcp_lro.h>
718452c1b3SSepherosa Ziehau #include <netinet/tcp_var.h>
7269a34e8dSRandall Stewart #include <netinet/tcpip.h>
73e57b2d0eSRandall Stewart #include <netinet/tcp_hpts.h>
74e57b2d0eSRandall Stewart #include <netinet/tcp_log_buf.h>
7531bc602fSRandall Stewart #include <netinet/tcp_fsm.h>
769ca874cfSHans Petter Selasky #include <netinet/udp.h>
7731bfc56eSBjoern A. Zeeb #include <netinet6/ip6_var.h>
7831bfc56eSBjoern A. Zeeb 
796c5087a8SJack F Vogel #include <machine/in_cksum.h>
806c5087a8SJack F Vogel 
81e936121dSHans Petter Selasky static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
826c5087a8SJack F Vogel 
836dd38b87SSepherosa Ziehau static void	tcp_lro_rx_done(struct lro_ctrl *lc);
849ca874cfSHans Petter Selasky static int	tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m,
859ca874cfSHans Petter Selasky 		    uint32_t csum, bool use_hash);
869ca874cfSHans Petter Selasky 
878452c1b3SSepherosa Ziehau SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro,  CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
888452c1b3SSepherosa Ziehau     "TCP LRO");
898452c1b3SSepherosa Ziehau 
904f9c93f1SGleb Smirnoff long tcplro_stacks_wanting_mbufq;
91*2c6fc36aSGleb Smirnoff int	(*tcp_lro_flush_tcphpts)(struct lro_ctrl *lc, struct lro_entry *le);
92*2c6fc36aSGleb Smirnoff void	(*tcp_hpts_softclock)(void);
93*2c6fc36aSGleb Smirnoff 
94e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_direct_queue;
95e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_wokeup_queue;
96e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_compressed;
97e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_locks_taken;
9869a34e8dSRandall Stewart counter_u64_t tcp_extra_mbuf;
9969a34e8dSRandall Stewart counter_u64_t tcp_would_have_but;
10069a34e8dSRandall Stewart counter_u64_t tcp_comp_total;
10169a34e8dSRandall Stewart counter_u64_t tcp_uncomp_total;
102ca1a7e10SRandall Stewart counter_u64_t tcp_bad_csums;
103e57b2d0eSRandall Stewart 
1048452c1b3SSepherosa Ziehau static unsigned	tcp_lro_entries = TCP_LRO_ENTRIES;
1058452c1b3SSepherosa Ziehau SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
1068452c1b3SSepherosa Ziehau     CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
1078452c1b3SSepherosa Ziehau     "default number of LRO entries");
10869a34e8dSRandall Stewart 
109d7955cc0SRandall Stewart static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH;
110d7955cc0SRandall Stewart SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold,
111d7955cc0SRandall Stewart     CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0,
11208c7f1b6SNavdeep Parhar     "Number of interrupts in a row on the same CPU that will make us declare an 'affinity' cpu?");
113d7955cc0SRandall Stewart 
1144e0ce82bSRandall Stewart static uint32_t tcp_less_accurate_lro_ts = 0;
1154e0ce82bSRandall Stewart SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_less_accurate,
1164e0ce82bSRandall Stewart     CTLFLAG_MPSAFE, &tcp_less_accurate_lro_ts, 0,
1174e0ce82bSRandall Stewart     "Do we trade off efficency by doing less timestamp operations for time accuracy?");
1184e0ce82bSRandall Stewart 
119e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD,
120e57b2d0eSRandall Stewart     &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport");
121e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD,
122e57b2d0eSRandall Stewart     &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts");
123e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD,
124e57b2d0eSRandall Stewart     &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport");
125e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
126e57b2d0eSRandall Stewart     &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
12769a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD,
12869a34e8dSRandall Stewart     &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp");
12969a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD,
1309ca874cfSHans Petter Selasky     &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed");
13169a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD,
13269a34e8dSRandall Stewart     &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set");
13369a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD,
13469a34e8dSRandall Stewart     &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP");
135ca1a7e10SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD,
136ca1a7e10SRandall Stewart     &tcp_bad_csums, "Number of packets that the common code saw with bad csums");
137e57b2d0eSRandall Stewart 
138e57b2d0eSRandall Stewart void
139e57b2d0eSRandall Stewart tcp_lro_reg_mbufq(void)
140e57b2d0eSRandall Stewart {
141e57b2d0eSRandall Stewart 	atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1);
142e57b2d0eSRandall Stewart }
143e57b2d0eSRandall Stewart 
144e57b2d0eSRandall Stewart void
145e57b2d0eSRandall Stewart tcp_lro_dereg_mbufq(void)
146e57b2d0eSRandall Stewart {
147e57b2d0eSRandall Stewart 	atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1);
148e57b2d0eSRandall Stewart }
1498452c1b3SSepherosa Ziehau 
15051e3c20dSSepherosa Ziehau static __inline void
15105cde7efSSepherosa Ziehau tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket,
15205cde7efSSepherosa Ziehau     struct lro_entry *le)
15351e3c20dSSepherosa Ziehau {
15451e3c20dSSepherosa Ziehau 
15551e3c20dSSepherosa Ziehau 	LIST_INSERT_HEAD(&lc->lro_active, le, next);
15605cde7efSSepherosa Ziehau 	LIST_INSERT_HEAD(bucket, le, hash_next);
15751e3c20dSSepherosa Ziehau }
15851e3c20dSSepherosa Ziehau 
15951e3c20dSSepherosa Ziehau static __inline void
16051e3c20dSSepherosa Ziehau tcp_lro_active_remove(struct lro_entry *le)
16151e3c20dSSepherosa Ziehau {
16251e3c20dSSepherosa Ziehau 
16305cde7efSSepherosa Ziehau 	LIST_REMOVE(le, next);		/* active list */
16405cde7efSSepherosa Ziehau 	LIST_REMOVE(le, hash_next);	/* hash bucket */
16551e3c20dSSepherosa Ziehau }
16651e3c20dSSepherosa Ziehau 
1676c5087a8SJack F Vogel int
16862b5b6ecSBjoern A. Zeeb tcp_lro_init(struct lro_ctrl *lc)
1696c5087a8SJack F Vogel {
1708452c1b3SSepherosa Ziehau 	return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0));
171e936121dSHans Petter Selasky }
172e936121dSHans Petter Selasky 
173e936121dSHans Petter Selasky int
174e936121dSHans Petter Selasky tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
175e936121dSHans Petter Selasky     unsigned lro_entries, unsigned lro_mbufs)
176e936121dSHans Petter Selasky {
17762b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
178e936121dSHans Petter Selasky 	size_t size;
17905cde7efSSepherosa Ziehau 	unsigned i, elements;
1806c5087a8SJack F Vogel 
18162b5b6ecSBjoern A. Zeeb 	lc->lro_bad_csum = 0;
18262b5b6ecSBjoern A. Zeeb 	lc->lro_queued = 0;
18362b5b6ecSBjoern A. Zeeb 	lc->lro_flushed = 0;
184e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
185e936121dSHans Petter Selasky 	lc->lro_mbuf_max = lro_mbufs;
186e936121dSHans Petter Selasky 	lc->lro_cnt = lro_entries;
1877ae3d4bfSSepherosa Ziehau 	lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX;
1887ae3d4bfSSepherosa Ziehau 	lc->lro_length_lim = TCP_LRO_LENGTH_MAX;
189e936121dSHans Petter Selasky 	lc->ifp = ifp;
1901ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_free);
1911ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_active);
1926c5087a8SJack F Vogel 
19305cde7efSSepherosa Ziehau 	/* create hash table to accelerate entry lookup */
19405cde7efSSepherosa Ziehau 	if (lro_entries > lro_mbufs)
19505cde7efSSepherosa Ziehau 		elements = lro_entries;
19605cde7efSSepherosa Ziehau 	else
19705cde7efSSepherosa Ziehau 		elements = lro_mbufs;
19805cde7efSSepherosa Ziehau 	lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz,
19905cde7efSSepherosa Ziehau 	    HASH_NOWAIT);
20005cde7efSSepherosa Ziehau 	if (lc->lro_hash == NULL) {
20105cde7efSSepherosa Ziehau 		memset(lc, 0, sizeof(*lc));
20205cde7efSSepherosa Ziehau 		return (ENOMEM);
20305cde7efSSepherosa Ziehau 	}
20405cde7efSSepherosa Ziehau 
205e936121dSHans Petter Selasky 	/* compute size to allocate */
206fc271df3SHans Petter Selasky 	size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) +
207e936121dSHans Petter Selasky 	    (lro_entries * sizeof(*le));
208fc271df3SHans Petter Selasky 	lc->lro_mbuf_data = (struct lro_mbuf_sort *)
209e936121dSHans Petter Selasky 	    malloc(size, M_LRO, M_NOWAIT | M_ZERO);
2106c5087a8SJack F Vogel 
211e936121dSHans Petter Selasky 	/* check for out of memory */
212e936121dSHans Petter Selasky 	if (lc->lro_mbuf_data == NULL) {
213a3927369SNavdeep Parhar 		free(lc->lro_hash, M_LRO);
214e936121dSHans Petter Selasky 		memset(lc, 0, sizeof(*lc));
215e936121dSHans Petter Selasky 		return (ENOMEM);
216e936121dSHans Petter Selasky 	}
217e936121dSHans Petter Selasky 	/* compute offset for LRO entries */
218e936121dSHans Petter Selasky 	le = (struct lro_entry *)
219e936121dSHans Petter Selasky 	    (lc->lro_mbuf_data + lro_mbufs);
220e936121dSHans Petter Selasky 
221e936121dSHans Petter Selasky 	/* setup linked list */
222e936121dSHans Petter Selasky 	for (i = 0; i != lro_entries; i++)
2231ea44822SSepherosa Ziehau 		LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
224e936121dSHans Petter Selasky 
225e936121dSHans Petter Selasky 	return (0);
2266c5087a8SJack F Vogel }
2276c5087a8SJack F Vogel 
2289ca874cfSHans Petter Selasky struct vxlan_header {
2299ca874cfSHans Petter Selasky 	uint32_t	vxlh_flags;
2309ca874cfSHans Petter Selasky 	uint32_t	vxlh_vni;
2319ca874cfSHans Petter Selasky };
232e57b2d0eSRandall Stewart 
2339ca874cfSHans Petter Selasky static inline void *
2341d171e5aSRandall Stewart tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen)
2359ca874cfSHans Petter Selasky {
2369ca874cfSHans Petter Selasky 	const struct ether_vlan_header *eh;
2379ca874cfSHans Petter Selasky 	void *old;
2389ca874cfSHans Petter Selasky 	uint16_t eth_type;
2399ca874cfSHans Petter Selasky 
2409ca874cfSHans Petter Selasky 	if (update_data)
2419ca874cfSHans Petter Selasky 		memset(parser, 0, sizeof(*parser));
2429ca874cfSHans Petter Selasky 
2439ca874cfSHans Petter Selasky 	old = ptr;
2449ca874cfSHans Petter Selasky 
2459ca874cfSHans Petter Selasky 	if (is_vxlan) {
2469ca874cfSHans Petter Selasky 		const struct vxlan_header *vxh;
2479ca874cfSHans Petter Selasky 		vxh = ptr;
2489ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + sizeof(*vxh);
2499ca874cfSHans Petter Selasky 		if (update_data) {
2509ca874cfSHans Petter Selasky 			parser->data.vxlan_vni =
2519ca874cfSHans Petter Selasky 			    vxh->vxlh_vni & htonl(0xffffff00);
252e57b2d0eSRandall Stewart 		}
2539ca874cfSHans Petter Selasky 	}
2549ca874cfSHans Petter Selasky 
2559ca874cfSHans Petter Selasky 	eh = ptr;
2569ca874cfSHans Petter Selasky 	if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) {
2579ca874cfSHans Petter Selasky 		eth_type = eh->evl_proto;
2589ca874cfSHans Petter Selasky 		if (update_data) {
2599ca874cfSHans Petter Selasky 			/* strip priority and keep VLAN ID only */
2609ca874cfSHans Petter Selasky 			parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK);
2619ca874cfSHans Petter Selasky 		}
2629ca874cfSHans Petter Selasky 		/* advance to next header */
2639ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
2641d171e5aSRandall Stewart 		mlen -= (ETHER_HDR_LEN  + ETHER_VLAN_ENCAP_LEN);
2659ca874cfSHans Petter Selasky 	} else {
2669ca874cfSHans Petter Selasky 		eth_type = eh->evl_encap_proto;
2679ca874cfSHans Petter Selasky 		/* advance to next header */
2681d171e5aSRandall Stewart 		mlen -= ETHER_HDR_LEN;
2699ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + ETHER_HDR_LEN;
2709ca874cfSHans Petter Selasky 	}
2711d171e5aSRandall Stewart 	if (__predict_false(mlen <= 0))
2721d171e5aSRandall Stewart 		return (NULL);
2739ca874cfSHans Petter Selasky 	switch (eth_type) {
2749ca874cfSHans Petter Selasky #ifdef INET
2759ca874cfSHans Petter Selasky 	case htons(ETHERTYPE_IP):
2769ca874cfSHans Petter Selasky 		parser->ip4 = ptr;
2771d171e5aSRandall Stewart 		if (__predict_false(mlen < sizeof(struct ip)))
2781d171e5aSRandall Stewart 			return (NULL);
2799ca874cfSHans Petter Selasky 		/* Ensure there are no IPv4 options. */
2809ca874cfSHans Petter Selasky 		if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4))
2819ca874cfSHans Petter Selasky 			break;
2829ca874cfSHans Petter Selasky 		/* .. and the packet is not fragmented. */
2839ca874cfSHans Petter Selasky 		if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK))
2849ca874cfSHans Petter Selasky 			break;
285abba5876SAndrew Gallatin 		/* .. and the packet has valid src/dst addrs */
286abba5876SAndrew Gallatin 		if (__predict_false(parser->ip4->ip_src.s_addr == INADDR_ANY ||
287abba5876SAndrew Gallatin 			parser->ip4->ip_dst.s_addr == INADDR_ANY))
288abba5876SAndrew Gallatin 			break;
2899ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2);
2901d171e5aSRandall Stewart 		mlen -= sizeof(struct ip);
2919ca874cfSHans Petter Selasky 		if (update_data) {
2929ca874cfSHans Petter Selasky 			parser->data.s_addr.v4 = parser->ip4->ip_src;
2939ca874cfSHans Petter Selasky 			parser->data.d_addr.v4 = parser->ip4->ip_dst;
2949ca874cfSHans Petter Selasky 		}
2959ca874cfSHans Petter Selasky 		switch (parser->ip4->ip_p) {
2969ca874cfSHans Petter Selasky 		case IPPROTO_UDP:
2971d171e5aSRandall Stewart 			if (__predict_false(mlen < sizeof(struct udphdr)))
2981d171e5aSRandall Stewart 				return (NULL);
2999ca874cfSHans Petter Selasky 			parser->udp = ptr;
3009ca874cfSHans Petter Selasky 			if (update_data) {
3019ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV4_UDP;
3029ca874cfSHans Petter Selasky 				parser->data.s_port = parser->udp->uh_sport;
3039ca874cfSHans Petter Selasky 				parser->data.d_port = parser->udp->uh_dport;
3049ca874cfSHans Petter Selasky 			} else {
3059ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP);
3069ca874cfSHans Petter Selasky 			}
3079ca874cfSHans Petter Selasky 			ptr = ((uint8_t *)ptr + sizeof(*parser->udp));
3089ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
3099ca874cfSHans Petter Selasky 			return (ptr);
3109ca874cfSHans Petter Selasky 		case IPPROTO_TCP:
3119ca874cfSHans Petter Selasky 			parser->tcp = ptr;
3121d171e5aSRandall Stewart 			if (__predict_false(mlen < sizeof(struct tcphdr)))
3131d171e5aSRandall Stewart 				return (NULL);
3149ca874cfSHans Petter Selasky 			if (update_data) {
3159ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV4_TCP;
3169ca874cfSHans Petter Selasky 				parser->data.s_port = parser->tcp->th_sport;
3179ca874cfSHans Petter Selasky 				parser->data.d_port = parser->tcp->th_dport;
3189ca874cfSHans Petter Selasky 			} else {
3199ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP);
3209ca874cfSHans Petter Selasky 			}
3211d171e5aSRandall Stewart 			if (__predict_false(mlen < (parser->tcp->th_off << 2)))
3221d171e5aSRandall Stewart 				return (NULL);
3239ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
3249ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
3259ca874cfSHans Petter Selasky 			return (ptr);
3269ca874cfSHans Petter Selasky 		default:
3279ca874cfSHans Petter Selasky 			break;
3289ca874cfSHans Petter Selasky 		}
3299ca874cfSHans Petter Selasky 		break;
3309ca874cfSHans Petter Selasky #endif
3319ca874cfSHans Petter Selasky #ifdef INET6
3329ca874cfSHans Petter Selasky 	case htons(ETHERTYPE_IPV6):
3339ca874cfSHans Petter Selasky 		parser->ip6 = ptr;
3341d171e5aSRandall Stewart 		if (__predict_false(mlen < sizeof(struct ip6_hdr)))
3351d171e5aSRandall Stewart 			return (NULL);
336abba5876SAndrew Gallatin 		/* Ensure the packet has valid src/dst addrs */
337abba5876SAndrew Gallatin 		if (__predict_false(IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_src) ||
338abba5876SAndrew Gallatin 			IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_dst)))
339abba5876SAndrew Gallatin 			return (NULL);
3409ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + sizeof(*parser->ip6);
3419ca874cfSHans Petter Selasky 		if (update_data) {
3429ca874cfSHans Petter Selasky 			parser->data.s_addr.v6 = parser->ip6->ip6_src;
3439ca874cfSHans Petter Selasky 			parser->data.d_addr.v6 = parser->ip6->ip6_dst;
3449ca874cfSHans Petter Selasky 		}
3451d171e5aSRandall Stewart 		mlen -= sizeof(struct ip6_hdr);
3469ca874cfSHans Petter Selasky 		switch (parser->ip6->ip6_nxt) {
3479ca874cfSHans Petter Selasky 		case IPPROTO_UDP:
3481d171e5aSRandall Stewart 			if (__predict_false(mlen < sizeof(struct udphdr)))
3491d171e5aSRandall Stewart 				return (NULL);
3509ca874cfSHans Petter Selasky 			parser->udp = ptr;
3519ca874cfSHans Petter Selasky 			if (update_data) {
3529ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV6_UDP;
3539ca874cfSHans Petter Selasky 				parser->data.s_port = parser->udp->uh_sport;
3549ca874cfSHans Petter Selasky 				parser->data.d_port = parser->udp->uh_dport;
3559ca874cfSHans Petter Selasky 			} else {
3569ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP);
3579ca874cfSHans Petter Selasky 			}
3589ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + sizeof(*parser->udp);
3599ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
3609ca874cfSHans Petter Selasky 			return (ptr);
3619ca874cfSHans Petter Selasky 		case IPPROTO_TCP:
3621d171e5aSRandall Stewart 			if (__predict_false(mlen < sizeof(struct tcphdr)))
3631d171e5aSRandall Stewart 				return (NULL);
3649ca874cfSHans Petter Selasky 			parser->tcp = ptr;
3659ca874cfSHans Petter Selasky 			if (update_data) {
3669ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV6_TCP;
3679ca874cfSHans Petter Selasky 				parser->data.s_port = parser->tcp->th_sport;
3689ca874cfSHans Petter Selasky 				parser->data.d_port = parser->tcp->th_dport;
3699ca874cfSHans Petter Selasky 			} else {
3709ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP);
3719ca874cfSHans Petter Selasky 			}
3721d171e5aSRandall Stewart 			if (__predict_false(mlen < (parser->tcp->th_off << 2)))
3731d171e5aSRandall Stewart 				return (NULL);
3749ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
3759ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
3769ca874cfSHans Petter Selasky 			return (ptr);
3779ca874cfSHans Petter Selasky 		default:
3789ca874cfSHans Petter Selasky 			break;
3799ca874cfSHans Petter Selasky 		}
3809ca874cfSHans Petter Selasky 		break;
3819ca874cfSHans Petter Selasky #endif
3829ca874cfSHans Petter Selasky 	default:
3839ca874cfSHans Petter Selasky 		break;
3849ca874cfSHans Petter Selasky 	}
3859ca874cfSHans Petter Selasky 	/* Invalid packet - cannot parse */
3869ca874cfSHans Petter Selasky 	return (NULL);
3879ca874cfSHans Petter Selasky }
3889ca874cfSHans Petter Selasky 
3899ca874cfSHans Petter Selasky static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
3909ca874cfSHans Petter Selasky     CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID;
3919ca874cfSHans Petter Selasky 
3929ca874cfSHans Petter Selasky static inline struct lro_parser *
3939ca874cfSHans Petter Selasky tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data)
3949ca874cfSHans Petter Selasky {
3959ca874cfSHans Petter Selasky 	void *data_ptr;
3969ca874cfSHans Petter Selasky 
3979ca874cfSHans Petter Selasky 	/* Try to parse outer headers first. */
3981d171e5aSRandall Stewart 	data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len);
3999ca874cfSHans Petter Selasky 	if (data_ptr == NULL || po->total_hdr_len > m->m_len)
4009ca874cfSHans Petter Selasky 		return (NULL);
4019ca874cfSHans Petter Selasky 
4029ca874cfSHans Petter Selasky 	if (update_data) {
4039ca874cfSHans Petter Selasky 		/* Store VLAN ID, if any. */
4049ca874cfSHans Petter Selasky 		if (__predict_false(m->m_flags & M_VLANTAG)) {
4059ca874cfSHans Petter Selasky 			po->data.vlan_id =
4069ca874cfSHans Petter Selasky 			    htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK);
4079ca874cfSHans Petter Selasky 		}
408bb5cd80eSHans Petter Selasky 		/* Store decrypted flag, if any. */
40910a62eb1SHans Petter Selasky 		if (__predict_false((m->m_pkthdr.csum_flags &
41010a62eb1SHans Petter Selasky 		    CSUM_TLS_MASK) == CSUM_TLS_DECRYPTED))
411bb5cd80eSHans Petter Selasky 			po->data.lro_flags |= LRO_FLAG_DECRYPTED;
4129ca874cfSHans Petter Selasky 	}
4139ca874cfSHans Petter Selasky 
4149ca874cfSHans Petter Selasky 	switch (po->data.lro_type) {
4159ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_UDP:
4169ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_UDP:
4179ca874cfSHans Petter Selasky 		/* Check for VXLAN headers. */
4189ca874cfSHans Petter Selasky 		if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum)
4199ca874cfSHans Petter Selasky 			break;
4209ca874cfSHans Petter Selasky 
4219ca874cfSHans Petter Selasky 		/* Try to parse inner headers. */
4221d171e5aSRandall Stewart 		data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true,
4231d171e5aSRandall Stewart 						    (m->m_len - ((caddr_t)data_ptr - m->m_data)));
4241d171e5aSRandall Stewart 		if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len)
4259ca874cfSHans Petter Selasky 			break;
4269ca874cfSHans Petter Selasky 
4279ca874cfSHans Petter Selasky 		/* Verify supported header types. */
4289ca874cfSHans Petter Selasky 		switch (pi->data.lro_type) {
4299ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV4_TCP:
4309ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV6_TCP:
4319ca874cfSHans Petter Selasky 			return (pi);
4329ca874cfSHans Petter Selasky 		default:
4339ca874cfSHans Petter Selasky 			break;
4349ca874cfSHans Petter Selasky 		}
4359ca874cfSHans Petter Selasky 		break;
4369ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
4379ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
4389ca874cfSHans Petter Selasky 		if (update_data)
4399ca874cfSHans Petter Selasky 			memset(pi, 0, sizeof(*pi));
4409ca874cfSHans Petter Selasky 		return (po);
4419ca874cfSHans Petter Selasky 	default:
4429ca874cfSHans Petter Selasky 		break;
4439ca874cfSHans Petter Selasky 	}
4449ca874cfSHans Petter Selasky 	return (NULL);
4459ca874cfSHans Petter Selasky }
4469ca874cfSHans Petter Selasky 
4479ca874cfSHans Petter Selasky static inline int
4489ca874cfSHans Petter Selasky tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po)
4499ca874cfSHans Petter Selasky {
4509ca874cfSHans Petter Selasky 	int len;
4519ca874cfSHans Petter Selasky 
4529ca874cfSHans Petter Selasky 	switch (po->data.lro_type) {
4539ca874cfSHans Petter Selasky #ifdef INET
4549ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
4559ca874cfSHans Petter Selasky 		len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) +
4569ca874cfSHans Petter Selasky 		    ntohs(po->ip4->ip_len);
4579ca874cfSHans Petter Selasky 		break;
4589ca874cfSHans Petter Selasky #endif
4599ca874cfSHans Petter Selasky #ifdef INET6
4609ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
4619ca874cfSHans Petter Selasky 		len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) +
4629ca874cfSHans Petter Selasky 		    ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6);
4639ca874cfSHans Petter Selasky 		break;
4649ca874cfSHans Petter Selasky #endif
4659ca874cfSHans Petter Selasky 	default:
4669ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
4679ca874cfSHans Petter Selasky 	}
4689ca874cfSHans Petter Selasky 
4699ca874cfSHans Petter Selasky 	/*
4709ca874cfSHans Petter Selasky 	 * If the frame is padded beyond the end of the IP packet,
4719ca874cfSHans Petter Selasky 	 * then trim the extra bytes off:
4729ca874cfSHans Petter Selasky 	 */
4739ca874cfSHans Petter Selasky 	if (__predict_true(m->m_pkthdr.len == len)) {
4749ca874cfSHans Petter Selasky 		return (0);
4759ca874cfSHans Petter Selasky 	} else if (m->m_pkthdr.len > len) {
4769ca874cfSHans Petter Selasky 		m_adj(m, len - m->m_pkthdr.len);
4779ca874cfSHans Petter Selasky 		return (0);
4789ca874cfSHans Petter Selasky 	}
4799ca874cfSHans Petter Selasky 	return (TCP_LRO_CANNOT);
4809ca874cfSHans Petter Selasky }
4819ca874cfSHans Petter Selasky 
48269a34e8dSRandall Stewart static void
48369a34e8dSRandall Stewart lro_free_mbuf_chain(struct mbuf *m)
48469a34e8dSRandall Stewart {
48569a34e8dSRandall Stewart 	struct mbuf *save;
48669a34e8dSRandall Stewart 
48769a34e8dSRandall Stewart 	while (m) {
48869a34e8dSRandall Stewart 		save = m->m_nextpkt;
48969a34e8dSRandall Stewart 		m->m_nextpkt = NULL;
49069a34e8dSRandall Stewart 		m_freem(m);
49169a34e8dSRandall Stewart 		m = save;
49269a34e8dSRandall Stewart 	}
49369a34e8dSRandall Stewart }
49469a34e8dSRandall Stewart 
4956c5087a8SJack F Vogel void
49662b5b6ecSBjoern A. Zeeb tcp_lro_free(struct lro_ctrl *lc)
4976c5087a8SJack F Vogel {
49862b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
499e936121dSHans Petter Selasky 	unsigned x;
5006c5087a8SJack F Vogel 
501e936121dSHans Petter Selasky 	/* reset LRO free list */
5021ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_free);
503e936121dSHans Petter Selasky 
504e936121dSHans Petter Selasky 	/* free active mbufs, if any */
5051ea44822SSepherosa Ziehau 	while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
50651e3c20dSSepherosa Ziehau 		tcp_lro_active_remove(le);
50769a34e8dSRandall Stewart 		lro_free_mbuf_chain(le->m_head);
5086c5087a8SJack F Vogel 	}
509e936121dSHans Petter Selasky 
51005cde7efSSepherosa Ziehau 	/* free hash table */
51105cde7efSSepherosa Ziehau 	free(lc->lro_hash, M_LRO);
51205cde7efSSepherosa Ziehau 	lc->lro_hash = NULL;
51305cde7efSSepherosa Ziehau 	lc->lro_hashsz = 0;
51405cde7efSSepherosa Ziehau 
515e936121dSHans Petter Selasky 	/* free mbuf array, if any */
516e936121dSHans Petter Selasky 	for (x = 0; x != lc->lro_mbuf_count; x++)
517fc271df3SHans Petter Selasky 		m_freem(lc->lro_mbuf_data[x].mb);
518e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
519e936121dSHans Petter Selasky 
520e936121dSHans Petter Selasky 	/* free allocated memory, if any */
521e936121dSHans Petter Selasky 	free(lc->lro_mbuf_data, M_LRO);
522e936121dSHans Petter Selasky 	lc->lro_mbuf_data = NULL;
5236c5087a8SJack F Vogel }
5246c5087a8SJack F Vogel 
52562b5b6ecSBjoern A. Zeeb static uint16_t
5269ca874cfSHans Petter Selasky tcp_lro_rx_csum_tcphdr(const struct tcphdr *th)
52762b5b6ecSBjoern A. Zeeb {
5289ca874cfSHans Petter Selasky 	const uint16_t *ptr;
5299ca874cfSHans Petter Selasky 	uint32_t csum;
5309ca874cfSHans Petter Selasky 	uint16_t len;
53162b5b6ecSBjoern A. Zeeb 
5329ca874cfSHans Petter Selasky 	csum = -th->th_sum;	/* exclude checksum field */
5339ca874cfSHans Petter Selasky 	len = th->th_off;
5349ca874cfSHans Petter Selasky 	ptr = (const uint16_t *)th;
5359ca874cfSHans Petter Selasky 	while (len--) {
5369ca874cfSHans Petter Selasky 		csum += *ptr;
5379ca874cfSHans Petter Selasky 		ptr++;
5389ca874cfSHans Petter Selasky 		csum += *ptr;
5399ca874cfSHans Petter Selasky 		ptr++;
54062b5b6ecSBjoern A. Zeeb 	}
5419ca874cfSHans Petter Selasky 	while (csum > 0xffff)
5429ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
54362b5b6ecSBjoern A. Zeeb 
5449ca874cfSHans Petter Selasky 	return (csum);
54562b5b6ecSBjoern A. Zeeb }
54662b5b6ecSBjoern A. Zeeb 
54762b5b6ecSBjoern A. Zeeb static uint16_t
5489ca874cfSHans Petter Selasky tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum)
54962b5b6ecSBjoern A. Zeeb {
55062b5b6ecSBjoern A. Zeeb 	uint32_t c;
55162b5b6ecSBjoern A. Zeeb 	uint16_t cs;
55262b5b6ecSBjoern A. Zeeb 
5539ca874cfSHans Petter Selasky 	c = tcp_csum;
55462b5b6ecSBjoern A. Zeeb 
5559ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
55662b5b6ecSBjoern A. Zeeb #ifdef INET6
5579ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
5589ca874cfSHans Petter Selasky 		/* Compute full pseudo IPv6 header checksum. */
5599ca874cfSHans Petter Selasky 		cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0);
56062b5b6ecSBjoern A. Zeeb 		break;
56162b5b6ecSBjoern A. Zeeb #endif
56262b5b6ecSBjoern A. Zeeb #ifdef INET
5639ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
5649ca874cfSHans Petter Selasky 		/* Compute full pseudo IPv4 header checsum. */
5659ca874cfSHans Petter Selasky 		cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP);
5669ca874cfSHans Petter Selasky 		cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs));
56762b5b6ecSBjoern A. Zeeb 		break;
56862b5b6ecSBjoern A. Zeeb #endif
56962b5b6ecSBjoern A. Zeeb 	default:
57062b5b6ecSBjoern A. Zeeb 		cs = 0;		/* Keep compiler happy. */
5719ca874cfSHans Petter Selasky 		break;
57262b5b6ecSBjoern A. Zeeb 	}
57362b5b6ecSBjoern A. Zeeb 
5749ca874cfSHans Petter Selasky 	/* Complement checksum. */
57562b5b6ecSBjoern A. Zeeb 	cs = ~cs;
57662b5b6ecSBjoern A. Zeeb 	c += cs;
57762b5b6ecSBjoern A. Zeeb 
5789ca874cfSHans Petter Selasky 	/* Remove TCP header checksum. */
5799ca874cfSHans Petter Selasky 	cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp);
58062b5b6ecSBjoern A. Zeeb 	c += cs;
5819ca874cfSHans Petter Selasky 
5829ca874cfSHans Petter Selasky 	/* Compute checksum remainder. */
58362b5b6ecSBjoern A. Zeeb 	while (c > 0xffff)
58462b5b6ecSBjoern A. Zeeb 		c = (c >> 16) + (c & 0xffff);
58562b5b6ecSBjoern A. Zeeb 
5869ca874cfSHans Petter Selasky 	return (c);
58762b5b6ecSBjoern A. Zeeb }
58862b5b6ecSBjoern A. Zeeb 
5896dd38b87SSepherosa Ziehau static void
5906dd38b87SSepherosa Ziehau tcp_lro_rx_done(struct lro_ctrl *lc)
5916dd38b87SSepherosa Ziehau {
5926dd38b87SSepherosa Ziehau 	struct lro_entry *le;
5936dd38b87SSepherosa Ziehau 
5941ea44822SSepherosa Ziehau 	while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
59551e3c20dSSepherosa Ziehau 		tcp_lro_active_remove(le);
5966dd38b87SSepherosa Ziehau 		tcp_lro_flush(lc, le);
5976dd38b87SSepherosa Ziehau 	}
5986dd38b87SSepherosa Ziehau }
5996dd38b87SSepherosa Ziehau 
6004e0ce82bSRandall Stewart static void
6014e0ce82bSRandall Stewart tcp_lro_flush_active(struct lro_ctrl *lc)
6024e0ce82bSRandall Stewart {
6034e0ce82bSRandall Stewart 	struct lro_entry *le;
6044e0ce82bSRandall Stewart 
6054e0ce82bSRandall Stewart 	/*
6064e0ce82bSRandall Stewart 	 * Walk through the list of le entries, and
6074e0ce82bSRandall Stewart 	 * any one that does have packets flush. This
6084e0ce82bSRandall Stewart 	 * is called because we have an inbound packet
6094e0ce82bSRandall Stewart 	 * (e.g. SYN) that has to have all others flushed
6104e0ce82bSRandall Stewart 	 * in front of it. Note we have to do the remove
6114e0ce82bSRandall Stewart 	 * because tcp_lro_flush() assumes that the entry
6124e0ce82bSRandall Stewart 	 * is being freed. This is ok it will just get
6134e0ce82bSRandall Stewart 	 * reallocated again like it was new.
6144e0ce82bSRandall Stewart 	 */
6154e0ce82bSRandall Stewart 	LIST_FOREACH(le, &lc->lro_active, next) {
6164e0ce82bSRandall Stewart 		if (le->m_head != NULL) {
6174e0ce82bSRandall Stewart 			tcp_lro_active_remove(le);
6184e0ce82bSRandall Stewart 			tcp_lro_flush(lc, le);
6194e0ce82bSRandall Stewart 		}
6204e0ce82bSRandall Stewart 	}
6214e0ce82bSRandall Stewart }
6224e0ce82bSRandall Stewart 
6236c5087a8SJack F Vogel void
6247127e6acSNavdeep Parhar tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
6257127e6acSNavdeep Parhar {
6267127e6acSNavdeep Parhar 	struct lro_entry *le, *le_tmp;
627b45daaeaSRandall Stewart 	uint64_t now, tov;
628b45daaeaSRandall Stewart 	struct bintime bt;
6297127e6acSNavdeep Parhar 
630dc6ab77dSMichael Tuexen 	NET_EPOCH_ASSERT();
6311ea44822SSepherosa Ziehau 	if (LIST_EMPTY(&lc->lro_active))
6327127e6acSNavdeep Parhar 		return;
6337127e6acSNavdeep Parhar 
634b45daaeaSRandall Stewart 	/* get timeout time and current time in ns */
635b45daaeaSRandall Stewart 	binuptime(&bt);
636b45daaeaSRandall Stewart 	now = bintime2ns(&bt);
637b45daaeaSRandall Stewart 	tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000));
6381ea44822SSepherosa Ziehau 	LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
639b45daaeaSRandall Stewart 		if (now >= (bintime2ns(&le->alloc_time) + tov)) {
64051e3c20dSSepherosa Ziehau 			tcp_lro_active_remove(le);
6417127e6acSNavdeep Parhar 			tcp_lro_flush(lc, le);
6427127e6acSNavdeep Parhar 		}
6437127e6acSNavdeep Parhar 	}
6447127e6acSNavdeep Parhar }
6457127e6acSNavdeep Parhar 
646e57b2d0eSRandall Stewart #ifdef INET
647e57b2d0eSRandall Stewart static int
6489ca874cfSHans Petter Selasky tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4)
649e57b2d0eSRandall Stewart {
650e57b2d0eSRandall Stewart 	uint16_t csum;
651e57b2d0eSRandall Stewart 
652e57b2d0eSRandall Stewart 	/* Legacy IP has a header checksum that needs to be correct. */
6539ca874cfSHans Petter Selasky 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
6549ca874cfSHans Petter Selasky 		if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) {
655e57b2d0eSRandall Stewart 			lc->lro_bad_csum++;
656e57b2d0eSRandall Stewart 			return (TCP_LRO_CANNOT);
657e57b2d0eSRandall Stewart 		}
658e57b2d0eSRandall Stewart 	} else {
659e57b2d0eSRandall Stewart 		csum = in_cksum_hdr(ip4);
6609ca874cfSHans Petter Selasky 		if (__predict_false(csum != 0)) {
661e57b2d0eSRandall Stewart 			lc->lro_bad_csum++;
662e57b2d0eSRandall Stewart 			return (TCP_LRO_CANNOT);
663e57b2d0eSRandall Stewart 		}
664e57b2d0eSRandall Stewart 	}
665e57b2d0eSRandall Stewart 	return (0);
666e57b2d0eSRandall Stewart }
667e57b2d0eSRandall Stewart #endif
668e57b2d0eSRandall Stewart 
6699ca874cfSHans Petter Selasky static inline void
6709ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum)
671e57b2d0eSRandall Stewart {
6729ca874cfSHans Petter Selasky 	uint32_t csum;
6736c5087a8SJack F Vogel 
6749ca874cfSHans Petter Selasky 	csum = 0xffff - *ptr + value;
6759ca874cfSHans Petter Selasky 	while (csum > 0xffff)
6769ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
6779ca874cfSHans Petter Selasky 	*ptr = value;
6789ca874cfSHans Petter Selasky 	*psum = csum;
67962b5b6ecSBjoern A. Zeeb }
68062b5b6ecSBjoern A. Zeeb 
6819ca874cfSHans Petter Selasky static uint16_t
6829ca874cfSHans Petter Selasky tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le,
6839ca874cfSHans Petter Selasky     uint16_t payload_len, uint16_t delta_sum)
6849ca874cfSHans Petter Selasky {
6859ca874cfSHans Petter Selasky 	uint32_t csum;
6869ca874cfSHans Petter Selasky 	uint16_t tlen;
6879ca874cfSHans Petter Selasky 	uint16_t temp[5] = {};
6889ca874cfSHans Petter Selasky 
6899ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
6909ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
6919ca874cfSHans Petter Selasky 		/* Compute new IPv4 length. */
6929ca874cfSHans Petter Selasky 		tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len;
6939ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
6949ca874cfSHans Petter Selasky 
6959ca874cfSHans Petter Selasky 		/* Subtract delta from current IPv4 checksum. */
6969ca874cfSHans Petter Selasky 		csum = pa->ip4->ip_sum + 0xffff - temp[0];
6979ca874cfSHans Petter Selasky 		while (csum > 0xffff)
6989ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
6999ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
7009ca874cfSHans Petter Selasky 		goto update_tcp_header;
7019ca874cfSHans Petter Selasky 
7029ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
7039ca874cfSHans Petter Selasky 		/* Compute new IPv6 length. */
7049ca874cfSHans Petter Selasky 		tlen = (pa->tcp->th_off << 2) + payload_len;
7059ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
7069ca874cfSHans Petter Selasky 		goto update_tcp_header;
7079ca874cfSHans Petter Selasky 
7089ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_UDP:
7099ca874cfSHans Petter Selasky 		/* Compute new IPv4 length. */
7109ca874cfSHans Petter Selasky 		tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len;
7119ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
7129ca874cfSHans Petter Selasky 
7139ca874cfSHans Petter Selasky 		/* Subtract delta from current IPv4 checksum. */
7149ca874cfSHans Petter Selasky 		csum = pa->ip4->ip_sum + 0xffff - temp[0];
7159ca874cfSHans Petter Selasky 		while (csum > 0xffff)
7169ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
7179ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
7189ca874cfSHans Petter Selasky 		goto update_udp_header;
7199ca874cfSHans Petter Selasky 
7209ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_UDP:
7219ca874cfSHans Petter Selasky 		/* Compute new IPv6 length. */
7229ca874cfSHans Petter Selasky 		tlen = sizeof(*pa->udp) + payload_len;
7239ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
7249ca874cfSHans Petter Selasky 		goto update_udp_header;
7259ca874cfSHans Petter Selasky 
72662b5b6ecSBjoern A. Zeeb 	default:
7279ca874cfSHans Petter Selasky 		return (0);
72862b5b6ecSBjoern A. Zeeb 	}
7299ca874cfSHans Petter Selasky 
7309ca874cfSHans Petter Selasky update_tcp_header:
7319ca874cfSHans Petter Selasky 	/* Compute current TCP header checksum. */
7329ca874cfSHans Petter Selasky 	temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp);
73362b5b6ecSBjoern A. Zeeb 
73462b5b6ecSBjoern A. Zeeb 	/* Incorporate the latest ACK into the TCP header. */
7359ca874cfSHans Petter Selasky 	pa->tcp->th_ack = le->ack_seq;
7369ca874cfSHans Petter Selasky 	pa->tcp->th_win = le->window;
7379ca874cfSHans Petter Selasky 
73862b5b6ecSBjoern A. Zeeb 	/* Incorporate latest timestamp into the TCP header. */
73962b5b6ecSBjoern A. Zeeb 	if (le->timestamp != 0) {
7406c5087a8SJack F Vogel 		uint32_t *ts_ptr;
7416c5087a8SJack F Vogel 
7429ca874cfSHans Petter Selasky 		ts_ptr = (uint32_t *)(pa->tcp + 1);
74362b5b6ecSBjoern A. Zeeb 		ts_ptr[1] = htonl(le->tsval);
74462b5b6ecSBjoern A. Zeeb 		ts_ptr[2] = le->tsecr;
74562b5b6ecSBjoern A. Zeeb 	}
7469ca874cfSHans Petter Selasky 
7479ca874cfSHans Petter Selasky 	/* Compute new TCP header checksum. */
7489ca874cfSHans Petter Selasky 	temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp);
7499ca874cfSHans Petter Selasky 
7509ca874cfSHans Petter Selasky 	/* Compute new TCP checksum. */
7519ca874cfSHans Petter Selasky 	csum = pa->tcp->th_sum + 0xffff - delta_sum +
7529ca874cfSHans Petter Selasky 	    0xffff - temp[0] + 0xffff - temp[3] + temp[2];
7539ca874cfSHans Petter Selasky 	while (csum > 0xffff)
7549ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
7559ca874cfSHans Petter Selasky 
7569ca874cfSHans Petter Selasky 	/* Assign new TCP checksum. */
7579ca874cfSHans Petter Selasky 	tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]);
7589ca874cfSHans Petter Selasky 
7599ca874cfSHans Petter Selasky 	/* Compute all modififications affecting next checksum. */
7609ca874cfSHans Petter Selasky 	csum = temp[0] + temp[1] + 0xffff - temp[2] +
7619ca874cfSHans Petter Selasky 	    temp[3] + temp[4] + delta_sum;
7629ca874cfSHans Petter Selasky 	while (csum > 0xffff)
7639ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
7649ca874cfSHans Petter Selasky 
7659ca874cfSHans Petter Selasky 	/* Return delta checksum to next stage, if any. */
7669ca874cfSHans Petter Selasky 	return (csum);
7679ca874cfSHans Petter Selasky 
7689ca874cfSHans Petter Selasky update_udp_header:
7699ca874cfSHans Petter Selasky 	tlen = sizeof(*pa->udp) + payload_len;
7709ca874cfSHans Petter Selasky 	/* Assign new UDP length and compute checksum delta. */
7719ca874cfSHans Petter Selasky 	tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]);
7729ca874cfSHans Petter Selasky 
7739ca874cfSHans Petter Selasky 	/* Check if there is a UDP checksum. */
7749ca874cfSHans Petter Selasky 	if (__predict_false(pa->udp->uh_sum != 0)) {
7759ca874cfSHans Petter Selasky 		/* Compute new UDP checksum. */
7769ca874cfSHans Petter Selasky 		csum = pa->udp->uh_sum + 0xffff - delta_sum +
7779ca874cfSHans Petter Selasky 		    0xffff - temp[0] + 0xffff - temp[2];
7789ca874cfSHans Petter Selasky 		while (csum > 0xffff)
7799ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
7809ca874cfSHans Petter Selasky 		/* Assign new UDP checksum. */
7819ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]);
782e57b2d0eSRandall Stewart 	}
7839ca874cfSHans Petter Selasky 
7849ca874cfSHans Petter Selasky 	/* Compute all modififications affecting next checksum. */
7859ca874cfSHans Petter Selasky 	csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum;
7869ca874cfSHans Petter Selasky 	while (csum > 0xffff)
7879ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
7889ca874cfSHans Petter Selasky 
7899ca874cfSHans Petter Selasky 	/* Return delta checksum to next stage, if any. */
7909ca874cfSHans Petter Selasky 	return (csum);
7919ca874cfSHans Petter Selasky }
7929ca874cfSHans Petter Selasky 
7939ca874cfSHans Petter Selasky static void
7949ca874cfSHans Petter Selasky tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le)
7959ca874cfSHans Petter Selasky {
7969ca874cfSHans Petter Selasky 	/* Check if we need to recompute any checksums. */
79724fe6643SRyan Stone 	if (le->needs_merge) {
7989ca874cfSHans Petter Selasky 		uint16_t csum;
7999ca874cfSHans Petter Selasky 
8009ca874cfSHans Petter Selasky 		switch (le->inner.data.lro_type) {
8019ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV4_TCP:
8029ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->inner, le,
8039ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len,
8049ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_csum);
8059ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->outer, NULL,
8069ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len +
8079ca874cfSHans Petter Selasky 			    le->inner.total_hdr_len, csum);
8089ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
8099ca874cfSHans Petter Selasky 			    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
8109ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_data = 0xffff;
81110a62eb1SHans Petter Selasky 			if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
81210a62eb1SHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
8139ca874cfSHans Petter Selasky 			break;
8149ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV6_TCP:
8159ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->inner, le,
8169ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len,
8179ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_csum);
8189ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->outer, NULL,
8199ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len +
8209ca874cfSHans Petter Selasky 			    le->inner.total_hdr_len, csum);
8219ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
8229ca874cfSHans Petter Selasky 			    CSUM_PSEUDO_HDR;
8239ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_data = 0xffff;
82410a62eb1SHans Petter Selasky 			if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
82510a62eb1SHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
8269ca874cfSHans Petter Selasky 			break;
8279ca874cfSHans Petter Selasky 		case LRO_TYPE_NONE:
8289ca874cfSHans Petter Selasky 			switch (le->outer.data.lro_type) {
8299ca874cfSHans Petter Selasky 			case LRO_TYPE_IPV4_TCP:
8309ca874cfSHans Petter Selasky 				csum = tcp_lro_update_checksum(&le->outer, le,
8319ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_len,
8329ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_csum);
8339ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
8349ca874cfSHans Petter Selasky 				    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
8359ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_data = 0xffff;
83610a62eb1SHans Petter Selasky 				if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
83710a62eb1SHans Petter Selasky 					le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
8389ca874cfSHans Petter Selasky 				break;
8399ca874cfSHans Petter Selasky 			case LRO_TYPE_IPV6_TCP:
8409ca874cfSHans Petter Selasky 				csum = tcp_lro_update_checksum(&le->outer, le,
8419ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_len,
8429ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_csum);
8439ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
8449ca874cfSHans Petter Selasky 				    CSUM_PSEUDO_HDR;
8459ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_data = 0xffff;
84610a62eb1SHans Petter Selasky 				if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
84710a62eb1SHans Petter Selasky 					le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
8489ca874cfSHans Petter Selasky 				break;
8499ca874cfSHans Petter Selasky 			default:
8509ca874cfSHans Petter Selasky 				break;
8519ca874cfSHans Petter Selasky 			}
8529ca874cfSHans Petter Selasky 			break;
8539ca874cfSHans Petter Selasky 		default:
8549ca874cfSHans Petter Selasky 			break;
8559ca874cfSHans Petter Selasky 		}
8569ca874cfSHans Petter Selasky 	}
8579ca874cfSHans Petter Selasky 
858e57b2d0eSRandall Stewart 	/*
859e57b2d0eSRandall Stewart 	 * Break any chain, this is not set to NULL on the singleton
860e57b2d0eSRandall Stewart 	 * case m_nextpkt points to m_head. Other case set them
861e57b2d0eSRandall Stewart 	 * m_nextpkt to NULL in push_and_replace.
862e57b2d0eSRandall Stewart 	 */
863e57b2d0eSRandall Stewart 	le->m_head->m_nextpkt = NULL;
8649ca874cfSHans Petter Selasky 	lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs;
865e57b2d0eSRandall Stewart 	(*lc->ifp->if_input)(lc->ifp, le->m_head);
86662b5b6ecSBjoern A. Zeeb }
8676c5087a8SJack F Vogel 
868e57b2d0eSRandall Stewart static void
8699ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le,
8709ca874cfSHans Petter Selasky     struct mbuf *m, struct tcphdr *th)
871e57b2d0eSRandall Stewart {
872e57b2d0eSRandall Stewart 	uint32_t *ts_ptr;
873e57b2d0eSRandall Stewart 	uint16_t tcp_data_len;
8749ca874cfSHans Petter Selasky 	uint16_t tcp_opt_len;
875e57b2d0eSRandall Stewart 
876e57b2d0eSRandall Stewart 	ts_ptr = (uint32_t *)(th + 1);
8779ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
8789ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
8799ca874cfSHans Petter Selasky 
8809ca874cfSHans Petter Selasky 	/* Check if there is a timestamp option. */
8819ca874cfSHans Petter Selasky 	if (tcp_opt_len == 0 ||
8829ca874cfSHans Petter Selasky 	    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
8839ca874cfSHans Petter Selasky 	    *ts_ptr != TCP_LRO_TS_OPTION)) {
8849ca874cfSHans Petter Selasky 		/* We failed to find the timestamp option. */
8859ca874cfSHans Petter Selasky 		le->timestamp = 0;
8869ca874cfSHans Petter Selasky 	} else {
887e57b2d0eSRandall Stewart 		le->timestamp = 1;
888e57b2d0eSRandall Stewart 		le->tsval = ntohl(*(ts_ptr + 1));
889e57b2d0eSRandall Stewart 		le->tsecr = *(ts_ptr + 2);
8909ca874cfSHans Petter Selasky 	}
8919ca874cfSHans Petter Selasky 
8929ca874cfSHans Petter Selasky 	tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
8939ca874cfSHans Petter Selasky 
8949ca874cfSHans Petter Selasky 	/* Pull out TCP sequence numbers and window size. */
895e57b2d0eSRandall Stewart 	le->next_seq = ntohl(th->th_seq) + tcp_data_len;
896e57b2d0eSRandall Stewart 	le->ack_seq = th->th_ack;
897e57b2d0eSRandall Stewart 	le->window = th->th_win;
8981ebf4607SRichard Scheffenegger 	le->flags = tcp_get_flags(th);
89924fe6643SRyan Stone 	le->needs_merge = 0;
9009ca874cfSHans Petter Selasky 
9019ca874cfSHans Petter Selasky 	/* Setup new data pointers. */
902e57b2d0eSRandall Stewart 	le->m_head = m;
903e57b2d0eSRandall Stewart 	le->m_tail = m_last(m);
904e57b2d0eSRandall Stewart }
905e57b2d0eSRandall Stewart 
906e57b2d0eSRandall Stewart static void
9079ca874cfSHans Petter Selasky tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
908e57b2d0eSRandall Stewart {
9099ca874cfSHans Petter Selasky 	struct lro_parser *pa;
9109ca874cfSHans Petter Selasky 
911e57b2d0eSRandall Stewart 	/*
9129ca874cfSHans Petter Selasky 	 * Push up the stack of the current entry
9139ca874cfSHans Petter Selasky 	 * and replace it with "m".
914e57b2d0eSRandall Stewart 	 */
915e57b2d0eSRandall Stewart 	struct mbuf *msave;
916e57b2d0eSRandall Stewart 
917e57b2d0eSRandall Stewart 	/* Grab off the next and save it */
918e57b2d0eSRandall Stewart 	msave = le->m_head->m_nextpkt;
919e57b2d0eSRandall Stewart 	le->m_head->m_nextpkt = NULL;
9209ca874cfSHans Petter Selasky 
9219ca874cfSHans Petter Selasky 	/* Now push out the old entry */
9229ca874cfSHans Petter Selasky 	tcp_flush_out_entry(lc, le);
9239ca874cfSHans Petter Selasky 
9249ca874cfSHans Petter Selasky 	/* Re-parse new header, should not fail. */
9259ca874cfSHans Petter Selasky 	pa = tcp_lro_parser(m, &le->outer, &le->inner, false);
9269ca874cfSHans Petter Selasky 	KASSERT(pa != NULL,
9279ca874cfSHans Petter Selasky 	    ("tcp_push_and_replace: LRO parser failed on m=%p\n", m));
9289ca874cfSHans Petter Selasky 
929e57b2d0eSRandall Stewart 	/*
9309ca874cfSHans Petter Selasky 	 * Now to replace the data properly in the entry
9319ca874cfSHans Petter Selasky 	 * we have to reset the TCP header and
932e57b2d0eSRandall Stewart 	 * other fields.
933e57b2d0eSRandall Stewart 	 */
9349ca874cfSHans Petter Selasky 	tcp_set_entry_to_mbuf(lc, le, m, pa->tcp);
9359ca874cfSHans Petter Selasky 
936e57b2d0eSRandall Stewart 	/* Restore the next list */
937e57b2d0eSRandall Stewart 	m->m_nextpkt = msave;
938e57b2d0eSRandall Stewart }
939e57b2d0eSRandall Stewart 
940e57b2d0eSRandall Stewart static void
94124fe6643SRyan Stone tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p)
9429ca874cfSHans Petter Selasky {
94324fe6643SRyan Stone 	struct mbuf *m;
9449ca874cfSHans Petter Selasky 	uint32_t csum;
9459ca874cfSHans Petter Selasky 
94624fe6643SRyan Stone 	m = le->m_head;
9479ca874cfSHans Petter Selasky 	if (m->m_pkthdr.lro_nsegs == 1) {
9489ca874cfSHans Petter Selasky 		/* Compute relative checksum. */
9499ca874cfSHans Petter Selasky 		csum = p->m_pkthdr.lro_tcp_d_csum;
9509ca874cfSHans Petter Selasky 	} else {
9519ca874cfSHans Petter Selasky 		/* Merge TCP data checksums. */
9529ca874cfSHans Petter Selasky 		csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum +
9539ca874cfSHans Petter Selasky 		    (uint32_t)p->m_pkthdr.lro_tcp_d_csum;
9549ca874cfSHans Petter Selasky 		while (csum > 0xffff)
9559ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
9569ca874cfSHans Petter Selasky 	}
9579ca874cfSHans Petter Selasky 
9589ca874cfSHans Petter Selasky 	/* Update various counters. */
9599ca874cfSHans Petter Selasky 	m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len;
9609ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_csum = csum;
9619ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len;
9629ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs;
96324fe6643SRyan Stone 	le->needs_merge = 1;
9649ca874cfSHans Petter Selasky }
9659ca874cfSHans Petter Selasky 
9669ca874cfSHans Petter Selasky static void
9679ca874cfSHans Petter Selasky tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le)
968e57b2d0eSRandall Stewart {
969e57b2d0eSRandall Stewart 	/*
970e57b2d0eSRandall Stewart 	 * Walk through the mbuf chain we
971e57b2d0eSRandall Stewart 	 * have on tap and compress/condense
972e57b2d0eSRandall Stewart 	 * as required.
973e57b2d0eSRandall Stewart 	 */
974e57b2d0eSRandall Stewart 	uint32_t *ts_ptr;
975e57b2d0eSRandall Stewart 	struct mbuf *m;
976e57b2d0eSRandall Stewart 	struct tcphdr *th;
9779ca874cfSHans Petter Selasky 	uint32_t tcp_data_len_total;
9789ca874cfSHans Petter Selasky 	uint32_t tcp_data_seg_total;
9799ca874cfSHans Petter Selasky 	uint16_t tcp_data_len;
9809ca874cfSHans Petter Selasky 	uint16_t tcp_opt_len;
981e57b2d0eSRandall Stewart 
982e57b2d0eSRandall Stewart 	/*
983e57b2d0eSRandall Stewart 	 * First we must check the lead (m_head)
984e57b2d0eSRandall Stewart 	 * we must make sure that it is *not*
985e57b2d0eSRandall Stewart 	 * something that should be sent up
986e57b2d0eSRandall Stewart 	 * right away (sack etc).
987e57b2d0eSRandall Stewart 	 */
988e57b2d0eSRandall Stewart again:
989e57b2d0eSRandall Stewart 	m = le->m_head->m_nextpkt;
990e57b2d0eSRandall Stewart 	if (m == NULL) {
9919ca874cfSHans Petter Selasky 		/* Just one left. */
992e57b2d0eSRandall Stewart 		return;
993e57b2d0eSRandall Stewart 	}
9949ca874cfSHans Petter Selasky 
9959ca874cfSHans Petter Selasky 	th = tcp_lro_get_th(m);
9969ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
9979ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
998e57b2d0eSRandall Stewart 	ts_ptr = (uint32_t *)(th + 1);
9999ca874cfSHans Petter Selasky 
10009ca874cfSHans Petter Selasky 	if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
10019ca874cfSHans Petter Selasky 	    *ts_ptr != TCP_LRO_TS_OPTION)) {
1002e57b2d0eSRandall Stewart 		/*
1003e57b2d0eSRandall Stewart 		 * Its not the timestamp. We can't
1004e57b2d0eSRandall Stewart 		 * use this guy as the head.
1005e57b2d0eSRandall Stewart 		 */
1006e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
10079ca874cfSHans Petter Selasky 		tcp_push_and_replace(lc, le, m);
1008e57b2d0eSRandall Stewart 		goto again;
1009e57b2d0eSRandall Stewart 	}
10101ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
1011e57b2d0eSRandall Stewart 		/*
10121dadeab3SGordon Bergling 		 * Make sure that previously seen segments/ACKs are delivered
1013e57b2d0eSRandall Stewart 		 * before this segment, e.g. FIN.
1014e57b2d0eSRandall Stewart 		 */
1015e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
10169ca874cfSHans Petter Selasky 		tcp_push_and_replace(lc, le, m);
1017e57b2d0eSRandall Stewart 		goto again;
1018e57b2d0eSRandall Stewart 	}
1019e57b2d0eSRandall Stewart 	while((m = le->m_head->m_nextpkt) != NULL) {
1020e57b2d0eSRandall Stewart 		/*
1021e57b2d0eSRandall Stewart 		 * condense m into le, first
1022e57b2d0eSRandall Stewart 		 * pull m out of the list.
1023e57b2d0eSRandall Stewart 		 */
1024e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
1025e57b2d0eSRandall Stewart 		m->m_nextpkt = NULL;
1026e57b2d0eSRandall Stewart 		/* Setup my data */
10279ca874cfSHans Petter Selasky 		tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
10289ca874cfSHans Petter Selasky 		th = tcp_lro_get_th(m);
1029e57b2d0eSRandall Stewart 		ts_ptr = (uint32_t *)(th + 1);
10309ca874cfSHans Petter Selasky 		tcp_opt_len = (th->th_off << 2);
10319ca874cfSHans Petter Selasky 		tcp_opt_len -= sizeof(*th);
10329ca874cfSHans Petter Selasky 		tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len;
10339ca874cfSHans Petter Selasky 		tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs;
10349ca874cfSHans Petter Selasky 
10359ca874cfSHans Petter Selasky 		if (tcp_data_seg_total >= lc->lro_ackcnt_lim ||
10369ca874cfSHans Petter Selasky 		    tcp_data_len_total >= lc->lro_length_lim) {
1037e57b2d0eSRandall Stewart 			/* Flush now if appending will result in overflow. */
10389ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1039e57b2d0eSRandall Stewart 			goto again;
1040e57b2d0eSRandall Stewart 		}
10419ca874cfSHans Petter Selasky 		if (tcp_opt_len != 0 &&
10429ca874cfSHans Petter Selasky 		    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
10439ca874cfSHans Petter Selasky 		    *ts_ptr != TCP_LRO_TS_OPTION)) {
1044e57b2d0eSRandall Stewart 			/*
1045e57b2d0eSRandall Stewart 			 * Maybe a sack in the new one? We need to
1046e57b2d0eSRandall Stewart 			 * start all over after flushing the
1047e57b2d0eSRandall Stewart 			 * current le. We will go up to the beginning
1048e57b2d0eSRandall Stewart 			 * and flush it (calling the replace again possibly
1049e57b2d0eSRandall Stewart 			 * or just returning).
1050e57b2d0eSRandall Stewart 			 */
10519ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1052e57b2d0eSRandall Stewart 			goto again;
1053e57b2d0eSRandall Stewart 		}
10541ebf4607SRichard Scheffenegger 		if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
10559ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1056e57b2d0eSRandall Stewart 			goto again;
1057e57b2d0eSRandall Stewart 		}
10589ca874cfSHans Petter Selasky 		if (tcp_opt_len != 0) {
1059e57b2d0eSRandall Stewart 			uint32_t tsval = ntohl(*(ts_ptr + 1));
1060e57b2d0eSRandall Stewart 			/* Make sure timestamp values are increasing. */
1061e57b2d0eSRandall Stewart 			if (TSTMP_GT(le->tsval, tsval))  {
10629ca874cfSHans Petter Selasky 				tcp_push_and_replace(lc, le, m);
1063e57b2d0eSRandall Stewart 				goto again;
1064e57b2d0eSRandall Stewart 			}
1065e57b2d0eSRandall Stewart 			le->tsval = tsval;
1066e57b2d0eSRandall Stewart 			le->tsecr = *(ts_ptr + 2);
1067e57b2d0eSRandall Stewart 		}
1068e57b2d0eSRandall Stewart 		/* Try to append the new segment. */
1069e57b2d0eSRandall Stewart 		if (__predict_false(ntohl(th->th_seq) != le->next_seq ||
10701ebf4607SRichard Scheffenegger 				    ((tcp_get_flags(th) & TH_ACK) !=
10713284f492SRyan Stone 				      (le->flags & TH_ACK)) ||
1072e57b2d0eSRandall Stewart 				    (tcp_data_len == 0 &&
1073e57b2d0eSRandall Stewart 				     le->ack_seq == th->th_ack &&
1074e57b2d0eSRandall Stewart 				     le->window == th->th_win))) {
10753284f492SRyan Stone 			/* Out of order packet, non-ACK + ACK or dup ACK. */
10769ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1077e57b2d0eSRandall Stewart 			goto again;
1078e57b2d0eSRandall Stewart 		}
10799ca874cfSHans Petter Selasky 		if (tcp_data_len != 0 ||
10809ca874cfSHans Petter Selasky 		    SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1081e57b2d0eSRandall Stewart 			le->next_seq += tcp_data_len;
1082e57b2d0eSRandall Stewart 			le->ack_seq = th->th_ack;
1083e57b2d0eSRandall Stewart 			le->window = th->th_win;
108424fe6643SRyan Stone 			le->needs_merge = 1;
1085e57b2d0eSRandall Stewart 		} else if (th->th_ack == le->ack_seq) {
108624fe6643SRyan Stone 			if (WIN_GT(th->th_win, le->window)) {
108724fe6643SRyan Stone 				le->window = th->th_win;
108824fe6643SRyan Stone 				le->needs_merge = 1;
108924fe6643SRyan Stone 			}
1090e57b2d0eSRandall Stewart 		}
10919ca874cfSHans Petter Selasky 
1092e57b2d0eSRandall Stewart 		if (tcp_data_len == 0) {
1093e57b2d0eSRandall Stewart 			m_freem(m);
1094e57b2d0eSRandall Stewart 			continue;
1095e57b2d0eSRandall Stewart 		}
10969ca874cfSHans Petter Selasky 
10979ca874cfSHans Petter Selasky 		/* Merge TCP data checksum and length to head mbuf. */
109824fe6643SRyan Stone 		tcp_lro_mbuf_append_pkthdr(le, m);
10999ca874cfSHans Petter Selasky 
1100e57b2d0eSRandall Stewart 		/*
1101e57b2d0eSRandall Stewart 		 * Adjust the mbuf so that m_data points to the first byte of
1102e57b2d0eSRandall Stewart 		 * the ULP payload.  Adjust the mbuf to avoid complications and
1103e57b2d0eSRandall Stewart 		 * append new segment to existing mbuf chain.
1104e57b2d0eSRandall Stewart 		 */
1105e57b2d0eSRandall Stewart 		m_adj(m, m->m_pkthdr.len - tcp_data_len);
1106e57b2d0eSRandall Stewart 		m_demote_pkthdr(m);
1107e57b2d0eSRandall Stewart 		le->m_tail->m_next = m;
1108e57b2d0eSRandall Stewart 		le->m_tail = m_last(m);
1109e57b2d0eSRandall Stewart 	}
1110e57b2d0eSRandall Stewart }
1111e57b2d0eSRandall Stewart 
111269a34e8dSRandall Stewart void
111369a34e8dSRandall Stewart tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
111469a34e8dSRandall Stewart {
111569a34e8dSRandall Stewart 
1116*2c6fc36aSGleb Smirnoff 	/* Only optimise if there are multiple packets waiting. */
1117dc6ab77dSMichael Tuexen 	NET_EPOCH_ASSERT();
1118*2c6fc36aSGleb Smirnoff 	if (tcp_lro_flush_tcphpts == NULL ||
1119*2c6fc36aSGleb Smirnoff 	    tcp_lro_flush_tcphpts(lc, le) != 0) {
11209ca874cfSHans Petter Selasky 		tcp_lro_condense(lc, le);
11219ca874cfSHans Petter Selasky 		tcp_flush_out_entry(lc, le);
1122e57b2d0eSRandall Stewart 	}
112362b5b6ecSBjoern A. Zeeb 	lc->lro_flushed++;
112462b5b6ecSBjoern A. Zeeb 	bzero(le, sizeof(*le));
11251ea44822SSepherosa Ziehau 	LIST_INSERT_HEAD(&lc->lro_free, le, next);
112662b5b6ecSBjoern A. Zeeb }
11276c5087a8SJack F Vogel 
1128fc271df3SHans Petter Selasky #define	tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1))
1129e936121dSHans Petter Selasky 
1130fc271df3SHans Petter Selasky /*
1131fc271df3SHans Petter Selasky  * The tcp_lro_sort() routine is comparable to qsort(), except it has
1132fc271df3SHans Petter Selasky  * a worst case complexity limit of O(MIN(N,64)*N), where N is the
1133fc271df3SHans Petter Selasky  * number of elements to sort and 64 is the number of sequence bits
1134fc271df3SHans Petter Selasky  * available. The algorithm is bit-slicing the 64-bit sequence number,
1135fc271df3SHans Petter Selasky  * sorting one bit at a time from the most significant bit until the
1136ec668905SHans Petter Selasky  * least significant one, skipping the constant bits. This is
1137ec668905SHans Petter Selasky  * typically called a radix sort.
1138fc271df3SHans Petter Selasky  */
1139fc271df3SHans Petter Selasky static void
1140fc271df3SHans Petter Selasky tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size)
1141fc271df3SHans Petter Selasky {
1142fc271df3SHans Petter Selasky 	struct lro_mbuf_sort temp;
1143fc271df3SHans Petter Selasky 	uint64_t ones;
1144fc271df3SHans Petter Selasky 	uint64_t zeros;
1145fc271df3SHans Petter Selasky 	uint32_t x;
1146fc271df3SHans Petter Selasky 	uint32_t y;
1147e936121dSHans Petter Selasky 
1148fc271df3SHans Petter Selasky repeat:
1149ec668905SHans Petter Selasky 	/* for small arrays insertion sort is faster */
1150fc271df3SHans Petter Selasky 	if (size <= 12) {
1151ec668905SHans Petter Selasky 		for (x = 1; x < size; x++) {
1152fc271df3SHans Petter Selasky 			temp = parray[x];
1153ec668905SHans Petter Selasky 			for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--)
1154ec668905SHans Petter Selasky 				parray[y] = parray[y - 1];
1155fc271df3SHans Petter Selasky 			parray[y] = temp;
1156fc271df3SHans Petter Selasky 		}
1157fc271df3SHans Petter Selasky 		return;
1158fc271df3SHans Petter Selasky 	}
1159e936121dSHans Petter Selasky 
1160fc271df3SHans Petter Selasky 	/* compute sequence bits which are constant */
1161fc271df3SHans Petter Selasky 	ones = 0;
1162fc271df3SHans Petter Selasky 	zeros = 0;
1163fc271df3SHans Petter Selasky 	for (x = 0; x != size; x++) {
1164fc271df3SHans Petter Selasky 		ones |= parray[x].seq;
1165fc271df3SHans Petter Selasky 		zeros |= ~parray[x].seq;
1166fc271df3SHans Petter Selasky 	}
1167fc271df3SHans Petter Selasky 
1168fc271df3SHans Petter Selasky 	/* compute bits which are not constant into "ones" */
1169fc271df3SHans Petter Selasky 	ones &= zeros;
1170fc271df3SHans Petter Selasky 	if (ones == 0)
1171fc271df3SHans Petter Selasky 		return;
1172fc271df3SHans Petter Selasky 
1173fc271df3SHans Petter Selasky 	/* pick the most significant bit which is not constant */
1174fc271df3SHans Petter Selasky 	ones = tcp_lro_msb_64(ones);
1175fc271df3SHans Petter Selasky 
1176fc271df3SHans Petter Selasky 	/*
1177fc271df3SHans Petter Selasky 	 * Move entries having cleared sequence bits to the beginning
1178fc271df3SHans Petter Selasky 	 * of the array:
1179fc271df3SHans Petter Selasky 	 */
1180fc271df3SHans Petter Selasky 	for (x = y = 0; y != size; y++) {
1181fc271df3SHans Petter Selasky 		/* skip set bits */
1182fc271df3SHans Petter Selasky 		if (parray[y].seq & ones)
1183fc271df3SHans Petter Selasky 			continue;
1184fc271df3SHans Petter Selasky 		/* swap entries */
1185fc271df3SHans Petter Selasky 		temp = parray[x];
1186fc271df3SHans Petter Selasky 		parray[x] = parray[y];
1187fc271df3SHans Petter Selasky 		parray[y] = temp;
1188fc271df3SHans Petter Selasky 		x++;
1189fc271df3SHans Petter Selasky 	}
1190fc271df3SHans Petter Selasky 
1191fc271df3SHans Petter Selasky 	KASSERT(x != 0 && x != size, ("Memory is corrupted\n"));
1192fc271df3SHans Petter Selasky 
1193fc271df3SHans Petter Selasky 	/* sort zeros */
1194fc271df3SHans Petter Selasky 	tcp_lro_sort(parray, x);
1195fc271df3SHans Petter Selasky 
1196fc271df3SHans Petter Selasky 	/* sort ones */
1197fc271df3SHans Petter Selasky 	parray += x;
1198fc271df3SHans Petter Selasky 	size -= x;
1199fc271df3SHans Petter Selasky 	goto repeat;
1200e936121dSHans Petter Selasky }
1201e936121dSHans Petter Selasky 
1202e936121dSHans Petter Selasky void
1203e936121dSHans Petter Selasky tcp_lro_flush_all(struct lro_ctrl *lc)
1204e936121dSHans Petter Selasky {
1205fc271df3SHans Petter Selasky 	uint64_t seq;
1206fc271df3SHans Petter Selasky 	uint64_t nseq;
1207e936121dSHans Petter Selasky 	unsigned x;
1208e936121dSHans Petter Selasky 
1209dc6ab77dSMichael Tuexen 	NET_EPOCH_ASSERT();
1210e936121dSHans Petter Selasky 	/* check if no mbufs to flush */
12116dd38b87SSepherosa Ziehau 	if (lc->lro_mbuf_count == 0)
1212e936121dSHans Petter Selasky 		goto done;
1213d7955cc0SRandall Stewart 	if (lc->lro_cpu_is_set == 0) {
1214d7955cc0SRandall Stewart 		if (lc->lro_last_cpu == curcpu) {
1215d7955cc0SRandall Stewart 			lc->lro_cnt_of_same_cpu++;
1216d7955cc0SRandall Stewart 			/* Have we reached the threshold to declare a cpu? */
1217d7955cc0SRandall Stewart 			if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh)
1218d7955cc0SRandall Stewart 				lc->lro_cpu_is_set = 1;
1219d7955cc0SRandall Stewart 		} else {
1220d7955cc0SRandall Stewart 			lc->lro_last_cpu = curcpu;
1221d7955cc0SRandall Stewart 			lc->lro_cnt_of_same_cpu = 0;
1222d7955cc0SRandall Stewart 		}
1223d7955cc0SRandall Stewart 	}
1224a9b66dbdSHans Petter Selasky 	CURVNET_SET(lc->ifp->if_vnet);
1225a9b66dbdSHans Petter Selasky 
12269ca874cfSHans Petter Selasky 	/* get current time */
1227b45daaeaSRandall Stewart 	binuptime(&lc->lro_last_queue_time);
12289ca874cfSHans Petter Selasky 
1229e936121dSHans Petter Selasky 	/* sort all mbufs according to stream */
1230fc271df3SHans Petter Selasky 	tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count);
1231e936121dSHans Petter Selasky 
1232e936121dSHans Petter Selasky 	/* input data into LRO engine, stream by stream */
1233fc271df3SHans Petter Selasky 	seq = 0;
1234e936121dSHans Petter Selasky 	for (x = 0; x != lc->lro_mbuf_count; x++) {
1235e936121dSHans Petter Selasky 		struct mbuf *mb;
1236e936121dSHans Petter Selasky 
1237fc271df3SHans Petter Selasky 		/* get mbuf */
1238fc271df3SHans Petter Selasky 		mb = lc->lro_mbuf_data[x].mb;
1239fc271df3SHans Petter Selasky 
1240fc271df3SHans Petter Selasky 		/* get sequence number, masking away the packet index */
1241fc271df3SHans Petter Selasky 		nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24);
1242e936121dSHans Petter Selasky 
1243e936121dSHans Petter Selasky 		/* check for new stream */
1244fc271df3SHans Petter Selasky 		if (seq != nseq) {
1245fc271df3SHans Petter Selasky 			seq = nseq;
1246e936121dSHans Petter Selasky 
1247e936121dSHans Petter Selasky 			/* flush active streams */
12486dd38b87SSepherosa Ziehau 			tcp_lro_rx_done(lc);
1249e936121dSHans Petter Selasky 		}
1250fc271df3SHans Petter Selasky 
1251e936121dSHans Petter Selasky 		/* add packet to LRO engine */
12529ca874cfSHans Petter Selasky 		if (tcp_lro_rx_common(lc, mb, 0, false) != 0) {
12534e0ce82bSRandall Stewart  			/* Flush anything we have acummulated */
12544e0ce82bSRandall Stewart  			tcp_lro_flush_active(lc);
1255e936121dSHans Petter Selasky 			/* input packet to network layer */
1256e936121dSHans Petter Selasky 			(*lc->ifp->if_input)(lc->ifp, mb);
1257e936121dSHans Petter Selasky 			lc->lro_queued++;
1258e936121dSHans Petter Selasky 			lc->lro_flushed++;
1259e936121dSHans Petter Selasky 		}
1260e936121dSHans Petter Selasky 	}
1261a9b66dbdSHans Petter Selasky 	CURVNET_RESTORE();
1262e936121dSHans Petter Selasky done:
1263e936121dSHans Petter Selasky 	/* flush active streams */
12646dd38b87SSepherosa Ziehau 	tcp_lro_rx_done(lc);
1265*2c6fc36aSGleb Smirnoff 	if (tcp_hpts_softclock != NULL)
1266*2c6fc36aSGleb Smirnoff 		tcp_hpts_softclock();
1267e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
126862b5b6ecSBjoern A. Zeeb }
12696c5087a8SJack F Vogel 
12709ca874cfSHans Petter Selasky static struct lro_head *
12719ca874cfSHans Petter Selasky tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser)
12729ca874cfSHans Petter Selasky {
12739ca874cfSHans Petter Selasky 	u_long hash;
12749ca874cfSHans Petter Selasky 
12759ca874cfSHans Petter Selasky 	if (M_HASHTYPE_ISHASH(m)) {
12769ca874cfSHans Petter Selasky 		hash = m->m_pkthdr.flowid;
12779ca874cfSHans Petter Selasky 	} else {
12789ca874cfSHans Petter Selasky 		for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++)
12799ca874cfSHans Petter Selasky 			hash += parser->data.raw[i];
128069a34e8dSRandall Stewart 	}
12819ca874cfSHans Petter Selasky 	return (&lc->lro_hash[hash % lc->lro_hashsz]);
12829ca874cfSHans Petter Selasky }
128369a34e8dSRandall Stewart 
128405cde7efSSepherosa Ziehau static int
12859ca874cfSHans Petter Selasky tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash)
128662b5b6ecSBjoern A. Zeeb {
12879ca874cfSHans Petter Selasky 	struct lro_parser pi;	/* inner address data */
12889ca874cfSHans Petter Selasky 	struct lro_parser po;	/* outer address data */
12899ca874cfSHans Petter Selasky 	struct lro_parser *pa;	/* current parser for TCP stream */
129062b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
129105cde7efSSepherosa Ziehau 	struct lro_head *bucket;
12929ca874cfSHans Petter Selasky 	struct tcphdr *th;
12939ca874cfSHans Petter Selasky 	int tcp_data_len;
12949ca874cfSHans Petter Selasky 	int tcp_opt_len;
12959ca874cfSHans Petter Selasky 	int error;
12969ca874cfSHans Petter Selasky 	uint16_t tcp_data_sum;
12976c5087a8SJack F Vogel 
12989ca874cfSHans Petter Selasky #ifdef INET
12999ca874cfSHans Petter Selasky 	/* Quickly decide if packet cannot be LRO'ed */
13009ca874cfSHans Petter Selasky 	if (__predict_false(V_ipforwarding != 0))
13019ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
13029ca874cfSHans Petter Selasky #endif
13039ca874cfSHans Petter Selasky #ifdef INET6
13049ca874cfSHans Petter Selasky 	/* Quickly decide if packet cannot be LRO'ed */
13059ca874cfSHans Petter Selasky 	if (__predict_false(V_ip6_forwarding != 0))
13069ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
13079ca874cfSHans Petter Selasky #endif
1308ca1a7e10SRandall Stewart 	if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
1309ca1a7e10SRandall Stewart 	     ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
1310ca1a7e10SRandall Stewart 	    (m->m_pkthdr.csum_data != 0xffff)) {
1311ca1a7e10SRandall Stewart 		/*
1312ca1a7e10SRandall Stewart 		 * The checksum either did not have hardware offload
1313ca1a7e10SRandall Stewart 		 * or it was a bad checksum. We can't LRO such
1314ca1a7e10SRandall Stewart 		 * a packet.
1315ca1a7e10SRandall Stewart 		 */
1316ca1a7e10SRandall Stewart 		counter_u64_add(tcp_bad_csums, 1);
1317ca1a7e10SRandall Stewart 		return (TCP_LRO_CANNOT);
1318ca1a7e10SRandall Stewart 	}
131962b5b6ecSBjoern A. Zeeb 	/* We expect a contiguous header [eh, ip, tcp]. */
13209ca874cfSHans Petter Selasky 	pa = tcp_lro_parser(m, &po, &pi, true);
13219ca874cfSHans Petter Selasky 	if (__predict_false(pa == NULL))
13229ca874cfSHans Petter Selasky 		return (TCP_LRO_NOT_SUPPORTED);
13239ca874cfSHans Petter Selasky 
13249ca874cfSHans Petter Selasky 	/* We don't expect any padding. */
13259ca874cfSHans Petter Selasky 	error = tcp_lro_trim_mbuf_chain(m, pa);
13269ca874cfSHans Petter Selasky 	if (__predict_false(error != 0))
13279ca874cfSHans Petter Selasky 		return (error);
13289ca874cfSHans Petter Selasky 
13299ca874cfSHans Petter Selasky #ifdef INET
13309ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
13319ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
13329ca874cfSHans Petter Selasky 		error = tcp_lro_rx_ipv4(lc, m, pa->ip4);
13339ca874cfSHans Petter Selasky 		if (__predict_false(error != 0))
13349ca874cfSHans Petter Selasky 			return (error);
13359ca874cfSHans Petter Selasky 		break;
13369ca874cfSHans Petter Selasky 	default:
13379ca874cfSHans Petter Selasky 		break;
13389ca874cfSHans Petter Selasky 	}
13399ca874cfSHans Petter Selasky #endif
13409ca874cfSHans Petter Selasky 	/* If no hardware or arrival stamp on the packet add timestamp */
1341e57b2d0eSRandall Stewart 	if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) {
1342b45daaeaSRandall Stewart 		m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time);
1343e57b2d0eSRandall Stewart 		m->m_flags |= M_TSTMP_LRO;
1344e57b2d0eSRandall Stewart 	}
13456c5087a8SJack F Vogel 
13469ca874cfSHans Petter Selasky 	/* Get pointer to TCP header. */
13479ca874cfSHans Petter Selasky 	th = pa->tcp;
13489ca874cfSHans Petter Selasky 
13499ca874cfSHans Petter Selasky 	/* Don't process SYN packets. */
13501ebf4607SRichard Scheffenegger 	if (__predict_false(tcp_get_flags(th) & TH_SYN))
135162b5b6ecSBjoern A. Zeeb 		return (TCP_LRO_CANNOT);
135262b5b6ecSBjoern A. Zeeb 
13539ca874cfSHans Petter Selasky 	/* Get total TCP header length and compute payload length. */
13549ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
13559ca874cfSHans Petter Selasky 	tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th -
13569ca874cfSHans Petter Selasky 	    (uint8_t *)m->m_data) - tcp_opt_len;
13579ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
13589ca874cfSHans Petter Selasky 
13599ca874cfSHans Petter Selasky 	/* Don't process invalid TCP headers. */
13609ca874cfSHans Petter Selasky 	if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0))
136162b5b6ecSBjoern A. Zeeb 		return (TCP_LRO_CANNOT);
13629ca874cfSHans Petter Selasky 
13639ca874cfSHans Petter Selasky 	/* Compute TCP data only checksum. */
13649ca874cfSHans Petter Selasky 	if (tcp_data_len == 0)
13659ca874cfSHans Petter Selasky 		tcp_data_sum = 0;	/* no data, no checksum */
13669ca874cfSHans Petter Selasky 	else if (__predict_false(csum != 0))
13679ca874cfSHans Petter Selasky 		tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum);
13689ca874cfSHans Petter Selasky 	else
13699ca874cfSHans Petter Selasky 		tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum);
13709ca874cfSHans Petter Selasky 
13719ca874cfSHans Petter Selasky 	/* Save TCP info in mbuf. */
13729ca874cfSHans Petter Selasky 	m->m_nextpkt = NULL;
13739ca874cfSHans Petter Selasky 	m->m_pkthdr.rcvif = lc->ifp;
13749ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum;
13759ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_len = tcp_data_len;
13769ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data);
13779ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_nsegs = 1;
13789ca874cfSHans Petter Selasky 
13799ca874cfSHans Petter Selasky 	/* Get hash bucket. */
138005cde7efSSepherosa Ziehau 	if (!use_hash) {
138105cde7efSSepherosa Ziehau 		bucket = &lc->lro_hash[0];
138205cde7efSSepherosa Ziehau 	} else {
13839ca874cfSHans Petter Selasky 		bucket = tcp_lro_rx_get_bucket(lc, m, pa);
138405cde7efSSepherosa Ziehau 	}
138505cde7efSSepherosa Ziehau 
138662b5b6ecSBjoern A. Zeeb 	/* Try to find a matching previous segment. */
138705cde7efSSepherosa Ziehau 	LIST_FOREACH(le, bucket, hash_next) {
13889ca874cfSHans Petter Selasky 		/* Compare addresses and ports. */
13899ca874cfSHans Petter Selasky 		if (lro_address_compare(&po.data, &le->outer.data) == false ||
13909ca874cfSHans Petter Selasky 		    lro_address_compare(&pi.data, &le->inner.data) == false)
139162b5b6ecSBjoern A. Zeeb 			continue;
13929ca874cfSHans Petter Selasky 
13939ca874cfSHans Petter Selasky 		/* Check if no data and old ACK. */
13949ca874cfSHans Petter Selasky 		if (tcp_data_len == 0 &&
13959ca874cfSHans Petter Selasky 		    SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1396d7fb35d1SSean Bruno 			m_freem(m);
1397d7fb35d1SSean Bruno 			return (0);
1398d7fb35d1SSean Bruno 		}
139969a34e8dSRandall Stewart 
14009ca874cfSHans Petter Selasky 		/* Mark "m" in the last spot. */
1401e57b2d0eSRandall Stewart 		le->m_last_mbuf->m_nextpkt = m;
14029ca874cfSHans Petter Selasky 		/* Now set the tail to "m". */
1403e57b2d0eSRandall Stewart 		le->m_last_mbuf = m;
140462b5b6ecSBjoern A. Zeeb 		return (0);
14056c5087a8SJack F Vogel 	}
14069ca874cfSHans Petter Selasky 
140762b5b6ecSBjoern A. Zeeb 	/* Try to find an empty slot. */
14081ea44822SSepherosa Ziehau 	if (LIST_EMPTY(&lc->lro_free))
1409489f0c3cSSepherosa Ziehau 		return (TCP_LRO_NO_ENTRIES);
141062b5b6ecSBjoern A. Zeeb 
141162b5b6ecSBjoern A. Zeeb 	/* Start a new segment chain. */
14121ea44822SSepherosa Ziehau 	le = LIST_FIRST(&lc->lro_free);
14131ea44822SSepherosa Ziehau 	LIST_REMOVE(le, next);
141405cde7efSSepherosa Ziehau 	tcp_lro_active_insert(lc, bucket, le);
141562b5b6ecSBjoern A. Zeeb 
14169ca874cfSHans Petter Selasky 	/* Make sure the headers are set. */
14179ca874cfSHans Petter Selasky 	le->inner = pi;
14189ca874cfSHans Petter Selasky 	le->outer = po;
141962b5b6ecSBjoern A. Zeeb 
14209ca874cfSHans Petter Selasky 	/* Store time this entry was allocated. */
14219ca874cfSHans Petter Selasky 	le->alloc_time = lc->lro_last_queue_time;
142269a34e8dSRandall Stewart 
14239ca874cfSHans Petter Selasky 	tcp_set_entry_to_mbuf(lc, le, m, th);
142469a34e8dSRandall Stewart 
14259ca874cfSHans Petter Selasky 	/* Now set the tail to "m". */
1426e57b2d0eSRandall Stewart 	le->m_last_mbuf = m;
14279ca874cfSHans Petter Selasky 
142862b5b6ecSBjoern A. Zeeb 	return (0);
142962b5b6ecSBjoern A. Zeeb }
143062b5b6ecSBjoern A. Zeeb 
143105cde7efSSepherosa Ziehau int
143205cde7efSSepherosa Ziehau tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
143305cde7efSSepherosa Ziehau {
14349ca874cfSHans Petter Selasky 	int error;
143505cde7efSSepherosa Ziehau 
1436ca1a7e10SRandall Stewart 	if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
1437ca1a7e10SRandall Stewart 	     ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
1438ca1a7e10SRandall Stewart 	    (m->m_pkthdr.csum_data != 0xffff)) {
1439ca1a7e10SRandall Stewart 		/*
1440ca1a7e10SRandall Stewart 		 * The checksum either did not have hardware offload
1441ca1a7e10SRandall Stewart 		 * or it was a bad checksum. We can't LRO such
1442ca1a7e10SRandall Stewart 		 * a packet.
1443ca1a7e10SRandall Stewart 		 */
1444ca1a7e10SRandall Stewart 		counter_u64_add(tcp_bad_csums, 1);
1445ca1a7e10SRandall Stewart 		return (TCP_LRO_CANNOT);
1446ca1a7e10SRandall Stewart 	}
14479ca874cfSHans Petter Selasky 	/* get current time */
1448b45daaeaSRandall Stewart 	binuptime(&lc->lro_last_queue_time);
14499ca874cfSHans Petter Selasky 	CURVNET_SET(lc->ifp->if_vnet);
14509ca874cfSHans Petter Selasky 	error = tcp_lro_rx_common(lc, m, csum, true);
14514e0ce82bSRandall Stewart 	if (__predict_false(error != 0)) {
14524e0ce82bSRandall Stewart 		/*
14534e0ce82bSRandall Stewart 		 * Flush anything we have acummulated
14544e0ce82bSRandall Stewart 		 * ahead of this packet that can't
14554e0ce82bSRandall Stewart 		 * be LRO'd. This preserves order.
14564e0ce82bSRandall Stewart 		 */
14574e0ce82bSRandall Stewart 		tcp_lro_flush_active(lc);
14584e0ce82bSRandall Stewart 	}
14599ca874cfSHans Petter Selasky 	CURVNET_RESTORE();
14609ca874cfSHans Petter Selasky 
14619ca874cfSHans Petter Selasky 	return (error);
146205cde7efSSepherosa Ziehau }
146305cde7efSSepherosa Ziehau 
1464e936121dSHans Petter Selasky void
1465e936121dSHans Petter Selasky tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
1466e936121dSHans Petter Selasky {
1467dc6ab77dSMichael Tuexen 	NET_EPOCH_ASSERT();
1468e936121dSHans Petter Selasky 	/* sanity checks */
1469e936121dSHans Petter Selasky 	if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
1470e936121dSHans Petter Selasky 	    lc->lro_mbuf_max == 0)) {
1471e936121dSHans Petter Selasky 		/* packet drop */
1472e936121dSHans Petter Selasky 		m_freem(mb);
1473e936121dSHans Petter Selasky 		return;
1474e936121dSHans Petter Selasky 	}
1475e936121dSHans Petter Selasky 
1476e936121dSHans Petter Selasky 	/* check if packet is not LRO capable */
1477ca1a7e10SRandall Stewart 	if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
1478e936121dSHans Petter Selasky 		/* input packet to network layer */
1479e936121dSHans Petter Selasky 		(*lc->ifp->if_input) (lc->ifp, mb);
1480e936121dSHans Petter Selasky 		return;
1481e936121dSHans Petter Selasky 	}
1482e936121dSHans Petter Selasky 
14834e0ce82bSRandall Stewart  	/* If no hardware or arrival stamp on the packet add timestamp */
14844e0ce82bSRandall Stewart  	if ((tcplro_stacks_wanting_mbufq > 0) &&
14854e0ce82bSRandall Stewart  	    (tcp_less_accurate_lro_ts == 0) &&
14864e0ce82bSRandall Stewart  	    ((mb->m_flags & M_TSTMP) == 0)) {
14874e0ce82bSRandall Stewart  		/* Add in an LRO time since no hardware */
14884e0ce82bSRandall Stewart  		binuptime(&lc->lro_last_queue_time);
14894e0ce82bSRandall Stewart  		mb->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time);
14904e0ce82bSRandall Stewart  		mb->m_flags |= M_TSTMP_LRO;
14914e0ce82bSRandall Stewart  	}
14924e0ce82bSRandall Stewart 
1493fc271df3SHans Petter Selasky 	/* create sequence number */
1494fc271df3SHans Petter Selasky 	lc->lro_mbuf_data[lc->lro_mbuf_count].seq =
1495fc271df3SHans Petter Selasky 	    (((uint64_t)M_HASHTYPE_GET(mb)) << 56) |
1496fc271df3SHans Petter Selasky 	    (((uint64_t)mb->m_pkthdr.flowid) << 24) |
1497fc271df3SHans Petter Selasky 	    ((uint64_t)lc->lro_mbuf_count);
1498e936121dSHans Petter Selasky 
1499e936121dSHans Petter Selasky 	/* enter mbuf */
1500f8acc03eSNavdeep Parhar 	lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb;
1501f8acc03eSNavdeep Parhar 
1502f8acc03eSNavdeep Parhar 	/* flush if array is full */
1503f8acc03eSNavdeep Parhar 	if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max))
1504f8acc03eSNavdeep Parhar 		tcp_lro_flush_all(lc);
1505e936121dSHans Petter Selasky }
1506e936121dSHans Petter Selasky 
150762b5b6ecSBjoern A. Zeeb /* end */
1508