127f190a3SBjoern A. Zeeb /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 427f190a3SBjoern A. Zeeb * Copyright (c) 2007, Myricom Inc. 527f190a3SBjoern A. Zeeb * Copyright (c) 2008, Intel Corporation. 662b5b6ecSBjoern A. Zeeb * Copyright (c) 2012 The FreeBSD Foundation 79ca874cfSHans Petter Selasky * Copyright (c) 2016-2021 Mellanox Technologies. 827f190a3SBjoern A. Zeeb * All rights reserved. 927f190a3SBjoern A. Zeeb * 1062b5b6ecSBjoern A. Zeeb * Portions of this software were developed by Bjoern Zeeb 1162b5b6ecSBjoern A. Zeeb * under sponsorship from the FreeBSD Foundation. 1262b5b6ecSBjoern A. Zeeb * 1327f190a3SBjoern A. Zeeb * Redistribution and use in source and binary forms, with or without 1427f190a3SBjoern A. Zeeb * modification, are permitted provided that the following conditions 1527f190a3SBjoern A. Zeeb * are met: 1627f190a3SBjoern A. Zeeb * 1. Redistributions of source code must retain the above copyright 1727f190a3SBjoern A. Zeeb * notice, this list of conditions and the following disclaimer. 1827f190a3SBjoern A. Zeeb * 2. Redistributions in binary form must reproduce the above copyright 1927f190a3SBjoern A. Zeeb * notice, this list of conditions and the following disclaimer in the 2027f190a3SBjoern A. Zeeb * documentation and/or other materials provided with the distribution. 2127f190a3SBjoern A. Zeeb * 2227f190a3SBjoern A. Zeeb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2327f190a3SBjoern A. Zeeb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2427f190a3SBjoern A. Zeeb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2527f190a3SBjoern A. Zeeb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2627f190a3SBjoern A. Zeeb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2727f190a3SBjoern A. Zeeb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2827f190a3SBjoern A. Zeeb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2927f190a3SBjoern A. Zeeb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3027f190a3SBjoern A. Zeeb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3127f190a3SBjoern A. Zeeb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3227f190a3SBjoern A. Zeeb * SUCH DAMAGE. 3327f190a3SBjoern A. Zeeb */ 346c5087a8SJack F Vogel 3562b5b6ecSBjoern A. Zeeb #include <sys/cdefs.h> 3662b5b6ecSBjoern A. Zeeb __FBSDID("$FreeBSD$"); 3762b5b6ecSBjoern A. Zeeb 3862b5b6ecSBjoern A. Zeeb #include "opt_inet.h" 3962b5b6ecSBjoern A. Zeeb #include "opt_inet6.h" 4062b5b6ecSBjoern A. Zeeb 416c5087a8SJack F Vogel #include <sys/param.h> 426c5087a8SJack F Vogel #include <sys/systm.h> 436c5087a8SJack F Vogel #include <sys/kernel.h> 448ec07310SGleb Smirnoff #include <sys/malloc.h> 458ec07310SGleb Smirnoff #include <sys/mbuf.h> 466c5087a8SJack F Vogel #include <sys/socket.h> 47e57b2d0eSRandall Stewart #include <sys/socketvar.h> 48e57b2d0eSRandall Stewart #include <sys/sockbuf.h> 498452c1b3SSepherosa Ziehau #include <sys/sysctl.h> 506c5087a8SJack F Vogel 516c5087a8SJack F Vogel #include <net/if.h> 5262b5b6ecSBjoern A. Zeeb #include <net/if_var.h> 536c5087a8SJack F Vogel #include <net/ethernet.h> 5469a34e8dSRandall Stewart #include <net/bpf.h> 555fa2656eSBjoern A. Zeeb #include <net/vnet.h> 566c5087a8SJack F Vogel 576c5087a8SJack F Vogel #include <netinet/in_systm.h> 586c5087a8SJack F Vogel #include <netinet/in.h> 5962b5b6ecSBjoern A. Zeeb #include <netinet/ip6.h> 606c5087a8SJack F Vogel #include <netinet/ip.h> 6131bfc56eSBjoern A. Zeeb #include <netinet/ip_var.h> 62e57b2d0eSRandall Stewart #include <netinet/in_pcb.h> 63e57b2d0eSRandall Stewart #include <netinet6/in6_pcb.h> 646c5087a8SJack F Vogel #include <netinet/tcp.h> 65d7fb35d1SSean Bruno #include <netinet/tcp_seq.h> 666c5087a8SJack F Vogel #include <netinet/tcp_lro.h> 678452c1b3SSepherosa Ziehau #include <netinet/tcp_var.h> 6869a34e8dSRandall Stewart #include <netinet/tcpip.h> 69e57b2d0eSRandall Stewart #include <netinet/tcp_hpts.h> 70e57b2d0eSRandall Stewart #include <netinet/tcp_log_buf.h> 719ca874cfSHans Petter Selasky #include <netinet/udp.h> 7231bfc56eSBjoern A. Zeeb #include <netinet6/ip6_var.h> 7331bfc56eSBjoern A. Zeeb 746c5087a8SJack F Vogel #include <machine/in_cksum.h> 756c5087a8SJack F Vogel 76e936121dSHans Petter Selasky static MALLOC_DEFINE(M_LRO, "LRO", "LRO control structures"); 776c5087a8SJack F Vogel 789ca874cfSHans Petter Selasky #define TCP_LRO_TS_OPTION \ 799ca874cfSHans Petter Selasky ntohl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 809ca874cfSHans Petter Selasky (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP) 816c5087a8SJack F Vogel 826dd38b87SSepherosa Ziehau static void tcp_lro_rx_done(struct lro_ctrl *lc); 839ca874cfSHans Petter Selasky static int tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, 849ca874cfSHans Petter Selasky uint32_t csum, bool use_hash); 859ca874cfSHans Petter Selasky 869ca874cfSHans Petter Selasky #ifdef TCPHPTS 879ca874cfSHans Petter Selasky static bool do_bpf_strip_and_compress(struct inpcb *, struct lro_ctrl *, 889ca874cfSHans Petter Selasky struct lro_entry *, struct mbuf **, struct mbuf **, struct mbuf **, bool *, bool); 899ca874cfSHans Petter Selasky 909ca874cfSHans Petter Selasky #endif 916dd38b87SSepherosa Ziehau 928452c1b3SSepherosa Ziehau SYSCTL_NODE(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 938452c1b3SSepherosa Ziehau "TCP LRO"); 948452c1b3SSepherosa Ziehau 959ca874cfSHans Petter Selasky static long tcplro_stacks_wanting_mbufq; 96e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_direct_queue; 97e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_wokeup_queue; 98e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_compressed; 99e57b2d0eSRandall Stewart counter_u64_t tcp_inp_lro_locks_taken; 10069a34e8dSRandall Stewart counter_u64_t tcp_extra_mbuf; 10169a34e8dSRandall Stewart counter_u64_t tcp_would_have_but; 10269a34e8dSRandall Stewart counter_u64_t tcp_comp_total; 10369a34e8dSRandall Stewart counter_u64_t tcp_uncomp_total; 104ca1a7e10SRandall Stewart counter_u64_t tcp_bad_csums; 105e57b2d0eSRandall Stewart 1068452c1b3SSepherosa Ziehau static unsigned tcp_lro_entries = TCP_LRO_ENTRIES; 1078452c1b3SSepherosa Ziehau SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, entries, 1088452c1b3SSepherosa Ziehau CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_entries, 0, 1098452c1b3SSepherosa Ziehau "default number of LRO entries"); 11069a34e8dSRandall Stewart 111d7955cc0SRandall Stewart static uint32_t tcp_lro_cpu_set_thresh = TCP_LRO_CPU_DECLARATION_THRESH; 112d7955cc0SRandall Stewart SYSCTL_UINT(_net_inet_tcp_lro, OID_AUTO, lro_cpu_threshold, 113d7955cc0SRandall Stewart CTLFLAG_RDTUN | CTLFLAG_MPSAFE, &tcp_lro_cpu_set_thresh, 0, 114d7955cc0SRandall Stewart "Number of interrups in a row on the same CPU that will make us declare an 'affinity' cpu?"); 115d7955cc0SRandall Stewart 116e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, fullqueue, CTLFLAG_RD, 117e57b2d0eSRandall Stewart &tcp_inp_lro_direct_queue, "Number of lro's fully queued to transport"); 118e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, wokeup, CTLFLAG_RD, 119e57b2d0eSRandall Stewart &tcp_inp_lro_wokeup_queue, "Number of lro's where we woke up transport via hpts"); 120e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, compressed, CTLFLAG_RD, 121e57b2d0eSRandall Stewart &tcp_inp_lro_compressed, "Number of lro's compressed and sent to transport"); 122e57b2d0eSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lockcnt, CTLFLAG_RD, 123e57b2d0eSRandall Stewart &tcp_inp_lro_locks_taken, "Number of lro's inp_wlocks taken"); 12469a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, extra_mbuf, CTLFLAG_RD, 12569a34e8dSRandall Stewart &tcp_extra_mbuf, "Number of times we had an extra compressed ack dropped into the tp"); 12669a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, would_have_but, CTLFLAG_RD, 1279ca874cfSHans Petter Selasky &tcp_would_have_but, "Number of times we would have had an extra compressed, but mget failed"); 12869a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, with_m_ackcmp, CTLFLAG_RD, 12969a34e8dSRandall Stewart &tcp_comp_total, "Number of mbufs queued with M_ACKCMP flags set"); 13069a34e8dSRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, without_m_ackcmp, CTLFLAG_RD, 13169a34e8dSRandall Stewart &tcp_uncomp_total, "Number of mbufs queued without M_ACKCMP"); 132ca1a7e10SRandall Stewart SYSCTL_COUNTER_U64(_net_inet_tcp_lro, OID_AUTO, lro_badcsum, CTLFLAG_RD, 133ca1a7e10SRandall Stewart &tcp_bad_csums, "Number of packets that the common code saw with bad csums"); 134e57b2d0eSRandall Stewart 135e57b2d0eSRandall Stewart void 136e57b2d0eSRandall Stewart tcp_lro_reg_mbufq(void) 137e57b2d0eSRandall Stewart { 138e57b2d0eSRandall Stewart atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, 1); 139e57b2d0eSRandall Stewart } 140e57b2d0eSRandall Stewart 141e57b2d0eSRandall Stewart void 142e57b2d0eSRandall Stewart tcp_lro_dereg_mbufq(void) 143e57b2d0eSRandall Stewart { 144e57b2d0eSRandall Stewart atomic_fetchadd_long(&tcplro_stacks_wanting_mbufq, -1); 145e57b2d0eSRandall Stewart } 1468452c1b3SSepherosa Ziehau 14751e3c20dSSepherosa Ziehau static __inline void 14805cde7efSSepherosa Ziehau tcp_lro_active_insert(struct lro_ctrl *lc, struct lro_head *bucket, 14905cde7efSSepherosa Ziehau struct lro_entry *le) 15051e3c20dSSepherosa Ziehau { 15151e3c20dSSepherosa Ziehau 15251e3c20dSSepherosa Ziehau LIST_INSERT_HEAD(&lc->lro_active, le, next); 15305cde7efSSepherosa Ziehau LIST_INSERT_HEAD(bucket, le, hash_next); 15451e3c20dSSepherosa Ziehau } 15551e3c20dSSepherosa Ziehau 15651e3c20dSSepherosa Ziehau static __inline void 15751e3c20dSSepherosa Ziehau tcp_lro_active_remove(struct lro_entry *le) 15851e3c20dSSepherosa Ziehau { 15951e3c20dSSepherosa Ziehau 16005cde7efSSepherosa Ziehau LIST_REMOVE(le, next); /* active list */ 16105cde7efSSepherosa Ziehau LIST_REMOVE(le, hash_next); /* hash bucket */ 16251e3c20dSSepherosa Ziehau } 16351e3c20dSSepherosa Ziehau 1646c5087a8SJack F Vogel int 16562b5b6ecSBjoern A. Zeeb tcp_lro_init(struct lro_ctrl *lc) 1666c5087a8SJack F Vogel { 1678452c1b3SSepherosa Ziehau return (tcp_lro_init_args(lc, NULL, tcp_lro_entries, 0)); 168e936121dSHans Petter Selasky } 169e936121dSHans Petter Selasky 170e936121dSHans Petter Selasky int 171e936121dSHans Petter Selasky tcp_lro_init_args(struct lro_ctrl *lc, struct ifnet *ifp, 172e936121dSHans Petter Selasky unsigned lro_entries, unsigned lro_mbufs) 173e936121dSHans Petter Selasky { 17462b5b6ecSBjoern A. Zeeb struct lro_entry *le; 175e936121dSHans Petter Selasky size_t size; 17605cde7efSSepherosa Ziehau unsigned i, elements; 1776c5087a8SJack F Vogel 17862b5b6ecSBjoern A. Zeeb lc->lro_bad_csum = 0; 17962b5b6ecSBjoern A. Zeeb lc->lro_queued = 0; 18062b5b6ecSBjoern A. Zeeb lc->lro_flushed = 0; 181e936121dSHans Petter Selasky lc->lro_mbuf_count = 0; 182e936121dSHans Petter Selasky lc->lro_mbuf_max = lro_mbufs; 183e936121dSHans Petter Selasky lc->lro_cnt = lro_entries; 1847ae3d4bfSSepherosa Ziehau lc->lro_ackcnt_lim = TCP_LRO_ACKCNT_MAX; 1857ae3d4bfSSepherosa Ziehau lc->lro_length_lim = TCP_LRO_LENGTH_MAX; 186e936121dSHans Petter Selasky lc->ifp = ifp; 1871ea44822SSepherosa Ziehau LIST_INIT(&lc->lro_free); 1881ea44822SSepherosa Ziehau LIST_INIT(&lc->lro_active); 1896c5087a8SJack F Vogel 19005cde7efSSepherosa Ziehau /* create hash table to accelerate entry lookup */ 19105cde7efSSepherosa Ziehau if (lro_entries > lro_mbufs) 19205cde7efSSepherosa Ziehau elements = lro_entries; 19305cde7efSSepherosa Ziehau else 19405cde7efSSepherosa Ziehau elements = lro_mbufs; 19505cde7efSSepherosa Ziehau lc->lro_hash = phashinit_flags(elements, M_LRO, &lc->lro_hashsz, 19605cde7efSSepherosa Ziehau HASH_NOWAIT); 19705cde7efSSepherosa Ziehau if (lc->lro_hash == NULL) { 19805cde7efSSepherosa Ziehau memset(lc, 0, sizeof(*lc)); 19905cde7efSSepherosa Ziehau return (ENOMEM); 20005cde7efSSepherosa Ziehau } 20105cde7efSSepherosa Ziehau 202e936121dSHans Petter Selasky /* compute size to allocate */ 203fc271df3SHans Petter Selasky size = (lro_mbufs * sizeof(struct lro_mbuf_sort)) + 204e936121dSHans Petter Selasky (lro_entries * sizeof(*le)); 205fc271df3SHans Petter Selasky lc->lro_mbuf_data = (struct lro_mbuf_sort *) 206e936121dSHans Petter Selasky malloc(size, M_LRO, M_NOWAIT | M_ZERO); 2076c5087a8SJack F Vogel 208e936121dSHans Petter Selasky /* check for out of memory */ 209e936121dSHans Petter Selasky if (lc->lro_mbuf_data == NULL) { 210a3927369SNavdeep Parhar free(lc->lro_hash, M_LRO); 211e936121dSHans Petter Selasky memset(lc, 0, sizeof(*lc)); 212e936121dSHans Petter Selasky return (ENOMEM); 213e936121dSHans Petter Selasky } 214e936121dSHans Petter Selasky /* compute offset for LRO entries */ 215e936121dSHans Petter Selasky le = (struct lro_entry *) 216e936121dSHans Petter Selasky (lc->lro_mbuf_data + lro_mbufs); 217e936121dSHans Petter Selasky 218e936121dSHans Petter Selasky /* setup linked list */ 219e936121dSHans Petter Selasky for (i = 0; i != lro_entries; i++) 2201ea44822SSepherosa Ziehau LIST_INSERT_HEAD(&lc->lro_free, le + i, next); 221e936121dSHans Petter Selasky 222e936121dSHans Petter Selasky return (0); 2236c5087a8SJack F Vogel } 2246c5087a8SJack F Vogel 2259ca874cfSHans Petter Selasky struct vxlan_header { 2269ca874cfSHans Petter Selasky uint32_t vxlh_flags; 2279ca874cfSHans Petter Selasky uint32_t vxlh_vni; 2289ca874cfSHans Petter Selasky }; 229e57b2d0eSRandall Stewart 2309ca874cfSHans Petter Selasky static inline void * 231*1d171e5aSRandall Stewart tcp_lro_low_level_parser(void *ptr, struct lro_parser *parser, bool update_data, bool is_vxlan, int mlen) 2329ca874cfSHans Petter Selasky { 2339ca874cfSHans Petter Selasky const struct ether_vlan_header *eh; 2349ca874cfSHans Petter Selasky void *old; 2359ca874cfSHans Petter Selasky uint16_t eth_type; 2369ca874cfSHans Petter Selasky 2379ca874cfSHans Petter Selasky if (update_data) 2389ca874cfSHans Petter Selasky memset(parser, 0, sizeof(*parser)); 2399ca874cfSHans Petter Selasky 2409ca874cfSHans Petter Selasky old = ptr; 2419ca874cfSHans Petter Selasky 2429ca874cfSHans Petter Selasky if (is_vxlan) { 2439ca874cfSHans Petter Selasky const struct vxlan_header *vxh; 2449ca874cfSHans Petter Selasky vxh = ptr; 2459ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + sizeof(*vxh); 2469ca874cfSHans Petter Selasky if (update_data) { 2479ca874cfSHans Petter Selasky parser->data.vxlan_vni = 2489ca874cfSHans Petter Selasky vxh->vxlh_vni & htonl(0xffffff00); 249e57b2d0eSRandall Stewart } 2509ca874cfSHans Petter Selasky } 2519ca874cfSHans Petter Selasky 2529ca874cfSHans Petter Selasky eh = ptr; 2539ca874cfSHans Petter Selasky if (__predict_false(eh->evl_encap_proto == htons(ETHERTYPE_VLAN))) { 2549ca874cfSHans Petter Selasky eth_type = eh->evl_proto; 2559ca874cfSHans Petter Selasky if (update_data) { 2569ca874cfSHans Petter Selasky /* strip priority and keep VLAN ID only */ 2579ca874cfSHans Petter Selasky parser->data.vlan_id = eh->evl_tag & htons(EVL_VLID_MASK); 2589ca874cfSHans Petter Selasky } 2599ca874cfSHans Petter Selasky /* advance to next header */ 2609ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 261*1d171e5aSRandall Stewart mlen -= (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN); 2629ca874cfSHans Petter Selasky } else { 2639ca874cfSHans Petter Selasky eth_type = eh->evl_encap_proto; 2649ca874cfSHans Petter Selasky /* advance to next header */ 265*1d171e5aSRandall Stewart mlen -= ETHER_HDR_LEN; 2669ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + ETHER_HDR_LEN; 2679ca874cfSHans Petter Selasky } 268*1d171e5aSRandall Stewart if (__predict_false(mlen <= 0)) 269*1d171e5aSRandall Stewart return (NULL); 2709ca874cfSHans Petter Selasky switch (eth_type) { 2719ca874cfSHans Petter Selasky #ifdef INET 2729ca874cfSHans Petter Selasky case htons(ETHERTYPE_IP): 2739ca874cfSHans Petter Selasky parser->ip4 = ptr; 274*1d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct ip))) 275*1d171e5aSRandall Stewart return (NULL); 2769ca874cfSHans Petter Selasky /* Ensure there are no IPv4 options. */ 2779ca874cfSHans Petter Selasky if ((parser->ip4->ip_hl << 2) != sizeof (*parser->ip4)) 2789ca874cfSHans Petter Selasky break; 2799ca874cfSHans Petter Selasky /* .. and the packet is not fragmented. */ 2809ca874cfSHans Petter Selasky if (parser->ip4->ip_off & htons(IP_MF|IP_OFFMASK)) 2819ca874cfSHans Petter Selasky break; 2829ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->ip4->ip_hl << 2); 283*1d171e5aSRandall Stewart mlen -= sizeof(struct ip); 2849ca874cfSHans Petter Selasky if (update_data) { 2859ca874cfSHans Petter Selasky parser->data.s_addr.v4 = parser->ip4->ip_src; 2869ca874cfSHans Petter Selasky parser->data.d_addr.v4 = parser->ip4->ip_dst; 2879ca874cfSHans Petter Selasky } 2889ca874cfSHans Petter Selasky switch (parser->ip4->ip_p) { 2899ca874cfSHans Petter Selasky case IPPROTO_UDP: 290*1d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct udphdr))) 291*1d171e5aSRandall Stewart return (NULL); 2929ca874cfSHans Petter Selasky parser->udp = ptr; 2939ca874cfSHans Petter Selasky if (update_data) { 2949ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV4_UDP; 2959ca874cfSHans Petter Selasky parser->data.s_port = parser->udp->uh_sport; 2969ca874cfSHans Petter Selasky parser->data.d_port = parser->udp->uh_dport; 2979ca874cfSHans Petter Selasky } else { 2989ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV4_UDP); 2999ca874cfSHans Petter Selasky } 3009ca874cfSHans Petter Selasky ptr = ((uint8_t *)ptr + sizeof(*parser->udp)); 3019ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3029ca874cfSHans Petter Selasky return (ptr); 3039ca874cfSHans Petter Selasky case IPPROTO_TCP: 3049ca874cfSHans Petter Selasky parser->tcp = ptr; 305*1d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct tcphdr))) 306*1d171e5aSRandall Stewart return (NULL); 3079ca874cfSHans Petter Selasky if (update_data) { 3089ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV4_TCP; 3099ca874cfSHans Petter Selasky parser->data.s_port = parser->tcp->th_sport; 3109ca874cfSHans Petter Selasky parser->data.d_port = parser->tcp->th_dport; 3119ca874cfSHans Petter Selasky } else { 3129ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV4_TCP); 3139ca874cfSHans Petter Selasky } 314*1d171e5aSRandall Stewart if (__predict_false(mlen < (parser->tcp->th_off << 2))) 315*1d171e5aSRandall Stewart return (NULL); 3169ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 3179ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3189ca874cfSHans Petter Selasky return (ptr); 3199ca874cfSHans Petter Selasky default: 3209ca874cfSHans Petter Selasky break; 3219ca874cfSHans Petter Selasky } 3229ca874cfSHans Petter Selasky break; 3239ca874cfSHans Petter Selasky #endif 3249ca874cfSHans Petter Selasky #ifdef INET6 3259ca874cfSHans Petter Selasky case htons(ETHERTYPE_IPV6): 3269ca874cfSHans Petter Selasky parser->ip6 = ptr; 327*1d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct ip6_hdr))) 328*1d171e5aSRandall Stewart return (NULL); 3299ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + sizeof(*parser->ip6); 3309ca874cfSHans Petter Selasky if (update_data) { 3319ca874cfSHans Petter Selasky parser->data.s_addr.v6 = parser->ip6->ip6_src; 3329ca874cfSHans Petter Selasky parser->data.d_addr.v6 = parser->ip6->ip6_dst; 3339ca874cfSHans Petter Selasky } 334*1d171e5aSRandall Stewart mlen -= sizeof(struct ip6_hdr); 3359ca874cfSHans Petter Selasky switch (parser->ip6->ip6_nxt) { 3369ca874cfSHans Petter Selasky case IPPROTO_UDP: 337*1d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct udphdr))) 338*1d171e5aSRandall Stewart return (NULL); 3399ca874cfSHans Petter Selasky parser->udp = ptr; 3409ca874cfSHans Petter Selasky if (update_data) { 3419ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV6_UDP; 3429ca874cfSHans Petter Selasky parser->data.s_port = parser->udp->uh_sport; 3439ca874cfSHans Petter Selasky parser->data.d_port = parser->udp->uh_dport; 3449ca874cfSHans Petter Selasky } else { 3459ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV6_UDP); 3469ca874cfSHans Petter Selasky } 3479ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + sizeof(*parser->udp); 3489ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3499ca874cfSHans Petter Selasky return (ptr); 3509ca874cfSHans Petter Selasky case IPPROTO_TCP: 351*1d171e5aSRandall Stewart if (__predict_false(mlen < sizeof(struct tcphdr))) 352*1d171e5aSRandall Stewart return (NULL); 3539ca874cfSHans Petter Selasky parser->tcp = ptr; 3549ca874cfSHans Petter Selasky if (update_data) { 3559ca874cfSHans Petter Selasky parser->data.lro_type = LRO_TYPE_IPV6_TCP; 3569ca874cfSHans Petter Selasky parser->data.s_port = parser->tcp->th_sport; 3579ca874cfSHans Petter Selasky parser->data.d_port = parser->tcp->th_dport; 3589ca874cfSHans Petter Selasky } else { 3599ca874cfSHans Petter Selasky MPASS(parser->data.lro_type == LRO_TYPE_IPV6_TCP); 3609ca874cfSHans Petter Selasky } 361*1d171e5aSRandall Stewart if (__predict_false(mlen < (parser->tcp->th_off << 2))) 362*1d171e5aSRandall Stewart return (NULL); 3639ca874cfSHans Petter Selasky ptr = (uint8_t *)ptr + (parser->tcp->th_off << 2); 3649ca874cfSHans Petter Selasky parser->total_hdr_len = (uint8_t *)ptr - (uint8_t *)old; 3659ca874cfSHans Petter Selasky return (ptr); 3669ca874cfSHans Petter Selasky default: 3679ca874cfSHans Petter Selasky break; 3689ca874cfSHans Petter Selasky } 3699ca874cfSHans Petter Selasky break; 3709ca874cfSHans Petter Selasky #endif 3719ca874cfSHans Petter Selasky default: 3729ca874cfSHans Petter Selasky break; 3739ca874cfSHans Petter Selasky } 3749ca874cfSHans Petter Selasky /* Invalid packet - cannot parse */ 3759ca874cfSHans Petter Selasky return (NULL); 3769ca874cfSHans Petter Selasky } 3779ca874cfSHans Petter Selasky 3789ca874cfSHans Petter Selasky static const int vxlan_csum = CSUM_INNER_L3_CALC | CSUM_INNER_L3_VALID | 3799ca874cfSHans Petter Selasky CSUM_INNER_L4_CALC | CSUM_INNER_L4_VALID; 3809ca874cfSHans Petter Selasky 3819ca874cfSHans Petter Selasky static inline struct lro_parser * 3829ca874cfSHans Petter Selasky tcp_lro_parser(struct mbuf *m, struct lro_parser *po, struct lro_parser *pi, bool update_data) 3839ca874cfSHans Petter Selasky { 3849ca874cfSHans Petter Selasky void *data_ptr; 3859ca874cfSHans Petter Selasky 3869ca874cfSHans Petter Selasky /* Try to parse outer headers first. */ 387*1d171e5aSRandall Stewart data_ptr = tcp_lro_low_level_parser(m->m_data, po, update_data, false, m->m_len); 3889ca874cfSHans Petter Selasky if (data_ptr == NULL || po->total_hdr_len > m->m_len) 3899ca874cfSHans Petter Selasky return (NULL); 3909ca874cfSHans Petter Selasky 3919ca874cfSHans Petter Selasky if (update_data) { 3929ca874cfSHans Petter Selasky /* Store VLAN ID, if any. */ 3939ca874cfSHans Petter Selasky if (__predict_false(m->m_flags & M_VLANTAG)) { 3949ca874cfSHans Petter Selasky po->data.vlan_id = 3959ca874cfSHans Petter Selasky htons(m->m_pkthdr.ether_vtag) & htons(EVL_VLID_MASK); 3969ca874cfSHans Petter Selasky } 3979ca874cfSHans Petter Selasky } 3989ca874cfSHans Petter Selasky 3999ca874cfSHans Petter Selasky switch (po->data.lro_type) { 4009ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_UDP: 4019ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_UDP: 4029ca874cfSHans Petter Selasky /* Check for VXLAN headers. */ 4039ca874cfSHans Petter Selasky if ((m->m_pkthdr.csum_flags & vxlan_csum) != vxlan_csum) 4049ca874cfSHans Petter Selasky break; 4059ca874cfSHans Petter Selasky 4069ca874cfSHans Petter Selasky /* Try to parse inner headers. */ 407*1d171e5aSRandall Stewart data_ptr = tcp_lro_low_level_parser(data_ptr, pi, update_data, true, 408*1d171e5aSRandall Stewart (m->m_len - ((caddr_t)data_ptr - m->m_data))); 409*1d171e5aSRandall Stewart if (data_ptr == NULL || (pi->total_hdr_len + po->total_hdr_len) > m->m_len) 4109ca874cfSHans Petter Selasky break; 4119ca874cfSHans Petter Selasky 4129ca874cfSHans Petter Selasky /* Verify supported header types. */ 4139ca874cfSHans Petter Selasky switch (pi->data.lro_type) { 4149ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4159ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4169ca874cfSHans Petter Selasky return (pi); 4179ca874cfSHans Petter Selasky default: 4189ca874cfSHans Petter Selasky break; 4199ca874cfSHans Petter Selasky } 4209ca874cfSHans Petter Selasky break; 4219ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4229ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4239ca874cfSHans Petter Selasky if (update_data) 4249ca874cfSHans Petter Selasky memset(pi, 0, sizeof(*pi)); 4259ca874cfSHans Petter Selasky return (po); 4269ca874cfSHans Petter Selasky default: 4279ca874cfSHans Petter Selasky break; 4289ca874cfSHans Petter Selasky } 4299ca874cfSHans Petter Selasky return (NULL); 4309ca874cfSHans Petter Selasky } 4319ca874cfSHans Petter Selasky 4329ca874cfSHans Petter Selasky static inline int 4339ca874cfSHans Petter Selasky tcp_lro_trim_mbuf_chain(struct mbuf *m, const struct lro_parser *po) 4349ca874cfSHans Petter Selasky { 4359ca874cfSHans Petter Selasky int len; 4369ca874cfSHans Petter Selasky 4379ca874cfSHans Petter Selasky switch (po->data.lro_type) { 4389ca874cfSHans Petter Selasky #ifdef INET 4399ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 4409ca874cfSHans Petter Selasky len = ((uint8_t *)po->ip4 - (uint8_t *)m->m_data) + 4419ca874cfSHans Petter Selasky ntohs(po->ip4->ip_len); 4429ca874cfSHans Petter Selasky break; 4439ca874cfSHans Petter Selasky #endif 4449ca874cfSHans Petter Selasky #ifdef INET6 4459ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 4469ca874cfSHans Petter Selasky len = ((uint8_t *)po->ip6 - (uint8_t *)m->m_data) + 4479ca874cfSHans Petter Selasky ntohs(po->ip6->ip6_plen) + sizeof(*po->ip6); 4489ca874cfSHans Petter Selasky break; 4499ca874cfSHans Petter Selasky #endif 4509ca874cfSHans Petter Selasky default: 4519ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 4529ca874cfSHans Petter Selasky } 4539ca874cfSHans Petter Selasky 4549ca874cfSHans Petter Selasky /* 4559ca874cfSHans Petter Selasky * If the frame is padded beyond the end of the IP packet, 4569ca874cfSHans Petter Selasky * then trim the extra bytes off: 4579ca874cfSHans Petter Selasky */ 4589ca874cfSHans Petter Selasky if (__predict_true(m->m_pkthdr.len == len)) { 4599ca874cfSHans Petter Selasky return (0); 4609ca874cfSHans Petter Selasky } else if (m->m_pkthdr.len > len) { 4619ca874cfSHans Petter Selasky m_adj(m, len - m->m_pkthdr.len); 4629ca874cfSHans Petter Selasky return (0); 4639ca874cfSHans Petter Selasky } 4649ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 4659ca874cfSHans Petter Selasky } 4669ca874cfSHans Petter Selasky 4679ca874cfSHans Petter Selasky static struct tcphdr * 4689ca874cfSHans Petter Selasky tcp_lro_get_th(struct mbuf *m) 4699ca874cfSHans Petter Selasky { 4709ca874cfSHans Petter Selasky return ((struct tcphdr *)((uint8_t *)m->m_data + m->m_pkthdr.lro_tcp_h_off)); 471e57b2d0eSRandall Stewart } 472e57b2d0eSRandall Stewart 47369a34e8dSRandall Stewart static void 47469a34e8dSRandall Stewart lro_free_mbuf_chain(struct mbuf *m) 47569a34e8dSRandall Stewart { 47669a34e8dSRandall Stewart struct mbuf *save; 47769a34e8dSRandall Stewart 47869a34e8dSRandall Stewart while (m) { 47969a34e8dSRandall Stewart save = m->m_nextpkt; 48069a34e8dSRandall Stewart m->m_nextpkt = NULL; 48169a34e8dSRandall Stewart m_freem(m); 48269a34e8dSRandall Stewart m = save; 48369a34e8dSRandall Stewart } 48469a34e8dSRandall Stewart } 48569a34e8dSRandall Stewart 4866c5087a8SJack F Vogel void 48762b5b6ecSBjoern A. Zeeb tcp_lro_free(struct lro_ctrl *lc) 4886c5087a8SJack F Vogel { 48962b5b6ecSBjoern A. Zeeb struct lro_entry *le; 490e936121dSHans Petter Selasky unsigned x; 4916c5087a8SJack F Vogel 492e936121dSHans Petter Selasky /* reset LRO free list */ 4931ea44822SSepherosa Ziehau LIST_INIT(&lc->lro_free); 494e936121dSHans Petter Selasky 495e936121dSHans Petter Selasky /* free active mbufs, if any */ 4961ea44822SSepherosa Ziehau while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 49751e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 49869a34e8dSRandall Stewart lro_free_mbuf_chain(le->m_head); 4996c5087a8SJack F Vogel } 500e936121dSHans Petter Selasky 50105cde7efSSepherosa Ziehau /* free hash table */ 50205cde7efSSepherosa Ziehau free(lc->lro_hash, M_LRO); 50305cde7efSSepherosa Ziehau lc->lro_hash = NULL; 50405cde7efSSepherosa Ziehau lc->lro_hashsz = 0; 50505cde7efSSepherosa Ziehau 506e936121dSHans Petter Selasky /* free mbuf array, if any */ 507e936121dSHans Petter Selasky for (x = 0; x != lc->lro_mbuf_count; x++) 508fc271df3SHans Petter Selasky m_freem(lc->lro_mbuf_data[x].mb); 509e936121dSHans Petter Selasky lc->lro_mbuf_count = 0; 510e936121dSHans Petter Selasky 511e936121dSHans Petter Selasky /* free allocated memory, if any */ 512e936121dSHans Petter Selasky free(lc->lro_mbuf_data, M_LRO); 513e936121dSHans Petter Selasky lc->lro_mbuf_data = NULL; 5146c5087a8SJack F Vogel } 5156c5087a8SJack F Vogel 51662b5b6ecSBjoern A. Zeeb static uint16_t 5179ca874cfSHans Petter Selasky tcp_lro_rx_csum_tcphdr(const struct tcphdr *th) 51862b5b6ecSBjoern A. Zeeb { 5199ca874cfSHans Petter Selasky const uint16_t *ptr; 5209ca874cfSHans Petter Selasky uint32_t csum; 5219ca874cfSHans Petter Selasky uint16_t len; 52262b5b6ecSBjoern A. Zeeb 5239ca874cfSHans Petter Selasky csum = -th->th_sum; /* exclude checksum field */ 5249ca874cfSHans Petter Selasky len = th->th_off; 5259ca874cfSHans Petter Selasky ptr = (const uint16_t *)th; 5269ca874cfSHans Petter Selasky while (len--) { 5279ca874cfSHans Petter Selasky csum += *ptr; 5289ca874cfSHans Petter Selasky ptr++; 5299ca874cfSHans Petter Selasky csum += *ptr; 5309ca874cfSHans Petter Selasky ptr++; 53162b5b6ecSBjoern A. Zeeb } 5329ca874cfSHans Petter Selasky while (csum > 0xffff) 5339ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 53462b5b6ecSBjoern A. Zeeb 5359ca874cfSHans Petter Selasky return (csum); 53662b5b6ecSBjoern A. Zeeb } 53762b5b6ecSBjoern A. Zeeb 53862b5b6ecSBjoern A. Zeeb static uint16_t 5399ca874cfSHans Petter Selasky tcp_lro_rx_csum_data(const struct lro_parser *pa, uint16_t tcp_csum) 54062b5b6ecSBjoern A. Zeeb { 54162b5b6ecSBjoern A. Zeeb uint32_t c; 54262b5b6ecSBjoern A. Zeeb uint16_t cs; 54362b5b6ecSBjoern A. Zeeb 5449ca874cfSHans Petter Selasky c = tcp_csum; 54562b5b6ecSBjoern A. Zeeb 5469ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 54762b5b6ecSBjoern A. Zeeb #ifdef INET6 5489ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 5499ca874cfSHans Petter Selasky /* Compute full pseudo IPv6 header checksum. */ 5509ca874cfSHans Petter Selasky cs = in6_cksum_pseudo(pa->ip6, ntohs(pa->ip6->ip6_plen), pa->ip6->ip6_nxt, 0); 55162b5b6ecSBjoern A. Zeeb break; 55262b5b6ecSBjoern A. Zeeb #endif 55362b5b6ecSBjoern A. Zeeb #ifdef INET 5549ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 5559ca874cfSHans Petter Selasky /* Compute full pseudo IPv4 header checsum. */ 5569ca874cfSHans Petter Selasky cs = in_addword(ntohs(pa->ip4->ip_len) - sizeof(*pa->ip4), IPPROTO_TCP); 5579ca874cfSHans Petter Selasky cs = in_pseudo(pa->ip4->ip_src.s_addr, pa->ip4->ip_dst.s_addr, htons(cs)); 55862b5b6ecSBjoern A. Zeeb break; 55962b5b6ecSBjoern A. Zeeb #endif 56062b5b6ecSBjoern A. Zeeb default: 56162b5b6ecSBjoern A. Zeeb cs = 0; /* Keep compiler happy. */ 5629ca874cfSHans Petter Selasky break; 56362b5b6ecSBjoern A. Zeeb } 56462b5b6ecSBjoern A. Zeeb 5659ca874cfSHans Petter Selasky /* Complement checksum. */ 56662b5b6ecSBjoern A. Zeeb cs = ~cs; 56762b5b6ecSBjoern A. Zeeb c += cs; 56862b5b6ecSBjoern A. Zeeb 5699ca874cfSHans Petter Selasky /* Remove TCP header checksum. */ 5709ca874cfSHans Petter Selasky cs = ~tcp_lro_rx_csum_tcphdr(pa->tcp); 57162b5b6ecSBjoern A. Zeeb c += cs; 5729ca874cfSHans Petter Selasky 5739ca874cfSHans Petter Selasky /* Compute checksum remainder. */ 57462b5b6ecSBjoern A. Zeeb while (c > 0xffff) 57562b5b6ecSBjoern A. Zeeb c = (c >> 16) + (c & 0xffff); 57662b5b6ecSBjoern A. Zeeb 5779ca874cfSHans Petter Selasky return (c); 57862b5b6ecSBjoern A. Zeeb } 57962b5b6ecSBjoern A. Zeeb 5806dd38b87SSepherosa Ziehau static void 5816dd38b87SSepherosa Ziehau tcp_lro_rx_done(struct lro_ctrl *lc) 5826dd38b87SSepherosa Ziehau { 5836dd38b87SSepherosa Ziehau struct lro_entry *le; 5846dd38b87SSepherosa Ziehau 5851ea44822SSepherosa Ziehau while ((le = LIST_FIRST(&lc->lro_active)) != NULL) { 58651e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 5876dd38b87SSepherosa Ziehau tcp_lro_flush(lc, le); 5886dd38b87SSepherosa Ziehau } 5896dd38b87SSepherosa Ziehau } 5906dd38b87SSepherosa Ziehau 5916c5087a8SJack F Vogel void 5927127e6acSNavdeep Parhar tcp_lro_flush_inactive(struct lro_ctrl *lc, const struct timeval *timeout) 5937127e6acSNavdeep Parhar { 5947127e6acSNavdeep Parhar struct lro_entry *le, *le_tmp; 595b45daaeaSRandall Stewart uint64_t now, tov; 596b45daaeaSRandall Stewart struct bintime bt; 5977127e6acSNavdeep Parhar 5981ea44822SSepherosa Ziehau if (LIST_EMPTY(&lc->lro_active)) 5997127e6acSNavdeep Parhar return; 6007127e6acSNavdeep Parhar 601b45daaeaSRandall Stewart /* get timeout time and current time in ns */ 602b45daaeaSRandall Stewart binuptime(&bt); 603b45daaeaSRandall Stewart now = bintime2ns(&bt); 604b45daaeaSRandall Stewart tov = ((timeout->tv_sec * 1000000000) + (timeout->tv_usec * 1000)); 6051ea44822SSepherosa Ziehau LIST_FOREACH_SAFE(le, &lc->lro_active, next, le_tmp) { 606b45daaeaSRandall Stewart if (now >= (bintime2ns(&le->alloc_time) + tov)) { 60751e3c20dSSepherosa Ziehau tcp_lro_active_remove(le); 6087127e6acSNavdeep Parhar tcp_lro_flush(lc, le); 6097127e6acSNavdeep Parhar } 6107127e6acSNavdeep Parhar } 6117127e6acSNavdeep Parhar } 6127127e6acSNavdeep Parhar 613e57b2d0eSRandall Stewart #ifdef INET 614e57b2d0eSRandall Stewart static int 6159ca874cfSHans Petter Selasky tcp_lro_rx_ipv4(struct lro_ctrl *lc, struct mbuf *m, struct ip *ip4) 616e57b2d0eSRandall Stewart { 617e57b2d0eSRandall Stewart uint16_t csum; 618e57b2d0eSRandall Stewart 619e57b2d0eSRandall Stewart /* Legacy IP has a header checksum that needs to be correct. */ 6209ca874cfSHans Petter Selasky if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 6219ca874cfSHans Petter Selasky if (__predict_false((m->m_pkthdr.csum_flags & CSUM_IP_VALID) == 0)) { 622e57b2d0eSRandall Stewart lc->lro_bad_csum++; 623e57b2d0eSRandall Stewart return (TCP_LRO_CANNOT); 624e57b2d0eSRandall Stewart } 625e57b2d0eSRandall Stewart } else { 626e57b2d0eSRandall Stewart csum = in_cksum_hdr(ip4); 6279ca874cfSHans Petter Selasky if (__predict_false(csum != 0)) { 628e57b2d0eSRandall Stewart lc->lro_bad_csum++; 629e57b2d0eSRandall Stewart return (TCP_LRO_CANNOT); 630e57b2d0eSRandall Stewart } 631e57b2d0eSRandall Stewart } 632e57b2d0eSRandall Stewart return (0); 633e57b2d0eSRandall Stewart } 634e57b2d0eSRandall Stewart #endif 635e57b2d0eSRandall Stewart 6360a4f8510SRandall Stewart #ifdef TCPHPTS 637e57b2d0eSRandall Stewart static void 6389ca874cfSHans Petter Selasky tcp_lro_log(struct tcpcb *tp, const struct lro_ctrl *lc, 6399ca874cfSHans Petter Selasky const struct lro_entry *le, const struct mbuf *m, 6409ca874cfSHans Petter Selasky int frm, int32_t tcp_data_len, uint32_t th_seq, 6419ca874cfSHans Petter Selasky uint32_t th_ack, uint16_t th_win) 642e57b2d0eSRandall Stewart { 643e57b2d0eSRandall Stewart if (tp->t_logstate != TCP_LOG_STATE_OFF) { 644e57b2d0eSRandall Stewart union tcp_log_stackspecific log; 645b45daaeaSRandall Stewart struct timeval tv, btv; 646e57b2d0eSRandall Stewart uint32_t cts; 647e57b2d0eSRandall Stewart 648e57b2d0eSRandall Stewart cts = tcp_get_usecs(&tv); 649e57b2d0eSRandall Stewart memset(&log, 0, sizeof(union tcp_log_stackspecific)); 650e57b2d0eSRandall Stewart log.u_bbr.flex8 = frm; 651e57b2d0eSRandall Stewart log.u_bbr.flex1 = tcp_data_len; 652e57b2d0eSRandall Stewart if (m) 653e57b2d0eSRandall Stewart log.u_bbr.flex2 = m->m_pkthdr.len; 654e57b2d0eSRandall Stewart else 655e57b2d0eSRandall Stewart log.u_bbr.flex2 = 0; 6569ca874cfSHans Petter Selasky log.u_bbr.flex3 = le->m_head->m_pkthdr.lro_nsegs; 6579ca874cfSHans Petter Selasky log.u_bbr.flex4 = le->m_head->m_pkthdr.lro_tcp_d_len; 65869a34e8dSRandall Stewart if (le->m_head) { 659e57b2d0eSRandall Stewart log.u_bbr.flex5 = le->m_head->m_pkthdr.len; 660e57b2d0eSRandall Stewart log.u_bbr.delRate = le->m_head->m_flags; 661e57b2d0eSRandall Stewart log.u_bbr.rttProp = le->m_head->m_pkthdr.rcv_tstmp; 66269a34e8dSRandall Stewart } 663e57b2d0eSRandall Stewart log.u_bbr.inflight = th_seq; 664d7955cc0SRandall Stewart log.u_bbr.delivered = th_ack; 665e57b2d0eSRandall Stewart log.u_bbr.timeStamp = cts; 666e57b2d0eSRandall Stewart log.u_bbr.epoch = le->next_seq; 667e57b2d0eSRandall Stewart log.u_bbr.lt_epoch = le->ack_seq; 668e57b2d0eSRandall Stewart log.u_bbr.pacing_gain = th_win; 669e57b2d0eSRandall Stewart log.u_bbr.cwnd_gain = le->window; 670d7955cc0SRandall Stewart log.u_bbr.lost = curcpu; 671b23b156eSWarner Losh log.u_bbr.cur_del_rate = (uintptr_t)m; 672b23b156eSWarner Losh log.u_bbr.bw_inuse = (uintptr_t)le->m_head; 673b45daaeaSRandall Stewart bintime2timeval(&lc->lro_last_queue_time, &btv); 674b45daaeaSRandall Stewart log.u_bbr.flex6 = tcp_tv_to_usectick(&btv); 6759ca874cfSHans Petter Selasky log.u_bbr.flex7 = le->compressed; 6769ca874cfSHans Petter Selasky log.u_bbr.pacing_gain = le->uncompressed; 67769a34e8dSRandall Stewart if (in_epoch(net_epoch_preempt)) 67869a34e8dSRandall Stewart log.u_bbr.inhpts = 1; 67969a34e8dSRandall Stewart else 68069a34e8dSRandall Stewart log.u_bbr.inhpts = 0; 681e57b2d0eSRandall Stewart TCP_LOG_EVENTP(tp, NULL, 682e57b2d0eSRandall Stewart &tp->t_inpcb->inp_socket->so_rcv, 683e57b2d0eSRandall Stewart &tp->t_inpcb->inp_socket->so_snd, 684e57b2d0eSRandall Stewart TCP_LOG_LRO, 0, 685e57b2d0eSRandall Stewart 0, &log, false, &tv); 686e57b2d0eSRandall Stewart } 687e57b2d0eSRandall Stewart } 6880a4f8510SRandall Stewart #endif 689e57b2d0eSRandall Stewart 6909ca874cfSHans Petter Selasky static inline void 6919ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(uint16_t *ptr, uint16_t value, uint16_t *psum) 692e57b2d0eSRandall Stewart { 6939ca874cfSHans Petter Selasky uint32_t csum; 6946c5087a8SJack F Vogel 6959ca874cfSHans Petter Selasky csum = 0xffff - *ptr + value; 6969ca874cfSHans Petter Selasky while (csum > 0xffff) 6979ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 6989ca874cfSHans Petter Selasky *ptr = value; 6999ca874cfSHans Petter Selasky *psum = csum; 70062b5b6ecSBjoern A. Zeeb } 70162b5b6ecSBjoern A. Zeeb 7029ca874cfSHans Petter Selasky static uint16_t 7039ca874cfSHans Petter Selasky tcp_lro_update_checksum(const struct lro_parser *pa, const struct lro_entry *le, 7049ca874cfSHans Petter Selasky uint16_t payload_len, uint16_t delta_sum) 7059ca874cfSHans Petter Selasky { 7069ca874cfSHans Petter Selasky uint32_t csum; 7079ca874cfSHans Petter Selasky uint16_t tlen; 7089ca874cfSHans Petter Selasky uint16_t temp[5] = {}; 7099ca874cfSHans Petter Selasky 7109ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 7119ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 7129ca874cfSHans Petter Selasky /* Compute new IPv4 length. */ 7139ca874cfSHans Petter Selasky tlen = (pa->ip4->ip_hl << 2) + (pa->tcp->th_off << 2) + payload_len; 7149ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 7159ca874cfSHans Petter Selasky 7169ca874cfSHans Petter Selasky /* Subtract delta from current IPv4 checksum. */ 7179ca874cfSHans Petter Selasky csum = pa->ip4->ip_sum + 0xffff - temp[0]; 7189ca874cfSHans Petter Selasky while (csum > 0xffff) 7199ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7209ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 7219ca874cfSHans Petter Selasky goto update_tcp_header; 7229ca874cfSHans Petter Selasky 7239ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 7249ca874cfSHans Petter Selasky /* Compute new IPv6 length. */ 7259ca874cfSHans Petter Selasky tlen = (pa->tcp->th_off << 2) + payload_len; 7269ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 7279ca874cfSHans Petter Selasky goto update_tcp_header; 7289ca874cfSHans Petter Selasky 7299ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_UDP: 7309ca874cfSHans Petter Selasky /* Compute new IPv4 length. */ 7319ca874cfSHans Petter Selasky tlen = (pa->ip4->ip_hl << 2) + sizeof(*pa->udp) + payload_len; 7329ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_len, htons(tlen), &temp[0]); 7339ca874cfSHans Petter Selasky 7349ca874cfSHans Petter Selasky /* Subtract delta from current IPv4 checksum. */ 7359ca874cfSHans Petter Selasky csum = pa->ip4->ip_sum + 0xffff - temp[0]; 7369ca874cfSHans Petter Selasky while (csum > 0xffff) 7379ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7389ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip4->ip_sum, csum, &temp[1]); 7399ca874cfSHans Petter Selasky goto update_udp_header; 7409ca874cfSHans Petter Selasky 7419ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_UDP: 7429ca874cfSHans Petter Selasky /* Compute new IPv6 length. */ 7439ca874cfSHans Petter Selasky tlen = sizeof(*pa->udp) + payload_len; 7449ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->ip6->ip6_plen, htons(tlen), &temp[0]); 7459ca874cfSHans Petter Selasky goto update_udp_header; 7469ca874cfSHans Petter Selasky 74762b5b6ecSBjoern A. Zeeb default: 7489ca874cfSHans Petter Selasky return (0); 74962b5b6ecSBjoern A. Zeeb } 7509ca874cfSHans Petter Selasky 7519ca874cfSHans Petter Selasky update_tcp_header: 7529ca874cfSHans Petter Selasky /* Compute current TCP header checksum. */ 7539ca874cfSHans Petter Selasky temp[2] = tcp_lro_rx_csum_tcphdr(pa->tcp); 75462b5b6ecSBjoern A. Zeeb 75562b5b6ecSBjoern A. Zeeb /* Incorporate the latest ACK into the TCP header. */ 7569ca874cfSHans Petter Selasky pa->tcp->th_ack = le->ack_seq; 7579ca874cfSHans Petter Selasky pa->tcp->th_win = le->window; 7589ca874cfSHans Petter Selasky 75962b5b6ecSBjoern A. Zeeb /* Incorporate latest timestamp into the TCP header. */ 76062b5b6ecSBjoern A. Zeeb if (le->timestamp != 0) { 7616c5087a8SJack F Vogel uint32_t *ts_ptr; 7626c5087a8SJack F Vogel 7639ca874cfSHans Petter Selasky ts_ptr = (uint32_t *)(pa->tcp + 1); 76462b5b6ecSBjoern A. Zeeb ts_ptr[1] = htonl(le->tsval); 76562b5b6ecSBjoern A. Zeeb ts_ptr[2] = le->tsecr; 76662b5b6ecSBjoern A. Zeeb } 7679ca874cfSHans Petter Selasky 7689ca874cfSHans Petter Selasky /* Compute new TCP header checksum. */ 7699ca874cfSHans Petter Selasky temp[3] = tcp_lro_rx_csum_tcphdr(pa->tcp); 7709ca874cfSHans Petter Selasky 7719ca874cfSHans Petter Selasky /* Compute new TCP checksum. */ 7729ca874cfSHans Petter Selasky csum = pa->tcp->th_sum + 0xffff - delta_sum + 7739ca874cfSHans Petter Selasky 0xffff - temp[0] + 0xffff - temp[3] + temp[2]; 7749ca874cfSHans Petter Selasky while (csum > 0xffff) 7759ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7769ca874cfSHans Petter Selasky 7779ca874cfSHans Petter Selasky /* Assign new TCP checksum. */ 7789ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->tcp->th_sum, csum, &temp[4]); 7799ca874cfSHans Petter Selasky 7809ca874cfSHans Petter Selasky /* Compute all modififications affecting next checksum. */ 7819ca874cfSHans Petter Selasky csum = temp[0] + temp[1] + 0xffff - temp[2] + 7829ca874cfSHans Petter Selasky temp[3] + temp[4] + delta_sum; 7839ca874cfSHans Petter Selasky while (csum > 0xffff) 7849ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 7859ca874cfSHans Petter Selasky 7869ca874cfSHans Petter Selasky /* Return delta checksum to next stage, if any. */ 7879ca874cfSHans Petter Selasky return (csum); 7889ca874cfSHans Petter Selasky 7899ca874cfSHans Petter Selasky update_udp_header: 7909ca874cfSHans Petter Selasky tlen = sizeof(*pa->udp) + payload_len; 7919ca874cfSHans Petter Selasky /* Assign new UDP length and compute checksum delta. */ 7929ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->udp->uh_ulen, htons(tlen), &temp[2]); 7939ca874cfSHans Petter Selasky 7949ca874cfSHans Petter Selasky /* Check if there is a UDP checksum. */ 7959ca874cfSHans Petter Selasky if (__predict_false(pa->udp->uh_sum != 0)) { 7969ca874cfSHans Petter Selasky /* Compute new UDP checksum. */ 7979ca874cfSHans Petter Selasky csum = pa->udp->uh_sum + 0xffff - delta_sum + 7989ca874cfSHans Petter Selasky 0xffff - temp[0] + 0xffff - temp[2]; 7999ca874cfSHans Petter Selasky while (csum > 0xffff) 8009ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 8019ca874cfSHans Petter Selasky /* Assign new UDP checksum. */ 8029ca874cfSHans Petter Selasky tcp_lro_assign_and_checksum_16(&pa->udp->uh_sum, csum, &temp[3]); 803e57b2d0eSRandall Stewart } 8049ca874cfSHans Petter Selasky 8059ca874cfSHans Petter Selasky /* Compute all modififications affecting next checksum. */ 8069ca874cfSHans Petter Selasky csum = temp[0] + temp[1] + temp[2] + temp[3] + delta_sum; 8079ca874cfSHans Petter Selasky while (csum > 0xffff) 8089ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 8099ca874cfSHans Petter Selasky 8109ca874cfSHans Petter Selasky /* Return delta checksum to next stage, if any. */ 8119ca874cfSHans Petter Selasky return (csum); 8129ca874cfSHans Petter Selasky } 8139ca874cfSHans Petter Selasky 8149ca874cfSHans Petter Selasky static void 8159ca874cfSHans Petter Selasky tcp_flush_out_entry(struct lro_ctrl *lc, struct lro_entry *le) 8169ca874cfSHans Petter Selasky { 8179ca874cfSHans Petter Selasky /* Check if we need to recompute any checksums. */ 8189ca874cfSHans Petter Selasky if (le->m_head->m_pkthdr.lro_nsegs > 1) { 8199ca874cfSHans Petter Selasky uint16_t csum; 8209ca874cfSHans Petter Selasky 8219ca874cfSHans Petter Selasky switch (le->inner.data.lro_type) { 8229ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 8239ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->inner, le, 8249ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8259ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8269ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, NULL, 8279ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len + 8289ca874cfSHans Petter Selasky le->inner.total_hdr_len, csum); 8299ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8309ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 8319ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 8329ca874cfSHans Petter Selasky break; 8339ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 8349ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->inner, le, 8359ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8369ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8379ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, NULL, 8389ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len + 8399ca874cfSHans Petter Selasky le->inner.total_hdr_len, csum); 8409ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8419ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR; 8429ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 8439ca874cfSHans Petter Selasky break; 8449ca874cfSHans Petter Selasky case LRO_TYPE_NONE: 8459ca874cfSHans Petter Selasky switch (le->outer.data.lro_type) { 8469ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 8479ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, le, 8489ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8499ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8509ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8519ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR | CSUM_IP_CHECKED | CSUM_IP_VALID; 8529ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 8539ca874cfSHans Petter Selasky break; 8549ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 8559ca874cfSHans Petter Selasky csum = tcp_lro_update_checksum(&le->outer, le, 8569ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_len, 8579ca874cfSHans Petter Selasky le->m_head->m_pkthdr.lro_tcp_d_csum); 8589ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_flags = CSUM_DATA_VALID | 8599ca874cfSHans Petter Selasky CSUM_PSEUDO_HDR; 8609ca874cfSHans Petter Selasky le->m_head->m_pkthdr.csum_data = 0xffff; 8619ca874cfSHans Petter Selasky break; 8629ca874cfSHans Petter Selasky default: 8639ca874cfSHans Petter Selasky break; 8649ca874cfSHans Petter Selasky } 8659ca874cfSHans Petter Selasky break; 8669ca874cfSHans Petter Selasky default: 8679ca874cfSHans Petter Selasky break; 8689ca874cfSHans Petter Selasky } 8699ca874cfSHans Petter Selasky } 8709ca874cfSHans Petter Selasky 871e57b2d0eSRandall Stewart /* 872e57b2d0eSRandall Stewart * Break any chain, this is not set to NULL on the singleton 873e57b2d0eSRandall Stewart * case m_nextpkt points to m_head. Other case set them 874e57b2d0eSRandall Stewart * m_nextpkt to NULL in push_and_replace. 875e57b2d0eSRandall Stewart */ 876e57b2d0eSRandall Stewart le->m_head->m_nextpkt = NULL; 8779ca874cfSHans Petter Selasky lc->lro_queued += le->m_head->m_pkthdr.lro_nsegs; 878e57b2d0eSRandall Stewart (*lc->ifp->if_input)(lc->ifp, le->m_head); 87962b5b6ecSBjoern A. Zeeb } 8806c5087a8SJack F Vogel 881e57b2d0eSRandall Stewart static void 8829ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(struct lro_ctrl *lc, struct lro_entry *le, 8839ca874cfSHans Petter Selasky struct mbuf *m, struct tcphdr *th) 884e57b2d0eSRandall Stewart { 885e57b2d0eSRandall Stewart uint32_t *ts_ptr; 886e57b2d0eSRandall Stewart uint16_t tcp_data_len; 8879ca874cfSHans Petter Selasky uint16_t tcp_opt_len; 888e57b2d0eSRandall Stewart 889e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 8909ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 8919ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 8929ca874cfSHans Petter Selasky 8939ca874cfSHans Petter Selasky /* Check if there is a timestamp option. */ 8949ca874cfSHans Petter Selasky if (tcp_opt_len == 0 || 8959ca874cfSHans Petter Selasky __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 8969ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 8979ca874cfSHans Petter Selasky /* We failed to find the timestamp option. */ 8989ca874cfSHans Petter Selasky le->timestamp = 0; 8999ca874cfSHans Petter Selasky } else { 900e57b2d0eSRandall Stewart le->timestamp = 1; 901e57b2d0eSRandall Stewart le->tsval = ntohl(*(ts_ptr + 1)); 902e57b2d0eSRandall Stewart le->tsecr = *(ts_ptr + 2); 9039ca874cfSHans Petter Selasky } 9049ca874cfSHans Petter Selasky 9059ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 9069ca874cfSHans Petter Selasky 9079ca874cfSHans Petter Selasky /* Pull out TCP sequence numbers and window size. */ 908e57b2d0eSRandall Stewart le->next_seq = ntohl(th->th_seq) + tcp_data_len; 909e57b2d0eSRandall Stewart le->ack_seq = th->th_ack; 910e57b2d0eSRandall Stewart le->window = th->th_win; 9119ca874cfSHans Petter Selasky 9129ca874cfSHans Petter Selasky /* Setup new data pointers. */ 913e57b2d0eSRandall Stewart le->m_head = m; 914e57b2d0eSRandall Stewart le->m_tail = m_last(m); 915e57b2d0eSRandall Stewart } 916e57b2d0eSRandall Stewart 917e57b2d0eSRandall Stewart static void 9189ca874cfSHans Petter Selasky tcp_push_and_replace(struct lro_ctrl *lc, struct lro_entry *le, struct mbuf *m) 919e57b2d0eSRandall Stewart { 9209ca874cfSHans Petter Selasky struct lro_parser *pa; 9219ca874cfSHans Petter Selasky 922e57b2d0eSRandall Stewart /* 9239ca874cfSHans Petter Selasky * Push up the stack of the current entry 9249ca874cfSHans Petter Selasky * and replace it with "m". 925e57b2d0eSRandall Stewart */ 926e57b2d0eSRandall Stewart struct mbuf *msave; 927e57b2d0eSRandall Stewart 928e57b2d0eSRandall Stewart /* Grab off the next and save it */ 929e57b2d0eSRandall Stewart msave = le->m_head->m_nextpkt; 930e57b2d0eSRandall Stewart le->m_head->m_nextpkt = NULL; 9319ca874cfSHans Petter Selasky 9329ca874cfSHans Petter Selasky /* Now push out the old entry */ 9339ca874cfSHans Petter Selasky tcp_flush_out_entry(lc, le); 9349ca874cfSHans Petter Selasky 9359ca874cfSHans Petter Selasky /* Re-parse new header, should not fail. */ 9369ca874cfSHans Petter Selasky pa = tcp_lro_parser(m, &le->outer, &le->inner, false); 9379ca874cfSHans Petter Selasky KASSERT(pa != NULL, 9389ca874cfSHans Petter Selasky ("tcp_push_and_replace: LRO parser failed on m=%p\n", m)); 9399ca874cfSHans Petter Selasky 940e57b2d0eSRandall Stewart /* 9419ca874cfSHans Petter Selasky * Now to replace the data properly in the entry 9429ca874cfSHans Petter Selasky * we have to reset the TCP header and 943e57b2d0eSRandall Stewart * other fields. 944e57b2d0eSRandall Stewart */ 9459ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(lc, le, m, pa->tcp); 9469ca874cfSHans Petter Selasky 947e57b2d0eSRandall Stewart /* Restore the next list */ 948e57b2d0eSRandall Stewart m->m_nextpkt = msave; 949e57b2d0eSRandall Stewart } 950e57b2d0eSRandall Stewart 951e57b2d0eSRandall Stewart static void 9529ca874cfSHans Petter Selasky tcp_lro_mbuf_append_pkthdr(struct mbuf *m, const struct mbuf *p) 9539ca874cfSHans Petter Selasky { 9549ca874cfSHans Petter Selasky uint32_t csum; 9559ca874cfSHans Petter Selasky 9569ca874cfSHans Petter Selasky if (m->m_pkthdr.lro_nsegs == 1) { 9579ca874cfSHans Petter Selasky /* Compute relative checksum. */ 9589ca874cfSHans Petter Selasky csum = p->m_pkthdr.lro_tcp_d_csum; 9599ca874cfSHans Petter Selasky } else { 9609ca874cfSHans Petter Selasky /* Merge TCP data checksums. */ 9619ca874cfSHans Petter Selasky csum = (uint32_t)m->m_pkthdr.lro_tcp_d_csum + 9629ca874cfSHans Petter Selasky (uint32_t)p->m_pkthdr.lro_tcp_d_csum; 9639ca874cfSHans Petter Selasky while (csum > 0xffff) 9649ca874cfSHans Petter Selasky csum = (csum >> 16) + (csum & 0xffff); 9659ca874cfSHans Petter Selasky } 9669ca874cfSHans Petter Selasky 9679ca874cfSHans Petter Selasky /* Update various counters. */ 9689ca874cfSHans Petter Selasky m->m_pkthdr.len += p->m_pkthdr.lro_tcp_d_len; 9699ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_csum = csum; 9709ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_len += p->m_pkthdr.lro_tcp_d_len; 9719ca874cfSHans Petter Selasky m->m_pkthdr.lro_nsegs += p->m_pkthdr.lro_nsegs; 9729ca874cfSHans Petter Selasky } 9739ca874cfSHans Petter Selasky 9749ca874cfSHans Petter Selasky static void 9759ca874cfSHans Petter Selasky tcp_lro_condense(struct lro_ctrl *lc, struct lro_entry *le) 976e57b2d0eSRandall Stewart { 977e57b2d0eSRandall Stewart /* 978e57b2d0eSRandall Stewart * Walk through the mbuf chain we 979e57b2d0eSRandall Stewart * have on tap and compress/condense 980e57b2d0eSRandall Stewart * as required. 981e57b2d0eSRandall Stewart */ 982e57b2d0eSRandall Stewart uint32_t *ts_ptr; 983e57b2d0eSRandall Stewart struct mbuf *m; 984e57b2d0eSRandall Stewart struct tcphdr *th; 9859ca874cfSHans Petter Selasky uint32_t tcp_data_len_total; 9869ca874cfSHans Petter Selasky uint32_t tcp_data_seg_total; 9879ca874cfSHans Petter Selasky uint16_t tcp_data_len; 9889ca874cfSHans Petter Selasky uint16_t tcp_opt_len; 989e57b2d0eSRandall Stewart 990e57b2d0eSRandall Stewart /* 991e57b2d0eSRandall Stewart * First we must check the lead (m_head) 992e57b2d0eSRandall Stewart * we must make sure that it is *not* 993e57b2d0eSRandall Stewart * something that should be sent up 994e57b2d0eSRandall Stewart * right away (sack etc). 995e57b2d0eSRandall Stewart */ 996e57b2d0eSRandall Stewart again: 997e57b2d0eSRandall Stewart m = le->m_head->m_nextpkt; 998e57b2d0eSRandall Stewart if (m == NULL) { 9999ca874cfSHans Petter Selasky /* Just one left. */ 1000e57b2d0eSRandall Stewart return; 1001e57b2d0eSRandall Stewart } 10029ca874cfSHans Petter Selasky 10039ca874cfSHans Petter Selasky th = tcp_lro_get_th(m); 10049ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 10059ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 1006e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 10079ca874cfSHans Petter Selasky 10089ca874cfSHans Petter Selasky if (tcp_opt_len != 0 && __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 10099ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 1010e57b2d0eSRandall Stewart /* 1011e57b2d0eSRandall Stewart * Its not the timestamp. We can't 1012e57b2d0eSRandall Stewart * use this guy as the head. 1013e57b2d0eSRandall Stewart */ 1014e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 10159ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1016e57b2d0eSRandall Stewart goto again; 1017e57b2d0eSRandall Stewart } 1018e57b2d0eSRandall Stewart if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) { 1019e57b2d0eSRandall Stewart /* 1020e57b2d0eSRandall Stewart * Make sure that previously seen segements/ACKs are delivered 1021e57b2d0eSRandall Stewart * before this segment, e.g. FIN. 1022e57b2d0eSRandall Stewart */ 1023e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 10249ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1025e57b2d0eSRandall Stewart goto again; 1026e57b2d0eSRandall Stewart } 1027e57b2d0eSRandall Stewart while((m = le->m_head->m_nextpkt) != NULL) { 1028e57b2d0eSRandall Stewart /* 1029e57b2d0eSRandall Stewart * condense m into le, first 1030e57b2d0eSRandall Stewart * pull m out of the list. 1031e57b2d0eSRandall Stewart */ 1032e57b2d0eSRandall Stewart le->m_head->m_nextpkt = m->m_nextpkt; 1033e57b2d0eSRandall Stewart m->m_nextpkt = NULL; 1034e57b2d0eSRandall Stewart /* Setup my data */ 10359ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.lro_tcp_d_len; 10369ca874cfSHans Petter Selasky th = tcp_lro_get_th(m); 1037e57b2d0eSRandall Stewart ts_ptr = (uint32_t *)(th + 1); 10389ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 10399ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 10409ca874cfSHans Petter Selasky tcp_data_len_total = le->m_head->m_pkthdr.lro_tcp_d_len + tcp_data_len; 10419ca874cfSHans Petter Selasky tcp_data_seg_total = le->m_head->m_pkthdr.lro_nsegs + m->m_pkthdr.lro_nsegs; 10429ca874cfSHans Petter Selasky 10439ca874cfSHans Petter Selasky if (tcp_data_seg_total >= lc->lro_ackcnt_lim || 10449ca874cfSHans Petter Selasky tcp_data_len_total >= lc->lro_length_lim) { 1045e57b2d0eSRandall Stewart /* Flush now if appending will result in overflow. */ 10469ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1047e57b2d0eSRandall Stewart goto again; 1048e57b2d0eSRandall Stewart } 10499ca874cfSHans Petter Selasky if (tcp_opt_len != 0 && 10509ca874cfSHans Petter Selasky __predict_false(tcp_opt_len != TCPOLEN_TSTAMP_APPA || 10519ca874cfSHans Petter Selasky *ts_ptr != TCP_LRO_TS_OPTION)) { 1052e57b2d0eSRandall Stewart /* 1053e57b2d0eSRandall Stewart * Maybe a sack in the new one? We need to 1054e57b2d0eSRandall Stewart * start all over after flushing the 1055e57b2d0eSRandall Stewart * current le. We will go up to the beginning 1056e57b2d0eSRandall Stewart * and flush it (calling the replace again possibly 1057e57b2d0eSRandall Stewart * or just returning). 1058e57b2d0eSRandall Stewart */ 10599ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1060e57b2d0eSRandall Stewart goto again; 1061e57b2d0eSRandall Stewart } 1062e57b2d0eSRandall Stewart if ((th->th_flags & ~(TH_ACK | TH_PUSH)) != 0) { 10639ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1064e57b2d0eSRandall Stewart goto again; 1065e57b2d0eSRandall Stewart } 10669ca874cfSHans Petter Selasky if (tcp_opt_len != 0) { 1067e57b2d0eSRandall Stewart uint32_t tsval = ntohl(*(ts_ptr + 1)); 1068e57b2d0eSRandall Stewart /* Make sure timestamp values are increasing. */ 1069e57b2d0eSRandall Stewart if (TSTMP_GT(le->tsval, tsval)) { 10709ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1071e57b2d0eSRandall Stewart goto again; 1072e57b2d0eSRandall Stewart } 1073e57b2d0eSRandall Stewart le->tsval = tsval; 1074e57b2d0eSRandall Stewart le->tsecr = *(ts_ptr + 2); 1075e57b2d0eSRandall Stewart } 1076e57b2d0eSRandall Stewart /* Try to append the new segment. */ 1077e57b2d0eSRandall Stewart if (__predict_false(ntohl(th->th_seq) != le->next_seq || 1078e57b2d0eSRandall Stewart (tcp_data_len == 0 && 1079e57b2d0eSRandall Stewart le->ack_seq == th->th_ack && 1080e57b2d0eSRandall Stewart le->window == th->th_win))) { 1081e57b2d0eSRandall Stewart /* Out of order packet or duplicate ACK. */ 10829ca874cfSHans Petter Selasky tcp_push_and_replace(lc, le, m); 1083e57b2d0eSRandall Stewart goto again; 1084e57b2d0eSRandall Stewart } 10859ca874cfSHans Petter Selasky if (tcp_data_len != 0 || 10869ca874cfSHans Petter Selasky SEQ_GT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1087e57b2d0eSRandall Stewart le->next_seq += tcp_data_len; 1088e57b2d0eSRandall Stewart le->ack_seq = th->th_ack; 1089e57b2d0eSRandall Stewart le->window = th->th_win; 1090e57b2d0eSRandall Stewart } else if (th->th_ack == le->ack_seq) { 1091e57b2d0eSRandall Stewart le->window = WIN_MAX(le->window, th->th_win); 1092e57b2d0eSRandall Stewart } 10939ca874cfSHans Petter Selasky 1094e57b2d0eSRandall Stewart if (tcp_data_len == 0) { 1095e57b2d0eSRandall Stewart m_freem(m); 1096e57b2d0eSRandall Stewart continue; 1097e57b2d0eSRandall Stewart } 10989ca874cfSHans Petter Selasky 10999ca874cfSHans Petter Selasky /* Merge TCP data checksum and length to head mbuf. */ 11009ca874cfSHans Petter Selasky tcp_lro_mbuf_append_pkthdr(le->m_head, m); 11019ca874cfSHans Petter Selasky 1102e57b2d0eSRandall Stewart /* 1103e57b2d0eSRandall Stewart * Adjust the mbuf so that m_data points to the first byte of 1104e57b2d0eSRandall Stewart * the ULP payload. Adjust the mbuf to avoid complications and 1105e57b2d0eSRandall Stewart * append new segment to existing mbuf chain. 1106e57b2d0eSRandall Stewart */ 1107e57b2d0eSRandall Stewart m_adj(m, m->m_pkthdr.len - tcp_data_len); 1108e57b2d0eSRandall Stewart m_demote_pkthdr(m); 1109e57b2d0eSRandall Stewart le->m_tail->m_next = m; 1110e57b2d0eSRandall Stewart le->m_tail = m_last(m); 1111e57b2d0eSRandall Stewart } 1112e57b2d0eSRandall Stewart } 1113e57b2d0eSRandall Stewart 1114373013b0SConrad Meyer #ifdef TCPHPTS 1115e57b2d0eSRandall Stewart static void 11169ca874cfSHans Petter Selasky tcp_queue_pkts(struct inpcb *inp, struct tcpcb *tp, struct lro_entry *le) 1117e57b2d0eSRandall Stewart { 11189ca874cfSHans Petter Selasky INP_WLOCK_ASSERT(inp); 1119e57b2d0eSRandall Stewart if (tp->t_in_pkt == NULL) { 1120e57b2d0eSRandall Stewart /* Nothing yet there */ 1121e57b2d0eSRandall Stewart tp->t_in_pkt = le->m_head; 1122e57b2d0eSRandall Stewart tp->t_tail_pkt = le->m_last_mbuf; 1123e57b2d0eSRandall Stewart } else { 1124e57b2d0eSRandall Stewart /* Already some there */ 1125e57b2d0eSRandall Stewart tp->t_tail_pkt->m_nextpkt = le->m_head; 1126e57b2d0eSRandall Stewart tp->t_tail_pkt = le->m_last_mbuf; 1127e57b2d0eSRandall Stewart } 1128e57b2d0eSRandall Stewart le->m_head = NULL; 1129e57b2d0eSRandall Stewart le->m_last_mbuf = NULL; 1130e57b2d0eSRandall Stewart } 1131e57b2d0eSRandall Stewart 113269a34e8dSRandall Stewart static struct mbuf * 11339ca874cfSHans Petter Selasky tcp_lro_get_last_if_ackcmp(struct lro_ctrl *lc, struct lro_entry *le, 11349ca874cfSHans Petter Selasky struct inpcb *inp, int32_t *new_m) 1135e57b2d0eSRandall Stewart { 113669a34e8dSRandall Stewart struct tcpcb *tp; 11379ca874cfSHans Petter Selasky struct mbuf *m; 1138e57b2d0eSRandall Stewart 113969a34e8dSRandall Stewart tp = intotcpcb(inp); 11409ca874cfSHans Petter Selasky if (__predict_false(tp == NULL)) 11419ca874cfSHans Petter Selasky return (NULL); 11429ca874cfSHans Petter Selasky 114369a34e8dSRandall Stewart /* Look at the last mbuf if any in queue */ 114469a34e8dSRandall Stewart m = tp->t_tail_pkt; 11459ca874cfSHans Petter Selasky if (m != NULL && (m->m_flags & M_ACKCMP) != 0) { 11469ca874cfSHans Petter Selasky if (M_TRAILINGSPACE(m) >= sizeof(struct tcp_ackent)) { 11479ca874cfSHans Petter Selasky tcp_lro_log(tp, lc, le, NULL, 23, 0, 0, 0, 0); 11489ca874cfSHans Petter Selasky *new_m = 0; 11499ca874cfSHans Petter Selasky counter_u64_add(tcp_extra_mbuf, 1); 11509ca874cfSHans Petter Selasky return (m); 115169a34e8dSRandall Stewart } else { 11529ca874cfSHans Petter Selasky /* Mark we ran out of space */ 115369a34e8dSRandall Stewart inp->inp_flags2 |= INP_MBUF_L_ACKS; 115469a34e8dSRandall Stewart } 115569a34e8dSRandall Stewart } 11569ca874cfSHans Petter Selasky /* Decide mbuf size. */ 11579ca874cfSHans Petter Selasky if (inp->inp_flags2 & INP_MBUF_L_ACKS) 11589ca874cfSHans Petter Selasky m = m_getcl(M_NOWAIT, MT_DATA, M_ACKCMP | M_PKTHDR); 11599ca874cfSHans Petter Selasky else 11609ca874cfSHans Petter Selasky m = m_gethdr(M_NOWAIT, MT_DATA); 11619ca874cfSHans Petter Selasky 11629ca874cfSHans Petter Selasky if (__predict_false(m == NULL)) { 11639ca874cfSHans Petter Selasky counter_u64_add(tcp_would_have_but, 1); 11649ca874cfSHans Petter Selasky return (NULL); 116569a34e8dSRandall Stewart } 11669ca874cfSHans Petter Selasky counter_u64_add(tcp_comp_total, 1); 11679ca874cfSHans Petter Selasky m->m_flags |= M_ACKCMP; 11689ca874cfSHans Petter Selasky *new_m = 1; 116969a34e8dSRandall Stewart return (m); 117069a34e8dSRandall Stewart } 117169a34e8dSRandall Stewart 117269a34e8dSRandall Stewart static struct inpcb * 11739ca874cfSHans Petter Selasky tcp_lro_lookup(struct ifnet *ifp, struct lro_parser *pa) 117469a34e8dSRandall Stewart { 11759ca874cfSHans Petter Selasky struct inpcb *inp; 117669a34e8dSRandall Stewart 117769a34e8dSRandall Stewart NET_EPOCH_ASSERT(); 11789ca874cfSHans Petter Selasky 11799ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 1180e57b2d0eSRandall Stewart #ifdef INET6 11819ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 11829ca874cfSHans Petter Selasky inp = in6_pcblookup(&V_tcbinfo, 11839ca874cfSHans Petter Selasky &pa->data.s_addr.v6, 11849ca874cfSHans Petter Selasky pa->data.s_port, 11859ca874cfSHans Petter Selasky &pa->data.d_addr.v6, 11869ca874cfSHans Petter Selasky pa->data.d_port, 1187e57b2d0eSRandall Stewart INPLOOKUP_WLOCKPCB, 11889ca874cfSHans Petter Selasky ifp); 1189e57b2d0eSRandall Stewart break; 1190e57b2d0eSRandall Stewart #endif 1191e57b2d0eSRandall Stewart #ifdef INET 11929ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 11939ca874cfSHans Petter Selasky inp = in_pcblookup(&V_tcbinfo, 11949ca874cfSHans Petter Selasky pa->data.s_addr.v4, 11959ca874cfSHans Petter Selasky pa->data.s_port, 11969ca874cfSHans Petter Selasky pa->data.d_addr.v4, 11979ca874cfSHans Petter Selasky pa->data.d_port, 1198e57b2d0eSRandall Stewart INPLOOKUP_WLOCKPCB, 11999ca874cfSHans Petter Selasky ifp); 1200e57b2d0eSRandall Stewart break; 1201e57b2d0eSRandall Stewart #endif 12029ca874cfSHans Petter Selasky default: 12039ca874cfSHans Petter Selasky inp = NULL; 12049ca874cfSHans Petter Selasky break; 1205e57b2d0eSRandall Stewart } 120669a34e8dSRandall Stewart return (inp); 120769a34e8dSRandall Stewart } 120869a34e8dSRandall Stewart 12099ca874cfSHans Petter Selasky static inline bool 12109ca874cfSHans Petter Selasky tcp_lro_ack_valid(struct mbuf *m, struct tcphdr *th, uint32_t **ppts, bool *other_opts) 12119ca874cfSHans Petter Selasky { 12129ca874cfSHans Petter Selasky /* 12139ca874cfSHans Petter Selasky * This function returns two bits of valuable information. 12149ca874cfSHans Petter Selasky * a) Is what is present capable of being ack-compressed, 12159ca874cfSHans Petter Selasky * we can ack-compress if there is no options or just 12169ca874cfSHans Petter Selasky * a timestamp option, and of course the th_flags must 12179ca874cfSHans Petter Selasky * be correct as well. 12189ca874cfSHans Petter Selasky * b) Our other options present such as SACK. This is 12199ca874cfSHans Petter Selasky * used to determine if we want to wakeup or not. 12209ca874cfSHans Petter Selasky */ 12219ca874cfSHans Petter Selasky bool ret = true; 12229ca874cfSHans Petter Selasky 12239ca874cfSHans Petter Selasky switch (th->th_off << 2) { 12249ca874cfSHans Petter Selasky case (sizeof(*th) + TCPOLEN_TSTAMP_APPA): 12259ca874cfSHans Petter Selasky *ppts = (uint32_t *)(th + 1); 12269ca874cfSHans Petter Selasky /* Check if we have only one timestamp option. */ 12279ca874cfSHans Petter Selasky if (**ppts == TCP_LRO_TS_OPTION) 12289ca874cfSHans Petter Selasky *other_opts = false; 12299ca874cfSHans Petter Selasky else { 12309ca874cfSHans Petter Selasky *other_opts = true; 12319ca874cfSHans Petter Selasky ret = false; 12329ca874cfSHans Petter Selasky } 12339ca874cfSHans Petter Selasky break; 12349ca874cfSHans Petter Selasky case (sizeof(*th)): 12359ca874cfSHans Petter Selasky /* No options. */ 12369ca874cfSHans Petter Selasky *ppts = NULL; 12379ca874cfSHans Petter Selasky *other_opts = false; 12389ca874cfSHans Petter Selasky break; 12399ca874cfSHans Petter Selasky default: 12409ca874cfSHans Petter Selasky *ppts = NULL; 12419ca874cfSHans Petter Selasky *other_opts = true; 12429ca874cfSHans Petter Selasky ret = false; 12439ca874cfSHans Petter Selasky break; 12449ca874cfSHans Petter Selasky } 12459ca874cfSHans Petter Selasky /* For ACKCMP we only accept ACK, PUSH, ECE and CWR. */ 12469ca874cfSHans Petter Selasky if ((th->th_flags & ~(TH_ACK | TH_PUSH | TH_ECE | TH_CWR)) != 0) 12479ca874cfSHans Petter Selasky ret = false; 12489ca874cfSHans Petter Selasky /* If it has data on it we cannot compress it */ 12499ca874cfSHans Petter Selasky if (m->m_pkthdr.lro_tcp_d_len) 12509ca874cfSHans Petter Selasky ret = false; 12519ca874cfSHans Petter Selasky 12529ca874cfSHans Petter Selasky /* ACK flag must be set. */ 12539ca874cfSHans Petter Selasky if (!(th->th_flags & TH_ACK)) 12549ca874cfSHans Petter Selasky ret = false; 12559ca874cfSHans Petter Selasky return (ret); 12569ca874cfSHans Petter Selasky } 12579ca874cfSHans Petter Selasky 12589ca874cfSHans Petter Selasky static int 12599ca874cfSHans Petter Selasky tcp_lro_flush_tcphpts(struct lro_ctrl *lc, struct lro_entry *le) 12609ca874cfSHans Petter Selasky { 12619ca874cfSHans Petter Selasky struct inpcb *inp; 12629ca874cfSHans Petter Selasky struct tcpcb *tp; 12639ca874cfSHans Petter Selasky struct mbuf **pp, *cmp, *mv_to; 12649ca874cfSHans Petter Selasky bool bpf_req, should_wake; 12659ca874cfSHans Petter Selasky 12669ca874cfSHans Petter Selasky /* Check if packet doesn't belongs to our network interface. */ 12679ca874cfSHans Petter Selasky if ((tcplro_stacks_wanting_mbufq == 0) || 12689ca874cfSHans Petter Selasky (le->outer.data.vlan_id != 0) || 12699ca874cfSHans Petter Selasky (le->inner.data.lro_type != LRO_TYPE_NONE)) 12709ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 12719ca874cfSHans Petter Selasky 12729ca874cfSHans Petter Selasky #ifdef INET6 12739ca874cfSHans Petter Selasky /* 12749ca874cfSHans Petter Selasky * Be proactive about unspecified IPv6 address in source. As 12759ca874cfSHans Petter Selasky * we use all-zero to indicate unbounded/unconnected pcb, 12769ca874cfSHans Petter Selasky * unspecified IPv6 address can be used to confuse us. 12779ca874cfSHans Petter Selasky * 12789ca874cfSHans Petter Selasky * Note that packets with unspecified IPv6 destination is 12799ca874cfSHans Petter Selasky * already dropped in ip6_input. 12809ca874cfSHans Petter Selasky */ 12819ca874cfSHans Petter Selasky if (__predict_false(le->outer.data.lro_type == LRO_TYPE_IPV6_TCP && 12829ca874cfSHans Petter Selasky IN6_IS_ADDR_UNSPECIFIED(&le->outer.data.s_addr.v6))) 12839ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 12849ca874cfSHans Petter Selasky 12859ca874cfSHans Petter Selasky if (__predict_false(le->inner.data.lro_type == LRO_TYPE_IPV6_TCP && 12869ca874cfSHans Petter Selasky IN6_IS_ADDR_UNSPECIFIED(&le->inner.data.s_addr.v6))) 12879ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 128869a34e8dSRandall Stewart #endif 12899ca874cfSHans Petter Selasky /* Lookup inp, if any. */ 12909ca874cfSHans Petter Selasky inp = tcp_lro_lookup(lc->ifp, 12919ca874cfSHans Petter Selasky (le->inner.data.lro_type == LRO_TYPE_NONE) ? &le->outer : &le->inner); 12929ca874cfSHans Petter Selasky if (inp == NULL) 12939ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 129469a34e8dSRandall Stewart 12959ca874cfSHans Petter Selasky counter_u64_add(tcp_inp_lro_locks_taken, 1); 12969ca874cfSHans Petter Selasky 12979ca874cfSHans Petter Selasky /* Get TCP control structure. */ 12989ca874cfSHans Petter Selasky tp = intotcpcb(inp); 12999ca874cfSHans Petter Selasky 13009ca874cfSHans Petter Selasky /* Check if the inp is dead, Jim. */ 13019ca874cfSHans Petter Selasky if (tp == NULL || 13029ca874cfSHans Petter Selasky (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) || 13039ca874cfSHans Petter Selasky (inp->inp_flags2 & INP_FREED)) { 13049ca874cfSHans Petter Selasky INP_WUNLOCK(inp); 13059ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 130669a34e8dSRandall Stewart } 1307d7955cc0SRandall Stewart if ((inp->inp_irq_cpu_set == 0) && (lc->lro_cpu_is_set == 1)) { 1308d7955cc0SRandall Stewart inp->inp_irq_cpu = lc->lro_last_cpu; 1309d7955cc0SRandall Stewart inp->inp_irq_cpu_set = 1; 1310d7955cc0SRandall Stewart } 13119ca874cfSHans Petter Selasky /* Check if the transport doesn't support the needed optimizations. */ 13129ca874cfSHans Petter Selasky if ((inp->inp_flags2 & (INP_SUPPORTS_MBUFQ | INP_MBUF_ACKCMP)) == 0) { 13139ca874cfSHans Petter Selasky INP_WUNLOCK(inp); 13149ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 131569a34e8dSRandall Stewart } 13169ca874cfSHans Petter Selasky 13179ca874cfSHans Petter Selasky if (inp->inp_flags2 & INP_MBUF_QUEUE_READY) 13189ca874cfSHans Petter Selasky should_wake = false; 13199ca874cfSHans Petter Selasky else 13209ca874cfSHans Petter Selasky should_wake = true; 13219ca874cfSHans Petter Selasky /* Check if packets should be tapped to BPF. */ 13229ca874cfSHans Petter Selasky bpf_req = bpf_peers_present(lc->ifp->if_bpf); 13239ca874cfSHans Petter Selasky 13249ca874cfSHans Petter Selasky /* Strip and compress all the incoming packets. */ 13259ca874cfSHans Petter Selasky cmp = NULL; 13269ca874cfSHans Petter Selasky for (pp = &le->m_head; *pp != NULL; ) { 13279ca874cfSHans Petter Selasky mv_to = NULL; 13289ca874cfSHans Petter Selasky if (do_bpf_strip_and_compress(inp, lc, le, pp, 13299ca874cfSHans Petter Selasky &cmp, &mv_to, &should_wake, bpf_req ) == false) { 13309ca874cfSHans Petter Selasky /* Advance to next mbuf. */ 13319ca874cfSHans Petter Selasky pp = &(*pp)->m_nextpkt; 13329ca874cfSHans Petter Selasky } else if (mv_to != NULL) { 13339ca874cfSHans Petter Selasky /* We are asked to move pp up */ 13349ca874cfSHans Petter Selasky pp = &mv_to->m_nextpkt; 13359ca874cfSHans Petter Selasky } 13369ca874cfSHans Petter Selasky } 13379ca874cfSHans Petter Selasky /* Update "m_last_mbuf", if any. */ 13389ca874cfSHans Petter Selasky if (pp == &le->m_head) 13399ca874cfSHans Petter Selasky le->m_last_mbuf = *pp; 13409ca874cfSHans Petter Selasky else 13419ca874cfSHans Petter Selasky le->m_last_mbuf = __containerof(pp, struct mbuf, m_nextpkt); 13429ca874cfSHans Petter Selasky 13439ca874cfSHans Petter Selasky /* Check if any data mbufs left. */ 13449ca874cfSHans Petter Selasky if (le->m_head != NULL) { 13459ca874cfSHans Petter Selasky counter_u64_add(tcp_inp_lro_direct_queue, 1); 13469ca874cfSHans Petter Selasky tcp_lro_log(tp, lc, le, NULL, 22, 1, 13479ca874cfSHans Petter Selasky inp->inp_flags2, inp->inp_in_input, 1); 13489ca874cfSHans Petter Selasky tcp_queue_pkts(inp, tp, le); 13499ca874cfSHans Petter Selasky } 13509ca874cfSHans Petter Selasky if (should_wake) { 13519ca874cfSHans Petter Selasky /* Wakeup */ 13529ca874cfSHans Petter Selasky counter_u64_add(tcp_inp_lro_wokeup_queue, 1); 13539ca874cfSHans Petter Selasky if ((*tp->t_fb->tfb_do_queued_segments)(inp->inp_socket, tp, 0)) 13549ca874cfSHans Petter Selasky inp = NULL; 13559ca874cfSHans Petter Selasky } 13569ca874cfSHans Petter Selasky if (inp != NULL) 13579ca874cfSHans Petter Selasky INP_WUNLOCK(inp); 13589ca874cfSHans Petter Selasky return (0); /* Success. */ 135969a34e8dSRandall Stewart } 136069a34e8dSRandall Stewart #endif 136169a34e8dSRandall Stewart 136269a34e8dSRandall Stewart void 136369a34e8dSRandall Stewart tcp_lro_flush(struct lro_ctrl *lc, struct lro_entry *le) 136469a34e8dSRandall Stewart { 13659ca874cfSHans Petter Selasky /* Only optimise if there are multiple packets waiting. */ 136669a34e8dSRandall Stewart #ifdef TCPHPTS 13679ca874cfSHans Petter Selasky int error; 136869a34e8dSRandall Stewart 136969a34e8dSRandall Stewart CURVNET_SET(lc->ifp->if_vnet); 13709ca874cfSHans Petter Selasky error = tcp_lro_flush_tcphpts(lc, le); 13719ca874cfSHans Petter Selasky CURVNET_RESTORE(); 13729ca874cfSHans Petter Selasky if (error != 0) { 13739ca874cfSHans Petter Selasky #endif 13749ca874cfSHans Petter Selasky tcp_lro_condense(lc, le); 13759ca874cfSHans Petter Selasky tcp_flush_out_entry(lc, le); 1376e57b2d0eSRandall Stewart #ifdef TCPHPTS 1377e57b2d0eSRandall Stewart } 1378e57b2d0eSRandall Stewart #endif 137962b5b6ecSBjoern A. Zeeb lc->lro_flushed++; 138062b5b6ecSBjoern A. Zeeb bzero(le, sizeof(*le)); 13811ea44822SSepherosa Ziehau LIST_INSERT_HEAD(&lc->lro_free, le, next); 138262b5b6ecSBjoern A. Zeeb } 13836c5087a8SJack F Vogel 1384fc271df3SHans Petter Selasky #ifdef HAVE_INLINE_FLSLL 1385fc271df3SHans Petter Selasky #define tcp_lro_msb_64(x) (1ULL << (flsll(x) - 1)) 1386fc271df3SHans Petter Selasky #else 1387fc271df3SHans Petter Selasky static inline uint64_t 1388fc271df3SHans Petter Selasky tcp_lro_msb_64(uint64_t x) 1389e936121dSHans Petter Selasky { 1390fc271df3SHans Petter Selasky x |= (x >> 1); 1391fc271df3SHans Petter Selasky x |= (x >> 2); 1392fc271df3SHans Petter Selasky x |= (x >> 4); 1393fc271df3SHans Petter Selasky x |= (x >> 8); 1394fc271df3SHans Petter Selasky x |= (x >> 16); 1395fc271df3SHans Petter Selasky x |= (x >> 32); 1396fc271df3SHans Petter Selasky return (x & ~(x >> 1)); 1397fc271df3SHans Petter Selasky } 1398fc271df3SHans Petter Selasky #endif 1399e936121dSHans Petter Selasky 1400fc271df3SHans Petter Selasky /* 1401fc271df3SHans Petter Selasky * The tcp_lro_sort() routine is comparable to qsort(), except it has 1402fc271df3SHans Petter Selasky * a worst case complexity limit of O(MIN(N,64)*N), where N is the 1403fc271df3SHans Petter Selasky * number of elements to sort and 64 is the number of sequence bits 1404fc271df3SHans Petter Selasky * available. The algorithm is bit-slicing the 64-bit sequence number, 1405fc271df3SHans Petter Selasky * sorting one bit at a time from the most significant bit until the 1406ec668905SHans Petter Selasky * least significant one, skipping the constant bits. This is 1407ec668905SHans Petter Selasky * typically called a radix sort. 1408fc271df3SHans Petter Selasky */ 1409fc271df3SHans Petter Selasky static void 1410fc271df3SHans Petter Selasky tcp_lro_sort(struct lro_mbuf_sort *parray, uint32_t size) 1411fc271df3SHans Petter Selasky { 1412fc271df3SHans Petter Selasky struct lro_mbuf_sort temp; 1413fc271df3SHans Petter Selasky uint64_t ones; 1414fc271df3SHans Petter Selasky uint64_t zeros; 1415fc271df3SHans Petter Selasky uint32_t x; 1416fc271df3SHans Petter Selasky uint32_t y; 1417e936121dSHans Petter Selasky 1418fc271df3SHans Petter Selasky repeat: 1419ec668905SHans Petter Selasky /* for small arrays insertion sort is faster */ 1420fc271df3SHans Petter Selasky if (size <= 12) { 1421ec668905SHans Petter Selasky for (x = 1; x < size; x++) { 1422fc271df3SHans Petter Selasky temp = parray[x]; 1423ec668905SHans Petter Selasky for (y = x; y > 0 && temp.seq < parray[y - 1].seq; y--) 1424ec668905SHans Petter Selasky parray[y] = parray[y - 1]; 1425fc271df3SHans Petter Selasky parray[y] = temp; 1426fc271df3SHans Petter Selasky } 1427fc271df3SHans Petter Selasky return; 1428fc271df3SHans Petter Selasky } 1429e936121dSHans Petter Selasky 1430fc271df3SHans Petter Selasky /* compute sequence bits which are constant */ 1431fc271df3SHans Petter Selasky ones = 0; 1432fc271df3SHans Petter Selasky zeros = 0; 1433fc271df3SHans Petter Selasky for (x = 0; x != size; x++) { 1434fc271df3SHans Petter Selasky ones |= parray[x].seq; 1435fc271df3SHans Petter Selasky zeros |= ~parray[x].seq; 1436fc271df3SHans Petter Selasky } 1437fc271df3SHans Petter Selasky 1438fc271df3SHans Petter Selasky /* compute bits which are not constant into "ones" */ 1439fc271df3SHans Petter Selasky ones &= zeros; 1440fc271df3SHans Petter Selasky if (ones == 0) 1441fc271df3SHans Petter Selasky return; 1442fc271df3SHans Petter Selasky 1443fc271df3SHans Petter Selasky /* pick the most significant bit which is not constant */ 1444fc271df3SHans Petter Selasky ones = tcp_lro_msb_64(ones); 1445fc271df3SHans Petter Selasky 1446fc271df3SHans Petter Selasky /* 1447fc271df3SHans Petter Selasky * Move entries having cleared sequence bits to the beginning 1448fc271df3SHans Petter Selasky * of the array: 1449fc271df3SHans Petter Selasky */ 1450fc271df3SHans Petter Selasky for (x = y = 0; y != size; y++) { 1451fc271df3SHans Petter Selasky /* skip set bits */ 1452fc271df3SHans Petter Selasky if (parray[y].seq & ones) 1453fc271df3SHans Petter Selasky continue; 1454fc271df3SHans Petter Selasky /* swap entries */ 1455fc271df3SHans Petter Selasky temp = parray[x]; 1456fc271df3SHans Petter Selasky parray[x] = parray[y]; 1457fc271df3SHans Petter Selasky parray[y] = temp; 1458fc271df3SHans Petter Selasky x++; 1459fc271df3SHans Petter Selasky } 1460fc271df3SHans Petter Selasky 1461fc271df3SHans Petter Selasky KASSERT(x != 0 && x != size, ("Memory is corrupted\n")); 1462fc271df3SHans Petter Selasky 1463fc271df3SHans Petter Selasky /* sort zeros */ 1464fc271df3SHans Petter Selasky tcp_lro_sort(parray, x); 1465fc271df3SHans Petter Selasky 1466fc271df3SHans Petter Selasky /* sort ones */ 1467fc271df3SHans Petter Selasky parray += x; 1468fc271df3SHans Petter Selasky size -= x; 1469fc271df3SHans Petter Selasky goto repeat; 1470e936121dSHans Petter Selasky } 1471e936121dSHans Petter Selasky 1472e936121dSHans Petter Selasky void 1473e936121dSHans Petter Selasky tcp_lro_flush_all(struct lro_ctrl *lc) 1474e936121dSHans Petter Selasky { 1475fc271df3SHans Petter Selasky uint64_t seq; 1476fc271df3SHans Petter Selasky uint64_t nseq; 1477e936121dSHans Petter Selasky unsigned x; 1478e936121dSHans Petter Selasky 1479e936121dSHans Petter Selasky /* check if no mbufs to flush */ 14806dd38b87SSepherosa Ziehau if (lc->lro_mbuf_count == 0) 1481e936121dSHans Petter Selasky goto done; 1482d7955cc0SRandall Stewart if (lc->lro_cpu_is_set == 0) { 1483d7955cc0SRandall Stewart if (lc->lro_last_cpu == curcpu) { 1484d7955cc0SRandall Stewart lc->lro_cnt_of_same_cpu++; 1485d7955cc0SRandall Stewart /* Have we reached the threshold to declare a cpu? */ 1486d7955cc0SRandall Stewart if (lc->lro_cnt_of_same_cpu > tcp_lro_cpu_set_thresh) 1487d7955cc0SRandall Stewart lc->lro_cpu_is_set = 1; 1488d7955cc0SRandall Stewart } else { 1489d7955cc0SRandall Stewart lc->lro_last_cpu = curcpu; 1490d7955cc0SRandall Stewart lc->lro_cnt_of_same_cpu = 0; 1491d7955cc0SRandall Stewart } 1492d7955cc0SRandall Stewart } 1493a9b66dbdSHans Petter Selasky CURVNET_SET(lc->ifp->if_vnet); 1494a9b66dbdSHans Petter Selasky 14959ca874cfSHans Petter Selasky /* get current time */ 1496b45daaeaSRandall Stewart binuptime(&lc->lro_last_queue_time); 14979ca874cfSHans Petter Selasky 1498e936121dSHans Petter Selasky /* sort all mbufs according to stream */ 1499fc271df3SHans Petter Selasky tcp_lro_sort(lc->lro_mbuf_data, lc->lro_mbuf_count); 1500e936121dSHans Petter Selasky 1501e936121dSHans Petter Selasky /* input data into LRO engine, stream by stream */ 1502fc271df3SHans Petter Selasky seq = 0; 1503e936121dSHans Petter Selasky for (x = 0; x != lc->lro_mbuf_count; x++) { 1504e936121dSHans Petter Selasky struct mbuf *mb; 1505e936121dSHans Petter Selasky 1506fc271df3SHans Petter Selasky /* get mbuf */ 1507fc271df3SHans Petter Selasky mb = lc->lro_mbuf_data[x].mb; 1508fc271df3SHans Petter Selasky 1509fc271df3SHans Petter Selasky /* get sequence number, masking away the packet index */ 1510fc271df3SHans Petter Selasky nseq = lc->lro_mbuf_data[x].seq & (-1ULL << 24); 1511e936121dSHans Petter Selasky 1512e936121dSHans Petter Selasky /* check for new stream */ 1513fc271df3SHans Petter Selasky if (seq != nseq) { 1514fc271df3SHans Petter Selasky seq = nseq; 1515e936121dSHans Petter Selasky 1516e936121dSHans Petter Selasky /* flush active streams */ 15176dd38b87SSepherosa Ziehau tcp_lro_rx_done(lc); 1518e936121dSHans Petter Selasky } 1519fc271df3SHans Petter Selasky 1520e936121dSHans Petter Selasky /* add packet to LRO engine */ 15219ca874cfSHans Petter Selasky if (tcp_lro_rx_common(lc, mb, 0, false) != 0) { 1522e936121dSHans Petter Selasky /* input packet to network layer */ 1523e936121dSHans Petter Selasky (*lc->ifp->if_input)(lc->ifp, mb); 1524e936121dSHans Petter Selasky lc->lro_queued++; 1525e936121dSHans Petter Selasky lc->lro_flushed++; 1526e936121dSHans Petter Selasky } 1527e936121dSHans Petter Selasky } 1528a9b66dbdSHans Petter Selasky CURVNET_RESTORE(); 1529e936121dSHans Petter Selasky done: 1530e936121dSHans Petter Selasky /* flush active streams */ 15316dd38b87SSepherosa Ziehau tcp_lro_rx_done(lc); 15326dd38b87SSepherosa Ziehau 1533d7955cc0SRandall Stewart #ifdef TCPHPTS 1534d7955cc0SRandall Stewart tcp_run_hpts(); 1535d7955cc0SRandall Stewart #endif 1536e936121dSHans Petter Selasky lc->lro_mbuf_count = 0; 153762b5b6ecSBjoern A. Zeeb } 15386c5087a8SJack F Vogel 1539ab4fad4bSRandall Stewart #ifdef TCPHPTS 154069a34e8dSRandall Stewart static void 15419ca874cfSHans Petter Selasky build_ack_entry(struct tcp_ackent *ae, struct tcphdr *th, struct mbuf *m, 15429ca874cfSHans Petter Selasky uint32_t *ts_ptr, uint16_t iptos) 154369a34e8dSRandall Stewart { 154469a34e8dSRandall Stewart /* 15459ca874cfSHans Petter Selasky * Given a TCP ACK, summarize it down into the small TCP ACK 15469ca874cfSHans Petter Selasky * entry. 154769a34e8dSRandall Stewart */ 154869a34e8dSRandall Stewart ae->timestamp = m->m_pkthdr.rcv_tstmp; 154969a34e8dSRandall Stewart if (m->m_flags & M_TSTMP_LRO) 155069a34e8dSRandall Stewart ae->flags = TSTMP_LRO; 155169a34e8dSRandall Stewart else if (m->m_flags & M_TSTMP) 155269a34e8dSRandall Stewart ae->flags = TSTMP_HDWR; 155369a34e8dSRandall Stewart ae->seq = ntohl(th->th_seq); 155469a34e8dSRandall Stewart ae->ack = ntohl(th->th_ack); 155569a34e8dSRandall Stewart ae->flags |= th->th_flags; 15569ca874cfSHans Petter Selasky if (ts_ptr != NULL) { 15579ca874cfSHans Petter Selasky ae->ts_value = ntohl(ts_ptr[1]); 15589ca874cfSHans Petter Selasky ae->ts_echo = ntohl(ts_ptr[2]); 155969a34e8dSRandall Stewart ae->flags |= HAS_TSTMP; 156069a34e8dSRandall Stewart } 156169a34e8dSRandall Stewart ae->win = ntohs(th->th_win); 156269a34e8dSRandall Stewart ae->codepoint = iptos; 156369a34e8dSRandall Stewart } 156469a34e8dSRandall Stewart 156569a34e8dSRandall Stewart /* 15669ca874cfSHans Petter Selasky * Do BPF tap for either ACK_CMP packets or MBUF QUEUE type packets 15679ca874cfSHans Petter Selasky * and strip all, but the IPv4/IPv6 header. 156869a34e8dSRandall Stewart */ 15699ca874cfSHans Petter Selasky static bool 15709ca874cfSHans Petter Selasky do_bpf_strip_and_compress(struct inpcb *inp, struct lro_ctrl *lc, 15719ca874cfSHans Petter Selasky struct lro_entry *le, struct mbuf **pp, struct mbuf **cmp, struct mbuf **mv_to, 15729ca874cfSHans Petter Selasky bool *should_wake, bool bpf_req) 15739ca874cfSHans Petter Selasky { 15749ca874cfSHans Petter Selasky union { 15759ca874cfSHans Petter Selasky void *ptr; 15769ca874cfSHans Petter Selasky struct ip *ip4; 15779ca874cfSHans Petter Selasky struct ip6_hdr *ip6; 15789ca874cfSHans Petter Selasky } l3; 15799ca874cfSHans Petter Selasky struct mbuf *m; 15809ca874cfSHans Petter Selasky struct mbuf *nm; 158169a34e8dSRandall Stewart struct tcphdr *th; 15829ca874cfSHans Petter Selasky struct tcp_ackent *ack_ent; 15839ca874cfSHans Petter Selasky uint32_t *ts_ptr; 15849ca874cfSHans Petter Selasky int32_t n_mbuf; 15859ca874cfSHans Petter Selasky bool other_opts, can_compress; 15869ca874cfSHans Petter Selasky uint16_t lro_type; 15879ca874cfSHans Petter Selasky uint16_t iptos; 15889ca874cfSHans Petter Selasky int tcp_hdr_offset; 15899ca874cfSHans Petter Selasky int idx; 159069a34e8dSRandall Stewart 15919ca874cfSHans Petter Selasky /* Get current mbuf. */ 15929ca874cfSHans Petter Selasky m = *pp; 159369a34e8dSRandall Stewart 159469a34e8dSRandall Stewart /* Let the BPF see the packet */ 15959ca874cfSHans Petter Selasky if (__predict_false(bpf_req)) 159669a34e8dSRandall Stewart ETHER_BPF_MTAP(lc->ifp, m); 15979ca874cfSHans Petter Selasky 15989ca874cfSHans Petter Selasky tcp_hdr_offset = m->m_pkthdr.lro_tcp_h_off; 15999ca874cfSHans Petter Selasky lro_type = le->inner.data.lro_type; 16009ca874cfSHans Petter Selasky switch (lro_type) { 16019ca874cfSHans Petter Selasky case LRO_TYPE_NONE: 16029ca874cfSHans Petter Selasky lro_type = le->outer.data.lro_type; 16039ca874cfSHans Petter Selasky switch (lro_type) { 16049ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 16059ca874cfSHans Petter Selasky tcp_hdr_offset -= sizeof(*le->outer.ip4); 16069ca874cfSHans Petter Selasky m->m_pkthdr.lro_etype = ETHERTYPE_IP; 16079ca874cfSHans Petter Selasky break; 16089ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 16099ca874cfSHans Petter Selasky tcp_hdr_offset -= sizeof(*le->outer.ip6); 16109ca874cfSHans Petter Selasky m->m_pkthdr.lro_etype = ETHERTYPE_IPV6; 16119ca874cfSHans Petter Selasky break; 16129ca874cfSHans Petter Selasky default: 16139ca874cfSHans Petter Selasky goto compressed; 16149ca874cfSHans Petter Selasky } 16159ca874cfSHans Petter Selasky break; 16169ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 16179ca874cfSHans Petter Selasky tcp_hdr_offset -= sizeof(*le->outer.ip4); 16189ca874cfSHans Petter Selasky m->m_pkthdr.lro_etype = ETHERTYPE_IP; 16199ca874cfSHans Petter Selasky break; 16209ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 16219ca874cfSHans Petter Selasky tcp_hdr_offset -= sizeof(*le->outer.ip6); 16229ca874cfSHans Petter Selasky m->m_pkthdr.lro_etype = ETHERTYPE_IPV6; 16239ca874cfSHans Petter Selasky break; 16249ca874cfSHans Petter Selasky default: 16259ca874cfSHans Petter Selasky goto compressed; 16269ca874cfSHans Petter Selasky } 16279ca874cfSHans Petter Selasky 16289ca874cfSHans Petter Selasky MPASS(tcp_hdr_offset >= 0); 16299ca874cfSHans Petter Selasky 16309ca874cfSHans Petter Selasky m_adj(m, tcp_hdr_offset); 163169a34e8dSRandall Stewart m->m_flags |= M_LRO_EHDRSTRP; 16329ca874cfSHans Petter Selasky m->m_flags &= ~M_ACKCMP; 16339ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_h_off -= tcp_hdr_offset; 163469a34e8dSRandall Stewart 16359ca874cfSHans Petter Selasky th = tcp_lro_get_th(m); 163669a34e8dSRandall Stewart 16379ca874cfSHans Petter Selasky th->th_sum = 0; /* TCP checksum is valid. */ 163869a34e8dSRandall Stewart 16399ca874cfSHans Petter Selasky /* Check if ACK can be compressed */ 16409ca874cfSHans Petter Selasky can_compress = tcp_lro_ack_valid(m, th, &ts_ptr, &other_opts); 164169a34e8dSRandall Stewart 16429ca874cfSHans Petter Selasky /* Now lets look at the should wake states */ 16439ca874cfSHans Petter Selasky if ((other_opts == true) && 16449ca874cfSHans Petter Selasky ((inp->inp_flags2 & INP_DONT_SACK_QUEUE) == 0)) { 164569a34e8dSRandall Stewart /* 16469ca874cfSHans Petter Selasky * If there are other options (SACK?) and the 16479ca874cfSHans Petter Selasky * tcp endpoint has not expressly told us it does 16489ca874cfSHans Petter Selasky * not care about SACKS, then we should wake up. 164969a34e8dSRandall Stewart */ 16509ca874cfSHans Petter Selasky *should_wake = true; 165169a34e8dSRandall Stewart } 16529ca874cfSHans Petter Selasky /* Is the ack compressable? */ 16539ca874cfSHans Petter Selasky if (can_compress == false) 16549ca874cfSHans Petter Selasky goto done; 16559ca874cfSHans Petter Selasky /* Does the TCP endpoint support ACK compression? */ 16569ca874cfSHans Petter Selasky if ((inp->inp_flags2 & INP_MBUF_ACKCMP) == 0) 16579ca874cfSHans Petter Selasky goto done; 16589ca874cfSHans Petter Selasky 16599ca874cfSHans Petter Selasky /* Lets get the TOS/traffic class field */ 16609ca874cfSHans Petter Selasky l3.ptr = mtod(m, void *); 16619ca874cfSHans Petter Selasky switch (lro_type) { 16629ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 16639ca874cfSHans Petter Selasky iptos = l3.ip4->ip_tos; 16649ca874cfSHans Petter Selasky break; 16659ca874cfSHans Petter Selasky case LRO_TYPE_IPV6_TCP: 16669ca874cfSHans Petter Selasky iptos = IPV6_TRAFFIC_CLASS(l3.ip6); 16679ca874cfSHans Petter Selasky break; 16689ca874cfSHans Petter Selasky default: 16699ca874cfSHans Petter Selasky iptos = 0; /* Keep compiler happy. */ 167069a34e8dSRandall Stewart break; 167169a34e8dSRandall Stewart } 16729ca874cfSHans Petter Selasky /* Now lets get space if we don't have some already */ 16739ca874cfSHans Petter Selasky if (*cmp == NULL) { 16749ca874cfSHans Petter Selasky new_one: 16759ca874cfSHans Petter Selasky nm = tcp_lro_get_last_if_ackcmp(lc, le, inp, &n_mbuf); 16769ca874cfSHans Petter Selasky if (__predict_false(nm == NULL)) 16779ca874cfSHans Petter Selasky goto done; 16789ca874cfSHans Petter Selasky *cmp = nm; 16799ca874cfSHans Petter Selasky if (n_mbuf) { 168069a34e8dSRandall Stewart /* 16819ca874cfSHans Petter Selasky * Link in the new cmp ack to our in-order place, 16829ca874cfSHans Petter Selasky * first set our cmp ack's next to where we are. 168369a34e8dSRandall Stewart */ 16849ca874cfSHans Petter Selasky nm->m_nextpkt = m; 16859ca874cfSHans Petter Selasky (*pp) = nm; 16869ca874cfSHans Petter Selasky /* 16879ca874cfSHans Petter Selasky * Set it up so mv_to is advanced to our 16889ca874cfSHans Petter Selasky * compressed ack. This way the caller can 16899ca874cfSHans Petter Selasky * advance pp to the right place. 16909ca874cfSHans Petter Selasky */ 16919ca874cfSHans Petter Selasky *mv_to = nm; 16929ca874cfSHans Petter Selasky /* 16939ca874cfSHans Petter Selasky * Advance it here locally as well. 16949ca874cfSHans Petter Selasky */ 16959ca874cfSHans Petter Selasky pp = &nm->m_nextpkt; 169669a34e8dSRandall Stewart } 16979ca874cfSHans Petter Selasky } else { 16989ca874cfSHans Petter Selasky /* We have one already we are working on */ 16999ca874cfSHans Petter Selasky nm = *cmp; 17009ca874cfSHans Petter Selasky if (M_TRAILINGSPACE(nm) < sizeof(struct tcp_ackent)) { 17019ca874cfSHans Petter Selasky /* We ran out of space */ 17029ca874cfSHans Petter Selasky inp->inp_flags2 |= INP_MBUF_L_ACKS; 17039ca874cfSHans Petter Selasky goto new_one; 17049ca874cfSHans Petter Selasky } 17059ca874cfSHans Petter Selasky } 17069ca874cfSHans Petter Selasky MPASS(M_TRAILINGSPACE(nm) >= sizeof(struct tcp_ackent)); 17079ca874cfSHans Petter Selasky counter_u64_add(tcp_inp_lro_compressed, 1); 17089ca874cfSHans Petter Selasky le->compressed++; 17099ca874cfSHans Petter Selasky /* We can add in to the one on the tail */ 17109ca874cfSHans Petter Selasky ack_ent = mtod(nm, struct tcp_ackent *); 17119ca874cfSHans Petter Selasky idx = (nm->m_len / sizeof(struct tcp_ackent)); 17129ca874cfSHans Petter Selasky build_ack_entry(&ack_ent[idx], th, m, ts_ptr, iptos); 171369a34e8dSRandall Stewart 17149ca874cfSHans Petter Selasky /* Bump the size of both pkt-hdr and len */ 17159ca874cfSHans Petter Selasky nm->m_len += sizeof(struct tcp_ackent); 17169ca874cfSHans Petter Selasky nm->m_pkthdr.len += sizeof(struct tcp_ackent); 17179ca874cfSHans Petter Selasky compressed: 17189ca874cfSHans Petter Selasky /* Advance to next mbuf before freeing. */ 17199ca874cfSHans Petter Selasky *pp = m->m_nextpkt; 17209ca874cfSHans Petter Selasky m->m_nextpkt = NULL; 172169a34e8dSRandall Stewart m_freem(m); 17229ca874cfSHans Petter Selasky return (true); 17239ca874cfSHans Petter Selasky done: 17249ca874cfSHans Petter Selasky counter_u64_add(tcp_uncomp_total, 1); 17259ca874cfSHans Petter Selasky le->uncompressed++; 17269ca874cfSHans Petter Selasky return (false); 172769a34e8dSRandall Stewart } 172869a34e8dSRandall Stewart #endif 17299ca874cfSHans Petter Selasky 17309ca874cfSHans Petter Selasky static struct lro_head * 17319ca874cfSHans Petter Selasky tcp_lro_rx_get_bucket(struct lro_ctrl *lc, struct mbuf *m, struct lro_parser *parser) 17329ca874cfSHans Petter Selasky { 17339ca874cfSHans Petter Selasky u_long hash; 17349ca874cfSHans Petter Selasky 17359ca874cfSHans Petter Selasky if (M_HASHTYPE_ISHASH(m)) { 17369ca874cfSHans Petter Selasky hash = m->m_pkthdr.flowid; 17379ca874cfSHans Petter Selasky } else { 17389ca874cfSHans Petter Selasky for (unsigned i = hash = 0; i != LRO_RAW_ADDRESS_MAX; i++) 17399ca874cfSHans Petter Selasky hash += parser->data.raw[i]; 174069a34e8dSRandall Stewart } 17419ca874cfSHans Petter Selasky return (&lc->lro_hash[hash % lc->lro_hashsz]); 17429ca874cfSHans Petter Selasky } 174369a34e8dSRandall Stewart 174405cde7efSSepherosa Ziehau static int 17459ca874cfSHans Petter Selasky tcp_lro_rx_common(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum, bool use_hash) 174662b5b6ecSBjoern A. Zeeb { 17479ca874cfSHans Petter Selasky struct lro_parser pi; /* inner address data */ 17489ca874cfSHans Petter Selasky struct lro_parser po; /* outer address data */ 17499ca874cfSHans Petter Selasky struct lro_parser *pa; /* current parser for TCP stream */ 175062b5b6ecSBjoern A. Zeeb struct lro_entry *le; 175105cde7efSSepherosa Ziehau struct lro_head *bucket; 17529ca874cfSHans Petter Selasky struct tcphdr *th; 17539ca874cfSHans Petter Selasky int tcp_data_len; 17549ca874cfSHans Petter Selasky int tcp_opt_len; 17559ca874cfSHans Petter Selasky int error; 17569ca874cfSHans Petter Selasky uint16_t tcp_data_sum; 17576c5087a8SJack F Vogel 17589ca874cfSHans Petter Selasky #ifdef INET 17599ca874cfSHans Petter Selasky /* Quickly decide if packet cannot be LRO'ed */ 17609ca874cfSHans Petter Selasky if (__predict_false(V_ipforwarding != 0)) 17619ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 17629ca874cfSHans Petter Selasky #endif 17639ca874cfSHans Petter Selasky #ifdef INET6 17649ca874cfSHans Petter Selasky /* Quickly decide if packet cannot be LRO'ed */ 17659ca874cfSHans Petter Selasky if (__predict_false(V_ip6_forwarding != 0)) 17669ca874cfSHans Petter Selasky return (TCP_LRO_CANNOT); 17679ca874cfSHans Petter Selasky #endif 1768ca1a7e10SRandall Stewart if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1769ca1a7e10SRandall Stewart ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1770ca1a7e10SRandall Stewart (m->m_pkthdr.csum_data != 0xffff)) { 1771ca1a7e10SRandall Stewart /* 1772ca1a7e10SRandall Stewart * The checksum either did not have hardware offload 1773ca1a7e10SRandall Stewart * or it was a bad checksum. We can't LRO such 1774ca1a7e10SRandall Stewart * a packet. 1775ca1a7e10SRandall Stewart */ 1776ca1a7e10SRandall Stewart counter_u64_add(tcp_bad_csums, 1); 1777ca1a7e10SRandall Stewart return (TCP_LRO_CANNOT); 1778ca1a7e10SRandall Stewart } 177962b5b6ecSBjoern A. Zeeb /* We expect a contiguous header [eh, ip, tcp]. */ 17809ca874cfSHans Petter Selasky pa = tcp_lro_parser(m, &po, &pi, true); 17819ca874cfSHans Petter Selasky if (__predict_false(pa == NULL)) 17829ca874cfSHans Petter Selasky return (TCP_LRO_NOT_SUPPORTED); 17839ca874cfSHans Petter Selasky 17849ca874cfSHans Petter Selasky /* We don't expect any padding. */ 17859ca874cfSHans Petter Selasky error = tcp_lro_trim_mbuf_chain(m, pa); 17869ca874cfSHans Petter Selasky if (__predict_false(error != 0)) 17879ca874cfSHans Petter Selasky return (error); 17889ca874cfSHans Petter Selasky 17899ca874cfSHans Petter Selasky #ifdef INET 17909ca874cfSHans Petter Selasky switch (pa->data.lro_type) { 17919ca874cfSHans Petter Selasky case LRO_TYPE_IPV4_TCP: 17929ca874cfSHans Petter Selasky error = tcp_lro_rx_ipv4(lc, m, pa->ip4); 17939ca874cfSHans Petter Selasky if (__predict_false(error != 0)) 17949ca874cfSHans Petter Selasky return (error); 17959ca874cfSHans Petter Selasky break; 17969ca874cfSHans Petter Selasky default: 17979ca874cfSHans Petter Selasky break; 17989ca874cfSHans Petter Selasky } 17999ca874cfSHans Petter Selasky #endif 18009ca874cfSHans Petter Selasky /* If no hardware or arrival stamp on the packet add timestamp */ 1801e57b2d0eSRandall Stewart if ((m->m_flags & (M_TSTMP_LRO | M_TSTMP)) == 0) { 1802b45daaeaSRandall Stewart m->m_pkthdr.rcv_tstmp = bintime2ns(&lc->lro_last_queue_time); 1803e57b2d0eSRandall Stewart m->m_flags |= M_TSTMP_LRO; 1804e57b2d0eSRandall Stewart } 18056c5087a8SJack F Vogel 18069ca874cfSHans Petter Selasky /* Get pointer to TCP header. */ 18079ca874cfSHans Petter Selasky th = pa->tcp; 18089ca874cfSHans Petter Selasky 18099ca874cfSHans Petter Selasky /* Don't process SYN packets. */ 18109ca874cfSHans Petter Selasky if (__predict_false(th->th_flags & TH_SYN)) 181162b5b6ecSBjoern A. Zeeb return (TCP_LRO_CANNOT); 181262b5b6ecSBjoern A. Zeeb 18139ca874cfSHans Petter Selasky /* Get total TCP header length and compute payload length. */ 18149ca874cfSHans Petter Selasky tcp_opt_len = (th->th_off << 2); 18159ca874cfSHans Petter Selasky tcp_data_len = m->m_pkthdr.len - ((uint8_t *)th - 18169ca874cfSHans Petter Selasky (uint8_t *)m->m_data) - tcp_opt_len; 18179ca874cfSHans Petter Selasky tcp_opt_len -= sizeof(*th); 18189ca874cfSHans Petter Selasky 18199ca874cfSHans Petter Selasky /* Don't process invalid TCP headers. */ 18209ca874cfSHans Petter Selasky if (__predict_false(tcp_opt_len < 0 || tcp_data_len < 0)) 182162b5b6ecSBjoern A. Zeeb return (TCP_LRO_CANNOT); 18229ca874cfSHans Petter Selasky 18239ca874cfSHans Petter Selasky /* Compute TCP data only checksum. */ 18249ca874cfSHans Petter Selasky if (tcp_data_len == 0) 18259ca874cfSHans Petter Selasky tcp_data_sum = 0; /* no data, no checksum */ 18269ca874cfSHans Petter Selasky else if (__predict_false(csum != 0)) 18279ca874cfSHans Petter Selasky tcp_data_sum = tcp_lro_rx_csum_data(pa, ~csum); 18289ca874cfSHans Petter Selasky else 18299ca874cfSHans Petter Selasky tcp_data_sum = tcp_lro_rx_csum_data(pa, ~th->th_sum); 18309ca874cfSHans Petter Selasky 18319ca874cfSHans Petter Selasky /* Save TCP info in mbuf. */ 18329ca874cfSHans Petter Selasky m->m_nextpkt = NULL; 18339ca874cfSHans Petter Selasky m->m_pkthdr.rcvif = lc->ifp; 18349ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_csum = tcp_data_sum; 18359ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_d_len = tcp_data_len; 18369ca874cfSHans Petter Selasky m->m_pkthdr.lro_tcp_h_off = ((uint8_t *)th - (uint8_t *)m->m_data); 18379ca874cfSHans Petter Selasky m->m_pkthdr.lro_nsegs = 1; 18389ca874cfSHans Petter Selasky 18399ca874cfSHans Petter Selasky /* Get hash bucket. */ 184005cde7efSSepherosa Ziehau if (!use_hash) { 184105cde7efSSepherosa Ziehau bucket = &lc->lro_hash[0]; 184205cde7efSSepherosa Ziehau } else { 18439ca874cfSHans Petter Selasky bucket = tcp_lro_rx_get_bucket(lc, m, pa); 184405cde7efSSepherosa Ziehau } 184505cde7efSSepherosa Ziehau 184662b5b6ecSBjoern A. Zeeb /* Try to find a matching previous segment. */ 184705cde7efSSepherosa Ziehau LIST_FOREACH(le, bucket, hash_next) { 18489ca874cfSHans Petter Selasky /* Compare addresses and ports. */ 18499ca874cfSHans Petter Selasky if (lro_address_compare(&po.data, &le->outer.data) == false || 18509ca874cfSHans Petter Selasky lro_address_compare(&pi.data, &le->inner.data) == false) 185162b5b6ecSBjoern A. Zeeb continue; 18529ca874cfSHans Petter Selasky 18539ca874cfSHans Petter Selasky /* Check if no data and old ACK. */ 18549ca874cfSHans Petter Selasky if (tcp_data_len == 0 && 18559ca874cfSHans Petter Selasky SEQ_LT(ntohl(th->th_ack), ntohl(le->ack_seq))) { 1856d7fb35d1SSean Bruno m_freem(m); 1857d7fb35d1SSean Bruno return (0); 1858d7fb35d1SSean Bruno } 185969a34e8dSRandall Stewart 18609ca874cfSHans Petter Selasky /* Mark "m" in the last spot. */ 1861e57b2d0eSRandall Stewart le->m_last_mbuf->m_nextpkt = m; 18629ca874cfSHans Petter Selasky /* Now set the tail to "m". */ 1863e57b2d0eSRandall Stewart le->m_last_mbuf = m; 186462b5b6ecSBjoern A. Zeeb return (0); 18656c5087a8SJack F Vogel } 18669ca874cfSHans Petter Selasky 186762b5b6ecSBjoern A. Zeeb /* Try to find an empty slot. */ 18681ea44822SSepherosa Ziehau if (LIST_EMPTY(&lc->lro_free)) 1869489f0c3cSSepherosa Ziehau return (TCP_LRO_NO_ENTRIES); 187062b5b6ecSBjoern A. Zeeb 187162b5b6ecSBjoern A. Zeeb /* Start a new segment chain. */ 18721ea44822SSepherosa Ziehau le = LIST_FIRST(&lc->lro_free); 18731ea44822SSepherosa Ziehau LIST_REMOVE(le, next); 187405cde7efSSepherosa Ziehau tcp_lro_active_insert(lc, bucket, le); 187562b5b6ecSBjoern A. Zeeb 18769ca874cfSHans Petter Selasky /* Make sure the headers are set. */ 18779ca874cfSHans Petter Selasky le->inner = pi; 18789ca874cfSHans Petter Selasky le->outer = po; 187962b5b6ecSBjoern A. Zeeb 18809ca874cfSHans Petter Selasky /* Store time this entry was allocated. */ 18819ca874cfSHans Petter Selasky le->alloc_time = lc->lro_last_queue_time; 188269a34e8dSRandall Stewart 18839ca874cfSHans Petter Selasky tcp_set_entry_to_mbuf(lc, le, m, th); 188469a34e8dSRandall Stewart 18859ca874cfSHans Petter Selasky /* Now set the tail to "m". */ 1886e57b2d0eSRandall Stewart le->m_last_mbuf = m; 18879ca874cfSHans Petter Selasky 188862b5b6ecSBjoern A. Zeeb return (0); 188962b5b6ecSBjoern A. Zeeb } 189062b5b6ecSBjoern A. Zeeb 189105cde7efSSepherosa Ziehau int 189205cde7efSSepherosa Ziehau tcp_lro_rx(struct lro_ctrl *lc, struct mbuf *m, uint32_t csum) 189305cde7efSSepherosa Ziehau { 18949ca874cfSHans Petter Selasky int error; 189505cde7efSSepherosa Ziehau 1896ca1a7e10SRandall Stewart if (((m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) != 1897ca1a7e10SRandall Stewart ((CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) || 1898ca1a7e10SRandall Stewart (m->m_pkthdr.csum_data != 0xffff)) { 1899ca1a7e10SRandall Stewart /* 1900ca1a7e10SRandall Stewart * The checksum either did not have hardware offload 1901ca1a7e10SRandall Stewart * or it was a bad checksum. We can't LRO such 1902ca1a7e10SRandall Stewart * a packet. 1903ca1a7e10SRandall Stewart */ 1904ca1a7e10SRandall Stewart counter_u64_add(tcp_bad_csums, 1); 1905ca1a7e10SRandall Stewart return (TCP_LRO_CANNOT); 1906ca1a7e10SRandall Stewart } 19079ca874cfSHans Petter Selasky /* get current time */ 1908b45daaeaSRandall Stewart binuptime(&lc->lro_last_queue_time); 19099ca874cfSHans Petter Selasky CURVNET_SET(lc->ifp->if_vnet); 19109ca874cfSHans Petter Selasky error = tcp_lro_rx_common(lc, m, csum, true); 19119ca874cfSHans Petter Selasky CURVNET_RESTORE(); 19129ca874cfSHans Petter Selasky 19139ca874cfSHans Petter Selasky return (error); 191405cde7efSSepherosa Ziehau } 191505cde7efSSepherosa Ziehau 1916e936121dSHans Petter Selasky void 1917e936121dSHans Petter Selasky tcp_lro_queue_mbuf(struct lro_ctrl *lc, struct mbuf *mb) 1918e936121dSHans Petter Selasky { 1919e936121dSHans Petter Selasky /* sanity checks */ 1920e936121dSHans Petter Selasky if (__predict_false(lc->ifp == NULL || lc->lro_mbuf_data == NULL || 1921e936121dSHans Petter Selasky lc->lro_mbuf_max == 0)) { 1922e936121dSHans Petter Selasky /* packet drop */ 1923e936121dSHans Petter Selasky m_freem(mb); 1924e936121dSHans Petter Selasky return; 1925e936121dSHans Petter Selasky } 1926e936121dSHans Petter Selasky 1927e936121dSHans Petter Selasky /* check if packet is not LRO capable */ 1928ca1a7e10SRandall Stewart if (__predict_false((lc->ifp->if_capenable & IFCAP_LRO) == 0)) { 1929e936121dSHans Petter Selasky /* input packet to network layer */ 1930e936121dSHans Petter Selasky (*lc->ifp->if_input) (lc->ifp, mb); 1931e936121dSHans Petter Selasky return; 1932e936121dSHans Petter Selasky } 1933e936121dSHans Petter Selasky 1934fc271df3SHans Petter Selasky /* create sequence number */ 1935fc271df3SHans Petter Selasky lc->lro_mbuf_data[lc->lro_mbuf_count].seq = 1936fc271df3SHans Petter Selasky (((uint64_t)M_HASHTYPE_GET(mb)) << 56) | 1937fc271df3SHans Petter Selasky (((uint64_t)mb->m_pkthdr.flowid) << 24) | 1938fc271df3SHans Petter Selasky ((uint64_t)lc->lro_mbuf_count); 1939e936121dSHans Petter Selasky 1940e936121dSHans Petter Selasky /* enter mbuf */ 1941f8acc03eSNavdeep Parhar lc->lro_mbuf_data[lc->lro_mbuf_count].mb = mb; 1942f8acc03eSNavdeep Parhar 1943f8acc03eSNavdeep Parhar /* flush if array is full */ 1944f8acc03eSNavdeep Parhar if (__predict_false(++lc->lro_mbuf_count == lc->lro_mbuf_max)) 1945f8acc03eSNavdeep Parhar tcp_lro_flush_all(lc); 1946e936121dSHans Petter Selasky } 1947e936121dSHans Petter Selasky 194862b5b6ecSBjoern A. Zeeb /* end */ 1949