xref: /freebsd/sys/netinet/tcp_lro.c (revision 2c6ff1d6320d57a9d0dc62c10c83145ed49a51dd)
127f190a3SBjoern A. Zeeb /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
3762b5b6ecSBjoern A. Zeeb 
3862b5b6ecSBjoern A. Zeeb #include "opt_inet.h"
3962b5b6ecSBjoern A. Zeeb #include "opt_inet6.h"
4062b5b6ecSBjoern A. Zeeb 
416c5087a8SJack F Vogel #include <sys/param.h>
426c5087a8SJack F Vogel #include <sys/systm.h>
436c5087a8SJack F Vogel #include <sys/kernel.h>
448ec07310SGleb Smirnoff #include <sys/malloc.h>
458ec07310SGleb Smirnoff #include <sys/mbuf.h>
466c5087a8SJack F Vogel #include <sys/socket.h>
47e57b2d0eSRandall Stewart #include <sys/socketvar.h>
48e57b2d0eSRandall Stewart #include <sys/sockbuf.h>
498452c1b3SSepherosa Ziehau #include <sys/sysctl.h>
506c5087a8SJack F Vogel 
516c5087a8SJack F Vogel #include <net/if.h>
5262b5b6ecSBjoern A. Zeeb #include <net/if_var.h>
536c5087a8SJack F Vogel #include <net/ethernet.h>
5469a34e8dSRandall Stewart #include <net/bpf.h>
555fa2656eSBjoern A. Zeeb #include <net/vnet.h>
56*2c6ff1d6SAndrew Gallatin #include <net/if_dl.h>
57*2c6ff1d6SAndrew Gallatin #include <net/if_media.h>
58*2c6ff1d6SAndrew Gallatin #include <net/if_types.h>
59*2c6ff1d6SAndrew Gallatin #include <net/infiniband.h>
60*2c6ff1d6SAndrew Gallatin #include <net/if_lagg.h>
616c5087a8SJack F Vogel 
626c5087a8SJack F Vogel #include <netinet/in_systm.h>
636c5087a8SJack F Vogel #include <netinet/in.h>
6462b5b6ecSBjoern A. Zeeb #include <netinet/ip6.h>
656c5087a8SJack F Vogel #include <netinet/ip.h>
6631bfc56eSBjoern A. Zeeb #include <netinet/ip_var.h>
67e57b2d0eSRandall Stewart #include <netinet/in_pcb.h>
68e57b2d0eSRandall Stewart #include <netinet6/in6_pcb.h>
696c5087a8SJack F Vogel #include <netinet/tcp.h>
70d7fb35d1SSean Bruno #include <netinet/tcp_seq.h>
716c5087a8SJack F Vogel #include <netinet/tcp_lro.h>
728452c1b3SSepherosa Ziehau #include <netinet/tcp_var.h>
7369a34e8dSRandall Stewart #include <netinet/tcpip.h>
74e57b2d0eSRandall Stewart #include <netinet/tcp_hpts.h>
75e57b2d0eSRandall Stewart #include <netinet/tcp_log_buf.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 
839ca874cfSHans Petter Selasky #define	TCP_LRO_TS_OPTION \
849ca874cfSHans Petter Selasky     ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
859ca874cfSHans Petter Selasky 	  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
866c5087a8SJack F Vogel 
876dd38b87SSepherosa Ziehau static void	tcp_lro_rx_done(struct lro_ctrl *lc);
889ca874cfSHans Petter Selasky static int	tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m,
899ca874cfSHans Petter Selasky 		    uint32_t csum, bool use_hash);
909ca874cfSHans Petter Selasky 
919ca874cfSHans Petter Selasky #ifdef TCPHPTS
929ca874cfSHans Petter Selasky static bool	do_bpf_strip_and_compress(struct inpcb *, struct lro_ctrl *,
93*2c6ff1d6SAndrew Gallatin 		struct lro_entry *, struct mbuf **, struct mbuf **, struct mbuf **,
94*2c6ff1d6SAndrew Gallatin 		bool *, bool, bool, struct ifnet *);
959ca874cfSHans Petter Selasky 
969ca874cfSHans Petter Selasky #endif
976dd38b87SSepherosa Ziehau 
988452c1b3SSepherosa Ziehau SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro,  CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
998452c1b3SSepherosa Ziehau     "TCP LRO");
1008452c1b3SSepherosa Ziehau 
1019ca874cfSHans Petter Selasky static long tcplro_stacks_wanting_mbufq;
102e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_direct_queue;
103e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_wokeup_queue;
104e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_compressed;
105e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_locks_taken;
10669a34e8dSRandall Stewart counter_u64_t tcp_extra_mbuf;
10769a34e8dSRandall Stewart counter_u64_t tcp_would_have_but;
10869a34e8dSRandall Stewart counter_u64_t tcp_comp_total;
10969a34e8dSRandall Stewart counter_u64_t tcp_uncomp_total;
110ca1a7e10SRandall Stewart counter_u64_t tcp_bad_csums;
111e57b2d0eSRandall Stewart 
1128452c1b3SSepherosa Ziehau static unsigned	tcp_lro_entries = TCP_LRO_ENTRIES;
1138452c1b3SSepherosa Ziehau SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
1148452c1b3SSepherosa Ziehau     CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
1158452c1b3SSepherosa Ziehau     "default number of LRO entries");
11669a34e8dSRandall Stewart 
117d7955cc0SRandall Stewart static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH;
118d7955cc0SRandall Stewart SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold,
119d7955cc0SRandall Stewart     CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0,
12008c7f1b6SNavdeep Parhar     "Number of interrupts in a row on the same CPU that will make us declare an 'affinity' cpu?");
121d7955cc0SRandall Stewart 
122e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD,
123e57b2d0eSRandall Stewart     &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport");
124e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD,
125e57b2d0eSRandall Stewart     &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts");
126e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD,
127e57b2d0eSRandall Stewart     &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport");
128e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
129e57b2d0eSRandall Stewart     &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
13069a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD,
13169a34e8dSRandall Stewart     &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp");
13269a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD,
1339ca874cfSHans Petter Selasky     &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed");
13469a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD,
13569a34e8dSRandall Stewart     &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set");
13669a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD,
13769a34e8dSRandall Stewart     &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP");
138ca1a7e10SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD,
139ca1a7e10SRandall Stewart     &tcp_bad_csums, "Number of packets that the common code saw with bad csums");
140e57b2d0eSRandall Stewart 
141e57b2d0eSRandall Stewart void
142e57b2d0eSRandall Stewart tcp_lro_reg_mbufq(void)
143e57b2d0eSRandall Stewart {
144e57b2d0eSRandall Stewart 	atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1);
145e57b2d0eSRandall Stewart }
146e57b2d0eSRandall Stewart 
147e57b2d0eSRandall Stewart void
148e57b2d0eSRandall Stewart tcp_lro_dereg_mbufq(void)
149e57b2d0eSRandall Stewart {
150e57b2d0eSRandall Stewart 	atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1);
151e57b2d0eSRandall Stewart }
1528452c1b3SSepherosa Ziehau 
15351e3c20dSSepherosa Ziehau static __inline void
15405cde7efSSepherosa Ziehau tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket,
15505cde7efSSepherosa Ziehau     struct lro_entry *le)
15651e3c20dSSepherosa Ziehau {
15751e3c20dSSepherosa Ziehau 
15851e3c20dSSepherosa Ziehau 	LIST_INSERT_HEAD(&lc->lro_active, le, next);
15905cde7efSSepherosa Ziehau 	LIST_INSERT_HEAD(bucket, le, hash_next);
16051e3c20dSSepherosa Ziehau }
16151e3c20dSSepherosa Ziehau 
16251e3c20dSSepherosa Ziehau static __inline void
16351e3c20dSSepherosa Ziehau tcp_lro_active_remove(struct lro_entry *le)
16451e3c20dSSepherosa Ziehau {
16551e3c20dSSepherosa Ziehau 
16605cde7efSSepherosa Ziehau 	LIST_REMOVE(le, next);		/* active list */
16705cde7efSSepherosa Ziehau 	LIST_REMOVE(le, hash_next);	/* hash bucket */
16851e3c20dSSepherosa Ziehau }
16951e3c20dSSepherosa Ziehau 
1706c5087a8SJack F Vogel int
17162b5b6ecSBjoern A. Zeeb tcp_lro_init(struct lro_ctrl *lc)
1726c5087a8SJack F Vogel {
1738452c1b3SSepherosa Ziehau 	return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0));
174e936121dSHans Petter Selasky }
175e936121dSHans Petter Selasky 
176e936121dSHans Petter Selasky int
177e936121dSHans Petter Selasky tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
178e936121dSHans Petter Selasky     unsigned lro_entries, unsigned lro_mbufs)
179e936121dSHans Petter Selasky {
18062b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
181e936121dSHans Petter Selasky 	size_t size;
18205cde7efSSepherosa Ziehau 	unsigned i, elements;
1836c5087a8SJack F Vogel 
18462b5b6ecSBjoern A. Zeeb 	lc->lro_bad_csum = 0;
18562b5b6ecSBjoern A. Zeeb 	lc->lro_queued = 0;
18662b5b6ecSBjoern A. Zeeb 	lc->lro_flushed = 0;
187e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
188e936121dSHans Petter Selasky 	lc->lro_mbuf_max = lro_mbufs;
189e936121dSHans Petter Selasky 	lc->lro_cnt = lro_entries;
1907ae3d4bfSSepherosa Ziehau 	lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX;
1917ae3d4bfSSepherosa Ziehau 	lc->lro_length_lim = TCP_LRO_LENGTH_MAX;
192e936121dSHans Petter Selasky 	lc->ifp = ifp;
1931ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_free);
1941ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_active);
1956c5087a8SJack F Vogel 
19605cde7efSSepherosa Ziehau 	/* create hash table to accelerate entry lookup */
19705cde7efSSepherosa Ziehau 	if (lro_entries > lro_mbufs)
19805cde7efSSepherosa Ziehau 		elements = lro_entries;
19905cde7efSSepherosa Ziehau 	else
20005cde7efSSepherosa Ziehau 		elements = lro_mbufs;
20105cde7efSSepherosa Ziehau 	lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz,
20205cde7efSSepherosa Ziehau 	    HASH_NOWAIT);
20305cde7efSSepherosa Ziehau 	if (lc->lro_hash == NULL) {
20405cde7efSSepherosa Ziehau 		memset(lc, 0, sizeof(*lc));
20505cde7efSSepherosa Ziehau 		return (ENOMEM);
20605cde7efSSepherosa Ziehau 	}
20705cde7efSSepherosa Ziehau 
208e936121dSHans Petter Selasky 	/* compute size to allocate */
209fc271df3SHans Petter Selasky 	size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) +
210e936121dSHans Petter Selasky 	    (lro_entries * sizeof(*le));
211fc271df3SHans Petter Selasky 	lc->lro_mbuf_data = (struct lro_mbuf_sort *)
212e936121dSHans Petter Selasky 	    malloc(size, M_LRO, M_NOWAIT | M_ZERO);
2136c5087a8SJack F Vogel 
214e936121dSHans Petter Selasky 	/* check for out of memory */
215e936121dSHans Petter Selasky 	if (lc->lro_mbuf_data == NULL) {
216a3927369SNavdeep Parhar 		free(lc->lro_hash, M_LRO);
217e936121dSHans Petter Selasky 		memset(lc, 0, sizeof(*lc));
218e936121dSHans Petter Selasky 		return (ENOMEM);
219e936121dSHans Petter Selasky 	}
220e936121dSHans Petter Selasky 	/* compute offset for LRO entries */
221e936121dSHans Petter Selasky 	le = (struct lro_entry *)
222e936121dSHans Petter Selasky 	    (lc->lro_mbuf_data + lro_mbufs);
223e936121dSHans Petter Selasky 
224e936121dSHans Petter Selasky 	/* setup linked list */
225e936121dSHans Petter Selasky 	for (i = 0; i != lro_entries; i++)
2261ea44822SSepherosa Ziehau 		LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
227e936121dSHans Petter Selasky 
228e936121dSHans Petter Selasky 	return (0);
2296c5087a8SJack F Vogel }
2306c5087a8SJack F Vogel 
2319ca874cfSHans Petter Selasky struct vxlan_header {
2329ca874cfSHans Petter Selasky 	uint32_t	vxlh_flags;
2339ca874cfSHans Petter Selasky 	uint32_t	vxlh_vni;
2349ca874cfSHans Petter Selasky };
235e57b2d0eSRandall Stewart 
2369ca874cfSHans Petter Selasky static inline void *
2371d171e5aSRandall Stewart tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen)
2389ca874cfSHans Petter Selasky {
2399ca874cfSHans Petter Selasky 	const struct ether_vlan_header *eh;
2409ca874cfSHans Petter Selasky 	void *old;
2419ca874cfSHans Petter Selasky 	uint16_t eth_type;
2429ca874cfSHans Petter Selasky 
2439ca874cfSHans Petter Selasky 	if (update_data)
2449ca874cfSHans Petter Selasky 		memset(parser, 0, sizeof(*parser));
2459ca874cfSHans Petter Selasky 
2469ca874cfSHans Petter Selasky 	old = ptr;
2479ca874cfSHans Petter Selasky 
2489ca874cfSHans Petter Selasky 	if (is_vxlan) {
2499ca874cfSHans Petter Selasky 		const struct vxlan_header *vxh;
2509ca874cfSHans Petter Selasky 		vxh = ptr;
2519ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + sizeof(*vxh);
2529ca874cfSHans Petter Selasky 		if (update_data) {
2539ca874cfSHans Petter Selasky 			parser->data.vxlan_vni =
2549ca874cfSHans Petter Selasky 			    vxh->vxlh_vni & htonl(0xffffff00);
255e57b2d0eSRandall Stewart 		}
2569ca874cfSHans Petter Selasky 	}
2579ca874cfSHans Petter Selasky 
2589ca874cfSHans Petter Selasky 	eh = ptr;
2599ca874cfSHans Petter Selasky 	if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) {
2609ca874cfSHans Petter Selasky 		eth_type = eh->evl_proto;
2619ca874cfSHans Petter Selasky 		if (update_data) {
2629ca874cfSHans Petter Selasky 			/* strip priority and keep VLAN ID only */
2639ca874cfSHans Petter Selasky 			parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK);
2649ca874cfSHans Petter Selasky 		}
2659ca874cfSHans Petter Selasky 		/* advance to next header */
2669ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
2671d171e5aSRandall Stewart 		mlen -= (ETHER_HDR_LEN  + ETHER_VLAN_ENCAP_LEN);
2689ca874cfSHans Petter Selasky 	} else {
2699ca874cfSHans Petter Selasky 		eth_type = eh->evl_encap_proto;
2709ca874cfSHans Petter Selasky 		/* advance to next header */
2711d171e5aSRandall Stewart 		mlen -= ETHER_HDR_LEN;
2729ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + ETHER_HDR_LEN;
2739ca874cfSHans Petter Selasky 	}
2741d171e5aSRandall Stewart 	if (__predict_false(mlen <= 0))
2751d171e5aSRandall Stewart 		return (NULL);
2769ca874cfSHans Petter Selasky 	switch (eth_type) {
2779ca874cfSHans Petter Selasky #ifdef INET
2789ca874cfSHans Petter Selasky 	case htons(ETHERTYPE_IP):
2799ca874cfSHans Petter Selasky 		parser->ip4 = ptr;
2801d171e5aSRandall Stewart 		if (__predict_false(mlen < sizeof(struct ip)))
2811d171e5aSRandall Stewart 			return (NULL);
2829ca874cfSHans Petter Selasky 		/* Ensure there are no IPv4 options. */
2839ca874cfSHans Petter Selasky 		if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4))
2849ca874cfSHans Petter Selasky 			break;
2859ca874cfSHans Petter Selasky 		/* .. and the packet is not fragmented. */
2869ca874cfSHans Petter Selasky 		if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK))
2879ca874cfSHans Petter Selasky 			break;
2889ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2);
2891d171e5aSRandall Stewart 		mlen -= sizeof(struct ip);
2909ca874cfSHans Petter Selasky 		if (update_data) {
2919ca874cfSHans Petter Selasky 			parser->data.s_addr.v4 = parser->ip4->ip_src;
2929ca874cfSHans Petter Selasky 			parser->data.d_addr.v4 = parser->ip4->ip_dst;
2939ca874cfSHans Petter Selasky 		}
2949ca874cfSHans Petter Selasky 		switch (parser->ip4->ip_p) {
2959ca874cfSHans Petter Selasky 		case IPPROTO_UDP:
2961d171e5aSRandall Stewart 			if (__predict_false(mlen < sizeof(struct udphdr)))
2971d171e5aSRandall Stewart 				return (NULL);
2989ca874cfSHans Petter Selasky 			parser->udp = ptr;
2999ca874cfSHans Petter Selasky 			if (update_data) {
3009ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV4_UDP;
3019ca874cfSHans Petter Selasky 				parser->data.s_port = parser->udp->uh_sport;
3029ca874cfSHans Petter Selasky 				parser->data.d_port = parser->udp->uh_dport;
3039ca874cfSHans Petter Selasky 			} else {
3049ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP);
3059ca874cfSHans Petter Selasky 			}
3069ca874cfSHans Petter Selasky 			ptr = ((uint8_t *)ptr + sizeof(*parser->udp));
3079ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
3089ca874cfSHans Petter Selasky 			return (ptr);
3099ca874cfSHans Petter Selasky 		case IPPROTO_TCP:
3109ca874cfSHans Petter Selasky 			parser->tcp = ptr;
3111d171e5aSRandall Stewart 			if (__predict_false(mlen < sizeof(struct tcphdr)))
3121d171e5aSRandall Stewart 				return (NULL);
3139ca874cfSHans Petter Selasky 			if (update_data) {
3149ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV4_TCP;
3159ca874cfSHans Petter Selasky 				parser->data.s_port = parser->tcp->th_sport;
3169ca874cfSHans Petter Selasky 				parser->data.d_port = parser->tcp->th_dport;
3179ca874cfSHans Petter Selasky 			} else {
3189ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP);
3199ca874cfSHans Petter Selasky 			}
3201d171e5aSRandall Stewart 			if (__predict_false(mlen < (parser->tcp->th_off << 2)))
3211d171e5aSRandall Stewart 				return (NULL);
3229ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
3239ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
3249ca874cfSHans Petter Selasky 			return (ptr);
3259ca874cfSHans Petter Selasky 		default:
3269ca874cfSHans Petter Selasky 			break;
3279ca874cfSHans Petter Selasky 		}
3289ca874cfSHans Petter Selasky 		break;
3299ca874cfSHans Petter Selasky #endif
3309ca874cfSHans Petter Selasky #ifdef INET6
3319ca874cfSHans Petter Selasky 	case htons(ETHERTYPE_IPV6):
3329ca874cfSHans Petter Selasky 		parser->ip6 = ptr;
3331d171e5aSRandall Stewart 		if (__predict_false(mlen < sizeof(struct ip6_hdr)))
3341d171e5aSRandall Stewart 			return (NULL);
3359ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + sizeof(*parser->ip6);
3369ca874cfSHans Petter Selasky 		if (update_data) {
3379ca874cfSHans Petter Selasky 			parser->data.s_addr.v6 = parser->ip6->ip6_src;
3389ca874cfSHans Petter Selasky 			parser->data.d_addr.v6 = parser->ip6->ip6_dst;
3399ca874cfSHans Petter Selasky 		}
3401d171e5aSRandall Stewart 		mlen -= sizeof(struct ip6_hdr);
3419ca874cfSHans Petter Selasky 		switch (parser->ip6->ip6_nxt) {
3429ca874cfSHans Petter Selasky 		case IPPROTO_UDP:
3431d171e5aSRandall Stewart 			if (__predict_false(mlen < sizeof(struct udphdr)))
3441d171e5aSRandall Stewart 				return (NULL);
3459ca874cfSHans Petter Selasky 			parser->udp = ptr;
3469ca874cfSHans Petter Selasky 			if (update_data) {
3479ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV6_UDP;
3489ca874cfSHans Petter Selasky 				parser->data.s_port = parser->udp->uh_sport;
3499ca874cfSHans Petter Selasky 				parser->data.d_port = parser->udp->uh_dport;
3509ca874cfSHans Petter Selasky 			} else {
3519ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP);
3529ca874cfSHans Petter Selasky 			}
3539ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + sizeof(*parser->udp);
3549ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
3559ca874cfSHans Petter Selasky 			return (ptr);
3569ca874cfSHans Petter Selasky 		case IPPROTO_TCP:
3571d171e5aSRandall Stewart 			if (__predict_false(mlen < sizeof(struct tcphdr)))
3581d171e5aSRandall Stewart 				return (NULL);
3599ca874cfSHans Petter Selasky 			parser->tcp = ptr;
3609ca874cfSHans Petter Selasky 			if (update_data) {
3619ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV6_TCP;
3629ca874cfSHans Petter Selasky 				parser->data.s_port = parser->tcp->th_sport;
3639ca874cfSHans Petter Selasky 				parser->data.d_port = parser->tcp->th_dport;
3649ca874cfSHans Petter Selasky 			} else {
3659ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP);
3669ca874cfSHans Petter Selasky 			}
3671d171e5aSRandall Stewart 			if (__predict_false(mlen < (parser->tcp->th_off << 2)))
3681d171e5aSRandall Stewart 				return (NULL);
3699ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
3709ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
3719ca874cfSHans Petter Selasky 			return (ptr);
3729ca874cfSHans Petter Selasky 		default:
3739ca874cfSHans Petter Selasky 			break;
3749ca874cfSHans Petter Selasky 		}
3759ca874cfSHans Petter Selasky 		break;
3769ca874cfSHans Petter Selasky #endif
3779ca874cfSHans Petter Selasky 	default:
3789ca874cfSHans Petter Selasky 		break;
3799ca874cfSHans Petter Selasky 	}
3809ca874cfSHans Petter Selasky 	/* Invalid packet - cannot parse */
3819ca874cfSHans Petter Selasky 	return (NULL);
3829ca874cfSHans Petter Selasky }
3839ca874cfSHans Petter Selasky 
3849ca874cfSHans Petter Selasky static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
3859ca874cfSHans Petter Selasky     CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID;
3869ca874cfSHans Petter Selasky 
3879ca874cfSHans Petter Selasky static inline struct lro_parser *
3889ca874cfSHans Petter Selasky tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data)
3899ca874cfSHans Petter Selasky {
3909ca874cfSHans Petter Selasky 	void *data_ptr;
3919ca874cfSHans Petter Selasky 
3929ca874cfSHans Petter Selasky 	/* Try to parse outer headers first. */
3931d171e5aSRandall Stewart 	data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len);
3949ca874cfSHans Petter Selasky 	if (data_ptr == NULL || po->total_hdr_len > m->m_len)
3959ca874cfSHans Petter Selasky 		return (NULL);
3969ca874cfSHans Petter Selasky 
3979ca874cfSHans Petter Selasky 	if (update_data) {
3989ca874cfSHans Petter Selasky 		/* Store VLAN ID, if any. */
3999ca874cfSHans Petter Selasky 		if (__predict_false(m->m_flags & M_VLANTAG)) {
4009ca874cfSHans Petter Selasky 			po->data.vlan_id =
4019ca874cfSHans Petter Selasky 			    htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK);
4029ca874cfSHans Petter Selasky 		}
403bb5cd80eSHans Petter Selasky 		/* Store decrypted flag, if any. */
40410a62eb1SHans Petter Selasky 		if (__predict_false((m->m_pkthdr.csum_flags &
40510a62eb1SHans Petter Selasky 		    CSUM_TLS_MASK) == CSUM_TLS_DECRYPTED))
406bb5cd80eSHans Petter Selasky 			po->data.lro_flags |= LRO_FLAG_DECRYPTED;
4079ca874cfSHans Petter Selasky 	}
4089ca874cfSHans Petter Selasky 
4099ca874cfSHans Petter Selasky 	switch (po->data.lro_type) {
4109ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_UDP:
4119ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_UDP:
4129ca874cfSHans Petter Selasky 		/* Check for VXLAN headers. */
4139ca874cfSHans Petter Selasky 		if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum)
4149ca874cfSHans Petter Selasky 			break;
4159ca874cfSHans Petter Selasky 
4169ca874cfSHans Petter Selasky 		/* Try to parse inner headers. */
4171d171e5aSRandall Stewart 		data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true,
4181d171e5aSRandall Stewart 						    (m->m_len - ((caddr_t)data_ptr - m->m_data)));
4191d171e5aSRandall Stewart 		if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len)
4209ca874cfSHans Petter Selasky 			break;
4219ca874cfSHans Petter Selasky 
4229ca874cfSHans Petter Selasky 		/* Verify supported header types. */
4239ca874cfSHans Petter Selasky 		switch (pi->data.lro_type) {
4249ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV4_TCP:
4259ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV6_TCP:
4269ca874cfSHans Petter Selasky 			return (pi);
4279ca874cfSHans Petter Selasky 		default:
4289ca874cfSHans Petter Selasky 			break;
4299ca874cfSHans Petter Selasky 		}
4309ca874cfSHans Petter Selasky 		break;
4319ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
4329ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
4339ca874cfSHans Petter Selasky 		if (update_data)
4349ca874cfSHans Petter Selasky 			memset(pi, 0, sizeof(*pi));
4359ca874cfSHans Petter Selasky 		return (po);
4369ca874cfSHans Petter Selasky 	default:
4379ca874cfSHans Petter Selasky 		break;
4389ca874cfSHans Petter Selasky 	}
4399ca874cfSHans Petter Selasky 	return (NULL);
4409ca874cfSHans Petter Selasky }
4419ca874cfSHans Petter Selasky 
4429ca874cfSHans Petter Selasky static inline int
4439ca874cfSHans Petter Selasky tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po)
4449ca874cfSHans Petter Selasky {
4459ca874cfSHans Petter Selasky 	int len;
4469ca874cfSHans Petter Selasky 
4479ca874cfSHans Petter Selasky 	switch (po->data.lro_type) {
4489ca874cfSHans Petter Selasky #ifdef INET
4499ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
4509ca874cfSHans Petter Selasky 		len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) +
4519ca874cfSHans Petter Selasky 		    ntohs(po->ip4->ip_len);
4529ca874cfSHans Petter Selasky 		break;
4539ca874cfSHans Petter Selasky #endif
4549ca874cfSHans Petter Selasky #ifdef INET6
4559ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
4569ca874cfSHans Petter Selasky 		len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) +
4579ca874cfSHans Petter Selasky 		    ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6);
4589ca874cfSHans Petter Selasky 		break;
4599ca874cfSHans Petter Selasky #endif
4609ca874cfSHans Petter Selasky 	default:
4619ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
4629ca874cfSHans Petter Selasky 	}
4639ca874cfSHans Petter Selasky 
4649ca874cfSHans Petter Selasky 	/*
4659ca874cfSHans Petter Selasky 	 * If the frame is padded beyond the end of the IP packet,
4669ca874cfSHans Petter Selasky 	 * then trim the extra bytes off:
4679ca874cfSHans Petter Selasky 	 */
4689ca874cfSHans Petter Selasky 	if (__predict_true(m->m_pkthdr.len == len)) {
4699ca874cfSHans Petter Selasky 		return (0);
4709ca874cfSHans Petter Selasky 	} else if (m->m_pkthdr.len > len) {
4719ca874cfSHans Petter Selasky 		m_adj(m, len - m->m_pkthdr.len);
4729ca874cfSHans Petter Selasky 		return (0);
4739ca874cfSHans Petter Selasky 	}
4749ca874cfSHans Petter Selasky 	return (TCP_LRO_CANNOT);
4759ca874cfSHans Petter Selasky }
4769ca874cfSHans Petter Selasky 
4779ca874cfSHans Petter Selasky static struct tcphdr *
4789ca874cfSHans Petter Selasky tcp_lro_get_th(struct mbuf *m)
4799ca874cfSHans Petter Selasky {
4809ca874cfSHans Petter Selasky 	return ((struct tcphdr *)((uint8_t *)m->m_data + m->m_pkthdr.lro_tcp_h_off));
481e57b2d0eSRandall Stewart }
482e57b2d0eSRandall Stewart 
48369a34e8dSRandall Stewart static void
48469a34e8dSRandall Stewart lro_free_mbuf_chain(struct mbuf *m)
48569a34e8dSRandall Stewart {
48669a34e8dSRandall Stewart 	struct mbuf *save;
48769a34e8dSRandall Stewart 
48869a34e8dSRandall Stewart 	while (m) {
48969a34e8dSRandall Stewart 		save = m->m_nextpkt;
49069a34e8dSRandall Stewart 		m->m_nextpkt = NULL;
49169a34e8dSRandall Stewart 		m_freem(m);
49269a34e8dSRandall Stewart 		m = save;
49369a34e8dSRandall Stewart 	}
49469a34e8dSRandall Stewart }
49569a34e8dSRandall Stewart 
4966c5087a8SJack F Vogel void
49762b5b6ecSBjoern A. Zeeb tcp_lro_free(struct lro_ctrl *lc)
4986c5087a8SJack F Vogel {
49962b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
500e936121dSHans Petter Selasky 	unsigned x;
5016c5087a8SJack F Vogel 
502e936121dSHans Petter Selasky 	/* reset LRO free list */
5031ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_free);
504e936121dSHans Petter Selasky 
505e936121dSHans Petter Selasky 	/* free active mbufs, if any */
5061ea44822SSepherosa Ziehau 	while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
50751e3c20dSSepherosa Ziehau 		tcp_lro_active_remove(le);
50869a34e8dSRandall Stewart 		lro_free_mbuf_chain(le->m_head);
5096c5087a8SJack F Vogel 	}
510e936121dSHans Petter Selasky 
51105cde7efSSepherosa Ziehau 	/* free hash table */
51205cde7efSSepherosa Ziehau 	free(lc->lro_hash, M_LRO);
51305cde7efSSepherosa Ziehau 	lc->lro_hash = NULL;
51405cde7efSSepherosa Ziehau 	lc->lro_hashsz = 0;
51505cde7efSSepherosa Ziehau 
516e936121dSHans Petter Selasky 	/* free mbuf array, if any */
517e936121dSHans Petter Selasky 	for (x = 0; x != lc->lro_mbuf_count; x++)
518fc271df3SHans Petter Selasky 		m_freem(lc->lro_mbuf_data[x].mb);
519e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
520e936121dSHans Petter Selasky 
521e936121dSHans Petter Selasky 	/* free allocated memory, if any */
522e936121dSHans Petter Selasky 	free(lc->lro_mbuf_data, M_LRO);
523e936121dSHans Petter Selasky 	lc->lro_mbuf_data = NULL;
5246c5087a8SJack F Vogel }
5256c5087a8SJack F Vogel 
52662b5b6ecSBjoern A. Zeeb static uint16_t
5279ca874cfSHans Petter Selasky tcp_lro_rx_csum_tcphdr(const struct tcphdr *th)
52862b5b6ecSBjoern A. Zeeb {
5299ca874cfSHans Petter Selasky 	const uint16_t *ptr;
5309ca874cfSHans Petter Selasky 	uint32_t csum;
5319ca874cfSHans Petter Selasky 	uint16_t len;
53262b5b6ecSBjoern A. Zeeb 
5339ca874cfSHans Petter Selasky 	csum = -th->th_sum;	/* exclude checksum field */
5349ca874cfSHans Petter Selasky 	len = th->th_off;
5359ca874cfSHans Petter Selasky 	ptr = (const uint16_t *)th;
5369ca874cfSHans Petter Selasky 	while (len--) {
5379ca874cfSHans Petter Selasky 		csum += *ptr;
5389ca874cfSHans Petter Selasky 		ptr++;
5399ca874cfSHans Petter Selasky 		csum += *ptr;
5409ca874cfSHans Petter Selasky 		ptr++;
54162b5b6ecSBjoern A. Zeeb 	}
5429ca874cfSHans Petter Selasky 	while (csum > 0xffff)
5439ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
54462b5b6ecSBjoern A. Zeeb 
5459ca874cfSHans Petter Selasky 	return (csum);
54662b5b6ecSBjoern A. Zeeb }
54762b5b6ecSBjoern A. Zeeb 
54862b5b6ecSBjoern A. Zeeb static uint16_t
5499ca874cfSHans Petter Selasky tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum)
55062b5b6ecSBjoern A. Zeeb {
55162b5b6ecSBjoern A. Zeeb 	uint32_t c;
55262b5b6ecSBjoern A. Zeeb 	uint16_t cs;
55362b5b6ecSBjoern A. Zeeb 
5549ca874cfSHans Petter Selasky 	c = tcp_csum;
55562b5b6ecSBjoern A. Zeeb 
5569ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
55762b5b6ecSBjoern A. Zeeb #ifdef INET6
5589ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
5599ca874cfSHans Petter Selasky 		/* Compute full pseudo IPv6 header checksum. */
5609ca874cfSHans Petter Selasky 		cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0);
56162b5b6ecSBjoern A. Zeeb 		break;
56262b5b6ecSBjoern A. Zeeb #endif
56362b5b6ecSBjoern A. Zeeb #ifdef INET
5649ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
5659ca874cfSHans Petter Selasky 		/* Compute full pseudo IPv4 header checsum. */
5669ca874cfSHans Petter Selasky 		cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP);
5679ca874cfSHans Petter Selasky 		cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs));
56862b5b6ecSBjoern A. Zeeb 		break;
56962b5b6ecSBjoern A. Zeeb #endif
57062b5b6ecSBjoern A. Zeeb 	default:
57162b5b6ecSBjoern A. Zeeb 		cs = 0;		/* Keep compiler happy. */
5729ca874cfSHans Petter Selasky 		break;
57362b5b6ecSBjoern A. Zeeb 	}
57462b5b6ecSBjoern A. Zeeb 
5759ca874cfSHans Petter Selasky 	/* Complement checksum. */
57662b5b6ecSBjoern A. Zeeb 	cs = ~cs;
57762b5b6ecSBjoern A. Zeeb 	c += cs;
57862b5b6ecSBjoern A. Zeeb 
5799ca874cfSHans Petter Selasky 	/* Remove TCP header checksum. */
5809ca874cfSHans Petter Selasky 	cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp);
58162b5b6ecSBjoern A. Zeeb 	c += cs;
5829ca874cfSHans Petter Selasky 
5839ca874cfSHans Petter Selasky 	/* Compute checksum remainder. */
58462b5b6ecSBjoern A. Zeeb 	while (c > 0xffff)
58562b5b6ecSBjoern A. Zeeb 		c = (c >> 16) + (c & 0xffff);
58662b5b6ecSBjoern A. Zeeb 
5879ca874cfSHans Petter Selasky 	return (c);
58862b5b6ecSBjoern A. Zeeb }
58962b5b6ecSBjoern A. Zeeb 
5906dd38b87SSepherosa Ziehau static void
5916dd38b87SSepherosa Ziehau tcp_lro_rx_done(struct lro_ctrl *lc)
5926dd38b87SSepherosa Ziehau {
5936dd38b87SSepherosa Ziehau 	struct lro_entry *le;
5946dd38b87SSepherosa Ziehau 
5951ea44822SSepherosa Ziehau 	while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
59651e3c20dSSepherosa Ziehau 		tcp_lro_active_remove(le);
5976dd38b87SSepherosa Ziehau 		tcp_lro_flush(lc, le);
5986dd38b87SSepherosa Ziehau 	}
5996dd38b87SSepherosa Ziehau }
6006dd38b87SSepherosa Ziehau 
6016c5087a8SJack F Vogel void
6027127e6acSNavdeep Parhar tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
6037127e6acSNavdeep Parhar {
6047127e6acSNavdeep Parhar 	struct lro_entry *le, *le_tmp;
605b45daaeaSRandall Stewart 	uint64_t now, tov;
606b45daaeaSRandall Stewart 	struct bintime bt;
6077127e6acSNavdeep Parhar 
608dc6ab77dSMichael Tuexen 	NET_EPOCH_ASSERT();
6091ea44822SSepherosa Ziehau 	if (LIST_EMPTY(&lc->lro_active))
6107127e6acSNavdeep Parhar 		return;
6117127e6acSNavdeep Parhar 
612b45daaeaSRandall Stewart 	/* get timeout time and current time in ns */
613b45daaeaSRandall Stewart 	binuptime(&bt);
614b45daaeaSRandall Stewart 	now = bintime2ns(&bt);
615b45daaeaSRandall Stewart 	tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000));
6161ea44822SSepherosa Ziehau 	LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
617b45daaeaSRandall Stewart 		if (now >= (bintime2ns(&le->alloc_time) + tov)) {
61851e3c20dSSepherosa Ziehau 			tcp_lro_active_remove(le);
6197127e6acSNavdeep Parhar 			tcp_lro_flush(lc, le);
6207127e6acSNavdeep Parhar 		}
6217127e6acSNavdeep Parhar 	}
6227127e6acSNavdeep Parhar }
6237127e6acSNavdeep Parhar 
624e57b2d0eSRandall Stewart #ifdef INET
625e57b2d0eSRandall Stewart static int
6269ca874cfSHans Petter Selasky tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4)
627e57b2d0eSRandall Stewart {
628e57b2d0eSRandall Stewart 	uint16_t csum;
629e57b2d0eSRandall Stewart 
630e57b2d0eSRandall Stewart 	/* Legacy IP has a header checksum that needs to be correct. */
6319ca874cfSHans Petter Selasky 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
6329ca874cfSHans Petter Selasky 		if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) {
633e57b2d0eSRandall Stewart 			lc->lro_bad_csum++;
634e57b2d0eSRandall Stewart 			return (TCP_LRO_CANNOT);
635e57b2d0eSRandall Stewart 		}
636e57b2d0eSRandall Stewart 	} else {
637e57b2d0eSRandall Stewart 		csum = in_cksum_hdr(ip4);
6389ca874cfSHans Petter Selasky 		if (__predict_false(csum != 0)) {
639e57b2d0eSRandall Stewart 			lc->lro_bad_csum++;
640e57b2d0eSRandall Stewart 			return (TCP_LRO_CANNOT);
641e57b2d0eSRandall Stewart 		}
642e57b2d0eSRandall Stewart 	}
643e57b2d0eSRandall Stewart 	return (0);
644e57b2d0eSRandall Stewart }
645e57b2d0eSRandall Stewart #endif
646e57b2d0eSRandall Stewart 
6470a4f8510SRandall Stewart #ifdef TCPHPTS
648e57b2d0eSRandall Stewart static void
6499ca874cfSHans Petter Selasky tcp_lro_log(struct tcpcb *tp, const struct lro_ctrl *lc,
6509ca874cfSHans Petter Selasky     const struct lro_entry *le, const struct mbuf *m,
6519ca874cfSHans Petter Selasky     int frm, int32_t tcp_data_len, uint32_t th_seq,
6529ca874cfSHans Petter Selasky     uint32_t th_ack, uint16_t th_win)
653e57b2d0eSRandall Stewart {
654e57b2d0eSRandall Stewart 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
655e57b2d0eSRandall Stewart 		union tcp_log_stackspecific log;
656b45daaeaSRandall Stewart 		struct timeval tv, btv;
657e57b2d0eSRandall Stewart 		uint32_t cts;
658e57b2d0eSRandall Stewart 
659e57b2d0eSRandall Stewart 		cts = tcp_get_usecs(&tv);
660e57b2d0eSRandall Stewart 		memset(&log, 0, sizeof(union tcp_log_stackspecific));
661e57b2d0eSRandall Stewart 		log.u_bbr.flex8 = frm;
662e57b2d0eSRandall Stewart 		log.u_bbr.flex1 = tcp_data_len;
663e57b2d0eSRandall Stewart 		if (m)
664e57b2d0eSRandall Stewart 			log.u_bbr.flex2 = m->m_pkthdr.len;
665e57b2d0eSRandall Stewart 		else
666e57b2d0eSRandall Stewart 			log.u_bbr.flex2 = 0;
6679ca874cfSHans Petter Selasky 		log.u_bbr.flex3 = le->m_head->m_pkthdr.lro_nsegs;
6689ca874cfSHans Petter Selasky 		log.u_bbr.flex4 = le->m_head->m_pkthdr.lro_tcp_d_len;
66969a34e8dSRandall Stewart 		if (le->m_head) {
670e57b2d0eSRandall Stewart 			log.u_bbr.flex5 = le->m_head->m_pkthdr.len;
671e57b2d0eSRandall Stewart 			log.u_bbr.delRate = le->m_head->m_flags;
672e57b2d0eSRandall Stewart 			log.u_bbr.rttProp = le->m_head->m_pkthdr.rcv_tstmp;
67369a34e8dSRandall Stewart 		}
674e57b2d0eSRandall Stewart 		log.u_bbr.inflight = th_seq;
675d7955cc0SRandall Stewart 		log.u_bbr.delivered = th_ack;
676e57b2d0eSRandall Stewart 		log.u_bbr.timeStamp = cts;
677e57b2d0eSRandall Stewart 		log.u_bbr.epoch = le->next_seq;
678e57b2d0eSRandall Stewart 		log.u_bbr.lt_epoch = le->ack_seq;
679e57b2d0eSRandall Stewart 		log.u_bbr.pacing_gain = th_win;
680e57b2d0eSRandall Stewart 		log.u_bbr.cwnd_gain = le->window;
681d7955cc0SRandall Stewart 		log.u_bbr.lost = curcpu;
682b23b156eSWarner Losh 		log.u_bbr.cur_del_rate = (uintptr_t)m;
683b23b156eSWarner Losh 		log.u_bbr.bw_inuse = (uintptr_t)le->m_head;
684b45daaeaSRandall Stewart 		bintime2timeval(&lc->lro_last_queue_time, &btv);
685b45daaeaSRandall Stewart 		log.u_bbr.flex6 = tcp_tv_to_usectick(&btv);
6869ca874cfSHans Petter Selasky 		log.u_bbr.flex7 = le->compressed;
6879ca874cfSHans Petter Selasky 		log.u_bbr.pacing_gain = le->uncompressed;
68869a34e8dSRandall Stewart 		if (in_epoch(net_epoch_preempt))
68969a34e8dSRandall Stewart 			log.u_bbr.inhpts = 1;
69069a34e8dSRandall Stewart 		else
69169a34e8dSRandall Stewart 			log.u_bbr.inhpts = 0;
692e57b2d0eSRandall Stewart 		TCP_LOG_EVENTP(tp, NULL,
693e57b2d0eSRandall Stewart 			       &tp->t_inpcb->inp_socket->so_rcv,
694e57b2d0eSRandall Stewart 			       &tp->t_inpcb->inp_socket->so_snd,
695e57b2d0eSRandall Stewart 			       TCP_LOG_LRO, 0,
696e57b2d0eSRandall Stewart 			       0, &log, false, &tv);
697e57b2d0eSRandall Stewart 	}
698e57b2d0eSRandall Stewart }
6990a4f8510SRandall Stewart #endif
700e57b2d0eSRandall Stewart 
7019ca874cfSHans Petter Selasky static inline void
7029ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum)
703e57b2d0eSRandall Stewart {
7049ca874cfSHans Petter Selasky 	uint32_t csum;
7056c5087a8SJack F Vogel 
7069ca874cfSHans Petter Selasky 	csum = 0xffff - *ptr + value;
7079ca874cfSHans Petter Selasky 	while (csum > 0xffff)
7089ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
7099ca874cfSHans Petter Selasky 	*ptr = value;
7109ca874cfSHans Petter Selasky 	*psum = csum;
71162b5b6ecSBjoern A. Zeeb }
71262b5b6ecSBjoern A. Zeeb 
7139ca874cfSHans Petter Selasky static uint16_t
7149ca874cfSHans Petter Selasky tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le,
7159ca874cfSHans Petter Selasky     uint16_t payload_len, uint16_t delta_sum)
7169ca874cfSHans Petter Selasky {
7179ca874cfSHans Petter Selasky 	uint32_t csum;
7189ca874cfSHans Petter Selasky 	uint16_t tlen;
7199ca874cfSHans Petter Selasky 	uint16_t temp[5] = {};
7209ca874cfSHans Petter Selasky 
7219ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
7229ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
7239ca874cfSHans Petter Selasky 		/* Compute new IPv4 length. */
7249ca874cfSHans Petter Selasky 		tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len;
7259ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
7269ca874cfSHans Petter Selasky 
7279ca874cfSHans Petter Selasky 		/* Subtract delta from current IPv4 checksum. */
7289ca874cfSHans Petter Selasky 		csum = pa->ip4->ip_sum + 0xffff - temp[0];
7299ca874cfSHans Petter Selasky 		while (csum > 0xffff)
7309ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
7319ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
7329ca874cfSHans Petter Selasky 		goto update_tcp_header;
7339ca874cfSHans Petter Selasky 
7349ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
7359ca874cfSHans Petter Selasky 		/* Compute new IPv6 length. */
7369ca874cfSHans Petter Selasky 		tlen = (pa->tcp->th_off << 2) + payload_len;
7379ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
7389ca874cfSHans Petter Selasky 		goto update_tcp_header;
7399ca874cfSHans Petter Selasky 
7409ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_UDP:
7419ca874cfSHans Petter Selasky 		/* Compute new IPv4 length. */
7429ca874cfSHans Petter Selasky 		tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len;
7439ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
7449ca874cfSHans Petter Selasky 
7459ca874cfSHans Petter Selasky 		/* Subtract delta from current IPv4 checksum. */
7469ca874cfSHans Petter Selasky 		csum = pa->ip4->ip_sum + 0xffff - temp[0];
7479ca874cfSHans Petter Selasky 		while (csum > 0xffff)
7489ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
7499ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
7509ca874cfSHans Petter Selasky 		goto update_udp_header;
7519ca874cfSHans Petter Selasky 
7529ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_UDP:
7539ca874cfSHans Petter Selasky 		/* Compute new IPv6 length. */
7549ca874cfSHans Petter Selasky 		tlen = sizeof(*pa->udp) + payload_len;
7559ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
7569ca874cfSHans Petter Selasky 		goto update_udp_header;
7579ca874cfSHans Petter Selasky 
75862b5b6ecSBjoern A. Zeeb 	default:
7599ca874cfSHans Petter Selasky 		return (0);
76062b5b6ecSBjoern A. Zeeb 	}
7619ca874cfSHans Petter Selasky 
7629ca874cfSHans Petter Selasky update_tcp_header:
7639ca874cfSHans Petter Selasky 	/* Compute current TCP header checksum. */
7649ca874cfSHans Petter Selasky 	temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp);
76562b5b6ecSBjoern A. Zeeb 
76662b5b6ecSBjoern A. Zeeb 	/* Incorporate the latest ACK into the TCP header. */
7679ca874cfSHans Petter Selasky 	pa->tcp->th_ack = le->ack_seq;
7689ca874cfSHans Petter Selasky 	pa->tcp->th_win = le->window;
7699ca874cfSHans Petter Selasky 
77062b5b6ecSBjoern A. Zeeb 	/* Incorporate latest timestamp into the TCP header. */
77162b5b6ecSBjoern A. Zeeb 	if (le->timestamp != 0) {
7726c5087a8SJack F Vogel 		uint32_t *ts_ptr;
7736c5087a8SJack F Vogel 
7749ca874cfSHans Petter Selasky 		ts_ptr = (uint32_t *)(pa->tcp + 1);
77562b5b6ecSBjoern A. Zeeb 		ts_ptr[1] = htonl(le->tsval);
77662b5b6ecSBjoern A. Zeeb 		ts_ptr[2] = le->tsecr;
77762b5b6ecSBjoern A. Zeeb 	}
7789ca874cfSHans Petter Selasky 
7799ca874cfSHans Petter Selasky 	/* Compute new TCP header checksum. */
7809ca874cfSHans Petter Selasky 	temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp);
7819ca874cfSHans Petter Selasky 
7829ca874cfSHans Petter Selasky 	/* Compute new TCP checksum. */
7839ca874cfSHans Petter Selasky 	csum = pa->tcp->th_sum + 0xffff - delta_sum +
7849ca874cfSHans Petter Selasky 	    0xffff - temp[0] + 0xffff - temp[3] + temp[2];
7859ca874cfSHans Petter Selasky 	while (csum > 0xffff)
7869ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
7879ca874cfSHans Petter Selasky 
7889ca874cfSHans Petter Selasky 	/* Assign new TCP checksum. */
7899ca874cfSHans Petter Selasky 	tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]);
7909ca874cfSHans Petter Selasky 
7919ca874cfSHans Petter Selasky 	/* Compute all modififications affecting next checksum. */
7929ca874cfSHans Petter Selasky 	csum = temp[0] + temp[1] + 0xffff - temp[2] +
7939ca874cfSHans Petter Selasky 	    temp[3] + temp[4] + delta_sum;
7949ca874cfSHans Petter Selasky 	while (csum > 0xffff)
7959ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
7969ca874cfSHans Petter Selasky 
7979ca874cfSHans Petter Selasky 	/* Return delta checksum to next stage, if any. */
7989ca874cfSHans Petter Selasky 	return (csum);
7999ca874cfSHans Petter Selasky 
8009ca874cfSHans Petter Selasky update_udp_header:
8019ca874cfSHans Petter Selasky 	tlen = sizeof(*pa->udp) + payload_len;
8029ca874cfSHans Petter Selasky 	/* Assign new UDP length and compute checksum delta. */
8039ca874cfSHans Petter Selasky 	tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]);
8049ca874cfSHans Petter Selasky 
8059ca874cfSHans Petter Selasky 	/* Check if there is a UDP checksum. */
8069ca874cfSHans Petter Selasky 	if (__predict_false(pa->udp->uh_sum != 0)) {
8079ca874cfSHans Petter Selasky 		/* Compute new UDP checksum. */
8089ca874cfSHans Petter Selasky 		csum = pa->udp->uh_sum + 0xffff - delta_sum +
8099ca874cfSHans Petter Selasky 		    0xffff - temp[0] + 0xffff - temp[2];
8109ca874cfSHans Petter Selasky 		while (csum > 0xffff)
8119ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
8129ca874cfSHans Petter Selasky 		/* Assign new UDP checksum. */
8139ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]);
814e57b2d0eSRandall Stewart 	}
8159ca874cfSHans Petter Selasky 
8169ca874cfSHans Petter Selasky 	/* Compute all modififications affecting next checksum. */
8179ca874cfSHans Petter Selasky 	csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum;
8189ca874cfSHans Petter Selasky 	while (csum > 0xffff)
8199ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
8209ca874cfSHans Petter Selasky 
8219ca874cfSHans Petter Selasky 	/* Return delta checksum to next stage, if any. */
8229ca874cfSHans Petter Selasky 	return (csum);
8239ca874cfSHans Petter Selasky }
8249ca874cfSHans Petter Selasky 
8259ca874cfSHans Petter Selasky static void
8269ca874cfSHans Petter Selasky tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le)
8279ca874cfSHans Petter Selasky {
8289ca874cfSHans Petter Selasky 	/* Check if we need to recompute any checksums. */
82924fe6643SRyan Stone 	if (le->needs_merge) {
8309ca874cfSHans Petter Selasky 		uint16_t csum;
8319ca874cfSHans Petter Selasky 
8329ca874cfSHans Petter Selasky 		switch (le->inner.data.lro_type) {
8339ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV4_TCP:
8349ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->inner, le,
8359ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len,
8369ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_csum);
8379ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->outer, NULL,
8389ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len +
8399ca874cfSHans Petter Selasky 			    le->inner.total_hdr_len, csum);
8409ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
8419ca874cfSHans Petter Selasky 			    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
8429ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_data = 0xffff;
84310a62eb1SHans Petter Selasky 			if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
84410a62eb1SHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
8459ca874cfSHans Petter Selasky 			break;
8469ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV6_TCP:
8479ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->inner, le,
8489ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len,
8499ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_csum);
8509ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->outer, NULL,
8519ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len +
8529ca874cfSHans Petter Selasky 			    le->inner.total_hdr_len, csum);
8539ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
8549ca874cfSHans Petter Selasky 			    CSUM_PSEUDO_HDR;
8559ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_data = 0xffff;
85610a62eb1SHans Petter Selasky 			if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
85710a62eb1SHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
8589ca874cfSHans Petter Selasky 			break;
8599ca874cfSHans Petter Selasky 		case LRO_TYPE_NONE:
8609ca874cfSHans Petter Selasky 			switch (le->outer.data.lro_type) {
8619ca874cfSHans Petter Selasky 			case LRO_TYPE_IPV4_TCP:
8629ca874cfSHans Petter Selasky 				csum = tcp_lro_update_checksum(&le->outer, le,
8639ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_len,
8649ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_csum);
8659ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
8669ca874cfSHans Petter Selasky 				    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
8679ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_data = 0xffff;
86810a62eb1SHans Petter Selasky 				if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
86910a62eb1SHans Petter Selasky 					le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
8709ca874cfSHans Petter Selasky 				break;
8719ca874cfSHans Petter Selasky 			case LRO_TYPE_IPV6_TCP:
8729ca874cfSHans Petter Selasky 				csum = tcp_lro_update_checksum(&le->outer, le,
8739ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_len,
8749ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_csum);
8759ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
8769ca874cfSHans Petter Selasky 				    CSUM_PSEUDO_HDR;
8779ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_data = 0xffff;
87810a62eb1SHans Petter Selasky 				if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
87910a62eb1SHans Petter Selasky 					le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
8809ca874cfSHans Petter Selasky 				break;
8819ca874cfSHans Petter Selasky 			default:
8829ca874cfSHans Petter Selasky 				break;
8839ca874cfSHans Petter Selasky 			}
8849ca874cfSHans Petter Selasky 			break;
8859ca874cfSHans Petter Selasky 		default:
8869ca874cfSHans Petter Selasky 			break;
8879ca874cfSHans Petter Selasky 		}
8889ca874cfSHans Petter Selasky 	}
8899ca874cfSHans Petter Selasky 
890e57b2d0eSRandall Stewart 	/*
891e57b2d0eSRandall Stewart 	 * Break any chain, this is not set to NULL on the singleton
892e57b2d0eSRandall Stewart 	 * case m_nextpkt points to m_head. Other case set them
893e57b2d0eSRandall Stewart 	 * m_nextpkt to NULL in push_and_replace.
894e57b2d0eSRandall Stewart 	 */
895e57b2d0eSRandall Stewart 	le->m_head->m_nextpkt = NULL;
8969ca874cfSHans Petter Selasky 	lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs;
897e57b2d0eSRandall Stewart 	(*lc->ifp->if_input)(lc->ifp, le->m_head);
89862b5b6ecSBjoern A. Zeeb }
8996c5087a8SJack F Vogel 
900e57b2d0eSRandall Stewart static void
9019ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le,
9029ca874cfSHans Petter Selasky     struct mbuf *m, struct tcphdr *th)
903e57b2d0eSRandall Stewart {
904e57b2d0eSRandall Stewart 	uint32_t *ts_ptr;
905e57b2d0eSRandall Stewart 	uint16_t tcp_data_len;
9069ca874cfSHans Petter Selasky 	uint16_t tcp_opt_len;
907e57b2d0eSRandall Stewart 
908e57b2d0eSRandall Stewart 	ts_ptr = (uint32_t *)(th + 1);
9099ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
9109ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
9119ca874cfSHans Petter Selasky 
9129ca874cfSHans Petter Selasky 	/* Check if there is a timestamp option. */
9139ca874cfSHans Petter Selasky 	if (tcp_opt_len == 0 ||
9149ca874cfSHans Petter Selasky 	    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
9159ca874cfSHans Petter Selasky 	    *ts_ptr != TCP_LRO_TS_OPTION)) {
9169ca874cfSHans Petter Selasky 		/* We failed to find the timestamp option. */
9179ca874cfSHans Petter Selasky 		le->timestamp = 0;
9189ca874cfSHans Petter Selasky 	} else {
919e57b2d0eSRandall Stewart 		le->timestamp = 1;
920e57b2d0eSRandall Stewart 		le->tsval = ntohl(*(ts_ptr + 1));
921e57b2d0eSRandall Stewart 		le->tsecr = *(ts_ptr + 2);
9229ca874cfSHans Petter Selasky 	}
9239ca874cfSHans Petter Selasky 
9249ca874cfSHans Petter Selasky 	tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
9259ca874cfSHans Petter Selasky 
9269ca874cfSHans Petter Selasky 	/* Pull out TCP sequence numbers and window size. */
927e57b2d0eSRandall Stewart 	le->next_seq = ntohl(th->th_seq) + tcp_data_len;
928e57b2d0eSRandall Stewart 	le->ack_seq = th->th_ack;
929e57b2d0eSRandall Stewart 	le->window = th->th_win;
9301ebf4607SRichard Scheffenegger 	le->flags = tcp_get_flags(th);
93124fe6643SRyan Stone 	le->needs_merge = 0;
9329ca874cfSHans Petter Selasky 
9339ca874cfSHans Petter Selasky 	/* Setup new data pointers. */
934e57b2d0eSRandall Stewart 	le->m_head = m;
935e57b2d0eSRandall Stewart 	le->m_tail = m_last(m);
936e57b2d0eSRandall Stewart }
937e57b2d0eSRandall Stewart 
938e57b2d0eSRandall Stewart static void
9399ca874cfSHans Petter Selasky tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
940e57b2d0eSRandall Stewart {
9419ca874cfSHans Petter Selasky 	struct lro_parser *pa;
9429ca874cfSHans Petter Selasky 
943e57b2d0eSRandall Stewart 	/*
9449ca874cfSHans Petter Selasky 	 * Push up the stack of the current entry
9459ca874cfSHans Petter Selasky 	 * and replace it with "m".
946e57b2d0eSRandall Stewart 	 */
947e57b2d0eSRandall Stewart 	struct mbuf *msave;
948e57b2d0eSRandall Stewart 
949e57b2d0eSRandall Stewart 	/* Grab off the next and save it */
950e57b2d0eSRandall Stewart 	msave = le->m_head->m_nextpkt;
951e57b2d0eSRandall Stewart 	le->m_head->m_nextpkt = NULL;
9529ca874cfSHans Petter Selasky 
9539ca874cfSHans Petter Selasky 	/* Now push out the old entry */
9549ca874cfSHans Petter Selasky 	tcp_flush_out_entry(lc, le);
9559ca874cfSHans Petter Selasky 
9569ca874cfSHans Petter Selasky 	/* Re-parse new header, should not fail. */
9579ca874cfSHans Petter Selasky 	pa = tcp_lro_parser(m, &le->outer, &le->inner, false);
9589ca874cfSHans Petter Selasky 	KASSERT(pa != NULL,
9599ca874cfSHans Petter Selasky 	    ("tcp_push_and_replace: LRO parser failed on m=%p\n", m));
9609ca874cfSHans Petter Selasky 
961e57b2d0eSRandall Stewart 	/*
9629ca874cfSHans Petter Selasky 	 * Now to replace the data properly in the entry
9639ca874cfSHans Petter Selasky 	 * we have to reset the TCP header and
964e57b2d0eSRandall Stewart 	 * other fields.
965e57b2d0eSRandall Stewart 	 */
9669ca874cfSHans Petter Selasky 	tcp_set_entry_to_mbuf(lc, le, m, pa->tcp);
9679ca874cfSHans Petter Selasky 
968e57b2d0eSRandall Stewart 	/* Restore the next list */
969e57b2d0eSRandall Stewart 	m->m_nextpkt = msave;
970e57b2d0eSRandall Stewart }
971e57b2d0eSRandall Stewart 
972e57b2d0eSRandall Stewart static void
97324fe6643SRyan Stone tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p)
9749ca874cfSHans Petter Selasky {
97524fe6643SRyan Stone 	struct mbuf *m;
9769ca874cfSHans Petter Selasky 	uint32_t csum;
9779ca874cfSHans Petter Selasky 
97824fe6643SRyan Stone 	m = le->m_head;
9799ca874cfSHans Petter Selasky 	if (m->m_pkthdr.lro_nsegs == 1) {
9809ca874cfSHans Petter Selasky 		/* Compute relative checksum. */
9819ca874cfSHans Petter Selasky 		csum = p->m_pkthdr.lro_tcp_d_csum;
9829ca874cfSHans Petter Selasky 	} else {
9839ca874cfSHans Petter Selasky 		/* Merge TCP data checksums. */
9849ca874cfSHans Petter Selasky 		csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum +
9859ca874cfSHans Petter Selasky 		    (uint32_t)p->m_pkthdr.lro_tcp_d_csum;
9869ca874cfSHans Petter Selasky 		while (csum > 0xffff)
9879ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
9889ca874cfSHans Petter Selasky 	}
9899ca874cfSHans Petter Selasky 
9909ca874cfSHans Petter Selasky 	/* Update various counters. */
9919ca874cfSHans Petter Selasky 	m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len;
9929ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_csum = csum;
9939ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len;
9949ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs;
99524fe6643SRyan Stone 	le->needs_merge = 1;
9969ca874cfSHans Petter Selasky }
9979ca874cfSHans Petter Selasky 
9989ca874cfSHans Petter Selasky static void
9999ca874cfSHans Petter Selasky tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le)
1000e57b2d0eSRandall Stewart {
1001e57b2d0eSRandall Stewart 	/*
1002e57b2d0eSRandall Stewart 	 * Walk through the mbuf chain we
1003e57b2d0eSRandall Stewart 	 * have on tap and compress/condense
1004e57b2d0eSRandall Stewart 	 * as required.
1005e57b2d0eSRandall Stewart 	 */
1006e57b2d0eSRandall Stewart 	uint32_t *ts_ptr;
1007e57b2d0eSRandall Stewart 	struct mbuf *m;
1008e57b2d0eSRandall Stewart 	struct tcphdr *th;
10099ca874cfSHans Petter Selasky 	uint32_t tcp_data_len_total;
10109ca874cfSHans Petter Selasky 	uint32_t tcp_data_seg_total;
10119ca874cfSHans Petter Selasky 	uint16_t tcp_data_len;
10129ca874cfSHans Petter Selasky 	uint16_t tcp_opt_len;
1013e57b2d0eSRandall Stewart 
1014e57b2d0eSRandall Stewart 	/*
1015e57b2d0eSRandall Stewart 	 * First we must check the lead (m_head)
1016e57b2d0eSRandall Stewart 	 * we must make sure that it is *not*
1017e57b2d0eSRandall Stewart 	 * something that should be sent up
1018e57b2d0eSRandall Stewart 	 * right away (sack etc).
1019e57b2d0eSRandall Stewart 	 */
1020e57b2d0eSRandall Stewart again:
1021e57b2d0eSRandall Stewart 	m = le->m_head->m_nextpkt;
1022e57b2d0eSRandall Stewart 	if (m == NULL) {
10239ca874cfSHans Petter Selasky 		/* Just one left. */
1024e57b2d0eSRandall Stewart 		return;
1025e57b2d0eSRandall Stewart 	}
10269ca874cfSHans Petter Selasky 
10279ca874cfSHans Petter Selasky 	th = tcp_lro_get_th(m);
10289ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
10299ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
1030e57b2d0eSRandall Stewart 	ts_ptr = (uint32_t *)(th + 1);
10319ca874cfSHans Petter Selasky 
10329ca874cfSHans Petter Selasky 	if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
10339ca874cfSHans Petter Selasky 	    *ts_ptr != TCP_LRO_TS_OPTION)) {
1034e57b2d0eSRandall Stewart 		/*
1035e57b2d0eSRandall Stewart 		 * Its not the timestamp. We can't
1036e57b2d0eSRandall Stewart 		 * use this guy as the head.
1037e57b2d0eSRandall Stewart 		 */
1038e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
10399ca874cfSHans Petter Selasky 		tcp_push_and_replace(lc, le, m);
1040e57b2d0eSRandall Stewart 		goto again;
1041e57b2d0eSRandall Stewart 	}
10421ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
1043e57b2d0eSRandall Stewart 		/*
10441dadeab3SGordon Bergling 		 * Make sure that previously seen segments/ACKs are delivered
1045e57b2d0eSRandall Stewart 		 * before this segment, e.g. FIN.
1046e57b2d0eSRandall Stewart 		 */
1047e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
10489ca874cfSHans Petter Selasky 		tcp_push_and_replace(lc, le, m);
1049e57b2d0eSRandall Stewart 		goto again;
1050e57b2d0eSRandall Stewart 	}
1051e57b2d0eSRandall Stewart 	while((m = le->m_head->m_nextpkt) != NULL) {
1052e57b2d0eSRandall Stewart 		/*
1053e57b2d0eSRandall Stewart 		 * condense m into le, first
1054e57b2d0eSRandall Stewart 		 * pull m out of the list.
1055e57b2d0eSRandall Stewart 		 */
1056e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
1057e57b2d0eSRandall Stewart 		m->m_nextpkt = NULL;
1058e57b2d0eSRandall Stewart 		/* Setup my data */
10599ca874cfSHans Petter Selasky 		tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
10609ca874cfSHans Petter Selasky 		th = tcp_lro_get_th(m);
1061e57b2d0eSRandall Stewart 		ts_ptr = (uint32_t *)(th + 1);
10629ca874cfSHans Petter Selasky 		tcp_opt_len = (th->th_off << 2);
10639ca874cfSHans Petter Selasky 		tcp_opt_len -= sizeof(*th);
10649ca874cfSHans Petter Selasky 		tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len;
10659ca874cfSHans Petter Selasky 		tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs;
10669ca874cfSHans Petter Selasky 
10679ca874cfSHans Petter Selasky 		if (tcp_data_seg_total >= lc->lro_ackcnt_lim ||
10689ca874cfSHans Petter Selasky 		    tcp_data_len_total >= lc->lro_length_lim) {
1069e57b2d0eSRandall Stewart 			/* Flush now if appending will result in overflow. */
10709ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1071e57b2d0eSRandall Stewart 			goto again;
1072e57b2d0eSRandall Stewart 		}
10739ca874cfSHans Petter Selasky 		if (tcp_opt_len != 0 &&
10749ca874cfSHans Petter Selasky 		    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
10759ca874cfSHans Petter Selasky 		    *ts_ptr != TCP_LRO_TS_OPTION)) {
1076e57b2d0eSRandall Stewart 			/*
1077e57b2d0eSRandall Stewart 			 * Maybe a sack in the new one? We need to
1078e57b2d0eSRandall Stewart 			 * start all over after flushing the
1079e57b2d0eSRandall Stewart 			 * current le. We will go up to the beginning
1080e57b2d0eSRandall Stewart 			 * and flush it (calling the replace again possibly
1081e57b2d0eSRandall Stewart 			 * or just returning).
1082e57b2d0eSRandall Stewart 			 */
10839ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1084e57b2d0eSRandall Stewart 			goto again;
1085e57b2d0eSRandall Stewart 		}
10861ebf4607SRichard Scheffenegger 		if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
10879ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1088e57b2d0eSRandall Stewart 			goto again;
1089e57b2d0eSRandall Stewart 		}
10909ca874cfSHans Petter Selasky 		if (tcp_opt_len != 0) {
1091e57b2d0eSRandall Stewart 			uint32_t tsval = ntohl(*(ts_ptr + 1));
1092e57b2d0eSRandall Stewart 			/* Make sure timestamp values are increasing. */
1093e57b2d0eSRandall Stewart 			if (TSTMP_GT(le->tsval, tsval))  {
10949ca874cfSHans Petter Selasky 				tcp_push_and_replace(lc, le, m);
1095e57b2d0eSRandall Stewart 				goto again;
1096e57b2d0eSRandall Stewart 			}
1097e57b2d0eSRandall Stewart 			le->tsval = tsval;
1098e57b2d0eSRandall Stewart 			le->tsecr = *(ts_ptr + 2);
1099e57b2d0eSRandall Stewart 		}
1100e57b2d0eSRandall Stewart 		/* Try to append the new segment. */
1101e57b2d0eSRandall Stewart 		if (__predict_false(ntohl(th->th_seq) != le->next_seq ||
11021ebf4607SRichard Scheffenegger 				    ((tcp_get_flags(th) & TH_ACK) !=
11033284f492SRyan Stone 				      (le->flags & TH_ACK)) ||
1104e57b2d0eSRandall Stewart 				    (tcp_data_len == 0 &&
1105e57b2d0eSRandall Stewart 				     le->ack_seq == th->th_ack &&
1106e57b2d0eSRandall Stewart 				     le->window == th->th_win))) {
11073284f492SRyan Stone 			/* Out of order packet, non-ACK + ACK or dup ACK. */
11089ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1109e57b2d0eSRandall Stewart 			goto again;
1110e57b2d0eSRandall Stewart 		}
11119ca874cfSHans Petter Selasky 		if (tcp_data_len != 0 ||
11129ca874cfSHans Petter Selasky 		    SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1113e57b2d0eSRandall Stewart 			le->next_seq += tcp_data_len;
1114e57b2d0eSRandall Stewart 			le->ack_seq = th->th_ack;
1115e57b2d0eSRandall Stewart 			le->window = th->th_win;
111624fe6643SRyan Stone 			le->needs_merge = 1;
1117e57b2d0eSRandall Stewart 		} else if (th->th_ack == le->ack_seq) {
111824fe6643SRyan Stone 			if (WIN_GT(th->th_win, le->window)) {
111924fe6643SRyan Stone 				le->window = th->th_win;
112024fe6643SRyan Stone 				le->needs_merge = 1;
112124fe6643SRyan Stone 			}
1122e57b2d0eSRandall Stewart 		}
11239ca874cfSHans Petter Selasky 
1124e57b2d0eSRandall Stewart 		if (tcp_data_len == 0) {
1125e57b2d0eSRandall Stewart 			m_freem(m);
1126e57b2d0eSRandall Stewart 			continue;
1127e57b2d0eSRandall Stewart 		}
11289ca874cfSHans Petter Selasky 
11299ca874cfSHans Petter Selasky 		/* Merge TCP data checksum and length to head mbuf. */
113024fe6643SRyan Stone 		tcp_lro_mbuf_append_pkthdr(le, m);
11319ca874cfSHans Petter Selasky 
1132e57b2d0eSRandall Stewart 		/*
1133e57b2d0eSRandall Stewart 		 * Adjust the mbuf so that m_data points to the first byte of
1134e57b2d0eSRandall Stewart 		 * the ULP payload.  Adjust the mbuf to avoid complications and
1135e57b2d0eSRandall Stewart 		 * append new segment to existing mbuf chain.
1136e57b2d0eSRandall Stewart 		 */
1137e57b2d0eSRandall Stewart 		m_adj(m, m->m_pkthdr.len - tcp_data_len);
1138e57b2d0eSRandall Stewart 		m_demote_pkthdr(m);
1139e57b2d0eSRandall Stewart 		le->m_tail->m_next = m;
1140e57b2d0eSRandall Stewart 		le->m_tail = m_last(m);
1141e57b2d0eSRandall Stewart 	}
1142e57b2d0eSRandall Stewart }
1143e57b2d0eSRandall Stewart 
1144373013b0SConrad Meyer #ifdef TCPHPTS
1145e57b2d0eSRandall Stewart static void
11469ca874cfSHans Petter Selasky tcp_queue_pkts(struct inpcb *inp, struct tcpcb *tp, struct lro_entry *le)
1147e57b2d0eSRandall Stewart {
11489ca874cfSHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
1149e57b2d0eSRandall Stewart 	if (tp->t_in_pkt == NULL) {
1150e57b2d0eSRandall Stewart 		/* Nothing yet there */
1151e57b2d0eSRandall Stewart 		tp->t_in_pkt = le->m_head;
1152e57b2d0eSRandall Stewart 		tp->t_tail_pkt = le->m_last_mbuf;
1153e57b2d0eSRandall Stewart 	} else {
1154e57b2d0eSRandall Stewart 		/* Already some there */
1155e57b2d0eSRandall Stewart 		tp->t_tail_pkt->m_nextpkt = le->m_head;
1156e57b2d0eSRandall Stewart 		tp->t_tail_pkt = le->m_last_mbuf;
1157e57b2d0eSRandall Stewart 	}
1158e57b2d0eSRandall Stewart 	le->m_head = NULL;
1159e57b2d0eSRandall Stewart 	le->m_last_mbuf = NULL;
1160e57b2d0eSRandall Stewart }
1161e57b2d0eSRandall Stewart 
116269a34e8dSRandall Stewart static struct mbuf *
11639ca874cfSHans Petter Selasky tcp_lro_get_last_if_ackcmp(struct lro_ctrl *lc, struct lro_entry *le,
11649ca874cfSHans Petter Selasky     struct inpcb *inp, int32_t *new_m)
1165e57b2d0eSRandall Stewart {
116669a34e8dSRandall Stewart 	struct tcpcb *tp;
11679ca874cfSHans Petter Selasky 	struct mbuf *m;
1168e57b2d0eSRandall Stewart 
116969a34e8dSRandall Stewart 	tp = intotcpcb(inp);
11709ca874cfSHans Petter Selasky 	if (__predict_false(tp == NULL))
11719ca874cfSHans Petter Selasky 		return (NULL);
11729ca874cfSHans Petter Selasky 
117369a34e8dSRandall Stewart 	/* Look at the last mbuf if any in queue */
117469a34e8dSRandall Stewart 	m = tp->t_tail_pkt;
11759ca874cfSHans Petter Selasky 	if (m != NULL && (m->m_flags & M_ACKCMP) != 0) {
11769ca874cfSHans Petter Selasky 		if (M_TRAILINGSPACE(m) >= sizeof(struct tcp_ackent)) {
11779ca874cfSHans Petter Selasky 			tcp_lro_log(tp, lc, le, NULL, 23, 0, 0, 0, 0);
11789ca874cfSHans Petter Selasky 			*new_m = 0;
11799ca874cfSHans Petter Selasky 			counter_u64_add(tcp_extra_mbuf, 1);
11809ca874cfSHans Petter Selasky 			return (m);
118169a34e8dSRandall Stewart 		} else {
11829ca874cfSHans Petter Selasky 			/* Mark we ran out of space */
118369a34e8dSRandall Stewart 			inp->inp_flags2 |= INP_MBUF_L_ACKS;
118469a34e8dSRandall Stewart 		}
118569a34e8dSRandall Stewart 	}
11869ca874cfSHans Petter Selasky 	/* Decide mbuf size. */
11879ca874cfSHans Petter Selasky 	if (inp->inp_flags2 & INP_MBUF_L_ACKS)
11889ca874cfSHans Petter Selasky 		m = m_getcl(M_NOWAIT, MT_DATA, M_ACKCMP | M_PKTHDR);
11899ca874cfSHans Petter Selasky 	else
11909ca874cfSHans Petter Selasky 		m = m_gethdr(M_NOWAIT, MT_DATA);
11919ca874cfSHans Petter Selasky 
11929ca874cfSHans Petter Selasky 	if (__predict_false(m == NULL)) {
11939ca874cfSHans Petter Selasky 		counter_u64_add(tcp_would_have_but, 1);
11949ca874cfSHans Petter Selasky 		return (NULL);
119569a34e8dSRandall Stewart 	}
11969ca874cfSHans Petter Selasky 	counter_u64_add(tcp_comp_total, 1);
11979ca874cfSHans Petter Selasky 	m->m_flags |= M_ACKCMP;
11989ca874cfSHans Petter Selasky 	*new_m = 1;
119969a34e8dSRandall Stewart 	return (m);
120069a34e8dSRandall Stewart }
120169a34e8dSRandall Stewart 
120269a34e8dSRandall Stewart static struct inpcb *
12039ca874cfSHans Petter Selasky tcp_lro_lookup(struct ifnet *ifp, struct lro_parser *pa)
120469a34e8dSRandall Stewart {
12059ca874cfSHans Petter Selasky 	struct inpcb *inp;
120669a34e8dSRandall Stewart 
12079ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
1208e57b2d0eSRandall Stewart #ifdef INET6
12099ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
12109ca874cfSHans Petter Selasky 		inp = in6_pcblookup(&V_tcbinfo,
12119ca874cfSHans Petter Selasky 		    &pa->data.s_addr.v6,
12129ca874cfSHans Petter Selasky 		    pa->data.s_port,
12139ca874cfSHans Petter Selasky 		    &pa->data.d_addr.v6,
12149ca874cfSHans Petter Selasky 		    pa->data.d_port,
1215e57b2d0eSRandall Stewart 		    INPLOOKUP_WLOCKPCB,
12169ca874cfSHans Petter Selasky 		    ifp);
1217e57b2d0eSRandall Stewart 		break;
1218e57b2d0eSRandall Stewart #endif
1219e57b2d0eSRandall Stewart #ifdef INET
12209ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
12219ca874cfSHans Petter Selasky 		inp = in_pcblookup(&V_tcbinfo,
12229ca874cfSHans Petter Selasky 		    pa->data.s_addr.v4,
12239ca874cfSHans Petter Selasky 		    pa->data.s_port,
12249ca874cfSHans Petter Selasky 		    pa->data.d_addr.v4,
12259ca874cfSHans Petter Selasky 		    pa->data.d_port,
1226e57b2d0eSRandall Stewart 		    INPLOOKUP_WLOCKPCB,
12279ca874cfSHans Petter Selasky 		    ifp);
1228e57b2d0eSRandall Stewart 		break;
1229e57b2d0eSRandall Stewart #endif
12309ca874cfSHans Petter Selasky 	default:
12319ca874cfSHans Petter Selasky 		inp = NULL;
12329ca874cfSHans Petter Selasky 		break;
1233e57b2d0eSRandall Stewart 	}
123469a34e8dSRandall Stewart 	return (inp);
123569a34e8dSRandall Stewart }
123669a34e8dSRandall Stewart 
12379ca874cfSHans Petter Selasky static inline bool
12389ca874cfSHans Petter Selasky tcp_lro_ack_valid(struct mbuf *m, struct tcphdr *th, uint32_t **ppts, bool *other_opts)
12399ca874cfSHans Petter Selasky {
12409ca874cfSHans Petter Selasky 	/*
12419ca874cfSHans Petter Selasky 	 * This function returns two bits of valuable information.
12429ca874cfSHans Petter Selasky 	 * a) Is what is present capable of being ack-compressed,
12439ca874cfSHans Petter Selasky 	 *    we can ack-compress if there is no options or just
12449ca874cfSHans Petter Selasky 	 *    a timestamp option, and of course the th_flags must
12459ca874cfSHans Petter Selasky 	 *    be correct as well.
12469ca874cfSHans Petter Selasky 	 * b) Our other options present such as SACK. This is
12479ca874cfSHans Petter Selasky 	 *    used to determine if we want to wakeup or not.
12489ca874cfSHans Petter Selasky 	 */
12499ca874cfSHans Petter Selasky 	bool ret = true;
12509ca874cfSHans Petter Selasky 
12519ca874cfSHans Petter Selasky 	switch (th->th_off << 2) {
12529ca874cfSHans Petter Selasky 	case (sizeof(*th) + TCPOLEN_TSTAMP_APPA):
12539ca874cfSHans Petter Selasky 		*ppts = (uint32_t *)(th + 1);
12549ca874cfSHans Petter Selasky 		/* Check if we have only one timestamp option. */
12559ca874cfSHans Petter Selasky 		if (**ppts == TCP_LRO_TS_OPTION)
12569ca874cfSHans Petter Selasky 			*other_opts = false;
12579ca874cfSHans Petter Selasky 		else {
12589ca874cfSHans Petter Selasky 			*other_opts = true;
12599ca874cfSHans Petter Selasky 			ret = false;
12609ca874cfSHans Petter Selasky 		}
12619ca874cfSHans Petter Selasky 		break;
12629ca874cfSHans Petter Selasky 	case (sizeof(*th)):
12639ca874cfSHans Petter Selasky 		/* No options. */
12649ca874cfSHans Petter Selasky 		*ppts = NULL;
12659ca874cfSHans Petter Selasky 		*other_opts = false;
12669ca874cfSHans Petter Selasky 		break;
12679ca874cfSHans Petter Selasky 	default:
12689ca874cfSHans Petter Selasky 		*ppts = NULL;
12699ca874cfSHans Petter Selasky 		*other_opts = true;
12709ca874cfSHans Petter Selasky 		ret = false;
12719ca874cfSHans Petter Selasky 		break;
12729ca874cfSHans Petter Selasky 	}
12739ca874cfSHans Petter Selasky 	/* For ACKCMP we only accept ACK, PUSH, ECE and CWR. */
12741ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH | TH_ECE | TH_CWR)) != 0)
12759ca874cfSHans Petter Selasky 		ret = false;
12769ca874cfSHans Petter Selasky 	/* If it has data on it we cannot compress it */
12779ca874cfSHans Petter Selasky 	if (m->m_pkthdr.lro_tcp_d_len)
12789ca874cfSHans Petter Selasky 		ret = false;
12799ca874cfSHans Petter Selasky 
12809ca874cfSHans Petter Selasky 	/* ACK flag must be set. */
12811ebf4607SRichard Scheffenegger 	if (!(tcp_get_flags(th) & TH_ACK))
12829ca874cfSHans Petter Selasky 		ret = false;
12839ca874cfSHans Petter Selasky 	return (ret);
12849ca874cfSHans Petter Selasky }
12859ca874cfSHans Petter Selasky 
12869ca874cfSHans Petter Selasky static int
12879ca874cfSHans Petter Selasky tcp_lro_flush_tcphpts(struct lro_ctrl *lc, struct lro_entry *le)
12889ca874cfSHans Petter Selasky {
12899ca874cfSHans Petter Selasky 	struct inpcb *inp;
12909ca874cfSHans Petter Selasky 	struct tcpcb *tp;
12919ca874cfSHans Petter Selasky 	struct mbuf **pp, *cmp, *mv_to;
1292*2c6ff1d6SAndrew Gallatin 	struct ifnet *lagg_ifp;
1293*2c6ff1d6SAndrew Gallatin 	bool bpf_req, lagg_bpf_req, should_wake;
12949ca874cfSHans Petter Selasky 
12959ca874cfSHans Petter Selasky 	/* Check if packet doesn't belongs to our network interface. */
12969ca874cfSHans Petter Selasky 	if ((tcplro_stacks_wanting_mbufq == 0) ||
12979ca874cfSHans Petter Selasky 	    (le->outer.data.vlan_id != 0) ||
12989ca874cfSHans Petter Selasky 	    (le->inner.data.lro_type != LRO_TYPE_NONE))
12999ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
13009ca874cfSHans Petter Selasky 
13019ca874cfSHans Petter Selasky #ifdef INET6
13029ca874cfSHans Petter Selasky 	/*
13039ca874cfSHans Petter Selasky 	 * Be proactive about unspecified IPv6 address in source. As
13049ca874cfSHans Petter Selasky 	 * we use all-zero to indicate unbounded/unconnected pcb,
13059ca874cfSHans Petter Selasky 	 * unspecified IPv6 address can be used to confuse us.
13069ca874cfSHans Petter Selasky 	 *
13079ca874cfSHans Petter Selasky 	 * Note that packets with unspecified IPv6 destination is
13089ca874cfSHans Petter Selasky 	 * already dropped in ip6_input.
13099ca874cfSHans Petter Selasky 	 */
13109ca874cfSHans Petter Selasky 	if (__predict_false(le->outer.data.lro_type == LRO_TYPE_IPV6_TCP &&
13119ca874cfSHans Petter Selasky 	    IN6_IS_ADDR_UNSPECIFIED(&le->outer.data.s_addr.v6)))
13129ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
13139ca874cfSHans Petter Selasky 
13149ca874cfSHans Petter Selasky 	if (__predict_false(le->inner.data.lro_type == LRO_TYPE_IPV6_TCP &&
13159ca874cfSHans Petter Selasky 	    IN6_IS_ADDR_UNSPECIFIED(&le->inner.data.s_addr.v6)))
13169ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
131769a34e8dSRandall Stewart #endif
13189ca874cfSHans Petter Selasky 	/* Lookup inp, if any. */
13199ca874cfSHans Petter Selasky 	inp = tcp_lro_lookup(lc->ifp,
13209ca874cfSHans Petter Selasky 	    (le->inner.data.lro_type == LRO_TYPE_NONE) ? &le->outer : &le->inner);
13219ca874cfSHans Petter Selasky 	if (inp == NULL)
13229ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
132369a34e8dSRandall Stewart 
13249ca874cfSHans Petter Selasky 	counter_u64_add(tcp_inp_lro_locks_taken, 1);
13259ca874cfSHans Petter Selasky 
13269ca874cfSHans Petter Selasky 	/* Get TCP control structure. */
13279ca874cfSHans Petter Selasky 	tp = intotcpcb(inp);
13289ca874cfSHans Petter Selasky 
13299ca874cfSHans Petter Selasky 	/* Check if the inp is dead, Jim. */
13309ca874cfSHans Petter Selasky 	if (tp == NULL ||
1331db0ac6deSCy Schubert 	    (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))) {
13329ca874cfSHans Petter Selasky 		INP_WUNLOCK(inp);
13339ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
133469a34e8dSRandall Stewart 	}
1335d7955cc0SRandall Stewart 	if ((inp->inp_irq_cpu_set == 0)  && (lc->lro_cpu_is_set == 1)) {
1336d7955cc0SRandall Stewart 		inp->inp_irq_cpu = lc->lro_last_cpu;
1337d7955cc0SRandall Stewart 		inp->inp_irq_cpu_set = 1;
1338d7955cc0SRandall Stewart 	}
13399ca874cfSHans Petter Selasky 	/* Check if the transport doesn't support the needed optimizations. */
13409ca874cfSHans Petter Selasky 	if ((inp->inp_flags2 & (INP_SUPPORTS_MBUFQ | INP_MBUF_ACKCMP)) == 0) {
13419ca874cfSHans Petter Selasky 		INP_WUNLOCK(inp);
13429ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
134369a34e8dSRandall Stewart 	}
13449ca874cfSHans Petter Selasky 
13459ca874cfSHans Petter Selasky 	if (inp->inp_flags2 & INP_MBUF_QUEUE_READY)
13469ca874cfSHans Petter Selasky 		should_wake = false;
13479ca874cfSHans Petter Selasky 	else
13489ca874cfSHans Petter Selasky 		should_wake = true;
13499ca874cfSHans Petter Selasky 	/* Check if packets should be tapped to BPF. */
13509ca874cfSHans Petter Selasky 	bpf_req = bpf_peers_present(lc->ifp->if_bpf);
1351*2c6ff1d6SAndrew Gallatin 	lagg_bpf_req = false;
1352*2c6ff1d6SAndrew Gallatin 	lagg_ifp = NULL;
1353*2c6ff1d6SAndrew Gallatin 	if (lc->ifp->if_type == IFT_IEEE8023ADLAG ||
1354*2c6ff1d6SAndrew Gallatin 	    lc->ifp->if_type == IFT_INFINIBANDLAG) {
1355*2c6ff1d6SAndrew Gallatin 		struct lagg_port *lp = lc->ifp->if_lagg;
1356*2c6ff1d6SAndrew Gallatin 		struct lagg_softc *sc = lp->lp_softc;
1357*2c6ff1d6SAndrew Gallatin 
1358*2c6ff1d6SAndrew Gallatin 		lagg_ifp = sc->sc_ifp;
1359*2c6ff1d6SAndrew Gallatin 		if (lagg_ifp != NULL)
1360*2c6ff1d6SAndrew Gallatin 			lagg_bpf_req = bpf_peers_present(lagg_ifp->if_bpf);
1361*2c6ff1d6SAndrew Gallatin 	}
13629ca874cfSHans Petter Selasky 
13639ca874cfSHans Petter Selasky 	/* Strip and compress all the incoming packets. */
13649ca874cfSHans Petter Selasky 	cmp = NULL;
13659ca874cfSHans Petter Selasky 	for (pp = &le->m_head; *pp != NULL; ) {
13669ca874cfSHans Petter Selasky 		mv_to = NULL;
13679ca874cfSHans Petter Selasky 		if (do_bpf_strip_and_compress(inp, lc, le, pp,
1368*2c6ff1d6SAndrew Gallatin 			&cmp, &mv_to, &should_wake, bpf_req,
1369*2c6ff1d6SAndrew Gallatin 			lagg_bpf_req, lagg_ifp) == false) {
13709ca874cfSHans Petter Selasky 			/* Advance to next mbuf. */
13719ca874cfSHans Petter Selasky 			pp = &(*pp)->m_nextpkt;
13729ca874cfSHans Petter Selasky 		} else if (mv_to != NULL) {
13739ca874cfSHans Petter Selasky 			/* We are asked to move pp up */
13749ca874cfSHans Petter Selasky 			pp = &mv_to->m_nextpkt;
13759ca874cfSHans Petter Selasky 		}
13769ca874cfSHans Petter Selasky 	}
13779ca874cfSHans Petter Selasky 	/* Update "m_last_mbuf", if any. */
13789ca874cfSHans Petter Selasky 	if (pp == &le->m_head)
13799ca874cfSHans Petter Selasky 		le->m_last_mbuf = *pp;
13809ca874cfSHans Petter Selasky 	else
13819ca874cfSHans Petter Selasky 		le->m_last_mbuf = __containerof(pp, struct mbuf, m_nextpkt);
13829ca874cfSHans Petter Selasky 
13839ca874cfSHans Petter Selasky 	/* Check if any data mbufs left. */
13849ca874cfSHans Petter Selasky 	if (le->m_head != NULL) {
13859ca874cfSHans Petter Selasky 		counter_u64_add(tcp_inp_lro_direct_queue, 1);
1386a370832bSGleb Smirnoff 		tcp_lro_log(tp, lc, le, NULL, 22, 1, inp->inp_flags2, 0, 1);
13879ca874cfSHans Petter Selasky 		tcp_queue_pkts(inp, tp, le);
13889ca874cfSHans Petter Selasky 	}
13899ca874cfSHans Petter Selasky 	if (should_wake) {
13909ca874cfSHans Petter Selasky 		/* Wakeup */
13919ca874cfSHans Petter Selasky 		counter_u64_add(tcp_inp_lro_wokeup_queue, 1);
13929ca874cfSHans Petter Selasky 		if ((*tp->t_fb->tfb_do_queued_segments)(inp->inp_socket, tp, 0))
13939ca874cfSHans Petter Selasky 			inp = NULL;
13949ca874cfSHans Petter Selasky 	}
13959ca874cfSHans Petter Selasky 	if (inp != NULL)
13969ca874cfSHans Petter Selasky 		INP_WUNLOCK(inp);
13979ca874cfSHans Petter Selasky 	return (0);	/* Success. */
139869a34e8dSRandall Stewart }
139969a34e8dSRandall Stewart #endif
140069a34e8dSRandall Stewart 
140169a34e8dSRandall Stewart void
140269a34e8dSRandall Stewart tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
140369a34e8dSRandall Stewart {
14049ca874cfSHans Petter Selasky 	/* Only optimise if there are multiple packets waiting. */
140569a34e8dSRandall Stewart #ifdef TCPHPTS
14069ca874cfSHans Petter Selasky 	int error;
1407dc6ab77dSMichael Tuexen #endif
140869a34e8dSRandall Stewart 
1409dc6ab77dSMichael Tuexen 	NET_EPOCH_ASSERT();
1410dc6ab77dSMichael Tuexen #ifdef TCPHPTS
141169a34e8dSRandall Stewart 	CURVNET_SET(lc->ifp->if_vnet);
14129ca874cfSHans Petter Selasky 	error = tcp_lro_flush_tcphpts(lc, le);
14139ca874cfSHans Petter Selasky 	CURVNET_RESTORE();
14149ca874cfSHans Petter Selasky 	if (error != 0) {
14159ca874cfSHans Petter Selasky #endif
14169ca874cfSHans Petter Selasky 		tcp_lro_condense(lc, le);
14179ca874cfSHans Petter Selasky 		tcp_flush_out_entry(lc, le);
1418e57b2d0eSRandall Stewart #ifdef TCPHPTS
1419e57b2d0eSRandall Stewart 	}
1420e57b2d0eSRandall Stewart #endif
142162b5b6ecSBjoern A. Zeeb 	lc->lro_flushed++;
142262b5b6ecSBjoern A. Zeeb 	bzero(le, sizeof(*le));
14231ea44822SSepherosa Ziehau 	LIST_INSERT_HEAD(&lc->lro_free, le, next);
142462b5b6ecSBjoern A. Zeeb }
14256c5087a8SJack F Vogel 
1426fc271df3SHans Petter Selasky #ifdef HAVE_INLINE_FLSLL
1427fc271df3SHans Petter Selasky #define	tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1))
1428fc271df3SHans Petter Selasky #else
1429fc271df3SHans Petter Selasky static inline uint64_t
1430fc271df3SHans Petter Selasky tcp_lro_msb_64(uint64_t x)
1431e936121dSHans Petter Selasky {
1432fc271df3SHans Petter Selasky 	x |= (x >> 1);
1433fc271df3SHans Petter Selasky 	x |= (x >> 2);
1434fc271df3SHans Petter Selasky 	x |= (x >> 4);
1435fc271df3SHans Petter Selasky 	x |= (x >> 8);
1436fc271df3SHans Petter Selasky 	x |= (x >> 16);
1437fc271df3SHans Petter Selasky 	x |= (x >> 32);
1438fc271df3SHans Petter Selasky 	return (x & ~(x >> 1));
1439fc271df3SHans Petter Selasky }
1440fc271df3SHans Petter Selasky #endif
1441e936121dSHans Petter Selasky 
1442fc271df3SHans Petter Selasky /*
1443fc271df3SHans Petter Selasky  * The tcp_lro_sort() routine is comparable to qsort(), except it has
1444fc271df3SHans Petter Selasky  * a worst case complexity limit of O(MIN(N,64)*N), where N is the
1445fc271df3SHans Petter Selasky  * number of elements to sort and 64 is the number of sequence bits
1446fc271df3SHans Petter Selasky  * available. The algorithm is bit-slicing the 64-bit sequence number,
1447fc271df3SHans Petter Selasky  * sorting one bit at a time from the most significant bit until the
1448ec668905SHans Petter Selasky  * least significant one, skipping the constant bits. This is
1449ec668905SHans Petter Selasky  * typically called a radix sort.
1450fc271df3SHans Petter Selasky  */
1451fc271df3SHans Petter Selasky static void
1452fc271df3SHans Petter Selasky tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size)
1453fc271df3SHans Petter Selasky {
1454fc271df3SHans Petter Selasky 	struct lro_mbuf_sort temp;
1455fc271df3SHans Petter Selasky 	uint64_t ones;
1456fc271df3SHans Petter Selasky 	uint64_t zeros;
1457fc271df3SHans Petter Selasky 	uint32_t x;
1458fc271df3SHans Petter Selasky 	uint32_t y;
1459e936121dSHans Petter Selasky 
1460fc271df3SHans Petter Selasky repeat:
1461ec668905SHans Petter Selasky 	/* for small arrays insertion sort is faster */
1462fc271df3SHans Petter Selasky 	if (size <= 12) {
1463ec668905SHans Petter Selasky 		for (x = 1; x < size; x++) {
1464fc271df3SHans Petter Selasky 			temp = parray[x];
1465ec668905SHans Petter Selasky 			for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--)
1466ec668905SHans Petter Selasky 				parray[y] = parray[y - 1];
1467fc271df3SHans Petter Selasky 			parray[y] = temp;
1468fc271df3SHans Petter Selasky 		}
1469fc271df3SHans Petter Selasky 		return;
1470fc271df3SHans Petter Selasky 	}
1471e936121dSHans Petter Selasky 
1472fc271df3SHans Petter Selasky 	/* compute sequence bits which are constant */
1473fc271df3SHans Petter Selasky 	ones = 0;
1474fc271df3SHans Petter Selasky 	zeros = 0;
1475fc271df3SHans Petter Selasky 	for (x = 0; x != size; x++) {
1476fc271df3SHans Petter Selasky 		ones |= parray[x].seq;
1477fc271df3SHans Petter Selasky 		zeros |= ~parray[x].seq;
1478fc271df3SHans Petter Selasky 	}
1479fc271df3SHans Petter Selasky 
1480fc271df3SHans Petter Selasky 	/* compute bits which are not constant into "ones" */
1481fc271df3SHans Petter Selasky 	ones &= zeros;
1482fc271df3SHans Petter Selasky 	if (ones == 0)
1483fc271df3SHans Petter Selasky 		return;
1484fc271df3SHans Petter Selasky 
1485fc271df3SHans Petter Selasky 	/* pick the most significant bit which is not constant */
1486fc271df3SHans Petter Selasky 	ones = tcp_lro_msb_64(ones);
1487fc271df3SHans Petter Selasky 
1488fc271df3SHans Petter Selasky 	/*
1489fc271df3SHans Petter Selasky 	 * Move entries having cleared sequence bits to the beginning
1490fc271df3SHans Petter Selasky 	 * of the array:
1491fc271df3SHans Petter Selasky 	 */
1492fc271df3SHans Petter Selasky 	for (x = y = 0; y != size; y++) {
1493fc271df3SHans Petter Selasky 		/* skip set bits */
1494fc271df3SHans Petter Selasky 		if (parray[y].seq & ones)
1495fc271df3SHans Petter Selasky 			continue;
1496fc271df3SHans Petter Selasky 		/* swap entries */
1497fc271df3SHans Petter Selasky 		temp = parray[x];
1498fc271df3SHans Petter Selasky 		parray[x] = parray[y];
1499fc271df3SHans Petter Selasky 		parray[y] = temp;
1500fc271df3SHans Petter Selasky 		x++;
1501fc271df3SHans Petter Selasky 	}
1502fc271df3SHans Petter Selasky 
1503fc271df3SHans Petter Selasky 	KASSERT(x != 0 && x != size, ("Memory is corrupted\n"));
1504fc271df3SHans Petter Selasky 
1505fc271df3SHans Petter Selasky 	/* sort zeros */
1506fc271df3SHans Petter Selasky 	tcp_lro_sort(parray, x);
1507fc271df3SHans Petter Selasky 
1508fc271df3SHans Petter Selasky 	/* sort ones */
1509fc271df3SHans Petter Selasky 	parray += x;
1510fc271df3SHans Petter Selasky 	size -= x;
1511fc271df3SHans Petter Selasky 	goto repeat;
1512e936121dSHans Petter Selasky }
1513e936121dSHans Petter Selasky 
1514e936121dSHans Petter Selasky void
1515e936121dSHans Petter Selasky tcp_lro_flush_all(struct lro_ctrl *lc)
1516e936121dSHans Petter Selasky {
1517fc271df3SHans Petter Selasky 	uint64_t seq;
1518fc271df3SHans Petter Selasky 	uint64_t nseq;
1519e936121dSHans Petter Selasky 	unsigned x;
1520e936121dSHans Petter Selasky 
1521dc6ab77dSMichael Tuexen 	NET_EPOCH_ASSERT();
1522e936121dSHans Petter Selasky 	/* check if no mbufs to flush */
15236dd38b87SSepherosa Ziehau 	if (lc->lro_mbuf_count == 0)
1524e936121dSHans Petter Selasky 		goto done;
1525d7955cc0SRandall Stewart 	if (lc->lro_cpu_is_set == 0) {
1526d7955cc0SRandall Stewart 		if (lc->lro_last_cpu == curcpu) {
1527d7955cc0SRandall Stewart 			lc->lro_cnt_of_same_cpu++;
1528d7955cc0SRandall Stewart 			/* Have we reached the threshold to declare a cpu? */
1529d7955cc0SRandall Stewart 			if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh)
1530d7955cc0SRandall Stewart 				lc->lro_cpu_is_set = 1;
1531d7955cc0SRandall Stewart 		} else {
1532d7955cc0SRandall Stewart 			lc->lro_last_cpu = curcpu;
1533d7955cc0SRandall Stewart 			lc->lro_cnt_of_same_cpu = 0;
1534d7955cc0SRandall Stewart 		}
1535d7955cc0SRandall Stewart 	}
1536a9b66dbdSHans Petter Selasky 	CURVNET_SET(lc->ifp->if_vnet);
1537a9b66dbdSHans Petter Selasky 
15389ca874cfSHans Petter Selasky 	/* get current time */
1539b45daaeaSRandall Stewart 	binuptime(&lc->lro_last_queue_time);
15409ca874cfSHans Petter Selasky 
1541e936121dSHans Petter Selasky 	/* sort all mbufs according to stream */
1542fc271df3SHans Petter Selasky 	tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count);
1543e936121dSHans Petter Selasky 
1544e936121dSHans Petter Selasky 	/* input data into LRO engine, stream by stream */
1545fc271df3SHans Petter Selasky 	seq = 0;
1546e936121dSHans Petter Selasky 	for (x = 0; x != lc->lro_mbuf_count; x++) {
1547e936121dSHans Petter Selasky 		struct mbuf *mb;
1548e936121dSHans Petter Selasky 
1549fc271df3SHans Petter Selasky 		/* get mbuf */
1550fc271df3SHans Petter Selasky 		mb = lc->lro_mbuf_data[x].mb;
1551fc271df3SHans Petter Selasky 
1552fc271df3SHans Petter Selasky 		/* get sequence number, masking away the packet index */
1553fc271df3SHans Petter Selasky 		nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24);
1554e936121dSHans Petter Selasky 
1555e936121dSHans Petter Selasky 		/* check for new stream */
1556fc271df3SHans Petter Selasky 		if (seq != nseq) {
1557fc271df3SHans Petter Selasky 			seq = nseq;
1558e936121dSHans Petter Selasky 
1559e936121dSHans Petter Selasky 			/* flush active streams */
15606dd38b87SSepherosa Ziehau 			tcp_lro_rx_done(lc);
1561e936121dSHans Petter Selasky 		}
1562fc271df3SHans Petter Selasky 
1563e936121dSHans Petter Selasky 		/* add packet to LRO engine */
15649ca874cfSHans Petter Selasky 		if (tcp_lro_rx_common(lc, mb, 0, false) != 0) {
1565e936121dSHans Petter Selasky 			/* input packet to network layer */
1566e936121dSHans Petter Selasky 			(*lc->ifp->if_input)(lc->ifp, mb);
1567e936121dSHans Petter Selasky 			lc->lro_queued++;
1568e936121dSHans Petter Selasky 			lc->lro_flushed++;
1569e936121dSHans Petter Selasky 		}
1570e936121dSHans Petter Selasky 	}
1571a9b66dbdSHans Petter Selasky 	CURVNET_RESTORE();
1572e936121dSHans Petter Selasky done:
1573e936121dSHans Petter Selasky 	/* flush active streams */
15746dd38b87SSepherosa Ziehau 	tcp_lro_rx_done(lc);
15756dd38b87SSepherosa Ziehau 
1576d7955cc0SRandall Stewart #ifdef TCPHPTS
1577d7955cc0SRandall Stewart 	tcp_run_hpts();
1578d7955cc0SRandall Stewart #endif
1579e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
158062b5b6ecSBjoern A. Zeeb }
15816c5087a8SJack F Vogel 
1582ab4fad4bSRandall Stewart #ifdef TCPHPTS
158369a34e8dSRandall Stewart static void
15849ca874cfSHans Petter Selasky build_ack_entry(struct tcp_ackent *ae, struct tcphdr *th, struct mbuf *m,
15859ca874cfSHans Petter Selasky     uint32_t *ts_ptr, uint16_t iptos)
158669a34e8dSRandall Stewart {
158769a34e8dSRandall Stewart 	/*
15889ca874cfSHans Petter Selasky 	 * Given a TCP ACK, summarize it down into the small TCP ACK
15899ca874cfSHans Petter Selasky 	 * entry.
159069a34e8dSRandall Stewart 	 */
159169a34e8dSRandall Stewart 	ae->timestamp = m->m_pkthdr.rcv_tstmp;
159269a34e8dSRandall Stewart 	if (m->m_flags & M_TSTMP_LRO)
159369a34e8dSRandall Stewart 		ae->flags = TSTMP_LRO;
159469a34e8dSRandall Stewart 	else if (m->m_flags & M_TSTMP)
159569a34e8dSRandall Stewart 		ae->flags = TSTMP_HDWR;
159669a34e8dSRandall Stewart 	ae->seq = ntohl(th->th_seq);
159769a34e8dSRandall Stewart 	ae->ack = ntohl(th->th_ack);
15981ebf4607SRichard Scheffenegger 	ae->flags |= tcp_get_flags(th);
15999ca874cfSHans Petter Selasky 	if (ts_ptr != NULL) {
16009ca874cfSHans Petter Selasky 		ae->ts_value = ntohl(ts_ptr[1]);
16019ca874cfSHans Petter Selasky 		ae->ts_echo = ntohl(ts_ptr[2]);
160269a34e8dSRandall Stewart 		ae->flags |= HAS_TSTMP;
160369a34e8dSRandall Stewart 	}
160469a34e8dSRandall Stewart 	ae->win = ntohs(th->th_win);
160569a34e8dSRandall Stewart 	ae->codepoint = iptos;
160669a34e8dSRandall Stewart }
160769a34e8dSRandall Stewart 
160869a34e8dSRandall Stewart /*
16099ca874cfSHans Petter Selasky  * Do BPF tap for either ACK_CMP packets or MBUF QUEUE type packets
16109ca874cfSHans Petter Selasky  * and strip all, but the IPv4/IPv6 header.
161169a34e8dSRandall Stewart  */
16129ca874cfSHans Petter Selasky static bool
16139ca874cfSHans Petter Selasky do_bpf_strip_and_compress(struct inpcb *inp, struct lro_ctrl *lc,
16149ca874cfSHans Petter Selasky     struct lro_entry *le, struct mbuf **pp, struct mbuf **cmp, struct mbuf **mv_to,
1615*2c6ff1d6SAndrew Gallatin     bool *should_wake, bool bpf_req, bool lagg_bpf_req, struct ifnet *lagg_ifp)
16169ca874cfSHans Petter Selasky {
16179ca874cfSHans Petter Selasky 	union {
16189ca874cfSHans Petter Selasky 		void *ptr;
16199ca874cfSHans Petter Selasky 		struct ip *ip4;
16209ca874cfSHans Petter Selasky 		struct ip6_hdr *ip6;
16219ca874cfSHans Petter Selasky 	} l3;
16229ca874cfSHans Petter Selasky 	struct mbuf *m;
16239ca874cfSHans Petter Selasky 	struct mbuf *nm;
162469a34e8dSRandall Stewart 	struct tcphdr *th;
16259ca874cfSHans Petter Selasky 	struct tcp_ackent *ack_ent;
16269ca874cfSHans Petter Selasky 	uint32_t *ts_ptr;
16279ca874cfSHans Petter Selasky 	int32_t n_mbuf;
16289ca874cfSHans Petter Selasky 	bool other_opts, can_compress;
1629bb5cd80eSHans Petter Selasky 	uint8_t lro_type;
16309ca874cfSHans Petter Selasky 	uint16_t iptos;
16319ca874cfSHans Petter Selasky 	int tcp_hdr_offset;
16329ca874cfSHans Petter Selasky 	int idx;
163369a34e8dSRandall Stewart 
16349ca874cfSHans Petter Selasky 	/* Get current mbuf. */
16359ca874cfSHans Petter Selasky 	m = *pp;
163669a34e8dSRandall Stewart 
163769a34e8dSRandall Stewart 	/* Let the BPF see the packet */
16389ca874cfSHans Petter Selasky 	if (__predict_false(bpf_req))
163969a34e8dSRandall Stewart 		ETHER_BPF_MTAP(lc->ifp, m);
16409ca874cfSHans Petter Selasky 
1641*2c6ff1d6SAndrew Gallatin 	if (__predict_false(lagg_bpf_req))
1642*2c6ff1d6SAndrew Gallatin 		ETHER_BPF_MTAP(lagg_ifp, m);
1643*2c6ff1d6SAndrew Gallatin 
16449ca874cfSHans Petter Selasky 	tcp_hdr_offset = m->m_pkthdr.lro_tcp_h_off;
16459ca874cfSHans Petter Selasky 	lro_type = le->inner.data.lro_type;
16469ca874cfSHans Petter Selasky 	switch (lro_type) {
16479ca874cfSHans Petter Selasky 	case LRO_TYPE_NONE:
16489ca874cfSHans Petter Selasky 		lro_type = le->outer.data.lro_type;
16499ca874cfSHans Petter Selasky 		switch (lro_type) {
16509ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV4_TCP:
16519ca874cfSHans Petter Selasky 			tcp_hdr_offset -= sizeof(*le->outer.ip4);
16529ca874cfSHans Petter Selasky 			m->m_pkthdr.lro_etype = ETHERTYPE_IP;
16539ca874cfSHans Petter Selasky 			break;
16549ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV6_TCP:
16559ca874cfSHans Petter Selasky 			tcp_hdr_offset -= sizeof(*le->outer.ip6);
16569ca874cfSHans Petter Selasky 			m->m_pkthdr.lro_etype = ETHERTYPE_IPV6;
16579ca874cfSHans Petter Selasky 			break;
16589ca874cfSHans Petter Selasky 		default:
16599ca874cfSHans Petter Selasky 			goto compressed;
16609ca874cfSHans Petter Selasky 		}
16619ca874cfSHans Petter Selasky 		break;
16629ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
16639ca874cfSHans Petter Selasky 		tcp_hdr_offset -= sizeof(*le->outer.ip4);
16649ca874cfSHans Petter Selasky 		m->m_pkthdr.lro_etype = ETHERTYPE_IP;
16659ca874cfSHans Petter Selasky 		break;
16669ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
16679ca874cfSHans Petter Selasky 		tcp_hdr_offset -= sizeof(*le->outer.ip6);
16689ca874cfSHans Petter Selasky 		m->m_pkthdr.lro_etype = ETHERTYPE_IPV6;
16699ca874cfSHans Petter Selasky 		break;
16709ca874cfSHans Petter Selasky 	default:
16719ca874cfSHans Petter Selasky 		goto compressed;
16729ca874cfSHans Petter Selasky 	}
16739ca874cfSHans Petter Selasky 
16749ca874cfSHans Petter Selasky 	MPASS(tcp_hdr_offset >= 0);
16759ca874cfSHans Petter Selasky 
16769ca874cfSHans Petter Selasky 	m_adj(m, tcp_hdr_offset);
167769a34e8dSRandall Stewart 	m->m_flags |= M_LRO_EHDRSTRP;
16789ca874cfSHans Petter Selasky 	m->m_flags &= ~M_ACKCMP;
16799ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_h_off -= tcp_hdr_offset;
168069a34e8dSRandall Stewart 
16819ca874cfSHans Petter Selasky 	th = tcp_lro_get_th(m);
168269a34e8dSRandall Stewart 
16839ca874cfSHans Petter Selasky 	th->th_sum = 0;		/* TCP checksum is valid. */
168469a34e8dSRandall Stewart 
16859ca874cfSHans Petter Selasky 	/* Check if ACK can be compressed */
16869ca874cfSHans Petter Selasky 	can_compress = tcp_lro_ack_valid(m, th, &ts_ptr, &other_opts);
168769a34e8dSRandall Stewart 
16889ca874cfSHans Petter Selasky 	/* Now lets look at the should wake states */
16899ca874cfSHans Petter Selasky 	if ((other_opts == true) &&
16909ca874cfSHans Petter Selasky 	    ((inp->inp_flags2 & INP_DONT_SACK_QUEUE) == 0)) {
169169a34e8dSRandall Stewart 		/*
16929ca874cfSHans Petter Selasky 		 * If there are other options (SACK?) and the
16939ca874cfSHans Petter Selasky 		 * tcp endpoint has not expressly told us it does
16949ca874cfSHans Petter Selasky 		 * not care about SACKS, then we should wake up.
169569a34e8dSRandall Stewart 		 */
16969ca874cfSHans Petter Selasky 		*should_wake = true;
169769a34e8dSRandall Stewart 	}
16989ca874cfSHans Petter Selasky 	/* Is the ack compressable? */
16999ca874cfSHans Petter Selasky 	if (can_compress == false)
17009ca874cfSHans Petter Selasky 		goto done;
17019ca874cfSHans Petter Selasky 	/* Does the TCP endpoint support ACK compression? */
17029ca874cfSHans Petter Selasky 	if ((inp->inp_flags2 & INP_MBUF_ACKCMP) == 0)
17039ca874cfSHans Petter Selasky 		goto done;
17049ca874cfSHans Petter Selasky 
17059ca874cfSHans Petter Selasky 	/* Lets get the TOS/traffic class field */
17069ca874cfSHans Petter Selasky 	l3.ptr = mtod(m, void *);
17079ca874cfSHans Petter Selasky 	switch (lro_type) {
17089ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
17099ca874cfSHans Petter Selasky 		iptos = l3.ip4->ip_tos;
17109ca874cfSHans Petter Selasky 		break;
17119ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
17129ca874cfSHans Petter Selasky 		iptos = IPV6_TRAFFIC_CLASS(l3.ip6);
17139ca874cfSHans Petter Selasky 		break;
17149ca874cfSHans Petter Selasky 	default:
17159ca874cfSHans Petter Selasky 		iptos = 0;	/* Keep compiler happy. */
171669a34e8dSRandall Stewart 		break;
171769a34e8dSRandall Stewart 	}
17189ca874cfSHans Petter Selasky 	/* Now lets get space if we don't have some already */
17199ca874cfSHans Petter Selasky 	if (*cmp == NULL) {
17209ca874cfSHans Petter Selasky new_one:
17219ca874cfSHans Petter Selasky 		nm = tcp_lro_get_last_if_ackcmp(lc, le, inp, &n_mbuf);
17229ca874cfSHans Petter Selasky 		if (__predict_false(nm == NULL))
17239ca874cfSHans Petter Selasky 			goto done;
17249ca874cfSHans Petter Selasky 		*cmp = nm;
17259ca874cfSHans Petter Selasky 		if (n_mbuf) {
172669a34e8dSRandall Stewart 			/*
17279ca874cfSHans Petter Selasky 			 *  Link in the new cmp ack to our in-order place,
17289ca874cfSHans Petter Selasky 			 * first set our cmp ack's next to where we are.
172969a34e8dSRandall Stewart 			 */
17309ca874cfSHans Petter Selasky 			nm->m_nextpkt = m;
17319ca874cfSHans Petter Selasky 			(*pp) = nm;
17329ca874cfSHans Petter Selasky 			/*
17339ca874cfSHans Petter Selasky 			 * Set it up so mv_to is advanced to our
17349ca874cfSHans Petter Selasky 			 * compressed ack. This way the caller can
17359ca874cfSHans Petter Selasky 			 * advance pp to the right place.
17369ca874cfSHans Petter Selasky 			 */
17379ca874cfSHans Petter Selasky 			*mv_to = nm;
17389ca874cfSHans Petter Selasky 			/*
17399ca874cfSHans Petter Selasky 			 * Advance it here locally as well.
17409ca874cfSHans Petter Selasky 			 */
17419ca874cfSHans Petter Selasky 			pp = &nm->m_nextpkt;
174269a34e8dSRandall Stewart 		}
17439ca874cfSHans Petter Selasky 	} else {
17449ca874cfSHans Petter Selasky 		/* We have one already we are working on */
17459ca874cfSHans Petter Selasky 		nm = *cmp;
17469ca874cfSHans Petter Selasky 		if (M_TRAILINGSPACE(nm) < sizeof(struct tcp_ackent)) {
17479ca874cfSHans Petter Selasky 			/* We ran out of space */
17489ca874cfSHans Petter Selasky 			inp->inp_flags2 |= INP_MBUF_L_ACKS;
17499ca874cfSHans Petter Selasky 			goto new_one;
17509ca874cfSHans Petter Selasky 		}
17519ca874cfSHans Petter Selasky 	}
17529ca874cfSHans Petter Selasky 	MPASS(M_TRAILINGSPACE(nm) >= sizeof(struct tcp_ackent));
17539ca874cfSHans Petter Selasky 	counter_u64_add(tcp_inp_lro_compressed, 1);
17549ca874cfSHans Petter Selasky 	le->compressed++;
17559ca874cfSHans Petter Selasky 	/* We can add in to the one on the tail */
17569ca874cfSHans Petter Selasky 	ack_ent = mtod(nm, struct tcp_ackent *);
17579ca874cfSHans Petter Selasky 	idx = (nm->m_len / sizeof(struct tcp_ackent));
17589ca874cfSHans Petter Selasky 	build_ack_entry(&ack_ent[idx], th, m, ts_ptr, iptos);
175969a34e8dSRandall Stewart 
17609ca874cfSHans Petter Selasky 	/* Bump the size of both pkt-hdr and len */
17619ca874cfSHans Petter Selasky 	nm->m_len += sizeof(struct tcp_ackent);
17629ca874cfSHans Petter Selasky 	nm->m_pkthdr.len += sizeof(struct tcp_ackent);
17639ca874cfSHans Petter Selasky compressed:
17649ca874cfSHans Petter Selasky 	/* Advance to next mbuf before freeing. */
17659ca874cfSHans Petter Selasky 	*pp = m->m_nextpkt;
17669ca874cfSHans Petter Selasky 	m->m_nextpkt = NULL;
176769a34e8dSRandall Stewart 	m_freem(m);
17689ca874cfSHans Petter Selasky 	return (true);
17699ca874cfSHans Petter Selasky done:
17709ca874cfSHans Petter Selasky 	counter_u64_add(tcp_uncomp_total, 1);
17719ca874cfSHans Petter Selasky 	le->uncompressed++;
17729ca874cfSHans Petter Selasky 	return (false);
177369a34e8dSRandall Stewart }
177469a34e8dSRandall Stewart #endif
17759ca874cfSHans Petter Selasky 
17769ca874cfSHans Petter Selasky static struct lro_head *
17779ca874cfSHans Petter Selasky tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser)
17789ca874cfSHans Petter Selasky {
17799ca874cfSHans Petter Selasky 	u_long hash;
17809ca874cfSHans Petter Selasky 
17819ca874cfSHans Petter Selasky 	if (M_HASHTYPE_ISHASH(m)) {
17829ca874cfSHans Petter Selasky 		hash = m->m_pkthdr.flowid;
17839ca874cfSHans Petter Selasky 	} else {
17849ca874cfSHans Petter Selasky 		for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++)
17859ca874cfSHans Petter Selasky 			hash += parser->data.raw[i];
178669a34e8dSRandall Stewart 	}
17879ca874cfSHans Petter Selasky 	return (&lc->lro_hash[hash % lc->lro_hashsz]);
17889ca874cfSHans Petter Selasky }
178969a34e8dSRandall Stewart 
179005cde7efSSepherosa Ziehau static int
17919ca874cfSHans Petter Selasky tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash)
179262b5b6ecSBjoern A. Zeeb {
17939ca874cfSHans Petter Selasky 	struct lro_parser pi;	/* inner address data */
17949ca874cfSHans Petter Selasky 	struct lro_parser po;	/* outer address data */
17959ca874cfSHans Petter Selasky 	struct lro_parser *pa;	/* current parser for TCP stream */
179662b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
179705cde7efSSepherosa Ziehau 	struct lro_head *bucket;
17989ca874cfSHans Petter Selasky 	struct tcphdr *th;
17999ca874cfSHans Petter Selasky 	int tcp_data_len;
18009ca874cfSHans Petter Selasky 	int tcp_opt_len;
18019ca874cfSHans Petter Selasky 	int error;
18029ca874cfSHans Petter Selasky 	uint16_t tcp_data_sum;
18036c5087a8SJack F Vogel 
18049ca874cfSHans Petter Selasky #ifdef INET
18059ca874cfSHans Petter Selasky 	/* Quickly decide if packet cannot be LRO'ed */
18069ca874cfSHans Petter Selasky 	if (__predict_false(V_ipforwarding != 0))
18079ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
18089ca874cfSHans Petter Selasky #endif
18099ca874cfSHans Petter Selasky #ifdef INET6
18109ca874cfSHans Petter Selasky 	/* Quickly decide if packet cannot be LRO'ed */
18119ca874cfSHans Petter Selasky 	if (__predict_false(V_ip6_forwarding != 0))
18129ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
18139ca874cfSHans Petter Selasky #endif
1814ca1a7e10SRandall Stewart 	if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
1815ca1a7e10SRandall Stewart 	     ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
1816ca1a7e10SRandall Stewart 	    (m->m_pkthdr.csum_data != 0xffff)) {
1817ca1a7e10SRandall Stewart 		/*
1818ca1a7e10SRandall Stewart 		 * The checksum either did not have hardware offload
1819ca1a7e10SRandall Stewart 		 * or it was a bad checksum. We can't LRO such
1820ca1a7e10SRandall Stewart 		 * a packet.
1821ca1a7e10SRandall Stewart 		 */
1822ca1a7e10SRandall Stewart 		counter_u64_add(tcp_bad_csums, 1);
1823ca1a7e10SRandall Stewart 		return (TCP_LRO_CANNOT);
1824ca1a7e10SRandall Stewart 	}
182562b5b6ecSBjoern A. Zeeb 	/* We expect a contiguous header [eh, ip, tcp]. */
18269ca874cfSHans Petter Selasky 	pa = tcp_lro_parser(m, &po, &pi, true);
18279ca874cfSHans Petter Selasky 	if (__predict_false(pa == NULL))
18289ca874cfSHans Petter Selasky 		return (TCP_LRO_NOT_SUPPORTED);
18299ca874cfSHans Petter Selasky 
18309ca874cfSHans Petter Selasky 	/* We don't expect any padding. */
18319ca874cfSHans Petter Selasky 	error = tcp_lro_trim_mbuf_chain(m, pa);
18329ca874cfSHans Petter Selasky 	if (__predict_false(error != 0))
18339ca874cfSHans Petter Selasky 		return (error);
18349ca874cfSHans Petter Selasky 
18359ca874cfSHans Petter Selasky #ifdef INET
18369ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
18379ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
18389ca874cfSHans Petter Selasky 		error = tcp_lro_rx_ipv4(lc, m, pa->ip4);
18399ca874cfSHans Petter Selasky 		if (__predict_false(error != 0))
18409ca874cfSHans Petter Selasky 			return (error);
18419ca874cfSHans Petter Selasky 		break;
18429ca874cfSHans Petter Selasky 	default:
18439ca874cfSHans Petter Selasky 		break;
18449ca874cfSHans Petter Selasky 	}
18459ca874cfSHans Petter Selasky #endif
18469ca874cfSHans Petter Selasky 	/* If no hardware or arrival stamp on the packet add timestamp */
1847e57b2d0eSRandall Stewart 	if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) {
1848b45daaeaSRandall Stewart 		m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time);
1849e57b2d0eSRandall Stewart 		m->m_flags |= M_TSTMP_LRO;
1850e57b2d0eSRandall Stewart 	}
18516c5087a8SJack F Vogel 
18529ca874cfSHans Petter Selasky 	/* Get pointer to TCP header. */
18539ca874cfSHans Petter Selasky 	th = pa->tcp;
18549ca874cfSHans Petter Selasky 
18559ca874cfSHans Petter Selasky 	/* Don't process SYN packets. */
18561ebf4607SRichard Scheffenegger 	if (__predict_false(tcp_get_flags(th) & TH_SYN))
185762b5b6ecSBjoern A. Zeeb 		return (TCP_LRO_CANNOT);
185862b5b6ecSBjoern A. Zeeb 
18599ca874cfSHans Petter Selasky 	/* Get total TCP header length and compute payload length. */
18609ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
18619ca874cfSHans Petter Selasky 	tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th -
18629ca874cfSHans Petter Selasky 	    (uint8_t *)m->m_data) - tcp_opt_len;
18639ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
18649ca874cfSHans Petter Selasky 
18659ca874cfSHans Petter Selasky 	/* Don't process invalid TCP headers. */
18669ca874cfSHans Petter Selasky 	if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0))
186762b5b6ecSBjoern A. Zeeb 		return (TCP_LRO_CANNOT);
18689ca874cfSHans Petter Selasky 
18699ca874cfSHans Petter Selasky 	/* Compute TCP data only checksum. */
18709ca874cfSHans Petter Selasky 	if (tcp_data_len == 0)
18719ca874cfSHans Petter Selasky 		tcp_data_sum = 0;	/* no data, no checksum */
18729ca874cfSHans Petter Selasky 	else if (__predict_false(csum != 0))
18739ca874cfSHans Petter Selasky 		tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum);
18749ca874cfSHans Petter Selasky 	else
18759ca874cfSHans Petter Selasky 		tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum);
18769ca874cfSHans Petter Selasky 
18779ca874cfSHans Petter Selasky 	/* Save TCP info in mbuf. */
18789ca874cfSHans Petter Selasky 	m->m_nextpkt = NULL;
18799ca874cfSHans Petter Selasky 	m->m_pkthdr.rcvif = lc->ifp;
18809ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum;
18819ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_len = tcp_data_len;
18829ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data);
18839ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_nsegs = 1;
18849ca874cfSHans Petter Selasky 
18859ca874cfSHans Petter Selasky 	/* Get hash bucket. */
188605cde7efSSepherosa Ziehau 	if (!use_hash) {
188705cde7efSSepherosa Ziehau 		bucket = &lc->lro_hash[0];
188805cde7efSSepherosa Ziehau 	} else {
18899ca874cfSHans Petter Selasky 		bucket = tcp_lro_rx_get_bucket(lc, m, pa);
189005cde7efSSepherosa Ziehau 	}
189105cde7efSSepherosa Ziehau 
189262b5b6ecSBjoern A. Zeeb 	/* Try to find a matching previous segment. */
189305cde7efSSepherosa Ziehau 	LIST_FOREACH(le, bucket, hash_next) {
18949ca874cfSHans Petter Selasky 		/* Compare addresses and ports. */
18959ca874cfSHans Petter Selasky 		if (lro_address_compare(&po.data, &le->outer.data) == false ||
18969ca874cfSHans Petter Selasky 		    lro_address_compare(&pi.data, &le->inner.data) == false)
189762b5b6ecSBjoern A. Zeeb 			continue;
18989ca874cfSHans Petter Selasky 
18999ca874cfSHans Petter Selasky 		/* Check if no data and old ACK. */
19009ca874cfSHans Petter Selasky 		if (tcp_data_len == 0 &&
19019ca874cfSHans Petter Selasky 		    SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1902d7fb35d1SSean Bruno 			m_freem(m);
1903d7fb35d1SSean Bruno 			return (0);
1904d7fb35d1SSean Bruno 		}
190569a34e8dSRandall Stewart 
19069ca874cfSHans Petter Selasky 		/* Mark "m" in the last spot. */
1907e57b2d0eSRandall Stewart 		le->m_last_mbuf->m_nextpkt = m;
19089ca874cfSHans Petter Selasky 		/* Now set the tail to "m". */
1909e57b2d0eSRandall Stewart 		le->m_last_mbuf = m;
191062b5b6ecSBjoern A. Zeeb 		return (0);
19116c5087a8SJack F Vogel 	}
19129ca874cfSHans Petter Selasky 
191362b5b6ecSBjoern A. Zeeb 	/* Try to find an empty slot. */
19141ea44822SSepherosa Ziehau 	if (LIST_EMPTY(&lc->lro_free))
1915489f0c3cSSepherosa Ziehau 		return (TCP_LRO_NO_ENTRIES);
191662b5b6ecSBjoern A. Zeeb 
191762b5b6ecSBjoern A. Zeeb 	/* Start a new segment chain. */
19181ea44822SSepherosa Ziehau 	le = LIST_FIRST(&lc->lro_free);
19191ea44822SSepherosa Ziehau 	LIST_REMOVE(le, next);
192005cde7efSSepherosa Ziehau 	tcp_lro_active_insert(lc, bucket, le);
192162b5b6ecSBjoern A. Zeeb 
19229ca874cfSHans Petter Selasky 	/* Make sure the headers are set. */
19239ca874cfSHans Petter Selasky 	le->inner = pi;
19249ca874cfSHans Petter Selasky 	le->outer = po;
192562b5b6ecSBjoern A. Zeeb 
19269ca874cfSHans Petter Selasky 	/* Store time this entry was allocated. */
19279ca874cfSHans Petter Selasky 	le->alloc_time = lc->lro_last_queue_time;
192869a34e8dSRandall Stewart 
19299ca874cfSHans Petter Selasky 	tcp_set_entry_to_mbuf(lc, le, m, th);
193069a34e8dSRandall Stewart 
19319ca874cfSHans Petter Selasky 	/* Now set the tail to "m". */
1932e57b2d0eSRandall Stewart 	le->m_last_mbuf = m;
19339ca874cfSHans Petter Selasky 
193462b5b6ecSBjoern A. Zeeb 	return (0);
193562b5b6ecSBjoern A. Zeeb }
193662b5b6ecSBjoern A. Zeeb 
193705cde7efSSepherosa Ziehau int
193805cde7efSSepherosa Ziehau tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
193905cde7efSSepherosa Ziehau {
19409ca874cfSHans Petter Selasky 	int error;
194105cde7efSSepherosa Ziehau 
1942ca1a7e10SRandall Stewart 	if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
1943ca1a7e10SRandall Stewart 	     ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
1944ca1a7e10SRandall Stewart 	    (m->m_pkthdr.csum_data != 0xffff)) {
1945ca1a7e10SRandall Stewart 		/*
1946ca1a7e10SRandall Stewart 		 * The checksum either did not have hardware offload
1947ca1a7e10SRandall Stewart 		 * or it was a bad checksum. We can't LRO such
1948ca1a7e10SRandall Stewart 		 * a packet.
1949ca1a7e10SRandall Stewart 		 */
1950ca1a7e10SRandall Stewart 		counter_u64_add(tcp_bad_csums, 1);
1951ca1a7e10SRandall Stewart 		return (TCP_LRO_CANNOT);
1952ca1a7e10SRandall Stewart 	}
19539ca874cfSHans Petter Selasky 	/* get current time */
1954b45daaeaSRandall Stewart 	binuptime(&lc->lro_last_queue_time);
19559ca874cfSHans Petter Selasky 	CURVNET_SET(lc->ifp->if_vnet);
19569ca874cfSHans Petter Selasky 	error = tcp_lro_rx_common(lc, m, csum, true);
19579ca874cfSHans Petter Selasky 	CURVNET_RESTORE();
19589ca874cfSHans Petter Selasky 
19599ca874cfSHans Petter Selasky 	return (error);
196005cde7efSSepherosa Ziehau }
196105cde7efSSepherosa Ziehau 
1962e936121dSHans Petter Selasky void
1963e936121dSHans Petter Selasky tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
1964e936121dSHans Petter Selasky {
1965dc6ab77dSMichael Tuexen 	NET_EPOCH_ASSERT();
1966e936121dSHans Petter Selasky 	/* sanity checks */
1967e936121dSHans Petter Selasky 	if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
1968e936121dSHans Petter Selasky 	    lc->lro_mbuf_max == 0)) {
1969e936121dSHans Petter Selasky 		/* packet drop */
1970e936121dSHans Petter Selasky 		m_freem(mb);
1971e936121dSHans Petter Selasky 		return;
1972e936121dSHans Petter Selasky 	}
1973e936121dSHans Petter Selasky 
1974e936121dSHans Petter Selasky 	/* check if packet is not LRO capable */
1975ca1a7e10SRandall Stewart 	if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
1976e936121dSHans Petter Selasky 		/* input packet to network layer */
1977e936121dSHans Petter Selasky 		(*lc->ifp->if_input) (lc->ifp, mb);
1978e936121dSHans Petter Selasky 		return;
1979e936121dSHans Petter Selasky 	}
1980e936121dSHans Petter Selasky 
1981fc271df3SHans Petter Selasky 	/* create sequence number */
1982fc271df3SHans Petter Selasky 	lc->lro_mbuf_data[lc->lro_mbuf_count].seq =
1983fc271df3SHans Petter Selasky 	    (((uint64_t)M_HASHTYPE_GET(mb)) << 56) |
1984fc271df3SHans Petter Selasky 	    (((uint64_t)mb->m_pkthdr.flowid) << 24) |
1985fc271df3SHans Petter Selasky 	    ((uint64_t)lc->lro_mbuf_count);
1986e936121dSHans Petter Selasky 
1987e936121dSHans Petter Selasky 	/* enter mbuf */
1988f8acc03eSNavdeep Parhar 	lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb;
1989f8acc03eSNavdeep Parhar 
1990f8acc03eSNavdeep Parhar 	/* flush if array is full */
1991f8acc03eSNavdeep Parhar 	if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max))
1992f8acc03eSNavdeep Parhar 		tcp_lro_flush_all(lc);
1993e936121dSHans Petter Selasky }
1994e936121dSHans Petter Selasky 
199562b5b6ecSBjoern A. Zeeb /* end */
1996