2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007, Myricom Inc.
5 * Copyright (c) 2008, Intel Corporation.
6 * Copyright (c) 2012 The FreeBSD Foundation
7 * Copyright (c) 2016 Mellanox Technologies.
10 * Portions of this software were developed by Bjoern Zeeb
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sockbuf.h>
49 #include <sys/sysctl.h>
52 #include <net/if_var.h>
53 #include <net/ethernet.h>
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 <netinet6/ip6_var.h>
73 #include <machine/in_cksum.h>
75 static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
77 #define TCP_LRO_UPDATE_CSUM 1
78 #ifndef TCP_LRO_UPDATE_CSUM
79 #define TCP_LRO_INVALID_CSUM 0x0000
82 static void tcp_lro_rx_done(struct lro_ctrl *lc);
83 static int tcp_lro_rx2(struct lro_ctrl *lc, struct mbuf *m,
84 uint32_t csum, int use_hash);
86 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
89 static long tcplro_stacks_wanting_mbufq = 0;
90 counter_u64_t tcp_inp_lro_direct_queue;
91 counter_u64_t tcp_inp_lro_wokeup_queue;
92 counter_u64_t tcp_inp_lro_compressed;
93 counter_u64_t tcp_inp_lro_single_push;
94 counter_u64_t tcp_inp_lro_locks_taken;
95 counter_u64_t tcp_inp_lro_sack_wake;
96 counter_u64_t tcp_extra_mbuf;
97 counter_u64_t tcp_would_have_but;
98 counter_u64_t tcp_comp_total;
99 counter_u64_t tcp_uncomp_total;
100 counter_u64_t tcp_csum_hardware;
101 counter_u64_t tcp_csum_hardware_w_ph;
102 counter_u64_t tcp_csum_software;
105 static unsigned tcp_lro_entries = TCP_LRO_ENTRIES;
106 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
107 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
108 "default number of LRO entries");
110 SYSCTL_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");
112 SYSCTL_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");
114 SYSCTL_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");
116 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, single, CTLFLAG_RD,
117 &tcp_inp_lro_single_push, "Number of lro's sent with single segment");
118 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
119 &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
120 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, sackwakeups, CTLFLAG_RD,
121 &tcp_inp_lro_sack_wake, "Number of wakeups caused by sack/fin");
122 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD,
123 &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp");
124 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD,
125 &tcp_would_have_but, "Number of times we would have had an extra compressed but out of room");
126 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD,
127 &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set");
128 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD,
129 &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP");
130 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, csum_hw, CTLFLAG_RD,
131 &tcp_csum_hardware, "Number of checksums processed in hardware");
132 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, csum_hw_ph, CTLFLAG_RD,
133 &tcp_csum_hardware_w_ph, "Number of checksums processed in hardware with pseudo header");
134 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, csum_sw, CTLFLAG_RD,
135 &tcp_csum_software, "Number of checksums processed in software");
140 tcp_lro_reg_mbufq(void)
142 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1);
146 tcp_lro_dereg_mbufq(void)
148 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1);
152 tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket,
153 struct lro_entry *le)
156 LIST_INSERT_HEAD(&lc->lro_active, le, next);
157 LIST_INSERT_HEAD(bucket, le, hash_next);
161 tcp_lro_active_remove(struct lro_entry *le)
164 LIST_REMOVE(le, next); /* active list */
165 LIST_REMOVE(le, hash_next); /* hash bucket */
169 tcp_lro_init(struct lro_ctrl *lc)
171 return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0));
175 tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
176 unsigned lro_entries, unsigned lro_mbufs)
178 struct lro_entry *le;
180 unsigned i, elements;
182 lc->lro_bad_csum = 0;
185 lc->lro_mbuf_count = 0;
186 lc->lro_mbuf_max = lro_mbufs;
187 lc->lro_cnt = lro_entries;
188 lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX;
189 lc->lro_length_lim = TCP_LRO_LENGTH_MAX;
191 LIST_INIT(&lc->lro_free);
192 LIST_INIT(&lc->lro_active);
194 /* create hash table to accelerate entry lookup */
195 if (lro_entries > lro_mbufs)
196 elements = lro_entries;
198 elements = lro_mbufs;
199 lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz,
201 if (lc->lro_hash == NULL) {
202 memset(lc, 0, sizeof(*lc));
206 /* compute size to allocate */
207 size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) +
208 (lro_entries * sizeof(*le));
209 lc->lro_mbuf_data = (struct lro_mbuf_sort *)
210 malloc(size, M_LRO, M_NOWAIT | M_ZERO);
212 /* check for out of memory */
213 if (lc->lro_mbuf_data == NULL) {
214 free(lc->lro_hash, M_LRO);
215 memset(lc, 0, sizeof(*lc));
218 /* compute offset for LRO entries */
219 le = (struct lro_entry *)
220 (lc->lro_mbuf_data + lro_mbufs);
222 /* setup linked list */
223 for (i = 0; i != lro_entries; i++)
224 LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
229 static struct tcphdr *
230 tcp_lro_get_th(struct lro_entry *le, struct mbuf *m)
232 struct ether_header *eh;
233 struct tcphdr *th = NULL;
235 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
238 struct ip *ip4 = NULL; /* Keep compiler happy. */
241 eh = mtod(m, struct ether_header *);
242 switch (le->eh_type) {
245 ip6 = (struct ip6_hdr *)(eh + 1);
246 th = (struct tcphdr *)(ip6 + 1);
251 ip4 = (struct ip *)(eh + 1);
252 th = (struct tcphdr *)(ip4 + 1);
260 lro_free_mbuf_chain(struct mbuf *m)
273 tcp_lro_free(struct lro_ctrl *lc)
275 struct lro_entry *le;
278 /* reset LRO free list */
279 LIST_INIT(&lc->lro_free);
281 /* free active mbufs, if any */
282 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
283 tcp_lro_active_remove(le);
284 lro_free_mbuf_chain(le->m_head);
287 /* free hash table */
288 free(lc->lro_hash, M_LRO);
292 /* free mbuf array, if any */
293 for (x = 0; x != lc->lro_mbuf_count; x++)
294 m_freem(lc->lro_mbuf_data[x].mb);
295 lc->lro_mbuf_count = 0;
297 /* free allocated memory, if any */
298 free(lc->lro_mbuf_data, M_LRO);
299 lc->lro_mbuf_data = NULL;
303 tcp_lro_csum_th(struct tcphdr *th)
308 ch = th->th_sum = 0x0000;
319 ch = (ch >> 16) + (ch & 0xffff);
321 return (ch & 0xffff);
325 tcp_lro_rx_csum_fixup(struct lro_entry *le, void *l3hdr, struct tcphdr *th,
326 uint16_t tcp_data_len, uint16_t csum)
333 /* Remove length from checksum. */
334 switch (le->eh_type) {
340 ip6 = (struct ip6_hdr *)l3hdr;
341 if (le->append_cnt == 0)
346 cx = ntohs(ip6->ip6_plen);
347 cs = in6_cksum_pseudo(ip6, cx, ip6->ip6_nxt, 0);
357 ip4 = (struct ip *)l3hdr;
358 if (le->append_cnt == 0)
361 cs = in_addword(ntohs(ip4->ip_len) - sizeof(*ip4),
363 cs = in_pseudo(ip4->ip_src.s_addr, ip4->ip_dst.s_addr,
370 cs = 0; /* Keep compiler happy. */
376 /* Remove TCP header csum. */
377 cs = ~tcp_lro_csum_th(th);
380 c = (c >> 16) + (c & 0xffff);
386 tcp_lro_rx_done(struct lro_ctrl *lc)
388 struct lro_entry *le;
390 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
391 tcp_lro_active_remove(le);
392 tcp_lro_flush(lc, le);
397 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
399 struct lro_entry *le, *le_tmp;
402 if (LIST_EMPTY(&lc->lro_active))
406 timevalsub(&tv, timeout);
407 LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
408 if (timevalcmp(&tv, &le->mtime, >=)) {
409 tcp_lro_active_remove(le);
410 tcp_lro_flush(lc, le);
417 tcp_lro_rx_ipv6(struct lro_ctrl *lc, struct mbuf *m, struct ip6_hdr *ip6,
421 /* XXX-BZ we should check the flow-label. */
423 /* XXX-BZ We do not yet support ext. hdrs. */
424 if (ip6->ip6_nxt != IPPROTO_TCP)
425 return (TCP_LRO_NOT_SUPPORTED);
427 /* Find the TCP header. */
428 *th = (struct tcphdr *)(ip6 + 1);
436 tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4,
442 if (ip4->ip_p != IPPROTO_TCP)
443 return (TCP_LRO_NOT_SUPPORTED);
445 /* Ensure there are no options. */
446 if ((ip4->ip_hl << 2) != sizeof (*ip4))
447 return (TCP_LRO_CANNOT);
449 /* .. and the packet is not fragmented. */
450 if (ip4->ip_off & htons(IP_MF|IP_OFFMASK))
451 return (TCP_LRO_CANNOT);
453 /* Legacy IP has a header checksum that needs to be correct. */
454 csum_flags = m->m_pkthdr.csum_flags;
455 if (csum_flags & CSUM_IP_CHECKED) {
456 if (__predict_false((csum_flags & CSUM_IP_VALID) == 0)) {
458 return (TCP_LRO_CANNOT);
461 csum = in_cksum_hdr(ip4);
462 if (__predict_false((csum) != 0)) {
464 return (TCP_LRO_CANNOT);
467 /* Find the TCP header (we assured there are no IP options). */
468 *th = (struct tcphdr *)(ip4 + 1);
475 tcp_lro_log(struct tcpcb *tp, struct lro_ctrl *lc,
476 struct lro_entry *le, struct mbuf *m, int frm, int32_t tcp_data_len,
477 uint32_t th_seq , uint32_t th_ack, uint16_t th_win)
479 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
480 union tcp_log_stackspecific log;
484 cts = tcp_get_usecs(&tv);
485 memset(&log, 0, sizeof(union tcp_log_stackspecific));
486 log.u_bbr.flex8 = frm;
487 log.u_bbr.flex1 = tcp_data_len;
489 log.u_bbr.flex2 = m->m_pkthdr.len;
492 log.u_bbr.flex3 = le->append_cnt;
493 log.u_bbr.flex4 = le->p_len;
495 log.u_bbr.flex5 = le->m_head->m_pkthdr.len;
496 log.u_bbr.delRate = le->m_head->m_flags;
497 log.u_bbr.rttProp = le->m_head->m_pkthdr.rcv_tstmp;
499 log.u_bbr.inflight = th_seq;
500 log.u_bbr.timeStamp = cts;
501 log.u_bbr.epoch = le->next_seq;
502 log.u_bbr.delivered = th_ack;
503 log.u_bbr.lt_epoch = le->ack_seq;
504 log.u_bbr.pacing_gain = th_win;
505 log.u_bbr.cwnd_gain = le->window;
506 log.u_bbr.cur_del_rate = (uintptr_t)m;
507 log.u_bbr.bw_inuse = (uintptr_t)le->m_head;
508 log.u_bbr.pkts_out = le->mbuf_cnt; /* Total mbufs added */
509 log.u_bbr.applimited = le->ulp_csum;
510 log.u_bbr.lost = le->mbuf_appended;
511 log.u_bbr.pkt_epoch = le->cmp_ack_cnt;
512 log.u_bbr.flex6 = tcp_tv_to_usectick(&lc->lro_last_flush);
513 if (in_epoch(net_epoch_preempt))
514 log.u_bbr.inhpts = 1;
516 log.u_bbr.inhpts = 0;
517 TCP_LOG_EVENTP(tp, NULL,
518 &tp->t_inpcb->inp_socket->so_rcv,
519 &tp->t_inpcb->inp_socket->so_snd,
521 0, &log, false, &tv);
527 tcp_flush_out_le(struct tcpcb *tp, struct lro_ctrl *lc, struct lro_entry *le)
529 if (le->append_cnt > 1) {
533 p_len = htons(le->p_len);
534 switch (le->eh_type) {
541 ip6->ip6_plen = p_len;
542 th = (struct tcphdr *)(ip6 + 1);
543 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
545 le->p_len += ETHER_HDR_LEN + sizeof(*ip6);
557 /* Fix IP header checksum for new length. */
563 cl = (cl >> 16) + (cl & 0xffff);
567 th = (struct tcphdr *)(ip4 + 1);
568 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
569 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
570 le->p_len += ETHER_HDR_LEN;
575 th = NULL; /* Keep compiler happy. */
577 le->m_head->m_pkthdr.csum_data = 0xffff;
578 le->m_head->m_pkthdr.len = le->p_len;
580 /* Incorporate the latest ACK into the TCP header. */
581 th->th_ack = le->ack_seq;
582 th->th_win = le->window;
583 /* Incorporate latest timestamp into the TCP header. */
584 if (le->timestamp != 0) {
587 ts_ptr = (uint32_t *)(th + 1);
588 ts_ptr[1] = htonl(le->tsval);
589 ts_ptr[2] = le->tsecr;
591 /* Update the TCP header checksum. */
592 le->ulp_csum += p_len;
593 le->ulp_csum += tcp_lro_csum_th(th);
594 while (le->ulp_csum > 0xffff)
595 le->ulp_csum = (le->ulp_csum >> 16) +
596 (le->ulp_csum & 0xffff);
597 th->th_sum = (le->ulp_csum & 0xffff);
598 th->th_sum = ~th->th_sum;
601 * Break any chain, this is not set to NULL on the singleton
602 * case m_nextpkt points to m_head. Other case set them
603 * m_nextpkt to NULL in push_and_replace.
605 le->m_head->m_nextpkt = NULL;
606 le->m_head->m_pkthdr.lro_nsegs = le->append_cnt;
607 (*lc->ifp->if_input)(lc->ifp, le->m_head);
608 lc->lro_queued += le->append_cnt;
612 tcp_set_le_to_m(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
614 struct ether_header *eh;
615 void *l3hdr = NULL; /* Keep compiler happy. */
618 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
621 struct ip *ip4 = NULL; /* Keep compiler happy. */
624 int error, l, ts_failed = 0;
625 uint16_t tcp_data_len;
629 eh = mtod(m, struct ether_header *);
631 * We must reset the other pointers since the mbuf
632 * we were pointing too is about to go away.
634 switch (le->eh_type) {
637 l3hdr = ip6 = (struct ip6_hdr *)(eh + 1);
638 error = tcp_lro_rx_ipv6(lc, m, ip6, &th);
640 le->source_ip6 = ip6->ip6_src;
641 le->dest_ip6 = ip6->ip6_dst;
642 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN - sizeof(*ip6);
647 l3hdr = ip4 = (struct ip *)(eh + 1);
648 error = tcp_lro_rx_ipv4(lc, m, ip4, &th);
650 le->source_ip4 = ip4->ip_src.s_addr;
651 le->dest_ip4 = ip4->ip_dst.s_addr;
652 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN;
656 KASSERT(error == 0, ("%s: le=%p tcp_lro_rx_xxx failed\n",
658 ts_ptr = (uint32_t *)(th + 1);
659 l = (th->th_off << 2);
662 (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
663 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
664 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
665 /* We have failed to find a timestamp some other option? */
668 if ((l != 0) && (ts_failed == 0)) {
670 le->tsval = ntohl(*(ts_ptr + 1));
671 le->tsecr = *(ts_ptr + 2);
674 le->source_port = th->th_sport;
675 le->dest_port = th->th_dport;
676 /* Pull out the csum */
677 tcp_data_len = m->m_pkthdr.lro_len;
678 le->next_seq = ntohl(th->th_seq) + tcp_data_len;
679 le->ack_seq = th->th_ack;
680 le->window = th->th_win;
682 /* Setup the data pointers */
684 le->m_tail = m_last(m);
686 le->ulp_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th, tcp_data_len,
689 th->th_sum = csum; /* Restore checksum on first packet. */
693 tcp_push_and_replace(struct tcpcb *tp, struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
696 * Push up the stack the current le and replace
701 /* Grab off the next and save it */
702 msave = le->m_head->m_nextpkt;
703 le->m_head->m_nextpkt = NULL;
704 /* Now push out the old le entry */
705 tcp_flush_out_le(tp, lc, le);
707 * Now to replace the data properly in the le
708 * we have to reset the tcp header and
711 tcp_set_le_to_m(lc, le, m);
712 /* Restore the next list */
713 m->m_nextpkt = msave;
717 tcp_lro_condense(struct tcpcb *tp, struct lro_ctrl *lc, struct lro_entry *le)
720 * Walk through the mbuf chain we
721 * have on tap and compress/condense
727 uint16_t tcp_data_len, csum_upd;
731 * First we must check the lead (m_head)
732 * we must make sure that it is *not*
733 * something that should be sent up
734 * right away (sack etc).
738 m = le->m_head->m_nextpkt;
740 /* Just the one left */
743 if (m->m_flags & M_ACKCMP)
744 panic("LRO condense lc:%p le:%p reaches with mbuf:%p ackcmp",
746 th = tcp_lro_get_th(le, le->m_head);
748 ("le:%p m:%p th comes back NULL?", le, le->m_head));
749 l = (th->th_off << 2);
751 ts_ptr = (uint32_t *)(th + 1);
752 if (l != 0 && (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
753 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
754 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
756 * Its not the timestamp. We can't
757 * use this guy as the head.
759 le->m_head->m_nextpkt = m->m_nextpkt;
760 tcp_push_and_replace(tp, lc, le, m);
763 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) {
765 * Make sure that previously seen segements/ACKs are delivered
766 * before this segment, e.g. FIN.
768 le->m_head->m_nextpkt = m->m_nextpkt;
769 KASSERT(((m->m_flags & M_LRO_EHDRSTRP) == 0) ,
770 ("tp:%p mbuf:%p has stripped ethernet flags:0x%x", tp, m, m->m_flags));
771 tcp_push_and_replace(tp, lc, le, m);
774 while((m = le->m_head->m_nextpkt) != NULL) {
776 * condense m into le, first
777 * pull m out of the list.
779 KASSERT(((m->m_flags & M_LRO_EHDRSTRP) == 0) ,
780 ("tp:%p mbuf:%p has stripped ethernet flags:0x%x", tp, m, m->m_flags));
781 KASSERT(((m->m_flags & M_ACKCMP) == 0),
782 ("LRO condense lc:%p le:%p reaches with mbuf:%p ackcmp", lc, le, m));
783 le->m_head->m_nextpkt = m->m_nextpkt;
786 tcp_data_len = m->m_pkthdr.lro_len;
787 th = tcp_lro_get_th(le, m);
789 ("le:%p m:%p th comes back NULL?", le, m));
790 ts_ptr = (uint32_t *)(th + 1);
791 l = (th->th_off << 2);
793 if (le->append_cnt >= lc->lro_ackcnt_lim) {
794 tcp_push_and_replace(tp, lc, le, m);
797 if (le->p_len > (lc->lro_length_lim - tcp_data_len)) {
798 /* Flush now if appending will result in overflow. */
799 tcp_push_and_replace(tp, lc, le, m);
802 if (l != 0 && (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
803 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
804 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
806 * Maybe a sack in the new one? We need to
807 * start all over after flushing the
808 * current le. We will go up to the beginning
809 * and flush it (calling the replace again possibly
810 * or just returning).
812 tcp_push_and_replace(tp, lc, le, m);
815 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) {
816 tcp_push_and_replace(tp, lc, le, m);
820 uint32_t tsval = ntohl(*(ts_ptr + 1));
821 /* Make sure timestamp values are increasing. */
822 if (TSTMP_GT(le->tsval, tsval)) {
823 tcp_push_and_replace(tp, lc, le, m);
827 le->tsecr = *(ts_ptr + 2);
829 /* Try to append the new segment. */
830 if (__predict_false(ntohl(th->th_seq) != le->next_seq ||
831 (tcp_data_len == 0 &&
832 le->ack_seq == th->th_ack &&
833 le->window == th->th_win))) {
834 /* Out of order packet or duplicate ACK. */
835 tcp_push_and_replace(tp, lc, le, m);
838 if (tcp_data_len || SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
839 le->next_seq += tcp_data_len;
840 le->ack_seq = th->th_ack;
841 le->window = th->th_win;
842 } else if (th->th_ack == le->ack_seq) {
843 le->window = WIN_MAX(le->window, th->th_win);
845 csum_upd = m->m_pkthdr.lro_csum;
846 le->ulp_csum += csum_upd;
847 if (tcp_data_len == 0) {
855 le->p_len += tcp_data_len;
857 * Adjust the mbuf so that m_data points to the first byte of
858 * the ULP payload. Adjust the mbuf to avoid complications and
859 * append new segment to existing mbuf chain.
861 m_adj(m, m->m_pkthdr.len - tcp_data_len);
863 le->m_tail->m_next = m;
864 le->m_tail = m_last(m);
870 tcp_queue_pkts(struct tcpcb *tp, struct lro_entry *le)
872 if (tp->t_in_pkt == NULL) {
873 /* Nothing yet there */
874 tp->t_in_pkt = le->m_head;
875 tp->t_tail_pkt = le->m_last_mbuf;
877 /* Already some there */
878 tp->t_tail_pkt->m_nextpkt = le->m_head;
879 tp->t_tail_pkt = le->m_last_mbuf;
882 le->m_last_mbuf = NULL;
886 tcp_lro_get_last_if_ackcmp(struct lro_ctrl *lc, struct lro_entry *le, struct inpcb *inp)
888 struct mbuf *m = NULL;
893 /* Look at the last mbuf if any in queue */
894 if ((tp->t_tail_pkt) &&
895 (tp->t_tail_pkt->m_flags & M_ACKCMP)) {
896 if (M_TRAILINGSPACE(tp->t_tail_pkt) >= sizeof(struct tcp_ackent)) {
897 tcp_lro_log(tp, lc, le, NULL, 23, 0, 0, 0, 0);
900 if ((inp->inp_flags2 & INP_MBUF_L_ACKS) == 0) {
901 counter_u64_add(tcp_would_have_but, 1);
902 inp->inp_flags2 |= INP_MBUF_L_ACKS;
910 static struct inpcb *
911 tcp_lro_lookup(struct lro_ctrl *lc, struct lro_entry *le)
913 struct inpcb *inp = NULL;
916 switch (le->eh_type) {
919 inp = in6_pcblookup(&V_tcbinfo, &le->source_ip6,
920 le->source_port, &le->dest_ip6,le->dest_port,
927 inp = in_pcblookup(&V_tcbinfo, le->le_ip4->ip_src,
928 le->source_port, le->le_ip4->ip_dst, le->dest_port,
940 stack_guard_prep(uint32_t *sg, int len)
944 for (i = 0; i < len; i++) {
950 stack_guard_check(struct lro_ctrl *lc, struct lro_entry *le, uint32_t *sg, int len)
954 for (i = 0; i < len; i++) {
955 if (sg[i] != 0xdeadc0de)
956 panic("Stack guard fails sg[%d] = 0x%x le:%p lc:%p sg:%p\n",
957 i, sg[i], le, lc, sg);
963 tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
965 struct tcpcb *tp = NULL;
967 struct inpcb *inp = NULL;
968 int need_wakeup = 0, can_queue = 0;
970 /* Now lets lookup the inp first */
971 CURVNET_SET(lc->ifp->if_vnet);
973 * XXXRRS Currently the common input handler for
974 * mbuf queuing cannot handle VLAN Tagged. This needs
975 * to be fixed and the or condition removed (i.e. the
976 * common code should do the right lookup for the vlan
977 * tag and anything else that the vlan_input() does).
979 if (le->m_head == NULL) {
981 * Everything was pushed up to the stack nothing to do
982 * but release the reference and be done.
986 if (in_pcbrele_wlocked(le->inp) == 0) {
988 * We released it and still
991 INP_WUNLOCK(le->inp);
996 if ((tcplro_stacks_wanting_mbufq == 0) || (le->m_head->m_flags & M_VLANTAG))
999 if (le->inp == NULL) {
1000 le->inp = inp = tcp_lro_lookup(lc, le);
1001 if (inp && ((inp->inp_flags & (INP_DROPPED|INP_TIMEWAIT)) ||
1002 (inp->inp_flags2 & INP_FREED))) {
1004 * We can't present these to the inp since
1005 * it will not support the stripped ethernet
1006 * header that these have nor if a compressed
1010 lro_free_mbuf_chain(le->m_head);
1013 if ((le->flags & HAS_COMP_ENTRIES) &&
1014 ((inp->inp_flags2 & INP_MBUF_ACKCMP) == 0)) {
1016 * It swapped to off, must be a stack
1017 * switch. We need to ditch all the packets
1018 * and the peer will just have to retransmit.
1021 lro_free_mbuf_chain(le->m_head);
1025 /* We have a reference on the inp lets lock and release it */
1028 if (in_pcbrele_wlocked(inp)) {
1030 * We lost the inp. We can't present these to the inp since
1031 * it will not support the stripped off etherent header.
1033 lro_free_mbuf_chain(le->m_head);
1036 if (inp && ((inp->inp_flags & (INP_DROPPED|INP_TIMEWAIT)) ||
1037 (inp->inp_flags2 & INP_FREED))) {
1039 * We can't present these to the inp since
1040 * it may not support them.
1043 lro_free_mbuf_chain(le->m_head);
1046 if ((le->flags & HAS_COMP_ENTRIES) &&
1047 ((inp->inp_flags2 & INP_MBUF_ACKCMP) == 0)) {
1049 * It swapped to off, must be a stack
1050 * switch. We need to ditch all the packets
1051 * and the peer will just have to retransmit.
1054 lro_free_mbuf_chain(le->m_head);
1058 if (inp && ((inp->inp_flags2 & INP_SUPPORTS_MBUFQ) ||
1059 (inp->inp_flags2 & INP_MBUF_ACKCMP))) {
1060 /* The transport supports mbuf queuing */
1062 if (le->need_wakeup ||
1063 ((inp->inp_in_input == 0) &&
1064 ((inp->inp_flags2 & INP_MBUF_QUEUE_READY) == 0))) {
1066 * Either the transport is off on a keep-alive
1067 * (it has the queue_ready flag clear and its
1068 * not already been woken) or the entry has
1069 * some urgent thing (FIN or possibly SACK blocks).
1070 * This means we need to wake the transport up by
1071 * putting it on the input pacer.
1074 if ((inp->inp_flags2 & INP_DONT_SACK_QUEUE) &&
1075 (le->need_wakeup != 1)) {
1077 * Prohibited from a sack wakeup.
1082 /* Do we need to be awoken due to lots of data or acks? */
1083 if ((le->tcp_tot_p_len >= lc->lro_length_lim) ||
1084 (le->mbuf_cnt >= lc->lro_ackcnt_lim))
1088 tp = intotcpcb(inp);
1092 counter_u64_add(tcp_inp_lro_direct_queue, 1);
1093 tcp_lro_log(tp, lc, le, NULL, 22, need_wakeup,
1094 inp->inp_flags2, inp->inp_in_input, le->need_wakeup);
1095 tcp_queue_pkts(tp, le);
1098 * We must get the guy to wakeup via
1102 if (le->need_wakeup == 2) {
1104 * The value 2 is set if the
1105 * options are unrecognized i.e.
1106 * not just a timestamp. So really
1107 * sack is usually what it is but
1108 * it might be some other option (CWR
1111 counter_u64_add(tcp_inp_lro_sack_wake, 1);
1113 counter_u64_add(tcp_inp_lro_wokeup_queue, 1);
1114 if ((*tp->t_fb->tfb_do_queued_segments)(inp->inp_socket, tp, 0)) {
1122 counter_u64_add(tcp_inp_lro_locks_taken, 1);
1125 if (can_queue == 0) {
1127 if (le->strip_cnt) {
1129 * We have stripped mbufs, the connection
1130 * must have changed underneath us. You
1131 * loose the packets as a penalty.
1133 lro_free_mbuf_chain(le->m_head);
1136 #endif /* TCPHPTS */
1137 /* Old fashioned lro method */
1138 if (le->m_head != le->m_last_mbuf) {
1139 counter_u64_add(tcp_inp_lro_compressed, 1);
1140 tcp_lro_condense(tp, lc, le);
1142 counter_u64_add(tcp_inp_lro_single_push, 1);
1143 tcp_flush_out_le(tp, lc, le);
1150 bzero(le, sizeof(*le));
1151 LIST_INSERT_HEAD(&lc->lro_free, le, next);
1154 #ifdef HAVE_INLINE_FLSLL
1155 #define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1))
1157 static inline uint64_t
1158 tcp_lro_msb_64(uint64_t x)
1166 return (x & ~(x >> 1));
1171 * The tcp_lro_sort() routine is comparable to qsort(), except it has
1172 * a worst case complexity limit of O(MIN(N,64)*N), where N is the
1173 * number of elements to sort and 64 is the number of sequence bits
1174 * available. The algorithm is bit-slicing the 64-bit sequence number,
1175 * sorting one bit at a time from the most significant bit until the
1176 * least significant one, skipping the constant bits. This is
1177 * typically called a radix sort.
1180 tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size)
1182 struct lro_mbuf_sort temp;
1189 /* for small arrays insertion sort is faster */
1191 for (x = 1; x < size; x++) {
1193 for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--)
1194 parray[y] = parray[y - 1];
1200 /* compute sequence bits which are constant */
1203 for (x = 0; x != size; x++) {
1204 ones |= parray[x].seq;
1205 zeros |= ~parray[x].seq;
1208 /* compute bits which are not constant into "ones" */
1213 /* pick the most significant bit which is not constant */
1214 ones = tcp_lro_msb_64(ones);
1217 * Move entries having cleared sequence bits to the beginning
1220 for (x = y = 0; y != size; y++) {
1222 if (parray[y].seq & ones)
1226 parray[x] = parray[y];
1231 KASSERT(x != 0 && x != size, ("Memory is corrupted\n"));
1234 tcp_lro_sort(parray, x);
1243 tcp_lro_flush_all(struct lro_ctrl *lc)
1249 /* check if no mbufs to flush */
1250 if (lc->lro_mbuf_count == 0)
1253 microuptime(&lc->lro_last_flush);
1254 /* sort all mbufs according to stream */
1255 tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count);
1257 /* input data into LRO engine, stream by stream */
1259 for (x = 0; x != lc->lro_mbuf_count; x++) {
1263 mb = lc->lro_mbuf_data[x].mb;
1265 /* get sequence number, masking away the packet index */
1266 nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24);
1268 /* check for new stream */
1272 /* flush active streams */
1273 tcp_lro_rx_done(lc);
1276 /* add packet to LRO engine */
1277 if (tcp_lro_rx2(lc, mb, 0, 0) != 0) {
1278 /* input packet to network layer */
1279 (*lc->ifp->if_input)(lc->ifp, mb);
1285 /* flush active streams */
1286 tcp_lro_rx_done(lc);
1288 lc->lro_mbuf_count = 0;
1292 lro_set_mtime(struct timeval *tv, struct timespec *ts)
1294 tv->tv_sec = ts->tv_sec;
1295 tv->tv_usec = ts->tv_nsec / 1000;
1300 build_ack_entry(struct tcp_ackent *ae, struct tcphdr *th, struct mbuf *m, uint16_t hdr_len, uint16_t iptos)
1303 * Given a TCP ack, summarize it down into the small tcp
1309 KASSERT(((th->th_flags & ~(TH_ACK | TH_PUSH | TH_CWR | TH_ECE)) == 0),
1310 ("tcphdr:%p mbuf:%p has unallowed bits %x", th, m, th->th_flags));
1311 ae->timestamp = m->m_pkthdr.rcv_tstmp;
1312 if (m->m_flags & M_TSTMP_LRO)
1313 ae->flags = TSTMP_LRO;
1314 else if (m->m_flags & M_TSTMP)
1315 ae->flags = TSTMP_HDWR;
1316 ae->seq = ntohl(th->th_seq);
1317 ae->ack = ntohl(th->th_ack);
1318 ae->flags |= th->th_flags;
1320 /* We have a timestamp options get out the bits */
1321 cp = (u_char *)(th + 1);
1322 /* Skip the two NOP's at the front */
1323 while (*cp == TCPOPT_NOP)
1325 KASSERT(((*cp == TCPOPT_TIMESTAMP) &&
1326 (cp[1] == TCPOLEN_TIMESTAMP)),
1327 ("At %p in tcphdr:%p options of %d not timestamp",
1329 bcopy((char *)cp + 2,
1330 (char *)&ae->ts_value, sizeof(uint32_t));
1331 ae->ts_value = ntohl(ae->ts_value);
1332 bcopy((char *)cp + 6,
1333 (char *)&ae->ts_echo, sizeof(uint32_t));
1334 ae->ts_echo = ntohl(ae->ts_echo);
1335 ae->flags |= HAS_TSTMP;
1337 ae->win = ntohs(th->th_win);
1338 ae->codepoint = iptos;
1341 static struct mbuf *
1342 do_bpf_and_csum(struct inpcb *inp, struct lro_ctrl *lc, struct lro_entry *le,
1343 struct ether_header *eh, struct mbuf *m, int bpf_req, int locked)
1346 * Do TCP/IP checksum and BPF tap for either ACK_CMP packets or
1347 * MBUF QUEUE type packets.
1351 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
1354 struct ip *ip = NULL; /* Keep compiler happy. */
1357 uint16_t drop_hdrlen;
1363 /* Let the BPF see the packet */
1364 if (bpf_req && lc->ifp)
1365 ETHER_BPF_MTAP(lc->ifp, m);
1366 /* Get type and Trim off the ethernet header */
1367 m->m_pkthdr.lro_etype = etype = ntohs(eh->ether_type);
1368 m_adj(m, sizeof(*eh));
1369 m->m_flags |= M_LRO_EHDRSTRP;
1372 case ETHERTYPE_IPV6:
1374 if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
1375 m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
1377 TCPSTAT_INC(tcps_rcvshort);
1382 ip6 = (struct ip6_hdr *)(eh + 1);
1383 th = (struct tcphdr *)(ip6 + 1);
1384 tlen = ntohs(ip6->ip6_plen);
1385 drop_hdrlen = sizeof(*ip6);
1386 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
1387 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
1388 counter_u64_add(tcp_csum_hardware_w_ph, 1);
1389 th->th_sum = m->m_pkthdr.csum_data;
1391 counter_u64_add(tcp_csum_hardware, 1);
1392 th->th_sum = in6_cksum_pseudo(ip6, tlen,
1393 IPPROTO_TCP, m->m_pkthdr.csum_data);
1395 th->th_sum ^= 0xffff;
1397 counter_u64_add(tcp_csum_software, 1);
1398 th->th_sum = in6_cksum(m, IPPROTO_TCP, drop_hdrlen, tlen);
1401 TCPSTAT_INC(tcps_rcvbadsum);
1403 /* Log the bad news */
1404 struct tcpcb *tp = intotcpcb(inp);
1406 tcp_lro_log(tp, lc, le, m, 13, tlen, m->m_pkthdr.csum_flags, drop_hdrlen, th->th_sum);
1416 * Be proactive about unspecified IPv6 address in source.
1417 * As we use all-zero to indicate unbounded/unconnected pcb,
1418 * unspecified IPv6 address can be used to confuse us.
1420 * Note that packets with unspecified IPv6 destination is
1421 * already dropped in ip6_input.
1423 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1434 if (m->m_len < sizeof (struct tcpiphdr)) {
1435 if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
1437 TCPSTAT_INC(tcps_rcvshort);
1442 ip = (struct ip *)(eh + 1);
1443 th = (struct tcphdr *)(ip + 1);
1445 drop_hdrlen = sizeof(*ip);
1446 tlen = ntohs(ip->ip_len) - sizeof(struct ip);
1447 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1448 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
1449 counter_u64_add(tcp_csum_hardware_w_ph, 1);
1450 th->th_sum = m->m_pkthdr.csum_data;
1452 counter_u64_add(tcp_csum_hardware, 1);
1453 th->th_sum = in_pseudo(ip->ip_src.s_addr,
1455 htonl(m->m_pkthdr.csum_data + tlen +
1458 th->th_sum ^= 0xffff;
1461 struct ipovly *ipov = (struct ipovly *)ip;
1463 * Checksum extended TCP header and data.
1465 counter_u64_add(tcp_csum_software, 1);
1466 len = drop_hdrlen + tlen;
1467 bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
1468 ipov->ih_len = htons(tlen);
1469 th->th_sum = in_cksum(m, len);
1470 /* Reset length for SDT probes. */
1471 ip->ip_len = htons(len);
1472 /* Reset TOS bits */
1474 /* Re-initialization for later version check */
1475 ip->ip_v = IPVERSION;
1476 ip->ip_hl = sizeof(*ip) >> 2;
1479 TCPSTAT_INC(tcps_rcvbadsum);
1481 /* Log the bad news */
1482 struct tcpcb *tp = intotcpcb(inp);
1484 tcp_lro_log(tp, lc, le, m, 13, tlen, m->m_pkthdr.csum_flags, drop_hdrlen, th->th_sum);
1498 tcp_lro_rx2(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, int use_hash)
1500 struct lro_entry *le;
1501 struct ether_header *eh;
1503 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
1506 struct ip *ip4 = NULL; /* Keep compiler happy. */
1509 void *l3hdr = NULL; /* Keep compiler happy. */
1512 int error, ip_len, hdr_len, locked = 0;
1513 uint16_t eh_type, tcp_data_len, need_flush;
1517 struct lro_head *bucket;
1518 struct timespec arrv;
1520 /* Clear the flags we may use to communicate with TCP */
1521 m->m_flags &= ~(M_ACKCMP|M_LRO_EHDRSTRP);
1523 /* We expect a contiguous header [eh, ip, tcp]. */
1524 if ((m->m_flags & (M_TSTMP_LRO|M_TSTMP)) == 0) {
1525 /* If no hardware or arrival stamp on the packet add arrival */
1527 m->m_pkthdr.rcv_tstmp = (arrv.tv_sec * 1000000000) + arrv.tv_nsec;
1528 m->m_flags |= M_TSTMP_LRO;
1530 eh = mtod(m, struct ether_header *);
1531 eh_type = ntohs(eh->ether_type);
1534 case ETHERTYPE_IPV6:
1536 CURVNET_SET(lc->ifp->if_vnet);
1537 if (V_ip6_forwarding != 0) {
1538 /* XXX-BZ stats but changing lro_ctrl is a problem. */
1540 return (TCP_LRO_CANNOT);
1543 l3hdr = ip6 = (struct ip6_hdr *)(eh + 1);
1544 error = tcp_lro_rx_ipv6(lc, m, ip6, &th);
1547 tcp_data_len = ntohs(ip6->ip6_plen);
1549 iptos = IPV6_TRAFFIC_CLASS(ip6);
1551 ip_len = sizeof(*ip6) + tcp_data_len;
1558 CURVNET_SET(lc->ifp->if_vnet);
1559 if (V_ipforwarding != 0) {
1560 /* XXX-BZ stats but changing lro_ctrl is a problem. */
1562 return (TCP_LRO_CANNOT);
1565 l3hdr = ip4 = (struct ip *)(eh + 1);
1566 error = tcp_lro_rx_ipv4(lc, m, ip4, &th);
1569 ip_len = ntohs(ip4->ip_len);
1571 iptos = ip4->ip_tos;
1573 tcp_data_len = ip_len - sizeof(*ip4);
1577 /* XXX-BZ what happens in case of VLAN(s)? */
1579 return (TCP_LRO_NOT_SUPPORTED);
1583 * If the frame is padded beyond the end of the IP packet, then we must
1584 * trim the extra bytes off.
1586 hdr_len = m->m_pkthdr.len - (ETHER_HDR_LEN + ip_len);
1589 /* Truncated packet. */
1590 return (TCP_LRO_CANNOT);
1595 * Check TCP header constraints.
1597 hdr_len = (th->th_off << 2);
1598 ts_ptr = (uint32_t *)(th + 1);
1599 tcp_data_len -= hdr_len;
1600 hdr_len -= sizeof(*th);
1601 if (th->th_flags & TH_SYN)
1602 return (TCP_LRO_CANNOT);
1603 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) {
1607 if (hdr_len != 0 && (__predict_false(hdr_len != TCPOLEN_TSTAMP_APPA) ||
1608 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
1609 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
1611 * We have an option besides Timestamps, maybe
1612 * it is a sack (most likely) which means we
1613 * will probably need to wake up a sleeper (if
1614 * the guy does queueing).
1618 /* If the driver did not pass in the checksum, set it now. */
1621 seq = ntohl(th->th_seq);
1623 bucket = &lc->lro_hash[0];
1624 } else if (M_HASHTYPE_ISHASH(m)) {
1625 bucket = &lc->lro_hash[m->m_pkthdr.flowid % lc->lro_hashsz];
1632 hash = ip4->ip_src.s_addr + ip4->ip_dst.s_addr;
1636 case ETHERTYPE_IPV6:
1637 hash = ip6->ip6_src.s6_addr32[0] +
1638 ip6->ip6_dst.s6_addr32[0];
1639 hash += ip6->ip6_src.s6_addr32[1] +
1640 ip6->ip6_dst.s6_addr32[1];
1641 hash += ip6->ip6_src.s6_addr32[2] +
1642 ip6->ip6_dst.s6_addr32[2];
1643 hash += ip6->ip6_src.s6_addr32[3] +
1644 ip6->ip6_dst.s6_addr32[3];
1651 hash += th->th_sport + th->th_dport;
1652 bucket = &lc->lro_hash[hash % lc->lro_hashsz];
1655 /* Try to find a matching previous segment. */
1656 LIST_FOREACH(le, bucket, hash_next) {
1657 if (le->eh_type != eh_type)
1659 if (le->source_port != th->th_sport ||
1660 le->dest_port != th->th_dport)
1664 case ETHERTYPE_IPV6:
1665 if (bcmp(&le->source_ip6, &ip6->ip6_src,
1666 sizeof(struct in6_addr)) != 0 ||
1667 bcmp(&le->dest_ip6, &ip6->ip6_dst,
1668 sizeof(struct in6_addr)) != 0)
1674 if (le->source_ip4 != ip4->ip_src.s_addr ||
1675 le->dest_ip4 != ip4->ip_dst.s_addr)
1680 if (tcp_data_len || SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq)) ||
1681 (th->th_ack == le->ack_seq)) {
1682 m->m_pkthdr.lro_len = tcp_data_len;
1684 /* no data and old ack */
1689 if ((tcplro_stacks_wanting_mbufq == 0) || (m->m_flags & M_VLANTAG))
1691 if (le->inp == NULL) {
1692 CURVNET_SET(lc->ifp->if_vnet);
1693 le->inp = tcp_lro_lookup(lc, le);
1699 } else if (le->inp) {
1703 if (locked && ((le->inp->inp_flags & (INP_DROPPED|INP_TIMEWAIT)) ||
1704 (le->inp->inp_flags2 & INP_FREED))) {
1705 /* We can't present these to the inp since
1710 ret = in_pcbrele_wlocked(le->inp);
1712 INP_WUNLOCK(le->inp);
1715 tcp_lro_active_remove(le);
1716 if (le->strip_cnt && le->m_head) {
1718 * If we have any stripped packets we
1719 * just dump the whole chain. The
1720 * tcp_lro_flush code knows how
1721 * to handle things when le->m_head is NULL
1722 * and even le->inp is NULL.
1724 lro_free_mbuf_chain(le->m_head);
1727 tcp_lro_flush(lc, le);
1728 return (TCP_LRO_CANNOT);
1730 /* See if it has been switched on */
1731 if (le->inp && (le->inp->inp_flags2 & INP_MBUF_ACKCMP))
1732 le->flags |= CAN_USE_ACKCMP;
1734 if ((need_flush == 1) &&
1736 (le->inp->inp_flags2 & (INP_MBUF_ACKCMP|INP_SUPPORTS_MBUFQ)) &&
1737 ((th->th_flags & ~(TH_ACK | TH_PUSH | TH_ECE | TH_CWR)) == 0)) {
1739 * For MBUF queuing or ACKCMP we can accept ECE and CWR
1740 * since each packet is sent to the transport (or the
1741 * compressed state including the ECN bits).
1748 le->need_wakeup = need_flush;
1749 /* Save of the data only csum */
1750 m->m_pkthdr.rcvif = lc->ifp;
1751 m->m_pkthdr.lro_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th,
1752 tcp_data_len, ~csum);
1753 th->th_sum = csum; /* Restore checksum */
1755 if ((le->flags & CAN_USE_ACKCMP) ||
1757 (le->inp->inp_flags2 & (INP_MBUF_ACKCMP|INP_SUPPORTS_MBUFQ)))) {
1759 * Mbuf queued and ACKCMP packets have their BPF and csum
1760 * done here in LRO. They will still end up looking at the
1761 * headers and such (IP/TCP) but we don't want to proceed
1762 * with any bad csum!
1764 m = do_bpf_and_csum(le->inp, lc, le, eh, m, bpf_peers_present(lc->ifp->if_bpf), locked);
1766 /* Bad csum, accounting already done */
1768 INP_WUNLOCK(le->inp);
1774 if ((need_flush == 0) &&
1775 (th->th_flags & TH_ACK) &&
1776 (tcp_data_len == 0) &&
1777 (le->flags & CAN_USE_ACKCMP)) {
1779 * Ok this is a pure ack lets find out if our
1780 * last packet already has one of these.
1783 struct tcp_ackent *ack_ent;
1786 INP_WLOCK_ASSERT(le->inp);
1787 if (le->m_head == NULL) {
1788 /* Ok can we still use the end of the inp's? */
1789 nm = tcp_lro_get_last_if_ackcmp(lc, le, le->inp);
1794 /* We can add in to the one on the tail */
1795 ack_ent = mtod(nm, struct tcp_ackent *);
1796 idx = (nm->m_len / sizeof(struct tcp_ackent));
1797 build_ack_entry(&ack_ent[idx], th, m, hdr_len, iptos);
1798 /* Bump the size of both pkt-hdr and len */
1799 nm->m_len += sizeof(struct tcp_ackent);
1800 nm->m_pkthdr.len += sizeof(struct tcp_ackent);
1801 le->ack_seq = th->th_ack;
1802 le->window = th->th_win;
1804 counter_u64_add(tcp_extra_mbuf, 1);
1805 INP_WUNLOCK(le->inp);
1807 } else if (le->m_last_mbuf->m_flags & M_ACKCMP) {
1808 /* Yes we might be able to be appended to */
1809 nm = le->m_last_mbuf;
1810 if (M_TRAILINGSPACE(nm) < sizeof(struct tcp_ackent)) {
1811 if ((le->inp->inp_flags2 & INP_MBUF_L_ACKS) == 0) {
1812 counter_u64_add(tcp_would_have_but, 1);
1813 le->inp->inp_flags2 |= INP_MBUF_L_ACKS;
1818 ack_ent = mtod(nm, struct tcp_ackent *);
1819 idx = (nm->m_len / sizeof(struct tcp_ackent));
1820 build_ack_entry(&ack_ent[idx], th, m, hdr_len, iptos);
1821 /* Bump the size of both pkt-hdr and len */
1822 nm->m_len += sizeof(struct tcp_ackent);
1823 nm->m_pkthdr.len += sizeof(struct tcp_ackent);
1825 le->flags |= HAS_COMP_ENTRIES;
1829 /* Nope we need a new one */
1831 if (le->inp->inp_flags2 & INP_MBUF_L_ACKS)
1832 nm = m_getcl(M_NOWAIT, MT_DATA, (M_ACKCMP|M_PKTHDR));
1834 nm = m_gethdr(M_NOWAIT, MT_DATA);
1835 nm->m_flags |= M_ACKCMP;
1838 nm->m_pkthdr.rcvif = lc->ifp;
1839 ack_ent = mtod(nm, struct tcp_ackent *);
1840 build_ack_entry(ack_ent, th, m, hdr_len, iptos);
1843 m->m_pkthdr.len = m->m_len = sizeof(struct tcp_ackent);
1844 le->flags |= HAS_COMP_ENTRIES;
1847 /* We fall through and append */
1850 if (m->m_flags & M_ACKCMP) {
1851 counter_u64_add(tcp_comp_total, 1);
1853 counter_u64_add(tcp_uncomp_total, 1);
1856 /* Save off the tail I am appending too (prev) */
1857 m->m_nextpkt = NULL;
1858 if (le->m_head == NULL) {
1860 * Case where we wer chaining off the inp
1861 * and now no-longer can.
1864 le->m_tail = m_last(m);
1865 le->m_last_mbuf = m;
1866 le->m_prev_last = NULL;
1868 le->m_prev_last = le->m_last_mbuf;
1869 /* Mark me in the last spot */
1870 le->m_last_mbuf->m_nextpkt = m;
1871 /* Now set the tail to me */
1872 le->m_last_mbuf = m;
1873 le->tcp_tot_p_len += tcp_data_len;
1879 /* Add to the total size of data */
1880 lro_set_mtime(&le->mtime, &arrv);
1882 INP_WUNLOCK(le->inp);
1885 /* Try to find an empty slot. */
1886 if (LIST_EMPTY(&lc->lro_free))
1887 return (TCP_LRO_NO_ENTRIES);
1889 /* Start a new segment chain. */
1890 le = LIST_FIRST(&lc->lro_free);
1891 LIST_REMOVE(le, next);
1892 tcp_lro_active_insert(lc, bucket, le);
1893 lro_set_mtime(&le->mtime, &arrv);
1895 /* Start filling in details. */
1898 case ETHERTYPE_IPV6:
1900 le->source_ip6 = ip6->ip6_src;
1901 le->dest_ip6 = ip6->ip6_dst;
1902 le->eh_type = eh_type;
1903 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN - sizeof(*ip6);
1909 le->source_ip4 = ip4->ip_src.s_addr;
1910 le->dest_ip4 = ip4->ip_dst.s_addr;
1911 le->eh_type = eh_type;
1912 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN;
1916 le->source_port = th->th_sport;
1917 le->dest_port = th->th_dport;
1918 le->next_seq = seq + tcp_data_len;
1919 le->ack_seq = th->th_ack;
1920 le->window = th->th_win;
1923 le->tsval = ntohl(*(ts_ptr + 1));
1924 le->tsecr = *(ts_ptr + 2);
1926 KASSERT(le->ulp_csum == 0, ("%s: le=%p le->ulp_csum=0x%04x\n",
1927 __func__, le, le->ulp_csum));
1930 le->ulp_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th, tcp_data_len,
1933 th->th_sum = csum; /* Restore checksum */
1934 m->m_pkthdr.rcvif = lc->ifp;
1935 m->m_pkthdr.lro_len = tcp_data_len;
1937 le->cmp_ack_cnt = 0;
1941 * Lets find out if we can use the mbuf-compression.
1943 if ((tcplro_stacks_wanting_mbufq == 0) || (m->m_flags & M_VLANTAG))
1945 CURVNET_SET(lc->ifp->if_vnet);
1946 le->inp = tcp_lro_lookup(lc, le);
1947 if (le->inp && ((le->inp->inp_flags & (INP_DROPPED|INP_TIMEWAIT)) ||
1948 (le->inp->inp_flags2 & INP_FREED))) {
1949 INP_WUNLOCK(le->inp);
1953 if ((need_flush == 1) &&
1954 (le->inp->inp_flags2 & (INP_MBUF_ACKCMP|INP_SUPPORTS_MBUFQ)) &&
1955 ((th->th_flags & ~(TH_ACK | TH_PUSH | TH_ECE | TH_CWR)) == 0)) {
1957 * For MBUF queuing or ACKCMP we can accept ECE and CWR
1958 * since each packet is sent to the transport (or the
1959 * compressed state including the ECN bits).
1964 if (le->inp->inp_flags2 & INP_MBUF_ACKCMP)
1965 le->flags |= CAN_USE_ACKCMP;
1966 if ((le->flags & CAN_USE_ACKCMP) ||
1968 (le->inp->inp_flags2 & (INP_MBUF_ACKCMP|INP_SUPPORTS_MBUFQ)))) {
1969 m = do_bpf_and_csum(le->inp, lc, le, eh, m, bpf_peers_present(lc->ifp->if_bpf), locked);
1971 /* Bad csum, accounting already done */
1972 INP_WUNLOCK(le->inp);
1981 if ((need_flush == 0) &&
1982 (th->th_flags & TH_ACK) &&
1983 (tcp_data_len == 0) &&
1984 (le->flags & CAN_USE_ACKCMP)) {
1985 /* Ok this is a pure ack lets build our special COMPRESS mbuf */
1987 struct tcp_ackent *ack_ent;
1989 /* Question what is going on with the last mbuf on the inp queue, can we use it? */
1990 INP_WLOCK_ASSERT(le->inp);
1991 nm = tcp_lro_get_last_if_ackcmp(lc, le, le->inp);
1995 /* We can add in to the one on the tail */
1996 ack_ent = mtod(nm, struct tcp_ackent *);
1997 idx = (nm->m_len / sizeof(struct tcp_ackent));
1998 build_ack_entry(&ack_ent[idx], th, m, hdr_len, iptos);
1999 nm->m_len += sizeof(struct tcp_ackent);
2000 nm->m_pkthdr.len += sizeof(struct tcp_ackent);
2001 le->ack_seq = th->th_ack;
2002 le->window = th->th_win;
2004 counter_u64_add(tcp_extra_mbuf, 1);
2007 le->m_last_mbuf = NULL;
2008 le->m_prev_last = NULL;
2009 INP_WUNLOCK(le->inp);
2012 if (le->inp->inp_flags2 & INP_MBUF_L_ACKS)
2013 nm = m_getcl(M_NOWAIT, MT_DATA, (M_ACKCMP|M_PKTHDR));
2015 nm = m_gethdr(M_NOWAIT, MT_DATA);
2016 nm->m_flags |= M_ACKCMP;
2019 nm->m_pkthdr.rcvif = lc->ifp;
2020 ack_ent = mtod(nm, struct tcp_ackent *);
2021 build_ack_entry(ack_ent, th, m, hdr_len, iptos);
2024 m->m_pkthdr.len = m->m_len = sizeof(struct tcp_ackent);
2025 le->flags |= HAS_COMP_ENTRIES;
2030 if (m->m_flags & M_ACKCMP) {
2031 counter_u64_add(tcp_comp_total, 1);
2033 counter_u64_add(tcp_uncomp_total, 1);
2038 le->need_wakeup = need_flush;
2040 le->need_wakeup = 0;
2041 m->m_nextpkt = NULL;
2043 le->m_tail = m_last(m);
2044 le->m_last_mbuf = m;
2045 le->m_prev_last = NULL;
2047 * We keep the total size here for cross checking when we may need
2048 * to flush/wakeup in the MBUF_QUEUE case.
2050 le->tcp_tot_p_len = tcp_data_len;
2052 INP_WUNLOCK(le->inp);
2057 tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
2060 return tcp_lro_rx2(lc, m, csum, 1);
2064 tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
2066 struct timespec arrv;
2069 if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
2070 lc->lro_mbuf_max == 0)) {
2076 /* check if packet is not LRO capable */
2077 if (__predict_false(mb->m_pkthdr.csum_flags == 0 ||
2078 (lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
2079 /* input packet to network layer */
2080 (*lc->ifp->if_input) (lc->ifp, mb);
2083 /* Arrival Stamp the packet */
2085 if ((mb->m_flags & M_TSTMP) == 0) {
2086 /* If no hardware or arrival stamp on the packet add arrival */
2088 mb->m_pkthdr.rcv_tstmp = ((arrv.tv_sec * 1000000000) +
2090 mb->m_flags |= M_TSTMP_LRO;
2092 /* create sequence number */
2093 lc->lro_mbuf_data[lc->lro_mbuf_count].seq =
2094 (((uint64_t)M_HASHTYPE_GET(mb)) << 56) |
2095 (((uint64_t)mb->m_pkthdr.flowid) << 24) |
2096 ((uint64_t)lc->lro_mbuf_count);
2099 lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb;
2101 /* flush if array is full */
2102 if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max))
2103 tcp_lro_flush_all(lc);