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>
56 #include <netinet/in_systm.h>
57 #include <netinet/in.h>
58 #include <netinet/ip6.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet6/in6_pcb.h>
63 #include <netinet/tcp.h>
64 #include <netinet/tcp_seq.h>
65 #include <netinet/tcp_lro.h>
66 #include <netinet/tcp_var.h>
67 #include <netinet/tcp_hpts.h>
68 #include <netinet/tcp_log_buf.h>
69 #include <netinet6/ip6_var.h>
71 #include <machine/in_cksum.h>
73 static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures");
75 #define TCP_LRO_UPDATE_CSUM 1
76 #ifndef TCP_LRO_UPDATE_CSUM
77 #define TCP_LRO_INVALID_CSUM 0x0000
80 static void tcp_lro_rx_done(struct lro_ctrl *lc);
81 static int tcp_lro_rx2(struct lro_ctrl *lc, struct mbuf *m,
82 uint32_t csum, int use_hash);
84 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
87 static long tcplro_stacks_wanting_mbufq = 0;
88 counter_u64_t tcp_inp_lro_direct_queue;
89 counter_u64_t tcp_inp_lro_wokeup_queue;
90 counter_u64_t tcp_inp_lro_compressed;
91 counter_u64_t tcp_inp_lro_single_push;
92 counter_u64_t tcp_inp_lro_locks_taken;
93 counter_u64_t tcp_inp_lro_sack_wake;
95 static unsigned tcp_lro_entries = TCP_LRO_ENTRIES;
96 static int32_t hold_lock_over_compress = 0;
97 SYSCTL_INT(_net_inet_tcp_lro, OID_AUTO, hold_lock, CTLFLAG_RW,
98 &hold_lock_over_compress, 0,
99 "Do we hold the lock over the compress of mbufs?");
100 SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries,
101 CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0,
102 "default number of LRO entries");
103 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD,
104 &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport");
105 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD,
106 &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts");
107 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD,
108 &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport");
109 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, single, CTLFLAG_RD,
110 &tcp_inp_lro_single_push, "Number of lro's sent with single segment");
111 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD,
112 &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken");
113 SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, sackwakeups, CTLFLAG_RD,
114 &tcp_inp_lro_sack_wake, "Number of wakeups caused by sack/fin");
117 tcp_lro_reg_mbufq(void)
119 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1);
123 tcp_lro_dereg_mbufq(void)
125 atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1);
129 tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket,
130 struct lro_entry *le)
133 LIST_INSERT_HEAD(&lc->lro_active, le, next);
134 LIST_INSERT_HEAD(bucket, le, hash_next);
138 tcp_lro_active_remove(struct lro_entry *le)
141 LIST_REMOVE(le, next); /* active list */
142 LIST_REMOVE(le, hash_next); /* hash bucket */
146 tcp_lro_init(struct lro_ctrl *lc)
148 return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0));
152 tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp,
153 unsigned lro_entries, unsigned lro_mbufs)
155 struct lro_entry *le;
157 unsigned i, elements;
159 lc->lro_bad_csum = 0;
162 lc->lro_mbuf_count = 0;
163 lc->lro_mbuf_max = lro_mbufs;
164 lc->lro_cnt = lro_entries;
165 lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX;
166 lc->lro_length_lim = TCP_LRO_LENGTH_MAX;
168 LIST_INIT(&lc->lro_free);
169 LIST_INIT(&lc->lro_active);
171 /* create hash table to accelerate entry lookup */
172 if (lro_entries > lro_mbufs)
173 elements = lro_entries;
175 elements = lro_mbufs;
176 lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz,
178 if (lc->lro_hash == NULL) {
179 memset(lc, 0, sizeof(*lc));
183 /* compute size to allocate */
184 size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) +
185 (lro_entries * sizeof(*le));
186 lc->lro_mbuf_data = (struct lro_mbuf_sort *)
187 malloc(size, M_LRO, M_NOWAIT | M_ZERO);
189 /* check for out of memory */
190 if (lc->lro_mbuf_data == NULL) {
191 free(lc->lro_hash, M_LRO);
192 memset(lc, 0, sizeof(*lc));
195 /* compute offset for LRO entries */
196 le = (struct lro_entry *)
197 (lc->lro_mbuf_data + lro_mbufs);
199 /* setup linked list */
200 for (i = 0; i != lro_entries; i++)
201 LIST_INSERT_HEAD(&lc->lro_free, le + i, next);
206 static struct tcphdr *
207 tcp_lro_get_th(struct lro_entry *le, struct mbuf *m)
209 struct ether_header *eh;
210 struct tcphdr *th = NULL;
212 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
215 struct ip *ip4 = NULL; /* Keep compiler happy. */
218 eh = mtod(m, struct ether_header *);
219 switch (le->eh_type) {
222 ip6 = (struct ip6_hdr *)(eh + 1);
223 th = (struct tcphdr *)(ip6 + 1);
228 ip4 = (struct ip *)(eh + 1);
229 th = (struct tcphdr *)(ip4 + 1);
237 tcp_lro_free(struct lro_ctrl *lc)
239 struct lro_entry *le;
242 /* reset LRO free list */
243 LIST_INIT(&lc->lro_free);
245 /* free active mbufs, if any */
246 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
247 tcp_lro_active_remove(le);
251 /* free hash table */
252 free(lc->lro_hash, M_LRO);
256 /* free mbuf array, if any */
257 for (x = 0; x != lc->lro_mbuf_count; x++)
258 m_freem(lc->lro_mbuf_data[x].mb);
259 lc->lro_mbuf_count = 0;
261 /* free allocated memory, if any */
262 free(lc->lro_mbuf_data, M_LRO);
263 lc->lro_mbuf_data = NULL;
267 tcp_lro_csum_th(struct tcphdr *th)
272 ch = th->th_sum = 0x0000;
283 ch = (ch >> 16) + (ch & 0xffff);
285 return (ch & 0xffff);
289 tcp_lro_rx_csum_fixup(struct lro_entry *le, void *l3hdr, struct tcphdr *th,
290 uint16_t tcp_data_len, uint16_t csum)
297 /* Remove length from checksum. */
298 switch (le->eh_type) {
304 ip6 = (struct ip6_hdr *)l3hdr;
305 if (le->append_cnt == 0)
310 cx = ntohs(ip6->ip6_plen);
311 cs = in6_cksum_pseudo(ip6, cx, ip6->ip6_nxt, 0);
321 ip4 = (struct ip *)l3hdr;
322 if (le->append_cnt == 0)
325 cs = in_addword(ntohs(ip4->ip_len) - sizeof(*ip4),
327 cs = in_pseudo(ip4->ip_src.s_addr, ip4->ip_dst.s_addr,
334 cs = 0; /* Keep compiler happy. */
340 /* Remove TCP header csum. */
341 cs = ~tcp_lro_csum_th(th);
344 c = (c >> 16) + (c & 0xffff);
350 tcp_lro_rx_done(struct lro_ctrl *lc)
352 struct lro_entry *le;
354 while ((le = LIST_FIRST(&lc->lro_active)) != NULL) {
355 tcp_lro_active_remove(le);
356 tcp_lro_flush(lc, le);
361 tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout)
363 struct lro_entry *le, *le_tmp;
366 if (LIST_EMPTY(&lc->lro_active))
370 timevalsub(&tv, timeout);
371 LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) {
372 if (timevalcmp(&tv, &le->mtime, >=)) {
373 tcp_lro_active_remove(le);
374 tcp_lro_flush(lc, le);
381 tcp_lro_rx_ipv6(struct lro_ctrl *lc, struct mbuf *m, struct ip6_hdr *ip6,
385 /* XXX-BZ we should check the flow-label. */
387 /* XXX-BZ We do not yet support ext. hdrs. */
388 if (ip6->ip6_nxt != IPPROTO_TCP)
389 return (TCP_LRO_NOT_SUPPORTED);
391 /* Find the TCP header. */
392 *th = (struct tcphdr *)(ip6 + 1);
400 tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4,
406 if (ip4->ip_p != IPPROTO_TCP)
407 return (TCP_LRO_NOT_SUPPORTED);
409 /* Ensure there are no options. */
410 if ((ip4->ip_hl << 2) != sizeof (*ip4))
411 return (TCP_LRO_CANNOT);
413 /* .. and the packet is not fragmented. */
414 if (ip4->ip_off & htons(IP_MF|IP_OFFMASK))
415 return (TCP_LRO_CANNOT);
417 /* Legacy IP has a header checksum that needs to be correct. */
418 csum_flags = m->m_pkthdr.csum_flags;
419 if (csum_flags & CSUM_IP_CHECKED) {
420 if (__predict_false((csum_flags & CSUM_IP_VALID) == 0)) {
422 return (TCP_LRO_CANNOT);
425 csum = in_cksum_hdr(ip4);
426 if (__predict_false((csum) != 0)) {
428 return (TCP_LRO_CANNOT);
431 /* Find the TCP header (we assured there are no IP options). */
432 *th = (struct tcphdr *)(ip4 + 1);
438 tcp_lro_log(struct tcpcb *tp, struct lro_ctrl *lc,
439 struct lro_entry *le, struct mbuf *m, int frm, int32_t tcp_data_len,
440 uint32_t th_seq , uint32_t th_ack, uint16_t th_win)
442 if (tp->t_logstate != TCP_LOG_STATE_OFF) {
443 union tcp_log_stackspecific log;
447 cts = tcp_get_usecs(&tv);
448 memset(&log, 0, sizeof(union tcp_log_stackspecific));
449 log.u_bbr.flex8 = frm;
450 log.u_bbr.flex1 = tcp_data_len;
452 log.u_bbr.flex2 = m->m_pkthdr.len;
455 log.u_bbr.flex3 = le->append_cnt;
456 log.u_bbr.flex4 = le->p_len;
457 log.u_bbr.flex5 = le->m_head->m_pkthdr.len;
458 log.u_bbr.delRate = le->m_head->m_flags;
459 log.u_bbr.rttProp = le->m_head->m_pkthdr.rcv_tstmp;
460 log.u_bbr.flex6 = lc->lro_length_lim;
461 log.u_bbr.flex7 = lc->lro_ackcnt_lim;
462 log.u_bbr.inflight = th_seq;
463 log.u_bbr.timeStamp = cts;
464 log.u_bbr.epoch = le->next_seq;
465 log.u_bbr.delivered = th_ack;
466 log.u_bbr.lt_epoch = le->ack_seq;
467 log.u_bbr.pacing_gain = th_win;
468 log.u_bbr.cwnd_gain = le->window;
469 log.u_bbr.cur_del_rate = (uintptr_t)m;
470 log.u_bbr.bw_inuse = (uintptr_t)le->m_head;
471 log.u_bbr.pkts_out = le->mbuf_cnt; /* Total mbufs added */
472 log.u_bbr.applimited = le->ulp_csum;
473 log.u_bbr.lost = le->mbuf_appended;
474 TCP_LOG_EVENTP(tp, NULL,
475 &tp->t_inpcb->inp_socket->so_rcv,
476 &tp->t_inpcb->inp_socket->so_snd,
478 0, &log, false, &tv);
483 tcp_flush_out_le(struct tcpcb *tp, struct lro_ctrl *lc, struct lro_entry *le, int locked)
485 if (le->append_cnt > 1) {
489 p_len = htons(le->p_len);
490 switch (le->eh_type) {
497 ip6->ip6_plen = p_len;
498 th = (struct tcphdr *)(ip6 + 1);
499 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
501 le->p_len += ETHER_HDR_LEN + sizeof(*ip6);
513 /* Fix IP header checksum for new length. */
519 cl = (cl >> 16) + (cl & 0xffff);
523 th = (struct tcphdr *)(ip4 + 1);
524 le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID |
525 CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID;
526 le->p_len += ETHER_HDR_LEN;
531 th = NULL; /* Keep compiler happy. */
533 le->m_head->m_pkthdr.csum_data = 0xffff;
534 le->m_head->m_pkthdr.len = le->p_len;
536 /* Incorporate the latest ACK into the TCP header. */
537 th->th_ack = le->ack_seq;
538 th->th_win = le->window;
539 /* Incorporate latest timestamp into the TCP header. */
540 if (le->timestamp != 0) {
543 ts_ptr = (uint32_t *)(th + 1);
544 ts_ptr[1] = htonl(le->tsval);
545 ts_ptr[2] = le->tsecr;
547 /* Update the TCP header checksum. */
548 le->ulp_csum += p_len;
549 le->ulp_csum += tcp_lro_csum_th(th);
550 while (le->ulp_csum > 0xffff)
551 le->ulp_csum = (le->ulp_csum >> 16) +
552 (le->ulp_csum & 0xffff);
553 th->th_sum = (le->ulp_csum & 0xffff);
554 th->th_sum = ~th->th_sum;
556 tcp_lro_log(tp, lc, le, NULL, 7, 0, 0, 0, 0);
560 * Break any chain, this is not set to NULL on the singleton
561 * case m_nextpkt points to m_head. Other case set them
562 * m_nextpkt to NULL in push_and_replace.
564 le->m_head->m_nextpkt = NULL;
565 le->m_head->m_pkthdr.lro_nsegs = le->append_cnt;
567 tcp_lro_log(tp, lc, le, le->m_head, 8, 0, 0, 0, 0);
569 (*lc->ifp->if_input)(lc->ifp, le->m_head);
570 lc->lro_queued += le->append_cnt;
574 tcp_set_le_to_m(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m)
576 struct ether_header *eh;
577 void *l3hdr = NULL; /* Keep compiler happy. */
580 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
583 struct ip *ip4 = NULL; /* Keep compiler happy. */
586 int error, l, ts_failed = 0;
587 uint16_t tcp_data_len;
591 eh = mtod(m, struct ether_header *);
593 * We must reset the other pointers since the mbuf
594 * we were pointing too is about to go away.
596 switch (le->eh_type) {
599 l3hdr = ip6 = (struct ip6_hdr *)(eh + 1);
600 error = tcp_lro_rx_ipv6(lc, m, ip6, &th);
602 le->source_ip6 = ip6->ip6_src;
603 le->dest_ip6 = ip6->ip6_dst;
604 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN - sizeof(*ip6);
609 l3hdr = ip4 = (struct ip *)(eh + 1);
610 error = tcp_lro_rx_ipv4(lc, m, ip4, &th);
612 le->source_ip4 = ip4->ip_src.s_addr;
613 le->dest_ip4 = ip4->ip_dst.s_addr;
614 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN;
618 KASSERT(error == 0, ("%s: le=%p tcp_lro_rx_xxx failed\n",
620 ts_ptr = (uint32_t *)(th + 1);
621 l = (th->th_off << 2);
624 (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
625 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
626 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
627 /* We have failed to find a timestamp some other option? */
630 if ((l != 0) && (ts_failed == 0)) {
632 le->tsval = ntohl(*(ts_ptr + 1));
633 le->tsecr = *(ts_ptr + 2);
636 le->source_port = th->th_sport;
637 le->dest_port = th->th_dport;
638 /* Pull out the csum */
639 tcp_data_len = m->m_pkthdr.lro_len;
640 le->next_seq = ntohl(th->th_seq) + tcp_data_len;
641 le->ack_seq = th->th_ack;
642 le->window = th->th_win;
644 /* Setup the data pointers */
646 le->m_tail = m_last(m);
648 le->ulp_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th, tcp_data_len,
651 th->th_sum = csum; /* Restore checksum on first packet. */
655 tcp_push_and_replace(struct tcpcb *tp, struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m, int locked)
658 * Push up the stack the current le and replace
663 /* Grab off the next and save it */
664 msave = le->m_head->m_nextpkt;
665 le->m_head->m_nextpkt = NULL;
666 /* Now push out the old le entry */
667 tcp_flush_out_le(tp, lc, le, locked);
669 * Now to replace the data properly in the le
670 * we have to reset the tcp header and
673 tcp_set_le_to_m(lc, le, m);
674 /* Restore the next list */
675 m->m_nextpkt = msave;
679 tcp_lro_condense(struct tcpcb *tp, struct lro_ctrl *lc, struct lro_entry *le, int locked)
682 * Walk through the mbuf chain we
683 * have on tap and compress/condense
689 uint16_t tcp_data_len, csum_upd;
693 * First we must check the lead (m_head)
694 * we must make sure that it is *not*
695 * something that should be sent up
696 * right away (sack etc).
700 m = le->m_head->m_nextpkt;
702 /* Just the one left */
705 th = tcp_lro_get_th(le, le->m_head);
707 ("le:%p m:%p th comes back NULL?", le, le->m_head));
708 l = (th->th_off << 2);
710 ts_ptr = (uint32_t *)(th + 1);
711 if (l != 0 && (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
712 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
713 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
715 * Its not the timestamp. We can't
716 * use this guy as the head.
718 le->m_head->m_nextpkt = m->m_nextpkt;
719 tcp_push_and_replace(tp, lc, le, m, locked);
722 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) {
724 * Make sure that previously seen segements/ACKs are delivered
725 * before this segment, e.g. FIN.
727 le->m_head->m_nextpkt = m->m_nextpkt;
728 tcp_push_and_replace(tp, lc, le, m, locked);
731 while((m = le->m_head->m_nextpkt) != NULL) {
733 * condense m into le, first
734 * pull m out of the list.
736 le->m_head->m_nextpkt = m->m_nextpkt;
739 tcp_data_len = m->m_pkthdr.lro_len;
740 th = tcp_lro_get_th(le, m);
742 ("le:%p m:%p th comes back NULL?", le, m));
743 ts_ptr = (uint32_t *)(th + 1);
744 l = (th->th_off << 2);
747 tcp_lro_log(tp, lc, le, m, 1, 0, 0, 0, 0);
749 if (le->append_cnt >= lc->lro_ackcnt_lim) {
751 tcp_lro_log(tp, lc, le, m, 2, 0, 0, 0, 0);
753 tcp_push_and_replace(tp, lc, le, m, locked);
756 if (le->p_len > (lc->lro_length_lim - tcp_data_len)) {
757 /* Flush now if appending will result in overflow. */
759 tcp_lro_log(tp, lc, le, m, 3, tcp_data_len, 0, 0, 0);
761 tcp_push_and_replace(tp, lc, le, m, locked);
764 if (l != 0 && (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
765 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
766 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
768 * Maybe a sack in the new one? We need to
769 * start all over after flushing the
770 * current le. We will go up to the beginning
771 * and flush it (calling the replace again possibly
772 * or just returning).
774 tcp_push_and_replace(tp, lc, le, m, locked);
777 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) {
778 tcp_push_and_replace(tp, lc, le, m, locked);
782 uint32_t tsval = ntohl(*(ts_ptr + 1));
783 /* Make sure timestamp values are increasing. */
784 if (TSTMP_GT(le->tsval, tsval)) {
785 tcp_push_and_replace(tp, lc, le, m, locked);
789 le->tsecr = *(ts_ptr + 2);
791 /* Try to append the new segment. */
792 if (__predict_false(ntohl(th->th_seq) != le->next_seq ||
793 (tcp_data_len == 0 &&
794 le->ack_seq == th->th_ack &&
795 le->window == th->th_win))) {
796 /* Out of order packet or duplicate ACK. */
798 tcp_lro_log(tp, lc, le, m, 4, tcp_data_len,
803 tcp_push_and_replace(tp, lc, le, m, locked);
806 if (tcp_data_len || SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) {
807 le->next_seq += tcp_data_len;
808 le->ack_seq = th->th_ack;
809 le->window = th->th_win;
810 } else if (th->th_ack == le->ack_seq) {
811 le->window = WIN_MAX(le->window, th->th_win);
813 csum_upd = m->m_pkthdr.lro_csum;
814 le->ulp_csum += csum_upd;
815 if (tcp_data_len == 0) {
819 tcp_lro_log(tp, lc, le, m, 5, tcp_data_len,
829 le->p_len += tcp_data_len;
831 * Adjust the mbuf so that m_data points to the first byte of
832 * the ULP payload. Adjust the mbuf to avoid complications and
833 * append new segment to existing mbuf chain.
835 m_adj(m, m->m_pkthdr.len - tcp_data_len);
837 tcp_lro_log(tp, lc, le, m, 6, tcp_data_len,
843 le->m_tail->m_next = m;
844 le->m_tail = m_last(m);
850 tcp_queue_pkts(struct tcpcb *tp, struct lro_entry *le)
852 if (tp->t_in_pkt == NULL) {
853 /* Nothing yet there */
854 tp->t_in_pkt = le->m_head;
855 tp->t_tail_pkt = le->m_last_mbuf;
857 /* Already some there */
858 tp->t_tail_pkt->m_nextpkt = le->m_head;
859 tp->t_tail_pkt = le->m_last_mbuf;
862 le->m_last_mbuf = NULL;
867 tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le)
869 struct tcpcb *tp = NULL;
872 struct inpcb *inp = NULL;
873 int need_wakeup = 0, can_queue = 0;
874 struct epoch_tracker et;
876 /* Now lets lookup the inp first */
877 CURVNET_SET(lc->ifp->if_vnet);
879 * XXXRRS Currently the common input handler for
880 * mbuf queuing cannot handle VLAN Tagged. This needs
881 * to be fixed and the or condition removed (i.e. the
882 * common code should do the right lookup for the vlan
883 * tag and anything else that the vlan_input() does).
885 if ((tcplro_stacks_wanting_mbufq == 0) || (le->m_head->m_flags & M_VLANTAG))
888 switch (le->eh_type) {
891 inp = in6_pcblookup(&V_tcbinfo, &le->source_ip6,
892 le->source_port, &le->dest_ip6,le->dest_port,
899 inp = in_pcblookup(&V_tcbinfo, le->le_ip4->ip_src,
900 le->source_port, le->le_ip4->ip_dst, le->dest_port,
907 if (inp && ((inp->inp_flags & (INP_DROPPED|INP_TIMEWAIT)) ||
908 (inp->inp_flags2 & INP_FREED))) {
909 /* We don't want this guy */
913 if (inp && (inp->inp_flags2 & INP_SUPPORTS_MBUFQ)) {
914 /* The transport supports mbuf queuing */
916 if (le->need_wakeup ||
917 ((inp->inp_in_input == 0) &&
918 ((inp->inp_flags2 & INP_MBUF_QUEUE_READY) == 0))) {
920 * Either the transport is off on a keep-alive
921 * (it has the queue_ready flag clear and its
922 * not already been woken) or the entry has
923 * some urgent thing (FIN or possibly SACK blocks).
924 * This means we need to wake the transport up by
925 * putting it on the input pacer.
928 if ((inp->inp_flags2 & INP_DONT_SACK_QUEUE) &&
929 (le->need_wakeup != 1)) {
931 * Prohibited from a sack wakeup.
936 /* Do we need to be awoken due to lots of data or acks? */
937 if ((le->tcp_tot_p_len >= lc->lro_length_lim) ||
938 (le->mbuf_cnt >= lc->lro_ackcnt_lim))
947 counter_u64_add(tcp_inp_lro_direct_queue, 1);
948 tcp_lro_log(tp, lc, le, NULL, 22, need_wakeup,
949 inp->inp_flags2, inp->inp_in_input, le->need_wakeup);
950 tcp_queue_pkts(tp, le);
953 * We must get the guy to wakeup via
956 counter_u64_add(tcp_inp_lro_wokeup_queue, 1);
958 counter_u64_add(tcp_inp_lro_sack_wake, 1);
959 tcp_queue_to_input(inp);
962 if (inp && (hold_lock_over_compress == 0)) {
966 counter_u64_add(tcp_inp_lro_locks_taken, 1);
969 if (can_queue == 0) {
972 /* Old fashioned lro method */
973 if (le->m_head != le->m_last_mbuf) {
974 counter_u64_add(tcp_inp_lro_compressed, 1);
975 tcp_lro_condense(tp, lc, le, locked);
977 counter_u64_add(tcp_inp_lro_single_push, 1);
978 tcp_flush_out_le(tp, lc, le, locked);
982 counter_u64_add(tcp_inp_lro_locks_taken, 1);
988 bzero(le, sizeof(*le));
989 LIST_INSERT_HEAD(&lc->lro_free, le, next);
992 #ifdef HAVE_INLINE_FLSLL
993 #define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1))
995 static inline uint64_t
996 tcp_lro_msb_64(uint64_t x)
1004 return (x & ~(x >> 1));
1009 * The tcp_lro_sort() routine is comparable to qsort(), except it has
1010 * a worst case complexity limit of O(MIN(N,64)*N), where N is the
1011 * number of elements to sort and 64 is the number of sequence bits
1012 * available. The algorithm is bit-slicing the 64-bit sequence number,
1013 * sorting one bit at a time from the most significant bit until the
1014 * least significant one, skipping the constant bits. This is
1015 * typically called a radix sort.
1018 tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size)
1020 struct lro_mbuf_sort temp;
1027 /* for small arrays insertion sort is faster */
1029 for (x = 1; x < size; x++) {
1031 for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--)
1032 parray[y] = parray[y - 1];
1038 /* compute sequence bits which are constant */
1041 for (x = 0; x != size; x++) {
1042 ones |= parray[x].seq;
1043 zeros |= ~parray[x].seq;
1046 /* compute bits which are not constant into "ones" */
1051 /* pick the most significant bit which is not constant */
1052 ones = tcp_lro_msb_64(ones);
1055 * Move entries having cleared sequence bits to the beginning
1058 for (x = y = 0; y != size; y++) {
1060 if (parray[y].seq & ones)
1064 parray[x] = parray[y];
1069 KASSERT(x != 0 && x != size, ("Memory is corrupted\n"));
1072 tcp_lro_sort(parray, x);
1081 tcp_lro_flush_all(struct lro_ctrl *lc)
1087 /* check if no mbufs to flush */
1088 if (lc->lro_mbuf_count == 0)
1091 /* sort all mbufs according to stream */
1092 tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count);
1094 /* input data into LRO engine, stream by stream */
1096 for (x = 0; x != lc->lro_mbuf_count; x++) {
1100 mb = lc->lro_mbuf_data[x].mb;
1102 /* get sequence number, masking away the packet index */
1103 nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24);
1105 /* check for new stream */
1109 /* flush active streams */
1110 tcp_lro_rx_done(lc);
1113 /* add packet to LRO engine */
1114 if (tcp_lro_rx2(lc, mb, 0, 0) != 0) {
1115 /* input packet to network layer */
1116 (*lc->ifp->if_input)(lc->ifp, mb);
1122 /* flush active streams */
1123 tcp_lro_rx_done(lc);
1125 lc->lro_mbuf_count = 0;
1129 lro_set_mtime(struct timeval *tv, struct timespec *ts)
1131 tv->tv_sec = ts->tv_sec;
1132 tv->tv_usec = ts->tv_nsec / 1000;
1136 tcp_lro_rx2(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, int use_hash)
1138 struct lro_entry *le;
1139 struct ether_header *eh;
1141 struct ip6_hdr *ip6 = NULL; /* Keep compiler happy. */
1144 struct ip *ip4 = NULL; /* Keep compiler happy. */
1147 void *l3hdr = NULL; /* Keep compiler happy. */
1150 int error, ip_len, l;
1151 uint16_t eh_type, tcp_data_len, need_flush;
1152 struct lro_head *bucket;
1153 struct timespec arrv;
1155 /* We expect a contiguous header [eh, ip, tcp]. */
1156 if ((m->m_flags & (M_TSTMP_LRO|M_TSTMP)) == 0) {
1157 /* If no hardware or arrival stamp on the packet add arrival */
1159 m->m_pkthdr.rcv_tstmp = (arrv.tv_sec * 1000000000) + arrv.tv_nsec;
1160 m->m_flags |= M_TSTMP_LRO;
1162 eh = mtod(m, struct ether_header *);
1163 eh_type = ntohs(eh->ether_type);
1166 case ETHERTYPE_IPV6:
1168 CURVNET_SET(lc->ifp->if_vnet);
1169 if (V_ip6_forwarding != 0) {
1170 /* XXX-BZ stats but changing lro_ctrl is a problem. */
1172 return (TCP_LRO_CANNOT);
1175 l3hdr = ip6 = (struct ip6_hdr *)(eh + 1);
1176 error = tcp_lro_rx_ipv6(lc, m, ip6, &th);
1179 tcp_data_len = ntohs(ip6->ip6_plen);
1180 ip_len = sizeof(*ip6) + tcp_data_len;
1187 CURVNET_SET(lc->ifp->if_vnet);
1188 if (V_ipforwarding != 0) {
1189 /* XXX-BZ stats but changing lro_ctrl is a problem. */
1191 return (TCP_LRO_CANNOT);
1194 l3hdr = ip4 = (struct ip *)(eh + 1);
1195 error = tcp_lro_rx_ipv4(lc, m, ip4, &th);
1198 ip_len = ntohs(ip4->ip_len);
1199 tcp_data_len = ip_len - sizeof(*ip4);
1203 /* XXX-BZ what happens in case of VLAN(s)? */
1205 return (TCP_LRO_NOT_SUPPORTED);
1209 * If the frame is padded beyond the end of the IP packet, then we must
1210 * trim the extra bytes off.
1212 l = m->m_pkthdr.len - (ETHER_HDR_LEN + ip_len);
1215 /* Truncated packet. */
1216 return (TCP_LRO_CANNOT);
1221 * Check TCP header constraints.
1223 if (th->th_flags & TH_SYN)
1224 return (TCP_LRO_CANNOT);
1225 if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0)
1229 l = (th->th_off << 2);
1230 ts_ptr = (uint32_t *)(th + 1);
1233 if (l != 0 && (__predict_false(l != TCPOLEN_TSTAMP_APPA) ||
1234 (*ts_ptr != ntohl(TCPOPT_NOP<<24|TCPOPT_NOP<<16|
1235 TCPOPT_TIMESTAMP<<8|TCPOLEN_TIMESTAMP)))) {
1237 * We have an option besides Timestamps, maybe
1238 * it is a sack (most likely) which means we
1239 * will probably need to wake up a sleeper (if
1240 * the guy does queueing).
1245 /* If the driver did not pass in the checksum, set it now. */
1248 seq = ntohl(th->th_seq);
1250 bucket = &lc->lro_hash[0];
1251 } else if (M_HASHTYPE_ISHASH(m)) {
1252 bucket = &lc->lro_hash[m->m_pkthdr.flowid % lc->lro_hashsz];
1259 hash = ip4->ip_src.s_addr + ip4->ip_dst.s_addr;
1263 case ETHERTYPE_IPV6:
1264 hash = ip6->ip6_src.s6_addr32[0] +
1265 ip6->ip6_dst.s6_addr32[0];
1266 hash += ip6->ip6_src.s6_addr32[1] +
1267 ip6->ip6_dst.s6_addr32[1];
1268 hash += ip6->ip6_src.s6_addr32[2] +
1269 ip6->ip6_dst.s6_addr32[2];
1270 hash += ip6->ip6_src.s6_addr32[3] +
1271 ip6->ip6_dst.s6_addr32[3];
1278 hash += th->th_sport + th->th_dport;
1279 bucket = &lc->lro_hash[hash % lc->lro_hashsz];
1282 /* Try to find a matching previous segment. */
1283 LIST_FOREACH(le, bucket, hash_next) {
1284 if (le->eh_type != eh_type)
1286 if (le->source_port != th->th_sport ||
1287 le->dest_port != th->th_dport)
1291 case ETHERTYPE_IPV6:
1292 if (bcmp(&le->source_ip6, &ip6->ip6_src,
1293 sizeof(struct in6_addr)) != 0 ||
1294 bcmp(&le->dest_ip6, &ip6->ip6_dst,
1295 sizeof(struct in6_addr)) != 0)
1301 if (le->source_ip4 != ip4->ip_src.s_addr ||
1302 le->dest_ip4 != ip4->ip_dst.s_addr)
1307 if (tcp_data_len || SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq)) ||
1308 (th->th_ack == le->ack_seq)) {
1309 m->m_pkthdr.lro_len = tcp_data_len;
1311 /* no data and old ack */
1316 le->need_wakeup = need_flush;
1317 /* Save of the data only csum */
1318 m->m_pkthdr.rcvif = lc->ifp;
1319 m->m_pkthdr.lro_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th,
1320 tcp_data_len, ~csum);
1321 th->th_sum = csum; /* Restore checksum */
1322 /* Save off the tail I am appending too (prev) */
1323 le->m_prev_last = le->m_last_mbuf;
1324 /* Mark me in the last spot */
1325 le->m_last_mbuf->m_nextpkt = m;
1326 /* Now set the tail to me */
1327 le->m_last_mbuf = m;
1329 m->m_nextpkt = NULL;
1330 /* Add to the total size of data */
1331 le->tcp_tot_p_len += tcp_data_len;
1332 lro_set_mtime(&le->mtime, &arrv);
1335 /* Try to find an empty slot. */
1336 if (LIST_EMPTY(&lc->lro_free))
1337 return (TCP_LRO_NO_ENTRIES);
1339 /* Start a new segment chain. */
1340 le = LIST_FIRST(&lc->lro_free);
1341 LIST_REMOVE(le, next);
1342 tcp_lro_active_insert(lc, bucket, le);
1343 lro_set_mtime(&le->mtime, &arrv);
1345 /* Start filling in details. */
1348 case ETHERTYPE_IPV6:
1350 le->source_ip6 = ip6->ip6_src;
1351 le->dest_ip6 = ip6->ip6_dst;
1352 le->eh_type = eh_type;
1353 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN - sizeof(*ip6);
1359 le->source_ip4 = ip4->ip_src.s_addr;
1360 le->dest_ip4 = ip4->ip_dst.s_addr;
1361 le->eh_type = eh_type;
1362 le->p_len = m->m_pkthdr.len - ETHER_HDR_LEN;
1366 le->source_port = th->th_sport;
1367 le->dest_port = th->th_dport;
1368 le->next_seq = seq + tcp_data_len;
1369 le->ack_seq = th->th_ack;
1370 le->window = th->th_win;
1373 le->tsval = ntohl(*(ts_ptr + 1));
1374 le->tsecr = *(ts_ptr + 2);
1376 KASSERT(le->ulp_csum == 0, ("%s: le=%p le->ulp_csum=0x%04x\n",
1377 __func__, le, le->ulp_csum));
1380 le->ulp_csum = tcp_lro_rx_csum_fixup(le, l3hdr, th, tcp_data_len,
1383 th->th_sum = csum; /* Restore checksum */
1385 m->m_pkthdr.rcvif = lc->ifp;
1388 le->need_wakeup = need_flush;
1390 le->need_wakeup = 0;
1391 le->m_tail = m_last(m);
1392 le->m_last_mbuf = m;
1393 m->m_nextpkt = NULL;
1394 le->m_prev_last = NULL;
1396 * We keep the total size here for cross checking when we may need
1397 * to flush/wakeup in the MBUF_QUEUE case.
1399 le->tcp_tot_p_len = tcp_data_len;
1400 m->m_pkthdr.lro_len = tcp_data_len;
1405 tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum)
1408 return tcp_lro_rx2(lc, m, csum, 1);
1412 tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb)
1414 struct timespec arrv;
1417 if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL ||
1418 lc->lro_mbuf_max == 0)) {
1424 /* check if packet is not LRO capable */
1425 if (__predict_false(mb->m_pkthdr.csum_flags == 0 ||
1426 (lc->ifp->if_capenable & IFCAP_LRO) == 0)) {
1428 /* input packet to network layer */
1429 (*lc->ifp->if_input) (lc->ifp, mb);
1432 /* Arrival Stamp the packet */
1434 if ((mb->m_flags & M_TSTMP) == 0) {
1435 /* If no hardware or arrival stamp on the packet add arrival */
1437 mb->m_pkthdr.rcv_tstmp = ((arrv.tv_sec * 1000000000) +
1439 mb->m_flags |= M_TSTMP_LRO;
1441 /* create sequence number */
1442 lc->lro_mbuf_data[lc->lro_mbuf_count].seq =
1443 (((uint64_t)M_HASHTYPE_GET(mb)) << 56) |
1444 (((uint64_t)mb->m_pkthdr.flowid) << 24) |
1445 ((uint64_t)lc->lro_mbuf_count);
1448 lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb;
1450 /* flush if array is full */
1451 if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max))
1452 tcp_lro_flush_all(lc);