xref: /freebsd/sys/netinet/tcp_lro.c (revision 9f44a47fd07924afc035991af15d84e6585dea4f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007, Myricom Inc.
5  * Copyright (c) 2008, Intel Corporation.
6  * Copyright (c) 2012 The FreeBSD Foundation
7  * Copyright (c) 2016-2021 Mellanox Technologies.
8  * All rights reserved.
9  *
10  * Portions of this software were developed by Bjoern Zeeb
11  * under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sockbuf.h>
49 #include <sys/sysctl.h>
50 
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/ethernet.h>
54 #include <net/bpf.h>
55 #include <net/vnet.h>
56 #include <net/if_dl.h>
57 #include <net/if_media.h>
58 #include <net/if_private.h>
59 #include <net/if_types.h>
60 #include <net/infiniband.h>
61 #include <net/if_lagg.h>
62 
63 #include <netinet/in_systm.h>
64 #include <netinet/in.h>
65 #include <netinet/ip6.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet6/in6_pcb.h>
70 #include <netinet/tcp.h>
71 #include <netinet/tcp_seq.h>
72 #include <netinet/tcp_lro.h>
73 #include <netinet/tcp_var.h>
74 #include <netinet/tcpip.h>
75 #include <netinet/tcp_hpts.h>
76 #include <netinet/tcp_log_buf.h>
77 #include <netinet/tcp_fsm.h>
78 #include <netinet/udp.h>
79 #include <netinet6/ip6_var.h>
80 
81 #include <machine/in_cksum.h>
82 
83 static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
84 
85 #define	TCP_LRO_TS_OPTION \
86     ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
87 	  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)
88 
89 static void	tcp_lro_rx_done(struct lro_ctrl *lc);
90 static int	tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m,
91 		    uint32_t csum, bool use_hash);
92 
93 #ifdef TCPHPTS
94 static bool	do_bpf_strip_and_compress(struct tcpcb *, struct lro_ctrl *,
95 		struct lro_entry *, struct mbuf **, struct mbuf **, struct mbuf **,
96  		bool *, bool, bool, struct ifnet *, bool);
97 
98 #endif
99 
100 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro,  CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
101     "TCP LRO");
102 
103 static long tcplro_stacks_wanting_mbufq;
104 counter_u64_t tcp_inp_lro_direct_queue;
105 counter_u64_t tcp_inp_lro_wokeup_queue;
106 counter_u64_t tcp_inp_lro_compressed;
107 counter_u64_t tcp_inp_lro_locks_taken;
108 counter_u64_t tcp_extra_mbuf;
109 counter_u64_t tcp_would_have_but;
110 counter_u64_t tcp_comp_total;
111 counter_u64_t tcp_uncomp_total;
112 counter_u64_t tcp_bad_csums;
113 
114 static unsigned	tcp_lro_entries = TCP_LRO_ENTRIES;
115 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
116     CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
117     "default number of LRO entries");
118 
119 static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH;
120 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold,
121     CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0,
122     "Number of interrupts in a row on the same CPU that will make us declare an 'affinity' cpu?");
123 
124 static uint32_t tcp_less_accurate_lro_ts = 0;
125 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_less_accurate,
126     CTLFLAG_MPSAFE, &tcp_less_accurate_lro_ts, 0,
127     "Do we trade off efficency by doing less timestamp operations for time accuracy?");
128 
129 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD,
130     &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport");
131 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD,
132     &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts");
133 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD,
134     &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport");
135 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
136     &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
137 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD,
138     &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp");
139 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD,
140     &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed");
141 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD,
142     &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set");
143 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD,
144     &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP");
145 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD,
146     &tcp_bad_csums, "Number of packets that the common code saw with bad csums");
147 
148 void
149 tcp_lro_reg_mbufq(void)
150 {
151 	atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1);
152 }
153 
154 void
155 tcp_lro_dereg_mbufq(void)
156 {
157 	atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1);
158 }
159 
160 static __inline void
161 tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket,
162     struct lro_entry *le)
163 {
164 
165 	LIST_INSERT_HEAD(&lc->lro_active, le, next);
166 	LIST_INSERT_HEAD(bucket, le, hash_next);
167 }
168 
169 static __inline void
170 tcp_lro_active_remove(struct lro_entry *le)
171 {
172 
173 	LIST_REMOVE(le, next);		/* active list */
174 	LIST_REMOVE(le, hash_next);	/* hash bucket */
175 }
176 
177 int
178 tcp_lro_init(struct lro_ctrl *lc)
179 {
180 	return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0));
181 }
182 
183 int
184 tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
185     unsigned lro_entries, unsigned lro_mbufs)
186 {
187 	struct lro_entry *le;
188 	size_t size;
189 	unsigned i, elements;
190 
191 	lc->lro_bad_csum = 0;
192 	lc->lro_queued = 0;
193 	lc->lro_flushed = 0;
194 	lc->lro_mbuf_count = 0;
195 	lc->lro_mbuf_max = lro_mbufs;
196 	lc->lro_cnt = lro_entries;
197 	lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX;
198 	lc->lro_length_lim = TCP_LRO_LENGTH_MAX;
199 	lc->ifp = ifp;
200 	LIST_INIT(&lc->lro_free);
201 	LIST_INIT(&lc->lro_active);
202 
203 	/* create hash table to accelerate entry lookup */
204 	if (lro_entries > lro_mbufs)
205 		elements = lro_entries;
206 	else
207 		elements = lro_mbufs;
208 	lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz,
209 	    HASH_NOWAIT);
210 	if (lc->lro_hash == NULL) {
211 		memset(lc, 0, sizeof(*lc));
212 		return (ENOMEM);
213 	}
214 
215 	/* compute size to allocate */
216 	size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) +
217 	    (lro_entries * sizeof(*le));
218 	lc->lro_mbuf_data = (struct lro_mbuf_sort *)
219 	    malloc(size, M_LRO, M_NOWAIT | M_ZERO);
220 
221 	/* check for out of memory */
222 	if (lc->lro_mbuf_data == NULL) {
223 		free(lc->lro_hash, M_LRO);
224 		memset(lc, 0, sizeof(*lc));
225 		return (ENOMEM);
226 	}
227 	/* compute offset for LRO entries */
228 	le = (struct lro_entry *)
229 	    (lc->lro_mbuf_data + lro_mbufs);
230 
231 	/* setup linked list */
232 	for (i = 0; i != lro_entries; i++)
233 		LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
234 
235 	return (0);
236 }
237 
238 struct vxlan_header {
239 	uint32_t	vxlh_flags;
240 	uint32_t	vxlh_vni;
241 };
242 
243 static inline void *
244 tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen)
245 {
246 	const struct ether_vlan_header *eh;
247 	void *old;
248 	uint16_t eth_type;
249 
250 	if (update_data)
251 		memset(parser, 0, sizeof(*parser));
252 
253 	old = ptr;
254 
255 	if (is_vxlan) {
256 		const struct vxlan_header *vxh;
257 		vxh = ptr;
258 		ptr = (uint8_t *)ptr + sizeof(*vxh);
259 		if (update_data) {
260 			parser->data.vxlan_vni =
261 			    vxh->vxlh_vni & htonl(0xffffff00);
262 		}
263 	}
264 
265 	eh = ptr;
266 	if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) {
267 		eth_type = eh->evl_proto;
268 		if (update_data) {
269 			/* strip priority and keep VLAN ID only */
270 			parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK);
271 		}
272 		/* advance to next header */
273 		ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
274 		mlen -= (ETHER_HDR_LEN  + ETHER_VLAN_ENCAP_LEN);
275 	} else {
276 		eth_type = eh->evl_encap_proto;
277 		/* advance to next header */
278 		mlen -= ETHER_HDR_LEN;
279 		ptr = (uint8_t *)ptr + ETHER_HDR_LEN;
280 	}
281 	if (__predict_false(mlen <= 0))
282 		return (NULL);
283 	switch (eth_type) {
284 #ifdef INET
285 	case htons(ETHERTYPE_IP):
286 		parser->ip4 = ptr;
287 		if (__predict_false(mlen < sizeof(struct ip)))
288 			return (NULL);
289 		/* Ensure there are no IPv4 options. */
290 		if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4))
291 			break;
292 		/* .. and the packet is not fragmented. */
293 		if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK))
294 			break;
295 		/* .. and the packet has valid src/dst addrs */
296 		if (__predict_false(parser->ip4->ip_src.s_addr == INADDR_ANY ||
297 			parser->ip4->ip_dst.s_addr == INADDR_ANY))
298 			break;
299 		ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2);
300 		mlen -= sizeof(struct ip);
301 		if (update_data) {
302 			parser->data.s_addr.v4 = parser->ip4->ip_src;
303 			parser->data.d_addr.v4 = parser->ip4->ip_dst;
304 		}
305 		switch (parser->ip4->ip_p) {
306 		case IPPROTO_UDP:
307 			if (__predict_false(mlen < sizeof(struct udphdr)))
308 				return (NULL);
309 			parser->udp = ptr;
310 			if (update_data) {
311 				parser->data.lro_type = LRO_TYPE_IPV4_UDP;
312 				parser->data.s_port = parser->udp->uh_sport;
313 				parser->data.d_port = parser->udp->uh_dport;
314 			} else {
315 				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP);
316 			}
317 			ptr = ((uint8_t *)ptr + sizeof(*parser->udp));
318 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
319 			return (ptr);
320 		case IPPROTO_TCP:
321 			parser->tcp = ptr;
322 			if (__predict_false(mlen < sizeof(struct tcphdr)))
323 				return (NULL);
324 			if (update_data) {
325 				parser->data.lro_type = LRO_TYPE_IPV4_TCP;
326 				parser->data.s_port = parser->tcp->th_sport;
327 				parser->data.d_port = parser->tcp->th_dport;
328 			} else {
329 				MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP);
330 			}
331 			if (__predict_false(mlen < (parser->tcp->th_off << 2)))
332 				return (NULL);
333 			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
334 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
335 			return (ptr);
336 		default:
337 			break;
338 		}
339 		break;
340 #endif
341 #ifdef INET6
342 	case htons(ETHERTYPE_IPV6):
343 		parser->ip6 = ptr;
344 		if (__predict_false(mlen < sizeof(struct ip6_hdr)))
345 			return (NULL);
346 		/* Ensure the packet has valid src/dst addrs */
347 		if (__predict_false(IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_src) ||
348 			IN6_IS_ADDR_UNSPECIFIED(&parser->ip6->ip6_dst)))
349 			return (NULL);
350 		ptr = (uint8_t *)ptr + sizeof(*parser->ip6);
351 		if (update_data) {
352 			parser->data.s_addr.v6 = parser->ip6->ip6_src;
353 			parser->data.d_addr.v6 = parser->ip6->ip6_dst;
354 		}
355 		mlen -= sizeof(struct ip6_hdr);
356 		switch (parser->ip6->ip6_nxt) {
357 		case IPPROTO_UDP:
358 			if (__predict_false(mlen < sizeof(struct udphdr)))
359 				return (NULL);
360 			parser->udp = ptr;
361 			if (update_data) {
362 				parser->data.lro_type = LRO_TYPE_IPV6_UDP;
363 				parser->data.s_port = parser->udp->uh_sport;
364 				parser->data.d_port = parser->udp->uh_dport;
365 			} else {
366 				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP);
367 			}
368 			ptr = (uint8_t *)ptr + sizeof(*parser->udp);
369 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
370 			return (ptr);
371 		case IPPROTO_TCP:
372 			if (__predict_false(mlen < sizeof(struct tcphdr)))
373 				return (NULL);
374 			parser->tcp = ptr;
375 			if (update_data) {
376 				parser->data.lro_type = LRO_TYPE_IPV6_TCP;
377 				parser->data.s_port = parser->tcp->th_sport;
378 				parser->data.d_port = parser->tcp->th_dport;
379 			} else {
380 				MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP);
381 			}
382 			if (__predict_false(mlen < (parser->tcp->th_off << 2)))
383 				return (NULL);
384 			ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2);
385 			parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old;
386 			return (ptr);
387 		default:
388 			break;
389 		}
390 		break;
391 #endif
392 	default:
393 		break;
394 	}
395 	/* Invalid packet - cannot parse */
396 	return (NULL);
397 }
398 
399 static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID |
400     CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID;
401 
402 static inline struct lro_parser *
403 tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data)
404 {
405 	void *data_ptr;
406 
407 	/* Try to parse outer headers first. */
408 	data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len);
409 	if (data_ptr == NULL || po->total_hdr_len > m->m_len)
410 		return (NULL);
411 
412 	if (update_data) {
413 		/* Store VLAN ID, if any. */
414 		if (__predict_false(m->m_flags & M_VLANTAG)) {
415 			po->data.vlan_id =
416 			    htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK);
417 		}
418 		/* Store decrypted flag, if any. */
419 		if (__predict_false((m->m_pkthdr.csum_flags &
420 		    CSUM_TLS_MASK) == CSUM_TLS_DECRYPTED))
421 			po->data.lro_flags |= LRO_FLAG_DECRYPTED;
422 	}
423 
424 	switch (po->data.lro_type) {
425 	case LRO_TYPE_IPV4_UDP:
426 	case LRO_TYPE_IPV6_UDP:
427 		/* Check for VXLAN headers. */
428 		if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum)
429 			break;
430 
431 		/* Try to parse inner headers. */
432 		data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true,
433 						    (m->m_len - ((caddr_t)data_ptr - m->m_data)));
434 		if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len)
435 			break;
436 
437 		/* Verify supported header types. */
438 		switch (pi->data.lro_type) {
439 		case LRO_TYPE_IPV4_TCP:
440 		case LRO_TYPE_IPV6_TCP:
441 			return (pi);
442 		default:
443 			break;
444 		}
445 		break;
446 	case LRO_TYPE_IPV4_TCP:
447 	case LRO_TYPE_IPV6_TCP:
448 		if (update_data)
449 			memset(pi, 0, sizeof(*pi));
450 		return (po);
451 	default:
452 		break;
453 	}
454 	return (NULL);
455 }
456 
457 static inline int
458 tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po)
459 {
460 	int len;
461 
462 	switch (po->data.lro_type) {
463 #ifdef INET
464 	case LRO_TYPE_IPV4_TCP:
465 		len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) +
466 		    ntohs(po->ip4->ip_len);
467 		break;
468 #endif
469 #ifdef INET6
470 	case LRO_TYPE_IPV6_TCP:
471 		len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) +
472 		    ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6);
473 		break;
474 #endif
475 	default:
476 		return (TCP_LRO_CANNOT);
477 	}
478 
479 	/*
480 	 * If the frame is padded beyond the end of the IP packet,
481 	 * then trim the extra bytes off:
482 	 */
483 	if (__predict_true(m->m_pkthdr.len == len)) {
484 		return (0);
485 	} else if (m->m_pkthdr.len > len) {
486 		m_adj(m, len - m->m_pkthdr.len);
487 		return (0);
488 	}
489 	return (TCP_LRO_CANNOT);
490 }
491 
492 static struct tcphdr *
493 tcp_lro_get_th(struct mbuf *m)
494 {
495 	return ((struct tcphdr *)((uint8_t *)m->m_data + m->m_pkthdr.lro_tcp_h_off));
496 }
497 
498 static void
499 lro_free_mbuf_chain(struct mbuf *m)
500 {
501 	struct mbuf *save;
502 
503 	while (m) {
504 		save = m->m_nextpkt;
505 		m->m_nextpkt = NULL;
506 		m_freem(m);
507 		m = save;
508 	}
509 }
510 
511 void
512 tcp_lro_free(struct lro_ctrl *lc)
513 {
514 	struct lro_entry *le;
515 	unsigned x;
516 
517 	/* reset LRO free list */
518 	LIST_INIT(&lc->lro_free);
519 
520 	/* free active mbufs, if any */
521 	while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
522 		tcp_lro_active_remove(le);
523 		lro_free_mbuf_chain(le->m_head);
524 	}
525 
526 	/* free hash table */
527 	free(lc->lro_hash, M_LRO);
528 	lc->lro_hash = NULL;
529 	lc->lro_hashsz = 0;
530 
531 	/* free mbuf array, if any */
532 	for (x = 0; x != lc->lro_mbuf_count; x++)
533 		m_freem(lc->lro_mbuf_data[x].mb);
534 	lc->lro_mbuf_count = 0;
535 
536 	/* free allocated memory, if any */
537 	free(lc->lro_mbuf_data, M_LRO);
538 	lc->lro_mbuf_data = NULL;
539 }
540 
541 static uint16_t
542 tcp_lro_rx_csum_tcphdr(const struct tcphdr *th)
543 {
544 	const uint16_t *ptr;
545 	uint32_t csum;
546 	uint16_t len;
547 
548 	csum = -th->th_sum;	/* exclude checksum field */
549 	len = th->th_off;
550 	ptr = (const uint16_t *)th;
551 	while (len--) {
552 		csum += *ptr;
553 		ptr++;
554 		csum += *ptr;
555 		ptr++;
556 	}
557 	while (csum > 0xffff)
558 		csum = (csum >> 16) + (csum & 0xffff);
559 
560 	return (csum);
561 }
562 
563 static uint16_t
564 tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum)
565 {
566 	uint32_t c;
567 	uint16_t cs;
568 
569 	c = tcp_csum;
570 
571 	switch (pa->data.lro_type) {
572 #ifdef INET6
573 	case LRO_TYPE_IPV6_TCP:
574 		/* Compute full pseudo IPv6 header checksum. */
575 		cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0);
576 		break;
577 #endif
578 #ifdef INET
579 	case LRO_TYPE_IPV4_TCP:
580 		/* Compute full pseudo IPv4 header checsum. */
581 		cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP);
582 		cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs));
583 		break;
584 #endif
585 	default:
586 		cs = 0;		/* Keep compiler happy. */
587 		break;
588 	}
589 
590 	/* Complement checksum. */
591 	cs = ~cs;
592 	c += cs;
593 
594 	/* Remove TCP header checksum. */
595 	cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp);
596 	c += cs;
597 
598 	/* Compute checksum remainder. */
599 	while (c > 0xffff)
600 		c = (c >> 16) + (c & 0xffff);
601 
602 	return (c);
603 }
604 
605 static void
606 tcp_lro_rx_done(struct lro_ctrl *lc)
607 {
608 	struct lro_entry *le;
609 
610 	while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
611 		tcp_lro_active_remove(le);
612 		tcp_lro_flush(lc, le);
613 	}
614 }
615 
616 static void
617 tcp_lro_flush_active(struct lro_ctrl *lc)
618 {
619 	struct lro_entry *le;
620 
621 	/*
622 	 * Walk through the list of le entries, and
623 	 * any one that does have packets flush. This
624 	 * is called because we have an inbound packet
625 	 * (e.g. SYN) that has to have all others flushed
626 	 * in front of it. Note we have to do the remove
627 	 * because tcp_lro_flush() assumes that the entry
628 	 * is being freed. This is ok it will just get
629 	 * reallocated again like it was new.
630 	 */
631 	LIST_FOREACH(le, &lc->lro_active, next) {
632 		if (le->m_head != NULL) {
633 			tcp_lro_active_remove(le);
634 			tcp_lro_flush(lc, le);
635 		}
636 	}
637 }
638 
639 void
640 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
641 {
642 	struct lro_entry *le, *le_tmp;
643 	uint64_t now, tov;
644 	struct bintime bt;
645 
646 	NET_EPOCH_ASSERT();
647 	if (LIST_EMPTY(&lc->lro_active))
648 		return;
649 
650 	/* get timeout time and current time in ns */
651 	binuptime(&bt);
652 	now = bintime2ns(&bt);
653 	tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000));
654 	LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
655 		if (now >= (bintime2ns(&le->alloc_time) + tov)) {
656 			tcp_lro_active_remove(le);
657 			tcp_lro_flush(lc, le);
658 		}
659 	}
660 }
661 
662 #ifdef INET
663 static int
664 tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4)
665 {
666 	uint16_t csum;
667 
668 	/* Legacy IP has a header checksum that needs to be correct. */
669 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
670 		if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) {
671 			lc->lro_bad_csum++;
672 			return (TCP_LRO_CANNOT);
673 		}
674 	} else {
675 		csum = in_cksum_hdr(ip4);
676 		if (__predict_false(csum != 0)) {
677 			lc->lro_bad_csum++;
678 			return (TCP_LRO_CANNOT);
679 		}
680 	}
681 	return (0);
682 }
683 #endif
684 
685 #ifdef TCPHPTS
686 static void
687 tcp_lro_log(struct tcpcb *tp, const struct lro_ctrl *lc,
688     const struct lro_entry *le, const struct mbuf *m,
689     int frm, int32_t tcp_data_len, uint32_t th_seq,
690     uint32_t th_ack, uint16_t th_win)
691 {
692 	if (tcp_bblogging_on(tp)) {
693 		union tcp_log_stackspecific log;
694 		struct timeval tv, btv;
695 		uint32_t cts;
696 
697 		cts = tcp_get_usecs(&tv);
698 		memset(&log, 0, sizeof(union tcp_log_stackspecific));
699 		log.u_bbr.flex8 = frm;
700 		log.u_bbr.flex1 = tcp_data_len;
701 		if (m)
702 			log.u_bbr.flex2 = m->m_pkthdr.len;
703 		else
704 			log.u_bbr.flex2 = 0;
705 		if (le->m_head) {
706 			log.u_bbr.flex3 = le->m_head->m_pkthdr.lro_nsegs;
707 			log.u_bbr.flex4 = le->m_head->m_pkthdr.lro_tcp_d_len;
708 			log.u_bbr.flex5 = le->m_head->m_pkthdr.len;
709 			log.u_bbr.delRate = le->m_head->m_flags;
710 			log.u_bbr.rttProp = le->m_head->m_pkthdr.rcv_tstmp;
711 		}
712 		log.u_bbr.inflight = th_seq;
713 		log.u_bbr.delivered = th_ack;
714 		log.u_bbr.timeStamp = cts;
715 		log.u_bbr.epoch = le->next_seq;
716 		log.u_bbr.lt_epoch = le->ack_seq;
717 		log.u_bbr.pacing_gain = th_win;
718 		log.u_bbr.cwnd_gain = le->window;
719 		log.u_bbr.lost = curcpu;
720 		log.u_bbr.cur_del_rate = (uintptr_t)m;
721 		log.u_bbr.bw_inuse = (uintptr_t)le->m_head;
722 		bintime2timeval(&lc->lro_last_queue_time, &btv);
723 		log.u_bbr.flex6 = tcp_tv_to_usectick(&btv);
724 		log.u_bbr.flex7 = le->compressed;
725 		log.u_bbr.pacing_gain = le->uncompressed;
726 		if (in_epoch(net_epoch_preempt))
727 			log.u_bbr.inhpts = 1;
728 		else
729 			log.u_bbr.inhpts = 0;
730 		TCP_LOG_EVENTP(tp, NULL, &tptosocket(tp)->so_rcv,
731 		    &tptosocket(tp)->so_snd,
732 		    TCP_LOG_LRO, 0, 0, &log, false, &tv);
733 	}
734 }
735 #endif
736 
737 static inline void
738 tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum)
739 {
740 	uint32_t csum;
741 
742 	csum = 0xffff - *ptr + value;
743 	while (csum > 0xffff)
744 		csum = (csum >> 16) + (csum & 0xffff);
745 	*ptr = value;
746 	*psum = csum;
747 }
748 
749 static uint16_t
750 tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le,
751     uint16_t payload_len, uint16_t delta_sum)
752 {
753 	uint32_t csum;
754 	uint16_t tlen;
755 	uint16_t temp[5] = {};
756 
757 	switch (pa->data.lro_type) {
758 	case LRO_TYPE_IPV4_TCP:
759 		/* Compute new IPv4 length. */
760 		tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len;
761 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
762 
763 		/* Subtract delta from current IPv4 checksum. */
764 		csum = pa->ip4->ip_sum + 0xffff - temp[0];
765 		while (csum > 0xffff)
766 			csum = (csum >> 16) + (csum & 0xffff);
767 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
768 		goto update_tcp_header;
769 
770 	case LRO_TYPE_IPV6_TCP:
771 		/* Compute new IPv6 length. */
772 		tlen = (pa->tcp->th_off << 2) + payload_len;
773 		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
774 		goto update_tcp_header;
775 
776 	case LRO_TYPE_IPV4_UDP:
777 		/* Compute new IPv4 length. */
778 		tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len;
779 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]);
780 
781 		/* Subtract delta from current IPv4 checksum. */
782 		csum = pa->ip4->ip_sum + 0xffff - temp[0];
783 		while (csum > 0xffff)
784 			csum = (csum >> 16) + (csum & 0xffff);
785 		tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]);
786 		goto update_udp_header;
787 
788 	case LRO_TYPE_IPV6_UDP:
789 		/* Compute new IPv6 length. */
790 		tlen = sizeof(*pa->udp) + payload_len;
791 		tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]);
792 		goto update_udp_header;
793 
794 	default:
795 		return (0);
796 	}
797 
798 update_tcp_header:
799 	/* Compute current TCP header checksum. */
800 	temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp);
801 
802 	/* Incorporate the latest ACK into the TCP header. */
803 	pa->tcp->th_ack = le->ack_seq;
804 	pa->tcp->th_win = le->window;
805 
806 	/* Incorporate latest timestamp into the TCP header. */
807 	if (le->timestamp != 0) {
808 		uint32_t *ts_ptr;
809 
810 		ts_ptr = (uint32_t *)(pa->tcp + 1);
811 		ts_ptr[1] = htonl(le->tsval);
812 		ts_ptr[2] = le->tsecr;
813 	}
814 
815 	/* Compute new TCP header checksum. */
816 	temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp);
817 
818 	/* Compute new TCP checksum. */
819 	csum = pa->tcp->th_sum + 0xffff - delta_sum +
820 	    0xffff - temp[0] + 0xffff - temp[3] + temp[2];
821 	while (csum > 0xffff)
822 		csum = (csum >> 16) + (csum & 0xffff);
823 
824 	/* Assign new TCP checksum. */
825 	tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]);
826 
827 	/* Compute all modififications affecting next checksum. */
828 	csum = temp[0] + temp[1] + 0xffff - temp[2] +
829 	    temp[3] + temp[4] + delta_sum;
830 	while (csum > 0xffff)
831 		csum = (csum >> 16) + (csum & 0xffff);
832 
833 	/* Return delta checksum to next stage, if any. */
834 	return (csum);
835 
836 update_udp_header:
837 	tlen = sizeof(*pa->udp) + payload_len;
838 	/* Assign new UDP length and compute checksum delta. */
839 	tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]);
840 
841 	/* Check if there is a UDP checksum. */
842 	if (__predict_false(pa->udp->uh_sum != 0)) {
843 		/* Compute new UDP checksum. */
844 		csum = pa->udp->uh_sum + 0xffff - delta_sum +
845 		    0xffff - temp[0] + 0xffff - temp[2];
846 		while (csum > 0xffff)
847 			csum = (csum >> 16) + (csum & 0xffff);
848 		/* Assign new UDP checksum. */
849 		tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]);
850 	}
851 
852 	/* Compute all modififications affecting next checksum. */
853 	csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum;
854 	while (csum > 0xffff)
855 		csum = (csum >> 16) + (csum & 0xffff);
856 
857 	/* Return delta checksum to next stage, if any. */
858 	return (csum);
859 }
860 
861 static void
862 tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le)
863 {
864 	/* Check if we need to recompute any checksums. */
865 	if (le->needs_merge) {
866 		uint16_t csum;
867 
868 		switch (le->inner.data.lro_type) {
869 		case LRO_TYPE_IPV4_TCP:
870 			csum = tcp_lro_update_checksum(&le->inner, le,
871 			    le->m_head->m_pkthdr.lro_tcp_d_len,
872 			    le->m_head->m_pkthdr.lro_tcp_d_csum);
873 			csum = tcp_lro_update_checksum(&le->outer, NULL,
874 			    le->m_head->m_pkthdr.lro_tcp_d_len +
875 			    le->inner.total_hdr_len, csum);
876 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
877 			    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
878 			le->m_head->m_pkthdr.csum_data = 0xffff;
879 			if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
880 				le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
881 			break;
882 		case LRO_TYPE_IPV6_TCP:
883 			csum = tcp_lro_update_checksum(&le->inner, le,
884 			    le->m_head->m_pkthdr.lro_tcp_d_len,
885 			    le->m_head->m_pkthdr.lro_tcp_d_csum);
886 			csum = tcp_lro_update_checksum(&le->outer, NULL,
887 			    le->m_head->m_pkthdr.lro_tcp_d_len +
888 			    le->inner.total_hdr_len, csum);
889 			le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
890 			    CSUM_PSEUDO_HDR;
891 			le->m_head->m_pkthdr.csum_data = 0xffff;
892 			if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
893 				le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
894 			break;
895 		case LRO_TYPE_NONE:
896 			switch (le->outer.data.lro_type) {
897 			case LRO_TYPE_IPV4_TCP:
898 				csum = tcp_lro_update_checksum(&le->outer, le,
899 				    le->m_head->m_pkthdr.lro_tcp_d_len,
900 				    le->m_head->m_pkthdr.lro_tcp_d_csum);
901 				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
902 				    CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
903 				le->m_head->m_pkthdr.csum_data = 0xffff;
904 				if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
905 					le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
906 				break;
907 			case LRO_TYPE_IPV6_TCP:
908 				csum = tcp_lro_update_checksum(&le->outer, le,
909 				    le->m_head->m_pkthdr.lro_tcp_d_len,
910 				    le->m_head->m_pkthdr.lro_tcp_d_csum);
911 				le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
912 				    CSUM_PSEUDO_HDR;
913 				le->m_head->m_pkthdr.csum_data = 0xffff;
914 				if (__predict_false(le->outer.data.lro_flags & LRO_FLAG_DECRYPTED))
915 					le->m_head->m_pkthdr.csum_flags |= CSUM_TLS_DECRYPTED;
916 				break;
917 			default:
918 				break;
919 			}
920 			break;
921 		default:
922 			break;
923 		}
924 	}
925 
926 	/*
927 	 * Break any chain, this is not set to NULL on the singleton
928 	 * case m_nextpkt points to m_head. Other case set them
929 	 * m_nextpkt to NULL in push_and_replace.
930 	 */
931 	le->m_head->m_nextpkt = NULL;
932 	lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs;
933 	(*lc->ifp->if_input)(lc->ifp, le->m_head);
934 }
935 
936 static void
937 tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le,
938     struct mbuf *m, struct tcphdr *th)
939 {
940 	uint32_t *ts_ptr;
941 	uint16_t tcp_data_len;
942 	uint16_t tcp_opt_len;
943 
944 	ts_ptr = (uint32_t *)(th + 1);
945 	tcp_opt_len = (th->th_off << 2);
946 	tcp_opt_len -= sizeof(*th);
947 
948 	/* Check if there is a timestamp option. */
949 	if (tcp_opt_len == 0 ||
950 	    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
951 	    *ts_ptr != TCP_LRO_TS_OPTION)) {
952 		/* We failed to find the timestamp option. */
953 		le->timestamp = 0;
954 	} else {
955 		le->timestamp = 1;
956 		le->tsval = ntohl(*(ts_ptr + 1));
957 		le->tsecr = *(ts_ptr + 2);
958 	}
959 
960 	tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
961 
962 	/* Pull out TCP sequence numbers and window size. */
963 	le->next_seq = ntohl(th->th_seq) + tcp_data_len;
964 	le->ack_seq = th->th_ack;
965 	le->window = th->th_win;
966 	le->flags = tcp_get_flags(th);
967 	le->needs_merge = 0;
968 
969 	/* Setup new data pointers. */
970 	le->m_head = m;
971 	le->m_tail = m_last(m);
972 }
973 
974 static void
975 tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
976 {
977 	struct lro_parser *pa;
978 
979 	/*
980 	 * Push up the stack of the current entry
981 	 * and replace it with "m".
982 	 */
983 	struct mbuf *msave;
984 
985 	/* Grab off the next and save it */
986 	msave = le->m_head->m_nextpkt;
987 	le->m_head->m_nextpkt = NULL;
988 
989 	/* Now push out the old entry */
990 	tcp_flush_out_entry(lc, le);
991 
992 	/* Re-parse new header, should not fail. */
993 	pa = tcp_lro_parser(m, &le->outer, &le->inner, false);
994 	KASSERT(pa != NULL,
995 	    ("tcp_push_and_replace: LRO parser failed on m=%p\n", m));
996 
997 	/*
998 	 * Now to replace the data properly in the entry
999 	 * we have to reset the TCP header and
1000 	 * other fields.
1001 	 */
1002 	tcp_set_entry_to_mbuf(lc, le, m, pa->tcp);
1003 
1004 	/* Restore the next list */
1005 	m->m_nextpkt = msave;
1006 }
1007 
1008 static void
1009 tcp_lro_mbuf_append_pkthdr(struct lro_entry *le, const struct mbuf *p)
1010 {
1011 	struct mbuf *m;
1012 	uint32_t csum;
1013 
1014 	m = le->m_head;
1015 	if (m->m_pkthdr.lro_nsegs == 1) {
1016 		/* Compute relative checksum. */
1017 		csum = p->m_pkthdr.lro_tcp_d_csum;
1018 	} else {
1019 		/* Merge TCP data checksums. */
1020 		csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum +
1021 		    (uint32_t)p->m_pkthdr.lro_tcp_d_csum;
1022 		while (csum > 0xffff)
1023 			csum = (csum >> 16) + (csum & 0xffff);
1024 	}
1025 
1026 	/* Update various counters. */
1027 	m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len;
1028 	m->m_pkthdr.lro_tcp_d_csum = csum;
1029 	m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len;
1030 	m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs;
1031 	le->needs_merge = 1;
1032 }
1033 
1034 static void
1035 tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le)
1036 {
1037 	/*
1038 	 * Walk through the mbuf chain we
1039 	 * have on tap and compress/condense
1040 	 * as required.
1041 	 */
1042 	uint32_t *ts_ptr;
1043 	struct mbuf *m;
1044 	struct tcphdr *th;
1045 	uint32_t tcp_data_len_total;
1046 	uint32_t tcp_data_seg_total;
1047 	uint16_t tcp_data_len;
1048 	uint16_t tcp_opt_len;
1049 
1050 	/*
1051 	 * First we must check the lead (m_head)
1052 	 * we must make sure that it is *not*
1053 	 * something that should be sent up
1054 	 * right away (sack etc).
1055 	 */
1056 again:
1057 	m = le->m_head->m_nextpkt;
1058 	if (m == NULL) {
1059 		/* Just one left. */
1060 		return;
1061 	}
1062 
1063 	th = tcp_lro_get_th(m);
1064 	tcp_opt_len = (th->th_off << 2);
1065 	tcp_opt_len -= sizeof(*th);
1066 	ts_ptr = (uint32_t *)(th + 1);
1067 
1068 	if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
1069 	    *ts_ptr != TCP_LRO_TS_OPTION)) {
1070 		/*
1071 		 * Its not the timestamp. We can't
1072 		 * use this guy as the head.
1073 		 */
1074 		le->m_head->m_nextpkt = m->m_nextpkt;
1075 		tcp_push_and_replace(lc, le, m);
1076 		goto again;
1077 	}
1078 	if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
1079 		/*
1080 		 * Make sure that previously seen segments/ACKs are delivered
1081 		 * before this segment, e.g. FIN.
1082 		 */
1083 		le->m_head->m_nextpkt = m->m_nextpkt;
1084 		tcp_push_and_replace(lc, le, m);
1085 		goto again;
1086 	}
1087 	while((m = le->m_head->m_nextpkt) != NULL) {
1088 		/*
1089 		 * condense m into le, first
1090 		 * pull m out of the list.
1091 		 */
1092 		le->m_head->m_nextpkt = m->m_nextpkt;
1093 		m->m_nextpkt = NULL;
1094 		/* Setup my data */
1095 		tcp_data_len = m->m_pkthdr.lro_tcp_d_len;
1096 		th = tcp_lro_get_th(m);
1097 		ts_ptr = (uint32_t *)(th + 1);
1098 		tcp_opt_len = (th->th_off << 2);
1099 		tcp_opt_len -= sizeof(*th);
1100 		tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len;
1101 		tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs;
1102 
1103 		if (tcp_data_seg_total >= lc->lro_ackcnt_lim ||
1104 		    tcp_data_len_total >= lc->lro_length_lim) {
1105 			/* Flush now if appending will result in overflow. */
1106 			tcp_push_and_replace(lc, le, m);
1107 			goto again;
1108 		}
1109 		if (tcp_opt_len != 0 &&
1110 		    __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA ||
1111 		    *ts_ptr != TCP_LRO_TS_OPTION)) {
1112 			/*
1113 			 * Maybe a sack in the new one? We need to
1114 			 * start all over after flushing the
1115 			 * current le. We will go up to the beginning
1116 			 * and flush it (calling the replace again possibly
1117 			 * or just returning).
1118 			 */
1119 			tcp_push_and_replace(lc, le, m);
1120 			goto again;
1121 		}
1122 		if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH)) != 0) {
1123 			tcp_push_and_replace(lc, le, m);
1124 			goto again;
1125 		}
1126 		if (tcp_opt_len != 0) {
1127 			uint32_t tsval = ntohl(*(ts_ptr + 1));
1128 			/* Make sure timestamp values are increasing. */
1129 			if (TSTMP_GT(le->tsval, tsval))  {
1130 				tcp_push_and_replace(lc, le, m);
1131 				goto again;
1132 			}
1133 			le->tsval = tsval;
1134 			le->tsecr = *(ts_ptr + 2);
1135 		}
1136 		/* Try to append the new segment. */
1137 		if (__predict_false(ntohl(th->th_seq) != le->next_seq ||
1138 				    ((tcp_get_flags(th) & TH_ACK) !=
1139 				      (le->flags & TH_ACK)) ||
1140 				    (tcp_data_len == 0 &&
1141 				     le->ack_seq == th->th_ack &&
1142 				     le->window == th->th_win))) {
1143 			/* Out of order packet, non-ACK + ACK or dup ACK. */
1144 			tcp_push_and_replace(lc, le, m);
1145 			goto again;
1146 		}
1147 		if (tcp_data_len != 0 ||
1148 		    SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1149 			le->next_seq += tcp_data_len;
1150 			le->ack_seq = th->th_ack;
1151 			le->window = th->th_win;
1152 			le->needs_merge = 1;
1153 		} else if (th->th_ack == le->ack_seq) {
1154 			if (WIN_GT(th->th_win, le->window)) {
1155 				le->window = th->th_win;
1156 				le->needs_merge = 1;
1157 			}
1158 		}
1159 
1160 		if (tcp_data_len == 0) {
1161 			m_freem(m);
1162 			continue;
1163 		}
1164 
1165 		/* Merge TCP data checksum and length to head mbuf. */
1166 		tcp_lro_mbuf_append_pkthdr(le, m);
1167 
1168 		/*
1169 		 * Adjust the mbuf so that m_data points to the first byte of
1170 		 * the ULP payload.  Adjust the mbuf to avoid complications and
1171 		 * append new segment to existing mbuf chain.
1172 		 */
1173 		m_adj(m, m->m_pkthdr.len - tcp_data_len);
1174 		m_demote_pkthdr(m);
1175 		le->m_tail->m_next = m;
1176 		le->m_tail = m_last(m);
1177 	}
1178 }
1179 
1180 #ifdef TCPHPTS
1181 static void
1182 tcp_queue_pkts(struct tcpcb *tp, struct lro_entry *le)
1183 {
1184 
1185 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1186 
1187 	STAILQ_HEAD(, mbuf) q = { le->m_head,
1188 	    &STAILQ_NEXT(le->m_last_mbuf, m_stailqpkt) };
1189 	STAILQ_CONCAT(&tp->t_inqueue, &q);
1190 	le->m_head = NULL;
1191 	le->m_last_mbuf = NULL;
1192 }
1193 
1194 static bool
1195 tcp_lro_check_wake_status(struct tcpcb *tp)
1196 {
1197 
1198 	if (tp->t_fb->tfb_early_wake_check != NULL)
1199 		return ((tp->t_fb->tfb_early_wake_check)(tp));
1200 	return (false);
1201 }
1202 
1203 static struct mbuf *
1204 tcp_lro_get_last_if_ackcmp(struct lro_ctrl *lc, struct lro_entry *le,
1205     struct tcpcb *tp, int32_t *new_m, bool can_append_old_cmp)
1206 {
1207 	struct mbuf *m;
1208 
1209 	/* Look at the last mbuf if any in queue */
1210  	if (can_append_old_cmp) {
1211 		m = STAILQ_LAST(&tp->t_inqueue, mbuf, m_stailqpkt);
1212 		if (m != NULL && (m->m_flags & M_ACKCMP) != 0) {
1213 			if (M_TRAILINGSPACE(m) >= sizeof(struct tcp_ackent)) {
1214 				tcp_lro_log(tp, lc, le, NULL, 23, 0, 0, 0, 0);
1215 				*new_m = 0;
1216 				counter_u64_add(tcp_extra_mbuf, 1);
1217 				return (m);
1218 			} else {
1219 				/* Mark we ran out of space */
1220 				tp->t_flags2 |= TF2_MBUF_L_ACKS;
1221 			}
1222 		}
1223 	}
1224 	/* Decide mbuf size. */
1225 	tcp_lro_log(tp, lc, le, NULL, 21, 0, 0, 0, 0);
1226 	if (tp->t_flags2 & TF2_MBUF_L_ACKS)
1227 		m = m_getcl(M_NOWAIT, MT_DATA, M_ACKCMP | M_PKTHDR);
1228 	else
1229 		m = m_gethdr(M_NOWAIT, MT_DATA);
1230 
1231 	if (__predict_false(m == NULL)) {
1232 		counter_u64_add(tcp_would_have_but, 1);
1233 		return (NULL);
1234 	}
1235 	counter_u64_add(tcp_comp_total, 1);
1236  	m->m_pkthdr.rcvif = lc->ifp;
1237 	m->m_flags |= M_ACKCMP;
1238 	*new_m = 1;
1239 	return (m);
1240 }
1241 
1242 static struct tcpcb *
1243 tcp_lro_lookup(struct ifnet *ifp, struct lro_parser *pa)
1244 {
1245 	struct inpcb *inp;
1246 
1247 	switch (pa->data.lro_type) {
1248 #ifdef INET6
1249 	case LRO_TYPE_IPV6_TCP:
1250 		inp = in6_pcblookup(&V_tcbinfo,
1251 		    &pa->data.s_addr.v6,
1252 		    pa->data.s_port,
1253 		    &pa->data.d_addr.v6,
1254 		    pa->data.d_port,
1255 		    INPLOOKUP_WLOCKPCB,
1256 		    ifp);
1257 		break;
1258 #endif
1259 #ifdef INET
1260 	case LRO_TYPE_IPV4_TCP:
1261 		inp = in_pcblookup(&V_tcbinfo,
1262 		    pa->data.s_addr.v4,
1263 		    pa->data.s_port,
1264 		    pa->data.d_addr.v4,
1265 		    pa->data.d_port,
1266 		    INPLOOKUP_WLOCKPCB,
1267 		    ifp);
1268 		break;
1269 #endif
1270 	default:
1271 		return (NULL);
1272 	}
1273 
1274 	return (intotcpcb(inp));
1275 }
1276 
1277 static inline bool
1278 tcp_lro_ack_valid(struct mbuf *m, struct tcphdr *th, uint32_t **ppts, bool *other_opts)
1279 {
1280 	/*
1281 	 * This function returns two bits of valuable information.
1282 	 * a) Is what is present capable of being ack-compressed,
1283 	 *    we can ack-compress if there is no options or just
1284 	 *    a timestamp option, and of course the th_flags must
1285 	 *    be correct as well.
1286 	 * b) Our other options present such as SACK. This is
1287 	 *    used to determine if we want to wakeup or not.
1288 	 */
1289 	bool ret = true;
1290 
1291 	switch (th->th_off << 2) {
1292 	case (sizeof(*th) + TCPOLEN_TSTAMP_APPA):
1293 		*ppts = (uint32_t *)(th + 1);
1294 		/* Check if we have only one timestamp option. */
1295 		if (**ppts == TCP_LRO_TS_OPTION)
1296 			*other_opts = false;
1297 		else {
1298 			*other_opts = true;
1299 			ret = false;
1300 		}
1301 		break;
1302 	case (sizeof(*th)):
1303 		/* No options. */
1304 		*ppts = NULL;
1305 		*other_opts = false;
1306 		break;
1307 	default:
1308 		*ppts = NULL;
1309 		*other_opts = true;
1310 		ret = false;
1311 		break;
1312 	}
1313 	/* For ACKCMP we only accept ACK, PUSH, ECE and CWR. */
1314 	if ((tcp_get_flags(th) & ~(TH_ACK | TH_PUSH | TH_ECE | TH_CWR)) != 0)
1315 		ret = false;
1316 	/* If it has data on it we cannot compress it */
1317 	if (m->m_pkthdr.lro_tcp_d_len)
1318 		ret = false;
1319 
1320 	/* ACK flag must be set. */
1321 	if (!(tcp_get_flags(th) & TH_ACK))
1322 		ret = false;
1323 	return (ret);
1324 }
1325 
1326 static int
1327 tcp_lro_flush_tcphpts(struct lro_ctrl *lc, struct lro_entry *le)
1328 {
1329 	struct tcpcb *tp;
1330 	struct mbuf **pp, *cmp, *mv_to;
1331 	struct ifnet *lagg_ifp;
1332  	bool bpf_req, lagg_bpf_req, should_wake, can_append_old_cmp;
1333 
1334 	/* Check if packet doesn't belongs to our network interface. */
1335 	if ((tcplro_stacks_wanting_mbufq == 0) ||
1336 	    (le->outer.data.vlan_id != 0) ||
1337 	    (le->inner.data.lro_type != LRO_TYPE_NONE))
1338 		return (TCP_LRO_CANNOT);
1339 
1340 #ifdef INET6
1341 	/*
1342 	 * Be proactive about unspecified IPv6 address in source. As
1343 	 * we use all-zero to indicate unbounded/unconnected pcb,
1344 	 * unspecified IPv6 address can be used to confuse us.
1345 	 *
1346 	 * Note that packets with unspecified IPv6 destination is
1347 	 * already dropped in ip6_input.
1348 	 */
1349 	if (__predict_false(le->outer.data.lro_type == LRO_TYPE_IPV6_TCP &&
1350 	    IN6_IS_ADDR_UNSPECIFIED(&le->outer.data.s_addr.v6)))
1351 		return (TCP_LRO_CANNOT);
1352 
1353 	if (__predict_false(le->inner.data.lro_type == LRO_TYPE_IPV6_TCP &&
1354 	    IN6_IS_ADDR_UNSPECIFIED(&le->inner.data.s_addr.v6)))
1355 		return (TCP_LRO_CANNOT);
1356 #endif
1357 	/* Lookup inp, if any.  Returns locked TCP inpcb. */
1358 	tp = tcp_lro_lookup(lc->ifp,
1359 	    (le->inner.data.lro_type == LRO_TYPE_NONE) ? &le->outer : &le->inner);
1360 	if (tp == NULL)
1361 		return (TCP_LRO_CANNOT);
1362 
1363 	counter_u64_add(tcp_inp_lro_locks_taken, 1);
1364 
1365 	/* Check if the inp is dead, Jim. */
1366 	if (tp->t_state == TCPS_TIME_WAIT) {
1367 		INP_WUNLOCK(tptoinpcb(tp));
1368 		return (TCP_LRO_CANNOT);
1369 	}
1370 	if (tp->t_lro_cpu == HPTS_CPU_NONE && lc->lro_cpu_is_set == 1)
1371 		tp->t_lro_cpu = lc->lro_last_cpu;
1372 	/* Check if the transport doesn't support the needed optimizations. */
1373 	if ((tp->t_flags2 & (TF2_SUPPORTS_MBUFQ | TF2_MBUF_ACKCMP)) == 0) {
1374 		INP_WUNLOCK(tptoinpcb(tp));
1375 		return (TCP_LRO_CANNOT);
1376 	}
1377 
1378 	if (tp->t_flags2 & TF2_MBUF_QUEUE_READY)
1379 		should_wake = false;
1380 	else
1381 		should_wake = true;
1382 	/* Check if packets should be tapped to BPF. */
1383 	bpf_req = bpf_peers_present(lc->ifp->if_bpf);
1384 	lagg_bpf_req = false;
1385 	lagg_ifp = NULL;
1386 	if (lc->ifp->if_type == IFT_IEEE8023ADLAG ||
1387 	    lc->ifp->if_type == IFT_INFINIBANDLAG) {
1388 		struct lagg_port *lp = lc->ifp->if_lagg;
1389 		struct lagg_softc *sc = lp->lp_softc;
1390 
1391 		lagg_ifp = sc->sc_ifp;
1392 		if (lagg_ifp != NULL)
1393 			lagg_bpf_req = bpf_peers_present(lagg_ifp->if_bpf);
1394 	}
1395 
1396 	/* Strip and compress all the incoming packets. */
1397  	can_append_old_cmp = true;
1398 	cmp = NULL;
1399 	for (pp = &le->m_head; *pp != NULL; ) {
1400 		mv_to = NULL;
1401 		if (do_bpf_strip_and_compress(tp, lc, le, pp,
1402 			&cmp, &mv_to, &should_wake, bpf_req,
1403  			lagg_bpf_req, lagg_ifp, can_append_old_cmp) == false) {
1404 			/* Advance to next mbuf. */
1405 			pp = &(*pp)->m_nextpkt;
1406  			/*
1407  			 * Once we have appended we can't look in the pending
1408  			 * inbound packets for a compressed ack to append to.
1409  			 */
1410  			can_append_old_cmp = false;
1411  			/*
1412  			 * Once we append we also need to stop adding to any
1413  			 * compressed ack we were remembering. A new cmp
1414  			 * ack will be required.
1415  			 */
1416  			cmp = NULL;
1417  			tcp_lro_log(tp, lc, le, NULL, 25, 0, 0, 0, 0);
1418 		} else if (mv_to != NULL) {
1419 			/* We are asked to move pp up */
1420 			pp = &mv_to->m_nextpkt;
1421  			tcp_lro_log(tp, lc, le, NULL, 24, 0, 0, 0, 0);
1422 		} else
1423  			tcp_lro_log(tp, lc, le, NULL, 26, 0, 0, 0, 0);
1424 	}
1425 	/* Update "m_last_mbuf", if any. */
1426 	if (pp == &le->m_head)
1427 		le->m_last_mbuf = *pp;
1428 	else
1429 		le->m_last_mbuf = __containerof(pp, struct mbuf, m_nextpkt);
1430 
1431 	/* Check if any data mbufs left. */
1432 	if (le->m_head != NULL) {
1433 		counter_u64_add(tcp_inp_lro_direct_queue, 1);
1434 		tcp_lro_log(tp, lc, le, NULL, 22, 1, tp->t_flags2, 0, 1);
1435 		tcp_queue_pkts(tp, le);
1436 	}
1437 	if (should_wake) {
1438 		/* Wakeup */
1439 		counter_u64_add(tcp_inp_lro_wokeup_queue, 1);
1440 		if ((*tp->t_fb->tfb_do_queued_segments)(tp, 0))
1441 			/* TCP cb gone and unlocked. */
1442 			return (0);
1443 	}
1444 	INP_WUNLOCK(tptoinpcb(tp));
1445 
1446 	return (0);	/* Success. */
1447 }
1448 #endif
1449 
1450 void
1451 tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
1452 {
1453 	/* Only optimise if there are multiple packets waiting. */
1454 #ifdef TCPHPTS
1455 	int error;
1456 #endif
1457 
1458 	NET_EPOCH_ASSERT();
1459 #ifdef TCPHPTS
1460 	CURVNET_SET(lc->ifp->if_vnet);
1461 	error = tcp_lro_flush_tcphpts(lc, le);
1462 	CURVNET_RESTORE();
1463 	if (error != 0) {
1464 #endif
1465 		tcp_lro_condense(lc, le);
1466 		tcp_flush_out_entry(lc, le);
1467 #ifdef TCPHPTS
1468 	}
1469 #endif
1470 	lc->lro_flushed++;
1471 	bzero(le, sizeof(*le));
1472 	LIST_INSERT_HEAD(&lc->lro_free, le, next);
1473 }
1474 
1475 #ifdef HAVE_INLINE_FLSLL
1476 #define	tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1))
1477 #else
1478 static inline uint64_t
1479 tcp_lro_msb_64(uint64_t x)
1480 {
1481 	x |= (x >> 1);
1482 	x |= (x >> 2);
1483 	x |= (x >> 4);
1484 	x |= (x >> 8);
1485 	x |= (x >> 16);
1486 	x |= (x >> 32);
1487 	return (x & ~(x >> 1));
1488 }
1489 #endif
1490 
1491 /*
1492  * The tcp_lro_sort() routine is comparable to qsort(), except it has
1493  * a worst case complexity limit of O(MIN(N,64)*N), where N is the
1494  * number of elements to sort and 64 is the number of sequence bits
1495  * available. The algorithm is bit-slicing the 64-bit sequence number,
1496  * sorting one bit at a time from the most significant bit until the
1497  * least significant one, skipping the constant bits. This is
1498  * typically called a radix sort.
1499  */
1500 static void
1501 tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size)
1502 {
1503 	struct lro_mbuf_sort temp;
1504 	uint64_t ones;
1505 	uint64_t zeros;
1506 	uint32_t x;
1507 	uint32_t y;
1508 
1509 repeat:
1510 	/* for small arrays insertion sort is faster */
1511 	if (size <= 12) {
1512 		for (x = 1; x < size; x++) {
1513 			temp = parray[x];
1514 			for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--)
1515 				parray[y] = parray[y - 1];
1516 			parray[y] = temp;
1517 		}
1518 		return;
1519 	}
1520 
1521 	/* compute sequence bits which are constant */
1522 	ones = 0;
1523 	zeros = 0;
1524 	for (x = 0; x != size; x++) {
1525 		ones |= parray[x].seq;
1526 		zeros |= ~parray[x].seq;
1527 	}
1528 
1529 	/* compute bits which are not constant into "ones" */
1530 	ones &= zeros;
1531 	if (ones == 0)
1532 		return;
1533 
1534 	/* pick the most significant bit which is not constant */
1535 	ones = tcp_lro_msb_64(ones);
1536 
1537 	/*
1538 	 * Move entries having cleared sequence bits to the beginning
1539 	 * of the array:
1540 	 */
1541 	for (x = y = 0; y != size; y++) {
1542 		/* skip set bits */
1543 		if (parray[y].seq & ones)
1544 			continue;
1545 		/* swap entries */
1546 		temp = parray[x];
1547 		parray[x] = parray[y];
1548 		parray[y] = temp;
1549 		x++;
1550 	}
1551 
1552 	KASSERT(x != 0 && x != size, ("Memory is corrupted\n"));
1553 
1554 	/* sort zeros */
1555 	tcp_lro_sort(parray, x);
1556 
1557 	/* sort ones */
1558 	parray += x;
1559 	size -= x;
1560 	goto repeat;
1561 }
1562 
1563 void
1564 tcp_lro_flush_all(struct lro_ctrl *lc)
1565 {
1566 	uint64_t seq;
1567 	uint64_t nseq;
1568 	unsigned x;
1569 
1570 	NET_EPOCH_ASSERT();
1571 	/* check if no mbufs to flush */
1572 	if (lc->lro_mbuf_count == 0)
1573 		goto done;
1574 	if (lc->lro_cpu_is_set == 0) {
1575 		if (lc->lro_last_cpu == curcpu) {
1576 			lc->lro_cnt_of_same_cpu++;
1577 			/* Have we reached the threshold to declare a cpu? */
1578 			if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh)
1579 				lc->lro_cpu_is_set = 1;
1580 		} else {
1581 			lc->lro_last_cpu = curcpu;
1582 			lc->lro_cnt_of_same_cpu = 0;
1583 		}
1584 	}
1585 	CURVNET_SET(lc->ifp->if_vnet);
1586 
1587 	/* get current time */
1588 	binuptime(&lc->lro_last_queue_time);
1589 
1590 	/* sort all mbufs according to stream */
1591 	tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count);
1592 
1593 	/* input data into LRO engine, stream by stream */
1594 	seq = 0;
1595 	for (x = 0; x != lc->lro_mbuf_count; x++) {
1596 		struct mbuf *mb;
1597 
1598 		/* get mbuf */
1599 		mb = lc->lro_mbuf_data[x].mb;
1600 
1601 		/* get sequence number, masking away the packet index */
1602 		nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24);
1603 
1604 		/* check for new stream */
1605 		if (seq != nseq) {
1606 			seq = nseq;
1607 
1608 			/* flush active streams */
1609 			tcp_lro_rx_done(lc);
1610 		}
1611 
1612 		/* add packet to LRO engine */
1613 		if (tcp_lro_rx_common(lc, mb, 0, false) != 0) {
1614  			/* Flush anything we have acummulated */
1615  			tcp_lro_flush_active(lc);
1616 			/* input packet to network layer */
1617 			(*lc->ifp->if_input)(lc->ifp, mb);
1618 			lc->lro_queued++;
1619 			lc->lro_flushed++;
1620 		}
1621 	}
1622 	CURVNET_RESTORE();
1623 done:
1624 	/* flush active streams */
1625 	tcp_lro_rx_done(lc);
1626 
1627 #ifdef TCPHPTS
1628 	tcp_run_hpts();
1629 #endif
1630 	lc->lro_mbuf_count = 0;
1631 }
1632 
1633 #ifdef TCPHPTS
1634 static void
1635 build_ack_entry(struct tcp_ackent *ae, struct tcphdr *th, struct mbuf *m,
1636     uint32_t *ts_ptr, uint16_t iptos)
1637 {
1638 	/*
1639 	 * Given a TCP ACK, summarize it down into the small TCP ACK
1640 	 * entry.
1641 	 */
1642 	ae->timestamp = m->m_pkthdr.rcv_tstmp;
1643 	ae->flags = 0;
1644 	if (m->m_flags & M_TSTMP_LRO)
1645 		ae->flags |= TSTMP_LRO;
1646 	else if (m->m_flags & M_TSTMP)
1647 		ae->flags |= TSTMP_HDWR;
1648 	ae->seq = ntohl(th->th_seq);
1649 	ae->ack = ntohl(th->th_ack);
1650 	ae->flags |= tcp_get_flags(th);
1651 	if (ts_ptr != NULL) {
1652 		ae->ts_value = ntohl(ts_ptr[1]);
1653 		ae->ts_echo = ntohl(ts_ptr[2]);
1654 		ae->flags |= HAS_TSTMP;
1655 	}
1656 	ae->win = ntohs(th->th_win);
1657 	ae->codepoint = iptos;
1658 }
1659 
1660 /*
1661  * Do BPF tap for either ACK_CMP packets or MBUF QUEUE type packets
1662  * and strip all, but the IPv4/IPv6 header.
1663  */
1664 static bool
1665 do_bpf_strip_and_compress(struct tcpcb *tp, struct lro_ctrl *lc,
1666     struct lro_entry *le, struct mbuf **pp, struct mbuf **cmp, struct mbuf **mv_to,
1667     bool *should_wake, bool bpf_req, bool lagg_bpf_req, struct ifnet *lagg_ifp, bool can_append_old_cmp)
1668 {
1669 	union {
1670 		void *ptr;
1671 		struct ip *ip4;
1672 		struct ip6_hdr *ip6;
1673 	} l3;
1674 	struct mbuf *m;
1675 	struct mbuf *nm;
1676 	struct tcphdr *th;
1677 	struct tcp_ackent *ack_ent;
1678 	uint32_t *ts_ptr;
1679 	int32_t n_mbuf;
1680 	bool other_opts, can_compress;
1681 	uint8_t lro_type;
1682 	uint16_t iptos;
1683 	int tcp_hdr_offset;
1684 	int idx;
1685 
1686 	/* Get current mbuf. */
1687 	m = *pp;
1688 
1689 	/* Let the BPF see the packet */
1690 	if (__predict_false(bpf_req))
1691 		ETHER_BPF_MTAP(lc->ifp, m);
1692 
1693 	if (__predict_false(lagg_bpf_req))
1694 		ETHER_BPF_MTAP(lagg_ifp, m);
1695 
1696 	tcp_hdr_offset = m->m_pkthdr.lro_tcp_h_off;
1697 	lro_type = le->inner.data.lro_type;
1698 	switch (lro_type) {
1699 	case LRO_TYPE_NONE:
1700 		lro_type = le->outer.data.lro_type;
1701 		switch (lro_type) {
1702 		case LRO_TYPE_IPV4_TCP:
1703 			tcp_hdr_offset -= sizeof(*le->outer.ip4);
1704 			m->m_pkthdr.lro_etype = ETHERTYPE_IP;
1705 			break;
1706 		case LRO_TYPE_IPV6_TCP:
1707 			tcp_hdr_offset -= sizeof(*le->outer.ip6);
1708 			m->m_pkthdr.lro_etype = ETHERTYPE_IPV6;
1709 			break;
1710 		default:
1711 			goto compressed;
1712 		}
1713 		break;
1714 	case LRO_TYPE_IPV4_TCP:
1715 		tcp_hdr_offset -= sizeof(*le->outer.ip4);
1716 		m->m_pkthdr.lro_etype = ETHERTYPE_IP;
1717 		break;
1718 	case LRO_TYPE_IPV6_TCP:
1719 		tcp_hdr_offset -= sizeof(*le->outer.ip6);
1720 		m->m_pkthdr.lro_etype = ETHERTYPE_IPV6;
1721 		break;
1722 	default:
1723 		goto compressed;
1724 	}
1725 
1726 	MPASS(tcp_hdr_offset >= 0);
1727 
1728 	m_adj(m, tcp_hdr_offset);
1729 	m->m_flags |= M_LRO_EHDRSTRP;
1730 	m->m_flags &= ~M_ACKCMP;
1731 	m->m_pkthdr.lro_tcp_h_off -= tcp_hdr_offset;
1732 
1733 	th = tcp_lro_get_th(m);
1734 
1735 	th->th_sum = 0;		/* TCP checksum is valid. */
1736 
1737 	/* Check if ACK can be compressed */
1738 	can_compress = tcp_lro_ack_valid(m, th, &ts_ptr, &other_opts);
1739 
1740 	/* Now lets look at the should wake states */
1741 	if ((other_opts == true) &&
1742 	    ((tp->t_flags2 & TF2_DONT_SACK_QUEUE) == 0)) {
1743 		/*
1744 		 * If there are other options (SACK?) and the
1745 		 * tcp endpoint has not expressly told us it does
1746 		 * not care about SACKS, then we should wake up.
1747 		 */
1748 		*should_wake = true;
1749 	} else if (*should_wake == false) {
1750 		/* Wakeup override check if we are false here  */
1751 		*should_wake = tcp_lro_check_wake_status(tp);
1752 	}
1753 	/* Is the ack compressable? */
1754 	if (can_compress == false)
1755 		goto done;
1756 	/* Does the TCP endpoint support ACK compression? */
1757 	if ((tp->t_flags2 & TF2_MBUF_ACKCMP) == 0)
1758 		goto done;
1759 
1760 	/* Lets get the TOS/traffic class field */
1761 	l3.ptr = mtod(m, void *);
1762 	switch (lro_type) {
1763 	case LRO_TYPE_IPV4_TCP:
1764 		iptos = l3.ip4->ip_tos;
1765 		break;
1766 	case LRO_TYPE_IPV6_TCP:
1767 		iptos = IPV6_TRAFFIC_CLASS(l3.ip6);
1768 		break;
1769 	default:
1770 		iptos = 0;	/* Keep compiler happy. */
1771 		break;
1772 	}
1773 	/* Now lets get space if we don't have some already */
1774 	if (*cmp == NULL) {
1775 new_one:
1776 		nm = tcp_lro_get_last_if_ackcmp(lc, le, tp, &n_mbuf,
1777 		    can_append_old_cmp);
1778 		if (__predict_false(nm == NULL))
1779 			goto done;
1780 		*cmp = nm;
1781 		if (n_mbuf) {
1782 			/*
1783 			 *  Link in the new cmp ack to our in-order place,
1784 			 * first set our cmp ack's next to where we are.
1785 			 */
1786 			nm->m_nextpkt = m;
1787 			(*pp) = nm;
1788 			/*
1789 			 * Set it up so mv_to is advanced to our
1790 			 * compressed ack. This way the caller can
1791 			 * advance pp to the right place.
1792 			 */
1793 			*mv_to = nm;
1794 			/*
1795 			 * Advance it here locally as well.
1796 			 */
1797 			pp = &nm->m_nextpkt;
1798 		}
1799 	} else {
1800 		/* We have one already we are working on */
1801 		nm = *cmp;
1802 		if (M_TRAILINGSPACE(nm) < sizeof(struct tcp_ackent)) {
1803 			/* We ran out of space */
1804 			tp->t_flags2 |= TF2_MBUF_L_ACKS;
1805 			goto new_one;
1806 		}
1807 	}
1808 	MPASS(M_TRAILINGSPACE(nm) >= sizeof(struct tcp_ackent));
1809 	counter_u64_add(tcp_inp_lro_compressed, 1);
1810 	le->compressed++;
1811 	/* We can add in to the one on the tail */
1812 	ack_ent = mtod(nm, struct tcp_ackent *);
1813 	idx = (nm->m_len / sizeof(struct tcp_ackent));
1814 	build_ack_entry(&ack_ent[idx], th, m, ts_ptr, iptos);
1815 
1816 	/* Bump the size of both pkt-hdr and len */
1817 	nm->m_len += sizeof(struct tcp_ackent);
1818 	nm->m_pkthdr.len += sizeof(struct tcp_ackent);
1819 compressed:
1820 	/* Advance to next mbuf before freeing. */
1821 	*pp = m->m_nextpkt;
1822 	m->m_nextpkt = NULL;
1823 	m_freem(m);
1824 	return (true);
1825 done:
1826 	counter_u64_add(tcp_uncomp_total, 1);
1827 	le->uncompressed++;
1828 	return (false);
1829 }
1830 #endif
1831 
1832 static struct lro_head *
1833 tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser)
1834 {
1835 	u_long hash;
1836 
1837 	if (M_HASHTYPE_ISHASH(m)) {
1838 		hash = m->m_pkthdr.flowid;
1839 	} else {
1840 		for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++)
1841 			hash += parser->data.raw[i];
1842 	}
1843 	return (&lc->lro_hash[hash % lc->lro_hashsz]);
1844 }
1845 
1846 static int
1847 tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash)
1848 {
1849 	struct lro_parser pi;	/* inner address data */
1850 	struct lro_parser po;	/* outer address data */
1851 	struct lro_parser *pa;	/* current parser for TCP stream */
1852 	struct lro_entry *le;
1853 	struct lro_head *bucket;
1854 	struct tcphdr *th;
1855 	int tcp_data_len;
1856 	int tcp_opt_len;
1857 	int error;
1858 	uint16_t tcp_data_sum;
1859 
1860 #ifdef INET
1861 	/* Quickly decide if packet cannot be LRO'ed */
1862 	if (__predict_false(V_ipforwarding != 0))
1863 		return (TCP_LRO_CANNOT);
1864 #endif
1865 #ifdef INET6
1866 	/* Quickly decide if packet cannot be LRO'ed */
1867 	if (__predict_false(V_ip6_forwarding != 0))
1868 		return (TCP_LRO_CANNOT);
1869 #endif
1870 	if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
1871 	     ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
1872 	    (m->m_pkthdr.csum_data != 0xffff)) {
1873 		/*
1874 		 * The checksum either did not have hardware offload
1875 		 * or it was a bad checksum. We can't LRO such
1876 		 * a packet.
1877 		 */
1878 		counter_u64_add(tcp_bad_csums, 1);
1879 		return (TCP_LRO_CANNOT);
1880 	}
1881 	/* We expect a contiguous header [eh, ip, tcp]. */
1882 	pa = tcp_lro_parser(m, &po, &pi, true);
1883 	if (__predict_false(pa == NULL))
1884 		return (TCP_LRO_NOT_SUPPORTED);
1885 
1886 	/* We don't expect any padding. */
1887 	error = tcp_lro_trim_mbuf_chain(m, pa);
1888 	if (__predict_false(error != 0))
1889 		return (error);
1890 
1891 #ifdef INET
1892 	switch (pa->data.lro_type) {
1893 	case LRO_TYPE_IPV4_TCP:
1894 		error = tcp_lro_rx_ipv4(lc, m, pa->ip4);
1895 		if (__predict_false(error != 0))
1896 			return (error);
1897 		break;
1898 	default:
1899 		break;
1900 	}
1901 #endif
1902 	/* If no hardware or arrival stamp on the packet add timestamp */
1903 	if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) {
1904 		m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time);
1905 		m->m_flags |= M_TSTMP_LRO;
1906 	}
1907 
1908 	/* Get pointer to TCP header. */
1909 	th = pa->tcp;
1910 
1911 	/* Don't process SYN packets. */
1912 	if (__predict_false(tcp_get_flags(th) & TH_SYN))
1913 		return (TCP_LRO_CANNOT);
1914 
1915 	/* Get total TCP header length and compute payload length. */
1916 	tcp_opt_len = (th->th_off << 2);
1917 	tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th -
1918 	    (uint8_t *)m->m_data) - tcp_opt_len;
1919 	tcp_opt_len -= sizeof(*th);
1920 
1921 	/* Don't process invalid TCP headers. */
1922 	if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0))
1923 		return (TCP_LRO_CANNOT);
1924 
1925 	/* Compute TCP data only checksum. */
1926 	if (tcp_data_len == 0)
1927 		tcp_data_sum = 0;	/* no data, no checksum */
1928 	else if (__predict_false(csum != 0))
1929 		tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum);
1930 	else
1931 		tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum);
1932 
1933 	/* Save TCP info in mbuf. */
1934 	m->m_nextpkt = NULL;
1935 	m->m_pkthdr.rcvif = lc->ifp;
1936 	m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum;
1937 	m->m_pkthdr.lro_tcp_d_len = tcp_data_len;
1938 	m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data);
1939 	m->m_pkthdr.lro_nsegs = 1;
1940 
1941 	/* Get hash bucket. */
1942 	if (!use_hash) {
1943 		bucket = &lc->lro_hash[0];
1944 	} else {
1945 		bucket = tcp_lro_rx_get_bucket(lc, m, pa);
1946 	}
1947 
1948 	/* Try to find a matching previous segment. */
1949 	LIST_FOREACH(le, bucket, hash_next) {
1950 		/* Compare addresses and ports. */
1951 		if (lro_address_compare(&po.data, &le->outer.data) == false ||
1952 		    lro_address_compare(&pi.data, &le->inner.data) == false)
1953 			continue;
1954 
1955 		/* Check if no data and old ACK. */
1956 		if (tcp_data_len == 0 &&
1957 		    SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
1958 			m_freem(m);
1959 			return (0);
1960 		}
1961 
1962 		/* Mark "m" in the last spot. */
1963 		le->m_last_mbuf->m_nextpkt = m;
1964 		/* Now set the tail to "m". */
1965 		le->m_last_mbuf = m;
1966 		return (0);
1967 	}
1968 
1969 	/* Try to find an empty slot. */
1970 	if (LIST_EMPTY(&lc->lro_free))
1971 		return (TCP_LRO_NO_ENTRIES);
1972 
1973 	/* Start a new segment chain. */
1974 	le = LIST_FIRST(&lc->lro_free);
1975 	LIST_REMOVE(le, next);
1976 	tcp_lro_active_insert(lc, bucket, le);
1977 
1978 	/* Make sure the headers are set. */
1979 	le->inner = pi;
1980 	le->outer = po;
1981 
1982 	/* Store time this entry was allocated. */
1983 	le->alloc_time = lc->lro_last_queue_time;
1984 
1985 	tcp_set_entry_to_mbuf(lc, le, m, th);
1986 
1987 	/* Now set the tail to "m". */
1988 	le->m_last_mbuf = m;
1989 
1990 	return (0);
1991 }
1992 
1993 int
1994 tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
1995 {
1996 	int error;
1997 
1998 	if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) !=
1999 	     ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) ||
2000 	    (m->m_pkthdr.csum_data != 0xffff)) {
2001 		/*
2002 		 * The checksum either did not have hardware offload
2003 		 * or it was a bad checksum. We can't LRO such
2004 		 * a packet.
2005 		 */
2006 		counter_u64_add(tcp_bad_csums, 1);
2007 		return (TCP_LRO_CANNOT);
2008 	}
2009 	/* get current time */
2010 	binuptime(&lc->lro_last_queue_time);
2011 	CURVNET_SET(lc->ifp->if_vnet);
2012 	error = tcp_lro_rx_common(lc, m, csum, true);
2013 	if (__predict_false(error != 0)) {
2014 		/*
2015 		 * Flush anything we have acummulated
2016 		 * ahead of this packet that can't
2017 		 * be LRO'd. This preserves order.
2018 		 */
2019 		tcp_lro_flush_active(lc);
2020 	}
2021 	CURVNET_RESTORE();
2022 
2023 	return (error);
2024 }
2025 
2026 void
2027 tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
2028 {
2029 	NET_EPOCH_ASSERT();
2030 	/* sanity checks */
2031 	if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
2032 	    lc->lro_mbuf_max == 0)) {
2033 		/* packet drop */
2034 		m_freem(mb);
2035 		return;
2036 	}
2037 
2038 	/* check if packet is not LRO capable */
2039 	if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
2040 		/* input packet to network layer */
2041 		(*lc->ifp->if_input) (lc->ifp, mb);
2042 		return;
2043 	}
2044 
2045  	/* If no hardware or arrival stamp on the packet add timestamp */
2046  	if ((tcplro_stacks_wanting_mbufq > 0) &&
2047  	    (tcp_less_accurate_lro_ts == 0) &&
2048  	    ((mb->m_flags & M_TSTMP) == 0)) {
2049  		/* Add in an LRO time since no hardware */
2050  		binuptime(&lc->lro_last_queue_time);
2051  		mb->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time);
2052  		mb->m_flags |= M_TSTMP_LRO;
2053  	}
2054 
2055 	/* create sequence number */
2056 	lc->lro_mbuf_data[lc->lro_mbuf_count].seq =
2057 	    (((uint64_t)M_HASHTYPE_GET(mb)) << 56) |
2058 	    (((uint64_t)mb->m_pkthdr.flowid) << 24) |
2059 	    ((uint64_t)lc->lro_mbuf_count);
2060 
2061 	/* enter mbuf */
2062 	lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb;
2063 
2064 	/* flush if array is full */
2065 	if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max))
2066 		tcp_lro_flush_all(lc);
2067 }
2068 
2069 /* end */
2070