xref: /freebsd/sys/netinet/tcp_lro.c (revision 9ca874cf740ee68c5742df8b5f9e20910085c011)
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
7*9ca874cfSHans 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>
566c5087a8SJack F Vogel 
576c5087a8SJack F Vogel #include <netinet/in_systm.h>
586c5087a8SJack F Vogel #include <netinet/in.h>
5962b5b6ecSBjoern A. Zeeb #include <netinet/ip6.h>
606c5087a8SJack F Vogel #include <netinet/ip.h>
6131bfc56eSBjoern A. Zeeb #include <netinet/ip_var.h>
62e57b2d0eSRandall Stewart #include <netinet/in_pcb.h>
63e57b2d0eSRandall Stewart #include <netinet6/in6_pcb.h>
646c5087a8SJack F Vogel #include <netinet/tcp.h>
65d7fb35d1SSean Bruno #include <netinet/tcp_seq.h>
666c5087a8SJack F Vogel #include <netinet/tcp_lro.h>
678452c1b3SSepherosa Ziehau #include <netinet/tcp_var.h>
6869a34e8dSRandall Stewart #include <netinet/tcpip.h>
69e57b2d0eSRandall Stewart #include <netinet/tcp_hpts.h>
70e57b2d0eSRandall Stewart #include <netinet/tcp_log_buf.h>
71*9ca874cfSHans Petter Selasky #include <netinet/udp.h>
7231bfc56eSBjoern A. Zeeb #include <netinet6/ip6_var.h>
7331bfc56eSBjoern A. Zeeb 
746c5087a8SJack F Vogel #include <machine/in_cksum.h>
756c5087a8SJack F Vogel 
76e936121dSHans Petter Selasky static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
776c5087a8SJack F Vogel 
78*9ca874cfSHans Petter Selasky #define	TCP_LRO_TS_OPTION \
79*9ca874cfSHans Petter Selasky     ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
80*9ca874cfSHans Petter Selasky 	  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
816c5087a8SJack F Vogel 
826dd38b87SSepherosa Ziehau static void	tcp_lro_rx_done(struct lro_ctrl *lc);
83*9ca874cfSHans Petter Selasky static int	tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m,
84*9ca874cfSHans Petter Selasky 		    uint32_t csum, bool use_hash);
85*9ca874cfSHans Petter Selasky 
86*9ca874cfSHans Petter Selasky #ifdef TCPHPTS
87*9ca874cfSHans Petter Selasky static bool	do_bpf_strip_and_compress(struct inpcb *, struct lro_ctrl *,
88*9ca874cfSHans Petter Selasky 		struct lro_entry *, struct mbuf **, struct mbuf **, struct mbuf **, bool *, bool);
89*9ca874cfSHans Petter Selasky 
90*9ca874cfSHans Petter Selasky #endif
916dd38b87SSepherosa Ziehau 
928452c1b3SSepherosa Ziehau SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro,  CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
938452c1b3SSepherosa Ziehau     "TCP LRO");
948452c1b3SSepherosa Ziehau 
95*9ca874cfSHans Petter Selasky static long tcplro_stacks_wanting_mbufq;
96e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_direct_queue;
97e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_wokeup_queue;
98e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_compressed;
99e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_locks_taken;
10069a34e8dSRandall Stewart counter_u64_t tcp_extra_mbuf;
10169a34e8dSRandall Stewart counter_u64_t tcp_would_have_but;
10269a34e8dSRandall Stewart counter_u64_t tcp_comp_total;
10369a34e8dSRandall Stewart counter_u64_t tcp_uncomp_total;
104e57b2d0eSRandall Stewart 
1058452c1b3SSepherosa Ziehau static unsigned	tcp_lro_entries = TCP_LRO_ENTRIES;
1068452c1b3SSepherosa Ziehau SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
1078452c1b3SSepherosa Ziehau     CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
1088452c1b3SSepherosa Ziehau     "default number of LRO entries");
10969a34e8dSRandall Stewart 
110e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD,
111e57b2d0eSRandall Stewart     &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport");
112e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD,
113e57b2d0eSRandall Stewart     &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts");
114e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD,
115e57b2d0eSRandall Stewart     &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport");
116e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
117e57b2d0eSRandall Stewart     &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
11869a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD,
11969a34e8dSRandall Stewart     &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp");
12069a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD,
121*9ca874cfSHans Petter Selasky     &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed");
12269a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD,
12369a34e8dSRandall Stewart     &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set");
12469a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD,
12569a34e8dSRandall Stewart     &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP");
126e57b2d0eSRandall Stewart 
127e57b2d0eSRandall Stewart void
128e57b2d0eSRandall Stewart tcp_lro_reg_mbufq(void)
129e57b2d0eSRandall Stewart {
130e57b2d0eSRandall Stewart 	atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1);
131e57b2d0eSRandall Stewart }
132e57b2d0eSRandall Stewart 
133e57b2d0eSRandall Stewart void
134e57b2d0eSRandall Stewart tcp_lro_dereg_mbufq(void)
135e57b2d0eSRandall Stewart {
136e57b2d0eSRandall Stewart 	atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1);
137e57b2d0eSRandall Stewart }
1388452c1b3SSepherosa Ziehau 
13951e3c20dSSepherosa Ziehau static __inline void
14005cde7efSSepherosa Ziehau tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket,
14105cde7efSSepherosa Ziehau     struct lro_entry *le)
14251e3c20dSSepherosa Ziehau {
14351e3c20dSSepherosa Ziehau 
14451e3c20dSSepherosa Ziehau 	LIST_INSERT_HEAD(&lc->lro_active, le, next);
14505cde7efSSepherosa Ziehau 	LIST_INSERT_HEAD(bucket, le, hash_next);
14651e3c20dSSepherosa Ziehau }
14751e3c20dSSepherosa Ziehau 
14851e3c20dSSepherosa Ziehau static __inline void
14951e3c20dSSepherosa Ziehau tcp_lro_active_remove(struct lro_entry *le)
15051e3c20dSSepherosa Ziehau {
15151e3c20dSSepherosa Ziehau 
15205cde7efSSepherosa Ziehau 	LIST_REMOVE(le, next);		/* active list */
15305cde7efSSepherosa Ziehau 	LIST_REMOVE(le, hash_next);	/* hash bucket */
15451e3c20dSSepherosa Ziehau }
15551e3c20dSSepherosa Ziehau 
1566c5087a8SJack F Vogel int
15762b5b6ecSBjoern A. Zeeb tcp_lro_init(struct lro_ctrl *lc)
1586c5087a8SJack F Vogel {
1598452c1b3SSepherosa Ziehau 	return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0));
160e936121dSHans Petter Selasky }
161e936121dSHans Petter Selasky 
162e936121dSHans Petter Selasky int
163e936121dSHans Petter Selasky tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
164e936121dSHans Petter Selasky     unsigned lro_entries, unsigned lro_mbufs)
165e936121dSHans Petter Selasky {
16662b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
167e936121dSHans Petter Selasky 	size_t size;
16805cde7efSSepherosa Ziehau 	unsigned i, elements;
1696c5087a8SJack F Vogel 
17062b5b6ecSBjoern A. Zeeb 	lc->lro_bad_csum = 0;
17162b5b6ecSBjoern A. Zeeb 	lc->lro_queued = 0;
17262b5b6ecSBjoern A. Zeeb 	lc->lro_flushed = 0;
173e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
174e936121dSHans Petter Selasky 	lc->lro_mbuf_max = lro_mbufs;
175e936121dSHans Petter Selasky 	lc->lro_cnt = lro_entries;
1767ae3d4bfSSepherosa Ziehau 	lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX;
1777ae3d4bfSSepherosa Ziehau 	lc->lro_length_lim = TCP_LRO_LENGTH_MAX;
178e936121dSHans Petter Selasky 	lc->ifp = ifp;
1791ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_free);
1801ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_active);
1816c5087a8SJack F Vogel 
18205cde7efSSepherosa Ziehau 	/* create hash table to accelerate entry lookup */
18305cde7efSSepherosa Ziehau 	if (lro_entries > lro_mbufs)
18405cde7efSSepherosa Ziehau 		elements = lro_entries;
18505cde7efSSepherosa Ziehau 	else
18605cde7efSSepherosa Ziehau 		elements = lro_mbufs;
18705cde7efSSepherosa Ziehau 	lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz,
18805cde7efSSepherosa Ziehau 	    HASH_NOWAIT);
18905cde7efSSepherosa Ziehau 	if (lc->lro_hash == NULL) {
19005cde7efSSepherosa Ziehau 		memset(lc, 0, sizeof(*lc));
19105cde7efSSepherosa Ziehau 		return (ENOMEM);
19205cde7efSSepherosa Ziehau 	}
19305cde7efSSepherosa Ziehau 
194e936121dSHans Petter Selasky 	/* compute size to allocate */
195fc271df3SHans Petter Selasky 	size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) +
196e936121dSHans Petter Selasky 	    (lro_entries * sizeof(*le));
197fc271df3SHans Petter Selasky 	lc->lro_mbuf_data = (struct lro_mbuf_sort *)
198e936121dSHans Petter Selasky 	    malloc(size, M_LRO, M_NOWAIT | M_ZERO);
1996c5087a8SJack F Vogel 
200e936121dSHans Petter Selasky 	/* check for out of memory */
201e936121dSHans Petter Selasky 	if (lc->lro_mbuf_data == NULL) {
202a3927369SNavdeep Parhar 		free(lc->lro_hash, M_LRO);
203e936121dSHans Petter Selasky 		memset(lc, 0, sizeof(*lc));
204e936121dSHans Petter Selasky 		return (ENOMEM);
205e936121dSHans Petter Selasky 	}
206e936121dSHans Petter Selasky 	/* compute offset for LRO entries */
207e936121dSHans Petter Selasky 	le = (struct lro_entry *)
208e936121dSHans Petter Selasky 	    (lc->lro_mbuf_data + lro_mbufs);
209e936121dSHans Petter Selasky 
210e936121dSHans Petter Selasky 	/* setup linked list */
211e936121dSHans Petter Selasky 	for (i = 0; i != lro_entries; i++)
2121ea44822SSepherosa Ziehau 		LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
213e936121dSHans Petter Selasky 
214e936121dSHans Petter Selasky 	return (0);
2156c5087a8SJack F Vogel }
2166c5087a8SJack F Vogel 
217*9ca874cfSHans Petter Selasky struct vxlan_header {
218*9ca874cfSHans Petter Selasky 	uint32_t	vxlh_flags;
219*9ca874cfSHans Petter Selasky 	uint32_t	vxlh_vni;
220*9ca874cfSHans Petter Selasky };
221e57b2d0eSRandall Stewart 
222*9ca874cfSHans Petter Selasky static inline void *
223*9ca874cfSHans Petter Selasky tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan)
224*9ca874cfSHans Petter Selasky {
225*9ca874cfSHans Petter Selasky 	const struct ether_vlan_header *eh;
226*9ca874cfSHans Petter Selasky 	void *old;
227*9ca874cfSHans Petter Selasky 	uint16_t eth_type;
228*9ca874cfSHans Petter Selasky 
229*9ca874cfSHans Petter Selasky 	if (update_data)
230*9ca874cfSHans Petter Selasky 		memset(parser, 0, sizeof(*parser));
231*9ca874cfSHans Petter Selasky 
232*9ca874cfSHans Petter Selasky 	old = ptr;
233*9ca874cfSHans Petter Selasky 
234*9ca874cfSHans Petter Selasky 	if (is_vxlan) {
235*9ca874cfSHans Petter Selasky 		const struct vxlan_header *vxh;
236*9ca874cfSHans Petter Selasky 		vxh = ptr;
237*9ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + sizeof(*vxh);
238*9ca874cfSHans Petter Selasky 		if (update_data) {
239*9ca874cfSHans Petter Selasky 			parser->data.vxlan_vni =
240*9ca874cfSHans Petter Selasky 			    vxh->vxlh_vni & htonl(0xffffff00);
241e57b2d0eSRandall Stewart 		}
242*9ca874cfSHans Petter Selasky 	}
243*9ca874cfSHans Petter Selasky 
244*9ca874cfSHans Petter Selasky 	eh = ptr;
245*9ca874cfSHans Petter Selasky 	if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) {
246*9ca874cfSHans Petter Selasky 		eth_type = eh->evl_proto;
247*9ca874cfSHans Petter Selasky 		if (update_data) {
248*9ca874cfSHans Petter Selasky 			/* strip priority and keep VLAN ID only */
249*9ca874cfSHans Petter Selasky 			parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK);
250*9ca874cfSHans Petter Selasky 		}
251*9ca874cfSHans Petter Selasky 		/* advance to next header */
252*9ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
253*9ca874cfSHans Petter Selasky 	} else {
254*9ca874cfSHans Petter Selasky 		eth_type = eh->evl_encap_proto;
255*9ca874cfSHans Petter Selasky 		/* advance to next header */
256*9ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + ETHER_HDR_LEN;
257*9ca874cfSHans Petter Selasky 	}
258*9ca874cfSHans Petter Selasky 
259*9ca874cfSHans Petter Selasky 	switch (eth_type) {
260*9ca874cfSHans Petter Selasky #ifdef INET
261*9ca874cfSHans Petter Selasky 	case htons(ETHERTYPE_IP):
262*9ca874cfSHans Petter Selasky 		parser->ip4 = ptr;
263*9ca874cfSHans Petter Selasky 		/* Ensure there are no IPv4 options. */
264*9ca874cfSHans Petter Selasky 		if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4))
265*9ca874cfSHans Petter Selasky 			break;
266*9ca874cfSHans Petter Selasky 		/* .. and the packet is not fragmented. */
267*9ca874cfSHans Petter Selasky 		if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK))
268*9ca874cfSHans Petter Selasky 			break;
269*9ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2);
270*9ca874cfSHans Petter Selasky 		if (update_data) {
271*9ca874cfSHans Petter Selasky 			parser->data.s_addr.v4 = parser->ip4->ip_src;
272*9ca874cfSHans Petter Selasky 			parser->data.d_addr.v4 = parser->ip4->ip_dst;
273*9ca874cfSHans Petter Selasky 		}
274*9ca874cfSHans Petter Selasky 		switch (parser->ip4->ip_p) {
275*9ca874cfSHans Petter Selasky 		case IPPROTO_UDP:
276*9ca874cfSHans Petter Selasky 			parser->udp = ptr;
277*9ca874cfSHans Petter Selasky 			if (update_data) {
278*9ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV4_UDP;
279*9ca874cfSHans Petter Selasky 				parser->data.s_port = parser->udp->uh_sport;
280*9ca874cfSHans Petter Selasky 				parser->data.d_port = parser->udp->uh_dport;
281*9ca874cfSHans Petter Selasky 			} else {
282*9ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP);
283*9ca874cfSHans Petter Selasky 			}
284*9ca874cfSHans Petter Selasky 			ptr = ((uint8_t *)ptr + sizeof(*parser->udp));
285*9ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
286*9ca874cfSHans Petter Selasky 			return (ptr);
287*9ca874cfSHans Petter Selasky 		case IPPROTO_TCP:
288*9ca874cfSHans Petter Selasky 			parser->tcp = ptr;
289*9ca874cfSHans Petter Selasky 			if (update_data) {
290*9ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV4_TCP;
291*9ca874cfSHans Petter Selasky 				parser->data.s_port = parser->tcp->th_sport;
292*9ca874cfSHans Petter Selasky 				parser->data.d_port = parser->tcp->th_dport;
293*9ca874cfSHans Petter Selasky 			} else {
294*9ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP);
295*9ca874cfSHans Petter Selasky 			}
296*9ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
297*9ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
298*9ca874cfSHans Petter Selasky 			return (ptr);
299*9ca874cfSHans Petter Selasky 		default:
300*9ca874cfSHans Petter Selasky 			break;
301*9ca874cfSHans Petter Selasky 		}
302*9ca874cfSHans Petter Selasky 		break;
303*9ca874cfSHans Petter Selasky #endif
304*9ca874cfSHans Petter Selasky #ifdef INET6
305*9ca874cfSHans Petter Selasky 	case htons(ETHERTYPE_IPV6):
306*9ca874cfSHans Petter Selasky 		parser->ip6 = ptr;
307*9ca874cfSHans Petter Selasky 		ptr = (uint8_t *)ptr + sizeof(*parser->ip6);
308*9ca874cfSHans Petter Selasky 		if (update_data) {
309*9ca874cfSHans Petter Selasky 			parser->data.s_addr.v6 = parser->ip6->ip6_src;
310*9ca874cfSHans Petter Selasky 			parser->data.d_addr.v6 = parser->ip6->ip6_dst;
311*9ca874cfSHans Petter Selasky 		}
312*9ca874cfSHans Petter Selasky 		switch (parser->ip6->ip6_nxt) {
313*9ca874cfSHans Petter Selasky 		case IPPROTO_UDP:
314*9ca874cfSHans Petter Selasky 			parser->udp = ptr;
315*9ca874cfSHans Petter Selasky 			if (update_data) {
316*9ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV6_UDP;
317*9ca874cfSHans Petter Selasky 				parser->data.s_port = parser->udp->uh_sport;
318*9ca874cfSHans Petter Selasky 				parser->data.d_port = parser->udp->uh_dport;
319*9ca874cfSHans Petter Selasky 			} else {
320*9ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP);
321*9ca874cfSHans Petter Selasky 			}
322*9ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + sizeof(*parser->udp);
323*9ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
324*9ca874cfSHans Petter Selasky 			return (ptr);
325*9ca874cfSHans Petter Selasky 		case IPPROTO_TCP:
326*9ca874cfSHans Petter Selasky 			parser->tcp = ptr;
327*9ca874cfSHans Petter Selasky 			if (update_data) {
328*9ca874cfSHans Petter Selasky 				parser->data.lro_type = LRO_TYPE_IPV6_TCP;
329*9ca874cfSHans Petter Selasky 				parser->data.s_port = parser->tcp->th_sport;
330*9ca874cfSHans Petter Selasky 				parser->data.d_port = parser->tcp->th_dport;
331*9ca874cfSHans Petter Selasky 			} else {
332*9ca874cfSHans Petter Selasky 				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP);
333*9ca874cfSHans Petter Selasky 			}
334*9ca874cfSHans Petter Selasky 			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
335*9ca874cfSHans Petter Selasky 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
336*9ca874cfSHans Petter Selasky 			return (ptr);
337*9ca874cfSHans Petter Selasky 		default:
338*9ca874cfSHans Petter Selasky 			break;
339*9ca874cfSHans Petter Selasky 		}
340*9ca874cfSHans Petter Selasky 		break;
341*9ca874cfSHans Petter Selasky #endif
342*9ca874cfSHans Petter Selasky 	default:
343*9ca874cfSHans Petter Selasky 		break;
344*9ca874cfSHans Petter Selasky 	}
345*9ca874cfSHans Petter Selasky 	/* Invalid packet - cannot parse */
346*9ca874cfSHans Petter Selasky 	return (NULL);
347*9ca874cfSHans Petter Selasky }
348*9ca874cfSHans Petter Selasky 
349*9ca874cfSHans Petter Selasky static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
350*9ca874cfSHans Petter Selasky     CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID;
351*9ca874cfSHans Petter Selasky 
352*9ca874cfSHans Petter Selasky static inline struct lro_parser *
353*9ca874cfSHans Petter Selasky tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data)
354*9ca874cfSHans Petter Selasky {
355*9ca874cfSHans Petter Selasky 	void *data_ptr;
356*9ca874cfSHans Petter Selasky 
357*9ca874cfSHans Petter Selasky 	/* Try to parse outer headers first. */
358*9ca874cfSHans Petter Selasky 	data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false);
359*9ca874cfSHans Petter Selasky 	if (data_ptr == NULL || po->total_hdr_len > m->m_len)
360*9ca874cfSHans Petter Selasky 		return (NULL);
361*9ca874cfSHans Petter Selasky 
362*9ca874cfSHans Petter Selasky 	if (update_data) {
363*9ca874cfSHans Petter Selasky 		/* Store VLAN ID, if any. */
364*9ca874cfSHans Petter Selasky 		if (__predict_false(m->m_flags & M_VLANTAG)) {
365*9ca874cfSHans Petter Selasky 			po->data.vlan_id =
366*9ca874cfSHans Petter Selasky 			    htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK);
367*9ca874cfSHans Petter Selasky 		}
368*9ca874cfSHans Petter Selasky 	}
369*9ca874cfSHans Petter Selasky 
370*9ca874cfSHans Petter Selasky 	switch (po->data.lro_type) {
371*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_UDP:
372*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_UDP:
373*9ca874cfSHans Petter Selasky 		/* Check for VXLAN headers. */
374*9ca874cfSHans Petter Selasky 		if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum)
375*9ca874cfSHans Petter Selasky 			break;
376*9ca874cfSHans Petter Selasky 
377*9ca874cfSHans Petter Selasky 		/* Try to parse inner headers. */
378*9ca874cfSHans Petter Selasky 		data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true);
379*9ca874cfSHans Petter Selasky 		if (data_ptr == NULL || pi->total_hdr_len > m->m_len)
380*9ca874cfSHans Petter Selasky 			break;
381*9ca874cfSHans Petter Selasky 
382*9ca874cfSHans Petter Selasky 		/* Verify supported header types. */
383*9ca874cfSHans Petter Selasky 		switch (pi->data.lro_type) {
384*9ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV4_TCP:
385*9ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV6_TCP:
386*9ca874cfSHans Petter Selasky 			return (pi);
387*9ca874cfSHans Petter Selasky 		default:
388*9ca874cfSHans Petter Selasky 			break;
389*9ca874cfSHans Petter Selasky 		}
390*9ca874cfSHans Petter Selasky 		break;
391*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
392*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
393*9ca874cfSHans Petter Selasky 		if (update_data)
394*9ca874cfSHans Petter Selasky 			memset(pi, 0, sizeof(*pi));
395*9ca874cfSHans Petter Selasky 		return (po);
396*9ca874cfSHans Petter Selasky 	default:
397*9ca874cfSHans Petter Selasky 		break;
398*9ca874cfSHans Petter Selasky 	}
399*9ca874cfSHans Petter Selasky 	return (NULL);
400*9ca874cfSHans Petter Selasky }
401*9ca874cfSHans Petter Selasky 
402*9ca874cfSHans Petter Selasky static inline int
403*9ca874cfSHans Petter Selasky tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po)
404*9ca874cfSHans Petter Selasky {
405*9ca874cfSHans Petter Selasky 	int len;
406*9ca874cfSHans Petter Selasky 
407*9ca874cfSHans Petter Selasky 	switch (po->data.lro_type) {
408*9ca874cfSHans Petter Selasky #ifdef INET
409*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
410*9ca874cfSHans Petter Selasky 		len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) +
411*9ca874cfSHans Petter Selasky 		    ntohs(po->ip4->ip_len);
412*9ca874cfSHans Petter Selasky 		break;
413*9ca874cfSHans Petter Selasky #endif
414*9ca874cfSHans Petter Selasky #ifdef INET6
415*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
416*9ca874cfSHans Petter Selasky 		len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) +
417*9ca874cfSHans Petter Selasky 		    ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6);
418*9ca874cfSHans Petter Selasky 		break;
419*9ca874cfSHans Petter Selasky #endif
420*9ca874cfSHans Petter Selasky 	default:
421*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
422*9ca874cfSHans Petter Selasky 	}
423*9ca874cfSHans Petter Selasky 
424*9ca874cfSHans Petter Selasky 	/*
425*9ca874cfSHans Petter Selasky 	 * If the frame is padded beyond the end of the IP packet,
426*9ca874cfSHans Petter Selasky 	 * then trim the extra bytes off:
427*9ca874cfSHans Petter Selasky 	 */
428*9ca874cfSHans Petter Selasky 	if (__predict_true(m->m_pkthdr.len == len)) {
429*9ca874cfSHans Petter Selasky 		return (0);
430*9ca874cfSHans Petter Selasky 	} else if (m->m_pkthdr.len > len) {
431*9ca874cfSHans Petter Selasky 		m_adj(m, len - m->m_pkthdr.len);
432*9ca874cfSHans Petter Selasky 		return (0);
433*9ca874cfSHans Petter Selasky 	}
434*9ca874cfSHans Petter Selasky 	return (TCP_LRO_CANNOT);
435*9ca874cfSHans Petter Selasky }
436*9ca874cfSHans Petter Selasky 
437*9ca874cfSHans Petter Selasky static struct tcphdr *
438*9ca874cfSHans Petter Selasky tcp_lro_get_th(struct mbuf *m)
439*9ca874cfSHans Petter Selasky {
440*9ca874cfSHans Petter Selasky 	return ((struct tcphdr *)((uint8_t *)m->m_data + m->m_pkthdr.lro_tcp_h_off));
441e57b2d0eSRandall Stewart }
442e57b2d0eSRandall Stewart 
44369a34e8dSRandall Stewart static void
44469a34e8dSRandall Stewart lro_free_mbuf_chain(struct mbuf *m)
44569a34e8dSRandall Stewart {
44669a34e8dSRandall Stewart 	struct mbuf *save;
44769a34e8dSRandall Stewart 
44869a34e8dSRandall Stewart 	while (m) {
44969a34e8dSRandall Stewart 		save = m->m_nextpkt;
45069a34e8dSRandall Stewart 		m->m_nextpkt = NULL;
45169a34e8dSRandall Stewart 		m_freem(m);
45269a34e8dSRandall Stewart 		m = save;
45369a34e8dSRandall Stewart 	}
45469a34e8dSRandall Stewart }
45569a34e8dSRandall Stewart 
4566c5087a8SJack F Vogel void
45762b5b6ecSBjoern A. Zeeb tcp_lro_free(struct lro_ctrl *lc)
4586c5087a8SJack F Vogel {
45962b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
460e936121dSHans Petter Selasky 	unsigned x;
4616c5087a8SJack F Vogel 
462e936121dSHans Petter Selasky 	/* reset LRO free list */
4631ea44822SSepherosa Ziehau 	LIST_INIT(&lc->lro_free);
464e936121dSHans Petter Selasky 
465e936121dSHans Petter Selasky 	/* free active mbufs, if any */
4661ea44822SSepherosa Ziehau 	while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
46751e3c20dSSepherosa Ziehau 		tcp_lro_active_remove(le);
46869a34e8dSRandall Stewart 		lro_free_mbuf_chain(le->m_head);
4696c5087a8SJack F Vogel 	}
470e936121dSHans Petter Selasky 
47105cde7efSSepherosa Ziehau 	/* free hash table */
47205cde7efSSepherosa Ziehau 	free(lc->lro_hash, M_LRO);
47305cde7efSSepherosa Ziehau 	lc->lro_hash = NULL;
47405cde7efSSepherosa Ziehau 	lc->lro_hashsz = 0;
47505cde7efSSepherosa Ziehau 
476e936121dSHans Petter Selasky 	/* free mbuf array, if any */
477e936121dSHans Petter Selasky 	for (x = 0; x != lc->lro_mbuf_count; x++)
478fc271df3SHans Petter Selasky 		m_freem(lc->lro_mbuf_data[x].mb);
479e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
480e936121dSHans Petter Selasky 
481e936121dSHans Petter Selasky 	/* free allocated memory, if any */
482e936121dSHans Petter Selasky 	free(lc->lro_mbuf_data, M_LRO);
483e936121dSHans Petter Selasky 	lc->lro_mbuf_data = NULL;
4846c5087a8SJack F Vogel }
4856c5087a8SJack F Vogel 
48662b5b6ecSBjoern A. Zeeb static uint16_t
487*9ca874cfSHans Petter Selasky tcp_lro_rx_csum_tcphdr(const struct tcphdr *th)
48862b5b6ecSBjoern A. Zeeb {
489*9ca874cfSHans Petter Selasky 	const uint16_t *ptr;
490*9ca874cfSHans Petter Selasky 	uint32_t csum;
491*9ca874cfSHans Petter Selasky 	uint16_t len;
49262b5b6ecSBjoern A. Zeeb 
493*9ca874cfSHans Petter Selasky 	csum = -th->th_sum;	/* exclude checksum field */
494*9ca874cfSHans Petter Selasky 	len = th->th_off;
495*9ca874cfSHans Petter Selasky 	ptr = (const uint16_t *)th;
496*9ca874cfSHans Petter Selasky 	while (len--) {
497*9ca874cfSHans Petter Selasky 		csum += *ptr;
498*9ca874cfSHans Petter Selasky 		ptr++;
499*9ca874cfSHans Petter Selasky 		csum += *ptr;
500*9ca874cfSHans Petter Selasky 		ptr++;
50162b5b6ecSBjoern A. Zeeb 	}
502*9ca874cfSHans Petter Selasky 	while (csum > 0xffff)
503*9ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
50462b5b6ecSBjoern A. Zeeb 
505*9ca874cfSHans Petter Selasky 	return (csum);
50662b5b6ecSBjoern A. Zeeb }
50762b5b6ecSBjoern A. Zeeb 
50862b5b6ecSBjoern A. Zeeb static uint16_t
509*9ca874cfSHans Petter Selasky tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum)
51062b5b6ecSBjoern A. Zeeb {
51162b5b6ecSBjoern A. Zeeb 	uint32_t c;
51262b5b6ecSBjoern A. Zeeb 	uint16_t cs;
51362b5b6ecSBjoern A. Zeeb 
514*9ca874cfSHans Petter Selasky 	c = tcp_csum;
51562b5b6ecSBjoern A. Zeeb 
516*9ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
51762b5b6ecSBjoern A. Zeeb #ifdef INET6
518*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
519*9ca874cfSHans Petter Selasky 		/* Compute full pseudo IPv6 header checksum. */
520*9ca874cfSHans Petter Selasky 		cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0);
52162b5b6ecSBjoern A. Zeeb 		break;
52262b5b6ecSBjoern A. Zeeb #endif
52362b5b6ecSBjoern A. Zeeb #ifdef INET
524*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
525*9ca874cfSHans Petter Selasky 		/* Compute full pseudo IPv4 header checsum. */
526*9ca874cfSHans Petter Selasky 		cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP);
527*9ca874cfSHans Petter Selasky 		cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs));
52862b5b6ecSBjoern A. Zeeb 		break;
52962b5b6ecSBjoern A. Zeeb #endif
53062b5b6ecSBjoern A. Zeeb 	default:
53162b5b6ecSBjoern A. Zeeb 		cs = 0;		/* Keep compiler happy. */
532*9ca874cfSHans Petter Selasky 		break;
53362b5b6ecSBjoern A. Zeeb 	}
53462b5b6ecSBjoern A. Zeeb 
535*9ca874cfSHans Petter Selasky 	/* Complement checksum. */
53662b5b6ecSBjoern A. Zeeb 	cs = ~cs;
53762b5b6ecSBjoern A. Zeeb 	c += cs;
53862b5b6ecSBjoern A. Zeeb 
539*9ca874cfSHans Petter Selasky 	/* Remove TCP header checksum. */
540*9ca874cfSHans Petter Selasky 	cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp);
54162b5b6ecSBjoern A. Zeeb 	c += cs;
542*9ca874cfSHans Petter Selasky 
543*9ca874cfSHans Petter Selasky 	/* Compute checksum remainder. */
54462b5b6ecSBjoern A. Zeeb 	while (c > 0xffff)
54562b5b6ecSBjoern A. Zeeb 		c = (c >> 16) + (c & 0xffff);
54662b5b6ecSBjoern A. Zeeb 
547*9ca874cfSHans Petter Selasky 	return (c);
54862b5b6ecSBjoern A. Zeeb }
54962b5b6ecSBjoern A. Zeeb 
5506dd38b87SSepherosa Ziehau static void
5516dd38b87SSepherosa Ziehau tcp_lro_rx_done(struct lro_ctrl *lc)
5526dd38b87SSepherosa Ziehau {
5536dd38b87SSepherosa Ziehau 	struct lro_entry *le;
5546dd38b87SSepherosa Ziehau 
5551ea44822SSepherosa Ziehau 	while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
55651e3c20dSSepherosa Ziehau 		tcp_lro_active_remove(le);
5576dd38b87SSepherosa Ziehau 		tcp_lro_flush(lc, le);
5586dd38b87SSepherosa Ziehau 	}
5596dd38b87SSepherosa Ziehau }
5606dd38b87SSepherosa Ziehau 
5616c5087a8SJack F Vogel void
5627127e6acSNavdeep Parhar tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
5637127e6acSNavdeep Parhar {
5647127e6acSNavdeep Parhar 	struct lro_entry *le, *le_tmp;
565*9ca874cfSHans Petter Selasky 	sbintime_t sbt;
5667127e6acSNavdeep Parhar 
5671ea44822SSepherosa Ziehau 	if (LIST_EMPTY(&lc->lro_active))
5687127e6acSNavdeep Parhar 		return;
5697127e6acSNavdeep Parhar 
570*9ca874cfSHans Petter Selasky 	/* get timeout time */
571*9ca874cfSHans Petter Selasky 	sbt = getsbinuptime() - tvtosbt(*timeout);
572*9ca874cfSHans Petter Selasky 
5731ea44822SSepherosa Ziehau 	LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
574*9ca874cfSHans Petter Selasky 		if (sbt >= le->alloc_time) {
57551e3c20dSSepherosa Ziehau 			tcp_lro_active_remove(le);
5767127e6acSNavdeep Parhar 			tcp_lro_flush(lc, le);
5777127e6acSNavdeep Parhar 		}
5787127e6acSNavdeep Parhar 	}
5797127e6acSNavdeep Parhar }
5807127e6acSNavdeep Parhar 
581e57b2d0eSRandall Stewart #ifdef INET
582e57b2d0eSRandall Stewart static int
583*9ca874cfSHans Petter Selasky tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4)
584e57b2d0eSRandall Stewart {
585e57b2d0eSRandall Stewart 	uint16_t csum;
586e57b2d0eSRandall Stewart 
587e57b2d0eSRandall Stewart 	/* Legacy IP has a header checksum that needs to be correct. */
588*9ca874cfSHans Petter Selasky 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
589*9ca874cfSHans Petter Selasky 		if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) {
590e57b2d0eSRandall Stewart 			lc->lro_bad_csum++;
591e57b2d0eSRandall Stewart 			return (TCP_LRO_CANNOT);
592e57b2d0eSRandall Stewart 		}
593e57b2d0eSRandall Stewart 	} else {
594e57b2d0eSRandall Stewart 		csum = in_cksum_hdr(ip4);
595*9ca874cfSHans Petter Selasky 		if (__predict_false(csum != 0)) {
596e57b2d0eSRandall Stewart 			lc->lro_bad_csum++;
597e57b2d0eSRandall Stewart 			return (TCP_LRO_CANNOT);
598e57b2d0eSRandall Stewart 		}
599e57b2d0eSRandall Stewart 	}
600e57b2d0eSRandall Stewart 	return (0);
601e57b2d0eSRandall Stewart }
602e57b2d0eSRandall Stewart #endif
603e57b2d0eSRandall Stewart 
6040a4f8510SRandall Stewart #ifdef TCPHPTS
605e57b2d0eSRandall Stewart static void
606*9ca874cfSHans Petter Selasky tcp_lro_log(struct tcpcb *tp, const struct lro_ctrl *lc,
607*9ca874cfSHans Petter Selasky     const struct lro_entry *le, const struct mbuf *m,
608*9ca874cfSHans Petter Selasky     int frm, int32_t tcp_data_len, uint32_t th_seq,
609*9ca874cfSHans Petter Selasky     uint32_t th_ack, uint16_t th_win)
610e57b2d0eSRandall Stewart {
611e57b2d0eSRandall Stewart 	if (tp->t_logstate != TCP_LOG_STATE_OFF) {
612e57b2d0eSRandall Stewart 		union tcp_log_stackspecific log;
613e57b2d0eSRandall Stewart 		struct timeval tv;
614e57b2d0eSRandall Stewart 		uint32_t cts;
615e57b2d0eSRandall Stewart 
616e57b2d0eSRandall Stewart 		cts = tcp_get_usecs(&tv);
617e57b2d0eSRandall Stewart 		memset(&log, 0, sizeof(union tcp_log_stackspecific));
618e57b2d0eSRandall Stewart 		log.u_bbr.flex8 = frm;
619e57b2d0eSRandall Stewart 		log.u_bbr.flex1 = tcp_data_len;
620e57b2d0eSRandall Stewart 		if (m)
621e57b2d0eSRandall Stewart 			log.u_bbr.flex2 = m->m_pkthdr.len;
622e57b2d0eSRandall Stewart 		else
623e57b2d0eSRandall Stewart 			log.u_bbr.flex2 = 0;
624*9ca874cfSHans Petter Selasky 		log.u_bbr.flex3 = le->m_head->m_pkthdr.lro_nsegs;
625*9ca874cfSHans Petter Selasky 		log.u_bbr.flex4 = le->m_head->m_pkthdr.lro_tcp_d_len;
62669a34e8dSRandall Stewart 		if (le->m_head) {
627e57b2d0eSRandall Stewart 			log.u_bbr.flex5 = le->m_head->m_pkthdr.len;
628e57b2d0eSRandall Stewart 			log.u_bbr.delRate = le->m_head->m_flags;
629e57b2d0eSRandall Stewart 			log.u_bbr.rttProp = le->m_head->m_pkthdr.rcv_tstmp;
63069a34e8dSRandall Stewart 		}
631e57b2d0eSRandall Stewart 		log.u_bbr.inflight = th_seq;
632e57b2d0eSRandall Stewart 		log.u_bbr.timeStamp = cts;
633e57b2d0eSRandall Stewart 		log.u_bbr.epoch = le->next_seq;
634e57b2d0eSRandall Stewart 		log.u_bbr.delivered = th_ack;
635e57b2d0eSRandall Stewart 		log.u_bbr.lt_epoch = le->ack_seq;
636e57b2d0eSRandall Stewart 		log.u_bbr.pacing_gain = th_win;
637e57b2d0eSRandall Stewart 		log.u_bbr.cwnd_gain = le->window;
638b23b156eSWarner Losh 		log.u_bbr.cur_del_rate = (uintptr_t)m;
639b23b156eSWarner Losh 		log.u_bbr.bw_inuse = (uintptr_t)le->m_head;
640*9ca874cfSHans Petter Selasky 		log.u_bbr.flex6 = sbttous(lc->lro_last_queue_time);
641*9ca874cfSHans Petter Selasky 		log.u_bbr.flex7 = le->compressed;
642*9ca874cfSHans Petter Selasky 		log.u_bbr.pacing_gain = le->uncompressed;
64369a34e8dSRandall Stewart 		if (in_epoch(net_epoch_preempt))
64469a34e8dSRandall Stewart 			log.u_bbr.inhpts = 1;
64569a34e8dSRandall Stewart 		else
64669a34e8dSRandall Stewart 			log.u_bbr.inhpts = 0;
647e57b2d0eSRandall Stewart 		TCP_LOG_EVENTP(tp, NULL,
648e57b2d0eSRandall Stewart 			       &tp->t_inpcb->inp_socket->so_rcv,
649e57b2d0eSRandall Stewart 			       &tp->t_inpcb->inp_socket->so_snd,
650e57b2d0eSRandall Stewart 			       TCP_LOG_LRO, 0,
651e57b2d0eSRandall Stewart 			       0, &log, false, &tv);
652e57b2d0eSRandall Stewart 	}
653e57b2d0eSRandall Stewart }
6540a4f8510SRandall Stewart #endif
655e57b2d0eSRandall Stewart 
656*9ca874cfSHans Petter Selasky static inline void
657*9ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum)
658e57b2d0eSRandall Stewart {
659*9ca874cfSHans Petter Selasky 	uint32_t csum;
6606c5087a8SJack F Vogel 
661*9ca874cfSHans Petter Selasky 	csum = 0xffff - *ptr + value;
662*9ca874cfSHans Petter Selasky 	while (csum > 0xffff)
663*9ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
664*9ca874cfSHans Petter Selasky 	*ptr = value;
665*9ca874cfSHans Petter Selasky 	*psum = csum;
66662b5b6ecSBjoern A. Zeeb }
66762b5b6ecSBjoern A. Zeeb 
668*9ca874cfSHans Petter Selasky static uint16_t
669*9ca874cfSHans Petter Selasky tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le,
670*9ca874cfSHans Petter Selasky     uint16_t payload_len, uint16_t delta_sum)
671*9ca874cfSHans Petter Selasky {
672*9ca874cfSHans Petter Selasky 	uint32_t csum;
673*9ca874cfSHans Petter Selasky 	uint16_t tlen;
674*9ca874cfSHans Petter Selasky 	uint16_t temp[5] = {};
675*9ca874cfSHans Petter Selasky 
676*9ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
677*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
678*9ca874cfSHans Petter Selasky 		/* Compute new IPv4 length. */
679*9ca874cfSHans Petter Selasky 		tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len;
680*9ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
681*9ca874cfSHans Petter Selasky 
682*9ca874cfSHans Petter Selasky 		/* Subtract delta from current IPv4 checksum. */
683*9ca874cfSHans Petter Selasky 		csum = pa->ip4->ip_sum + 0xffff - temp[0];
684*9ca874cfSHans Petter Selasky 		while (csum > 0xffff)
685*9ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
686*9ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
687*9ca874cfSHans Petter Selasky 		goto update_tcp_header;
688*9ca874cfSHans Petter Selasky 
689*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
690*9ca874cfSHans Petter Selasky 		/* Compute new IPv6 length. */
691*9ca874cfSHans Petter Selasky 		tlen = (pa->tcp->th_off << 2) + payload_len;
692*9ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
693*9ca874cfSHans Petter Selasky 		goto update_tcp_header;
694*9ca874cfSHans Petter Selasky 
695*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_UDP:
696*9ca874cfSHans Petter Selasky 		/* Compute new IPv4 length. */
697*9ca874cfSHans Petter Selasky 		tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len;
698*9ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
699*9ca874cfSHans Petter Selasky 
700*9ca874cfSHans Petter Selasky 		/* Subtract delta from current IPv4 checksum. */
701*9ca874cfSHans Petter Selasky 		csum = pa->ip4->ip_sum + 0xffff - temp[0];
702*9ca874cfSHans Petter Selasky 		while (csum > 0xffff)
703*9ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
704*9ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
705*9ca874cfSHans Petter Selasky 		goto update_udp_header;
706*9ca874cfSHans Petter Selasky 
707*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_UDP:
708*9ca874cfSHans Petter Selasky 		/* Compute new IPv6 length. */
709*9ca874cfSHans Petter Selasky 		tlen = sizeof(*pa->udp) + payload_len;
710*9ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
711*9ca874cfSHans Petter Selasky 		goto update_udp_header;
712*9ca874cfSHans Petter Selasky 
71362b5b6ecSBjoern A. Zeeb 	default:
714*9ca874cfSHans Petter Selasky 		return (0);
71562b5b6ecSBjoern A. Zeeb 	}
716*9ca874cfSHans Petter Selasky 
717*9ca874cfSHans Petter Selasky update_tcp_header:
718*9ca874cfSHans Petter Selasky 	/* Compute current TCP header checksum. */
719*9ca874cfSHans Petter Selasky 	temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp);
72062b5b6ecSBjoern A. Zeeb 
72162b5b6ecSBjoern A. Zeeb 	/* Incorporate the latest ACK into the TCP header. */
722*9ca874cfSHans Petter Selasky 	pa->tcp->th_ack = le->ack_seq;
723*9ca874cfSHans Petter Selasky 	pa->tcp->th_win = le->window;
724*9ca874cfSHans Petter Selasky 
72562b5b6ecSBjoern A. Zeeb 	/* Incorporate latest timestamp into the TCP header. */
72662b5b6ecSBjoern A. Zeeb 	if (le->timestamp != 0) {
7276c5087a8SJack F Vogel 		uint32_t *ts_ptr;
7286c5087a8SJack F Vogel 
729*9ca874cfSHans Petter Selasky 		ts_ptr = (uint32_t *)(pa->tcp + 1);
73062b5b6ecSBjoern A. Zeeb 		ts_ptr[1] = htonl(le->tsval);
73162b5b6ecSBjoern A. Zeeb 		ts_ptr[2] = le->tsecr;
73262b5b6ecSBjoern A. Zeeb 	}
733*9ca874cfSHans Petter Selasky 
734*9ca874cfSHans Petter Selasky 	/* Compute new TCP header checksum. */
735*9ca874cfSHans Petter Selasky 	temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp);
736*9ca874cfSHans Petter Selasky 
737*9ca874cfSHans Petter Selasky 	/* Compute new TCP checksum. */
738*9ca874cfSHans Petter Selasky 	csum = pa->tcp->th_sum + 0xffff - delta_sum +
739*9ca874cfSHans Petter Selasky 	    0xffff - temp[0] + 0xffff - temp[3] + temp[2];
740*9ca874cfSHans Petter Selasky 	while (csum > 0xffff)
741*9ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
742*9ca874cfSHans Petter Selasky 
743*9ca874cfSHans Petter Selasky 	/* Assign new TCP checksum. */
744*9ca874cfSHans Petter Selasky 	tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]);
745*9ca874cfSHans Petter Selasky 
746*9ca874cfSHans Petter Selasky 	/* Compute all modififications affecting next checksum. */
747*9ca874cfSHans Petter Selasky 	csum = temp[0] + temp[1] + 0xffff - temp[2] +
748*9ca874cfSHans Petter Selasky 	    temp[3] + temp[4] + delta_sum;
749*9ca874cfSHans Petter Selasky 	while (csum > 0xffff)
750*9ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
751*9ca874cfSHans Petter Selasky 
752*9ca874cfSHans Petter Selasky 	/* Return delta checksum to next stage, if any. */
753*9ca874cfSHans Petter Selasky 	return (csum);
754*9ca874cfSHans Petter Selasky 
755*9ca874cfSHans Petter Selasky update_udp_header:
756*9ca874cfSHans Petter Selasky 	tlen = sizeof(*pa->udp) + payload_len;
757*9ca874cfSHans Petter Selasky 	/* Assign new UDP length and compute checksum delta. */
758*9ca874cfSHans Petter Selasky 	tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]);
759*9ca874cfSHans Petter Selasky 
760*9ca874cfSHans Petter Selasky 	/* Check if there is a UDP checksum. */
761*9ca874cfSHans Petter Selasky 	if (__predict_false(pa->udp->uh_sum != 0)) {
762*9ca874cfSHans Petter Selasky 		/* Compute new UDP checksum. */
763*9ca874cfSHans Petter Selasky 		csum = pa->udp->uh_sum + 0xffff - delta_sum +
764*9ca874cfSHans Petter Selasky 		    0xffff - temp[0] + 0xffff - temp[2];
765*9ca874cfSHans Petter Selasky 		while (csum > 0xffff)
766*9ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
767*9ca874cfSHans Petter Selasky 		/* Assign new UDP checksum. */
768*9ca874cfSHans Petter Selasky 		tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]);
769e57b2d0eSRandall Stewart 	}
770*9ca874cfSHans Petter Selasky 
771*9ca874cfSHans Petter Selasky 	/* Compute all modififications affecting next checksum. */
772*9ca874cfSHans Petter Selasky 	csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum;
773*9ca874cfSHans Petter Selasky 	while (csum > 0xffff)
774*9ca874cfSHans Petter Selasky 		csum = (csum >> 16) + (csum & 0xffff);
775*9ca874cfSHans Petter Selasky 
776*9ca874cfSHans Petter Selasky 	/* Return delta checksum to next stage, if any. */
777*9ca874cfSHans Petter Selasky 	return (csum);
778*9ca874cfSHans Petter Selasky }
779*9ca874cfSHans Petter Selasky 
780*9ca874cfSHans Petter Selasky static void
781*9ca874cfSHans Petter Selasky tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le)
782*9ca874cfSHans Petter Selasky {
783*9ca874cfSHans Petter Selasky 	/* Check if we need to recompute any checksums. */
784*9ca874cfSHans Petter Selasky 	if (le->m_head->m_pkthdr.lro_nsegs > 1) {
785*9ca874cfSHans Petter Selasky 		uint16_t csum;
786*9ca874cfSHans Petter Selasky 
787*9ca874cfSHans Petter Selasky 		switch (le->inner.data.lro_type) {
788*9ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV4_TCP:
789*9ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->inner, le,
790*9ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len,
791*9ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_csum);
792*9ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->outer, NULL,
793*9ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len +
794*9ca874cfSHans Petter Selasky 			    le->inner.total_hdr_len, csum);
795*9ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
796*9ca874cfSHans Petter Selasky 			    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
797*9ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_data = 0xffff;
798*9ca874cfSHans Petter Selasky 			break;
799*9ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV6_TCP:
800*9ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->inner, le,
801*9ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len,
802*9ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_csum);
803*9ca874cfSHans Petter Selasky 			csum = tcp_lro_update_checksum(&le->outer, NULL,
804*9ca874cfSHans Petter Selasky 			    le->m_head->m_pkthdr.lro_tcp_d_len +
805*9ca874cfSHans Petter Selasky 			    le->inner.total_hdr_len, csum);
806*9ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
807*9ca874cfSHans Petter Selasky 			    CSUM_PSEUDO_HDR;
808*9ca874cfSHans Petter Selasky 			le->m_head->m_pkthdr.csum_data = 0xffff;
809*9ca874cfSHans Petter Selasky 			break;
810*9ca874cfSHans Petter Selasky 		case LRO_TYPE_NONE:
811*9ca874cfSHans Petter Selasky 			switch (le->outer.data.lro_type) {
812*9ca874cfSHans Petter Selasky 			case LRO_TYPE_IPV4_TCP:
813*9ca874cfSHans Petter Selasky 				csum = tcp_lro_update_checksum(&le->outer, le,
814*9ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_len,
815*9ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_csum);
816*9ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
817*9ca874cfSHans Petter Selasky 				    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
818*9ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_data = 0xffff;
819*9ca874cfSHans Petter Selasky 				break;
820*9ca874cfSHans Petter Selasky 			case LRO_TYPE_IPV6_TCP:
821*9ca874cfSHans Petter Selasky 				csum = tcp_lro_update_checksum(&le->outer, le,
822*9ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_len,
823*9ca874cfSHans Petter Selasky 				    le->m_head->m_pkthdr.lro_tcp_d_csum);
824*9ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
825*9ca874cfSHans Petter Selasky 				    CSUM_PSEUDO_HDR;
826*9ca874cfSHans Petter Selasky 				le->m_head->m_pkthdr.csum_data = 0xffff;
827*9ca874cfSHans Petter Selasky 				break;
828*9ca874cfSHans Petter Selasky 			default:
829*9ca874cfSHans Petter Selasky 				break;
830*9ca874cfSHans Petter Selasky 			}
831*9ca874cfSHans Petter Selasky 			break;
832*9ca874cfSHans Petter Selasky 		default:
833*9ca874cfSHans Petter Selasky 			break;
834*9ca874cfSHans Petter Selasky 		}
835*9ca874cfSHans Petter Selasky 	}
836*9ca874cfSHans Petter Selasky 
837e57b2d0eSRandall Stewart 	/*
838e57b2d0eSRandall Stewart 	 * Break any chain, this is not set to NULL on the singleton
839e57b2d0eSRandall Stewart 	 * case m_nextpkt points to m_head. Other case set them
840e57b2d0eSRandall Stewart 	 * m_nextpkt to NULL in push_and_replace.
841e57b2d0eSRandall Stewart 	 */
842e57b2d0eSRandall Stewart 	le->m_head->m_nextpkt = NULL;
843*9ca874cfSHans Petter Selasky 	lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs;
844e57b2d0eSRandall Stewart 	(*lc->ifp->if_input)(lc->ifp, le->m_head);
84562b5b6ecSBjoern A. Zeeb }
8466c5087a8SJack F Vogel 
847e57b2d0eSRandall Stewart static void
848*9ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le,
849*9ca874cfSHans Petter Selasky     struct mbuf *m, struct tcphdr *th)
850e57b2d0eSRandall Stewart {
851e57b2d0eSRandall Stewart 	uint32_t *ts_ptr;
852e57b2d0eSRandall Stewart 	uint16_t tcp_data_len;
853*9ca874cfSHans Petter Selasky 	uint16_t tcp_opt_len;
854e57b2d0eSRandall Stewart 
855e57b2d0eSRandall Stewart 	ts_ptr = (uint32_t *)(th + 1);
856*9ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
857*9ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
858*9ca874cfSHans Petter Selasky 
859*9ca874cfSHans Petter Selasky 	/* Check if there is a timestamp option. */
860*9ca874cfSHans Petter Selasky 	if (tcp_opt_len == 0 ||
861*9ca874cfSHans Petter Selasky 	    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
862*9ca874cfSHans Petter Selasky 	    *ts_ptr != TCP_LRO_TS_OPTION)) {
863*9ca874cfSHans Petter Selasky 		/* We failed to find the timestamp option. */
864*9ca874cfSHans Petter Selasky 		le->timestamp = 0;
865*9ca874cfSHans Petter Selasky 	} else {
866e57b2d0eSRandall Stewart 		le->timestamp = 1;
867e57b2d0eSRandall Stewart 		le->tsval = ntohl(*(ts_ptr + 1));
868e57b2d0eSRandall Stewart 		le->tsecr = *(ts_ptr + 2);
869*9ca874cfSHans Petter Selasky 	}
870*9ca874cfSHans Petter Selasky 
871*9ca874cfSHans Petter Selasky 	tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
872*9ca874cfSHans Petter Selasky 
873*9ca874cfSHans Petter Selasky 	/* Pull out TCP sequence numbers and window size. */
874e57b2d0eSRandall Stewart 	le->next_seq = ntohl(th->th_seq) + tcp_data_len;
875e57b2d0eSRandall Stewart 	le->ack_seq = th->th_ack;
876e57b2d0eSRandall Stewart 	le->window = th->th_win;
877*9ca874cfSHans Petter Selasky 
878*9ca874cfSHans Petter Selasky 	/* Setup new data pointers. */
879e57b2d0eSRandall Stewart 	le->m_head = m;
880e57b2d0eSRandall Stewart 	le->m_tail = m_last(m);
881e57b2d0eSRandall Stewart }
882e57b2d0eSRandall Stewart 
883e57b2d0eSRandall Stewart static void
884*9ca874cfSHans Petter Selasky tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
885e57b2d0eSRandall Stewart {
886*9ca874cfSHans Petter Selasky 	struct lro_parser *pa;
887*9ca874cfSHans Petter Selasky 
888e57b2d0eSRandall Stewart 	/*
889*9ca874cfSHans Petter Selasky 	 * Push up the stack of the current entry
890*9ca874cfSHans Petter Selasky 	 * and replace it with "m".
891e57b2d0eSRandall Stewart 	 */
892e57b2d0eSRandall Stewart 	struct mbuf *msave;
893e57b2d0eSRandall Stewart 
894e57b2d0eSRandall Stewart 	/* Grab off the next and save it */
895e57b2d0eSRandall Stewart 	msave = le->m_head->m_nextpkt;
896e57b2d0eSRandall Stewart 	le->m_head->m_nextpkt = NULL;
897*9ca874cfSHans Petter Selasky 
898*9ca874cfSHans Petter Selasky 	/* Now push out the old entry */
899*9ca874cfSHans Petter Selasky 	tcp_flush_out_entry(lc, le);
900*9ca874cfSHans Petter Selasky 
901*9ca874cfSHans Petter Selasky 	/* Re-parse new header, should not fail. */
902*9ca874cfSHans Petter Selasky 	pa = tcp_lro_parser(m, &le->outer, &le->inner, false);
903*9ca874cfSHans Petter Selasky 	KASSERT(pa != NULL,
904*9ca874cfSHans Petter Selasky 	    ("tcp_push_and_replace: LRO parser failed on m=%p\n", m));
905*9ca874cfSHans Petter Selasky 
906e57b2d0eSRandall Stewart 	/*
907*9ca874cfSHans Petter Selasky 	 * Now to replace the data properly in the entry
908*9ca874cfSHans Petter Selasky 	 * we have to reset the TCP header and
909e57b2d0eSRandall Stewart 	 * other fields.
910e57b2d0eSRandall Stewart 	 */
911*9ca874cfSHans Petter Selasky 	tcp_set_entry_to_mbuf(lc, le, m, pa->tcp);
912*9ca874cfSHans Petter Selasky 
913e57b2d0eSRandall Stewart 	/* Restore the next list */
914e57b2d0eSRandall Stewart 	m->m_nextpkt = msave;
915e57b2d0eSRandall Stewart }
916e57b2d0eSRandall Stewart 
917e57b2d0eSRandall Stewart static void
918*9ca874cfSHans Petter Selasky tcp_lro_mbuf_append_pkthdr(struct mbuf *m, const struct mbuf *p)
919*9ca874cfSHans Petter Selasky {
920*9ca874cfSHans Petter Selasky 	uint32_t csum;
921*9ca874cfSHans Petter Selasky 
922*9ca874cfSHans Petter Selasky 	if (m->m_pkthdr.lro_nsegs == 1) {
923*9ca874cfSHans Petter Selasky 		/* Compute relative checksum. */
924*9ca874cfSHans Petter Selasky 		csum = p->m_pkthdr.lro_tcp_d_csum;
925*9ca874cfSHans Petter Selasky 	} else {
926*9ca874cfSHans Petter Selasky 		/* Merge TCP data checksums. */
927*9ca874cfSHans Petter Selasky 		csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum +
928*9ca874cfSHans Petter Selasky 		    (uint32_t)p->m_pkthdr.lro_tcp_d_csum;
929*9ca874cfSHans Petter Selasky 		while (csum > 0xffff)
930*9ca874cfSHans Petter Selasky 			csum = (csum >> 16) + (csum & 0xffff);
931*9ca874cfSHans Petter Selasky 	}
932*9ca874cfSHans Petter Selasky 
933*9ca874cfSHans Petter Selasky 	/* Update various counters. */
934*9ca874cfSHans Petter Selasky 	m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len;
935*9ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_csum = csum;
936*9ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len;
937*9ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs;
938*9ca874cfSHans Petter Selasky }
939*9ca874cfSHans Petter Selasky 
940*9ca874cfSHans Petter Selasky static void
941*9ca874cfSHans Petter Selasky tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le)
942e57b2d0eSRandall Stewart {
943e57b2d0eSRandall Stewart 	/*
944e57b2d0eSRandall Stewart 	 * Walk through the mbuf chain we
945e57b2d0eSRandall Stewart 	 * have on tap and compress/condense
946e57b2d0eSRandall Stewart 	 * as required.
947e57b2d0eSRandall Stewart 	 */
948e57b2d0eSRandall Stewart 	uint32_t *ts_ptr;
949e57b2d0eSRandall Stewart 	struct mbuf *m;
950e57b2d0eSRandall Stewart 	struct tcphdr *th;
951*9ca874cfSHans Petter Selasky 	uint32_t tcp_data_len_total;
952*9ca874cfSHans Petter Selasky 	uint32_t tcp_data_seg_total;
953*9ca874cfSHans Petter Selasky 	uint16_t tcp_data_len;
954*9ca874cfSHans Petter Selasky 	uint16_t tcp_opt_len;
955e57b2d0eSRandall Stewart 
956e57b2d0eSRandall Stewart 	/*
957e57b2d0eSRandall Stewart 	 * First we must check the lead (m_head)
958e57b2d0eSRandall Stewart 	 * we must make sure that it is *not*
959e57b2d0eSRandall Stewart 	 * something that should be sent up
960e57b2d0eSRandall Stewart 	 * right away (sack etc).
961e57b2d0eSRandall Stewart 	 */
962e57b2d0eSRandall Stewart again:
963e57b2d0eSRandall Stewart 	m = le->m_head->m_nextpkt;
964e57b2d0eSRandall Stewart 	if (m == NULL) {
965*9ca874cfSHans Petter Selasky 		/* Just one left. */
966e57b2d0eSRandall Stewart 		return;
967e57b2d0eSRandall Stewart 	}
968*9ca874cfSHans Petter Selasky 
969*9ca874cfSHans Petter Selasky 	th = tcp_lro_get_th(m);
970*9ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
971*9ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
972e57b2d0eSRandall Stewart 	ts_ptr = (uint32_t *)(th + 1);
973*9ca874cfSHans Petter Selasky 
974*9ca874cfSHans Petter Selasky 	if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
975*9ca874cfSHans Petter Selasky 	    *ts_ptr != TCP_LRO_TS_OPTION)) {
976e57b2d0eSRandall Stewart 		/*
977e57b2d0eSRandall Stewart 		 * Its not the timestamp. We can't
978e57b2d0eSRandall Stewart 		 * use this guy as the head.
979e57b2d0eSRandall Stewart 		 */
980e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
981*9ca874cfSHans Petter Selasky 		tcp_push_and_replace(lc, le, m);
982e57b2d0eSRandall Stewart 		goto again;
983e57b2d0eSRandall Stewart 	}
984e57b2d0eSRandall Stewart 	if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) {
985e57b2d0eSRandall Stewart 		/*
986e57b2d0eSRandall Stewart 		 * Make sure that previously seen segements/ACKs are delivered
987e57b2d0eSRandall Stewart 		 * before this segment, e.g. FIN.
988e57b2d0eSRandall Stewart 		 */
989e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
990*9ca874cfSHans Petter Selasky 		tcp_push_and_replace(lc, le, m);
991e57b2d0eSRandall Stewart 		goto again;
992e57b2d0eSRandall Stewart 	}
993e57b2d0eSRandall Stewart 	while((m = le->m_head->m_nextpkt) != NULL) {
994e57b2d0eSRandall Stewart 		/*
995e57b2d0eSRandall Stewart 		 * condense m into le, first
996e57b2d0eSRandall Stewart 		 * pull m out of the list.
997e57b2d0eSRandall Stewart 		 */
998e57b2d0eSRandall Stewart 		le->m_head->m_nextpkt = m->m_nextpkt;
999e57b2d0eSRandall Stewart 		m->m_nextpkt = NULL;
1000e57b2d0eSRandall Stewart 		/* Setup my data */
1001*9ca874cfSHans Petter Selasky 		tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
1002*9ca874cfSHans Petter Selasky 		th = tcp_lro_get_th(m);
1003e57b2d0eSRandall Stewart 		ts_ptr = (uint32_t *)(th + 1);
1004*9ca874cfSHans Petter Selasky 		tcp_opt_len = (th->th_off << 2);
1005*9ca874cfSHans Petter Selasky 		tcp_opt_len -= sizeof(*th);
1006*9ca874cfSHans Petter Selasky 		tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len;
1007*9ca874cfSHans Petter Selasky 		tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs;
1008*9ca874cfSHans Petter Selasky 
1009*9ca874cfSHans Petter Selasky 		if (tcp_data_seg_total >= lc->lro_ackcnt_lim ||
1010*9ca874cfSHans Petter Selasky 		    tcp_data_len_total >= lc->lro_length_lim) {
1011e57b2d0eSRandall Stewart 			/* Flush now if appending will result in overflow. */
1012*9ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1013e57b2d0eSRandall Stewart 			goto again;
1014e57b2d0eSRandall Stewart 		}
1015*9ca874cfSHans Petter Selasky 		if (tcp_opt_len != 0 &&
1016*9ca874cfSHans Petter Selasky 		    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
1017*9ca874cfSHans Petter Selasky 		    *ts_ptr != TCP_LRO_TS_OPTION)) {
1018e57b2d0eSRandall Stewart 			/*
1019e57b2d0eSRandall Stewart 			 * Maybe a sack in the new one? We need to
1020e57b2d0eSRandall Stewart 			 * start all over after flushing the
1021e57b2d0eSRandall Stewart 			 * current le. We will go up to the beginning
1022e57b2d0eSRandall Stewart 			 * and flush it (calling the replace again possibly
1023e57b2d0eSRandall Stewart 			 * or just returning).
1024e57b2d0eSRandall Stewart 			 */
1025*9ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1026e57b2d0eSRandall Stewart 			goto again;
1027e57b2d0eSRandall Stewart 		}
1028e57b2d0eSRandall Stewart 		if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) {
1029*9ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1030e57b2d0eSRandall Stewart 			goto again;
1031e57b2d0eSRandall Stewart 		}
1032*9ca874cfSHans Petter Selasky 		if (tcp_opt_len != 0) {
1033e57b2d0eSRandall Stewart 			uint32_t tsval = ntohl(*(ts_ptr + 1));
1034e57b2d0eSRandall Stewart 			/* Make sure timestamp values are increasing. */
1035e57b2d0eSRandall Stewart 			if (TSTMP_GT(le->tsval, tsval))  {
1036*9ca874cfSHans Petter Selasky 				tcp_push_and_replace(lc, le, m);
1037e57b2d0eSRandall Stewart 				goto again;
1038e57b2d0eSRandall Stewart 			}
1039e57b2d0eSRandall Stewart 			le->tsval = tsval;
1040e57b2d0eSRandall Stewart 			le->tsecr = *(ts_ptr + 2);
1041e57b2d0eSRandall Stewart 		}
1042e57b2d0eSRandall Stewart 		/* Try to append the new segment. */
1043e57b2d0eSRandall Stewart 		if (__predict_false(ntohl(th->th_seq) != le->next_seq ||
1044e57b2d0eSRandall Stewart 				    (tcp_data_len == 0 &&
1045e57b2d0eSRandall Stewart 				     le->ack_seq == th->th_ack &&
1046e57b2d0eSRandall Stewart 				     le->window == th->th_win))) {
1047e57b2d0eSRandall Stewart 			/* Out of order packet or duplicate ACK. */
1048*9ca874cfSHans Petter Selasky 			tcp_push_and_replace(lc, le, m);
1049e57b2d0eSRandall Stewart 			goto again;
1050e57b2d0eSRandall Stewart 		}
1051*9ca874cfSHans Petter Selasky 		if (tcp_data_len != 0 ||
1052*9ca874cfSHans Petter Selasky 		    SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1053e57b2d0eSRandall Stewart 			le->next_seq += tcp_data_len;
1054e57b2d0eSRandall Stewart 			le->ack_seq = th->th_ack;
1055e57b2d0eSRandall Stewart 			le->window = th->th_win;
1056e57b2d0eSRandall Stewart 		} else if (th->th_ack == le->ack_seq) {
1057e57b2d0eSRandall Stewart 			le->window = WIN_MAX(le->window, th->th_win);
1058e57b2d0eSRandall Stewart 		}
1059*9ca874cfSHans Petter Selasky 
1060e57b2d0eSRandall Stewart 		if (tcp_data_len == 0) {
1061e57b2d0eSRandall Stewart 			m_freem(m);
1062e57b2d0eSRandall Stewart 			continue;
1063e57b2d0eSRandall Stewart 		}
1064*9ca874cfSHans Petter Selasky 
1065*9ca874cfSHans Petter Selasky 		/* Merge TCP data checksum and length to head mbuf. */
1066*9ca874cfSHans Petter Selasky 		tcp_lro_mbuf_append_pkthdr(le->m_head, m);
1067*9ca874cfSHans Petter Selasky 
1068e57b2d0eSRandall Stewart 		/*
1069e57b2d0eSRandall Stewart 		 * Adjust the mbuf so that m_data points to the first byte of
1070e57b2d0eSRandall Stewart 		 * the ULP payload.  Adjust the mbuf to avoid complications and
1071e57b2d0eSRandall Stewart 		 * append new segment to existing mbuf chain.
1072e57b2d0eSRandall Stewart 		 */
1073e57b2d0eSRandall Stewart 		m_adj(m, m->m_pkthdr.len - tcp_data_len);
1074e57b2d0eSRandall Stewart 		m_demote_pkthdr(m);
1075e57b2d0eSRandall Stewart 		le->m_tail->m_next = m;
1076e57b2d0eSRandall Stewart 		le->m_tail = m_last(m);
1077e57b2d0eSRandall Stewart 	}
1078e57b2d0eSRandall Stewart }
1079e57b2d0eSRandall Stewart 
1080373013b0SConrad Meyer #ifdef TCPHPTS
1081e57b2d0eSRandall Stewart static void
1082*9ca874cfSHans Petter Selasky tcp_queue_pkts(struct inpcb *inp, struct tcpcb *tp, struct lro_entry *le)
1083e57b2d0eSRandall Stewart {
1084*9ca874cfSHans Petter Selasky 	INP_WLOCK_ASSERT(inp);
1085e57b2d0eSRandall Stewart 	if (tp->t_in_pkt == NULL) {
1086e57b2d0eSRandall Stewart 		/* Nothing yet there */
1087e57b2d0eSRandall Stewart 		tp->t_in_pkt = le->m_head;
1088e57b2d0eSRandall Stewart 		tp->t_tail_pkt = le->m_last_mbuf;
1089e57b2d0eSRandall Stewart 	} else {
1090e57b2d0eSRandall Stewart 		/* Already some there */
1091e57b2d0eSRandall Stewart 		tp->t_tail_pkt->m_nextpkt = le->m_head;
1092e57b2d0eSRandall Stewart 		tp->t_tail_pkt = le->m_last_mbuf;
1093e57b2d0eSRandall Stewart 	}
1094e57b2d0eSRandall Stewart 	le->m_head = NULL;
1095e57b2d0eSRandall Stewart 	le->m_last_mbuf = NULL;
1096e57b2d0eSRandall Stewart }
1097e57b2d0eSRandall Stewart 
109869a34e8dSRandall Stewart static struct mbuf *
1099*9ca874cfSHans Petter Selasky tcp_lro_get_last_if_ackcmp(struct lro_ctrl *lc, struct lro_entry *le,
1100*9ca874cfSHans Petter Selasky     struct inpcb *inp, int32_t *new_m)
1101e57b2d0eSRandall Stewart {
110269a34e8dSRandall Stewart 	struct tcpcb *tp;
1103*9ca874cfSHans Petter Selasky 	struct mbuf *m;
1104e57b2d0eSRandall Stewart 
110569a34e8dSRandall Stewart 	tp = intotcpcb(inp);
1106*9ca874cfSHans Petter Selasky 	if (__predict_false(tp == NULL))
1107*9ca874cfSHans Petter Selasky 		return (NULL);
1108*9ca874cfSHans Petter Selasky 
110969a34e8dSRandall Stewart 	/* Look at the last mbuf if any in queue */
111069a34e8dSRandall Stewart 	m = tp->t_tail_pkt;
1111*9ca874cfSHans Petter Selasky 	if (m != NULL && (m->m_flags & M_ACKCMP) != 0) {
1112*9ca874cfSHans Petter Selasky 		if (M_TRAILINGSPACE(m) >= sizeof(struct tcp_ackent)) {
1113*9ca874cfSHans Petter Selasky 			tcp_lro_log(tp, lc, le, NULL, 23, 0, 0, 0, 0);
1114*9ca874cfSHans Petter Selasky 			*new_m = 0;
1115*9ca874cfSHans Petter Selasky 			counter_u64_add(tcp_extra_mbuf, 1);
1116*9ca874cfSHans Petter Selasky 			return (m);
111769a34e8dSRandall Stewart 		} else {
1118*9ca874cfSHans Petter Selasky 			/* Mark we ran out of space */
111969a34e8dSRandall Stewart 			inp->inp_flags2 |= INP_MBUF_L_ACKS;
112069a34e8dSRandall Stewart 		}
112169a34e8dSRandall Stewart 	}
1122*9ca874cfSHans Petter Selasky 	/* Decide mbuf size. */
1123*9ca874cfSHans Petter Selasky 	if (inp->inp_flags2 & INP_MBUF_L_ACKS)
1124*9ca874cfSHans Petter Selasky 		m = m_getcl(M_NOWAIT, MT_DATA, M_ACKCMP | M_PKTHDR);
1125*9ca874cfSHans Petter Selasky 	else
1126*9ca874cfSHans Petter Selasky 		m = m_gethdr(M_NOWAIT, MT_DATA);
1127*9ca874cfSHans Petter Selasky 
1128*9ca874cfSHans Petter Selasky 	if (__predict_false(m == NULL)) {
1129*9ca874cfSHans Petter Selasky 		counter_u64_add(tcp_would_have_but, 1);
1130*9ca874cfSHans Petter Selasky 		return (NULL);
113169a34e8dSRandall Stewart 	}
1132*9ca874cfSHans Petter Selasky 	counter_u64_add(tcp_comp_total, 1);
1133*9ca874cfSHans Petter Selasky 	m->m_flags |= M_ACKCMP;
1134*9ca874cfSHans Petter Selasky 	*new_m = 1;
113569a34e8dSRandall Stewart 	return (m);
113669a34e8dSRandall Stewart }
113769a34e8dSRandall Stewart 
113869a34e8dSRandall Stewart static struct inpcb *
1139*9ca874cfSHans Petter Selasky tcp_lro_lookup(struct ifnet *ifp, struct lro_parser *pa)
114069a34e8dSRandall Stewart {
1141*9ca874cfSHans Petter Selasky 	struct inpcb *inp;
114269a34e8dSRandall Stewart 
114369a34e8dSRandall Stewart 	NET_EPOCH_ASSERT();
1144*9ca874cfSHans Petter Selasky 
1145*9ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
1146e57b2d0eSRandall Stewart #ifdef INET6
1147*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
1148*9ca874cfSHans Petter Selasky 		inp = in6_pcblookup(&V_tcbinfo,
1149*9ca874cfSHans Petter Selasky 		    &pa->data.s_addr.v6,
1150*9ca874cfSHans Petter Selasky 		    pa->data.s_port,
1151*9ca874cfSHans Petter Selasky 		    &pa->data.d_addr.v6,
1152*9ca874cfSHans Petter Selasky 		    pa->data.d_port,
1153e57b2d0eSRandall Stewart 		    INPLOOKUP_WLOCKPCB,
1154*9ca874cfSHans Petter Selasky 		    ifp);
1155e57b2d0eSRandall Stewart 		break;
1156e57b2d0eSRandall Stewart #endif
1157e57b2d0eSRandall Stewart #ifdef INET
1158*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
1159*9ca874cfSHans Petter Selasky 		inp = in_pcblookup(&V_tcbinfo,
1160*9ca874cfSHans Petter Selasky 		    pa->data.s_addr.v4,
1161*9ca874cfSHans Petter Selasky 		    pa->data.s_port,
1162*9ca874cfSHans Petter Selasky 		    pa->data.d_addr.v4,
1163*9ca874cfSHans Petter Selasky 		    pa->data.d_port,
1164e57b2d0eSRandall Stewart 		    INPLOOKUP_WLOCKPCB,
1165*9ca874cfSHans Petter Selasky 		    ifp);
1166e57b2d0eSRandall Stewart 		break;
1167e57b2d0eSRandall Stewart #endif
1168*9ca874cfSHans Petter Selasky 	default:
1169*9ca874cfSHans Petter Selasky 		inp = NULL;
1170*9ca874cfSHans Petter Selasky 		break;
1171e57b2d0eSRandall Stewart 	}
117269a34e8dSRandall Stewart 	return (inp);
117369a34e8dSRandall Stewart }
117469a34e8dSRandall Stewart 
1175*9ca874cfSHans Petter Selasky static inline bool
1176*9ca874cfSHans Petter Selasky tcp_lro_ack_valid(struct mbuf *m, struct tcphdr *th, uint32_t **ppts, bool *other_opts)
1177*9ca874cfSHans Petter Selasky {
1178*9ca874cfSHans Petter Selasky 	/*
1179*9ca874cfSHans Petter Selasky 	 * This function returns two bits of valuable information.
1180*9ca874cfSHans Petter Selasky 	 * a) Is what is present capable of being ack-compressed,
1181*9ca874cfSHans Petter Selasky 	 *    we can ack-compress if there is no options or just
1182*9ca874cfSHans Petter Selasky 	 *    a timestamp option, and of course the th_flags must
1183*9ca874cfSHans Petter Selasky 	 *    be correct as well.
1184*9ca874cfSHans Petter Selasky 	 * b) Our other options present such as SACK. This is
1185*9ca874cfSHans Petter Selasky 	 *    used to determine if we want to wakeup or not.
1186*9ca874cfSHans Petter Selasky 	 */
1187*9ca874cfSHans Petter Selasky 	bool ret = true;
1188*9ca874cfSHans Petter Selasky 
1189*9ca874cfSHans Petter Selasky 	switch (th->th_off << 2) {
1190*9ca874cfSHans Petter Selasky 	case (sizeof(*th) + TCPOLEN_TSTAMP_APPA):
1191*9ca874cfSHans Petter Selasky 		*ppts = (uint32_t *)(th + 1);
1192*9ca874cfSHans Petter Selasky 		/* Check if we have only one timestamp option. */
1193*9ca874cfSHans Petter Selasky 		if (**ppts == TCP_LRO_TS_OPTION)
1194*9ca874cfSHans Petter Selasky 			*other_opts = false;
1195*9ca874cfSHans Petter Selasky 		else {
1196*9ca874cfSHans Petter Selasky 			*other_opts = true;
1197*9ca874cfSHans Petter Selasky 			ret = false;
1198*9ca874cfSHans Petter Selasky 		}
1199*9ca874cfSHans Petter Selasky 		break;
1200*9ca874cfSHans Petter Selasky 	case (sizeof(*th)):
1201*9ca874cfSHans Petter Selasky 		/* No options. */
1202*9ca874cfSHans Petter Selasky 		*ppts = NULL;
1203*9ca874cfSHans Petter Selasky 		*other_opts = false;
1204*9ca874cfSHans Petter Selasky 		break;
1205*9ca874cfSHans Petter Selasky 	default:
1206*9ca874cfSHans Petter Selasky 		*ppts = NULL;
1207*9ca874cfSHans Petter Selasky 		*other_opts = true;
1208*9ca874cfSHans Petter Selasky 		ret = false;
1209*9ca874cfSHans Petter Selasky 		break;
1210*9ca874cfSHans Petter Selasky 	}
1211*9ca874cfSHans Petter Selasky 	/* For ACKCMP we only accept ACK, PUSH, ECE and CWR. */
1212*9ca874cfSHans Petter Selasky 	if ((th->th_flags & ~(TH_ACK | TH_PUSH | TH_ECE | TH_CWR)) != 0)
1213*9ca874cfSHans Petter Selasky 		ret = false;
1214*9ca874cfSHans Petter Selasky 	/* If it has data on it we cannot compress it */
1215*9ca874cfSHans Petter Selasky 	if (m->m_pkthdr.lro_tcp_d_len)
1216*9ca874cfSHans Petter Selasky 		ret = false;
1217*9ca874cfSHans Petter Selasky 
1218*9ca874cfSHans Petter Selasky 	/* ACK flag must be set. */
1219*9ca874cfSHans Petter Selasky 	if (!(th->th_flags & TH_ACK))
1220*9ca874cfSHans Petter Selasky 		ret = false;
1221*9ca874cfSHans Petter Selasky 	return (ret);
1222*9ca874cfSHans Petter Selasky }
1223*9ca874cfSHans Petter Selasky 
1224*9ca874cfSHans Petter Selasky static int
1225*9ca874cfSHans Petter Selasky tcp_lro_flush_tcphpts(struct lro_ctrl *lc, struct lro_entry *le)
1226*9ca874cfSHans Petter Selasky {
1227*9ca874cfSHans Petter Selasky 	struct inpcb *inp;
1228*9ca874cfSHans Petter Selasky 	struct tcpcb *tp;
1229*9ca874cfSHans Petter Selasky 	struct mbuf **pp, *cmp, *mv_to;
1230*9ca874cfSHans Petter Selasky 	bool bpf_req, should_wake;
1231*9ca874cfSHans Petter Selasky 
1232*9ca874cfSHans Petter Selasky 	/* Check if packet doesn't belongs to our network interface. */
1233*9ca874cfSHans Petter Selasky 	if ((tcplro_stacks_wanting_mbufq == 0) ||
1234*9ca874cfSHans Petter Selasky 	    (le->outer.data.vlan_id != 0) ||
1235*9ca874cfSHans Petter Selasky 	    (le->inner.data.lro_type != LRO_TYPE_NONE))
1236*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
1237*9ca874cfSHans Petter Selasky 
1238*9ca874cfSHans Petter Selasky #ifdef INET6
1239*9ca874cfSHans Petter Selasky 	/*
1240*9ca874cfSHans Petter Selasky 	 * Be proactive about unspecified IPv6 address in source. As
1241*9ca874cfSHans Petter Selasky 	 * we use all-zero to indicate unbounded/unconnected pcb,
1242*9ca874cfSHans Petter Selasky 	 * unspecified IPv6 address can be used to confuse us.
1243*9ca874cfSHans Petter Selasky 	 *
1244*9ca874cfSHans Petter Selasky 	 * Note that packets with unspecified IPv6 destination is
1245*9ca874cfSHans Petter Selasky 	 * already dropped in ip6_input.
1246*9ca874cfSHans Petter Selasky 	 */
1247*9ca874cfSHans Petter Selasky 	if (__predict_false(le->outer.data.lro_type == LRO_TYPE_IPV6_TCP &&
1248*9ca874cfSHans Petter Selasky 	    IN6_IS_ADDR_UNSPECIFIED(&le->outer.data.s_addr.v6)))
1249*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
1250*9ca874cfSHans Petter Selasky 
1251*9ca874cfSHans Petter Selasky 	if (__predict_false(le->inner.data.lro_type == LRO_TYPE_IPV6_TCP &&
1252*9ca874cfSHans Petter Selasky 	    IN6_IS_ADDR_UNSPECIFIED(&le->inner.data.s_addr.v6)))
1253*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
125469a34e8dSRandall Stewart #endif
1255*9ca874cfSHans Petter Selasky 	/* Lookup inp, if any. */
1256*9ca874cfSHans Petter Selasky 	inp = tcp_lro_lookup(lc->ifp,
1257*9ca874cfSHans Petter Selasky 	    (le->inner.data.lro_type == LRO_TYPE_NONE) ? &le->outer : &le->inner);
1258*9ca874cfSHans Petter Selasky 	if (inp == NULL)
1259*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
126069a34e8dSRandall Stewart 
1261*9ca874cfSHans Petter Selasky 	counter_u64_add(tcp_inp_lro_locks_taken, 1);
1262*9ca874cfSHans Petter Selasky 
1263*9ca874cfSHans Petter Selasky 	/* Get TCP control structure. */
1264*9ca874cfSHans Petter Selasky 	tp = intotcpcb(inp);
1265*9ca874cfSHans Petter Selasky 
1266*9ca874cfSHans Petter Selasky 	/* Check if the inp is dead, Jim. */
1267*9ca874cfSHans Petter Selasky 	if (tp == NULL ||
1268*9ca874cfSHans Petter Selasky 	    (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) ||
1269*9ca874cfSHans Petter Selasky 	    (inp->inp_flags2 & INP_FREED)) {
1270*9ca874cfSHans Petter Selasky 		INP_WUNLOCK(inp);
1271*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
127269a34e8dSRandall Stewart 	}
127369a34e8dSRandall Stewart 
1274*9ca874cfSHans Petter Selasky 	/* Check if the transport doesn't support the needed optimizations. */
1275*9ca874cfSHans Petter Selasky 	if ((inp->inp_flags2 & (INP_SUPPORTS_MBUFQ | INP_MBUF_ACKCMP)) == 0) {
1276*9ca874cfSHans Petter Selasky 		INP_WUNLOCK(inp);
1277*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
127869a34e8dSRandall Stewart 	}
1279*9ca874cfSHans Petter Selasky 
1280*9ca874cfSHans Petter Selasky 	if (inp->inp_flags2 & INP_MBUF_QUEUE_READY)
1281*9ca874cfSHans Petter Selasky 		should_wake = false;
1282*9ca874cfSHans Petter Selasky 	else
1283*9ca874cfSHans Petter Selasky 		should_wake = true;
1284*9ca874cfSHans Petter Selasky 	/* Check if packets should be tapped to BPF. */
1285*9ca874cfSHans Petter Selasky 	bpf_req = bpf_peers_present(lc->ifp->if_bpf);
1286*9ca874cfSHans Petter Selasky 
1287*9ca874cfSHans Petter Selasky 	/* Strip and compress all the incoming packets. */
1288*9ca874cfSHans Petter Selasky 	cmp = NULL;
1289*9ca874cfSHans Petter Selasky 	for (pp = &le->m_head; *pp != NULL; ) {
1290*9ca874cfSHans Petter Selasky 		mv_to = NULL;
1291*9ca874cfSHans Petter Selasky 		if (do_bpf_strip_and_compress(inp, lc, le, pp,
1292*9ca874cfSHans Petter Selasky 			 &cmp, &mv_to, &should_wake, bpf_req ) == false) {
1293*9ca874cfSHans Petter Selasky 			/* Advance to next mbuf. */
1294*9ca874cfSHans Petter Selasky 			pp = &(*pp)->m_nextpkt;
1295*9ca874cfSHans Petter Selasky 		} else if (mv_to != NULL) {
1296*9ca874cfSHans Petter Selasky 			/* We are asked to move pp up */
1297*9ca874cfSHans Petter Selasky 			pp = &mv_to->m_nextpkt;
1298*9ca874cfSHans Petter Selasky 		}
1299*9ca874cfSHans Petter Selasky 	}
1300*9ca874cfSHans Petter Selasky 	/* Update "m_last_mbuf", if any. */
1301*9ca874cfSHans Petter Selasky 	if (pp == &le->m_head)
1302*9ca874cfSHans Petter Selasky 		le->m_last_mbuf = *pp;
1303*9ca874cfSHans Petter Selasky 	else
1304*9ca874cfSHans Petter Selasky 		le->m_last_mbuf = __containerof(pp, struct mbuf, m_nextpkt);
1305*9ca874cfSHans Petter Selasky 
1306*9ca874cfSHans Petter Selasky 	/* Check if any data mbufs left. */
1307*9ca874cfSHans Petter Selasky 	if (le->m_head != NULL) {
1308*9ca874cfSHans Petter Selasky 		counter_u64_add(tcp_inp_lro_direct_queue, 1);
1309*9ca874cfSHans Petter Selasky 		tcp_lro_log(tp, lc, le, NULL, 22, 1,
1310*9ca874cfSHans Petter Selasky 			    inp->inp_flags2, inp->inp_in_input, 1);
1311*9ca874cfSHans Petter Selasky 		tcp_queue_pkts(inp, tp, le);
1312*9ca874cfSHans Petter Selasky 	}
1313*9ca874cfSHans Petter Selasky 	if (should_wake) {
1314*9ca874cfSHans Petter Selasky 		/* Wakeup */
1315*9ca874cfSHans Petter Selasky 		counter_u64_add(tcp_inp_lro_wokeup_queue, 1);
1316*9ca874cfSHans Petter Selasky 		if ((*tp->t_fb->tfb_do_queued_segments)(inp->inp_socket, tp, 0))
1317*9ca874cfSHans Petter Selasky 			inp = NULL;
1318*9ca874cfSHans Petter Selasky 	}
1319*9ca874cfSHans Petter Selasky 	if (inp != NULL)
1320*9ca874cfSHans Petter Selasky 		INP_WUNLOCK(inp);
1321*9ca874cfSHans Petter Selasky 	return (0);	/* Success. */
132269a34e8dSRandall Stewart }
132369a34e8dSRandall Stewart #endif
132469a34e8dSRandall Stewart 
132569a34e8dSRandall Stewart void
132669a34e8dSRandall Stewart tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
132769a34e8dSRandall Stewart {
1328*9ca874cfSHans Petter Selasky 	/* Only optimise if there are multiple packets waiting. */
132969a34e8dSRandall Stewart #ifdef TCPHPTS
1330*9ca874cfSHans Petter Selasky 	int error;
133169a34e8dSRandall Stewart 
133269a34e8dSRandall Stewart 	CURVNET_SET(lc->ifp->if_vnet);
1333*9ca874cfSHans Petter Selasky 	error = tcp_lro_flush_tcphpts(lc, le);
1334*9ca874cfSHans Petter Selasky 	CURVNET_RESTORE();
1335*9ca874cfSHans Petter Selasky 	if (error != 0) {
1336*9ca874cfSHans Petter Selasky #endif
1337*9ca874cfSHans Petter Selasky 		tcp_lro_condense(lc, le);
1338*9ca874cfSHans Petter Selasky 		tcp_flush_out_entry(lc, le);
1339e57b2d0eSRandall Stewart #ifdef TCPHPTS
1340e57b2d0eSRandall Stewart 	}
1341e57b2d0eSRandall Stewart #endif
134262b5b6ecSBjoern A. Zeeb 	lc->lro_flushed++;
134362b5b6ecSBjoern A. Zeeb 	bzero(le, sizeof(*le));
13441ea44822SSepherosa Ziehau 	LIST_INSERT_HEAD(&lc->lro_free, le, next);
134562b5b6ecSBjoern A. Zeeb }
13466c5087a8SJack F Vogel 
1347fc271df3SHans Petter Selasky #ifdef HAVE_INLINE_FLSLL
1348fc271df3SHans Petter Selasky #define	tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1))
1349fc271df3SHans Petter Selasky #else
1350fc271df3SHans Petter Selasky static inline uint64_t
1351fc271df3SHans Petter Selasky tcp_lro_msb_64(uint64_t x)
1352e936121dSHans Petter Selasky {
1353fc271df3SHans Petter Selasky 	x |= (x >> 1);
1354fc271df3SHans Petter Selasky 	x |= (x >> 2);
1355fc271df3SHans Petter Selasky 	x |= (x >> 4);
1356fc271df3SHans Petter Selasky 	x |= (x >> 8);
1357fc271df3SHans Petter Selasky 	x |= (x >> 16);
1358fc271df3SHans Petter Selasky 	x |= (x >> 32);
1359fc271df3SHans Petter Selasky 	return (x & ~(x >> 1));
1360fc271df3SHans Petter Selasky }
1361fc271df3SHans Petter Selasky #endif
1362e936121dSHans Petter Selasky 
1363fc271df3SHans Petter Selasky /*
1364fc271df3SHans Petter Selasky  * The tcp_lro_sort() routine is comparable to qsort(), except it has
1365fc271df3SHans Petter Selasky  * a worst case complexity limit of O(MIN(N,64)*N), where N is the
1366fc271df3SHans Petter Selasky  * number of elements to sort and 64 is the number of sequence bits
1367fc271df3SHans Petter Selasky  * available. The algorithm is bit-slicing the 64-bit sequence number,
1368fc271df3SHans Petter Selasky  * sorting one bit at a time from the most significant bit until the
1369ec668905SHans Petter Selasky  * least significant one, skipping the constant bits. This is
1370ec668905SHans Petter Selasky  * typically called a radix sort.
1371fc271df3SHans Petter Selasky  */
1372fc271df3SHans Petter Selasky static void
1373fc271df3SHans Petter Selasky tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size)
1374fc271df3SHans Petter Selasky {
1375fc271df3SHans Petter Selasky 	struct lro_mbuf_sort temp;
1376fc271df3SHans Petter Selasky 	uint64_t ones;
1377fc271df3SHans Petter Selasky 	uint64_t zeros;
1378fc271df3SHans Petter Selasky 	uint32_t x;
1379fc271df3SHans Petter Selasky 	uint32_t y;
1380e936121dSHans Petter Selasky 
1381fc271df3SHans Petter Selasky repeat:
1382ec668905SHans Petter Selasky 	/* for small arrays insertion sort is faster */
1383fc271df3SHans Petter Selasky 	if (size <= 12) {
1384ec668905SHans Petter Selasky 		for (x = 1; x < size; x++) {
1385fc271df3SHans Petter Selasky 			temp = parray[x];
1386ec668905SHans Petter Selasky 			for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--)
1387ec668905SHans Petter Selasky 				parray[y] = parray[y - 1];
1388fc271df3SHans Petter Selasky 			parray[y] = temp;
1389fc271df3SHans Petter Selasky 		}
1390fc271df3SHans Petter Selasky 		return;
1391fc271df3SHans Petter Selasky 	}
1392e936121dSHans Petter Selasky 
1393fc271df3SHans Petter Selasky 	/* compute sequence bits which are constant */
1394fc271df3SHans Petter Selasky 	ones = 0;
1395fc271df3SHans Petter Selasky 	zeros = 0;
1396fc271df3SHans Petter Selasky 	for (x = 0; x != size; x++) {
1397fc271df3SHans Petter Selasky 		ones |= parray[x].seq;
1398fc271df3SHans Petter Selasky 		zeros |= ~parray[x].seq;
1399fc271df3SHans Petter Selasky 	}
1400fc271df3SHans Petter Selasky 
1401fc271df3SHans Petter Selasky 	/* compute bits which are not constant into "ones" */
1402fc271df3SHans Petter Selasky 	ones &= zeros;
1403fc271df3SHans Petter Selasky 	if (ones == 0)
1404fc271df3SHans Petter Selasky 		return;
1405fc271df3SHans Petter Selasky 
1406fc271df3SHans Petter Selasky 	/* pick the most significant bit which is not constant */
1407fc271df3SHans Petter Selasky 	ones = tcp_lro_msb_64(ones);
1408fc271df3SHans Petter Selasky 
1409fc271df3SHans Petter Selasky 	/*
1410fc271df3SHans Petter Selasky 	 * Move entries having cleared sequence bits to the beginning
1411fc271df3SHans Petter Selasky 	 * of the array:
1412fc271df3SHans Petter Selasky 	 */
1413fc271df3SHans Petter Selasky 	for (x = y = 0; y != size; y++) {
1414fc271df3SHans Petter Selasky 		/* skip set bits */
1415fc271df3SHans Petter Selasky 		if (parray[y].seq & ones)
1416fc271df3SHans Petter Selasky 			continue;
1417fc271df3SHans Petter Selasky 		/* swap entries */
1418fc271df3SHans Petter Selasky 		temp = parray[x];
1419fc271df3SHans Petter Selasky 		parray[x] = parray[y];
1420fc271df3SHans Petter Selasky 		parray[y] = temp;
1421fc271df3SHans Petter Selasky 		x++;
1422fc271df3SHans Petter Selasky 	}
1423fc271df3SHans Petter Selasky 
1424fc271df3SHans Petter Selasky 	KASSERT(x != 0 && x != size, ("Memory is corrupted\n"));
1425fc271df3SHans Petter Selasky 
1426fc271df3SHans Petter Selasky 	/* sort zeros */
1427fc271df3SHans Petter Selasky 	tcp_lro_sort(parray, x);
1428fc271df3SHans Petter Selasky 
1429fc271df3SHans Petter Selasky 	/* sort ones */
1430fc271df3SHans Petter Selasky 	parray += x;
1431fc271df3SHans Petter Selasky 	size -= x;
1432fc271df3SHans Petter Selasky 	goto repeat;
1433e936121dSHans Petter Selasky }
1434e936121dSHans Petter Selasky 
1435e936121dSHans Petter Selasky void
1436e936121dSHans Petter Selasky tcp_lro_flush_all(struct lro_ctrl *lc)
1437e936121dSHans Petter Selasky {
1438fc271df3SHans Petter Selasky 	uint64_t seq;
1439fc271df3SHans Petter Selasky 	uint64_t nseq;
1440e936121dSHans Petter Selasky 	unsigned x;
1441e936121dSHans Petter Selasky 
1442*9ca874cfSHans Petter Selasky 	CURVNET_SET(lc->ifp->if_vnet);
1443*9ca874cfSHans Petter Selasky 
1444e936121dSHans Petter Selasky 	/* check if no mbufs to flush */
14456dd38b87SSepherosa Ziehau 	if (lc->lro_mbuf_count == 0)
1446e936121dSHans Petter Selasky 		goto done;
1447e936121dSHans Petter Selasky 
1448*9ca874cfSHans Petter Selasky 	/* get current time */
1449*9ca874cfSHans Petter Selasky 	lc->lro_last_queue_time = getsbinuptime();
1450*9ca874cfSHans Petter Selasky 
1451e936121dSHans Petter Selasky 	/* sort all mbufs according to stream */
1452fc271df3SHans Petter Selasky 	tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count);
1453e936121dSHans Petter Selasky 
1454e936121dSHans Petter Selasky 	/* input data into LRO engine, stream by stream */
1455fc271df3SHans Petter Selasky 	seq = 0;
1456e936121dSHans Petter Selasky 	for (x = 0; x != lc->lro_mbuf_count; x++) {
1457e936121dSHans Petter Selasky 		struct mbuf *mb;
1458e936121dSHans Petter Selasky 
1459fc271df3SHans Petter Selasky 		/* get mbuf */
1460fc271df3SHans Petter Selasky 		mb = lc->lro_mbuf_data[x].mb;
1461fc271df3SHans Petter Selasky 
1462fc271df3SHans Petter Selasky 		/* get sequence number, masking away the packet index */
1463fc271df3SHans Petter Selasky 		nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24);
1464e936121dSHans Petter Selasky 
1465e936121dSHans Petter Selasky 		/* check for new stream */
1466fc271df3SHans Petter Selasky 		if (seq != nseq) {
1467fc271df3SHans Petter Selasky 			seq = nseq;
1468e936121dSHans Petter Selasky 
1469e936121dSHans Petter Selasky 			/* flush active streams */
14706dd38b87SSepherosa Ziehau 			tcp_lro_rx_done(lc);
1471e936121dSHans Petter Selasky 		}
1472fc271df3SHans Petter Selasky 
1473e936121dSHans Petter Selasky 		/* add packet to LRO engine */
1474*9ca874cfSHans Petter Selasky 		if (tcp_lro_rx_common(lc, mb, 0, false) != 0) {
1475e936121dSHans Petter Selasky 			/* input packet to network layer */
1476e936121dSHans Petter Selasky 			(*lc->ifp->if_input)(lc->ifp, mb);
1477e936121dSHans Petter Selasky 			lc->lro_queued++;
1478e936121dSHans Petter Selasky 			lc->lro_flushed++;
1479e936121dSHans Petter Selasky 		}
1480e936121dSHans Petter Selasky 	}
1481e936121dSHans Petter Selasky done:
1482e936121dSHans Petter Selasky 	/* flush active streams */
14836dd38b87SSepherosa Ziehau 	tcp_lro_rx_done(lc);
14846dd38b87SSepherosa Ziehau 
1485e936121dSHans Petter Selasky 	lc->lro_mbuf_count = 0;
1486e936121dSHans Petter Selasky 
1487*9ca874cfSHans Petter Selasky 	CURVNET_RESTORE();
148862b5b6ecSBjoern A. Zeeb }
14896c5087a8SJack F Vogel 
1490ab4fad4bSRandall Stewart #ifdef TCPHPTS
149169a34e8dSRandall Stewart static void
1492*9ca874cfSHans Petter Selasky build_ack_entry(struct tcp_ackent *ae, struct tcphdr *th, struct mbuf *m,
1493*9ca874cfSHans Petter Selasky     uint32_t *ts_ptr, uint16_t iptos)
149469a34e8dSRandall Stewart {
149569a34e8dSRandall Stewart 	/*
1496*9ca874cfSHans Petter Selasky 	 * Given a TCP ACK, summarize it down into the small TCP ACK
1497*9ca874cfSHans Petter Selasky 	 * entry.
149869a34e8dSRandall Stewart 	 */
149969a34e8dSRandall Stewart 	ae->timestamp = m->m_pkthdr.rcv_tstmp;
150069a34e8dSRandall Stewart 	if (m->m_flags & M_TSTMP_LRO)
150169a34e8dSRandall Stewart 		ae->flags = TSTMP_LRO;
150269a34e8dSRandall Stewart 	else if (m->m_flags & M_TSTMP)
150369a34e8dSRandall Stewart 		ae->flags = TSTMP_HDWR;
150469a34e8dSRandall Stewart 	ae->seq = ntohl(th->th_seq);
150569a34e8dSRandall Stewart 	ae->ack = ntohl(th->th_ack);
150669a34e8dSRandall Stewart 	ae->flags |= th->th_flags;
1507*9ca874cfSHans Petter Selasky 	if (ts_ptr != NULL) {
1508*9ca874cfSHans Petter Selasky 		ae->ts_value = ntohl(ts_ptr[1]);
1509*9ca874cfSHans Petter Selasky 		ae->ts_echo = ntohl(ts_ptr[2]);
151069a34e8dSRandall Stewart 		ae->flags |= HAS_TSTMP;
151169a34e8dSRandall Stewart 	}
151269a34e8dSRandall Stewart 	ae->win = ntohs(th->th_win);
151369a34e8dSRandall Stewart 	ae->codepoint = iptos;
151469a34e8dSRandall Stewart }
151569a34e8dSRandall Stewart 
151669a34e8dSRandall Stewart /*
1517*9ca874cfSHans Petter Selasky  * Do BPF tap for either ACK_CMP packets or MBUF QUEUE type packets
1518*9ca874cfSHans Petter Selasky  * and strip all, but the IPv4/IPv6 header.
151969a34e8dSRandall Stewart  */
1520*9ca874cfSHans Petter Selasky static bool
1521*9ca874cfSHans Petter Selasky do_bpf_strip_and_compress(struct inpcb *inp, struct lro_ctrl *lc,
1522*9ca874cfSHans Petter Selasky     struct lro_entry *le, struct mbuf **pp, struct mbuf **cmp, struct mbuf **mv_to,
1523*9ca874cfSHans Petter Selasky     bool *should_wake, bool bpf_req)
1524*9ca874cfSHans Petter Selasky {
1525*9ca874cfSHans Petter Selasky 	union {
1526*9ca874cfSHans Petter Selasky 		void *ptr;
1527*9ca874cfSHans Petter Selasky 		struct ip *ip4;
1528*9ca874cfSHans Petter Selasky 		struct ip6_hdr *ip6;
1529*9ca874cfSHans Petter Selasky 	} l3;
1530*9ca874cfSHans Petter Selasky 	struct mbuf *m;
1531*9ca874cfSHans Petter Selasky 	struct mbuf *nm;
153269a34e8dSRandall Stewart 	struct tcphdr *th;
1533*9ca874cfSHans Petter Selasky 	struct tcp_ackent *ack_ent;
1534*9ca874cfSHans Petter Selasky 	uint32_t *ts_ptr;
1535*9ca874cfSHans Petter Selasky 	int32_t n_mbuf;
1536*9ca874cfSHans Petter Selasky 	bool other_opts, can_compress;
1537*9ca874cfSHans Petter Selasky 	uint16_t lro_type;
1538*9ca874cfSHans Petter Selasky 	uint16_t iptos;
1539*9ca874cfSHans Petter Selasky 	int tcp_hdr_offset;
1540*9ca874cfSHans Petter Selasky 	int idx;
154169a34e8dSRandall Stewart 
1542*9ca874cfSHans Petter Selasky 	/* Get current mbuf. */
1543*9ca874cfSHans Petter Selasky 	m = *pp;
154469a34e8dSRandall Stewart 
154569a34e8dSRandall Stewart 	/* Let the BPF see the packet */
1546*9ca874cfSHans Petter Selasky 	if (__predict_false(bpf_req))
154769a34e8dSRandall Stewart 		ETHER_BPF_MTAP(lc->ifp, m);
1548*9ca874cfSHans Petter Selasky 
1549*9ca874cfSHans Petter Selasky 	tcp_hdr_offset = m->m_pkthdr.lro_tcp_h_off;
1550*9ca874cfSHans Petter Selasky 	lro_type = le->inner.data.lro_type;
1551*9ca874cfSHans Petter Selasky 	switch (lro_type) {
1552*9ca874cfSHans Petter Selasky 	case LRO_TYPE_NONE:
1553*9ca874cfSHans Petter Selasky 		lro_type = le->outer.data.lro_type;
1554*9ca874cfSHans Petter Selasky 		switch (lro_type) {
1555*9ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV4_TCP:
1556*9ca874cfSHans Petter Selasky 			tcp_hdr_offset -= sizeof(*le->outer.ip4);
1557*9ca874cfSHans Petter Selasky 			m->m_pkthdr.lro_etype = ETHERTYPE_IP;
1558*9ca874cfSHans Petter Selasky 			break;
1559*9ca874cfSHans Petter Selasky 		case LRO_TYPE_IPV6_TCP:
1560*9ca874cfSHans Petter Selasky 			tcp_hdr_offset -= sizeof(*le->outer.ip6);
1561*9ca874cfSHans Petter Selasky 			m->m_pkthdr.lro_etype = ETHERTYPE_IPV6;
1562*9ca874cfSHans Petter Selasky 			break;
1563*9ca874cfSHans Petter Selasky 		default:
1564*9ca874cfSHans Petter Selasky 			goto compressed;
1565*9ca874cfSHans Petter Selasky 		}
1566*9ca874cfSHans Petter Selasky 		break;
1567*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
1568*9ca874cfSHans Petter Selasky 		tcp_hdr_offset -= sizeof(*le->outer.ip4);
1569*9ca874cfSHans Petter Selasky 		m->m_pkthdr.lro_etype = ETHERTYPE_IP;
1570*9ca874cfSHans Petter Selasky 		break;
1571*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
1572*9ca874cfSHans Petter Selasky 		tcp_hdr_offset -= sizeof(*le->outer.ip6);
1573*9ca874cfSHans Petter Selasky 		m->m_pkthdr.lro_etype = ETHERTYPE_IPV6;
1574*9ca874cfSHans Petter Selasky 		break;
1575*9ca874cfSHans Petter Selasky 	default:
1576*9ca874cfSHans Petter Selasky 		goto compressed;
1577*9ca874cfSHans Petter Selasky 	}
1578*9ca874cfSHans Petter Selasky 
1579*9ca874cfSHans Petter Selasky 	MPASS(tcp_hdr_offset >= 0);
1580*9ca874cfSHans Petter Selasky 
1581*9ca874cfSHans Petter Selasky 	m_adj(m, tcp_hdr_offset);
158269a34e8dSRandall Stewart 	m->m_flags |= M_LRO_EHDRSTRP;
1583*9ca874cfSHans Petter Selasky 	m->m_flags &= ~M_ACKCMP;
1584*9ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_h_off -= tcp_hdr_offset;
158569a34e8dSRandall Stewart 
1586*9ca874cfSHans Petter Selasky 	th = tcp_lro_get_th(m);
158769a34e8dSRandall Stewart 
1588*9ca874cfSHans Petter Selasky 	th->th_sum = 0;		/* TCP checksum is valid. */
158969a34e8dSRandall Stewart 
1590*9ca874cfSHans Petter Selasky 	/* Check if ACK can be compressed */
1591*9ca874cfSHans Petter Selasky 	can_compress = tcp_lro_ack_valid(m, th, &ts_ptr, &other_opts);
159269a34e8dSRandall Stewart 
1593*9ca874cfSHans Petter Selasky 	/* Now lets look at the should wake states */
1594*9ca874cfSHans Petter Selasky 	if ((other_opts == true) &&
1595*9ca874cfSHans Petter Selasky 	    ((inp->inp_flags2 & INP_DONT_SACK_QUEUE) == 0)) {
159669a34e8dSRandall Stewart 		/*
1597*9ca874cfSHans Petter Selasky 		 * If there are other options (SACK?) and the
1598*9ca874cfSHans Petter Selasky 		 * tcp endpoint has not expressly told us it does
1599*9ca874cfSHans Petter Selasky 		 * not care about SACKS, then we should wake up.
160069a34e8dSRandall Stewart 		 */
1601*9ca874cfSHans Petter Selasky 		*should_wake = true;
160269a34e8dSRandall Stewart 	}
1603*9ca874cfSHans Petter Selasky 	/* Is the ack compressable? */
1604*9ca874cfSHans Petter Selasky 	if (can_compress == false)
1605*9ca874cfSHans Petter Selasky 		goto done;
1606*9ca874cfSHans Petter Selasky 	/* Does the TCP endpoint support ACK compression? */
1607*9ca874cfSHans Petter Selasky 	if ((inp->inp_flags2 & INP_MBUF_ACKCMP) == 0)
1608*9ca874cfSHans Petter Selasky 		goto done;
1609*9ca874cfSHans Petter Selasky 
1610*9ca874cfSHans Petter Selasky 	/* Lets get the TOS/traffic class field */
1611*9ca874cfSHans Petter Selasky 	l3.ptr = mtod(m, void *);
1612*9ca874cfSHans Petter Selasky 	switch (lro_type) {
1613*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
1614*9ca874cfSHans Petter Selasky 		iptos = l3.ip4->ip_tos;
1615*9ca874cfSHans Petter Selasky 		break;
1616*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV6_TCP:
1617*9ca874cfSHans Petter Selasky 		iptos = IPV6_TRAFFIC_CLASS(l3.ip6);
1618*9ca874cfSHans Petter Selasky 		break;
1619*9ca874cfSHans Petter Selasky 	default:
1620*9ca874cfSHans Petter Selasky 		iptos = 0;	/* Keep compiler happy. */
162169a34e8dSRandall Stewart 		break;
162269a34e8dSRandall Stewart 	}
1623*9ca874cfSHans Petter Selasky 	/* Now lets get space if we don't have some already */
1624*9ca874cfSHans Petter Selasky 	if (*cmp == NULL) {
1625*9ca874cfSHans Petter Selasky new_one:
1626*9ca874cfSHans Petter Selasky 		nm = tcp_lro_get_last_if_ackcmp(lc, le, inp, &n_mbuf);
1627*9ca874cfSHans Petter Selasky 		if (__predict_false(nm == NULL))
1628*9ca874cfSHans Petter Selasky 			goto done;
1629*9ca874cfSHans Petter Selasky 		*cmp = nm;
1630*9ca874cfSHans Petter Selasky 		if (n_mbuf) {
163169a34e8dSRandall Stewart 			/*
1632*9ca874cfSHans Petter Selasky 			 *  Link in the new cmp ack to our in-order place,
1633*9ca874cfSHans Petter Selasky 			 * first set our cmp ack's next to where we are.
163469a34e8dSRandall Stewart 			 */
1635*9ca874cfSHans Petter Selasky 			nm->m_nextpkt = m;
1636*9ca874cfSHans Petter Selasky 			(*pp) = nm;
1637*9ca874cfSHans Petter Selasky 			/*
1638*9ca874cfSHans Petter Selasky 			 * Set it up so mv_to is advanced to our
1639*9ca874cfSHans Petter Selasky 			 * compressed ack. This way the caller can
1640*9ca874cfSHans Petter Selasky 			 * advance pp to the right place.
1641*9ca874cfSHans Petter Selasky 			 */
1642*9ca874cfSHans Petter Selasky 			*mv_to = nm;
1643*9ca874cfSHans Petter Selasky 			/*
1644*9ca874cfSHans Petter Selasky 			 * Advance it here locally as well.
1645*9ca874cfSHans Petter Selasky 			 */
1646*9ca874cfSHans Petter Selasky 			pp = &nm->m_nextpkt;
164769a34e8dSRandall Stewart 		}
1648*9ca874cfSHans Petter Selasky 	} else {
1649*9ca874cfSHans Petter Selasky 		/* We have one already we are working on */
1650*9ca874cfSHans Petter Selasky 		nm = *cmp;
1651*9ca874cfSHans Petter Selasky 		if (M_TRAILINGSPACE(nm) < sizeof(struct tcp_ackent)) {
1652*9ca874cfSHans Petter Selasky 			/* We ran out of space */
1653*9ca874cfSHans Petter Selasky 			inp->inp_flags2 |= INP_MBUF_L_ACKS;
1654*9ca874cfSHans Petter Selasky 			goto new_one;
1655*9ca874cfSHans Petter Selasky 		}
1656*9ca874cfSHans Petter Selasky 	}
1657*9ca874cfSHans Petter Selasky 	MPASS(M_TRAILINGSPACE(nm) >= sizeof(struct tcp_ackent));
1658*9ca874cfSHans Petter Selasky 	counter_u64_add(tcp_inp_lro_compressed, 1);
1659*9ca874cfSHans Petter Selasky 	le->compressed++;
1660*9ca874cfSHans Petter Selasky 	/* We can add in to the one on the tail */
1661*9ca874cfSHans Petter Selasky 	ack_ent = mtod(nm, struct tcp_ackent *);
1662*9ca874cfSHans Petter Selasky 	idx = (nm->m_len / sizeof(struct tcp_ackent));
1663*9ca874cfSHans Petter Selasky 	build_ack_entry(&ack_ent[idx], th, m, ts_ptr, iptos);
166469a34e8dSRandall Stewart 
1665*9ca874cfSHans Petter Selasky 	/* Bump the size of both pkt-hdr and len */
1666*9ca874cfSHans Petter Selasky 	nm->m_len += sizeof(struct tcp_ackent);
1667*9ca874cfSHans Petter Selasky 	nm->m_pkthdr.len += sizeof(struct tcp_ackent);
1668*9ca874cfSHans Petter Selasky compressed:
1669*9ca874cfSHans Petter Selasky 	/* Advance to next mbuf before freeing. */
1670*9ca874cfSHans Petter Selasky 	*pp = m->m_nextpkt;
1671*9ca874cfSHans Petter Selasky 	m->m_nextpkt = NULL;
167269a34e8dSRandall Stewart 	m_freem(m);
1673*9ca874cfSHans Petter Selasky 	return (true);
1674*9ca874cfSHans Petter Selasky done:
1675*9ca874cfSHans Petter Selasky 	counter_u64_add(tcp_uncomp_total, 1);
1676*9ca874cfSHans Petter Selasky 	le->uncompressed++;
1677*9ca874cfSHans Petter Selasky 	return (false);
167869a34e8dSRandall Stewart }
167969a34e8dSRandall Stewart #endif
1680*9ca874cfSHans Petter Selasky 
1681*9ca874cfSHans Petter Selasky static struct lro_head *
1682*9ca874cfSHans Petter Selasky tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser)
1683*9ca874cfSHans Petter Selasky {
1684*9ca874cfSHans Petter Selasky 	u_long hash;
1685*9ca874cfSHans Petter Selasky 
1686*9ca874cfSHans Petter Selasky 	if (M_HASHTYPE_ISHASH(m)) {
1687*9ca874cfSHans Petter Selasky 		hash = m->m_pkthdr.flowid;
1688*9ca874cfSHans Petter Selasky 	} else {
1689*9ca874cfSHans Petter Selasky 		for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++)
1690*9ca874cfSHans Petter Selasky 			hash += parser->data.raw[i];
169169a34e8dSRandall Stewart 	}
1692*9ca874cfSHans Petter Selasky 	return (&lc->lro_hash[hash % lc->lro_hashsz]);
1693*9ca874cfSHans Petter Selasky }
169469a34e8dSRandall Stewart 
169505cde7efSSepherosa Ziehau static int
1696*9ca874cfSHans Petter Selasky tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash)
169762b5b6ecSBjoern A. Zeeb {
1698*9ca874cfSHans Petter Selasky 	struct lro_parser pi;	/* inner address data */
1699*9ca874cfSHans Petter Selasky 	struct lro_parser po;	/* outer address data */
1700*9ca874cfSHans Petter Selasky 	struct lro_parser *pa;	/* current parser for TCP stream */
170162b5b6ecSBjoern A. Zeeb 	struct lro_entry *le;
170205cde7efSSepherosa Ziehau 	struct lro_head *bucket;
1703*9ca874cfSHans Petter Selasky 	struct tcphdr *th;
1704*9ca874cfSHans Petter Selasky 	int tcp_data_len;
1705*9ca874cfSHans Petter Selasky 	int tcp_opt_len;
1706*9ca874cfSHans Petter Selasky 	int error;
1707*9ca874cfSHans Petter Selasky 	uint16_t tcp_data_sum;
17086c5087a8SJack F Vogel 
1709*9ca874cfSHans Petter Selasky #ifdef INET
1710*9ca874cfSHans Petter Selasky 	/* Quickly decide if packet cannot be LRO'ed */
1711*9ca874cfSHans Petter Selasky 	if (__predict_false(V_ipforwarding != 0))
1712*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
1713*9ca874cfSHans Petter Selasky #endif
1714*9ca874cfSHans Petter Selasky #ifdef INET6
1715*9ca874cfSHans Petter Selasky 	/* Quickly decide if packet cannot be LRO'ed */
1716*9ca874cfSHans Petter Selasky 	if (__predict_false(V_ip6_forwarding != 0))
1717*9ca874cfSHans Petter Selasky 		return (TCP_LRO_CANNOT);
1718*9ca874cfSHans Petter Selasky #endif
171969a34e8dSRandall Stewart 
172062b5b6ecSBjoern A. Zeeb 	/* We expect a contiguous header [eh, ip, tcp]. */
1721*9ca874cfSHans Petter Selasky 	pa = tcp_lro_parser(m, &po, &pi, true);
1722*9ca874cfSHans Petter Selasky 	if (__predict_false(pa == NULL))
1723*9ca874cfSHans Petter Selasky 		return (TCP_LRO_NOT_SUPPORTED);
1724*9ca874cfSHans Petter Selasky 
1725*9ca874cfSHans Petter Selasky 	/* We don't expect any padding. */
1726*9ca874cfSHans Petter Selasky 	error = tcp_lro_trim_mbuf_chain(m, pa);
1727*9ca874cfSHans Petter Selasky 	if (__predict_false(error != 0))
1728*9ca874cfSHans Petter Selasky 		return (error);
1729*9ca874cfSHans Petter Selasky 
1730*9ca874cfSHans Petter Selasky #ifdef INET
1731*9ca874cfSHans Petter Selasky 	switch (pa->data.lro_type) {
1732*9ca874cfSHans Petter Selasky 	case LRO_TYPE_IPV4_TCP:
1733*9ca874cfSHans Petter Selasky 		error = tcp_lro_rx_ipv4(lc, m, pa->ip4);
1734*9ca874cfSHans Petter Selasky 		if (__predict_false(error != 0))
1735*9ca874cfSHans Petter Selasky 			return (error);
1736*9ca874cfSHans Petter Selasky 		break;
1737*9ca874cfSHans Petter Selasky 	default:
1738*9ca874cfSHans Petter Selasky 		break;
1739*9ca874cfSHans Petter Selasky 	}
1740*9ca874cfSHans Petter Selasky #endif
1741*9ca874cfSHans Petter Selasky 	/* If no hardware or arrival stamp on the packet add timestamp */
1742e57b2d0eSRandall Stewart 	if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) {
1743*9ca874cfSHans Petter Selasky 		m->m_pkthdr.rcv_tstmp = sbttons(lc->lro_last_queue_time);
1744e57b2d0eSRandall Stewart 		m->m_flags |= M_TSTMP_LRO;
1745e57b2d0eSRandall Stewart 	}
17466c5087a8SJack F Vogel 
1747*9ca874cfSHans Petter Selasky 	/* Get pointer to TCP header. */
1748*9ca874cfSHans Petter Selasky 	th = pa->tcp;
1749*9ca874cfSHans Petter Selasky 
1750*9ca874cfSHans Petter Selasky 	/* Don't process SYN packets. */
1751*9ca874cfSHans Petter Selasky 	if (__predict_false(th->th_flags & TH_SYN))
175262b5b6ecSBjoern A. Zeeb 		return (TCP_LRO_CANNOT);
175362b5b6ecSBjoern A. Zeeb 
1754*9ca874cfSHans Petter Selasky 	/* Get total TCP header length and compute payload length. */
1755*9ca874cfSHans Petter Selasky 	tcp_opt_len = (th->th_off << 2);
1756*9ca874cfSHans Petter Selasky 	tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th -
1757*9ca874cfSHans Petter Selasky 	    (uint8_t *)m->m_data) - tcp_opt_len;
1758*9ca874cfSHans Petter Selasky 	tcp_opt_len -= sizeof(*th);
1759*9ca874cfSHans Petter Selasky 
1760*9ca874cfSHans Petter Selasky 	/* Don't process invalid TCP headers. */
1761*9ca874cfSHans Petter Selasky 	if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0))
176262b5b6ecSBjoern A. Zeeb 		return (TCP_LRO_CANNOT);
1763*9ca874cfSHans Petter Selasky 
1764*9ca874cfSHans Petter Selasky 	/* Compute TCP data only checksum. */
1765*9ca874cfSHans Petter Selasky 	if (tcp_data_len == 0)
1766*9ca874cfSHans Petter Selasky 		tcp_data_sum = 0;	/* no data, no checksum */
1767*9ca874cfSHans Petter Selasky 	else if (__predict_false(csum != 0))
1768*9ca874cfSHans Petter Selasky 		tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum);
1769*9ca874cfSHans Petter Selasky 	else
1770*9ca874cfSHans Petter Selasky 		tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum);
1771*9ca874cfSHans Petter Selasky 
1772*9ca874cfSHans Petter Selasky 	/* Save TCP info in mbuf. */
1773*9ca874cfSHans Petter Selasky 	m->m_nextpkt = NULL;
1774*9ca874cfSHans Petter Selasky 	m->m_pkthdr.rcvif = lc->ifp;
1775*9ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum;
1776*9ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_d_len = tcp_data_len;
1777*9ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data);
1778*9ca874cfSHans Petter Selasky 	m->m_pkthdr.lro_nsegs = 1;
1779*9ca874cfSHans Petter Selasky 
1780*9ca874cfSHans Petter Selasky 	/* Get hash bucket. */
178105cde7efSSepherosa Ziehau 	if (!use_hash) {
178205cde7efSSepherosa Ziehau 		bucket = &lc->lro_hash[0];
178305cde7efSSepherosa Ziehau 	} else {
1784*9ca874cfSHans Petter Selasky 		bucket = tcp_lro_rx_get_bucket(lc, m, pa);
178505cde7efSSepherosa Ziehau 	}
178605cde7efSSepherosa Ziehau 
178762b5b6ecSBjoern A. Zeeb 	/* Try to find a matching previous segment. */
178805cde7efSSepherosa Ziehau 	LIST_FOREACH(le, bucket, hash_next) {
1789*9ca874cfSHans Petter Selasky 		/* Compare addresses and ports. */
1790*9ca874cfSHans Petter Selasky 		if (lro_address_compare(&po.data, &le->outer.data) == false ||
1791*9ca874cfSHans Petter Selasky 		    lro_address_compare(&pi.data, &le->inner.data) == false)
179262b5b6ecSBjoern A. Zeeb 			continue;
1793*9ca874cfSHans Petter Selasky 
1794*9ca874cfSHans Petter Selasky 		/* Check if no data and old ACK. */
1795*9ca874cfSHans Petter Selasky 		if (tcp_data_len == 0 &&
1796*9ca874cfSHans Petter Selasky 		    SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1797d7fb35d1SSean Bruno 			m_freem(m);
1798d7fb35d1SSean Bruno 			return (0);
1799d7fb35d1SSean Bruno 		}
180069a34e8dSRandall Stewart 
1801*9ca874cfSHans Petter Selasky 		/* Mark "m" in the last spot. */
1802e57b2d0eSRandall Stewart 		le->m_last_mbuf->m_nextpkt = m;
1803*9ca874cfSHans Petter Selasky 		/* Now set the tail to "m". */
1804e57b2d0eSRandall Stewart 		le->m_last_mbuf = m;
180562b5b6ecSBjoern A. Zeeb 		return (0);
18066c5087a8SJack F Vogel 	}
1807*9ca874cfSHans Petter Selasky 
180862b5b6ecSBjoern A. Zeeb 	/* Try to find an empty slot. */
18091ea44822SSepherosa Ziehau 	if (LIST_EMPTY(&lc->lro_free))
1810489f0c3cSSepherosa Ziehau 		return (TCP_LRO_NO_ENTRIES);
181162b5b6ecSBjoern A. Zeeb 
181262b5b6ecSBjoern A. Zeeb 	/* Start a new segment chain. */
18131ea44822SSepherosa Ziehau 	le = LIST_FIRST(&lc->lro_free);
18141ea44822SSepherosa Ziehau 	LIST_REMOVE(le, next);
181505cde7efSSepherosa Ziehau 	tcp_lro_active_insert(lc, bucket, le);
181662b5b6ecSBjoern A. Zeeb 
1817*9ca874cfSHans Petter Selasky 	/* Make sure the headers are set. */
1818*9ca874cfSHans Petter Selasky 	le->inner = pi;
1819*9ca874cfSHans Petter Selasky 	le->outer = po;
182062b5b6ecSBjoern A. Zeeb 
1821*9ca874cfSHans Petter Selasky 	/* Store time this entry was allocated. */
1822*9ca874cfSHans Petter Selasky 	le->alloc_time = lc->lro_last_queue_time;
182369a34e8dSRandall Stewart 
1824*9ca874cfSHans Petter Selasky 	tcp_set_entry_to_mbuf(lc, le, m, th);
182569a34e8dSRandall Stewart 
1826*9ca874cfSHans Petter Selasky 	/* Now set the tail to "m". */
1827e57b2d0eSRandall Stewart 	le->m_last_mbuf = m;
1828*9ca874cfSHans Petter Selasky 
182962b5b6ecSBjoern A. Zeeb 	return (0);
183062b5b6ecSBjoern A. Zeeb }
183162b5b6ecSBjoern A. Zeeb 
183205cde7efSSepherosa Ziehau int
183305cde7efSSepherosa Ziehau tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
183405cde7efSSepherosa Ziehau {
1835*9ca874cfSHans Petter Selasky 	int error;
183605cde7efSSepherosa Ziehau 
1837*9ca874cfSHans Petter Selasky 	/* get current time */
1838*9ca874cfSHans Petter Selasky 	lc->lro_last_queue_time = getsbinuptime();
1839*9ca874cfSHans Petter Selasky 
1840*9ca874cfSHans Petter Selasky 	CURVNET_SET(lc->ifp->if_vnet);
1841*9ca874cfSHans Petter Selasky 	error = tcp_lro_rx_common(lc, m, csum, true);
1842*9ca874cfSHans Petter Selasky 	CURVNET_RESTORE();
1843*9ca874cfSHans Petter Selasky 
1844*9ca874cfSHans Petter Selasky 	return (error);
184505cde7efSSepherosa Ziehau }
184605cde7efSSepherosa Ziehau 
1847e936121dSHans Petter Selasky void
1848e936121dSHans Petter Selasky tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
1849e936121dSHans Petter Selasky {
1850e936121dSHans Petter Selasky 	/* sanity checks */
1851e936121dSHans Petter Selasky 	if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
1852e936121dSHans Petter Selasky 	    lc->lro_mbuf_max == 0)) {
1853e936121dSHans Petter Selasky 		/* packet drop */
1854e936121dSHans Petter Selasky 		m_freem(mb);
1855e936121dSHans Petter Selasky 		return;
1856e936121dSHans Petter Selasky 	}
1857e936121dSHans Petter Selasky 
1858e936121dSHans Petter Selasky 	/* check if packet is not LRO capable */
1859e936121dSHans Petter Selasky 	if (__predict_false(mb->m_pkthdr.csum_flags == 0 ||
1860e936121dSHans Petter Selasky 	    (lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
1861e936121dSHans Petter Selasky 		/* input packet to network layer */
1862e936121dSHans Petter Selasky 		(*lc->ifp->if_input) (lc->ifp, mb);
1863e936121dSHans Petter Selasky 		return;
1864e936121dSHans Petter Selasky 	}
1865e936121dSHans Petter Selasky 
1866fc271df3SHans Petter Selasky 	/* create sequence number */
1867fc271df3SHans Petter Selasky 	lc->lro_mbuf_data[lc->lro_mbuf_count].seq =
1868fc271df3SHans Petter Selasky 	    (((uint64_t)M_HASHTYPE_GET(mb)) << 56) |
1869fc271df3SHans Petter Selasky 	    (((uint64_t)mb->m_pkthdr.flowid) << 24) |
1870fc271df3SHans Petter Selasky 	    ((uint64_t)lc->lro_mbuf_count);
1871e936121dSHans Petter Selasky 
1872e936121dSHans Petter Selasky 	/* enter mbuf */
1873f8acc03eSNavdeep Parhar 	lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb;
1874f8acc03eSNavdeep Parhar 
1875f8acc03eSNavdeep Parhar 	/* flush if array is full */
1876f8acc03eSNavdeep Parhar 	if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max))
1877f8acc03eSNavdeep Parhar 		tcp_lro_flush_all(lc);
1878e936121dSHans Petter Selasky }
1879e936121dSHans Petter Selasky 
188062b5b6ecSBjoern A. Zeeb /* end */
1881