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